From 0c3e4cf680505c565580ab91a66da6b7b858ba56 Mon Sep 17 00:00:00 2001 From: duanyanhui <45005871+YanhuiDua@users.noreply.github.com> Date: Tue, 29 Aug 2023 17:02:53 +0800 Subject: [PATCH 001/194] [DCU] support cum & multinomial for dcu (#56612) * support cum & multinomial for dcu * rm commt --- paddle/phi/kernels/gpu/cum_grad_kernel.cu | 1 + paddle/phi/kernels/gpu/cum_kernel.cu | 1 + paddle/phi/kernels/gpu/multinomial_kernel.cu | 18 ++++++++++-------- python/paddle/tensor/random.py | 4 ---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/paddle/phi/kernels/gpu/cum_grad_kernel.cu b/paddle/phi/kernels/gpu/cum_grad_kernel.cu index 620d185475ef9..0e99305ac3127 100644 --- a/paddle/phi/kernels/gpu/cum_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/cum_grad_kernel.cu @@ -70,6 +70,7 @@ PD_REGISTER_KERNEL(cumsum_grad, phi::CumsumGradKernel, float, double, + phi::dtype::float16, int16_t, int, int64_t) {} diff --git a/paddle/phi/kernels/gpu/cum_kernel.cu b/paddle/phi/kernels/gpu/cum_kernel.cu index eb70b98f7ddca..c671d1f6b260e 100644 --- a/paddle/phi/kernels/gpu/cum_kernel.cu +++ b/paddle/phi/kernels/gpu/cum_kernel.cu @@ -435,6 +435,7 @@ PD_REGISTER_KERNEL(cumsum, ALL_LAYOUT, phi::CumsumKernel, float, + phi::dtype::float16, double, int16_t, int, diff --git a/paddle/phi/kernels/gpu/multinomial_kernel.cu b/paddle/phi/kernels/gpu/multinomial_kernel.cu index effc963cd0a3d..ba137b6fadc76 100644 --- a/paddle/phi/kernels/gpu/multinomial_kernel.cu +++ b/paddle/phi/kernels/gpu/multinomial_kernel.cu @@ -12,10 +12,6 @@ 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. */ -#ifndef PADDLE_WITH_HIP -// To-do(qili93): fix this after issue resolved -// https://github.com/ROCmSoftwarePlatform/rocPRIM/issues/202 - #include "paddle/phi/kernels/multinomial_kernel.h" #ifdef __NVCC__ @@ -107,14 +103,22 @@ __global__ void sampleMultinomialWithReplacement( size_t idx = gridDim.x * blockDim.x * blockIdx.y + blockDim.x * blockIdx.x + threadIdx.x; +#if defined(__NVCC__) curandStatePhilox4_32_10_t state; curand_init(seed, idx, offset, &state); +#else + hiprandStatePhilox4_32_10_t state; + hiprand_init(seed, idx, offset, &state); +#endif int sample = blockIdx.x * blockDim.x + threadIdx.x; for (int dist = blockIdx.y; dist < num_distributions; dist += gridDim.y) { if (sample < num_samples) { +#if defined(__NVCC__) T rng_number = static_cast(curand_uniform4(&state).x); - // Find the bucket that a uniform random number lies in +#else + T rng_number = static_cast(hiprand_uniform4(&state).x); +#endif int selected_category = binarySearchFunctor(cumulative_probs_data + dist * num_categories, norm_probs_data + dist * num_categories, @@ -283,7 +287,7 @@ void MultinomialKernel(const Context& dev_ctx, } // namespace phi -PD_REGISTER_KERNEL(multinomial, // cuda_only +PD_REGISTER_KERNEL(multinomial, GPU, ALL_LAYOUT, phi::MultinomialKernel, @@ -293,5 +297,3 @@ PD_REGISTER_KERNEL(multinomial, // cuda_only double) { kernel->OutputAt(0).SetDataType(phi::DataType::INT64); } - -#endif diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index b1c2d32c19b19..f92d714dccb0b 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -183,10 +183,6 @@ def multinomial(x, num_samples=1, replacement=False, name=None): """ - assert ( - not core.is_compiled_with_rocm() - ), "multinomial op is not supported on ROCM yet." - if in_dynamic_mode(): return _C_ops.multinomial(x, num_samples, replacement) else: From daac3829d87d2a96b6015c946bcf6fa82a690fd2 Mon Sep 17 00:00:00 2001 From: Sonder <55493212+AndSonder@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:58:04 +0800 Subject: [PATCH 002/194] Remove need_move_to_phi (#56371) * remove flag * open static build flag * add searchsorted to list * add register info for fused layernorm * fix fused_layernorm_kernel output registe info * fix stft registe info * add include * fix registe info * add skip fake init for fused_layernorm:residual_out * fix error * add distributed_fused_lamb_init to StaticBuildBlackList * set static_build flag to false --- .../new_executor/interpreter/static_build.cc | 81 +++++++++++++------ paddle/phi/infermeta/multiary.cc | 8 +- paddle/phi/kernels/cpu/stft_kernel.cc | 8 +- .../fusion/gpu/fused_layernorm_kernel.cu | 18 ++++- paddle/phi/kernels/gpu/c_identity_kernel.cu | 12 +++ paddle/phi/kernels/gpu/stft_kernel.cu | 4 +- paddle/phi/kernels/impl/stft_kernel_impl.h | 1 + 7 files changed, 93 insertions(+), 39 deletions(-) diff --git a/paddle/fluid/framework/new_executor/interpreter/static_build.cc b/paddle/fluid/framework/new_executor/interpreter/static_build.cc index ce988db26204c..a3c9802a7d35e 100644 --- a/paddle/fluid/framework/new_executor/interpreter/static_build.cc +++ b/paddle/fluid/framework/new_executor/interpreter/static_build.cc @@ -46,19 +46,11 @@ std::set StaticBuildBlackList = { "cinn_instruction_run" /*: to handle subgraph infermeta*/, "cinn_launch" /*: to handle subgraph infermeta*/, "run_program" /*: to handle scope output*/, - "sparse_sparse_coo_tensor" /*: to handle sparse output*/}; - -// TODO(lizhiyu): This operator list is only for pipeline strategy temporarily. -std::set SkipCheckForPipelineTempList = { - "c_broadcast", - "c_allreduce_sum", - "c_allgather", - "layer_norm", - "recv_v2", - "reshape2_grad", - "c_identity", - "c_reduce_sum", -}; + "sparse_sparse_coo_tensor" /*: to handle sparse output*/, + "shuffle_batch", + "shuffle_batch_grad", + "distributed_fused_lamb_init"}; + namespace paddle { namespace framework { namespace interpreter { @@ -70,15 +62,10 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { // use_mkldnn = (kernelCode >> 4) & 1 // has_fluid_kernel = (kernelCode >> 3) & 1 // has_structed_kernel = (kernelCode >> 2) & 1 - // need_move_to_phi = (kernelCode >> 1) & 1 using KernelCode = int8_t; std::set> invalid_ops; for (auto& op : block.AllOps()) { auto op_type = op->Type(); - if (SkipCheckForPipelineTempList.find(op_type) != - SkipCheckForPipelineTempList.end()) { - continue; - } const framework::OpInfo& info = OpInfoMap::Instance().Get(op_type); auto op_base = info.Creator()(op_type, op->Inputs(), op->Outputs(), op->GetAttrMap()); @@ -97,17 +84,16 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { bool has_fluid_kernel = OperatorWithKernel::AllOpKernels().count(op_type); bool has_structured_kernel = phi::KernelFactory::Instance().HasStructuredKernel(op_type); - bool need_move_to_phi = (has_fluid_kernel || has_structured_kernel); - KernelCode kernel_code = - (in_black_list << 7) + (is_operator_base << 6) + (is_custom_op << 5) + - (use_mkldnn << 4) + (has_fluid_kernel << 3) + - (has_structured_kernel << 2) + (need_move_to_phi << 1); + KernelCode kernel_code = (in_black_list << 7) + (is_operator_base << 6) + + (is_custom_op << 5) + (use_mkldnn << 4) + + (has_fluid_kernel << 3) + + (has_structured_kernel << 2); if (!OpsCanSkipedFakeAllocInStaticBuild.count(op_type)) { if (in_black_list || (is_operator_base && !OperatorBasesHandledInStaticBuild.count(op_type)) || - is_custom_op || use_mkldnn || need_move_to_phi) { + is_custom_op || use_mkldnn) { invalid_ops.insert(std::make_pair(op_type, kernel_code)); } } @@ -122,8 +108,7 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { << ", is_custom_op = " << (item.second >> 5 & 1) << ", use_mkldnn = " << (item.second >> 4 & 1) << ", has_fluid_kernel = " << (item.second >> 3 & 1) - << ", has_structed_kerenl = " << (item.second >> 2 & 1) - << ", need_move_to_phi = " << (item.second >> 1 & 1) << "]\n"; + << ", has_structed_kerenl = " << (item.second >> 2 & 1) << "]\n"; } VLOG(1) << ss.str(); } @@ -168,6 +153,27 @@ bool TensorShouldBeFakeInitialized(const OperatorBase& op, return false; } + if (op_type == "fused_bias_residual_layernorm" && + parameter_name == "residual_out") { + if (op.HasInputs("residual")) { + bool is_residual_empty = op.Input("residual") == kEmptyVarName; + bool is_norm_weight_empty = op.Input("norm_weight") == kEmptyVarName; + bool is_norm_bias_empty = op.Input("norm_bias") == kEmptyVarName; + if (!is_residual_empty) { + if (is_norm_weight_empty && is_norm_bias_empty) { + VLOG(2) << "Skip fake initialization for: " << parameter_name; + return false; + } + } else { + VLOG(2) << "Skip fake initialization for: " << parameter_name; + return false; + } + } else { + VLOG(2) << "Skip fake initialization for: " << parameter_name; + return false; + } + } + if (op_type == "fake_quantize_range_abs_max") { if (op.Attr("is_test") && (parameter_name == "OutScale" || parameter_name == "OutScales")) { @@ -375,6 +381,15 @@ phi::DataType GetInputDType(const RuntimeContext& runtime_ctx, return in_tensor->dtype(); } +bool InputExisted(const RuntimeContext& runtime_ctx, + const std::string& parameter_name) { + auto it = runtime_ctx.inputs.find(parameter_name); + if (it == runtime_ctx.inputs.end() || it->second.empty()) { + return false; + } + return true; +} + phi::DataType InferDTypeFromAttr(const framework::OperatorBase& op, const RuntimeContext& runtime_ctx, const std::string& attr_name) { @@ -497,6 +512,20 @@ void FakeInitializeOutputsForFunctionKernel( } else { dtype = DataType::INT64; } + } else if (op_type == "fused_bias_residual_layernorm") { + auto in_dtype = GetInputDType(runtime_ctx, "x"); + float quant_scale = op.Attr("quant_scale"); + if (InputExisted(runtime_ctx, "residual") && + !InputExisted(runtime_ctx, "norm_weight") && + !InputExisted(runtime_ctx, "norm_bias")) { + dtype = in_dtype; + } else { + if (quant_scale > 0.0f) { + dtype = DataType::INT8; + } else { + dtype = in_dtype; + } + } } else { VLOG(4) << "Get dtype result from InferMeta"; RuntimeInferShapeContext infer_shape_ctx(op, runtime_ctx); diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 4b01ddcabec2f..7c2e941bac2e0 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -1646,10 +1646,14 @@ void FusedLayerNormInferMeta(const MetaTensor& x, auto out_dims = phi::make_ddim(x_dims_vec); out->set_dims(out_dims); - if (quant_scale <= 0.0f) { + if (residual_out && !norm_weight && !norm_bias) { out->set_dtype(x.dtype()); } else { - out->set_dtype(phi::DataType::INT8); + if (quant_scale <= 0.0f) { + out->set_dtype(x.dtype()); + } else { + out->set_dtype(phi::DataType::INT8); + } } out->set_layout(x.layout()); diff --git a/paddle/phi/kernels/cpu/stft_kernel.cc b/paddle/phi/kernels/cpu/stft_kernel.cc index 2e790d9319571..c6df800ec432f 100644 --- a/paddle/phi/kernels/cpu/stft_kernel.cc +++ b/paddle/phi/kernels/cpu/stft_kernel.cc @@ -18,11 +18,5 @@ #include "paddle/phi/kernels/impl/stft_kernel_impl.h" PD_REGISTER_KERNEL(stft, CPU, ALL_LAYOUT, phi::StftKernel, float, double) { - if (kernel_key.dtype() == phi::DataType::FLOAT16 && - kernel_key.dtype() == phi::DataType::FLOAT32 && - kernel_key.dtype() == phi::DataType::FLOAT64) { - kernel->OutputAt(0).SetDataType(phi::DataType::COMPLEX64); - } else { - kernel->OutputAt(0).SetDataType(phi::DataType::COMPLEX128); - } + kernel->OutputAt(0).SetDataType(phi::dtype::ToComplex(kernel_key.dtype())); } diff --git a/paddle/phi/kernels/fusion/gpu/fused_layernorm_kernel.cu b/paddle/phi/kernels/fusion/gpu/fused_layernorm_kernel.cu index 61e8890664847..e31b24e7e105e 100644 --- a/paddle/phi/kernels/fusion/gpu/fused_layernorm_kernel.cu +++ b/paddle/phi/kernels/fusion/gpu/fused_layernorm_kernel.cu @@ -1074,14 +1074,22 @@ PD_REGISTER_KERNEL(fused_bias_residual_layernorm, phi::fusion::FusedLayerNormKernel, float, phi::dtype::float16, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT32); +} #else PD_REGISTER_KERNEL(fused_bias_residual_layernorm, GPU, ALL_LAYOUT, phi::fusion::FusedLayerNormKernel, float, - phi::dtype::float16) {} + phi::dtype::float16) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT32); +} #endif // CUDNN_VERSION_MIN #else PD_REGISTER_KERNEL(fused_bias_residual_layernorm, @@ -1089,5 +1097,9 @@ PD_REGISTER_KERNEL(fused_bias_residual_layernorm, ALL_LAYOUT, phi::fusion::FusedLayerNormKernel, float, - phi::dtype::float16) {} + phi::dtype::float16) { + kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT32); +} #endif // PADDLE_WITH_HIP diff --git a/paddle/phi/kernels/gpu/c_identity_kernel.cu b/paddle/phi/kernels/gpu/c_identity_kernel.cu index 34efc54ab0b56..1f552951b8ac7 100644 --- a/paddle/phi/kernels/gpu/c_identity_kernel.cu +++ b/paddle/phi/kernels/gpu/c_identity_kernel.cu @@ -18,6 +18,7 @@ limitations under the License. */ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#if NCCL_VERSION_CODE >= 21000 && CUDA_VERSION >= 11000 PD_REGISTER_KERNEL(c_identity, GPU, ALL_LAYOUT, @@ -28,3 +29,14 @@ PD_REGISTER_KERNEL(c_identity, int64_t, phi::dtype::bfloat16, phi::dtype::float16) {} +#else +PD_REGISTER_KERNEL(c_identity, + GPU, + ALL_LAYOUT, + phi::CIdentityKernel, + float, + double, + int, + int64_t, + phi::dtype::float16) {} +#endif diff --git a/paddle/phi/kernels/gpu/stft_kernel.cu b/paddle/phi/kernels/gpu/stft_kernel.cu index 324883581610b..370c5c75e88bf 100644 --- a/paddle/phi/kernels/gpu/stft_kernel.cu +++ b/paddle/phi/kernels/gpu/stft_kernel.cu @@ -17,4 +17,6 @@ #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/impl/stft_kernel_impl.h" -PD_REGISTER_KERNEL(stft, GPU, ALL_LAYOUT, phi::StftKernel, float, double) {} +PD_REGISTER_KERNEL(stft, GPU, ALL_LAYOUT, phi::StftKernel, float, double) { + kernel->OutputAt(0).SetDataType(phi::dtype::ToComplex(kernel_key.dtype())); +} diff --git a/paddle/phi/kernels/impl/stft_kernel_impl.h b/paddle/phi/kernels/impl/stft_kernel_impl.h index 8d590de9d9767..ac0a1752062f3 100644 --- a/paddle/phi/kernels/impl/stft_kernel_impl.h +++ b/paddle/phi/kernels/impl/stft_kernel_impl.h @@ -16,6 +16,7 @@ #include #include "paddle/phi/common/complex.h" +#include "paddle/phi/common/type_traits.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/kernels/cpu/elementwise.h" #include "paddle/phi/kernels/funcs/elementwise_base.h" From fc1e1b77c0c9ffcad238647d175e6b739921b3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Tue, 29 Aug 2023 19:09:10 +0800 Subject: [PATCH 003/194] [xdoctest] reformat example code with google style in No. 240 (#56474) * 240 * fix bugs * fix bugs --- python/paddle/incubate/asp/utils.py | 259 +++++++++++++++------------- 1 file changed, 142 insertions(+), 117 deletions(-) diff --git a/python/paddle/incubate/asp/utils.py b/python/paddle/incubate/asp/utils.py index 6502f9c8ab473..1028dcc4dae29 100644 --- a/python/paddle/incubate/asp/utils.py +++ b/python/paddle/incubate/asp/utils.py @@ -57,17 +57,14 @@ def get_checking_method(mask_algo): Examples: .. code-block:: python - import numpy as np - from paddle.incubate.asp import CheckMethod, MaskAlgo - - CheckMethod.get_checking_method(MaskAlgo.MASK_1D) - # CheckMethod.CHECK_1D - - CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY) - # CheckMethod.CHECK_2D - - CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST) - # CheckMethod.CHECK_2D + >>> import numpy as np + >>> from paddle.incubate.asp import CheckMethod, MaskAlgo + >>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_1D)) + CheckMethod.CHECK_1D + >>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_GREEDY)) + CheckMethod.CHECK_2D + >>> print(CheckMethod.get_checking_method(MaskAlgo.MASK_2D_BEST)) + CheckMethod.CHECK_2D """ assert isinstance( mask_algo, MaskAlgo @@ -92,12 +89,14 @@ def calculate_density(x): Examples: .. code-block:: python - import paddle - import numpy as np + >>> import paddle + >>> import numpy as np - x = np.array([[0, 1, 3, 0], - [1, 1, 0, 1]]) - paddle.incubate.asp.calculate_density(x) # 0.625 + >>> x = np.array([[0, 1, 3, 0], + ... [1, 1, 0, 1]]) + >>> out = paddle.incubate.asp.calculate_density(x) + >>> print(out) + 0.625 """ x_flattened = x.flatten() @@ -149,21 +148,27 @@ def check_mask_1d(mat, n, m): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - x = np.array([[0, 1, 3, 0], - [1, 0, 0, 1]]) - sparsity.check_mask_1d(x, 2, 4) # True - - x = np.array([[0, 1, 5, 4], - [1, 0, 0, 1]]) - sparsity.check_mask_1d(x, 2, 4) # False - - # x would be padded to shape (2, 8) - x = np.array([[0, 1, 0, 4, 6], - [1, 0, 0, 1, 7]]) - sparsity.check_mask_1d(x, 2, 4) # True + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> x = np.array([[0, 1, 3, 0], + ... [1, 0, 0, 1]]) + >>> y = sparsity.check_mask_1d(x, 2, 4) + >>> print(y) + True + + >>> x = np.array([[0, 1, 5, 4], + ... [1, 0, 0, 1]]) + >>> y = sparsity.check_mask_1d(x, 2, 4) + >>> print(y) + False + + >>> # x would be padded to shape (2, 8) + >>> x = np.array([[0, 1, 0, 4, 6], + ... [1, 0, 0, 1, 7]]) + >>> y = sparsity.check_mask_1d(x, 2, 4) + >>> print(y) + True """ if len(mat.shape) <= 1: mat_flattern, shape = _reshape_1d(mat.reshape(1, mat.shape[0]), m) @@ -193,15 +198,17 @@ def get_mask_1d(mat, n, m): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - mat = np.array([[0, 1, 5, 4], - [2, 7, 3, 6]]) - mask = sparsity.get_mask_1d(mat, 2, 4) - # nparray([[0, 0, 1, 1], - # [0, 1, 0, 1]]) - sparsity.check_mask_1d(mask, 2, 4) # True + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + >>> mat = np.array([[0, 1, 5, 4], + ... [2, 7, 3, 6]]) + >>> mask = sparsity.get_mask_1d(mat, 2, 4) + >>> print(mask) + [[0 0 1 1] + [0 1 0 1]] + >>> y = sparsity.check_mask_1d(mask, 2, 4) + >>> print(y) + True """ mat_flattern, shape = _reshape_1d(mat, m) @@ -277,28 +284,34 @@ def check_mask_2d(mat, n, m): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - x = np.array([[0, 8, 9, 0], - [9, 0, 0, 10], - [5, 0, 0, 6], - [0, 4, 6, 0]]) - sparsity.check_mask_2d(x, 2, 4) # True - - x = np.array([[0, 8, 0, 9], - [9, 0, 0, 10], - [0, 5, 0, 6], - [0, 4, 6, 0]]) - sparsity.check_mask_2d(x, 2, 4) # False - - # x would be padded to shape (8, 8) - x = np.array([[0, 8, 0, 9], - [9, 0, 7, 0], - [0, 5, 0, 6], - [3, 0, 6, 0], - [1, 1, 0, 1]]) - sparsity.check_mask_2d(x, 2, 4) # True + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> x = np.array([[0, 8, 9, 0], + ... [9, 0, 0, 10], + ... [5, 0, 0, 6], + ... [0, 4, 6, 0]]) + >>> y = sparsity.check_mask_2d(x, 2, 4) + >>> print(y) + True + + >>> x = np.array([[0, 8, 0, 9], + ... [9, 0, 0, 10], + ... [0, 5, 0, 6], + ... [0, 4, 6, 0]]) + >>> y = sparsity.check_mask_2d(x, 2, 4) + >>> print(y) + True + + >>> # x would be padded to shape (8, 8) + >>> x = np.array([[0, 8, 0, 9], + ... [9, 0, 7, 0], + ... [0, 5, 0, 6], + ... [3, 0, 6, 0], + ... [1, 1, 0, 1]]) + >>> y = sparsity.check_mask_2d(x, 2, 4) + >>> print(y) + True """ mat_padded, shape = _reshape_2d(mat, m) for sub_mat in mat_padded: @@ -328,19 +341,22 @@ def get_mask_2d_greedy(mat, n, m): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - mat = np.array([[9, 8, 3, 7], - [9, 2, 1, 10], - [5, 1, 3, 6], - [2, 4, 6, 1]]) - mask = sparsity.get_mask_2d_greedy(mat, 2, 4) - # nparray([[1. 1. 0. 0.] - # [1. 0. 0. 1.] - # [0. 0. 1. 1.] - # [0. 1. 1. 0.]]) - sparsity.check_mask_2d(mask, 2, 4) # True + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> mat = np.array([[9, 8, 3, 7], + ... [9, 2, 1, 10], + ... [5, 1, 3, 6], + ... [2, 4, 6, 1]]) + >>> mask = sparsity.get_mask_2d_greedy(mat, 2, 4) + >>> print(mask) + [[1. 1. 0. 0.] + [1. 0. 0. 1.] + [0. 0. 1. 1.] + [0. 1. 1. 0.]] + >>> y = sparsity.check_mask_2d(mask, 2, 4) + >>> print(y) + True """ mat_padded, shape = _reshape_2d(mat, m) mask_padded = np.zeros_like(mat_padded).reshape(-1, m, m) @@ -443,17 +459,19 @@ def get_mask_2d_best(mat, n, m): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - mat = np.array([[2, 8, 9, 9], - [9, 1, 3, 9], - [5, 6, 3, 9], - [2, 4, 6, 9]]) - mask_greedy = sparsity.get_mask_2d_greedy(mat, 2, 4) - mask_best = sparsity.get_mask_2d_best(mat, 2, 4) - print("L1 norm of `greedy` sparse matrix", np.multiply(mat, mask_greedy).sum()) # 56 - print("L1 norm of `best` sparse matrix", np.multiply(mat, mask_best).sum()) # 61 + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> mat = np.array([[2, 8, 9, 9], + ... [9, 1, 3, 9], + ... [5, 6, 3, 9], + ... [2, 4, 6, 9]]) + >>> mask_greedy = sparsity.get_mask_2d_greedy(mat, 2, 4) + >>> mask_best = sparsity.get_mask_2d_best(mat, 2, 4) + >>> print("L1 norm of `greedy` sparse matrix", np.multiply(mat, mask_greedy).sum()) + L1 norm of `greedy` sparse matrix 56.0 + >>> print("L1 norm of `best` sparse matrix", np.multiply(mat, mask_best).sum()) + L1 norm of `best` sparse matrix 61.0 """ patterns = _compute_valid_2d_patterns(n, m) @@ -492,23 +510,25 @@ def create_mask(tensor, func_name=MaskAlgo.MASK_1D, n=2, m=4): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - tensor = np.array([[2, 8, 9, 9], - [9, 1, 3, 9], - [5, 6, 3, 9], - [2, 4, 6, 9]]) - mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) - # nparray([[0 0 1 1], - # [1 0 0 1], - # [0 1 0 1], - # [0 0 1 1]]) - mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST) - # nparray([[0 1 1 0], - # [1 0 0 1], - # [1 1 0 0], - # [0 0 1 1]]) + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> tensor = np.array([[2, 8, 9, 9], + ... [9, 1, 3, 9], + ... [5, 6, 3, 9], + ... [2, 4, 6, 9]]) + >>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) + >>> print(mask_1d) + [[0 0 1 1] + [1 0 0 1] + [0 1 0 1] + [0 0 1 1]] + >>> mask_2d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_2D_BEST) + >>> print(mask_2d) + [[0 1 1 0] + [1 0 0 1] + [1 1 0 0] + [0 0 1 1]] """ shape = tensor.shape dtype = tensor.dtype @@ -561,20 +581,25 @@ def check_sparsity(tensor, func_name=CheckMethod.CHECK_1D, n=2, m=4): Examples: .. code-block:: python - import numpy as np - import paddle.incubate.asp as sparsity - - tensor = np.array([[2, 8, 9, 9], - [9, 1, 3, 9], - [5, 6, 3, 9], - [2, 4, 6, 9]]) - mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) - # nparray([[0 0 1 1], - # [1 0 0 1], - # [0 1 0 1], - # [0 0 1 1]]) - sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D) # True - sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D) # False + >>> import numpy as np + >>> import paddle.incubate.asp as sparsity + + >>> tensor = np.array([[2, 8, 9, 9], + ... [9, 1, 3, 9], + ... [5, 6, 3, 9], + ... [2, 4, 6, 9]]) + >>> mask_1d = sparsity.create_mask(tensor, func_name=sparsity.MaskAlgo.MASK_1D) + >>> print(mask_1d) + [[0 0 1 1] + [1 0 0 1] + [0 1 0 1] + [0 0 1 1]] + >>> y = sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_1D) + >>> print(y) + True + >>> y = sparsity.check_sparsity(mask_1d, func_name=sparsity.CheckMethod.CHECK_2D) + >>> print(y) + True """ shape = tensor.shape t = tensor.astype(float) From 9999e8497a485ea76df8fd198afd9c8db08c926f Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Tue, 29 Aug 2023 19:14:55 +0800 Subject: [PATCH 004/194] [New-IR] add pass registry (#56729) * add pass registry * add pass registry macro --- paddle/fluid/pybind/ir.cc | 15 +-- paddle/ir/pass/pass.h | 1 + paddle/ir/pass/pass_registry.cc | 23 ++++ paddle/ir/pass/pass_registry.h | 104 ++++++++++++++++++ .../transforms/dead_code_elimination_pass.cc | 3 + test/ir/new_ir/test_pass_manager.py | 3 +- 6 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 paddle/ir/pass/pass_registry.cc create mode 100644 paddle/ir/pass/pass_registry.h diff --git a/paddle/fluid/pybind/ir.cc b/paddle/fluid/pybind/ir.cc index 6c6957c3e00e0..675e6f2acd25d 100644 --- a/paddle/fluid/pybind/ir.cc +++ b/paddle/fluid/pybind/ir.cc @@ -39,6 +39,7 @@ #include "paddle/ir/core/value.h" #include "paddle/ir/pass/pass.h" #include "paddle/ir/pass/pass_manager.h" +#include "paddle/ir/pass/pass_registry.h" #include "paddle/ir/transforms/dead_code_elimination_pass.h" #include "paddle/phi/core/enforce.h" #include "pybind11/stl.h" @@ -57,6 +58,8 @@ using paddle::dialect::APIBuilder; using paddle::dialect::DenseTensorType; using pybind11::return_value_policy; +USE_PASS(dead_code_elimination); + namespace paddle { namespace pybind { @@ -488,15 +491,6 @@ void BindIrPass(pybind11::module *m) { [](const Pass &self) { return self.pass_info().dependents; }); } -// TODO(zhiqiu): refine pass registry -std::unique_ptr CreatePassByName(std::string name) { - if (name == "DeadCodeEliminationPass") { - return ir::CreateDeadCodeEliminationPass(); - } else { - IR_THROW("The %s pass is not registed", name); - } -} - void BindPassManager(pybind11::module *m) { py::class_> pass_manager( *m, @@ -514,7 +508,8 @@ void BindPassManager(pybind11::module *m) { py::arg("opt_level") = 2) .def("add_pass", [](PassManager &self, std::string pass_name) { - self.AddPass(std::move(CreatePassByName(pass_name))); + self.AddPass( + std::move(ir::PassRegistry::Instance().Get(pass_name))); }) .def("passes", [](PassManager &self) { diff --git a/paddle/ir/pass/pass.h b/paddle/ir/pass/pass.h index 484651e87e22a..4a4cbf629d678 100644 --- a/paddle/ir/pass/pass.h +++ b/paddle/ir/pass/pass.h @@ -20,6 +20,7 @@ #include "paddle/ir/core/enforce.h" #include "paddle/ir/pass/analysis_manager.h" +#include "paddle/ir/pass/pass_registry.h" #include "paddle/phi/core/enforce.h" namespace ir { diff --git a/paddle/ir/pass/pass_registry.cc b/paddle/ir/pass/pass_registry.cc new file mode 100644 index 0000000000000..a0239219a694d --- /dev/null +++ b/paddle/ir/pass/pass_registry.cc @@ -0,0 +1,23 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/ir/pass/pass_registry.h" + +namespace ir { +PassRegistry &PassRegistry::Instance() { + static PassRegistry g_pass_info_map; + return g_pass_info_map; +} + +} // namespace ir diff --git a/paddle/ir/pass/pass_registry.h b/paddle/ir/pass/pass_registry.h new file mode 100644 index 0000000000000..c35dc0ba90a30 --- /dev/null +++ b/paddle/ir/pass/pass_registry.h @@ -0,0 +1,104 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include +#include + +#include "paddle/ir/core/enforce.h" +#include "paddle/ir/core/macros.h" +#include "paddle/ir/pass/pass.h" + +namespace ir { + +class Pass; + +using PassCreator = std::function()>; + +class PassRegistry { + public: + static PassRegistry &Instance(); + + bool Has(const std::string &pass_type) const { + return pass_map_.find(pass_type) != pass_map_.end(); + } + + void Insert(const std::string &pass_type, const PassCreator &pass_creator) { + IR_ENFORCE( + Has(pass_type) != true, "Pass %s has been registered.", pass_type); + pass_map_.insert({pass_type, pass_creator}); + } + + std::unique_ptr Get(const std::string &pass_type) const { + IR_ENFORCE( + Has(pass_type) == true, "Pass %s has not been registered.", pass_type); + return pass_map_.at(pass_type)(); + } + + private: + PassRegistry() = default; + std::unordered_map pass_map_; + + DISABLE_COPY_AND_ASSIGN(PassRegistry); +}; + +template +class PassRegistrar { + public: + // In our design, various kinds of passes, + // have their corresponding registry and registrar. The action of + // registration is in the constructor of a global registrar variable, which + // are not used in the code that calls package framework, and would + // be removed from the generated binary file by the linker. To avoid such + // removal, we add Touch to all registrar classes and make USE_PASS macros to + // call this method. So, as long as the callee code calls USE_PASS, the global + // registrar variable won't be removed by the linker. + void Touch() {} + explicit PassRegistrar(const char *pass_type) { + PassRegistry::Instance().Insert( + pass_type, []() { return std::make_unique(); }); + } +}; + +#define STATIC_ASSERT_PASS_GLOBAL_NAMESPACE(uniq_name, msg) \ + struct __test_global_namespace_##uniq_name##__ {}; \ + static_assert(std::is_same<::__test_global_namespace_##uniq_name##__, \ + __test_global_namespace_##uniq_name##__>::value, \ + msg) + +// Register a new pass that can be applied on the IR. +#define REGISTER_PASS(pass_type, pass_class) \ + STATIC_ASSERT_PASS_GLOBAL_NAMESPACE( \ + __reg_pass__##pass_type, \ + "REGISTER_PASS must be called in global namespace"); \ + static ::ir::PassRegistrar __pass_registrar_##pass_type##__( \ + #pass_type); \ + int TouchPassRegistrar_##pass_type() { \ + __pass_registrar_##pass_type##__.Touch(); \ + return 0; \ + } \ + static ::ir::PassRegistrar &__pass_tmp_registrar_##pass_type##__ \ + UNUSED = __pass_registrar_##pass_type##__ + +#define USE_PASS(pass_type) \ + STATIC_ASSERT_PASS_GLOBAL_NAMESPACE( \ + __use_pass_itself_##pass_type, \ + "USE_PASS must be called in global namespace"); \ + extern int TouchPassRegistrar_##pass_type(); \ + static int use_pass_itself_##pass_type##_ UNUSED = \ + TouchPassRegistrar_##pass_type() + +} // namespace ir diff --git a/paddle/ir/transforms/dead_code_elimination_pass.cc b/paddle/ir/transforms/dead_code_elimination_pass.cc index f58a4485fc772..d56b83b844680 100644 --- a/paddle/ir/transforms/dead_code_elimination_pass.cc +++ b/paddle/ir/transforms/dead_code_elimination_pass.cc @@ -17,6 +17,7 @@ #include "paddle/ir/core/builtin_op.h" #include "paddle/ir/core/program.h" #include "paddle/ir/pass/pass.h" +#include "paddle/ir/pass/pass_registry.h" namespace { @@ -75,3 +76,5 @@ std::unique_ptr CreateDeadCodeEliminationPass() { } } // namespace ir + +REGISTER_PASS(dead_code_elimination, DeadCodeEliminationPass); diff --git a/test/ir/new_ir/test_pass_manager.py b/test/ir/new_ir/test_pass_manager.py index 580dea776772c..2f31e945f31f4 100644 --- a/test/ir/new_ir/test_pass_manager.py +++ b/test/ir/new_ir/test_pass_manager.py @@ -51,11 +51,12 @@ def test_op(self): self.assertTrue('pd.uniform' in op_names) pm = ir.PassManager() pm.add_pass( - 'DeadCodeEliminationPass' + 'dead_code_elimination' ) # apply pass to elimitate dead code pm.run(new_program) op_names = [op.name() for op in new_program.block().ops] # print(op_names) + # TODO(zhiqiu): unify the name of pass self.assertEqual(pm.passes(), ['DeadCodeEliminationPass']) self.assertFalse(pm.empty()) self.assertTrue( From b0b827c7947a0fbefda94e30393d0cfe3968a5c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Tue, 29 Aug 2023 19:34:57 +0800 Subject: [PATCH 005/194] [xdoctest] reformat example code with google style in No. 299 (#56597) * Update dlpack.py * Apply suggestions from code review * Apply suggestions from code review * xdoc * Apply suggestions from code review * Apply suggestions from code review --- python/paddle/incubate/optimizer/lbfgs.py | 76 +++++++++++------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/python/paddle/incubate/optimizer/lbfgs.py b/python/paddle/incubate/optimizer/lbfgs.py index ae7511ae03e94..04d90d1f8c271 100644 --- a/python/paddle/incubate/optimizer/lbfgs.py +++ b/python/paddle/incubate/optimizer/lbfgs.py @@ -76,45 +76,43 @@ class LBFGS(Optimizer): Examples: .. code-block:: python - import paddle - import numpy as np - from paddle.incubate.optimizer import LBFGS - - paddle.disable_static() - np.random.seed(0) - np_w = np.random.rand(1).astype(np.float32) - np_x = np.random.rand(1).astype(np.float32) - - inputs = [np.random.rand(1).astype(np.float32) for i in range(10)] - # y = 2x - targets = [2 * x for x in inputs] - - class Net(paddle.nn.Layer): - def __init__(self): - super().__init__() - w = paddle.to_tensor(np_w) - self.w = paddle.create_parameter(shape=w.shape, dtype=w.dtype, default_initializer=paddle.nn.initializer.Assign(w)) - - def forward(self, x): - return self.w * x - - net = Net() - opt = LBFGS(learning_rate=1, max_iter=1, max_eval=None, tolerance_grad=1e-07, tolerance_change=1e-09, history_size=100, line_search_fn='strong_wolfe', parameters=net.parameters()) - def train_step(inputs, targets): - def closure(): - outputs = net(inputs) - loss = paddle.nn.functional.mse_loss(outputs, targets) - print('loss: ', loss.item()) - opt.clear_grad() - loss.backward() - return loss - opt.step(closure) - - - for input, target in zip(inputs, targets): - input = paddle.to_tensor(input) - target = paddle.to_tensor(target) - train_step(input, target) + >>> import paddle + >>> import numpy as np + >>> from paddle.incubate.optimizer import LBFGS + + >>> paddle.disable_static() + >>> np.random.seed(0) + >>> np_w = np.random.rand(1).astype(np.float32) + >>> np_x = np.random.rand(1).astype(np.float32) + + >>> inputs = [np.random.rand(1).astype(np.float32) for i in range(10)] + >>> # y = 2x + >>> targets = [2 * x for x in inputs] + + >>> class Net(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... w = paddle.to_tensor(np_w) + ... self.w = paddle.create_parameter(shape=w.shape, dtype=w.dtype, default_initializer=paddle.nn.initializer.Assign(w)) + ... def forward(self, x): + ... return self.w * x + + >>> net = Net() + >>> opt = LBFGS(learning_rate=1, max_iter=1, max_eval=None, tolerance_grad=1e-07, tolerance_change=1e-09, history_size=100, line_search_fn='strong_wolfe', parameters=net.parameters()) + >>> def train_step(inputs, targets): + ... def closure(): + ... outputs = net(inputs) + ... loss = paddle.nn.functional.mse_loss(outputs, targets) + ... print('loss: ', loss.item()) + ... opt.clear_grad() + ... loss.backward() + ... return loss + ... opt.step(closure) + + >>> for input, target in zip(inputs, targets): + ... input = paddle.to_tensor(input) + ... target = paddle.to_tensor(target) + ... train_step(input, target) """ From 128f95a10336361bef61b03edf411cdebd97faec Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Tue, 29 Aug 2023 19:35:09 +0800 Subject: [PATCH 006/194] Vjp autogen for grad list op(split) (#56720) * add vjp code gen for SplitOp * change vjp manual file name --- .../dialect/op_generator/op_interface_gen.py | 35 +++++++++++++------ .../op_generator/vjp_interface_gen_op_list.py | 1 + .../dialect/paddle_dialect/ir/CMakeLists.txt | 2 +- ...d_op_vjp_manual.cc => pd_manual_op_vjp.cc} | 33 ----------------- 4 files changed, 27 insertions(+), 44 deletions(-) rename paddle/fluid/ir/dialect/paddle_dialect/ir/{pd_op_vjp_manual.cc => pd_manual_op_vjp.cc} (65%) diff --git a/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py b/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py index 389643cf16113..5f72a2efbc1d6 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py @@ -38,7 +38,11 @@ Tensor {output_grad_name}(std::make_shared(out_grads[{idx1}][{idx2}]));""" OP_VJP_FORWARD_OUTPUT_GRAD_LIST_TEMPLATE = """ - std::vector {output_grad_name}(std::make_shared(out_grads[{idx1}]));""" + std::vector {output_grad_name}; + for (size_t idx = 0; idx < out_grads[{index}].size(); idx++) {{ + {output_grad_name}.emplace_back( + std::make_shared(out_grads[{index}][idx])); + }}""" OP_VJP_ATTRIBUTE_TEMPLATE = """ {attr_type} {attr_name} = op->attribute("{attr_name}").dyn_cast<{attr_parse_type}>().data();""" @@ -47,9 +51,10 @@ {attr_type} {attr_name} = {default_value};""" -OP_VJP_CALL_VJP_TEMPLATE = """ std::vector> tensor_res = - primitive::{op_phi_name}_vjp( - {inputs_list}stop_gradients);""" +OP_VJP_CALL_VJP_TEMPLATE = """ + std::vector> tensor_res = + primitive::{op_phi_name}_vjp( + {inputs_list}stop_gradients);""" OP_VJP_STOPGRADIENT_TEMPLATE = """ std::vector> res(tensor_res.size()); @@ -73,10 +78,10 @@ VLOG(6) << "Vjp prepare Prepare attributes of {op_grad_name}"; {attribute_code} - VLOG(4) << "Vjp prepare call {op_phi_name}'s vjp inteface"; + VLOG(6) << "Vjp prepare call {op_phi_name}'s vjp inteface"; {call_vjp_code} - VLOG(4) << "Vjp prepare stop gradient of {op_grad_name}"; + VLOG(6) << "Vjp prepare stop gradient of {op_grad_name}"; {stop_gradient_input_grad_code} return res; }} @@ -122,11 +127,21 @@ def gen_op_vjp_str( ) else: grad_idx += 1 - forward_output_grad_code += ( - OP_VJP_FORWARD_OUTPUT_GRAD_TEMPLATE.format( - output_grad_name=bw_input_list[idx], idx1=grad_idx, idx2=0 + input_type = input_types_map[op_grad_info.input_type_list[idx]] + if input_type == 'Tensor': + forward_output_grad_code += ( + OP_VJP_FORWARD_OUTPUT_GRAD_TEMPLATE.format( + output_grad_name=bw_input_list[idx], + idx1=grad_idx, + idx2=0, + ) + ) + else: + forward_input_output_code += ( + OP_VJP_FORWARD_OUTPUT_GRAD_LIST_TEMPLATE.format( + output_grad_name=bw_input_list[idx], index=grad_idx + ) ) - ) op_attribute_list = op_grad_info.attribute_name_list attribute_code = '' for idx in range(len(op_attribute_list)): diff --git a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py index aa8d8d1c8e3e8..4fc85a07511f6 100644 --- a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py +++ b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py @@ -37,4 +37,5 @@ "divide", "add", "concat", + "split", ] diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index 02bedd7fd8619..86ade99a3cc22 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -134,6 +134,6 @@ target_include_directories(pd_dialect_api PRIVATE ${PD_DIALECT_BINARY_DIR}) cc_library( pd_dialect - SRCS pd_dialect.cc pd_op_vjp_manual.cc ${op_vjp_source_file} + SRCS pd_dialect.cc pd_manual_op_vjp.cc ${op_vjp_source_file} DEPS pd_dialect_api param_to_variable primitive_vjp_experimental pd_dialect_utils op_yaml_info_parser) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op_vjp_manual.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc similarity index 65% rename from paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op_vjp_manual.cc rename to paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc index 9806fb4cf0ce2..a69a1af650b98 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op_vjp_manual.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc @@ -54,38 +54,5 @@ std::vector> SumOp::Vjp( return res; } -std::vector> SplitOp::Vjp( - ir::Operation* op, - const std::vector>& out_grads, - const std::vector>& stop_gradients) { - SplitOp op_obj = op->dyn_cast(); - - Tensor axis(std::make_shared(op_obj.axis())); - std::vector out_grads_; - for (size_t idx = 0; idx < out_grads[0].size(); idx++) { - out_grads_.emplace_back( - std::make_shared(out_grads[0][idx])); - } - - std::vector> tensor_res = - primitive::split_vjp(out_grads_, axis, stop_gradients); - - std::vector> res(tensor_res.size(), - std::vector()); - - for (uint64_t i = 0; i < tensor_res.size(); i++) { - res[i].resize(tensor_res[i].size()); - for (uint64_t j = 0; j < tensor_res[i].size(); j++) { - if (tensor_res[i][j].defined()) { - res[i][j] = std::static_pointer_cast( - tensor_res[i][j].impl()) - ->getValue() - .dyn_cast(); - } - } - } - return res; -} - } // namespace dialect } // namespace paddle From df9d9c5994d1b61d86f78bca9d0f05decc4583ba Mon Sep 17 00:00:00 2001 From: Fisher Date: Tue, 29 Aug 2023 19:52:46 +0800 Subject: [PATCH 007/194] Fix instant variable oom in paddle2cinn (#56662) When using paddle2cinn, CompilationContext.with_instantiate_variables should be set to false, otherwise CINN will instant and manage variables memory, this leads to double the memory usage, which eventually leads to out of memory error. This PR will set CompilationContext.with_instantiate_variables to false before context pass to constructing the graph compiler. --- paddle/fluid/framework/paddle2cinn/cinn_compiler.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc index 0616d3f23e477..29ea6098088b6 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc @@ -322,11 +322,11 @@ std::unique_ptr CinnCompiler::CompileGraph( auto scope = BuildScope(target, cinn_graph); CompilationContext context(cinn_graph, scope, target); - auto graph_compiler = std::make_unique(context); context.with_instantiate_variables = false; if (!FLAGS_enable_pe_launch_cinn) { context.with_buffer_handle_instruction_inserted = true; } + auto graph_compiler = std::make_unique(context); std::unique_ptr auto_tuner; if (FLAGS_enable_cinn_auto_tune) { VLOG(4) << "Compile with auto-tune"; @@ -339,11 +339,11 @@ std::unique_ptr CinnCompiler::CompileGraph( } context.fetch_var_ids = std::move(fetch_ids); context.stream = stream; - auto compiled_res = graph_compiler->Build(); + auto compiled_res = graph_compiler->Build(&context); auto compiled_obj = std::make_unique(); *compiled_obj = {std::move(graph_compiler), std::move(auto_tuner), - std::move(compiled_res), + std::move(compiled_res.runtime_program), scope, symbol.var_model_to_program_map()}; compiled_obj->cached_index = compiled_num; From 0ce66c1ccf7bf340c04198b9f04221ef47285d3f Mon Sep 17 00:00:00 2001 From: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:58:10 +0800 Subject: [PATCH 008/194] [NewIR] support c_sync_calc_stream/c_sync_comm_stream/send_v2/recv_v2 (#56557) * [AutoParallel][NewIR] support calc_sync/comm_sync/send_v2/recv_v2 * pre-commit * rm unittest * tiny fix * api_gen support send_v2's output is empty * fix format * python_c_gen support send_v2 --- .../fluid/ir/dialect/op_generator/api_gen.py | 6 +- .../ir/dialect/op_generator/python_c_gen.py | 33 +++++++++- .../ir/dialect/paddle_dialect/ir/pd_op.yaml | 60 +++++++++++++++++++ .../ir/dialect/paddle_dialect/utils/utils.cc | 5 +- paddle/phi/api/yaml/legacy_ops.yaml | 20 +++++++ paddle/phi/api/yaml/op_compat.yaml | 20 +++++++ paddle/phi/infermeta/unary.cc | 44 ++++++++++++++ paddle/phi/infermeta/unary.h | 7 +++ 8 files changed, 192 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/ir/dialect/op_generator/api_gen.py b/paddle/fluid/ir/dialect/op_generator/api_gen.py index b9ece187beea5..667174e0fbcb3 100644 --- a/paddle/fluid/ir/dialect/op_generator/api_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/api_gen.py @@ -275,16 +275,20 @@ def _gen_return_result(self, ret_list): return 'return;' def _gen_one_impl(self, op_info, op_name, is_mutable_attr): + ret_type = self._gen_ret_type(op_info) in_combine, in_combine_op_list = self._gen_in_combine(op_info) compute_op, op_inst_name = self._gen_compute_op( op_info, op_name, in_combine_op_list, is_mutable_attr ) + if ret_type == 'void': + compute_op += f' (void){op_inst_name};' + out_split, ret_list = self._gen_out_split_and_ret_list( op_info, op_inst_name ) ret = API_IMPL_TEMPLATE.format( - ret_type=self._gen_ret_type(op_info), + ret_type=ret_type, api_name=op_name, args=self._gen_api_args(op_info, False, is_mutable_attr), in_combine=in_combine, diff --git a/paddle/fluid/ir/dialect/op_generator/python_c_gen.py b/paddle/fluid/ir/dialect/op_generator/python_c_gen.py index 4510864baac5a..340aa9569818a 100644 --- a/paddle/fluid/ir/dialect/op_generator/python_c_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/python_c_gen.py @@ -81,6 +81,29 @@ }} """ +NO_OUTPUT_API_IMPL_TEMPLATE = """ +PyObject *static_api_{api_name}(PyObject *self, PyObject *args, PyObject *kwargs) {{ + try {{ + VLOG(6) << "Add {api_name} op into program"; + VLOG(8) << "args count: " << (PyTuple_Size(args) / 2); + + // Get OpResult from args + {inputs} + + // Parse Attributes + {attrs} + + // Call ir static api + paddle::dialect::{api_name}({args}); + + return nullptr; + }} catch (...) {{ + ThrowExceptionToPython(std::current_exception()); + return nullptr; + }} +}} +""" + INPUT_TEMPLATE = """ PyObject *{name}_obj = PyTuple_GET_ITEM(args, {index}); auto {name} = {cast_func}({name}_obj, "{api_name}", {index});""" @@ -263,7 +286,15 @@ def _gen_one_impl(self, op_info, op_name): attr_name_list = op_info.attribute_name_list mutable_attr_name_list = op_info.mutable_attribute_name_list no_mutable_attr_name_list = op_info.non_mutable_attribute_name_list - if len(mutable_attr_name_list) > 0: + + if op_name == "send_v2": + ret = NO_OUTPUT_API_IMPL_TEMPLATE.format( + api_name=op_name, + inputs=self._gen_inputs(op_info, op_name), + attrs=self._gen_attrs_without_mutable(op_info, op_name), + args=', '.join(input_name_list + attr_name_list), + ) + elif len(mutable_attr_name_list) > 0: ret = MUTABLE_ATTR_API_IMPL_TEMPLATE.format( api_name=op_name, inputs=self._gen_inputs(op_info, op_name), diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml index ac76e130aac86..942c3b8703c7e 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml @@ -355,6 +355,66 @@ inplace: null backward: null +- name: send_v2 + inputs: + - typename: Tensor + name: x + optional: false + no_need_buffer: false + data_transform: {} + attrs: + - {typename: int, name: ring_id, default_value: '0'} + - {typename: int, name: peer, default_value: '0'} + - {typename: bool, name: use_calc_stream, default_value: 'false'} + - {typename: bool, name: dynamic_shape, default_value: 'false'} + outputs: [] + no_need_buffer: null + data_transform: null + infer_meta: + func: SendV2InferMeta + param: [peer, ring_id] + kernel: + func: [send_v2] + param: [x, ring_id, dynamic_shape, peer, use_calc_stream] + backend: null + layout: null + data_type: null + dispatch: {send_v2: null} + force_backend: null + inplace: null + view: null + backward: null + +- name: recv_v2 + inputs: [] + attrs: + - {typename: 'int[]', name: out_shape, default_value: '{}'} + - {typename: DataType, name: dtype, default_value: 'DataType::FLOAT32'} + - {typename: int, name: peer, default_value: '0'} + - {typename: int, name: ring_id, default_value: '0'} + - {typename: bool, name: use_calc_stream, default_value: 'false'} + - {typename: bool, name: dynamic_shape, default_value: 'false'} + outputs: + - {typename: Tensor, name: out, optional: false, intermediate: false} + no_need_buffer: null + data_transform: null + infer_meta: + func: RecvV2InferMeta + param: [peer, dtype, out_shape] + kernel: + func: [recv_v2] + param: [ring_id, dynamic_shape, peer, out_shape, dtype, use_calc_stream] + backend: null + layout: null + data_type: + ordered: false + candidates: [dtype] + to_complex_flag: [false] + dispatch: {recv_v2: null} + force_backend: null + inplace: null + view: null + - name : set_value inputs: - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc index a2c774242abb8..1216589aeb584 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc @@ -24,7 +24,10 @@ const std::unordered_set LegacyOpList = { "pd.c_broadcast_", "pd.fused_bn_add_activation_", "pd.fused_bn_add_activation_grad", -}; + "pd.c_sync_calc_stream_", + "pd.c_sync_comm_stream_", + "pd.send_v2", + "pd.recv_v2"}; enum class AttrType { UNDEFINED = 0, diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 1bb77b99fa0bb..f760c6f96aac3 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -142,6 +142,26 @@ kernel : func : c_concat +- op : c_sync_calc_stream + args : (Tensor x) + output : Tensor(out) + infer_meta : + func : UnchangedInferMeta + param : [x] + kernel : + func : c_sync_calc_stream + inplace : (x -> out) + +- op : c_sync_comm_stream + args : (Tensor x) + output : Tensor(out) + infer_meta : + func : UnchangedInferMeta + param : [x] + kernel : + func : c_sync_comm_stream + inplace : (x -> out) + - op : cast args : (Tensor x, DataType dtype) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 00f0c9f300a93..575b01c33afef 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3034,6 +3034,18 @@ outputs : out : Out +- op: c_sync_calc_stream + inputs : + x : X + outputs : + out : Out + +- op: c_sync_comm_stream + inputs : + x : X + outputs : + out : Out + - op: channel_shuffle inputs: {x: X} @@ -3071,6 +3083,10 @@ outputs : out : Out +- op: recv_v2 + outputs : + out : Out + - op: reindex_graph (graph_reindex) inputs : {x : X, neighbors : Neighbors, count : Count, hashtable_value : HashTable_Value, hashtable_index : HashTable_Index} @@ -3083,6 +3099,10 @@ outputs: {out: Out, noise: Noise} +- op: send_v2 + inputs : + x : X + - op: set_value backward: set_value_grad inputs: diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index fc4e4483d17e0..6e04a149266ac 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -403,6 +403,50 @@ void CConcatInferMeta(const MetaTensor& x, int nranks, MetaTensor* out) { out->set_dtype(x.dtype()); } +void SendV2InferMeta(const int peer, const int ring_id) { + PADDLE_ENFORCE_GE( + peer, + 0, + errors::InvalidArgument( + "The peer (%d) for send_v2 op must be non-negative.", peer)); + PADDLE_ENFORCE_GE( + ring_id, + 0, + errors::InvalidArgument( + "The ring_id (%d) for send_v2 op must be non-negative.", ring_id)); +} + +void RecvV2InferMeta(int peer, + DataType dtype, + const std::vector& out_shape, + MetaTensor* out) { + PADDLE_ENFORCE_GE( + peer, + 0, + errors::InvalidArgument( + "The peer (%d) for p_recv op must be non-negative.", peer)); + + PADDLE_ENFORCE_GE(out_shape.size(), + 1, + errors::InvalidArgument( + "The size of the output shape must be greater than 0 " + "but the value given is %d.", + out_shape.size())); + + for (size_t i = 0; i < out_shape.size(); ++i) { + PADDLE_ENFORCE_GE( + out_shape[i], + 1, + errors::InvalidArgument("The shape attribute for recv must be set " + "explicitly, but the %dth element is %d which " + "is less than 1. Or dynamic_shape should be " + "set to True for both send_v2 and recv_v2.", + i, + out_shape[i])); + } + out->set_dtype(dtype); +} + void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out) { auto dims = x.dims(); auto rank = dims.size(); diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 14c3ec0f857bd..136b8c240e5f3 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -73,6 +73,13 @@ void CastInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out); void CConcatInferMeta(const MetaTensor& x, int nranks, MetaTensor* out); +void SendV2InferMeta(const int peer, const int ring_id); + +void RecvV2InferMeta(int peer, + DataType dtype, + const std::vector& out_shape, + MetaTensor* out); + void ChannelShuffleInferMeta(const MetaTensor& x, int groups, const std::string& data_format, From 220f13bd659338d7182525da830814f5e75e641e Mon Sep 17 00:00:00 2001 From: ronnywang Date: Tue, 29 Aug 2023 21:52:15 +0800 Subject: [PATCH 009/194] [CustomDevice] Not reset pass_builder (#56755) --- paddle/fluid/inference/api/analysis_predictor.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 3ef178a2a6773..7d60d203f2b28 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -1571,6 +1571,8 @@ void AnalysisPredictor::PrepareArgument() { } } else if (config_.use_xpu()) { // All passes support fp16. Not reset pass_builder. + } else if (config_.use_custom_device()) { + // All passes support fp16. Not reset pass_builder. } else { pass_builder->ClearPasses(); } From 34eecb0eba296e9a698602781313059eac73e6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:26:47 +0800 Subject: [PATCH 010/194] [xdoctest] reformat example code with google style in No.307 (#56595) * weight_norm_hook * Update weight_norm_hook.py * Update weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py Co-authored-by: Nyakku Shigure * xdoc * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Nyakku Shigure --- python/paddle/distributed/entry_attr.py | 117 +++++++++++++----------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/python/paddle/distributed/entry_attr.py b/python/paddle/distributed/entry_attr.py index dcd5153bb5f1e..831fff7e2c9e5 100644 --- a/python/paddle/distributed/entry_attr.py +++ b/python/paddle/distributed/entry_attr.py @@ -22,22 +22,26 @@ class EntryAttr: Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.enable_static() - sparse_feature_dim = 1024 - embedding_size = 64 + >>> sparse_feature_dim = 1024 + >>> embedding_size = 64 - entry = paddle.distributed.ProbabilityEntry(0.1) + >>> entry = paddle.distributed.ProbabilityEntry(0.1) - input = paddle.static.data(name='ins', shape=[1], dtype='int64') + >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64') - emb = paddle.static.nn.sparse_embedding(( - input=input, - size=[sparse_feature_dim, embedding_size], - is_test=False, - entry=entry, - param_attr=paddle.ParamAttr(name="SparseFeatFactors", - initializer=paddle.nn.initializer.Uniform())) + >>> emb = paddle.static.nn.sparse_embedding( + ... input=input, + ... size=[sparse_feature_dim, embedding_size], + ... is_test=False, + ... entry=entry, + ... param_attr=paddle.ParamAttr( + ... name="SparseFeatFactors", + ... initializer=paddle.nn.initializer.Uniform() + ... ) + ... ) """ @@ -59,22 +63,26 @@ class ProbabilityEntry(EntryAttr): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.enable_static() - sparse_feature_dim = 1024 - embedding_size = 64 + >>> sparse_feature_dim = 1024 + >>> embedding_size = 64 - entry = paddle.distributed.ProbabilityEntry(0.1) + >>> entry = paddle.distributed.ProbabilityEntry(0.1) - input = paddle.static.data(name='ins', shape=[1], dtype='int64') + >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64') - emb = paddle.static.nn.sparse_embedding(( - input=input, - size=[sparse_feature_dim, embedding_size], - is_test=False, - entry=entry, - param_attr=paddle.ParamAttr(name="SparseFeatFactors", - initializer=paddle.nn.initializer.Uniform())) + >>> emb = paddle.static.nn.sparse_embedding( + ... input=input, + ... size=[sparse_feature_dim, embedding_size], + ... is_test=False, + ... entry=entry, + ... param_attr=paddle.ParamAttr( + ... name="SparseFeatFactors", + ... initializer=paddle.nn.initializer.Uniform() + ... ) + ... ) """ @@ -100,22 +108,26 @@ class CountFilterEntry(EntryAttr): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.enable_static() - sparse_feature_dim = 1024 - embedding_size = 64 + >>> sparse_feature_dim = 1024 + >>> embedding_size = 64 - entry = paddle.distributed.CountFilterEntry(10) + >>> entry = paddle.distributed.CountFilterEntry(10) - input = paddle.static.data(name='ins', shape=[1], dtype='int64') + >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64') - emb = paddle.static.nn.sparse_embedding(( - input=input, - size=[sparse_feature_dim, embedding_size], - is_test=False, - entry=entry, - param_attr=paddle.ParamAttr(name="SparseFeatFactors", - initializer=paddle.nn.initializer.Uniform())) + >>> emb = paddle.static.nn.sparse_embedding( + ... input=input, + ... size=[sparse_feature_dim, embedding_size], + ... is_test=False, + ... entry=entry, + ... param_attr=paddle.ParamAttr( + ... name="SparseFeatFactors", + ... initializer=paddle.nn.initializer.Uniform() + ... ) + ... ) """ @@ -144,25 +156,28 @@ class ShowClickEntry(EntryAttr): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - sparse_feature_dim = 1024 - embedding_size = 64 + >>> sparse_feature_dim = 1024 + >>> embedding_size = 64 - shows = paddle.static.data(name='show', shape=[1], dtype='int64') - clicks = paddle.static.data(name='click', shape=[1], dtype='int64') - input = paddle.static.data(name='ins', shape=[1], dtype='int64') + >>> shows = paddle.static.data(name='show', shape=[1], dtype='int64') + >>> clicks = paddle.static.data(name='click', shape=[1], dtype='int64') + >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64') - entry = paddle.distributed.ShowClickEntry("show", "click") + >>> entry = paddle.distributed.ShowClickEntry("show", "click") - emb = paddle.static.nn.sparse_embedding( - input=input, - size=[sparse_feature_dim, embedding_size], - is_test=False, - entry=entry, - param_attr=paddle.ParamAttr(name="SparseFeatFactors", - initializer=paddle.nn.initializer.Uniform())) + >>> emb = paddle.static.nn.sparse_embedding( + ... input=input, + ... size=[sparse_feature_dim, embedding_size], + ... is_test=False, + ... entry=entry, + ... param_attr=paddle.ParamAttr( + ... name="SparseFeatFactors", + ... initializer=paddle.nn.initializer.Uniform() + ... ) + ... ) """ From fc1e505ea491eb4418178cb123908adfb0347fd9 Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Wed, 30 Aug 2023 10:37:35 +0800 Subject: [PATCH 011/194] [xdoctest] reformat example code with google style in No.6-No.10 (#56146) * fix sample code * fix bug * fix bug * Update regularizer.py * Update __init__.py * Update decorator.py * fix code-style --- python/paddle/reader/__init__.py | 28 ++-- python/paddle/reader/decorator.py | 266 +++++++++++++++--------------- python/paddle/regularizer.py | 127 +++++++------- python/paddle/signal.py | 239 +++++++++++++++------------ python/paddle/sysconfig.py | 8 +- 5 files changed, 350 insertions(+), 318 deletions(-) diff --git a/python/paddle/reader/__init__.py b/python/paddle/reader/__init__.py index b810a7b8c43cb..4ce0ed643c343 100644 --- a/python/paddle/reader/__init__.py +++ b/python/paddle/reader/__init__.py @@ -31,9 +31,9 @@ items. It can be any function with no parameter that creates a iterable (anything can be used in :code:`for x in iterable`)\: -.. code-block:: python +.. code-block:: python - iterable = data_reader() + >>> iterable = data_reader() Element produced from the iterable should be a **single** entry of data, **not** a mini batch. That entry of data could be a single item, or a tuple of @@ -43,23 +43,23 @@ An example implementation for single item data reader creator: -.. code-block:: python +.. code-block:: python - def reader_creator_random_image(width, height): - def reader(): - while True: - yield numpy.random.uniform(-1, 1, size=width*height) - return reader + >>> def reader_creator_random_image(width, height): + ... def reader(): + ... while True: + ... yield numpy.random.uniform(-1, 1, size=width*height) + ... return reader An example implementation for multiple item data reader creator: -.. code-block:: python +.. code-block:: python - def reader_creator_random_image_and_label(width, height, label): - def reader(): - while True: - yield numpy.random.uniform(-1, 1, size=width*height), label - return reader + >>> def reader_creator_random_image_and_label(width, height, label): + ... def reader(): + ... while True: + ... yield numpy.random.uniform(-1, 1, size=width*height), label + ... return reader """ diff --git a/python/paddle/reader/decorator.py b/python/paddle/reader/decorator.py index bd40c4553e89d..fba81b96f6355 100644 --- a/python/paddle/reader/decorator.py +++ b/python/paddle/reader/decorator.py @@ -60,18 +60,20 @@ def cache(reader): Examples: .. code-block:: python - import paddle - - def reader(): - for i in range(3): - yield i - - # All data is cached into memory - cached_reader = paddle.io.cache(reader) - - # Output: 0 1 2 - for i in cached_reader(): - print(i) + >>> import paddle + + >>> def reader(): + ... for i in range(3): + ... yield i + ... + >>> # All data is cached into memory + >>> cached_reader = paddle.fluid.io.cache(reader) + + >>> for i in cached_reader(): + ... print(i) + 0 + 1 + 2 """ all_data = tuple(reader()) @@ -103,14 +105,14 @@ def map_readers(func, *readers): .. code-block:: python - import paddle.reader - d = {"h": 0, "i": 1} - def func(x): - return d[x] - def reader(): - yield "h" - yield "i" - map_reader_result = paddle.reader.map_readers(func, reader) + >>> import paddle.reader + >>> d = {"h": 0, "i": 1} + >>> def func(x): + ... return d[x] + >>> def reader(): + ... yield "h" + ... yield "i" + >>> map_reader_result = paddle.reader.map_readers(func, reader) """ def reader(): @@ -142,15 +144,14 @@ def shuffle(reader, buf_size): Examples: .. code-block:: python - import paddle.fluid as fluid - - def reader(): - for i in range(5): - yield i - shuffled_reader = fluid.io.shuffle(reader, 3) - for e in shuffled_reader(): - print(e) - # outputs are 0~4 unordered arrangement + >>> # doctest: +SKIP('outputs are 0~4 unordered arrangement') + >>> def reader(): + ... for i in range(5): + ... yield i + >>> shuffled_reader = paddle.reader.decorator.shuffle(reader, 3) + >>> for e in shuffled_reader(): + ... print(e) + >>> # outputs are 0~4 unordered arrangement """ def data_reader(): @@ -195,29 +196,28 @@ def chain(*readers): callable: the new chained data reader. Examples: - .. code-block:: python - - import paddle - - def reader_creator_3(start): - def reader(): - for i in range(start, start + 3): - yield [i, i, i] - return reader - - c = paddle.reader.chain(reader_creator_3(0), reader_creator_3(10), reader_creator_3(20)) - for e in c(): - print(e) - # Output: - # [0, 0, 0] - # [1, 1, 1] - # [2, 2, 2] - # [10, 10, 10] - # [11, 11, 11] - # [12, 12, 12] - # [20, 20, 20] - # [21, 21, 21] - # [22, 22, 22] + .. code-block:: python + + >>> import paddle + + >>> def reader_creator_3(start): + ... def reader(): + ... for i in range(start, start + 3): + ... yield [i, i, i] + ... return reader + ... + >>> c = paddle.reader.chain(reader_creator_3(0), reader_creator_3(10), reader_creator_3(20)) + >>> for e in c(): + ... print(e) + [0, 0, 0] + [1, 1, 1] + [2, 2, 2] + [10, 10, 10] + [11, 11, 11] + [12, 12, 12] + [20, 20, 20] + [21, 21, 21] + [22, 22, 22] """ @@ -257,13 +257,12 @@ def compose(*readers, **kwargs): Examples: .. code-block:: python - import paddle.fluid as fluid - def reader_creator_10(dur): - def reader(): - for i in range(10): - yield i - return reader - reader = fluid.io.compose(reader_creator_10(0), reader_creator_10(0)) + >>> def reader_creator_10(dur): + ... def reader(): + ... for i in range(10): + ... yield i + ... return reader + >>> reader = paddle.reader.decorator.compose(reader_creator_10(0), reader_creator_10(0)) """ check_alignment = kwargs.pop('check_alignment', True) @@ -311,18 +310,21 @@ def buffered(reader, size): Examples: .. code-block:: python - import paddle - - def reader(): - for i in range(3): - yield i - - # Create a buffered reader, and the buffer size is 2. - buffered_reader = paddle.io.buffered(reader, 2) - - # Output: 0 1 2 - for i in buffered_reader(): - print(i) + >>> import paddle + + >>> def reader(): + ... for i in range(3): + ... yield i + ... + >>> # Create a buffered reader, and the buffer size is 2. + >>> buffered_reader = paddle.reader.decorator.buffered(reader, 2) + + >>> # Output: 0 1 2 + >>> for i in buffered_reader(): + ... print(i) + 0 + 1 + 2 """ class EndSignal: @@ -373,15 +375,17 @@ def firstn(reader, n): Examples: .. code-block:: python - import paddle.fluid as fluid - - def reader(): - for i in range(100): - yield i - firstn_reader = fluid.io.firstn(reader, 5) - for e in firstn_reader(): - print(e) - # the outputs are: 0 1 2 3 4 + >>> def reader(): + ... for i in range(100): + ... yield i + >>> firstn_reader = paddle.reader.decorator.firstn(reader, 5) + >>> for e in firstn_reader(): + ... print(e) + 0 + 1 + 2 + 3 + 4 """ # TODO(yuyang18): Check if just drop the reader, could clean the opened @@ -523,60 +527,56 @@ def multiprocess_reader(readers, use_pipe=True, queue_size=1000): Example: - .. code-block:: python - import paddle - import paddle.fluid as fluid - from paddle.fluid.io import multiprocess_reader - import numpy as np - - sample_files = ['sample_file_1', 'sample_file_2'] - - def fake_input_files(): - with open(sample_files[0], 'w') as f: - np.savez(f, a=np.array([1, 2]), b=np.array([3, 4]), c=np.array([5, 6]), d=np.array([7, 8])) - with open(sample_files[1], 'w') as f: - np.savez(f, a=np.array([9, 10]), b=np.array([11, 12]), c=np.array([13, 14])) - - - def generate_reader(file_name): - # load data file - def _impl(): - data = np.load(file_name) - for item in sorted(data.files): - yield data[item], - return _impl - - if __name__ == '__main__': - # generate sample input files - fake_input_files() - - with fluid.program_guard(fluid.Program(), fluid.Program()): - place = fluid.CPUPlace() - # the 1st 2 is batch size - - image = paddle.static.data(name='image', dtype='int64', shape=[2, 1, 2]) - paddle.static.Print(image) - # print detailed tensor info of image variable - - reader = fluid.io.PyReader(feed_list=[image], capacity=2) - - decorated_reader = multiprocess_reader( - [generate_reader(sample_files[0]), generate_reader(sample_files[1])], False) - - reader.decorate_sample_generator(decorated_reader, batch_size=2, places=[place]) - - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - - for data in reader(): - res = exe.run(feed=data, fetch_list=[image]) - print(res[0]) - # print below content in this case - # [[[1 2]], [[3 4]]] - # [[[5 6]], [[7 8]]] - # [[[9 10]], [[11 12]]] - # [13,14] will be dropped + .. code-block:: python + >>> import paddle + >>> import numpy as np + + >>> sample_files = ['sample_file_1', 'sample_file_2'] + + >>> def fake_input_files(): + ... with open(sample_files[0], 'wb') as f: + ... np.savez(f, a=np.array([1, 2]), b=np.array([3, 4]), c=np.array([5, 6]), d=np.array([7, 8])) + ... with open(sample_files[1], 'wb') as f: + ... np.savez(f, a=np.array([9, 10]), b=np.array([11, 12]), c=np.array([13, 14])) + ... + ... + >>> def generate_reader(file_name): + ... # load data file + ... def _impl(): + ... data = np.load(file_name) + ... for item in sorted(data.files): + ... yield data[item], + ... return _impl + ... + >>> if __name__ == '__main__': + ... # generate sample input files + ... fake_input_files() + ... + ... with fluid.program_guard(fluid.Program(), fluid.Program()): + ... place = fluid.CPUPlace() + ... # the 1st 2 is batch size + ... + ... image = paddle.static.data(name='image', dtype='int64', shape=[2, 1, 2]) + ... paddle.static.Print(image) + ... # print detailed tensor info of image variable + ... + ... reader = fluid.io.PyReader(feed_list=[image], capacity=2) + ... + ... decorated_reader = paddle.reader.multiprocess_reader( + ... [generate_reader(sample_files[0]), generate_reader(sample_files[1])], False) + ... + ... reader.decorate_sample_generator(decorated_reader, batch_size=2, places=[place]) + ... + ... exe = fluid.Executor(place) + ... exe.run(fluid.default_startup_program()) + ... + ... for data in reader(): + ... res = exe.run(feed=data, fetch_list=[image]) + ... print(res[0]) + [[[1 2]], [[3 4]]] + [[[5 6]], [[7 8]]] + [[[9 10]], [[11 12]]] """ if sys.platform == 'win32': diff --git a/python/paddle/regularizer.py b/python/paddle/regularizer.py index 9a746cb4bf53c..501a06e32f89e 100644 --- a/python/paddle/regularizer.py +++ b/python/paddle/regularizer.py @@ -67,42 +67,42 @@ class L1Decay(WeightDecayRegularizer): .. code-block:: python :name: code-example1 - # Example1: set Regularizer in optimizer - import paddle - from paddle.regularizer import L1Decay - - linear = paddle.nn.Linear(10, 10) - inp = paddle.rand(shape=[10, 10], dtype="float32") - out = linear(inp) - loss = paddle.mean(out) - beta1 = paddle.to_tensor([0.9], dtype="float32") - beta2 = paddle.to_tensor([0.99], dtype="float32") - momentum = paddle.optimizer.Momentum( - learning_rate=0.1, - parameters=linear.parameters(), - weight_decay=L1Decay(0.0001)) - back = out.backward() - momentum.step() - momentum.clear_grad() + >>> # Example1: set Regularizer in optimizer + >>> import paddle + >>> from paddle.regularizer import L1Decay + + >>> linear = paddle.nn.Linear(10, 10) + >>> inp = paddle.rand(shape=[10, 10], dtype="float32") + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> beta1 = paddle.to_tensor([0.9], dtype="float32") + >>> beta2 = paddle.to_tensor([0.99], dtype="float32") + >>> momentum = paddle.optimizer.Momentum( + ... learning_rate=0.1, + ... parameters=linear.parameters(), + ... weight_decay=L1Decay(0.0001)) + >>> back = out.backward() + >>> momentum.step() + >>> momentum.clear_grad() .. code-block:: python :name: code-example2 - # Example2: set Regularizer in parameters - # Set L1 regularization in parameters. - # Global regularizer does not take effect on my_conv2d for this case. - from paddle.nn import Conv2D - from paddle import ParamAttr - from paddle.regularizer import L2Decay - - my_conv2d = Conv2D( - in_channels=10, - out_channels=10, - kernel_size=1, - stride=1, - padding=0, - weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)), - bias_attr=False) + >>> # Example2: set Regularizer in parameters + >>> # Set L1 regularization in parameters. + >>> # Global regularizer does not take effect on my_conv2d for this case. + >>> from paddle.nn import Conv2D + >>> from paddle import ParamAttr + >>> from paddle.regularizer import L1Decay + + >>> my_conv2d = Conv2D( + ... in_channels=10, + ... out_channels=10, + ... kernel_size=1, + ... stride=1, + ... padding=0, + ... weight_attr=ParamAttr(regularizer=L1Decay(coeff=0.01)), + ... bias_attr=False) """ def __init__(self, coeff=0.0): @@ -178,40 +178,41 @@ class L2Decay(WeightDecayRegularizer): .. code-block:: python :name: code-example1 - # Example1: set Regularizer in optimizer - import paddle - from paddle.regularizer import L2Decay - linear = paddle.nn.Linear(10, 10) - inp = paddle.rand(shape=[10, 10], dtype="float32") - out = linear(inp) - loss = paddle.mean(out) - beta1 = paddle.to_tensor([0.9], dtype="float32") - beta2 = paddle.to_tensor([0.99], dtype="float32") - momentum = paddle.optimizer.Momentum( - learning_rate=0.1, - parameters=linear.parameters(), - weight_decay=L2Decay(0.0001)) - back = out.backward() - momentum.step() - momentum.clear_grad() + >>> # Example1: set Regularizer in optimizer + >>> import paddle + >>> from paddle.regularizer import L2Decay + >>> linear = paddle.nn.Linear(10, 10) + >>> inp = paddle.rand(shape=[10, 10], dtype="float32") + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> beta1 = paddle.to_tensor([0.9], dtype="float32") + >>> beta2 = paddle.to_tensor([0.99], dtype="float32") + >>> momentum = paddle.optimizer.Momentum( + ... learning_rate=0.1, + ... parameters=linear.parameters(), + ... weight_decay=L2Decay(0.0001)) + >>> back = out.backward() + >>> momentum.step() + >>> momentum.clear_grad() .. code-block:: python :name: code-example2 - # Example2: set Regularizer in parameters - # Set L2 regularization in parameters. - # Global regularizer does not take effect on my_conv2d for this case. - from paddle.nn import Conv2D - from paddle import ParamAttr - from paddle.regularizer import L2Decay - - my_conv2d = Conv2D( - in_channels=10, - out_channels=10, - kernel_size=1, - stride=1, - padding=0, - weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)), - bias_attr=False) + + >>> # Example2: set Regularizer in parameters + >>> # Set L2 regularization in parameters. + >>> # Global regularizer does not take effect on my_conv2d for this case. + >>> from paddle.nn import Conv2D + >>> from paddle import ParamAttr + >>> from paddle.regularizer import L2Decay + + >>> my_conv2d = Conv2D( + ... in_channels=10, + ... out_channels=10, + ... kernel_size=1, + ... stride=1, + ... padding=0, + ... weight_attr=ParamAttr(regularizer=L2Decay(coeff=0.01)), + ... bias_attr=False) """ def __init__(self, coeff=0.0): diff --git a/python/paddle/signal.py b/python/paddle/signal.py index f61df7a2b07a3..d1dc910f043ea 100644 --- a/python/paddle/signal.py +++ b/python/paddle/signal.py @@ -49,60 +49,57 @@ def frame(x, frame_length, hop_length, axis=-1, name=None): Examples: - .. code-block:: python - - import paddle - from paddle.signal import frame - - # 1D - x = paddle.arange(8) - y0 = frame(x, frame_length=4, hop_length=2, axis=-1) # [4, 3] - # [[0, 2, 4], - # [1, 3, 5], - # [2, 4, 6], - # [3, 5, 7]] - - y1 = frame(x, frame_length=4, hop_length=2, axis=0) # [3, 4] - # [[0, 1, 2, 3], - # [2, 3, 4, 5], - # [4, 5, 6, 7]] - - # 2D - x0 = paddle.arange(16).reshape([2, 8]) - y0 = frame(x0, frame_length=4, hop_length=2, axis=-1) # [2, 4, 3] - # [[[0, 2, 4], - # [1, 3, 5], - # [2, 4, 6], - # [3, 5, 7]], - # - # [[8 , 10, 12], - # [9 , 11, 13], - # [10, 12, 14], - # [11, 13, 15]]] - - x1 = paddle.arange(16).reshape([8, 2]) - y1 = frame(x1, frame_length=4, hop_length=2, axis=0) # [3, 4, 2] - # [[[0 , 1 ], - # [2 , 3 ], - # [4 , 5 ], - # [6 , 7 ]], - # - # [4 , 5 ], - # [6 , 7 ], - # [8 , 9 ], - # [10, 11]], - # - # [8 , 9 ], - # [10, 11], - # [12, 13], - # [14, 15]]] - - # > 2D - x0 = paddle.arange(32).reshape([2, 2, 8]) - y0 = frame(x0, frame_length=4, hop_length=2, axis=-1) # [2, 2, 4, 3] - - x1 = paddle.arange(32).reshape([8, 2, 2]) - y1 = frame(x1, frame_length=4, hop_length=2, axis=0) # [3, 4, 2, 2] + .. code-block:: python + + >>> import paddle + >>> from paddle import signal + + >>> # 1D + >>> x = paddle.arange(8) + >>> y0 = signal.frame(x, frame_length=4, hop_length=2, axis=-1) + >>> print(y0) + Tensor(shape=[4, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 2, 4], + [1, 3, 5], + [2, 4, 6], + [3, 5, 7]]) + + >>> y1 = signal.frame(x, frame_length=4, hop_length=2, axis=0) + >>> print(y1) + Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 1, 2, 3], + [2, 3, 4, 5], + [4, 5, 6, 7]]) + + >>> # 2D + >>> x0 = paddle.arange(16).reshape([2, 8]) + >>> y0 = signal.frame(x0, frame_length=4, hop_length=2, axis=-1) + >>> print(y0) + Tensor(shape=[2, 4, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[0 , 2 , 4 ], + [1 , 3 , 5 ], + [2 , 4 , 6 ], + [3 , 5 , 7 ]], + [[8 , 10, 12], + [9 , 11, 13], + [10, 12, 14], + [11, 13, 15]]]) + + >>> x1 = paddle.arange(16).reshape([8, 2]) + >>> y1 = signal.frame(x1, frame_length=4, hop_length=2, axis=0) + >>> print(y1.shape) + [3, 4, 2] + + >>> # > 2D + >>> x0 = paddle.arange(32).reshape([2, 2, 8]) + >>> y0 = signal.frame(x0, frame_length=4, hop_length=2, axis=-1) + >>> print(y0.shape) + [2, 2, 4, 3] + + >>> x1 = paddle.arange(32).reshape([8, 2, 2]) + >>> y1 = signal.frame(x1, frame_length=4, hop_length=2, axis=0) + >>> print(y1.shape) + [3, 4, 2, 2] """ if axis not in [0, -1]: raise ValueError(f'Unexpected axis: {axis}. It should be 0 or -1.') @@ -167,36 +164,53 @@ def overlap_add(x, hop_length, axis=-1, name=None): Examples: - .. code-block:: python - - import paddle - from paddle.signal import overlap_add - - # 2D - x0 = paddle.arange(16).reshape([8, 2]) - # [[0 , 1 ], - # [2 , 3 ], - # [4 , 5 ], - # [6 , 7 ], - # [8 , 9 ], - # [10, 11], - # [12, 13], - # [14, 15]] - y0 = overlap_add(x0, hop_length=2, axis=-1) # [10] - # [0 , 2 , 5 , 9 , 13, 17, 21, 25, 13, 15] - - x1 = paddle.arange(16).reshape([2, 8]) - # [[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ], - # [8 , 9 , 10, 11, 12, 13, 14, 15]] - y1 = overlap_add(x1, hop_length=2, axis=0) # [10] - # [0 , 1 , 10, 12, 14, 16, 18, 20, 14, 15] - - # > 2D - x0 = paddle.arange(32).reshape([2, 1, 8, 2]) - y0 = overlap_add(x0, hop_length=2, axis=-1) # [2, 1, 10] - - x1 = paddle.arange(32).reshape([2, 8, 1, 2]) - y1 = overlap_add(x1, hop_length=2, axis=0) # [10, 1, 2] + .. code-block:: python + + >>> import paddle + >>> from paddle.signal import overlap_add + + >>> # 2D + >>> x0 = paddle.arange(16).reshape([8, 2]) + >>> print(x0) + Tensor(shape=[8, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0 , 1 ], + [2 , 3 ], + [4 , 5 ], + [6 , 7 ], + [8 , 9 ], + [10, 11], + [12, 13], + [14, 15]]) + + + >>> y0 = overlap_add(x0, hop_length=2, axis=-1) + >>> print(y0) + Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True, + [0 , 2 , 5 , 9 , 13, 17, 21, 25, 13, 15]) + + >>> x1 = paddle.arange(16).reshape([2, 8]) + >>> print(x1) + Tensor(shape=[2, 8], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ], + [8 , 9 , 10, 11, 12, 13, 14, 15]]) + + + >>> y1 = overlap_add(x1, hop_length=2, axis=0) + >>> print(y1) + Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True, + [0 , 1 , 10, 12, 14, 16, 18, 20, 14, 15]) + + + >>> # > 2D + >>> x0 = paddle.arange(32).reshape([2, 1, 8, 2]) + >>> y0 = overlap_add(x0, hop_length=2, axis=-1) + >>> print(y0.shape) + [2, 1, 10] + + >>> x1 = paddle.arange(32).reshape([2, 8, 1, 2]) + >>> y1 = overlap_add(x1, hop_length=2, axis=0) + >>> print(y1.shape) + [10, 1, 2] """ if axis not in [0, -1]: raise ValueError(f'Unexpected axis: {axis}. It should be 0 or -1.') @@ -289,18 +303,30 @@ def stft( Examples: .. code-block:: python - import paddle - from paddle.signal import stft + >>> import paddle + >>> from paddle.signal import stft + + >>> # real-valued input + >>> x = paddle.randn([8, 48000], dtype=paddle.float64) + >>> y1 = stft(x, n_fft=512) + >>> print(y1.shape) + [8, 257, 376] + + >>> y2 = stft(x, n_fft=512, onesided=False) + >>> print(y2.shape) + [8, 512, 376] - # real-valued input - x = paddle.randn([8, 48000], dtype=paddle.float64) - y1 = stft(x, n_fft=512) # [8, 257, 376] - y2 = stft(x, n_fft=512, onesided=False) # [8, 512, 376] + >>> # complex input + >>> x = paddle.randn([8, 48000], dtype=paddle.float64) + \ + ... paddle.randn([8, 48000], dtype=paddle.float64)*1j + >>> print(x.shape) + [8, 48000] + >>> print(x.dtype) + paddle.complex128 - # complex input - x = paddle.randn([8, 48000], dtype=paddle.float64) + \ - paddle.randn([8, 48000], dtype=paddle.float64)*1j # [8, 48000] complex128 - y1 = stft(x, n_fft=512, center=False, onesided=False) # [8, 512, 372] + >>> y1 = stft(x, n_fft=512, center=False, onesided=False) + >>> print(y1.shape) + [8, 512, 372] """ @@ -465,20 +491,25 @@ def istft( Examples: .. code-block:: python - import numpy as np - import paddle - from paddle.signal import stft, istft + >>> import numpy as np + >>> import paddle + >>> from paddle.signal import stft, istft - paddle.seed(0) + >>> paddle.seed(0) - # STFT - x = paddle.randn([8, 48000], dtype=paddle.float64) - y = stft(x, n_fft=512) # [8, 257, 376] + >>> # STFT + >>> x = paddle.randn([8, 48000], dtype=paddle.float64) + >>> y = stft(x, n_fft=512) + >>> print(y.shape) + [8, 257, 376] - # ISTFT - x_ = istft(y, n_fft=512) # [8, 48000] + >>> # ISTFT + >>> x_ = istft(y, n_fft=512) + >>> print(x_.shape) + [8, 48000] - np.allclose(x, x_) # True + >>> np.allclose(x, x_) + True """ check_variable_and_dtype(x, 'x', ['complex64', 'complex128'], 'istft') diff --git a/python/paddle/sysconfig.py b/python/paddle/sysconfig.py index 720f07a4ce2fc..e717eaa05d38d 100644 --- a/python/paddle/sysconfig.py +++ b/python/paddle/sysconfig.py @@ -27,8 +27,8 @@ def get_include(): Examples: .. code-block:: python - import paddle - include_dir = paddle.sysconfig.get_include() + >>> import paddle + >>> include_dir = paddle.sysconfig.get_include() """ import paddle @@ -46,8 +46,8 @@ def get_lib(): Examples: .. code-block:: python - import paddle - include_dir = paddle.sysconfig.get_lib() + >>> import paddle + >>> include_dir = paddle.sysconfig.get_lib() """ import paddle From 5cbf5bd4f0be0b9203daa3dc868cb8e263929f7f Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Wed, 30 Aug 2023 10:47:46 +0800 Subject: [PATCH 012/194] =?UTF-8?q?=E3=80=90complex=20op=E3=80=91No.6=20ad?= =?UTF-8?q?d=20complex=20support=20for=20logical=5Fand/or/xor/not=20(#5632?= =?UTF-8?q?3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 【complex op】No.6 add complex support for logical_and/or/xor/not * fix dtype check * modify the docs * add special condition for not raise when x.dtype is complex * add random generate for complex dtype * fix generate for complex * fix * fix * add corner case for complex type * fix ut * fix ut --- paddle/phi/kernels/cpu/logical_kernel.cc | 2 ++ paddle/phi/kernels/kps/logical_kernel.cu | 2 ++ python/paddle/tensor/logic.py | 18 ++++++---- test/legacy_test/test_logical_op.py | 45 +++++++++++++++++++++++- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/paddle/phi/kernels/cpu/logical_kernel.cc b/paddle/phi/kernels/cpu/logical_kernel.cc index 33e3ea6e05f7e..06dff8428533f 100644 --- a/paddle/phi/kernels/cpu/logical_kernel.cc +++ b/paddle/phi/kernels/cpu/logical_kernel.cc @@ -77,6 +77,8 @@ void LogicalNotKernel(const Context& dev_ctx, int64_t, \ int, \ int8_t, \ + phi::dtype::complex, \ + phi::dtype::complex, \ int16_t) {} REGISTER_LOGICAL_CPU_KERNEL(logical_and, And) diff --git a/paddle/phi/kernels/kps/logical_kernel.cu b/paddle/phi/kernels/kps/logical_kernel.cu index d2a6346fd3f04..f7c390e65d0ff 100644 --- a/paddle/phi/kernels/kps/logical_kernel.cu +++ b/paddle/phi/kernels/kps/logical_kernel.cu @@ -97,6 +97,8 @@ PD_REGISTER_KERNEL(logical_xor, KPS, ALL_LAYOUT, phi::LogicalXorKernel, int) { int64_t, \ int, \ int8_t, \ + phi::dtype::complex, \ + phi::dtype::complex, \ int16_t) {} REGISTER_LOGICAL_CUDA_KERNEL(logical_and, And) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index c9009b37ab71a..6632738695d7e 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -53,6 +53,8 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True): "float32", "float64", "uint16", + "complex64", + "complex128", ], op_name, ) @@ -70,6 +72,8 @@ def _logical_op(op_name, x, y, out=None, name=None, binary_op=True): "float32", "float64", "uint16", + "complex64", + "complex128", ], op_name, ) @@ -114,8 +118,8 @@ def logical_and(x, y, out=None, name=None): .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64. + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128. out(Tensor, optional): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -173,8 +177,8 @@ def logical_or(x, y, out=None, name=None): .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64. + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, in32, in64, float16, float32, float64, complex64, complex128. out(Tensor): The ``Variable`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -234,8 +238,8 @@ def logical_xor(x, y, out=None, name=None): .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: - x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64. - y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64. + x (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128. + y (Tensor): the input tensor, it's data type should be one of bool, int8, int16, int32, int64, float16, float32, float64, complex64, complex128. out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor`` will be created to save the output. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -296,7 +300,7 @@ def logical_not(x, out=None, name=None): .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: - x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64. + x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64, complex64, complex128. out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. name(str|None): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. diff --git a/test/legacy_test/test_logical_op.py b/test/legacy_test/test_logical_op.py index 80a42d4145181..10bf33e40a5e0 100755 --- a/test/legacy_test/test_logical_op.py +++ b/test/legacy_test/test_logical_op.py @@ -31,6 +31,8 @@ np.float16, np.float32, np.float64, + np.complex64, + np.complex128, ] TEST_META_OP_DATA = [ @@ -124,6 +126,10 @@ def np_data_generator(np_shape, dtype, *args, **kwargs): elif dtype == np.uint16: x = np.random.uniform(0.0, 1.0, np_shape).astype(np.float32) return convert_float_to_uint16(x) + elif dtype == np.complex64 or dtype == np.complex128: + return np.random.normal(0, 1, np_shape).astype(dtype) + ( + 1.0j * np.random.normal(0, 1, np_shape) + ).astype(dtype) else: return np.random.normal(0, 1, np_shape).astype(dtype) @@ -169,6 +175,41 @@ def test(unit_test, use_gpu=False, test_error=False): (dygraph_result.numpy() == np_result).all() ) unit_test.assertTrue((eager_result.numpy() == np_result).all()) + # add some corner case for complex datatype + for complex_data_type in [np.complex64, np.complex128]: + for x_data in (0 + 0j, 0 + 1j, 1 + 0j, 1 + 1j): + for y_data in (0 + 0j, 0 + 1j, 1 + 0j, 1 + 1j): + meta_data['x_np'] = ( + x_data * np.ones(shape_data['x_shape']) + ).astype(complex_data_type) + meta_data['y_np'] = ( + y_data * np.ones(shape_data['y_shape']) + ).astype(complex_data_type) + if meta_data['binary_op'] and test_error: + # catch C++ Exception + unit_test.assertRaises( + BaseException, run_static, **meta_data + ) + unit_test.assertRaises( + BaseException, run_dygraph, **meta_data + ) + continue + static_result = run_static(**meta_data) + dygraph_result = run_dygraph(**meta_data) + eager_result = run_eager(**meta_data) + if meta_data['binary_op']: + np_result = np_op( + meta_data['x_np'], meta_data['y_np'] + ) + else: + np_result = np_op(meta_data['x_np']) + unit_test.assertTrue((static_result == np_result).all()) + unit_test.assertTrue( + (dygraph_result.numpy() == np_result).all() + ) + unit_test.assertTrue( + (eager_result.numpy() == np_result).all() + ) def test_type_error(unit_test, use_gpu, type_str_map): @@ -180,7 +221,9 @@ def check_type(op_str, x, y, binary_op): y = paddle.to_tensor(y) error_type = BaseException if binary_op: - if type_str_map['x'] != type_str_map['y']: + if type_str_map['x'] != type_str_map['y'] and type_str_map[ + 'x' + ] not in [np.complex64, np.complex128]: unit_test.assertRaises(error_type, op, x=x, y=y) if not in_dynamic_mode(): error_type = TypeError From 6d19073ad5c20cce98acb6296f1155bade5e6c31 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:06:50 +0800 Subject: [PATCH 013/194] [clang-tidy] enable clang-analyzer-optin.cplusplus.UninitializedObject check (#56648) --- .clang-tidy | 2 +- paddle/phi/common/place.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 820a244182d23..18576ae15bb36 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -83,7 +83,7 @@ clang-analyzer-cplusplus.InnerPointer, -clang-analyzer-nullability.NullableDereferenced, -clang-analyzer-nullability.NullablePassedToNonnull, -clang-analyzer-nullability.NullableReturnedFromNonnull, --clang-analyzer-optin.cplusplus.UninitializedObject, +clang-analyzer-optin.cplusplus.UninitializedObject, -clang-analyzer-optin.cplusplus.VirtualCall, -clang-analyzer-optin.mpi.MPI-Checker, -clang-analyzer-optin.osx.OSObjectCStyleCast, diff --git a/paddle/phi/common/place.h b/paddle/phi/common/place.h index 09f7c53cde80d..82731d9984dc8 100644 --- a/paddle/phi/common/place.h +++ b/paddle/phi/common/place.h @@ -54,7 +54,8 @@ const char* AllocationTypeStr(AllocationType type); /// \brief The place is used to specify where the data is stored. class PADDLE_API Place { public: - Place() : device(0), alloc_type_(AllocationType::UNDEFINED) {} + Place() + : device(0), alloc_type_(AllocationType::UNDEFINED), device_type_id_(0) {} explicit Place(AllocationType type, int8_t id, From 609c032118aa713407939cc87bd241c3ad2d2d18 Mon Sep 17 00:00:00 2001 From: yoyoIcy <142126786+yoyoIcy@users.noreply.github.com> Date: Tue, 29 Aug 2023 22:36:40 -0500 Subject: [PATCH 014/194] [Doctest]fix No.303, test=docs_preview (#56777) --- python/paddle/incubate/tensor/manipulation.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/python/paddle/incubate/tensor/manipulation.py b/python/paddle/incubate/tensor/manipulation.py index 4e1380a0bd723..70c017a8eec36 100644 --- a/python/paddle/incubate/tensor/manipulation.py +++ b/python/paddle/incubate/tensor/manipulation.py @@ -40,12 +40,14 @@ def _npu_identity(x, format=-1): Examples: .. code-block:: python - # required: npu - import paddle + >>> # doctest: +REQUIRES(env:NPU) + >>> import paddle + >>> paddle.device.set_device('npu') - x = paddle.ones(shape=[6]) - y = paddle.incubate._npu_identity(x, 3) # ACL_FORMAT_NC1HWC0 = 3 - # y.shape = [1, 1, 1, 1, 16] + >>> x = paddle.ones(shape=[6]) + >>> y = paddle.incubate._npu_identity(x, 3) # ACL_FORMAT_NC1HWC0 = 3 + >>> print(y.shape) + [1, 1, 1, 1, 16] """ if in_dynamic_mode(): return _C_ops.npu_identity(x, format) From 31a9688868e05d6df1b8b9d56df3dc1253e49a55 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Wed, 30 Aug 2023 11:45:23 +0800 Subject: [PATCH 015/194] [clang-tidy][task 61] enable `hicpp-exception-baseclass` and fix existing errors (#55847) * [clang-tidy] enable `hicpp-exception-baseclass` and fix existing errors * config * update error format to pass the ci check (at least 20 chars) --- .clang-tidy | 2 +- .../ir/mkldnn/reshape_transpose_matmul_mkldnn_fuse_pass.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 18576ae15bb36..37d956eaa943b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -162,7 +162,7 @@ cppcoreguidelines-c-copy-assignment-signature, -cppcoreguidelines-pro-type-member-init, -cppcoreguidelines-slicing, -hicpp-avoid-goto, --hicpp-exception-baseclass, +hicpp-exception-baseclass, misc-unused-alias-decls, misc-unused-using-decls, modernize-avoid-bind, diff --git a/paddle/fluid/framework/ir/mkldnn/reshape_transpose_matmul_mkldnn_fuse_pass.cc b/paddle/fluid/framework/ir/mkldnn/reshape_transpose_matmul_mkldnn_fuse_pass.cc index e5a8c5330e15a..07675a3f4efeb 100644 --- a/paddle/fluid/framework/ir/mkldnn/reshape_transpose_matmul_mkldnn_fuse_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/reshape_transpose_matmul_mkldnn_fuse_pass.cc @@ -99,8 +99,8 @@ void ReshapeTransposeMatmulMkldnnFusePass::Fuse( } else if (matmul_desc->Inputs().at("Y").at(0) == input_var_name) { matmul_input_name = "Y"; } else { - throw platform::errors::InvalidArgument("Unexpected input to " + - matmul_type + " encountered."); + PADDLE_THROW(platform::errors::InvalidArgument( + "Unexpected input to %s encountered.", matmul_type)); } // Return if input of fused_matmul is already fused From c5786be10719497170ed5b0c401edd827b041dda Mon Sep 17 00:00:00 2001 From: ronnywang Date: Wed, 30 Aug 2023 13:02:49 +0800 Subject: [PATCH 016/194] [CustomDevice] Fix error that query a destroyed event (#56745) --- .../memory/allocation/stream_safe_custom_device_allocator.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc index b88f40f6a2732..ce63ab807e01e 100644 --- a/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc +++ b/paddle/fluid/memory/allocation/stream_safe_custom_device_allocator.cc @@ -88,8 +88,7 @@ bool StreamSafeCustomDeviceAllocation::CanBeFreed() { } std::call_once(once_flag_, [this] { phi::DeviceManager::SetDevice(place_); }); for (auto it = outstanding_event_map_.begin(); - it != outstanding_event_map_.end(); - ++it) { + it != outstanding_event_map_.end();) { auto& event = it->second; if (!event->Query()) { VLOG(9) << "Event " << event->raw_event() << " for " << ptr() @@ -98,6 +97,7 @@ bool StreamSafeCustomDeviceAllocation::CanBeFreed() { } VLOG(8) << "Destroy event " << event->raw_event(); event->Destroy(); + it = outstanding_event_map_.erase(it); } outstanding_event_map_.clear(); return true; From 5d164968e92f307cde54d67ceb0fe7cf4791c67d Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Wed, 30 Aug 2023 13:05:00 +0800 Subject: [PATCH 017/194] [docs] fix api labels in math.py (#56682) --- python/paddle/tensor/math.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 086edfd4dd70b..5bca583fe6066 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -435,7 +435,7 @@ def multiplex(inputs, index, name=None): def scale_(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): """ Inplace version of ``scale`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_scale`. + Please refer to :ref:`api_paddle_scale`. """ if in_dynamic_mode(): return _C_ops.scale_(x, scale, float(bias), bias_after_scale) @@ -685,7 +685,7 @@ def add(x, y, name=None): def add_(x, y, name=None): """ Inplace version of ``add`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_add`. + Please refer to :ref:`api_paddle_add`. """ out_shape = broadcast_shape(x.shape, y.shape) @@ -825,7 +825,7 @@ def subtract(x, y, name=None): def subtract_(x, y, name=None): """ Inplace version of ``subtract`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_subtract`. + Please refer to :ref:`api_paddle_subtract`. """ out_shape = broadcast_shape(x.shape, y.shape) @@ -1003,7 +1003,7 @@ def remainder(x, y, name=None): def remainder_(x, y, name=None): r""" Inplace version of ``remainder`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_remainder`. + Please refer to :ref:`api_paddle_remainder`. """ out_shape = broadcast_shape(x.shape, y.shape) if out_shape != x.shape: @@ -1086,7 +1086,7 @@ def multiply(x, y, name=None): def multiply_(x, y, name=None): """ Inplace version of ``multiply`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_multiply`. + Please refer to :ref:`api_paddle_multiply`. """ out_shape = broadcast_shape(x.shape, y.shape) @@ -2287,7 +2287,7 @@ def addmm(input, x, y, beta=1.0, alpha=1.0, name=None): def addmm_(input, x, y, beta=1.0, alpha=1.0, name=None): """ Inplace version of ``addmm`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_label_addmm`. + Please refer to :ref:`api_paddle_addmm`. """ input_shape = input.shape x_shape = x.shape @@ -3575,7 +3575,7 @@ def clip(x, min=None, max=None, name=None): def clip_(x, min=None, max=None, name=None): """ Inplace version of ``clip`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_clip`. + Please refer to :ref:`api_paddle_clip`. """ fmin = float(np.finfo(np.float32).min) fmax = float(np.finfo(np.float32).max) @@ -4638,7 +4638,7 @@ def tanh(x, name=None): def tanh_(x, name=None): r""" Inplace version of ``tanh`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_tanh`. + Please refer to :ref:`api_paddle_tanh`. """ return _C_ops.tanh_(x) @@ -5287,7 +5287,7 @@ def lerp(x, y, weight, name=None): def lerp_(x, y, weight, name=None): r""" Inplace version of ``lerp`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_lerp`. + Please refer to :ref:`api_paddle_lerp`. """ out_shape = broadcast_shape(x.shape, y.shape) check_type(weight, 'weight', (float, paddle.Tensor, Variable), 'lerp') @@ -5347,7 +5347,7 @@ def erfinv(x, name=None): def erfinv_(x, name=None): r""" Inplace version of ``erfinv`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_tensor_erfinv`. + Please refer to :ref:`api_paddle_erfinv`. """ check_type(x, 'x', (paddle.Tensor, Variable), 'erfinv') return _C_ops.erfinv_(x) From e457c29852df5fe418b4743fac4cd19b069fedcf Mon Sep 17 00:00:00 2001 From: cyber-pioneer <116002591+cyber-pioneer@users.noreply.github.com> Date: Wed, 30 Aug 2023 13:41:13 +0800 Subject: [PATCH 018/194] [Prim][NewIR] Support prim all in new IR (#56614) * support prim all in new ir * process makefile * fix rule bug * polish case * fix flag * fix rules bug --- python/paddle/decomposition/decomp.py | 4 +- python/paddle/decomposition/rules.py | 5 +- .../incubate/autograd/composite_rules.py | 9 +- test/prim/new_ir_prim/CMakeLists.txt | 16 +++- test/prim/new_ir_prim/test_decomp_op.py | 3 + test/prim/new_ir_prim/test_prim_program.py | 94 +++++++++++++++++++ 6 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 test/prim/new_ir_prim/test_prim_program.py diff --git a/python/paddle/decomposition/decomp.py b/python/paddle/decomposition/decomp.py index 9bd288dacd481..940249d0ae526 100644 --- a/python/paddle/decomposition/decomp.py +++ b/python/paddle/decomposition/decomp.py @@ -27,7 +27,7 @@ def _build_tensor_tuple(xs): return (xs,) elif isinstance(xs, typing.Sequence): return tuple(xs) - return TypeError(f"Type {type(xs)} is not supported") + return TypeError(f"Type {type(xs)} is not supported.") def _prepare_python_api_arguments(op): @@ -125,6 +125,8 @@ def decompose( Returns: dst_vars (list): A list contains all vars which replace origin ones in src_vars. """ + if not core._is_fwd_prim_enabled(): + return src_vars if not isinstance(program, Program): raise TypeError(f"Expect type Program, but got type {type(program)}.") block = program.block() diff --git a/python/paddle/decomposition/rules.py b/python/paddle/decomposition/rules.py index ec8959cc960c0..4184dbdbea62e 100644 --- a/python/paddle/decomposition/rules.py +++ b/python/paddle/decomposition/rules.py @@ -20,8 +20,9 @@ def mean(x, axis, keepdim): """define composite rule of op mean""" x_shape = x.shape - axes = axis or tuple(range(0, len(x_shape))) - axes = (axes,) if isinstance(axes, int) else axes + if axis in (None, []): + axis = tuple(range(0, len(x_shape))) + axes = (axis,) if isinstance(axis, int) else axis sum_x = sum(x, axis=axes, keepdim=keepdim) value_to_fill = 1 for axis in axes: diff --git a/python/paddle/incubate/autograd/composite_rules.py b/python/paddle/incubate/autograd/composite_rules.py index caedc31a3c17d..1ca8ab62a8363 100644 --- a/python/paddle/incubate/autograd/composite_rules.py +++ b/python/paddle/incubate/autograd/composite_rules.py @@ -171,11 +171,11 @@ def layernorm_composite(x, scale, bias, epsilon, begin_norm_axis): out = difference * rsqrt_var if scale is not None: - if x.shape[begin_norm_axis:] is not scale.shape: + if x.shape[begin_norm_axis:] != scale.shape: scale = reshape(scale, x.shape[begin_norm_axis:]) out = out * scale if bias is not None: - if x.shape[begin_norm_axis:] is not bias.shape: + if x.shape[begin_norm_axis:] != bias.shape: bias = reshape(bias, x.shape[begin_norm_axis:]) out = out + bias @@ -266,8 +266,9 @@ def mean_composite(x, axis, keepdim): is_amp = True x = cast(x, "float32") - axes = axis or list(range(0, len(x.shape))) - axes = [axes] if isinstance(axes, int) else axes + if axis in (None, []): + axis = tuple(range(0, len(x.shape))) + axes = (axis,) if isinstance(axis, int) else axis sum_x = sum(x, axis=axes, keepdim=keepdim) ele_nums_list = [x.shape[axis] for axis in axes] if ele_nums_list == []: diff --git a/test/prim/new_ir_prim/CMakeLists.txt b/test/prim/new_ir_prim/CMakeLists.txt index 393bc869d9bea..85611d846cbbe 100644 --- a/test/prim/new_ir_prim/CMakeLists.txt +++ b/test/prim/new_ir_prim/CMakeLists.txt @@ -1,10 +1,20 @@ +set(TEST_PRIM_PURE_NEW_IR_CASES test_prim_program) + +foreach(target ${TEST_PRIM_PURE_NEW_IR_CASES}) + py_test_modules(${target} MODULES ${target} ENVS GLOG_v=1 + FLAGS_enable_new_ir_api=true) +endforeach() + file( - GLOB TEST_INTERP_CASES + GLOB TEST_PRIM_TRANS_NEW_IR_CASES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py") -string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") +string(REPLACE ".py" "" TEST_PRIM_TRANS_NEW_IR_CASES + "${TEST_PRIM_TRANS_NEW_IR_CASES}") + +list(REMOVE_ITEM TEST_PRIM_TRANS_NEW_IR_CASES ${TEST_PRIM_PURE_NEW_IR_CASES}) -foreach(target ${TEST_INTERP_CASES}) +foreach(target ${TEST_PRIM_TRANS_NEW_IR_CASES}) py_test_modules(${target} MODULES ${target} ENVS GLOG_v=1 FLAGS_enable_new_ir_in_executor=true) endforeach() diff --git a/test/prim/new_ir_prim/test_decomp_op.py b/test/prim/new_ir_prim/test_decomp_op.py index f90e0fe24391b..413008f814f7f 100644 --- a/test/prim/new_ir_prim/test_decomp_op.py +++ b/test/prim/new_ir_prim/test_decomp_op.py @@ -17,6 +17,7 @@ import paddle from paddle import ir from paddle.decomposition import decompose +from paddle.framework import core paddle.enable_static() @@ -44,7 +45,9 @@ def test_build_op(self): y = newir_program.block().ops[-2].results() orig_shape = y[0].shape paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) + core._set_prim_forward_enabled(True) y_new = decompose(newir_program, y) + core._set_prim_forward_enabled(False) new_shape = y_new[0].shape assert ( orig_shape == new_shape diff --git a/test/prim/new_ir_prim/test_prim_program.py b/test/prim/new_ir_prim/test_prim_program.py new file mode 100644 index 0000000000000..594f65baa9b2e --- /dev/null +++ b/test/prim/new_ir_prim/test_prim_program.py @@ -0,0 +1,94 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle.autograd.backward import grad +from paddle.decomposition import decompose +from paddle.framework import core + +paddle.enable_static() + + +class TestPrimMode(unittest.TestCase): + def setUp(self): + np.random.seed(2023) + self.shape_x = [8, 16, 32, 64] + self.shape_y = [8, 16, 32, 64] + self.x = np.random.random(self.shape_x).astype("float32") + self.y = np.random.random(self.shape_y).astype("float32") + + def base_net(self, flag=None): + if flag == "forward": + core._set_prim_forward_enabled(True) + elif flag == "backward": + core._set_prim_backward_enabled(True) + elif flag == "all": + core._set_prim_all_enabled(True) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.static.data('x', self.shape_x, dtype='float32') + y = paddle.static.data('y', self.shape_y, dtype='float32') + x.stop_gradient = False + y.stop_gradient = False + divide_out = paddle.divide(x, y) + sum_out = paddle.mean(divide_out, axis=0) + [new_out] = decompose(main_program, [sum_out]) + gradients = grad(new_out, (x, y)) + + exe = paddle.static.Executor() + [fwd, dx, dy] = exe.run( + feed={'x': self.x, 'y': self.y}, fetch_list=[new_out, gradients] + ) + + whole_ops = [op.name() for op in main_program.block().ops] + if flag == "forward": + core._set_prim_forward_enabled(False) + assert 'pd.mean' not in whole_ops and 'pd.divide_grad' in whole_ops + elif flag == "backward": + core._set_prim_backward_enabled(False) + assert 'pd.mean' in whole_ops and 'pd.divide_grad' not in whole_ops + elif flag == "all": + core._set_prim_all_enabled(False) + assert ( + 'pd.mean' not in whole_ops and 'pd.divide_grad' not in whole_ops + ) + else: + assert 'pd.mean' in whole_ops and 'pd.divide_grad' in whole_ops + return fwd, dx, dy + + def test_prim_forward(self): + res_ref = self.base_net() + res = self.base_net("forward") + for ref, actual in zip(res_ref, res): + np.testing.assert_equal(ref, actual) + + def test_prim_backward(self): + res_ref = self.base_net() + res = self.base_net("backward") + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose(ref, actual, rtol=1e-6) + + def test_prim_all(self): + res_ref = self.base_net() + res = self.base_net("all") + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose(ref, actual, rtol=1e-6) + + +if __name__ == "__main__": + unittest.main() From ded1044258ac05a76e76a5955febedb2ae2a7168 Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:11:11 +0800 Subject: [PATCH 019/194] [IR] Rigister LegacyKernelOp into KernelDialect (#56680) * Register LegacyKernelDialect & Rigister LegacyKernelOp * fix code style * delete LegacyKernelDialect ,register LegacyKernelOp into PaddleKernelDialect * fix bug * change as reviewed comments * bug fix * bug fix * try to restart coverage CI * pass legacy op to kernel pass * fix code style * fix code style * fix code style --- paddle/cinn/hlir/dialect/.gitignore | 1 + .../new_executor/new_ir_interpreter.cc | 7 ++- .../ir/kernel_dialect.cc | 7 ++- .../paddle_kernel_dialect/ir/kernel_op.h | 2 +- .../ir/legacy_kernel_op.cc | 62 +++++++++++++++++++ .../ir/legacy_kernel_op.h | 38 ++++++++++++ .../ir/transforms/pd_op_to_kernel_pass.cc | 16 ++++- 7 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc create mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h diff --git a/paddle/cinn/hlir/dialect/.gitignore b/paddle/cinn/hlir/dialect/.gitignore index a21ba08d95acf..3e41ce4c67822 100644 --- a/paddle/cinn/hlir/dialect/.gitignore +++ b/paddle/cinn/hlir/dialect/.gitignore @@ -1 +1,2 @@ generated/** +generated/* diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index 28752ef1f7eb1..df9f398770e28 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -42,6 +42,11 @@ #include "paddle/fluid/framework/new_executor/instruction/legacy_kernel_instruction.h" #include "paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h" #include "paddle/ir/core/builtin_attribute.h" @@ -517,7 +522,7 @@ void NewIRInterpreter::BuildInstruction() { } VLOG(6) << "process " << op_name; - if (dialect::IsLegacyOp(op_name)) { + if (op->name().compare(paddle::dialect::LegacyKernelOp::name()) == 0) { vec_instruction_base_.emplace_back( std::make_unique(op_idx++, place_, diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc index bb3d52d766405..5269fe62892a5 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc @@ -16,6 +16,7 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/init_phi.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/ddim.h" @@ -32,9 +33,9 @@ PaddleKernelDialect::PaddleKernelDialect(ir::IrContext *context) void PaddleKernelDialect::initialize() { RegisterTypes(); - RegisterTypes(); - RegisterOps(); - + RegisterTypes(); + RegisterOps(); RegisterAttributes(); } diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h index 5cbd4b0b434b3..91dae1cfd560f 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h @@ -24,7 +24,7 @@ namespace dialect { class PhiKernelOp : public ir::Op { public: using Op::Op; - static const char *name() { return "phi.kernel"; } + static const char *name() { return "pd_kernel.phi_kernel"; } static constexpr uint32_t attributes_num = 3; static const char *attributes_name[attributes_num]; std::string op_name(); diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc new file mode 100644 index 0000000000000..ca15657e10485 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" +#include "paddle/ir/core/builtin_attribute.h" +#include "paddle/phi/core/enforce.h" + +namespace paddle { +namespace dialect { + +const char* LegacyKernelOp::attributes_name[attributes_num] = { // NOLINT + "op_name", + "kernel_name", + "kernel_key"}; + +void LegacyKernelOp::Verify() { + VLOG(4) << "Verifying inputs, outputs and attributes for: LegacyKernelOp."; + + auto& attributes = this->attributes(); + + PADDLE_ENFORCE(attributes.count("op_name") > 0 && + attributes.at("op_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: op_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_name") > 0 && + attributes.at("kernel_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_key") > 0 && + attributes.at("kernel_key").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_key is not right.")); +} + +std::string LegacyKernelOp::op_name() { + return attributes().at("op_name").dyn_cast().AsString(); +} +std::string LegacyKernelOp::kernel_name() { + return attributes().at("kernel_name").dyn_cast().AsString(); +} +phi::KernelKey LegacyKernelOp::kernel_key() { + return attributes().at("kernel_key").dyn_cast().data(); +} + +} // namespace dialect +} // namespace paddle + +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h new file mode 100644 index 0000000000000..524505a48305f --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h @@ -0,0 +1,38 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/ir/core/builder.h" +#include "paddle/ir/core/op_base.h" +#include "paddle/phi/core/kernel_factory.h" + +namespace paddle { +namespace dialect { +class LegacyKernelOp : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd_kernel.legacy_kernel"; } + static constexpr uint32_t attributes_num = 3; + static const char *attributes_name[attributes_num]; + std::string op_name(); + std::string kernel_name(); + phi::KernelKey kernel_key(); + void Verify(); +}; + +} // namespace dialect +} // namespace paddle + +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 4198098f2bd4f..64d37c95c1aee 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -27,6 +27,7 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/api/lib/kernel_dispatch.h" @@ -507,6 +508,10 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, std::string phi_kernel_op_name = paddle::dialect::PhiKernelOp::name(); ir::OpInfo phi_kernel_op_info = ctx->GetRegisteredOpInfo(phi_kernel_op_name); + std::string legacy_kernel_op_name = paddle::dialect::LegacyKernelOp::name(); + ir::OpInfo legacy_kernel_op_info = + ctx->GetRegisteredOpInfo(legacy_kernel_op_name); + auto skip_feed_names = GetSkipFeedNames(block); for (auto op_item : *block) { @@ -1008,7 +1013,6 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, {"op_name", ir::StrAttribute::get(ctx, op_item->name())}, {"kernel_name", ir::StrAttribute::get(ctx, kernel_fn_str)}, {"kernel_key", dialect::KernelAttribute::get(ctx, kernel_key)}}; - auto op_attr_map = op_item->attributes(); for (auto& map_item : op_attr_map) { @@ -1019,8 +1023,14 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, op_attribute.emplace("is_inplace", ir::BoolAttribute::get(ctx, true)); } - ir::Operation* op = ir::Operation::Create( - vec_inputs, op_attribute, op_output_types, phi_kernel_op_info); + ir::Operation* op; + if (dialect::IsLegacyOp(op_item->name())) { + op = ir::Operation::Create( + vec_inputs, op_attribute, op_output_types, legacy_kernel_op_info); + } else { + op = ir::Operation::Create( + vec_inputs, op_attribute, op_output_types, phi_kernel_op_info); + } map_op_pair[op_item] = op; From ade51aa57d3d4357535cdad972307d8854a70d74 Mon Sep 17 00:00:00 2001 From: Ghost Screaming Date: Wed, 30 Aug 2023 14:18:27 +0800 Subject: [PATCH 020/194] [Auto Parallel] Compatible new comm library upgrade (#56604) * for verify fluid operator support new comm library * u * u * u * compatiable new comm library upgrade for c_allgather, c_reduce, c_reduce_scatter and c_scatter. * Remove useless comments in process_group.py * Polish code style. * Fix some problems. * Remove use fluid api in phi comm_context_manager. * Add PPADDLE_WITH_CUDA and PADDLE_WITH_NCCL micro judgement. * Fix bug of HIP architecture. * Fix some problems. 1. remove useless loggings. 2. Fix conditional compilation for HIP. 3. Fix problems of test_pass_generation_pipeline.py. It calls paddle.distributed.init_parallel_env() at first, then auto.Engine calls _init_comm(), which will calls process_group.instantiate(). However, init_parallel_env() will call paddle.distributed.barrier(), it will call CreateNCCLEnvCache and create corresponding NCCLCommContext. But dev_id is not set, as a result, NCCLCommContext's dev_ctx is not initialized. * Fix some problems. * Polish code. * Polish code. * Revert compatiable upgrade for communication operators. Their upgrades will be submitted in another PR. * Remove StaticTCPStore. * Remove useless modification. * Remove useless set_cuda_device_id. * Polish code. * Remove fluid header files in phi files. * Remove useless comments. * Fix problems of hip arch. * Fix some problems. * Polish code. * Polish code style. --------- Co-authored-by: hitywt --- paddle/fluid/platform/init.cc | 40 ++++++++++++ paddle/fluid/pybind/communication.cc | 6 +- paddle/phi/common/memory_utils.cc | 28 ++++++++ paddle/phi/common/memory_utils.h | 64 +++++++++++++++++++ .../core/distributed/comm_context_manager.cc | 52 +++++++++++---- .../core/distributed/comm_context_manager.h | 5 +- .../phi/core/distributed/nccl_comm_context.cc | 24 +++++++ .../phi/core/distributed/nccl_comm_context.h | 36 +++++++++++ .../auto_parallel/static/process_group.py | 21 ++++-- python/paddle/distributed/collective.py | 2 +- python/paddle/distributed/parallel.py | 13 ++-- 11 files changed, 262 insertions(+), 29 deletions(-) diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 2ae413db5e6ca..2cc3dc7124a52 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -57,6 +57,11 @@ limitations under the License. */ #include "paddle/phi/common/memory_utils.h" #include "paddle/phi/core/custom_kernel.h" +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) +#include "paddle/fluid/platform/device/gpu/gpu_resource_pool.h" +#endif + PHI_DECLARE_int32(paddle_num_threads); PADDLE_DEFINE_EXPORTED_int32( multiple_of_cupti_buffer_size, @@ -440,6 +445,41 @@ void InitMemoryMethod() { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) memory_method->gpu_memory_usage = paddle::platform::GpuMemoryUsage; #endif + +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) + // TODO(GhostScreaming): Use phi methods later. + memory_method->get_allocator = + [](int device_id, phi::gpuStream_t stream) -> phi::Allocator * { + return paddle::memory::allocation::AllocatorFacade::Instance() + .GetAllocator(phi::GPUPlace(device_id), stream) + .get(); + }; + memory_method->get_host_allocator = []() -> phi::Allocator * { + return paddle::memory::allocation::AllocatorFacade::Instance() + .GetAllocator(phi::CPUPlace()) + .get(); + }; + memory_method->get_zero_allocator = [](int device_id) -> phi::Allocator * { + return paddle::memory::allocation::AllocatorFacade::Instance() + .GetZeroAllocator(phi::GPUPlace(device_id)) + .get(); + }; + memory_method->get_host_zero_allocator = []() -> phi::Allocator * { + return paddle::memory::allocation::AllocatorFacade::Instance() + .GetZeroAllocator(phi::CPUPlace()) + .get(); + }; + memory_method->get_pinned_allocator = []() -> phi::Allocator * { + return paddle::memory::allocation::AllocatorFacade::Instance() + .GetAllocator(phi::GPUPinnedPlace()) + .get(); + }; + memory_method->get_new_cuda_event = [](int device_id) { + return paddle::platform::CudaEventResourcePool::Instance().New(device_id); + }; +#endif + memory_method->emplace_device_contexts = paddle::platform::EmplaceDeviceContexts; memory_method->init_devices = InitDevices; diff --git a/paddle/fluid/pybind/communication.cc b/paddle/fluid/pybind/communication.cc index bf58f1d6ac0d8..59ad5779111f1 100644 --- a/paddle/fluid/pybind/communication.cc +++ b/paddle/fluid/pybind/communication.cc @@ -42,14 +42,14 @@ void BindCommContextManager(py::module *m) { py::class_>( *m, "CommContextManager") + .def_static("set_device_id", + &phi::distributed::CommContextManager::SetDeviceId, + py::call_guard()) #if defined(PADDLE_WITH_RCCL) || defined(PADDLE_WITH_NCCL) .def_static( "create_nccl_comm_context", &phi::distributed::CommContextManager::CreateNCCLCommContext, py::call_guard()) - .def_static("set_cuda_device_id", - &phi::distributed::CommContextManager::SetCUDADeviceId, - py::call_guard()) #endif #if defined(PADDLE_WITH_GLOO) .def_static( diff --git a/paddle/phi/common/memory_utils.cc b/paddle/phi/common/memory_utils.cc index f9ef606049297..1af8cc442a117 100644 --- a/paddle/phi/common/memory_utils.cc +++ b/paddle/phi/common/memory_utils.cc @@ -90,6 +90,34 @@ void EmplaceDeviceContexts( stream_priority); } +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) +const phi::Allocator* GetAllocator(int device_id, phi::gpuStream_t stream) { + return MemoryUtils::Instance().GetAllocator(device_id, stream); +} + +const phi::Allocator* GetHostAllocator() { + return MemoryUtils::Instance().GetHostAllocator(); +} + +const phi::Allocator* GetZeroAllocator(int device_id) { + return MemoryUtils::Instance().GetZeroAllocator(device_id); +} + +const phi::Allocator* GetHostZeroAllocator() { + return MemoryUtils::Instance().GetHostZeroAllocator(); +} + +const phi::Allocator* GetPinnedAllocator() { + return MemoryUtils::Instance().GetPinnedAllocator(); +} + +std::shared_ptr::type> GetCudaEvent( + int device_id) { + return MemoryUtils::Instance().GetCudaEvent(device_id); +} +#endif + } // namespace memory_utils } // namespace phi diff --git a/paddle/phi/common/memory_utils.h b/paddle/phi/common/memory_utils.h index f6a4afcea2f78..5f4766f8b6b91 100644 --- a/paddle/phi/common/memory_utils.h +++ b/paddle/phi/common/memory_utils.h @@ -24,6 +24,15 @@ #include "paddle/phi/core/macros.h" #include "paddle/phi/core/stream.h" +#ifdef PADDLE_WITH_CUDA +#include +#include +#endif + +#ifdef PADDLE_WITH_HIP +#include +#endif + namespace phi { struct MemoryInterface { @@ -150,6 +159,17 @@ struct MemoryInterface { const std::vector& places, bool disable_setting_default_stream_for_allocator, int stream_priority); + +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) + phi::Allocator* (*get_allocator)(int device_id, phi::gpuStream_t stream); + phi::Allocator* (*get_host_allocator)(); + phi::Allocator* (*get_zero_allocator)(int device_id); + phi::Allocator* (*get_host_zero_allocator)(); + phi::Allocator* (*get_pinned_allocator)(); + std::shared_ptr::type> ( + *get_new_cuda_event)(int device_id); +#endif }; class MemoryUtils { @@ -323,6 +343,34 @@ class MemoryUtils { "Fluid. You can call InitMemoryMethod() for initialization.")); } +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) + const phi::Allocator* GetAllocator(int device_id, phi::gpuStream_t stream) { + return memory_method_->get_allocator(device_id, stream); + } + + const phi::Allocator* GetHostAllocator() { + return memory_method_->get_host_allocator(); + } + + const phi::Allocator* GetZeroAllocator(int device_id) { + return memory_method_->get_zero_allocator(device_id); + } + + const phi::Allocator* GetHostZeroAllocator() { + return memory_method_->get_host_zero_allocator(); + } + + const phi::Allocator* GetPinnedAllocator() { + return memory_method_->get_pinned_allocator(); + } + + std::shared_ptr::type> GetCudaEvent( + int device_id) { + return memory_method_->get_new_cuda_event(device_id); + } +#endif + private: MemoryUtils() = default; @@ -385,6 +433,22 @@ void EmplaceDeviceContexts( bool disable_setting_default_stream_for_allocator, int stream_priority); +#if (defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)) && \ + (defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)) +const Allocator* GetAllocator(int device_id, phi::gpuStream_t stream); + +const Allocator* GetHostAllocator(); + +const Allocator* GetZeroAllocator(int device_id); + +const Allocator* GetHostZeroAllocator(); + +const Allocator* GetPinnedAllocator(); + +std::shared_ptr::type> GetCudaEvent( + int device_id); +#endif + class Buffer { public: explicit Buffer(const phi::Place& place) : place_(place) {} diff --git a/paddle/phi/core/distributed/comm_context_manager.cc b/paddle/phi/core/distributed/comm_context_manager.cc index 7bbf0612ab323..385bbb137cfea 100644 --- a/paddle/phi/core/distributed/comm_context_manager.cc +++ b/paddle/phi/core/distributed/comm_context_manager.cc @@ -12,44 +12,53 @@ // See the License for the specific language governing permissions and // limitations under the License. -#if defined(PADDLE_WITH_GLOO) -#include - -#include "paddle/phi/core/distributed/gloo_comm_context.h" -#include "paddle/phi/core/distributed/gloo_utils.h" -#include "paddle/phi/core/distributed/store/gloo_store.h" -#endif - #include "paddle/phi/core/distributed/comm_context_manager.h" #include #include +#include "glog/logging.h" #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/core/distributed/store/store.h" #include "paddle/phi/core/enforce.h" +#if defined(PADDLE_WITH_GLOO) +#include +#include "paddle/phi/core/distributed/gloo_comm_context.h" +#include "paddle/phi/core/distributed/gloo_utils.h" +#include "paddle/phi/core/distributed/store/gloo_store.h" +#endif + #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) +#include "paddle/phi/common/memory_utils.h" #include "paddle/phi/core/distributed/nccl_comm_context.h" #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE -#include "glog/logging.h" #include "paddle/phi/core/distributed/xccl_comm_context.h" #endif namespace phi { namespace distributed { +int CommContextManager::device_id = -1; + +void CommContextManager::SetDeviceId(int dev_id) { #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) -void CommContextManager::SetCUDADeviceId(int dev_id) { phi::backends::gpu::SetDeviceId(dev_id); + CommContextManager::device_id = dev_id; +#endif } +#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) void CommContextManager::CreateNCCLCommContext( const std::shared_ptr& store, const std::string& unique_comm_key, int rank, int size) { + auto& comm_context_manager = CommContextManager::GetInstance(); + if (comm_context_manager.Has(unique_comm_key)) { + return; + } ncclUniqueId nccl_id; if (rank == 0) { PADDLE_ENFORCE_GPU_SUCCESS(phi::dynload::ncclGetUniqueId(&nccl_id)); @@ -68,7 +77,28 @@ void CommContextManager::CreateNCCLCommContext( auto nccl_comm_context = std::make_unique(rank, size, nccl_id); - auto& comm_context_manager = CommContextManager::GetInstance(); + + if (CommContextManager::device_id != -1) { + std::unique_ptr dev_ctx( + new phi::GPUContext(phi::GPUPlace(CommContextManager::device_id))); + dev_ctx->SetAllocator(phi::memory_utils::GetAllocator( + CommContextManager::device_id, dev_ctx->stream())); + dev_ctx->SetHostAllocator(phi::memory_utils::GetHostAllocator()); + dev_ctx->SetZeroAllocator( + phi::memory_utils::GetZeroAllocator(CommContextManager::device_id)); + dev_ctx->SetHostZeroAllocator(phi::memory_utils::GetHostZeroAllocator()); + dev_ctx->SetPinnedAllocator(phi::memory_utils::GetPinnedAllocator()); + dev_ctx->PartialInitWithAllocator(); + auto compute_event = + phi::memory_utils::GetCudaEvent(CommContextManager::device_id); + auto comm_event = + phi::memory_utils::GetCudaEvent(CommContextManager::device_id); + + nccl_comm_context->SetDevContext(std::move(dev_ctx)); + nccl_comm_context->SetComputeEvent(std::move(compute_event)); + nccl_comm_context->SetCommEvent(std::move(comm_event)); + } + comm_context_manager.SetStore(store); comm_context_manager.Emplace(unique_comm_key, std::move(nccl_comm_context)); } diff --git a/paddle/phi/core/distributed/comm_context_manager.h b/paddle/phi/core/distributed/comm_context_manager.h index 6d82e89f92ba0..55fa831c27014 100644 --- a/paddle/phi/core/distributed/comm_context_manager.h +++ b/paddle/phi/core/distributed/comm_context_manager.h @@ -46,13 +46,13 @@ class CommContextManager { bool Has(const std::string& unique_comm_key) const; + static void SetDeviceId(int dev_id); + #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) static void CreateNCCLCommContext(const std::shared_ptr& store, const std::string& unique_comm_key, int rank, int size); - - static void SetCUDADeviceId(int dev_id); #endif #if defined(PADDLE_WITH_GLOO) @@ -76,6 +76,7 @@ class CommContextManager { std::unordered_map> id_to_comm_context_; std::shared_ptr store_; + static int device_id; }; } // namespace distributed diff --git a/paddle/phi/core/distributed/nccl_comm_context.cc b/paddle/phi/core/distributed/nccl_comm_context.cc index 2ad3ece71a5d6..90b6a4c447c92 100644 --- a/paddle/phi/core/distributed/nccl_comm_context.cc +++ b/paddle/phi/core/distributed/nccl_comm_context.cc @@ -37,6 +37,30 @@ NCCLCommContext::NCCLCommContext(int rank, int size, ncclUniqueId nccl_id) ncclComm_t NCCLCommContext::GetNcclComm() { return nccl_comm_; } +gpuStream_t NCCLCommContext::GetStream() { return dev_ctx_->stream(); } + +phi::GPUContext* NCCLCommContext::GetDevContext() { return dev_ctx_.get(); } + +void NCCLCommContext::SetDevContext( + std::unique_ptr&& dev_ctx) { + dev_ctx_ = std::move(dev_ctx); +} + +gpuEvent_t NCCLCommContext::GetComputeEvent() { return compute_event_.get(); } + +void NCCLCommContext::SetComputeEvent( + std::shared_ptr::type>&& + compute_event) { + compute_event_ = std::move(compute_event); +} + +gpuEvent_t NCCLCommContext::GetCommEvent() { return comm_event_.get(); } + +void NCCLCommContext::SetCommEvent( + std::shared_ptr::type>&& comm_event) { + comm_event_ = std::move(comm_event); +} + void NCCLCommContext::Broadcast(phi::DenseTensor* out_tensor, const phi::DenseTensor& in_tensor, int root, diff --git a/paddle/phi/core/distributed/nccl_comm_context.h b/paddle/phi/core/distributed/nccl_comm_context.h index b8f14cef131e4..fdd45793a6387 100644 --- a/paddle/phi/core/distributed/nccl_comm_context.h +++ b/paddle/phi/core/distributed/nccl_comm_context.h @@ -13,6 +13,16 @@ // limitations under the License. #pragma once +#ifdef PADDLE_WITH_CUDA +#include +#include +#endif + +#ifdef PADDLE_WITH_HIP +#include +#endif + +#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_decls.h" #include "paddle/phi/core/distributed/comm_context.h" #include "paddle/phi/core/macros.h" @@ -30,9 +40,27 @@ namespace distributed { class NCCLCommContext final : public CommContext { public: NCCLCommContext(int rank, int size, ncclUniqueId nccl_id); + ~NCCLCommContext() {} ncclComm_t GetNcclComm(); + gpuStream_t GetStream(); + + gpuEvent_t GetComputeEvent(); + + void SetComputeEvent( + std::shared_ptr::type>&& + compute_event); + + gpuEvent_t GetCommEvent(); + + void SetCommEvent( + std::shared_ptr::type>&& comm_event); + + phi::GPUContext* GetDevContext(); + + void SetDevContext(std::unique_ptr&& dev_ctx); + void Broadcast(phi::DenseTensor* out_tensor, const phi::DenseTensor& in_tensor, int root, @@ -75,6 +103,14 @@ class NCCLCommContext final : public CommContext { DISABLE_COPY_AND_ASSIGN(NCCLCommContext); ncclComm_t nccl_comm_; + + std::unique_ptr dev_ctx_; + + // used for comm wait compute, compute_stream-->event-->comm_stream + std::shared_ptr::type> compute_event_; + + // used for compute wait comm, comm_stream-->event-->compute_stream + std::shared_ptr::type> comm_event_; }; } // namespace distributed diff --git a/python/paddle/distributed/auto_parallel/static/process_group.py b/python/paddle/distributed/auto_parallel/static/process_group.py index 285184db14cd9..9bbe1fbcf48e1 100644 --- a/python/paddle/distributed/auto_parallel/static/process_group.py +++ b/python/paddle/distributed/auto_parallel/static/process_group.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License +import os from collections import OrderedDict import paddle @@ -146,9 +147,6 @@ def instantiate(self): global_rank = genv.rank if self.nranks >= 2 and global_rank in self.ranks: - logger.info( - f"group_id: {self.id}, ranks: {self.ranks}, nranks: {self.nranks}, trainer_endpoints: {genv.current_endpoint}" - ) strategy = core.ParallelStrategy() strategy.nranks = self.nranks strategy.local_rank = self.local_rank(global_rank) @@ -159,9 +157,22 @@ def instantiate(self): strategy.nrings = 1 if core.is_compiled_with_cuda(): place = core.CUDAPlace(genv.device_id) - core.NCCLParallelContext(strategy, place).init_with_ring_id( - ring_id + use_new_comm = os.getenv( + "FLAGS_dynamic_static_unified_comm", "0" ) + if use_new_comm in ["1", "True", "true"]: + store = core.create_or_get_global_tcp_store() + core.CommContextManager.set_device_id(genv.device_id) + core.CommContextManager.create_nccl_comm_context( + store, + str(ring_id), + strategy.local_rank, + strategy.nranks, + ) + else: + core.NCCLParallelContext(strategy, place).init_with_ring_id( + ring_id + ) elif core.is_compiled_with_xpu(): place = core.XPUPlace(genv.device_id) core.BKCLParallelContext(strategy, place).init_with_ring_id( diff --git a/python/paddle/distributed/collective.py b/python/paddle/distributed/collective.py index b1d61196e056d..144be980a7599 100644 --- a/python/paddle/distributed/collective.py +++ b/python/paddle/distributed/collective.py @@ -330,7 +330,7 @@ def _init_parallel_env(backend): store, "0", rank, world_size ) elif backend == "nccl": - core.CommContextManager.set_cuda_device_id(dev_id) + core.CommContextManager.set_device_id(dev_id) core.CommContextManager.create_nccl_comm_context( store, "0", rank, world_size ) diff --git a/python/paddle/distributed/parallel.py b/python/paddle/distributed/parallel.py index 217920debe4de..67452c1a4e1b2 100644 --- a/python/paddle/distributed/parallel.py +++ b/python/paddle/distributed/parallel.py @@ -1084,13 +1084,7 @@ def train(): master_port = int(master_port) is_master = rank == 0 stop_check_timeout = int(os.getenv("FLAGS_stop_check_timeout", "900")) - default_store = core.TCPStore( - master_addr, - master_port, - is_master, - world_size, - timeout=stop_check_timeout, - ) + default_store = core.create_or_get_global_tcp_store() _set_default_store(default_store) pg = _new_process_group_impl( backend, @@ -1108,6 +1102,11 @@ def train(): _add_new_group(group) parallel_helper._set_parallel_ctx(True) + # barrier will call CreateNCCLEnvCache which will call CreateNCCLCommContext. + # Set device_id to prevent creating null dev_ctx. + # TODO(mine): support XPU and other backends. + if backend in ["nccl", 'xccl', 'bkcl']: + core.CommContextManager.set_device_id(parallel_env.device_id) paddle.distributed.barrier(group=group) return group From 1692af99eade96eba49830b4712feee695557d74 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 30 Aug 2023 14:22:10 +0800 Subject: [PATCH 021/194] [NewIR] add_arg_mapping_for_fetch (#56752) * add_arg_mapping_for_fetch * fix * fix --- paddle/fluid/ir_adaptor/translator/op_compat_gen.py | 1 + paddle/fluid/ir_adaptor/translator/op_translator.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/paddle/fluid/ir_adaptor/translator/op_compat_gen.py b/paddle/fluid/ir_adaptor/translator/op_compat_gen.py index 7c1a78305e71c..60a4a09204066 100644 --- a/paddle/fluid/ir_adaptor/translator/op_compat_gen.py +++ b/paddle/fluid/ir_adaptor/translator/op_compat_gen.py @@ -128,6 +128,7 @@ def insert_new_mutable_attributes( # special mapping list op_arg_name_mappings["set_value_grad"]["values_grad"] = "ValueTensor@GRAD" + op_arg_name_mappings["fetch"] = {"x": "X"} op_name_normailzer_template = env.get_template("op_compat_info.cc.j2") with open(output_source_file, 'wt') as f: diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index cad900ee7f6da..38d833fc312de 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -1699,6 +1699,7 @@ OpTranslator::OpTranslator() { special_handlers["cast"] = CastOpTranscriber(); special_handlers["feed"] = FeedOpTranscriber(); special_handlers["data"] = DataOpTranscriber(); + special_handlers["fetch"] = FetchOpTranscriber(); special_handlers["fetch_v2"] = FetchOpTranscriber(); special_handlers["fill_constant"] = FillConstantTranscriber(); special_handlers["grad_add"] = GradAddOpTranscriber(); From 59b2ad39e1d335c6394dad5d4a132f84852e2203 Mon Sep 17 00:00:00 2001 From: WangZhen <23097963+0x45f@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:24:05 +0800 Subject: [PATCH 022/194] [NewIR]Gen ops_api.cc for static mode (#56653) --- .gitignore | 1 + .../ir/dialect/op_generator/ops_api_gen.py | 115 ++++++++++++++++++ .../dialect/paddle_dialect/ir/CMakeLists.txt | 26 ++++ paddle/fluid/pybind/CMakeLists.txt | 1 + paddle/fluid/pybind/eager_utils.cc | 2 +- .../fluid/pybind/generate_file_structures.py | 1 + paddle/fluid/pybind/ops_api.cc | 104 ---------------- 7 files changed, 145 insertions(+), 105 deletions(-) create mode 100644 paddle/fluid/ir/dialect/op_generator/ops_api_gen.py delete mode 100644 paddle/fluid/pybind/ops_api.cc diff --git a/.gitignore b/.gitignore index 98a3d18b8a2a6..cc68215a8d2dc 100644 --- a/.gitignore +++ b/.gitignore @@ -98,3 +98,4 @@ paddle/phi/kernels/fusion/cutlass/conv2d/generated/* python/paddle/fluid/incubate/fleet/parameter_server/pslib/ps_pb2.py paddle/fluid/ir_adaptor/translator/op_compat_info.cc paddle/fluid/pybind/static_op_function.* +paddle/fluid/pybind/ops_api.cc diff --git a/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py b/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py new file mode 100644 index 0000000000000..135d75ecf9fd8 --- /dev/null +++ b/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py @@ -0,0 +1,115 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import argparse +import os + +from api_gen import NAMESPACE_TEMPLATE, PD_MANUAL_OP_LIST, CodeGen + +CPP_FILE_TEMPLATE = """ +#include + +#include "paddle/fluid/pybind/static_op_function.h" +#include "paddle/phi/core/enforce.h" + +{body} + +""" + +NAMESPACE_INNER_TEMPLATE = """ +{function_impl} + +static PyMethodDef OpsAPI[] = {{ +{ops_api} +{{nullptr, nullptr, 0, nullptr}} +}}; + +void BindOpsAPI(pybind11::module *module) {{ + if (PyModule_AddFunctions(module->ptr(), OpsAPI) < 0) {{ + PADDLE_THROW(phi::errors::Fatal("Add C++ api to core.ops failed!")); + }} +}} +""" + +FUNCTION_IMPL_TEMPLATE = """ +static PyObject *{name}(PyObject *self, PyObject *args, PyObject *kwargs) {{ + return static_api_{name}(self, args, kwargs); +}}""" + +OPS_API_TEMPLATE = """ +{{"{name}", (PyCFunction)(void (*)(void)){name}, METH_VARARGS | METH_KEYWORDS, "C++ interface function for {name}."}},""" + + +class OpsAPIGen(CodeGen): + def __init__(self) -> None: + super().__init__() + + def _gen_one_function_impl(self, name): + return FUNCTION_IMPL_TEMPLATE.format(name=name) + + def _gen_one_ops_api(self, name): + return OPS_API_TEMPLATE.format(name=name) + + def gen_cpp_file( + self, op_yaml_files, op_compat_yaml_file, namespaces, cpp_file_path + ): + if os.path.exists(cpp_file_path): + os.remove(cpp_file_path) + op_info_items = self._parse_yaml(op_yaml_files, op_compat_yaml_file) + function_impl_str = '' + ops_api_str = '' + for op_info in op_info_items: + for op_name in op_info.op_phi_name: + if ( + op_info.infer_meta_func is None + and op_name not in PD_MANUAL_OP_LIST + ): + continue + function_impl_str += self._gen_one_function_impl(op_name) + ops_api_str += self._gen_one_ops_api(op_name) + + inner_body = NAMESPACE_INNER_TEMPLATE.format( + function_impl=function_impl_str, ops_api=ops_api_str + ) + + body = inner_body + for namespace in reversed(namespaces): + body = NAMESPACE_TEMPLATE.format(namespace=namespace, body=body) + with open(cpp_file_path, 'w') as f: + f.write(CPP_FILE_TEMPLATE.format(body=body)) + + +def ParseArguments(): + parser = argparse.ArgumentParser( + description='Generate Dialect Python C Files By Yaml' + ) + parser.add_argument('--op_yaml_files', type=str) + parser.add_argument('--op_compat_yaml_file', type=str) + parser.add_argument('--namespaces', type=str) + parser.add_argument('--ops_api_file', type=str) + return parser.parse_args() + + +if __name__ == '__main__': + args = ParseArguments() + op_yaml_files = args.op_yaml_files.split(",") + op_compat_yaml_file = args.op_compat_yaml_file + if args.namespaces is not None: + namespaces = args.namespaces.split(",") + ops_api_file = args.ops_api_file + + code_gen = OpsAPIGen() + code_gen.gen_cpp_file( + op_yaml_files, op_compat_yaml_file, namespaces, ops_api_file + ) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index 86ade99a3cc22..d7269369ae1a3 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -113,6 +113,32 @@ add_custom_command( add_custom_target(static_op_function_gen ALL DEPENDS ${python_c_header_file} ${python_c_source_file}) +set(ops_api_gen_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py) +set(ops_api_source_file ${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/ops_api.cc) +set(ops_api_source_file_tmp ${ops_api_source_file}.tmp) + +add_custom_command( + OUTPUT ${ops_api_source_file} + COMMAND + ${PYTHON_EXECUTABLE} ${ops_api_gen_file} --op_yaml_files ${op_yaml_files} + --op_compat_yaml_file ${op_compat_yaml_file} --namespaces "paddle,pybind" + --ops_api_file ${ops_api_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ops_api_source_file_tmp} + ${ops_api_source_file} + COMMENT "copy_if_different ${ops_api_source_file}" + DEPENDS ${ops_api_gen_file} + ${op_forward_yaml_file1} + ${op_forward_yaml_file2} + ${op_backward_yaml_file1} + ${op_backward_yaml_file2} + ${op_compat_yaml_file} + ${python_c_header_file} + ${python_c_source_file} + VERBATIM) + +add_custom_target(ops_api_gen ALL DEPENDS ${ops_api_source_file}) + cc_library( pd_dialect_core SRCS pd_attribute.cc pd_type.cc diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 64388a4c88eb9..42cdd9be3067c 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -536,6 +536,7 @@ if(WITH_PYTHON) # PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) # endif() + add_dependencies(${SHARD_LIB_NAME} ops_api_gen) if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) add_dependencies(${SHARD_LIB_NAME} legacy_eager_codegen) add_dependencies(${SHARD_LIB_NAME} eager_legacy_op_function_generator_cmd) diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 237e4fcfee0c5..d953e953424b7 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -664,7 +664,7 @@ paddle::DataType CastPyArg2DataTypeDirectly(PyObject* obj, } else { PADDLE_THROW(platform::errors::InvalidArgument( "%s: argument (position %d) must be " - "one of core.VarDesc.VarType, " + "one of paddle::DataType, " "but got %s", op_type, arg_pos + 1, diff --git a/paddle/fluid/pybind/generate_file_structures.py b/paddle/fluid/pybind/generate_file_structures.py index 966c512a31eb2..97d8ffc7fc149 100644 --- a/paddle/fluid/pybind/generate_file_structures.py +++ b/paddle/fluid/pybind/generate_file_structures.py @@ -23,6 +23,7 @@ empty_files = [os.path.join(pybind_dir, "eager_legacy_op_function.cc")] empty_files.append(os.path.join(pybind_dir, "eager_op_function.cc")) empty_files.append(os.path.join(pybind_dir, "static_op_function.cc")) + empty_files.append(os.path.join(pybind_dir, "ops_api.cc")) for path in empty_files: if not os.path.exists(path): diff --git a/paddle/fluid/pybind/ops_api.cc b/paddle/fluid/pybind/ops_api.cc deleted file mode 100644 index 9efe49a97c8c9..0000000000000 --- a/paddle/fluid/pybind/ops_api.cc +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include - -#include "paddle/fluid/pybind/static_op_function.h" -#include "paddle/phi/core/enforce.h" - -namespace paddle { -namespace pybind { - -static PyObject *add_n(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_add_n(self, args, kwargs); -} - -static PyObject *mean(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_mean(self, args, kwargs); -} - -static PyObject *sum(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_sum(self, args, kwargs); -} - -static PyObject *full(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_full(self, args, kwargs); -} - -static PyObject *divide(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_divide(self, args, kwargs); -} - -static PyObject *data(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_data(self, args, kwargs); -} - -static PyObject *fetch(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_fetch(self, args, kwargs); -} - -static PyObject *concat(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_concat(self, args, kwargs); -} - -static PyObject *split(PyObject *self, PyObject *args, PyObject *kwargs) { - return static_api_split(self, args, kwargs); -} - -static PyMethodDef OpsAPI[] = {{"add_n", - (PyCFunction)(void (*)(void))add_n, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for add_n."}, - {"mean", - (PyCFunction)(void (*)(void))mean, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for mean."}, - {"sum", - (PyCFunction)(void (*)(void))sum, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for sum."}, - {"divide", - (PyCFunction)(void (*)(void))divide, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for divide."}, - {"concat", - (PyCFunction)(void (*)(void))concat, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for concat."}, - {"full", - (PyCFunction)(void (*)(void))full, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for full."}, - {"split", - (PyCFunction)(void (*)(void))split, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for split."}, - {"data", - (PyCFunction)(void (*)(void))data, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for data."}, - {"fetch", - (PyCFunction)(void (*)(void))fetch, - METH_VARARGS | METH_KEYWORDS, - "C++ interface function for fetch."}, - {nullptr, nullptr, 0, nullptr}}; - -void BindOpsAPI(pybind11::module *module) { - if (PyModule_AddFunctions(module->ptr(), OpsAPI) < 0) { - PADDLE_THROW(phi::errors::Fatal("Add C++ api to core.ops failed!")); - } -} - -} // namespace pybind -} // namespace paddle From 8c1548801e198832ced1c8a218138ca96308abc1 Mon Sep 17 00:00:00 2001 From: ronnywang Date: Wed, 30 Aug 2023 14:40:40 +0800 Subject: [PATCH 023/194] [ROCM] Remove the constraint with a maximum number of threads per block of 256, P4 (#56702) --- paddle/phi/kernels/funcs/layer_norm_impl.cu.h | 7 +------ paddle/phi/kernels/gpu/send_ue_recv_grad_kernel.cu | 4 ---- paddle/phi/kernels/gpu/send_ue_recv_kernel.cu | 4 ---- .../phi/kernels/gpu/sigmoid_cross_entropy_with_logits.h | 4 ---- paddle/phi/kernels/gpu/unpool_grad_kernel.cu | 8 -------- paddle/phi/kernels/gpu/unpool_kernel.cu | 8 -------- paddle/phi/kernels/gpudnn/softmax_gpudnn.h | 4 ---- 7 files changed, 1 insertion(+), 38 deletions(-) diff --git a/paddle/phi/kernels/funcs/layer_norm_impl.cu.h b/paddle/phi/kernels/funcs/layer_norm_impl.cu.h index 778f13634300b..1a52e57e45f23 100644 --- a/paddle/phi/kernels/funcs/layer_norm_impl.cu.h +++ b/paddle/phi/kernels/funcs/layer_norm_impl.cu.h @@ -42,11 +42,10 @@ template using LayerNormParamType = typename CudnnDataType::BatchNormParamType; inline static int GetDesiredBlockDim(int64_t block_dim) { + const int kMaxBlockDim = 512; #ifdef __HIPCC__ - const int kMaxBlockDim = 256; const int lwarpSize = 64; #else - const int kMaxBlockDim = 512; const int lwarpSize = 32; #endif return block_dim >= kMaxBlockDim ? kMaxBlockDim : lwarpSize; @@ -1875,11 +1874,7 @@ static void LayerNormBackward( int64_t feature_size, const phi::GPUContext &dev_ctx) { auto stream = dev_ctx.stream(); -#ifdef __HIPCC__ - const int kMaxBlockDim = 256; -#else const int kMaxBlockDim = 512; -#endif const int kMaxBlockNum = 128; int gradient_flag = ((d_x != nullptr ? 1 : 0) << 2) | ((d_scale != nullptr ? 1 : 0) << 1) | diff --git a/paddle/phi/kernels/gpu/send_ue_recv_grad_kernel.cu b/paddle/phi/kernels/gpu/send_ue_recv_grad_kernel.cu index d368c43a29753..e455714c50829 100644 --- a/paddle/phi/kernels/gpu/send_ue_recv_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/send_ue_recv_grad_kernel.cu @@ -113,11 +113,7 @@ void CalculateXGrad(const Context& ctx, const DenseTensor& out_grad_tensor, const DenseTensor* dst_count = nullptr, const DenseTensor* out = nullptr) { -#ifdef PADDLE_WITH_HIP - int block = 256; -#else int block = 1024; -#endif int64_t n = slice_size * index_size; int max_grid_dimx = ctx.GetCUDAMaxGridDimSize()[0]; int64_t grid_tmp = (n + block - 1) / block; diff --git a/paddle/phi/kernels/gpu/send_ue_recv_kernel.cu b/paddle/phi/kernels/gpu/send_ue_recv_kernel.cu index 764490bd1cb8b..7274b391e8d13 100644 --- a/paddle/phi/kernels/gpu/send_ue_recv_kernel.cu +++ b/paddle/phi/kernels/gpu/send_ue_recv_kernel.cu @@ -101,11 +101,7 @@ void GraphSendUERecvOpCUDAKernelLaunchHelper(const Context& ctx, const dim3 grid(nbx, nby); const dim3 block(ntx, nty); int64_t input_size = x.dims()[0]; -#ifdef PADDLE_WITH_HIP - int block_ = 256; -#else int block_ = 1024; -#endif if (reduce_op == "SUM" || reduce_op == "MEAN") { GraphSendUERecvSumCUDAFunctor sum_functor; if (message_op == "ADD") { diff --git a/paddle/phi/kernels/gpu/sigmoid_cross_entropy_with_logits.h b/paddle/phi/kernels/gpu/sigmoid_cross_entropy_with_logits.h index 3962d86c3e7b9..307b51a1ca119 100644 --- a/paddle/phi/kernels/gpu/sigmoid_cross_entropy_with_logits.h +++ b/paddle/phi/kernels/gpu/sigmoid_cross_entropy_with_logits.h @@ -35,11 +35,7 @@ namespace cub = hipcub; namespace phi { -#ifdef __HIPCC__ -static constexpr int kNumCUDAThreads = 256; -#else static constexpr int kNumCUDAThreads = 512; -#endif static constexpr int kNumMaxinumNumBlocks = 4096; static inline int NumBlocks(const int N) { diff --git a/paddle/phi/kernels/gpu/unpool_grad_kernel.cu b/paddle/phi/kernels/gpu/unpool_grad_kernel.cu index 959544cdbb969..7cf08d92401cb 100644 --- a/paddle/phi/kernels/gpu/unpool_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/unpool_grad_kernel.cu @@ -88,11 +88,7 @@ class Unpool2dMaxGradFunctor { const T* output_data = output.data(); const T* output_grad_data = output_grad.data(); T* input_grad_data = dev_ctx.template Alloc(input_grad); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool2dMaxGrad <<>>(input.numel(), @@ -131,11 +127,7 @@ class Unpool3dMaxGradFunctor { const T* output_data = output.data(); const T* output_grad_data = output_grad.data(); T* input_grad_data = dev_ctx.template Alloc(input_grad); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool3dMaxGrad <<>>(input.numel(), diff --git a/paddle/phi/kernels/gpu/unpool_kernel.cu b/paddle/phi/kernels/gpu/unpool_kernel.cu index 9365c286195be..1e09323642b67 100644 --- a/paddle/phi/kernels/gpu/unpool_kernel.cu +++ b/paddle/phi/kernels/gpu/unpool_kernel.cu @@ -80,11 +80,7 @@ class Unpool2dMaxFunctor { const T* input_data = input.data(); const int* indices_data = indices.data(); T* output_data = dev_ctx.template Alloc(output); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool2dMax <<>>(input.numel(), @@ -117,11 +113,7 @@ class Unpool3dMaxFunctor { const T* input_data = input.data(); const int* indices_data = indices.data(); T* output_data = dev_ctx.template Alloc(output); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool3dMax <<>>(input.numel(), diff --git a/paddle/phi/kernels/gpudnn/softmax_gpudnn.h b/paddle/phi/kernels/gpudnn/softmax_gpudnn.h index fb434b5c9cfd7..a4571b83e39e7 100644 --- a/paddle/phi/kernels/gpudnn/softmax_gpudnn.h +++ b/paddle/phi/kernels/gpudnn/softmax_gpudnn.h @@ -870,11 +870,7 @@ static void GetGridDim( } static void GetBlockDim(int mid_dim, int low_dim, dim3* block) { -#ifdef __HIPCC__ - constexpr int max_num_threads = 256; -#else constexpr int max_num_threads = 1024; -#endif int block_x = 1 << Log2Ceil(low_dim); int block_y = 1 << Log2Ceil(mid_dim); block->x = std::min(block_x, 32); From e285234c6df6183722417492be9601be2aae93da Mon Sep 17 00:00:00 2001 From: xuxinyi389 <104957571+xuxinyi389@users.noreply.github.com> Date: Wed, 30 Aug 2023 15:04:54 +0800 Subject: [PATCH 024/194] Fix bugs of third party (#56670) * fix bugs of tp * fix bugs of tp * fix bugs * fix bugs * fix bugs of md5 --- paddle/scripts/paddle_build.bat | 54 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/paddle/scripts/paddle_build.bat b/paddle/scripts/paddle_build.bat index a6884b0e1e7b7..5098e025fea5e 100644 --- a/paddle/scripts/paddle_build.bat +++ b/paddle/scripts/paddle_build.bat @@ -402,8 +402,8 @@ if %day_now% NEQ %day_before% ( ) echo set -ex > cache.sh -echo md5_content=$(cat %work_dir:\=/%/cmake/external/*.cmake ^|md5sum ^| awk '{print $1}') >> cache.sh -echo echo ${md5_content}^>md5.txt >> cache.sh +echo md5_content=$(cat %work_dir:\=/%/cmake/external/*.cmake^|md5sum^|awk '{print $1}')$(git submodule status^|md5sum^|awk '{print $1}')>>cache.sh +echo echo ${md5_content}^>md5.txt>>cache.sh %cache_dir%\tools\busybox64.exe cat cache.sh %cache_dir%\tools\busybox64.exe bash cache.sh @@ -415,6 +415,51 @@ if "%WITH_GPU%"=="ON" ( ) else ( set sub_dir=cpu ) + +@ECHO ON +cd /d %work_dir% +python -c "import wget;wget.download('https://paddle-windows.bj.bcebos.com/third_party_code/%sub_dir%/%md5%.tar.gz')" 2>nul +if !ERRORLEVEL! EQU 0 ( + echo Getting source code of third party : extracting ... + tar -xf %md5%.tar.gz + del %md5%.tar.gz + if !errorlevel! EQU 0 ( + echo Getting source code of third party : successful + ) +) else ( + git submodule update --init --recursive + set BCE_FILE=%cache_dir%\bce-python-sdk-new\BosClient.py + echo Uploading source code of third_party: checking bce ... + if not exist %cache_dir%\bce-python-sdk-new ( + echo There is no bce in this PC, will install bce. + cd /d %cache_dir% + echo Download package from https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz + python -c "import wget;wget.download('https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz')" + python -c "import shutil;shutil.unpack_archive('bos_new.tar.gz', extract_dir='./bce-python-sdk-new',format='gztar')" + ) + python -m pip install pycryptodome + python -m pip install bce-python-sdk==0.8.74 + if !errorlevel! EQU 0 ( + cd /d %work_dir% + echo Uploading source code of third party: compressing ... + tar -zcf %md5%.tar.gz ./third_party ./.git/modules + if !errorlevel! EQU 0 ( + echo Uploading source code of third party: uploading ... + python !BCE_FILE! %md5%.tar.gz paddle-windows/third_party_code/%sub_dir% 1>nul + if !errorlevel! EQU 0 ( + echo Upload source code of third party %md5% to bos paddle-windows/third_party_code/%sub_dir% successfully. + ) else ( + echo Failed upload source code of third party to bos, reason: upload failed. + ) + ) else ( + echo Failed upload source code of third party to bos, reason: compress failed. + ) + del %md5%.tar.gz + ) else ( + echo Failed upload source code of third party to bos, reason: install bce failed. + ) +) + set THIRD_PARTY_HOME=%cache_dir:\=/%/third_party/%sub_dir% set THIRD_PARTY_PATH=%THIRD_PARTY_HOME%/%md5% @@ -450,7 +495,9 @@ if not exist %THIRD_PARTY_PATH% ( echo Found reusable third_party cache in %THIRD_PARTY_PATH%, will reuse it. ) + :cmake_impl +cd /d %work_dir%\%BUILD_DIR% echo cmake .. -G %GENERATOR% -DCMAKE_BUILD_TYPE=Release -DWITH_AVX=%WITH_AVX% -DWITH_GPU=%WITH_GPU% -DWITH_MKL=%WITH_MKL% ^ -DWITH_TESTING=%WITH_TESTING% -DWITH_PYTHON=%WITH_PYTHON% -DPYTHON_EXECUTABLE=%PYTHON_EXECUTABLE% -DON_INFER=%ON_INFER% ^ -DWITH_INFERENCE_API_TEST=%WITH_INFERENCE_API_TEST% -DTHIRD_PARTY_PATH=%THIRD_PARTY_PATH% ^ @@ -582,8 +629,9 @@ if "%UPLOAD_TP_FILE%"=="ON" ( echo Download package from https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz python -c "import wget;wget.download('https://xly-devops.bj.bcebos.com/home/bos_new.tar.gz')" python -c "import shutil;shutil.unpack_archive('bos_new.tar.gz', extract_dir='./bce-python-sdk-new',format='gztar')" - python -m pip install bce-python-sdk==0.8.74 ) + python -m pip install pycryptodome + python -m pip install bce-python-sdk==0.8.74 if !errorlevel! EQU 0 ( cd /d %THIRD_PARTY_HOME% echo Uploading third_party: compressing ... From ac80251a198e5d24198ef18ddc360761a99b660b Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Wed, 30 Aug 2023 16:02:33 +0800 Subject: [PATCH 025/194] [clang-tidy][task 5] enable `modernize-make-shared` and fix existing linter errors (#55807) --- .clang-tidy | 2 +- .../eager_manual/forwards/add_n_fwd_func.cc | 4 ++-- .../forwards/conv2d_fwd_function.cc | 4 ++-- .../forwards/multiply_fwd_func.cc | 11 +++++----- .../forwards/sync_batch_norm_fwd_func.cc | 8 ++++---- .../manual/eager_manual/nodes/conv2d_nodes.cc | 2 +- .../eager_manual/nodes/multiply_node.cc | 2 +- .../forwards/fused_attention_fwd_func.cc | 5 +++-- ...as_dropout_residual_layer_norm_fwd_func.cc | 6 +++--- .../forwards/fused_feedforward_fwd_func.cc | 5 +++-- .../forwards/fused_gate_attention_fwd_func.cc | 5 +++-- .../forwards/fused_gemm_epilogue_fwd_func.cc | 5 +++-- .../auto_code_generator/eager_generator.cc | 15 +++++++------- .../generator/eager_gen.py | 6 +++--- paddle/fluid/pybind/eager_functions.cc | 4 ++-- paddle/fluid/pybind/imperative.cc | 4 ++-- .../performance_tests/benchmark_utils.cc | 20 +++++-------------- 17 files changed, 51 insertions(+), 57 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 37d956eaa943b..c0d5b09ac1f16 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -170,7 +170,7 @@ modernize-avoid-c-arrays, -modernize-deprecated-headers, -modernize-deprecated-ios-base-aliases, modernize-loop-convert, --modernize-make-shared, +modernize-make-shared, modernize-make-unique, -modernize-pass-by-value, modernize-raw-string-literal, diff --git a/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc b/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc index d14832b80a1db..7dca372a23ba0 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc @@ -83,8 +83,8 @@ paddle::Tensor add_n_ad_func(const std::vector& x) { egr::EagerUtils::PassStopGradient(false, out_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new AddNGradNodeFinal(1, 1)); + auto grad_node = std::shared_ptr( // NOLINT + new AddNGradNodeFinal(1, 1)); // Set forward's stack if (FLAGS_check_nan_inf) { diff --git a/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc b/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc index b7ca5a7c26710..5ef6fcf9c3fa1 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc @@ -137,8 +137,8 @@ paddle::Tensor conv2d_ad_func(const paddle::Tensor& input, egr::EagerUtils::PassStopGradient(false, out_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new Conv2dGradNodeFinal(1, 2)); + auto grad_node = std::shared_ptr( // NOLINT + new Conv2dGradNodeFinal(1, 2)); // Set forward's stack if (FLAGS_check_nan_inf) { diff --git a/paddle/fluid/eager/api/manual/eager_manual/forwards/multiply_fwd_func.cc b/paddle/fluid/eager/api/manual/eager_manual/forwards/multiply_fwd_func.cc index d9b34643034a4..0a72ab810fc64 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/forwards/multiply_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/forwards/multiply_fwd_func.cc @@ -132,8 +132,8 @@ paddle::Tensor multiply_ad_func(const paddle::Tensor& x, egr::EagerUtils::PassStopGradient(false, out_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new MultiplyGradNode(1, 2)); + auto grad_node = std::shared_ptr( // NOLINT + new MultiplyGradNode(1, 2)); // Set for forward trace if (FLAGS_check_nan_inf) { grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack()); @@ -275,7 +275,8 @@ paddle::Tensor& multiply__ad_func(paddle::Tensor& x, // NOLINT paddle::platform::TracerEventType::OperatorInner, 1); - grad_node = std::shared_ptr(new MultiplyGradNode(1, 2)); + grad_node = std::shared_ptr( // NOLINT + new MultiplyGradNode(1, 2)); // Set for forward trace if (FLAGS_check_nan_inf) { grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack()); @@ -462,8 +463,8 @@ paddle::Tensor multiply_ad_func(const paddle::Tensor& x, egr::EagerUtils::PassStopGradient(false, out_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new MultiplyGradNode(1, 2)); + auto grad_node = std::shared_ptr( // NOLINT + new MultiplyGradNode(1, 2)); // Set for forward trace if (FLAGS_check_nan_inf) { grad_node->SetForwardTrace(egr::Controller::Instance().GetPythonStack()); diff --git a/paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc b/paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc index a5b0c4d70b07f..79b5c60c66b19 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc @@ -225,8 +225,8 @@ sync_batch_norm__ad_func(const paddle::Tensor& x, reserve_space_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new SyncBatchNormGradNode(6, 5)); + auto grad_node = std::shared_ptr( // NOLINT + new SyncBatchNormGradNode(6, 5)); // Set forward's stack if (FLAGS_check_nan_inf) { @@ -567,8 +567,8 @@ sync_batch_norm__ad_func(const paddle::Tensor& x, reserve_space_autograd_meta); // Node Construction - auto grad_node = - std::shared_ptr(new SyncBatchNormGradNode(6, 5)); + auto grad_node = std::shared_ptr( // NOLINT + new SyncBatchNormGradNode(6, 5)); egr::Controller::Instance().PushBackForceSequentialNodes(grad_node.get()); // SetAttributes if needed grad_node->SetAttributemomentum(momentum); diff --git a/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc b/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc index a7d00f8df1802..438188ea4b7f6 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc @@ -123,7 +123,7 @@ Conv2dGradNodeFinal::operator()( 1); // Node Construction - auto grad_node = std::shared_ptr( + auto grad_node = std::shared_ptr( // NOLINT new Conv2dDoubleGradNodeFinal(2, 3)); // SetAttributes if needed grad_node->SetAttributestrides(strides); diff --git a/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc b/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc index f7a90c43e7d93..f3ae667777aa6 100644 --- a/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc +++ b/paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc @@ -158,7 +158,7 @@ MultiplyGradNode::operator()( 1); // Node Construction - auto grad_node = std::shared_ptr( + auto grad_node = std::shared_ptr( // NOLINT new MultiplyDoubleGradNode(2, 3)); // SetAttributes if needed grad_node->SetAttributeaxis(axis); diff --git a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_attention_fwd_func.cc b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_attention_fwd_func.cc index 77ecc8a30e19f..0f1192ae1bd28 100644 --- a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_attention_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_attention_fwd_func.cc @@ -390,8 +390,9 @@ fused_attention_dygraph_function( p_autograd_CacheKVOut, p_autograd_Y); // Create GradOpNode - auto grad_node = std::shared_ptr( - new fused_attentionGradNodeCompat(20, 23)); + auto grad_node = + std::shared_ptr( // NOLINT + new fused_attentionGradNodeCompat(20, 23)); bool pre_layer_norm = false; if (attrs.count("pre_layer_norm")) { diff --git a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_bias_dropout_residual_layer_norm_fwd_func.cc b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_bias_dropout_residual_layer_norm_fwd_func.cc index 4b57d2e3c5ba2..1e61714525f48 100644 --- a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_bias_dropout_residual_layer_norm_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_bias_dropout_residual_layer_norm_fwd_func.cc @@ -184,9 +184,9 @@ fused_bias_dropout_residual_layer_norm_dygraph_function( p_autograd_LnVariance, p_autograd_Y); // Create GradOpNode - auto grad_node = - std::shared_ptr( - new fused_bias_dropout_residual_layer_normGradNodeCompat(5, 5)); + auto grad_node = std::shared_ptr< // NOLINT + fused_bias_dropout_residual_layer_normGradNodeCompat>( + new fused_bias_dropout_residual_layer_normGradNodeCompat(5, 5)); // Set Attributes grad_node->SetAttrMap(std::move(attrs)); diff --git a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_feedforward_fwd_func.cc b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_feedforward_fwd_func.cc index 9f13579c5aab7..e46e677c318a1 100644 --- a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_feedforward_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_feedforward_fwd_func.cc @@ -310,8 +310,9 @@ fused_feedforward_dygraph_function( p_autograd_Dropout1Out, p_autograd_Dropout2Out); // Create GradOpNode - auto grad_node = std::shared_ptr( - new fused_feedforwardGradNodeCompat(11, 11)); + auto grad_node = + std::shared_ptr( // NOLINT + new fused_feedforwardGradNodeCompat(11, 11)); bool pre_layer_norm = false; if (attrs.count("pre_layer_norm")) { diff --git a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gate_attention_fwd_func.cc b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gate_attention_fwd_func.cc index 546b60438fedc..8c66a10631142 100644 --- a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gate_attention_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gate_attention_fwd_func.cc @@ -301,8 +301,9 @@ fused_gate_attention_dygraph_function( p_autograd_GateOut, p_autograd_Out); // Create GradOpNode - auto grad_node = std::shared_ptr( - new fused_gate_attentionGradNodeCompat(9, 12)); + auto grad_node = + std::shared_ptr( // NOLINT + new fused_gate_attentionGradNodeCompat(9, 12)); bool merge_qkv = true; if (attrs.count("merge_qkv")) { diff --git a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gemm_epilogue_fwd_func.cc b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gemm_epilogue_fwd_func.cc index 2eb7327601189..74e10cd0685ad 100644 --- a/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gemm_epilogue_fwd_func.cc +++ b/paddle/fluid/eager/api/manual/fluid_manual/forwards/fused_gemm_epilogue_fwd_func.cc @@ -102,8 +102,9 @@ paddle::Tensor fused_gemm_epilogue_dygraph_function( VLOG(6) << " Construct Grad for fused_gemm_epilogue "; egr::EagerUtils::PassStopGradient(false, p_autograd_Out); // Create GradOpNode - auto grad_node = std::shared_ptr( - new fused_gemm_epilogueGradNodeCompat(1, 3)); + auto grad_node = + std::shared_ptr( // NOLINT + new fused_gemm_epilogueGradNodeCompat(1, 3)); // Set Attributes grad_node->SetAttrMap(std::move(attrs)); diff --git a/paddle/fluid/eager/auto_code_generator/eager_generator.cc b/paddle/fluid/eager/auto_code_generator/eager_generator.cc index 6cc98ed6fb374..d250989199a52 100644 --- a/paddle/fluid/eager/auto_code_generator/eager_generator.cc +++ b/paddle/fluid/eager/auto_code_generator/eager_generator.cc @@ -827,9 +827,8 @@ static bool CollectGradInformationFromOpInfo( const std::string& in_name = op_proto.inputs()[0].name(); ins[in_name] = {}; for (size_t i = 0; i < NUM_CREATED_DUP_INPUTS; i++) { - ins[in_name].emplace_back(std::shared_ptr( - new paddle::imperative::VarBase("auto_" + in_name + "_" + - std::to_string(i)))); + ins[in_name].emplace_back(std::make_shared( + "auto_" + in_name + "_" + std::to_string(i))); ins[in_name][i]->SetOverridedStopGradient(false); ins[in_name][i]->MutableVar()->GetMutable(); } @@ -852,8 +851,8 @@ static bool CollectGradInformationFromOpInfo( // but we only need to identify the slot name order, // therefore fill in 1 single input VarBase is enough in this scenario - ins[in_name] = {std::shared_ptr( - new paddle::imperative::VarBase("auto_" + in_name))}; + ins[in_name] = { + std::make_shared("auto_" + in_name)}; ins[in_name][0]->SetOverridedStopGradient(false); ins[in_name][0]->MutableVar()->GetMutable(); } @@ -870,8 +869,8 @@ static bool CollectGradInformationFromOpInfo( // We always create output VarBase regardless of its dispensability. // We dont know the exact number of outputs during code generation, // however, simply identifying the slot name order would be enough - outs[out_name] = {std::shared_ptr( - new paddle::imperative::VarBase("auto_" + out_name))}; + outs[out_name] = { + std::make_shared("auto_" + out_name)}; outs[out_name][0]->SetOverridedStopGradient(false); outs[out_name][0]->MutableVar()->GetMutable(); } @@ -1179,7 +1178,7 @@ static std::string GenerateGradNodeCreationContent( const char* GRAD_OP_NODE_TEMPLATE = " auto grad_node = std::shared_ptr<%sGradNodeCompat>(new " "%sGradNodeCompat(%d, " - "%d));\n"; + "%d)); // NOLINT\n"; grad_node_creation_str += " // Create GradOpNode\n"; grad_node_creation_str += paddle::string::Sprintf(GRAD_OP_NODE_TEMPLATE, op_type, diff --git a/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py b/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py index 9bbe26d0f8a4e..da4a9aab53870 100644 --- a/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py +++ b/paddle/fluid/eager/auto_code_generator/generator/eager_gen.py @@ -953,13 +953,13 @@ def GenerateNodeCreationCodes(self, for_backward=False, is_inplaced=False): # Helper indent = GetIndent(2) - # NOTE(Aurelius74): DO NOT use make_shared here. Because some Node contains experimental::Scalar + # NOTE(Aurelius84): DO NOT use make_shared here. Because some Node contains experimental::Scalar # which contains "complex128" as data. "complex128" is memory-aligned manually. But make_shared # request MEMALIGN for allocation (Maybe). # See https://stackoverflow.com/questions/31228656/how-can-shared-ptr-disrupt-alignment # and https://github.com/MRtrix3/mrtrix3/issues/957 - node_construction_str = f"{indent}auto grad_node = std::shared_ptr<{grad_node_name}>(new {grad_node_name}({num_backward_inputs}, {num_backward_outputs}));" - node_assignment_str = f"{indent}grad_node = std::shared_ptr<{grad_node_name}>(new {grad_node_name}({num_backward_inputs}, {num_backward_outputs}));" + node_construction_str = f"{indent}auto grad_node = std::shared_ptr<{grad_node_name}>(new {grad_node_name}({num_backward_inputs}, {num_backward_outputs})); // NOLINT" + node_assignment_str = f"{indent}grad_node = std::shared_ptr<{grad_node_name}>(new {grad_node_name}({num_backward_inputs}, {num_backward_outputs})); // NOLINT" # SetAttributes set_attributes_list = [] diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index db6ce75c7d045..d03a20537eee6 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -1187,8 +1187,8 @@ static PyObject* eager_api_to_uva_tensor(PyObject* self, PyObject* kwargs) { EAGER_TRY VLOG(4) << "Running in eager_api_to_uva_tensor."; - auto new_tensor = std::shared_ptr( - new paddle::Tensor(egr::Controller::Instance().GenerateUniqueName())); + auto new_tensor = std::make_shared( + egr::Controller::Instance().GenerateUniqueName()); PyObject* obj = PyTuple_GET_ITEM(args, 0); auto array = py::cast(py::handle(obj)); diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 2e67906f4f2a9..111e8ebdfdf86 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -976,8 +976,8 @@ void BindImperative(py::module *m_ptr) { "to_uva_tensor", [](const py::object &obj, int device_id) { const auto &tracer = imperative::GetCurrentTracer(); - auto new_tensor = std::shared_ptr( - new imperative::VarBase(tracer->GenerateUniqueName())); + auto new_tensor = + std::make_shared(tracer->GenerateUniqueName()); auto array = obj.cast(); if (py::isinstance>(array)) { SetUVATensorFromPyArray(new_tensor, array, device_id); diff --git a/test/cpp/eager/performance_tests/benchmark_utils.cc b/test/cpp/eager/performance_tests/benchmark_utils.cc index 83d14a6b45b89..73c4404fca7a8 100644 --- a/test/cpp/eager/performance_tests/benchmark_utils.cc +++ b/test/cpp/eager/performance_tests/benchmark_utils.cc @@ -241,9 +241,7 @@ void benchmark_fluid_scale(const std::shared_ptr& X, for (size_t i = 0; i < max_num_runs; i++) { imperative::NameVarBaseMap ins = {{"X", {tmp_out}}}; imperative::NameVarBaseMap outs = { - {"Out", - {std::shared_ptr( - new imperative::VarBase(true, "Out"))}}}; + {"Out", {std::make_shared(true, "Out")}}}; tracer.TraceOp("scale", ins, outs, attrs, place, true); @@ -277,9 +275,7 @@ void benchmark_fluid_matmul(const std::shared_ptr& X, framework::AttributeMap attrs; imperative::NameVarBaseMap ins = {{"X", {tmp_out}}, {"Y", {Y}}}; imperative::NameVarBaseMap outs = { - {"Out", - {std::shared_ptr( - new imperative::VarBase(true, "Out"))}}}; + {"Out", {std::make_shared(true, "Out")}}}; tracer.TraceOp("matmul_v2", ins, outs, attrs, place, true); @@ -316,17 +312,13 @@ void benchmark_fluid_mlp( for (size_t i = 0; i < MLP_NUM_LINEAR; i++) { // Matmul0 ins = {{"X", {input0}}, {"Y", {Ws[0]}}}; - outs = {{"Out", - {std::shared_ptr( - new imperative::VarBase(true, "Out"))}}}; + outs = {{"Out", {std::make_shared(true, "Out")}}}; tracer.TraceOp("matmul_v2", ins, outs, attrs, place, true); // EW-Add0 ins = {{"X", outs["Out"]}, {"Y", {Bs[i]}}}; - outs = {{"Out", - {std::shared_ptr( - new imperative::VarBase(true, "Out"))}}}; + outs = {{"Out", {std::make_shared(true, "Out")}}}; tracer.TraceOp("elementwise_add", ins, outs, attrs, place, true); input0 = outs["Out"][0]; @@ -334,9 +326,7 @@ void benchmark_fluid_mlp( // ReduceSum ins = {{"X", {input0}}}; - outs = {{"Out", - {std::shared_ptr( - new imperative::VarBase(true, "Out"))}}}; + outs = {{"Out", {std::make_shared(true, "Out")}}}; attrs = {{"reduce_all", true}}; tracer.TraceOp("reduce_sum", ins, outs, attrs, place, true); From 5f3c7ba437f020b32ef53e6916b8264d7a9ed830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:15:24 +0800 Subject: [PATCH 026/194] [xdoctest] reformat example code with google style in No. 201 (#56472) * xdoc * Update python/paddle/tensor/einsum.py * Update einsum.py * Apply suggestions from code review * Update einsum.py * Apply suggestions from code review --- python/paddle/tensor/einsum.py | 130 ++++++++++++++++----------------- 1 file changed, 63 insertions(+), 67 deletions(-) diff --git a/python/paddle/tensor/einsum.py b/python/paddle/tensor/einsum.py index c379136e26287..11fb7e6f47607 100644 --- a/python/paddle/tensor/einsum.py +++ b/python/paddle/tensor/einsum.py @@ -953,73 +953,69 @@ def einsum(equation, *operands): Examples: .. code-block:: python - import paddle - paddle.seed(102) - x = paddle.rand([4]) - y = paddle.rand([5]) - - # sum - print(paddle.einsum('i->', x)) - # Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # 1.95791852) - - # dot - print(paddle.einsum('i,i->', x, x)) - # Tensor(shape=[], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # 1.45936954) - - # outer - print(paddle.einsum("i,j->ij", x, y)) - # Tensor(shape=[4, 5], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[0.00079869, 0.00120950, 0.00136844, 0.00187187, 0.00192194], - # [0.23455200, 0.35519385, 0.40186870, 0.54970956, 0.56441545], - # [0.11773264, 0.17828843, 0.20171674, 0.27592498, 0.28330654], - # [0.32897076, 0.49817693, 0.56364071, 0.77099484, 0.79162055]]) - - A = paddle.rand([2, 3, 2]) - B = paddle.rand([2, 2, 3]) - - # transpose - print(paddle.einsum('ijk->kji', A)) - # Tensor(shape=[2, 3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[[0.95649719, 0.49684682], - # [0.80071914, 0.46258664], - # [0.49814570, 0.33383518]], - # - # [[0.07637714, 0.29374704], - # [0.51470858, 0.51907635], - # [0.99066722, 0.55802226]]]) - - # batch matrix multiplication - print(paddle.einsum('ijk, ikl->ijl', A,B)) - # Tensor(shape=[2, 3, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[[0.32172769, 0.50617385, 0.41394392], - # [0.51736701, 0.49921003, 0.38730967], - # [0.69078457, 0.42282537, 0.30161136]], - # - # [[0.32043904, 0.18164253, 0.27810261], - # [0.50226176, 0.24512935, 0.39881429], - # [0.51476848, 0.23367381, 0.39229113]]]) - - # Ellipsis transpose - print(paddle.einsum('...jk->...kj', A)) - # Tensor(shape=[2, 2, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[[0.95649719, 0.80071914, 0.49814570], - # [0.07637714, 0.51470858, 0.99066722]], - # - # [[0.49684682, 0.46258664, 0.33383518], - # [0.29374704, 0.51907635, 0.55802226]]]) - - # Ellipsis batch matrix multiplication - print(paddle.einsum('...jk, ...kl->...jl', A,B)) - # Tensor(shape=[2, 3, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[[0.32172769, 0.50617385, 0.41394392], - # [0.51736701, 0.49921003, 0.38730967], - # [0.69078457, 0.42282537, 0.30161136]], - # - # [[0.32043904, 0.18164253, 0.27810261], - # [0.50226176, 0.24512935, 0.39881429], - # [0.51476848, 0.23367381, 0.39229113]]]) + >>> import paddle + >>> paddle.seed(102) + >>> x = paddle.rand([4]) + >>> y = paddle.rand([5]) + + >>> # sum + >>> print(paddle.einsum('i->', x)) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.81225157) + + >>> # dot + >>> print(paddle.einsum('i,i->', x, x)) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.13530672) + + >>> # outer + >>> print(paddle.einsum("i,j->ij", x, y)) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.26443148, 0.05962684, 0.25360870, 0.21900642, 0.56994802], + [0.20955276, 0.04725220, 0.20097610, 0.17355499, 0.45166403], + [0.35836059, 0.08080698, 0.34369346, 0.29680005, 0.77240014], + [0.00484230, 0.00109189, 0.00464411, 0.00401047, 0.01043695]]) + + >>> A = paddle.rand([2, 3, 2]) + >>> B = paddle.rand([2, 2, 3]) + + >>> # transpose + >>> print(paddle.einsum('ijk->kji', A)) + Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.50882483, 0.56067896], + [0.84598064, 0.36310029], + [0.55289471, 0.33273944]], + [[0.04836850, 0.73811269], + [0.29769155, 0.28137168], + [0.84636718, 0.67521429]]]) + + >>> # batch matrix multiplication + >>> print(paddle.einsum('ijk, ikl->ijl', A,B)) + Tensor(shape=[2, 3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.36321065, 0.42009076, 0.40849245], + [0.74353045, 0.79189068, 0.81345987], + [0.90488225, 0.79786193, 0.93451476]], + [[0.12680580, 1.06945944, 0.79821426], + [0.07774551, 0.55068684, 0.44512171], + [0.08053084, 0.80583858, 0.56031936]]]) + + >>> # Ellipsis transpose + >>> print(paddle.einsum('...jk->...kj', A)) + Tensor(shape=[2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.50882483, 0.84598064, 0.55289471], + [0.04836850, 0.29769155, 0.84636718]], + [[0.56067896, 0.36310029, 0.33273944], + [0.73811269, 0.28137168, 0.67521429]]]) + + >>> # Ellipsis batch matrix multiplication + >>> print(paddle.einsum('...jk, ...kl->...jl', A,B)) + Tensor(shape=[2, 3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.36321065, 0.42009076, 0.40849245], + [0.74353045, 0.79189068, 0.81345987], + [0.90488225, 0.79786193, 0.93451476]], + [[0.12680580, 1.06945944, 0.79821426], + [0.07774551, 0.55068684, 0.44512171], + [0.08053084, 0.80583858, 0.56031936]]]) """ import os From 1c858591eafe233cbf164a2a2d10f1b11c550ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A3=9E=E7=8C=AA?= <106524776+ooooo-create@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:19:19 +0800 Subject: [PATCH 027/194] [xdoctest][task 239] reformat example code with google style in `python/paddle/incubate/asp/asp.py` (#56731) * [Doctest]fix No.239, test=docs_preview * fix style --- python/paddle/incubate/asp/asp.py | 597 +++++++++++++++--------------- 1 file changed, 299 insertions(+), 298 deletions(-) diff --git a/python/paddle/incubate/asp/asp.py b/python/paddle/incubate/asp/asp.py index 7bf04dc151c7f..8ba4966973fed 100644 --- a/python/paddle/incubate/asp/asp.py +++ b/python/paddle/incubate/asp/asp.py @@ -51,71 +51,71 @@ def set_excluded_layers(param_names, main_program=None): .. code-block:: python - import paddle - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 100) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - prediction = self.linear1(hidden) - return prediction - - my_layer = MyLayer() - optimizer = paddle.optimizer.SGD( - learning_rate=0.01, parameters=my_layer.parameters()) - - # Need to set excluded layers before calling decorate - paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()]) - - optimizer = paddle.incubate.asp.decorate(optimizer) + >>> import paddle + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 100) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... prediction = self.linear1(hidden) + ... return prediction + + >>> my_layer = MyLayer() + >>> optimizer = paddle.optimizer.SGD( + ... learning_rate=0.01, parameters=my_layer.parameters()) + + >>> # Need to set excluded layers before calling decorate + >>> paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()]) + + >>> optimizer = paddle.incubate.asp.decorate(optimizer) 2. Usage of Static Graph .. code-block:: python - import paddle - - paddle.enable_static() - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 100) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - prediction = self.linear1(hidden) - return prediction - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - - with paddle.static.program_guard(main_program, startup_program): - input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) - label = paddle.static.data(name='label', shape=[None, 100]) - my_layer = MyLayer() - prob = my_layer(input_data) - loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) - - # Setup exluded layers out from ASP workflow. - # Please note, excluded_layers must be set before calling optimizer.minimize(). - paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program) - - optimizer = paddle.optimizer.SGD(learning_rate=0.1) - optimizer = paddle.static.amp.decorate(optimizer ) - # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which - # will insert necessary masking operations for ASP workflow. - optimizer = paddle.incubate.asp.decorate(optimizer) - optimizer.minimize(loss, startup_program) + >>> import paddle + + >>> paddle.enable_static() + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 100) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... prediction = self.linear1(hidden) + ... return prediction + + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() + + >>> with paddle.static.program_guard(main_program, startup_program): + ... input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) + ... label = paddle.static.data(name='label', shape=[None, 100]) + ... my_layer = MyLayer() + ... prob = my_layer(input_data) + ... loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) + ... + ... # Setup exluded layers out from ASP workflow. + ... # Please note, excluded_layers must be set before calling optimizer.minimize(). + ... paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program) + ... + ... optimizer = paddle.optimizer.SGD(learning_rate=0.1) + ... optimizer = paddle.static.amp.decorate(optimizer ) + ... # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which + ... # will insert necessary masking operations for ASP workflow. + ... optimizer = paddle.incubate.asp.decorate(optimizer) + ... optimizer.minimize(loss, startup_program) """ if main_program is None: main_program = paddle.static.default_main_program() @@ -138,77 +138,77 @@ def reset_excluded_layers(main_program=None): .. code-block:: python - import paddle - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 100) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - prediction = self.linear1(hidden) - return prediction - - my_layer = MyLayer() - optimizer = paddle.optimizer.SGD( - learning_rate=0.01, parameters=my_layer.parameters()) - - # Need to set excluded layers before calling decorate - paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()]) - # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. - # Please note, reset_excluded_layers also must be called before calling asp.decorate(). - paddle.incubate.asp.reset_excluded_layers() - - optimizer = paddle.incubate.asp.decorate(optimizer) + >>> import paddle + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 100) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... prediction = self.linear1(hidden) + ... return prediction + + >>> my_layer = MyLayer() + >>> optimizer = paddle.optimizer.SGD( + ... learning_rate=0.01, parameters=my_layer.parameters()) + + >>> # Need to set excluded layers before calling decorate + >>> paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()]) + >>> # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. + >>> # Please note, reset_excluded_layers also must be called before calling asp.decorate(). + >>> paddle.incubate.asp.reset_excluded_layers() + + >>> optimizer = paddle.incubate.asp.decorate(optimizer) 2. Usage of Static Graph .. code-block:: python - import paddle - - paddle.enable_static() - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 100) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - prediction = self.linear1(hidden) - return prediction - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - - with paddle.static.program_guard(main_program, startup_program): - input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) - label = paddle.static.data(name='label', shape=[None, 100]) - my_layer = MyLayer() - prob = my_layer(input_data) - loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) - - # Setup exluded layers out from ASP workflow. - # Please note, excluded_layers must be set before calling optimizer.minimize(). - paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program) - # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. - # Please note, reset_excluded_layers also must be called before calling optimizer.minimize(). - paddle.incubate.asp.reset_excluded_layers(main_program) - - optimizer = paddle.optimizer.SGD(learning_rate=0.1) - optimizer = paddle.static.amp.decorate(optimizer ) - # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which - # will insert necessary masking operations for ASP workflow. - optimizer = paddle.incubate.asp.decorate(optimizer) - optimizer.minimize(loss, startup_program) + >>> import paddle + + >>> paddle.enable_static() + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 100) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... prediction = self.linear1(hidden) + ... return prediction + + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() + + >>> with paddle.static.program_guard(main_program, startup_program): + ... input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) + ... label = paddle.static.data(name='label', shape=[None, 100]) + ... my_layer = MyLayer() + ... prob = my_layer(input_data) + ... loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) + ... + ... # Setup exluded layers out from ASP workflow. + ... # Please note, excluded_layers must be set before calling optimizer.minimize(). + ... paddle.incubate.asp.set_excluded_layers([my_layer.linear1.full_name()], main_program) + ... # Reset excluded_layers, all supported layers would be included into Automatic SParsity's workflow. + ... # Please note, reset_excluded_layers also must be called before calling optimizer.minimize(). + ... paddle.incubate.asp.reset_excluded_layers(main_program) + ... + ... optimizer = paddle.optimizer.SGD(learning_rate=0.1) + ... optimizer = paddle.static.amp.decorate(optimizer ) + ... # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which + ... # will insert necessary masking operations for ASP workflow. + ... optimizer = paddle.incubate.asp.decorate(optimizer) + ... optimizer.minimize(loss, startup_program) """ ASPHelper.reset_excluded_layers(main_program=main_program) @@ -229,72 +229,72 @@ def decorate(optimizer): .. code-block:: python - import paddle - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 32) - self.linear2 = paddle.nn.Linear(32, 32) - self.linear3 = paddle.nn.Linear(32, 10) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - hidden = self.linear1(hidden) - hidden = self.linear2(hidden) - prediction = self.linear3(hidden) - return prediction - - my_layer = MyLayer() - optimizer = paddle.optimizer.SGD( - learning_rate=0.01, parameters=my_layer.parameters()) - - # Calling paddle.incubate.asp.decorate() to wrap step() in optimizer, which - # will apply necessary masking operations for ASP workflow. - # In dynamic graph mode, ASP would create related mask variables during decoration. - optimizer = paddle.incubate.asp.decorate(optimizer) + >>> import paddle + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 32) + ... self.linear2 = paddle.nn.Linear(32, 32) + ... self.linear3 = paddle.nn.Linear(32, 10) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... hidden = self.linear1(hidden) + ... hidden = self.linear2(hidden) + ... prediction = self.linear3(hidden) + ... return prediction + + >>> my_layer = MyLayer() + >>> optimizer = paddle.optimizer.SGD( + ... learning_rate=0.01, parameters=my_layer.parameters()) + + >>> # Calling paddle.incubate.asp.decorate() to wrap step() in optimizer, which + >>> # will apply necessary masking operations for ASP workflow. + >>> # In dynamic graph mode, ASP would create related mask variables during decoration. + >>> optimizer = paddle.incubate.asp.decorate(optimizer) 2. Usage of Static Graph .. code-block:: python - import paddle - - paddle.enable_static() - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 100) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - prediction = self.linear1(hidden) - return prediction - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - - with paddle.static.program_guard(main_program, startup_program): - input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) - label = paddle.static.data(name='label', shape=[None, 100]) - my_layer = MyLayer() - prob = my_layer(input_data) - loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) - - optimizer = paddle.optimizer.SGD(learning_rate=0.1) - # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which - # will insert necessary masking operations for ASP workflow. - # In static graph mode, ASP creates related mask variables - # during minimize(). - optimizer = paddle.incubate.asp.decorate(optimizer) - optimizer.minimize(loss, startup_program) + >>> import paddle + + >>> paddle.enable_static() + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 100) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... prediction = self.linear1(hidden) + ... return prediction + + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() + + >>> with paddle.static.program_guard(main_program, startup_program): + ... input_data = paddle.static.data(name='data', shape=[None, 3, 224, 224]) + ... label = paddle.static.data(name='label', shape=[None, 100]) + ... my_layer = MyLayer() + ... prob = my_layer(input_data) + ... loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) + ... + ... optimizer = paddle.optimizer.SGD(learning_rate=0.1) + ... # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which + ... # will insert necessary masking operations for ASP workflow. + ... # In static graph mode, ASP creates related mask variables + ... # during minimize(). + ... optimizer = paddle.incubate.asp.decorate(optimizer) + ... optimizer.minimize(loss, startup_program) """ return ASPHelper.decorate(optimizer) @@ -326,112 +326,112 @@ def prune_model(model, n=2, m=4, mask_algo='mask_1d', with_mask=True): .. code-block:: python - import paddle - import numpy as np - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 32) - self.linear2 = paddle.nn.Linear(32, 32) - self.linear3 = paddle.nn.Linear(32, 10) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - hidden = self.linear1(hidden) - hidden = self.linear2(hidden) - prediction = self.linear3(hidden) - return prediction - - my_layer = MyLayer() - loss_fn = paddle.nn.MSELoss(reduction='mean') - - optimizer = paddle.optimizer.SGD( - learning_rate=0.01, parameters=my_layer.parameters()) - - # Calling paddle.incubate.asp.decorate() to wrap step() in optimizer, which - # will apply necessary masking operations for ASP workflow. - # In dynamic graph mode, ASP would create related mask variables during decoration. - optimizer = paddle.incubate.asp.decorate(optimizer) - - # Must call paddle.incubate.asp.decorate() first before calling paddle.incubate.asp.prune_model() - paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best') - - for i in range(10): - imgs = paddle.to_tensor( - np.random.randn(64, 3, 32, 32), - dtype='float32', stop_gradient=False) - labels = paddle.to_tensor( - np.random.randint(10, size=(64, 1)), - dtype='float32', stop_gradient=False) - output = my_layer(imgs) - loss = loss_fn(output, labels) - loss.backward() - optimizer.step() - optimizer.clear_grad() + >>> import paddle + >>> import numpy as np + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 32) + ... self.linear2 = paddle.nn.Linear(32, 32) + ... self.linear3 = paddle.nn.Linear(32, 10) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... hidden = self.linear1(hidden) + ... hidden = self.linear2(hidden) + ... prediction = self.linear3(hidden) + ... return prediction + + >>> my_layer = MyLayer() + >>> loss_fn = paddle.nn.MSELoss(reduction='mean') + + >>> optimizer = paddle.optimizer.SGD( + ... learning_rate=0.01, parameters=my_layer.parameters()) + + >>> # Calling paddle.incubate.asp.decorate() to wrap step() in optimizer, which + >>> # will apply necessary masking operations for ASP workflow. + >>> # In dynamic graph mode, ASP would create related mask variables during decoration. + >>> optimizer = paddle.incubate.asp.decorate(optimizer) + + >>> # Must call paddle.incubate.asp.decorate() first before calling paddle.incubate.asp.prune_model() + >>> paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best') + + >>> for i in range(10): + ... imgs = paddle.to_tensor( + ... np.random.randn(64, 3, 32, 32), + ... dtype='float32', stop_gradient=False) + ... labels = paddle.to_tensor( + ... np.random.randint(10, size=(64, 1)), + ... dtype='float32', stop_gradient=False) + ... output = my_layer(imgs) + ... loss = loss_fn(output, labels) + ... loss.backward() + ... optimizer.step() + ... optimizer.clear_grad() 2. Usage of Static Graph .. code-block:: python - import paddle - import numpy as np - - paddle.enable_static() - - class MyLayer(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.conv1 = paddle.nn.Conv2D( - in_channels=3, out_channels=4, kernel_size=3, padding=2) - self.linear1 = paddle.nn.Linear(4624, 32) - self.linear2 = paddle.nn.Linear(32, 32) - self.linear3 = paddle.nn.Linear(32, 10) - - def forward(self, img): - hidden = self.conv1(img) - hidden = paddle.flatten(hidden, start_axis=1) - hidden = self.linear1(hidden) - hidden = self.linear2(hidden) - prediction = self.linear3(hidden) - return prediction - - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - - with paddle.static.program_guard(main_program, startup_program): - input_data = paddle.static.data(name='data', shape=[None, 3, 32, 32]) - label = paddle.static.data(name='label', shape=[None, 1]) - my_layer = MyLayer() - prob = my_layer(input_data) - loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) - - optimizer = paddle.optimizer.SGD(learning_rate=0.1) - # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which - # will insert necessary masking operations for ASP workflow. - # In static graph mode, ASP creates related mask variables - # during minimize(). - optimizer = paddle.incubate.asp.decorate(optimizer) - optimizer.minimize(loss, startup_program) - - device = paddle.device.get_device() - place = paddle.set_device(device) - - exe = paddle.static.Executor(place) - exe.run(startup_program) - - # Must call exe.run(startup_program) first before calling paddle.asp.prune_model() - paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best') - # it also be accepted to call - # paddle.incubate.asp.prune_model(main_program, mask_algo='mask_2d_best') - - for i in range(10): - imgs = np.random.randn(64, 3, 32, 32).astype('float32') - labels = np.random.randint(10, size=(64, 1)).astype('float32') - exe.run(main_program, feed={'data':imgs, 'label':labels}) + >>> import paddle + >>> import numpy as np + + >>> paddle.enable_static() + + >>> class MyLayer(paddle.nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self.conv1 = paddle.nn.Conv2D( + ... in_channels=3, out_channels=4, kernel_size=3, padding=2) + ... self.linear1 = paddle.nn.Linear(4624, 32) + ... self.linear2 = paddle.nn.Linear(32, 32) + ... self.linear3 = paddle.nn.Linear(32, 10) + ... + ... def forward(self, img): + ... hidden = self.conv1(img) + ... hidden = paddle.flatten(hidden, start_axis=1) + ... hidden = self.linear1(hidden) + ... hidden = self.linear2(hidden) + ... prediction = self.linear3(hidden) + ... return prediction + + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() + + >>> with paddle.static.program_guard(main_program, startup_program): + ... input_data = paddle.static.data(name='data', shape=[None, 3, 32, 32]) + ... label = paddle.static.data(name='label', shape=[None, 1]) + ... my_layer = MyLayer() + ... prob = my_layer(input_data) + ... loss = paddle.mean(paddle.nn.functional.square_error_cost(prob, label)) + ... + ... optimizer = paddle.optimizer.SGD(learning_rate=0.1) + ... # Calling paddle.incubate.asp.decorate() to wrap minimize() in optimizer, which + ... # will insert necessary masking operations for ASP workflow. + ... # In static graph mode, ASP creates related mask variables + ... # during minimize(). + ... optimizer = paddle.incubate.asp.decorate(optimizer) + ... optimizer.minimize(loss, startup_program) + + >>> device = paddle.device.get_device() + >>> place = paddle.set_device(device) + + >>> exe = paddle.static.Executor(place) + >>> exe.run(startup_program) + + >>> # Must call exe.run(startup_program) first before calling paddle.asp.prune_model() + >>> paddle.incubate.asp.prune_model(my_layer, mask_algo='mask_2d_best') + >>> # it also be accepted to call + >>> # paddle.incubate.asp.prune_model(main_program, mask_algo='mask_2d_best') + + >>> for i in range(10): + ... imgs = np.random.randn(64, 3, 32, 32).astype('float32') + ... labels = np.random.randint(10, size=(64, 1)).astype('float32') + ... exe.run(main_program, feed={'data':imgs, 'label':labels}) """ device = paddle.device.get_device() place = paddle.set_device(device) @@ -726,19 +726,20 @@ def _is_supported_layer(cls, main_program, param_name): Examples: .. code-block:: python - from paddle.incubate.asp import ASPHelper + >>> from paddle.incubate.asp import ASPHelper + >>> paddle.enable_static() - main_program = paddle.static.Program() - startup_program = paddle.static.Program() + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - input_data = paddle.static.data(name='data', shape=[None, 128]) - fc = paddle.static.nn.fc(x=input_data, num_flatten_dims=-1, size=32, activation=None) + >>> with paddle.static.program_guard(main_program, startup_program): + ... input_data = paddle.static.data(name='data', shape=[None, 128]) + ... fc = paddle.static.nn.fc(x=input_data, num_flatten_dims=-1, size=32, activation=None) - for param in main_program.global_block().all_parameters(): - ASPHelper._is_supported_layer(main_program, param.name) - # fc_0.w_0 -> True - # fc_0.b_0 -> False + >>> for param in main_program.global_block().all_parameters(): + ... print(param.name,'->',ASPHelper._is_supported_layer(main_program, param.name)) + fc_0.w_0 -> True + fc_0.b_0 -> False """ param_name_list = param_name.split('.') From 2ef4ec71117f77bfe2021fbbf624e617098f3abd Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:58:24 +0800 Subject: [PATCH 028/194] Add paddle custom flags support (#56256) * update * repalce gflags header * replace DEFINE_ with PD_DEFINE_ * fix bug * fix bug * fix bug * update cmake * add :: before some paddle namespace * fix link error * fix CI-Py3 * allow commandline parse * fix SetFlagsFromEnv * fix bug * fix bug * fix CI-CINN * fix CI-Coverage-build * fix CI-Windows-build * fix CI-Inference * fix bug * fix bug * fix CI-CINN * fix inference api test * fix infer_ut test * revert infer_ut gflags usage * update * fix inference * remove flags export macro * revert inference demo_ci gflags usage * update * update * update * update * update * update * update * update * fix bug when turn on WITH_GFLAGS * turn on WITH_GFLAGS * fix bug when turn on WITH_GFLAGS * fix bug when turn on WITH_GFLAGS * update * update and add unittest * add unittest * fix conflict * rerun ci * update * resolve conflict --- CMakeLists.txt | 1 + cmake/cinn.cmake | 10 + cmake/configure.cmake | 4 + cmake/external/brpc.cmake | 13 + cmake/external/gflags.cmake | 11 + cmake/inference_lib.cmake | 11 + .../collective/process_group_custom.cc | 2 +- .../collective/process_group_nccl.cc | 2 +- .../fluid/distributed/collective/reducer.cc | 2 +- .../distributed/fleet_executor/CMakeLists.txt | 11 +- .../distributed/fleet_executor/carrier.cc | 2 +- .../distributed/ps/service/CMakeLists.txt | 27 +- .../distributed/ps/service/brpc_ps_client.cc | 79 +-- .../distributed/ps/service/brpc_ps_server.cc | 24 +- .../ps/service/communicator/communicator.cc | 26 +- .../ps/service/communicator/communicator.h | 8 +- .../ps/service/coordinator_client.cc | 12 +- .../ps/service/coordinator_client.h | 15 +- paddle/fluid/distributed/ps/service/env.h | 2 +- .../distributed/ps/service/heter_client.cc | 14 +- .../distributed/ps/service/heter_client.h | 4 +- .../distributed/ps/service/heter_server.cc | 8 +- .../distributed/ps/service/heter_server.h | 18 +- .../fluid/distributed/ps/service/ps_client.cc | 5 +- .../ps/service/ps_service/graph_py_service.h | 10 +- .../ps/service/ps_service/service.cc | 21 +- .../ps/service/ps_service/service.h | 18 +- paddle/fluid/distributed/ps/service/server.h | 6 +- .../ps/service/simple_rpc/baidu_rpc_server.cc | 4 +- .../ps/table/common_graph_table.cc | 126 ++--- .../distributed/ps/table/common_graph_table.h | 10 +- .../distributed/ps/table/ctr_accessor.cc | 3 +- .../ps/table/ctr_double_accessor.cc | 3 +- .../distributed/ps/table/ctr_dymf_accessor.cc | 3 +- .../distributed/ps/table/depends/dense.h | 2 +- .../ps/table/depends/feature_value.h | 5 +- .../ps/table/depends/initializers.h | 6 +- .../distributed/ps/table/graph/class_macro.h | 2 +- .../ps/table/memory_sparse_table.cc | 97 ++-- .../distributed/ps/table/sparse_accessor.cc | 5 +- .../distributed/ps/table/sparse_sgd_rule.cc | 6 +- .../distributed/ps/table/ssd_sparse_table.cc | 214 ++++---- .../distributed/ps/table/ssd_sparse_table.h | 2 +- paddle/fluid/distributed/ps/table/table.h | 6 +- paddle/fluid/distributed/ps/wrapper/fleet.cc | 40 +- paddle/fluid/distributed/ps/wrapper/fleet.h | 10 +- paddle/fluid/distributed/rpc/CMakeLists.txt | 12 +- paddle/fluid/framework/CMakeLists.txt | 6 +- paddle/fluid/framework/async_executor.cc | 2 +- .../fluid/framework/copy_same_tensor_test.cc | 4 +- .../fluid/framework/details/bkcl_op_handle.h | 2 +- .../fluid/framework/details/build_strategy.cc | 4 +- .../framework/details/build_strategy_test.cc | 2 +- .../details/fused_all_reduce_op_handle.cc | 2 +- paddle/fluid/framework/executor.cc | 2 +- .../fluid/framework/executor_thread_worker.cc | 2 +- paddle/fluid/framework/fleet/CMakeLists.txt | 2 +- paddle/fluid/framework/garbage_collector.cc | 2 +- paddle/fluid/framework/garbage_collector.h | 2 +- paddle/fluid/framework/ir/graph.h | 5 +- paddle/fluid/framework/ir/graph_helper.cc | 2 +- .../framework/ir/graph_to_program_pass.cc | 3 +- .../new_executor/executor_statistics.cc | 2 +- .../interpreter/dependency_builder.h | 2 +- .../interpreter/execution_config.cc | 2 +- .../new_executor/interpreter_base_impl.h | 14 +- .../framework/new_executor/interpretercore.h | 2 +- .../new_executor/new_ir_interpreter.cc | 2 +- paddle/fluid/framework/op_kernel_type.h | 4 +- paddle/fluid/framework/operator.cc | 6 +- paddle/fluid/framework/operator_test.cc | 2 +- .../framework/paddle2cinn/build_cinn_pass.cc | 6 +- .../framework/paddle2cinn/cinn_compiler.cc | 2 +- .../paddle2cinn/cinn_compiler_test.cc | 2 +- paddle/fluid/framework/scope.cc | 2 +- paddle/fluid/framework/unused_var_check.h | 2 +- paddle/fluid/imperative/prepared_operator.cc | 2 +- paddle/fluid/imperative/tracer.cc | 2 +- paddle/fluid/imperative/tracer.h | 2 +- paddle/fluid/inference/CMakeLists.txt | 4 + paddle/fluid/inference/analysis/analyzer.h | 2 +- .../ir_params_sync_among_devices_pass.cc | 2 +- paddle/fluid/inference/analysis/ut_helper.h | 4 +- .../fluid/inference/api/analysis_predictor.cc | 10 +- paddle/fluid/inference/api/api.cc | 12 +- paddle/fluid/inference/api/api_impl.cc | 3 +- .../api/demo_ci/onnxruntime_mobilenet_demo.cc | 2 +- .../api/demo_ci/simple_on_word2vec.cc | 2 +- .../api/demo_ci/trt_mobilenet_demo.cc | 2 +- .../fluid/inference/api/demo_ci/vis_demo.cc | 5 +- .../api/demo_ci/windows_mobilenet.cc | 2 +- .../api/onnxruntime_predictor_tester.cc | 2 +- paddle/fluid/inference/api/paddle_api.h | 2 +- .../inference/api/paddle_inference_api.h | 2 +- paddle/fluid/inference/io.cc | 12 +- paddle/fluid/inference/paddle_inference.map | 1 + .../inference/tensorrt/plugin/trt_plugin.h | 2 +- .../allocator_facade_abs_flags_test.cc | 2 +- .../allocator_facade_frac_flags_test.cc | 2 +- ...o_growth_best_fit_allocator_facade_test.cc | 2 +- .../auto_growth_best_fit_allocator_test.cc | 4 +- .../allocation/naive_best_fit_allocator.cc | 2 +- paddle/fluid/operators/pscore/CMakeLists.txt | 8 +- paddle/fluid/operators/tdm_child_op.h | 2 +- paddle/fluid/operators/tdm_sampler_op.h | 2 +- paddle/fluid/platform/cpu_info_test.cc | 2 +- .../platform/cuda_graph_with_memory_pool.cc | 2 +- paddle/fluid/platform/device/gpu/gpu_info.cc | 2 +- paddle/fluid/platform/device/xpu/xpu_info.cc | 2 +- .../fluid/platform/dynload/dynamic_loader.cc | 2 +- paddle/fluid/platform/enforce.h | 2 +- paddle/fluid/platform/flags.h | 2 +- paddle/fluid/platform/init.cc | 4 +- paddle/fluid/platform/init.h | 2 +- paddle/fluid/platform/profiler.cc | 4 +- paddle/fluid/platform/profiler/host_tracer.cc | 8 - paddle/fluid/platform/profiler/profiler.cc | 8 + paddle/fluid/platform/profiler/profiler.h | 2 +- paddle/fluid/pybind/CMakeLists.txt | 13 +- paddle/fluid/pybind/eager_method.cc | 2 +- .../pybind/global_value_getter_setter.cc | 6 +- paddle/fluid/pybind/reader_py.cc | 2 +- paddle/fluid/string/pretty_log.h | 2 +- paddle/phi/CMakeLists.txt | 2 +- paddle/phi/api/lib/api_gen_utils.cc | 4 +- paddle/phi/api/lib/data_transform.cc | 4 +- paddle/phi/api/profiler/device_tracer.cc | 4 +- paddle/phi/api/yaml/generator/api_gen.py | 6 +- .../api/yaml/generator/backward_api_gen.py | 6 +- .../phi/api/yaml/generator/dist_bw_api_gen.py | 6 +- .../yaml/generator/intermediate_api_gen.py | 4 +- .../phi/api/yaml/generator/sparse_api_gen.py | 4 +- .../api/yaml/generator/sparse_bw_api_gen.py | 4 +- .../phi/api/yaml/generator/strings_api_gen.py | 4 +- .../api/yaml/generator/tensor_operants_gen.py | 4 +- paddle/phi/backends/cpu/cpu_info.cc | 6 +- paddle/phi/backends/device_base.cc | 8 +- paddle/phi/backends/dynload/cudnn_frontend.h | 4 +- paddle/phi/backends/dynload/dynamic_loader.cc | 2 +- paddle/phi/backends/gpu/cuda/cudnn_helper.h | 4 +- paddle/phi/backends/gpu/gpu_info.cc | 4 +- paddle/phi/backends/gpu/rocm/miopen_helper.h | 4 +- paddle/phi/core/enforce.cc | 4 +- paddle/phi/core/flags.cc | 28 +- paddle/phi/core/flags.h | 21 +- paddle/phi/core/kernel_factory.cc | 8 +- paddle/phi/core/threadpool.cc | 10 +- paddle/phi/infermeta/unary.cc | 2 +- .../kernels/autotune/cache_cudnn_frontend.h | 2 +- .../phi/kernels/autotune/switch_autotune.cc | 4 +- paddle/phi/kernels/cpu/adam_kernel.cc | 2 +- paddle/phi/kernels/funcs/blas/blas_impl.cu.h | 2 +- paddle/phi/kernels/funcs/blas/blas_impl.hip.h | 4 +- .../phi/kernels/funcs/fused_gemm_epilogue.h | 4 +- paddle/phi/kernels/funcs/jit/benchmark.cc | 12 +- paddle/phi/kernels/funcs/jit/gen_base.h | 2 +- paddle/phi/kernels/funcs/jit/test.cc | 4 +- .../phi/kernels/gpu/batch_norm_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/batch_norm_kernel.cu | 2 +- .../kernels/gpu/c_embedding_grad_kernel.cu | 4 +- .../phi/kernels/gpu/embedding_grad_kernel.cu | 4 +- .../phi/kernels/gpu/flash_attn_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/flash_attn_kernel.cu | 2 +- paddle/phi/kernels/gpu/gelu_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/gelu_kernel.cu | 2 +- paddle/phi/kernels/gpu/index_add_kernel.cu | 4 +- .../kernels/gpu/index_select_grad_kernel.cu | 4 +- paddle/phi/kernels/gpu/layer_norm_kernel.cu | 4 +- paddle/phi/kernels/gpu/randperm_kernel.cu | 2 +- .../phi/kernels/gpu/uniform_inplace_kernel.cu | 2 +- paddle/phi/kernels/gpu/uniform_kernel.cu | 2 +- paddle/phi/kernels/gpudnn/conv_gpudnn_info.h | 6 +- paddle/phi/kernels/impl/conv_cudnn_impl.h | 6 +- paddle/phi/kernels/impl/einsum_impl.h | 4 +- .../phi/kernels/legacy/gpu/uniform_kernel.cu | 2 +- .../kernels/selected_rows/cpu/adam_kernel.cc | 4 +- paddle/testing/paddle_gtest_main.cc | 38 +- paddle/utils/CMakeLists.txt | 8 + paddle/utils/flags.h | 77 +++ paddle/utils/flags_native.cc | 484 ++++++++++++++++++ paddle/utils/flags_native.h | 131 +++++ paddle/utils/flags_native_test.cc | 104 ++++ paddle/utils/string/pretty_log.cc | 4 +- paddle/utils/string/pretty_log.h | 4 +- test/cpp/fluid/benchmark/op_tester.cc | 14 +- .../mkldnn/test_mkldnn_cpu_quantize_pass.cc | 2 +- test/cpp/fluid/pscore/CMakeLists.txt | 12 +- test/cpp/fluid/pscore/switch_server_test.cc | 10 +- .../api/analysis_predictor_tester.cc | 2 +- ...er_bfloat16_image_classification_tester.cc | 2 +- test/cpp/inference/api/analyzer_dam_tester.cc | 8 +- ...nalyzer_detect_functional_mkldnn_tester.cc | 4 +- .../inference/api/analyzer_detect_tester.cc | 4 +- .../analyzer_image_classification_tester.cc | 2 +- ...alyzer_int8_image_classification_tester.cc | 4 +- .../analyzer_int8_object_detection_tester.cc | 10 +- test/cpp/inference/api/analyzer_lac_tester.cc | 14 +- test/cpp/inference/api/analyzer_mmp_tester.cc | 16 +- ...lyzer_quant_image_classification_tester.cc | 2 +- .../cpp/inference/api/analyzer_rnn1_tester.cc | 2 +- .../api/analyzer_seq_pool1_tester_helper.h | 16 +- test/cpp/inference/api/analyzer_vis_tester.cc | 4 +- test/cpp/inference/api/api_impl_tester.cc | 62 +-- .../inference/api/ipu_multi_model_profile.cc | 2 +- .../inference/api/ipu_resnet50_fp16_test.cc | 2 +- test/cpp/inference/api/ipu_resnet50_test.cc | 2 +- test/cpp/inference/api/ipu_word2vec_sample.cc | 6 +- test/cpp/inference/api/lite_mul_model_test.cc | 2 +- test/cpp/inference/api/lite_resnet50_test.cc | 2 +- .../inference/api/mkldnn_quantizer_tester.cc | 2 +- .../paddle_infer_api_copy_tensor_tester.cc | 2 +- .../api/paddle_infer_api_errors_tester.cc | 2 +- .../inference/api/paddle_infer_api_test.cc | 2 +- test/cpp/inference/api/tester_helper.h | 138 ++--- .../inference/api/trt_cascade_rcnn_test.cc | 2 +- ...e_ernie_fp16_serialize_deserialize_test.cc | 2 +- ..._shape_ernie_serialize_deserialize_test.cc | 2 +- ...c_shape_ernie_serialize_deserialize_test.h | 2 +- .../api/trt_dynamic_shape_ernie_test.cc | 2 +- .../inference/api/trt_dynamic_shape_test.cc | 2 +- ...rt_dynamic_shape_transformer_prune_test.cc | 2 +- test/cpp/inference/api/trt_fc_prelu_test.cc | 2 +- .../api/trt_instance_norm_converter_test.cc | 2 +- .../api/trt_mark_trt_engine_outputs_test.cc | 1 - test/cpp/inference/api/trt_mobilenet_test.cc | 2 +- test/cpp/inference/api/trt_quant_int8_test.cc | 2 +- .../api/trt_quant_int8_yolov3_r50_test.cc | 2 +- .../inference/api/trt_rebind_stream_test.cc | 2 +- test/cpp/inference/api/trt_resnet50_test.cc | 2 +- test/cpp/inference/api/trt_resnext_test.cc | 2 +- .../inference/api/trt_split_converter_test.cc | 2 +- test/cpp/inference/api/trt_test_helper.h | 8 +- .../inference/api/xpu_config_resnet50_test.cc | 2 +- .../api/xpu_runtime_config_resnet50_test.cc | 2 +- test/cpp/inference/infer_ut/test_LeViT.cc | 2 +- .../cpp/inference/infer_ut/test_det_mv3_db.cc | 2 +- .../inference/infer_ut/test_ernie_text_cls.cc | 2 +- .../infer_ut/test_ernie_xnli_int8.cc | 2 +- .../cpp/inference/infer_ut/test_mobilnetv1.cc | 2 +- .../inference/infer_ut/test_ppyolo_mbv3.cc | 2 +- .../inference/infer_ut/test_ppyolov2_r50vd.cc | 2 +- test/cpp/inference/infer_ut/test_resnet50.cc | 2 +- .../inference/infer_ut/test_resnet50_quant.cc | 2 +- test/cpp/inference/infer_ut/test_yolov3.cc | 2 +- test/cpp/phi/api/scale_api.h | 2 +- test/cpp/prim/test_static_prim.cc | 2 +- 246 files changed, 1750 insertions(+), 956 deletions(-) create mode 100644 paddle/utils/flags.h create mode 100644 paddle/utils/flags_native.cc create mode 100644 paddle/utils/flags_native.h create mode 100644 paddle/utils/flags_native_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 34e748c794f69..318c9df489311 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,6 +254,7 @@ option(WITH_BRPC_RDMA "Use brpc rdma as the rpc protocal" OFF) option(ON_INFER "Turn on inference optimization and inference-lib generation" ON) option(WITH_CPP_DIST "Install PaddlePaddle C++ distribution" OFF) +option(WITH_GFLAGS "Compile PaddlePaddle with gflags support" OFF) ################################ Internal Configurations ####################################### option(WITH_NV_JETSON "Compile PaddlePaddle with NV JETSON" OFF) option(WITH_PROFILER "Compile PaddlePaddle with GPU profiler and gperftools" diff --git a/cmake/cinn.cmake b/cmake/cinn.cmake index de13f71526c8a..ca25a7d5d30a5 100644 --- a/cmake/cinn.cmake +++ b/cmake/cinn.cmake @@ -183,6 +183,11 @@ if(WITH_MKL) endif() endif() +if(NOT WITH_GFLAGS) + target_link_libraries(cinnapi gflags) + add_dependencies(cinnapi gflags) +endif() + if(WITH_GPU) target_link_libraries( cinnapi @@ -237,6 +242,11 @@ function(gen_cinncore LINKTYPE) endif() endif() + if(NOT WITH_GFLAGS) + target_link_libraries(${CINNCORE_TARGET} gflags) + add_dependencies(${CINNCORE_TARGET} gflags) + endif() + if(WITH_GPU) target_link_libraries( ${CINNCORE_TARGET} diff --git a/cmake/configure.cmake b/cmake/configure.cmake index dc661fce388fe..4d0b04209c059 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -201,6 +201,10 @@ if(WITH_DISTRIBUTE) add_definitions(-DPADDLE_WITH_DISTRIBUTE) endif() +if(WITH_GFLAGS) + add_definitions(-DPADDLE_WITH_GFLAGS) +endif() + if(WITH_PSCORE) add_definitions(-DPADDLE_WITH_PSCORE) endif() diff --git a/cmake/external/brpc.cmake b/cmake/external/brpc.cmake index ff33e142addbd..3c9f2b6962048 100755 --- a/cmake/external/brpc.cmake +++ b/cmake/external/brpc.cmake @@ -91,3 +91,16 @@ add_dependencies(brpc extern_brpc) add_definitions(-DBRPC_WITH_GLOG) list(APPEND external_project_dependencies brpc) + +set(EXTERNAL_BRPC_DEPS + brpc + protobuf + ssl + crypto + leveldb + glog + snappy) + +if(NOT WITH_GFLAGS) + set(EXTERNAL_BRPC_DEPS ${EXTERNAL_BRPC_DEPS} gflags) +endif() diff --git a/cmake/external/gflags.cmake b/cmake/external/gflags.cmake index 06827732e5966..75436783c7ede 100755 --- a/cmake/external/gflags.cmake +++ b/cmake/external/gflags.cmake @@ -102,3 +102,14 @@ if(WIN32) set_property(GLOBAL PROPERTY OS_DEPENDENCY_MODULES shlwapi.lib) endif() endif() + +# We have implemented a custom flags tool paddle_flags to replace gflags. +# User can also choose to use gflags by setting WITH_GFLAGS=ON. But when +# using paddle_flags, gflags is also needed for other third party libraries +# including glog and brpc. So we can not remove gflags completely. +set(flags_dep) +if(WITH_GFLAGS) + list(APPEND flags_dep gflags) +else() + list(APPEND flags_dep paddle_flags) +endif() diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake index c97a68a0175e1..13fce9613650f 100755 --- a/cmake/inference_lib.cmake +++ b/cmake/inference_lib.cmake @@ -336,11 +336,22 @@ copy( inference_lib_dist SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/flat_hash_map.h DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/paddle/utils/) +copy( + inference_lib_dist + SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/flags.h + DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/paddle/utils/) copy( inference_lib_dist SRCS ${PADDLE_SOURCE_DIR}/paddle/extension.h DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/paddle/) +if(NOT WITH_GFLAGS) + copy( + inference_lib_dist + SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/flags_native.h + DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/paddle/utils/) +endif() + # the include path of phi needs to be changed to adapt to inference api path add_custom_command( TARGET inference_lib_dist diff --git a/paddle/fluid/distributed/collective/process_group_custom.cc b/paddle/fluid/distributed/collective/process_group_custom.cc index 6d5c30da3133b..a905fcc095599 100644 --- a/paddle/fluid/distributed/collective/process_group_custom.cc +++ b/paddle/fluid/distributed/collective/process_group_custom.cc @@ -27,7 +27,7 @@ constexpr int64_t kWaitBlockTImeout = 10; -DECLARE_bool(use_stream_safe_cuda_allocator); +PD_DECLARE_bool(use_stream_safe_cuda_allocator); namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/collective/process_group_nccl.cc b/paddle/fluid/distributed/collective/process_group_nccl.cc index a8539278d2f28..7ffe00b8cd824 100644 --- a/paddle/fluid/distributed/collective/process_group_nccl.cc +++ b/paddle/fluid/distributed/collective/process_group_nccl.cc @@ -29,7 +29,7 @@ #include "paddle/phi/core/distributed/comm_context_manager.h" PHI_DECLARE_bool(nccl_blocking_wait); -DECLARE_bool(use_stream_safe_cuda_allocator); +PD_DECLARE_bool(use_stream_safe_cuda_allocator); // set this flag to `true` and recompile to enable dynamic checks constexpr bool FLAGS_enable_nccl_dynamic_check = false; diff --git a/paddle/fluid/distributed/collective/reducer.cc b/paddle/fluid/distributed/collective/reducer.cc index c3262b8db8d56..378668499c90b 100644 --- a/paddle/fluid/distributed/collective/reducer.cc +++ b/paddle/fluid/distributed/collective/reducer.cc @@ -18,7 +18,7 @@ #include "paddle/phi/backends/device_manager.h" #include "paddle/phi/core/flags.h" -DECLARE_bool(use_stream_safe_cuda_allocator); +PD_DECLARE_bool(use_stream_safe_cuda_allocator); PHI_DECLARE_string(allocator_strategy); namespace paddle { diff --git a/paddle/fluid/distributed/fleet_executor/CMakeLists.txt b/paddle/fluid/distributed/fleet_executor/CMakeLists.txt index eea257b5ddf4f..9c28205520129 100755 --- a/paddle/fluid/distributed/fleet_executor/CMakeLists.txt +++ b/paddle/fluid/distributed/fleet_executor/CMakeLists.txt @@ -7,16 +7,7 @@ proto_library(interceptor_message_proto SRCS interceptor_message.proto) if(WITH_ARM_BRPC) set(BRPC_DEPS arm_brpc snappy phi glog) elseif(WITH_DISTRIBUTE AND NOT WITH_PSLIB) - set(BRPC_DEPS - brpc - ssl - crypto - protobuf - zlib - leveldb - snappy - phi - glog) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS} zlib phi) else() set(BRPC_DEPS "") endif() diff --git a/paddle/fluid/distributed/fleet_executor/carrier.cc b/paddle/fluid/distributed/fleet_executor/carrier.cc index 1dc29493af9d8..bb128c1287c9c 100644 --- a/paddle/fluid/distributed/fleet_executor/carrier.cc +++ b/paddle/fluid/distributed/fleet_executor/carrier.cc @@ -17,7 +17,6 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/distributed/fleet_executor/global.h" #include "paddle/fluid/distributed/fleet_executor/interceptor.h" #include "paddle/fluid/distributed/fleet_executor/message_bus.h" @@ -29,6 +28,7 @@ #include "paddle/fluid/framework/variable.h" #include "paddle/fluid/framework/variable_helper.h" #include "paddle/fluid/platform/flags.h" +#include "paddle/utils/flags.h" PADDLE_DEFINE_EXPORTED_bool( fleet_executor_with_standalone, false, diff --git a/paddle/fluid/distributed/ps/service/CMakeLists.txt b/paddle/fluid/distributed/ps/service/CMakeLists.txt index 585dd111bf7d1..c23f26c635218 100755 --- a/paddle/fluid/distributed/ps/service/CMakeLists.txt +++ b/paddle/fluid/distributed/ps/service/CMakeLists.txt @@ -3,34 +3,11 @@ set_source_files_properties(${BRPC_SRCS}) if(WITH_HETERPS) - set(BRPC_DEPS - brpc - ssl - crypto - protobuf - phi - glog - zlib - leveldb - snappy - glog - device_context - rocksdb) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS} phi zlib device_context rocksdb) else() - set(BRPC_DEPS - brpc - ssl - crypto - protobuf - phi - glog - zlib - leveldb - snappy - glog - device_context) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS} phi zlib device_context) endif() diff --git a/paddle/fluid/distributed/ps/service/brpc_ps_client.cc b/paddle/fluid/distributed/ps/service/brpc_ps_client.cc index 93fe8c849be69..9ad8768e0927d 100644 --- a/paddle/fluid/distributed/ps/service/brpc_ps_client.cc +++ b/paddle/fluid/distributed/ps/service/brpc_ps_client.cc @@ -34,49 +34,53 @@ class Variable; namespace paddle { namespace distributed { -DEFINE_int32(pserver_push_dense_merge_limit, - 12, - "limit max push_dense local merge requests"); +PD_DEFINE_int32(pserver_push_dense_merge_limit, + 12, + "limit max push_dense local merge requests"); -DEFINE_int32(pserver_push_sparse_merge_limit, - 12, - "limit max push_sparse local merge requests"); +PD_DEFINE_int32(pserver_push_sparse_merge_limit, + 12, + "limit max push_sparse local merge requests"); -DEFINE_int32(pserver_pull_dense_limit, - 12, - "limit max push_sparse local merge requests"); +PD_DEFINE_int32(pserver_pull_dense_limit, + 12, + "limit max push_sparse local merge requests"); -DEFINE_int32(pserver_async_push_dense_interval_ms, - 10, - "async push_dense to server interval"); +PD_DEFINE_int32(pserver_async_push_dense_interval_ms, + 10, + "async push_dense to server interval"); -DEFINE_int32(pserver_async_push_sparse_interval_ms, - 10, - "async push_sparse to server interval"); +PD_DEFINE_int32(pserver_async_push_sparse_interval_ms, + 10, + "async push_sparse to server interval"); -DEFINE_bool(pserver_scale_gradient_by_merge, - false, - "scale dense gradient when merged"); +PD_DEFINE_bool(pserver_scale_gradient_by_merge, + false, + "scale dense gradient when merged"); -DEFINE_int32(pserver_communicate_compress_type, - 0, - "none:0 snappy:1 gzip:2 zlib:3 lz4:4"); +PD_DEFINE_int32(pserver_communicate_compress_type, + 0, + "none:0 snappy:1 gzip:2 zlib:3 lz4:4"); -DEFINE_int32(pserver_max_async_call_num, - 13, - "max task num in async_call_server"); +PD_DEFINE_int32(pserver_max_async_call_num, + 13, + "max task num in async_call_server"); -DEFINE_int32(pserver_timeout_ms, 500000, "pserver request server timeout_ms"); +PD_DEFINE_int32(pserver_timeout_ms, + 500000, + "pserver request server timeout_ms"); -DEFINE_int32(pserver_connect_timeout_ms, - 10000, - "pserver connect server timeout_ms"); +PD_DEFINE_int32(pserver_connect_timeout_ms, + 10000, + "pserver connect server timeout_ms"); -DEFINE_int32(pserver_sparse_merge_thread, 1, "pserver sparse merge thread num"); +PD_DEFINE_int32(pserver_sparse_merge_thread, + 1, + "pserver sparse merge thread num"); -DEFINE_int32(pserver_sparse_table_shard_num, - 1000, - "sparse table shard for save & load"); +PD_DEFINE_int32(pserver_sparse_table_shard_num, + 1000, + "sparse table shard for save & load"); inline size_t get_sparse_shard(uint32_t shard_num, uint32_t server_num, @@ -140,7 +144,7 @@ int32_t BrpcPsClient::StartFlClientService(const std::string &self_endpoint) { if (_fl_server.Start(self_endpoint.c_str(), &options) != 0) { VLOG(0) << "fl-ps > StartFlClientService failed. Try again."; - auto ip_port = paddle::string::Split(self_endpoint, ':'); + auto ip_port = ::paddle::string::Split(self_endpoint, ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -202,8 +206,7 @@ int32_t BrpcPsClient::InitializeFlWorker(const std::string &self_endpoint) { options.protocol = "baidu_std"; options.timeout_ms = FLAGS_pserver_timeout_ms; options.connection_type = "pooled"; - options.connect_timeout_ms = - paddle::distributed::FLAGS_pserver_connect_timeout_ms; + options.connect_timeout_ms = FLAGS_pserver_connect_timeout_ms; options.max_retry = 3; // 获取 coordinator 列表,并连接 std::string coordinator_ip_port; @@ -336,11 +339,11 @@ int32_t BrpcPsClient::Initialize() { auto table_id = worker_param.downpour_table_param(i).table_id(); if (type == PS_DENSE_TABLE) { _push_dense_task_queue_map[table_id] = - paddle::framework::MakeChannel(); + ::paddle::framework::MakeChannel(); } if (type == PS_SPARSE_TABLE) { _push_sparse_task_queue_map[table_id] = - paddle::framework::MakeChannel(); + ::paddle::framework::MakeChannel(); _push_sparse_merge_count_map[table_id] = 0; } } @@ -446,7 +449,7 @@ std::future BrpcPsClient::PrintTableStat(uint32_t table_id) { int ret = 0; uint64_t feasign_size = 0; uint64_t mf_size = 0; - paddle::framework::BinaryArchive ar; + ::paddle::framework::BinaryArchive ar; auto *closure = reinterpret_cast(done); for (size_t i = 0; i < request_call_num; ++i) { if (closure->check_response(i, PS_PRINT_TABLE_STAT) != 0) { diff --git a/paddle/fluid/distributed/ps/service/brpc_ps_server.cc b/paddle/fluid/distributed/ps/service/brpc_ps_server.cc index 84784c32f3b2d..28ac123fa08ff 100644 --- a/paddle/fluid/distributed/ps/service/brpc_ps_server.cc +++ b/paddle/fluid/distributed/ps/service/brpc_ps_server.cc @@ -30,15 +30,15 @@ class RpcController; } // namespace protobuf } // namespace google -DEFINE_int32(pserver_timeout_ms_s2s, - 10000, - "pserver request server timeout_ms"); -DEFINE_int32(pserver_connect_timeout_ms_s2s, - 10000, - "pserver connect server timeout_ms"); -DEFINE_string(pserver_connection_type_s2s, - "pooled", - "pserver connection_type[pooled:single]"); +PD_DEFINE_int32(pserver_timeout_ms_s2s, + 10000, + "pserver request server timeout_ms"); +PD_DEFINE_int32(pserver_connect_timeout_ms_s2s, + 10000, + "pserver connect server timeout_ms"); +PD_DEFINE_string(pserver_connection_type_s2s, + "pooled", + "pserver connection_type[pooled:single]"); namespace paddle { namespace distributed { @@ -169,7 +169,7 @@ int32_t BrpcPsServer::ReceiveFromPServer(int msg_type, LOG(WARNING) << "SERVER>>RESPONSE>>msg = 0 Finish S2S Response"; return 0; } - paddle::framework::BinaryArchive ar; + ::paddle::framework::BinaryArchive ar; ar.SetReadBuffer(const_cast(msg.c_str()), msg.length(), nullptr); if (ar.Cursor() == ar.Finish()) { LOG(WARNING) << "SERVER>>RESPONSE ar = 0>> Finish S2S Response"; @@ -598,7 +598,7 @@ int32_t BrpcPsService::PrintTableStat(Table *table, brpc::Controller *cntl) { CHECK_TABLE_EXIST(table, request, response) std::pair ret = table->PrintTableStat(); - paddle::framework::BinaryArchive ar; + ::paddle::framework::BinaryArchive ar; ar << ret.first << ret.second; std::string table_info(ar.Buffer(), ar.Length()); response.set_data(table_info); @@ -723,7 +723,7 @@ int32_t BrpcPsService::CacheShuffle(Table *table, table->Flush(); double cache_threshold = std::stod(request.params(2)); LOG(INFO) << "cache threshold for cache shuffle: " << cache_threshold; - // auto shuffled_ins = paddle::ps::make_channel>(); // shuffled_ins->set_block_size(80000); _server->StartS2S(); diff --git a/paddle/fluid/distributed/ps/service/communicator/communicator.cc b/paddle/fluid/distributed/ps/service/communicator/communicator.cc index 1ad58d9eb0fca..9932343fa779b 100644 --- a/paddle/fluid/distributed/ps/service/communicator/communicator.cc +++ b/paddle/fluid/distributed/ps/service/communicator/communicator.cc @@ -16,11 +16,11 @@ limitations under the License. */ #include -#include "gflags/gflags.h" #include "paddle/fluid/distributed/ps/service/brpc_ps_client.h" #include "paddle/fluid/distributed/ps/wrapper/fleet.h" #include "paddle/fluid/platform/profiler.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/utils/flags.h" #define LEARNING_RATE_DECAY_COUNTER "@LR_DECAY_COUNTER@" #define STEP_COUNTER "@PS_STEP_COUNTER@" @@ -42,7 +42,7 @@ Communicator::Communicator() = default; void Communicator::InitGFlag(const std::string &gflags) { VLOG(3) << "Init With Gflags:" << gflags; - std::vector flags = paddle::string::split_string(gflags); + std::vector flags = ::paddle::string::split_string(gflags); if (flags.empty()) { flags.push_back("-max_body_size=314217728"); flags.push_back("-bthread_concurrency=40"); @@ -57,7 +57,7 @@ void Communicator::InitGFlag(const std::string &gflags) { } int params_cnt = flags.size(); char **params_ptr = &(flags_ptr[0]); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr, true); + ::paddle::flags::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr); } std::once_flag Communicator::init_flag_; @@ -66,7 +66,7 @@ std::shared_ptr Communicator::communicator_(nullptr); void Communicator::InitBrpcClient( const std::string &dist_desc, const std::vector &host_sign_list) { - auto fleet = paddle::distributed::FleetWrapper::GetInstance(); + auto fleet = ::paddle::distributed::FleetWrapper::GetInstance(); if (_worker_ptr.get() == nullptr) { _worker_ptr = fleet->worker_ptr_; } @@ -92,7 +92,7 @@ void Communicator::RpcRecvDense(const std::vector &varnames, platform::RecordEvent record_event("Communicator->RpcRecvDense", platform::TracerEventType::Communication, 1); - std::vector regions; + std::vector<::paddle::distributed::Region> regions; regions.reserve(varnames.size()); for (auto &t : varnames) { Variable *var = scope->Var(t); @@ -103,7 +103,7 @@ void Communicator::RpcRecvDense(const std::vector &varnames, phi::DenseTensor *temp_tensor = temp_var->GetMutable(); temp_tensor->Resize(tensor->dims()); float *temp_data = temp_tensor->mutable_data(platform::CPUPlace()); - paddle::distributed::Region reg(temp_data, tensor->numel()); + ::paddle::distributed::Region reg(temp_data, tensor->numel()); regions.emplace_back(std::move(reg)); VLOG(1) << "Communicator::RpcRecvDense Var " << t << " table_id " << table_id << " Temp_data[0] " << temp_data[0] @@ -111,7 +111,7 @@ void Communicator::RpcRecvDense(const std::vector &varnames, #endif } else { float *w = tensor->mutable_data(tensor->place()); - paddle::distributed::Region reg(w, tensor->numel()); + ::paddle::distributed::Region reg(w, tensor->numel()); regions.emplace_back(std::move(reg)); } } @@ -152,7 +152,7 @@ void Communicator::RpcSendDenseParam(const std::vector &varnames, platform::TracerEventType::Communication, 1); auto place = platform::CPUPlace(); - std::vector regions; + std::vector<::paddle::distributed::Region> regions; for (auto &t : varnames) { Variable *var = scope.FindVar(t); CHECK(var != nullptr) << "var[" << t << "] not found"; @@ -164,7 +164,7 @@ void Communicator::RpcSendDenseParam(const std::vector &varnames, temp_tensor->Resize(tensor->dims()); float *temp_data = temp_tensor->mutable_data(platform::CPUPlace()); framework::TensorCopy(*tensor, platform::CPUPlace(), temp_tensor); - paddle::distributed::Region reg(temp_data, tensor->numel()); + ::paddle::distributed::Region reg(temp_data, tensor->numel()); regions.emplace_back(std::move(reg)); VLOG(1) << "rpc_send_dense_param Var " << t << " table_id " << table_id << " Temp_data[0] " << temp_data[0] << " Temp_data[-1] " @@ -172,7 +172,7 @@ void Communicator::RpcSendDenseParam(const std::vector &varnames, #endif } else { float *w = tensor->mutable_data(place); - paddle::distributed::Region reg(w, tensor->numel()); + ::paddle::distributed::Region reg(w, tensor->numel()); regions.emplace_back(reg); VLOG(1) << "rpc_send_dense_param Var " << t << " table_id " << table_id << " Temp_data[0] " << w[0] << " Temp_data[-1] " @@ -1096,10 +1096,10 @@ void GeoCommunicator::InitImpl(const RpcCtxMap &send_varname_to_ctx, parallel_task_nums_ += 1; sparse_id_queues_.insert( std::pair>>>( splited_var, - paddle::framework::MakeChannel< + ::paddle::framework::MakeChannel< std::shared_ptr>>(send_queue_size_))); } } @@ -1509,7 +1509,7 @@ void GeoCommunicator::MainThread() { void FLCommunicator::InitBrpcClient( const std::string &dist_desc, const std::vector &host_sign_list) { - auto fleet = paddle::distributed::FleetWrapper::GetInstance(); + auto fleet = ::paddle::distributed::FleetWrapper::GetInstance(); if (_worker_ptr.get() == nullptr) { VLOG(0) << "fl-ps > FLCommunicator::InitBrpcClient get _worker_ptr"; _worker_ptr = diff --git a/paddle/fluid/distributed/ps/service/communicator/communicator.h b/paddle/fluid/distributed/ps/service/communicator/communicator.h index 643c91b5b05e9..f6d062460b826 100644 --- a/paddle/fluid/distributed/ps/service/communicator/communicator.h +++ b/paddle/fluid/distributed/ps/service/communicator/communicator.h @@ -29,7 +29,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/distributed/ps/service/communicator/communicator_common.h" #include "paddle/fluid/distributed/ps/service/coordinator_client.h" #include "paddle/fluid/distributed/ps/service/ps_client.h" @@ -45,6 +44,7 @@ limitations under the License. */ #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/selected_rows_functor.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { @@ -410,8 +410,8 @@ class Communicator { } void InitGFlag(const std::string &gflags); - paddle::distributed::PSParameter _ps_param; - paddle::distributed::PaddlePSEnvironment _ps_env; + ::paddle::distributed::PSParameter _ps_param; + ::paddle::distributed::PaddlePSEnvironment _ps_env; int servers_ = 0; int trainers_; int trainer_id_ = 0; @@ -661,7 +661,7 @@ class GeoCommunicator : public AsyncCommunicator { std::unordered_map< std::string, - paddle::framework::Channel>>> + ::paddle::framework::Channel>>> sparse_id_queues_; }; diff --git a/paddle/fluid/distributed/ps/service/coordinator_client.cc b/paddle/fluid/distributed/ps/service/coordinator_client.cc index 9a77170b37c53..c9c2ba49c9bf3 100644 --- a/paddle/fluid/distributed/ps/service/coordinator_client.cc +++ b/paddle/fluid/distributed/ps/service/coordinator_client.cc @@ -28,8 +28,8 @@ static const int MAX_PORT = 65535; namespace paddle { namespace distributed { -DEFINE_uint64(total_fl_client_size, 100, "supported total fl client size"); -DEFINE_uint32(coordinator_wait_all_clients_max_time, 60, "uint32: s"); +PD_DEFINE_uint64(total_fl_client_size, 100, "supported total fl client size"); +PD_DEFINE_uint32(coordinator_wait_all_clients_max_time, 60, "uint32: s"); void CoordinatorService::FLService( ::google::protobuf::RpcController* controller, @@ -62,10 +62,10 @@ int32_t CoordinatorClient::Initialize( const std::vector& trainer_endpoints) { brpc::ChannelOptions options; options.protocol = "baidu_std"; - options.timeout_ms = paddle::distributed::FLAGS_pserver_timeout_ms; + options.timeout_ms = ::paddle::distributed::FLAGS_pserver_timeout_ms; options.connection_type = "pooled"; options.connect_timeout_ms = - paddle::distributed::FLAGS_pserver_connect_timeout_ms; + ::paddle::distributed::FLAGS_pserver_connect_timeout_ms; options.max_retry = 3; std::string server_ip_port; @@ -109,7 +109,7 @@ int32_t CoordinatorClient::Initialize( } for (size_t i = 0; i < trainer_endpoints.size(); i++) { std::vector addr = - paddle::string::Split(trainer_endpoints[i], ':'); + ::paddle::string::Split(trainer_endpoints[i], ':'); fl_client_list[i].ip = addr[0]; fl_client_list[i].port = std::stol(addr[1]); fl_client_list[i].rank = i; // TO CHECK @@ -152,7 +152,7 @@ int32_t CoordinatorClient::StartClientService() { LOG(ERROR) << "fl-ps > coordinator server endpoint not set"; return -1; } - auto addr = paddle::string::Split(_endpoint, ':'); + auto addr = ::paddle::string::Split(_endpoint, ':'); std::string ip = addr[0]; std::string port = addr[1]; std::string rank = addr[2]; diff --git a/paddle/fluid/distributed/ps/service/coordinator_client.h b/paddle/fluid/distributed/ps/service/coordinator_client.h index bd1f0f7754d8c..8db08c3fc7999 100644 --- a/paddle/fluid/distributed/ps/service/coordinator_client.h +++ b/paddle/fluid/distributed/ps/service/coordinator_client.h @@ -34,10 +34,10 @@ namespace paddle { namespace distributed { -DECLARE_int32(pserver_timeout_ms); -DECLARE_int32(pserver_connect_timeout_ms); -DECLARE_uint64(total_fl_client_size); -DECLARE_uint32(coordinator_wait_all_clients_max_time); +PD_DECLARE_int32(pserver_timeout_ms); +PD_DECLARE_int32(pserver_connect_timeout_ms); +PD_DECLARE_uint64(total_fl_client_size); +PD_DECLARE_uint32(coordinator_wait_all_clients_max_time); using CoordinatorServiceFunc = std::function bool { while (query_wait_time < - paddle::distributed:: - FLAGS_coordinator_wait_all_clients_max_time) { // in case that - // some - // clients down + FLAGS_coordinator_wait_all_clients_max_time) { // in case that + // some + // clients down if (_is_all_clients_info_collected == true) { // LOG(INFO) << "fl-ps > _is_all_clients_info_collected"; return true; diff --git a/paddle/fluid/distributed/ps/service/env.h b/paddle/fluid/distributed/ps/service/env.h index aa230f86c9d17..d6b403523496c 100644 --- a/paddle/fluid/distributed/ps/service/env.h +++ b/paddle/fluid/distributed/ps/service/env.h @@ -25,8 +25,8 @@ #include #include -#include "gflags/gflags.h" #include "paddle/phi/core/macros.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/service/heter_client.cc b/paddle/fluid/distributed/ps/service/heter_client.cc index 2ca9fef5c0876..d7a1f5cf7c4f1 100644 --- a/paddle/fluid/distributed/ps/service/heter_client.cc +++ b/paddle/fluid/distributed/ps/service/heter_client.cc @@ -19,8 +19,8 @@ namespace paddle { namespace distributed { -DEFINE_int32(heter_world_size, 100, "group size"); // group max size -DEFINE_int32(switch_send_recv_timeout_s, 600, "switch_send_recv_timeout_s"); +PD_DEFINE_int32(heter_world_size, 100, "group size"); // group max size +PD_DEFINE_int32(switch_send_recv_timeout_s, 600, "switch_send_recv_timeout_s"); std::shared_ptr HeterClient::s_instance_ = nullptr; std::mutex HeterClient::mtx_; @@ -85,7 +85,7 @@ void HeterClient::CreateClient2XpuConnection() { xpu_channels_[i].reset(new brpc::Channel()); if (xpu_channels_[i]->Init(xpu_list_[i].c_str(), "", &options) != 0) { VLOG(0) << "HeterClient channel init fail. Try Again"; - auto ip_port = paddle::string::Split(xpu_list_[i], ':'); + auto ip_port = ::paddle::string::Split(xpu_list_[i], ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -100,7 +100,7 @@ void HeterClient::CreateClient2XpuConnection() { if (previous_xpu_channels_[i]->Init( previous_xpu_list_[i].c_str(), "", &options) != 0) { VLOG(0) << "HeterClient channel init fail. Try Again"; - auto ip_port = paddle::string::Split(previous_xpu_list_[i], ':'); + auto ip_port = ::paddle::string::Split(previous_xpu_list_[i], ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -181,11 +181,11 @@ void HeterClient::SendAndRecvAsync( std::future HeterClient::SendCmd( uint32_t table_id, int cmd_id, const std::vector& params) { size_t request_call_num = xpu_channels_.size(); - paddle::distributed::DownpourBrpcClosure* closure = - new paddle::distributed::DownpourBrpcClosure( + ::paddle::distributed::DownpourBrpcClosure* closure = + new ::paddle::distributed::DownpourBrpcClosure( request_call_num, [request_call_num, cmd_id](void* done) { int ret = 0; - auto* closure = (paddle::distributed::DownpourBrpcClosure*)done; + auto* closure = (::paddle::distributed::DownpourBrpcClosure*)done; for (size_t i = 0; i < request_call_num; ++i) { if (closure->check_response(i, cmd_id) != 0) { ret = -1; diff --git a/paddle/fluid/distributed/ps/service/heter_client.h b/paddle/fluid/distributed/ps/service/heter_client.h index 10d916b710011..e6c231338ac52 100755 --- a/paddle/fluid/distributed/ps/service/heter_client.h +++ b/paddle/fluid/distributed/ps/service/heter_client.h @@ -42,7 +42,7 @@ class Scope; namespace paddle { namespace distributed { -DECLARE_int32(pserver_timeout_ms); +PD_DECLARE_int32(pserver_timeout_ms); using MultiVarMsg = ::paddle::distributed::MultiVariableMessage; using VarMsg = ::paddle::distributed::VariableMessage; @@ -116,7 +116,7 @@ class HeterClient { if ((*client_channels)[i]->Init(node_list[i].c_str(), "", &options) != 0) { VLOG(0) << "client channel init failed! try again"; - auto ip_port = paddle::string::Split(node_list[i], ':'); + auto ip_port = ::paddle::string::Split(node_list[i], ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); diff --git a/paddle/fluid/distributed/ps/service/heter_server.cc b/paddle/fluid/distributed/ps/service/heter_server.cc index d5d8546bf792e..eb4d9b8304513 100644 --- a/paddle/fluid/distributed/ps/service/heter_server.cc +++ b/paddle/fluid/distributed/ps/service/heter_server.cc @@ -18,8 +18,8 @@ namespace paddle { namespace distributed { -// DEFINE_string(cert_path, "./cert.pem", "cert.pem path"); -// DEFINE_string(key_path, "./key.pem", "key.pem path"); +// PD_DEFINE_string(cert_path, "./cert.pem", "cert.pem path"); +// PD_DEFINE_string(key_path, "./key.pem", "key.pem path"); std::shared_ptr HeterServer::s_instance_ = nullptr; std::mutex HeterServer::mtx_; @@ -37,7 +37,7 @@ void HeterServer::StartHeterService(bool neeed_encrypt) { } if (server_.Start(endpoint_.c_str(), &options) != 0) { VLOG(0) << "HeterServer start fail. Try again."; - auto ip_port = paddle::string::Split(endpoint_, ':'); + auto ip_port = ::paddle::string::Split(endpoint_, ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -72,7 +72,7 @@ void HeterServer::StartHeterInterService(bool neeed_encrypt) { } if (server_inter_.Start(endpoint_inter_.c_str(), &options) != 0) { VLOG(4) << "switch inter server start fail. Try again."; - auto ip_port = paddle::string::Split(endpoint_inter_, ':'); + auto ip_port = ::paddle::string::Split(endpoint_inter_, ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); diff --git a/paddle/fluid/distributed/ps/service/heter_server.h b/paddle/fluid/distributed/ps/service/heter_server.h index c4e9d05ac1352..1f134d4db18ae 100755 --- a/paddle/fluid/distributed/ps/service/heter_server.h +++ b/paddle/fluid/distributed/ps/service/heter_server.h @@ -57,9 +57,9 @@ PHI_DECLARE_double(eager_delete_tensor_gb); namespace paddle { namespace distributed { -DECLARE_int32(pserver_timeout_ms); -DECLARE_int32(heter_world_size); -DECLARE_int32(switch_send_recv_timeout_s); +PD_DECLARE_int32(pserver_timeout_ms); +PD_DECLARE_int32(heter_world_size); +PD_DECLARE_int32(switch_send_recv_timeout_s); using MultiVarMsg = MultiVariableMessage; using VarMsg = VariableMessage; @@ -216,8 +216,8 @@ class SendAndRecvVariableHandler final : public ServiceHandlerBase { // get microID from request // deserialize variable to micro scope // Push to heter worker's task_queue - std::unique_ptr local_scope_ptr( - new paddle::framework::Scope()); + std::unique_ptr<::paddle::framework::Scope> local_scope_ptr( + new ::paddle::framework::Scope()); auto& local_scope = *(local_scope_ptr.get()); platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance(); platform::CPUPlace cpu_place; @@ -257,7 +257,7 @@ class SendAndRecvVariableHandler final : public ServiceHandlerBase { auto* minibatch_scope = &(scope_->NewScope()); (*mini_scopes_)[minibatch_index] = minibatch_scope; (*micro_scopes_)[minibatch_index].reset( - new std::vector{}); + new std::vector<::paddle::framework::Scope*>{}); for (int i = 0; i < num_microbatch_; i++) { auto* micro_scope = &(minibatch_scope->NewScope()); (*((*micro_scopes_)[minibatch_index])).push_back(micro_scope); @@ -300,7 +300,7 @@ class SendAndRecvVariableHandler final : public ServiceHandlerBase { public: using shard_type = SparseTableShard; - std::shared_ptr local_scope_ptr; // for switch + std::shared_ptr<::paddle::framework::Scope> local_scope_ptr; // for switch std::unordered_map> vars_ready_flag; std::unique_ptr _local_shards; @@ -344,7 +344,7 @@ class HeterService : public PsService { std::placeholders::_3); service_handler_.local_scope_ptr = - std::make_shared(); + std::make_shared<::paddle::framework::Scope>(); } virtual ~HeterService() {} @@ -613,7 +613,7 @@ class HeterServer { void SetLocalScope() { request_handler_->local_scope_ptr = - std::make_shared(); + std::make_shared<::paddle::framework::Scope>(); } void SetInterEndpoint(const std::string& endpoint) { diff --git a/paddle/fluid/distributed/ps/service/ps_client.cc b/paddle/fluid/distributed/ps/service/ps_client.cc index 3dd2b8c775cbe..ecdcd8b874377 100644 --- a/paddle/fluid/distributed/ps/service/ps_client.cc +++ b/paddle/fluid/distributed/ps/service/ps_client.cc @@ -37,7 +37,8 @@ REGISTER_PSCORE_CLASS(PSClient, PsGraphClient); int32_t PSClient::Configure( // called in FleetWrapper::InitWorker const PSParameter &config, - const std::map> ®ions, + const std::map> + ®ions, PSEnvironment &env, size_t client_id) { _env = &env; @@ -88,7 +89,7 @@ PSClient *PSClientFactory::Create(const PSParameter &ps_config) { PSClient *client = NULL; #if defined(PADDLE_WITH_GLOO) && defined(PADDLE_WITH_GPU_GRAPH) - auto gloo = paddle::framework::GlooWrapper::GetInstance(); + auto gloo = ::paddle::framework::GlooWrapper::GetInstance(); if (client_name == "PsLocalClient" && gloo->Size() > 1) { client = CREATE_PSCORE_CLASS(PSClient, "PsGraphClient"); LOG(WARNING) << "change PsLocalClient to PsGraphClient"; diff --git a/paddle/fluid/distributed/ps/service/ps_service/graph_py_service.h b/paddle/fluid/distributed/ps/service/ps_service/graph_py_service.h index 33490281981d5..44836e7661b5f 100644 --- a/paddle/fluid/distributed/ps/service/ps_service/graph_py_service.h +++ b/paddle/fluid/distributed/ps/service/ps_service/graph_py_service.h @@ -143,13 +143,13 @@ class GraphPyServer : public GraphPyService { void start_server(bool block = true); ::paddle::distributed::PSParameter GetServerProto(); - std::shared_ptr get_ps_server() { + std::shared_ptr<::paddle::distributed::GraphBrpcServer> get_ps_server() { return pserver_ptr; } protected: int rank; - std::shared_ptr pserver_ptr; + std::shared_ptr<::paddle::distributed::GraphBrpcServer> pserver_ptr; std::thread* server_thread; }; class GraphPyClient : public GraphPyService { @@ -162,14 +162,14 @@ class GraphPyClient : public GraphPyService { set_client_id(client_id); GraphPyService::set_up(ips_str, shard_num, node_types, edge_types); } - std::shared_ptr get_ps_client() { + std::shared_ptr<::paddle::distributed::GraphBrpcClient> get_ps_client() { return worker_ptr; } void bind_local_server(int local_channel_index, GraphPyServer& server) { // NOLINT worker_ptr->set_local_channel(local_channel_index); worker_ptr->set_local_graph_service( - (paddle::distributed::GraphBrpcService*)server.get_ps_server() + (::paddle::distributed::GraphBrpcService*)server.get_ps_server() ->get_service()); } void StopServer(); @@ -209,7 +209,7 @@ class GraphPyClient : public GraphPyService { protected: mutable std::mutex mutex_; int client_id; - std::shared_ptr worker_ptr; + std::shared_ptr<::paddle::distributed::GraphBrpcClient> worker_ptr; std::thread* client_thread; bool stoped_ = false; }; diff --git a/paddle/fluid/distributed/ps/service/ps_service/service.cc b/paddle/fluid/distributed/ps/service/ps_service/service.cc index d62cdb4c133ed..e66475e88d875 100644 --- a/paddle/fluid/distributed/ps/service/ps_service/service.cc +++ b/paddle/fluid/distributed/ps/service/ps_service/service.cc @@ -28,9 +28,9 @@ using namespace std; // NOLINT namespace paddle { namespace distributed { -paddle::distributed::PSParameter load_from_prototxt( +::paddle::distributed::PSParameter load_from_prototxt( const std::string& filename) { - paddle::distributed::PSParameter param; + ::paddle::distributed::PSParameter param; int file_descriptor = open(filename.c_str(), O_RDONLY); if (file_descriptor == -1) { @@ -50,7 +50,7 @@ paddle::distributed::PSParameter load_from_prototxt( void PSCore::InitGFlag(const std::string& gflags) { VLOG(3) << "Init With Gflags:" << gflags; - std::vector flags = paddle::string::split_string(gflags); + std::vector flags = ::paddle::string::split_string(gflags); if (flags.empty()) { flags.push_back("-max_body_size=314217728"); flags.push_back("-socket_max_unwritten_bytes=2048000000"); @@ -64,7 +64,7 @@ void PSCore::InitGFlag(const std::string& gflags) { } int params_cnt = flags.size(); char** params_ptr = &(flags_ptr[0]); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr, true); + ::paddle::flags::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr); } int PSCore::InitServer( @@ -76,12 +76,12 @@ int PSCore::InitServer( const std::vector& server_sub_program) { google::protobuf::TextFormat::ParseFromString(dist_desc, &_ps_param); InitGFlag(_ps_param.init_gflags()); - _ps_env = paddle::distributed::PaddlePSEnvironment(); + _ps_env = ::paddle::distributed::PaddlePSEnvironment(); _ps_env.SetPsServers(host_sign_list, node_num); _ps_env.SetTrainers(trainers); int ret = 0; - _server_ptr = std::shared_ptr( - paddle::distributed::PSServerFactory::Create(_ps_param)); + _server_ptr = std::shared_ptr<::paddle::distributed::PSServer>( + ::paddle::distributed::PSServerFactory::Create(_ps_param)); ret = _server_ptr->Configure(_ps_param, _ps_env, index, server_sub_program); CHECK(ret == 0) << "failed to configure server"; return ret; @@ -89,13 +89,14 @@ int PSCore::InitServer( int PSCore::InitWorker( const std::string& dist_desc, - const std::map>& regions, + const std::map>& + regions, const std::vector* host_sign_list, int node_num, int index) { google::protobuf::TextFormat::ParseFromString(dist_desc, &_ps_param); InitGFlag(_ps_param.init_gflags()); - _ps_env = paddle::distributed::PaddlePSEnvironment(); + _ps_env = ::paddle::distributed::PaddlePSEnvironment(); _ps_env.SetPsServers(host_sign_list, node_num); int ret = 0; VLOG(1) << "PSCore::InitWorker"; @@ -132,6 +133,6 @@ int PSCore::StopServer() { stop_status.wait(); return 0; } -paddle::distributed::PSParameter* PSCore::GetParam() { return &_ps_param; } +::paddle::distributed::PSParameter* PSCore::GetParam() { return &_ps_param; } } // namespace distributed } // namespace paddle diff --git a/paddle/fluid/distributed/ps/service/ps_service/service.h b/paddle/fluid/distributed/ps/service/ps_service/service.h index eb190073fbd83..4c3c6db61e2bc 100644 --- a/paddle/fluid/distributed/ps/service/ps_service/service.h +++ b/paddle/fluid/distributed/ps/service/ps_service/service.h @@ -33,9 +33,9 @@ class PsRequestMessage; class PsResponseMessage; class PsService; -using paddle::distributed::PsRequestMessage; -using paddle::distributed::PsResponseMessage; -using paddle::distributed::PsService; +using ::paddle::distributed::PsRequestMessage; +using ::paddle::distributed::PsResponseMessage; +using ::paddle::distributed::PsService; class PSCore { public: @@ -51,7 +51,7 @@ class PSCore { const std::vector& server_sub_program = {}); virtual int InitWorker( const std::string& dist_desc, - const std::map>& + const std::map>& regions, const std::vector* host_sign_list, int node_num, @@ -63,16 +63,16 @@ class PSCore { virtual int CreateClient2ClientConnection(int pserver_timeout_ms, int pserver_connect_timeout_ms, int max_retry); - std::shared_ptr + std::shared_ptr<::paddle::distributed::PSServer> _server_ptr; // pointer to server - std::shared_ptr + std::shared_ptr<::paddle::distributed::PSClient> _worker_ptr; // pointer to worker - virtual paddle::distributed::PSParameter* GetParam(); + virtual ::paddle::distributed::PSParameter* GetParam(); private: void InitGFlag(const std::string& gflags); - paddle::distributed::PSParameter _ps_param; - paddle::distributed::PaddlePSEnvironment _ps_env; + ::paddle::distributed::PSParameter _ps_param; + ::paddle::distributed::PaddlePSEnvironment _ps_env; }; } // namespace distributed diff --git a/paddle/fluid/distributed/ps/service/server.h b/paddle/fluid/distributed/ps/service/server.h index 48b32d22cac79..fc1d4a2bd343b 100644 --- a/paddle/fluid/distributed/ps/service/server.h +++ b/paddle/fluid/distributed/ps/service/server.h @@ -57,8 +57,8 @@ namespace distributed { class Table; -using paddle::distributed::PsRequestMessage; -using paddle::distributed::PsResponseMessage; +using ::paddle::distributed::PsRequestMessage; +using ::paddle::distributed::PsResponseMessage; class PSServer { public: @@ -134,7 +134,7 @@ class PSServer { return -1; } - paddle::framework::Channel> _shuffled_ins; + ::paddle::framework::Channel> _shuffled_ins; protected: virtual int32_t Initialize() = 0; diff --git a/paddle/fluid/distributed/ps/service/simple_rpc/baidu_rpc_server.cc b/paddle/fluid/distributed/ps/service/simple_rpc/baidu_rpc_server.cc index f3e501dd00ce1..a10e78fe94162 100644 --- a/paddle/fluid/distributed/ps/service/simple_rpc/baidu_rpc_server.cc +++ b/paddle/fluid/distributed/ps/service/simple_rpc/baidu_rpc_server.cc @@ -19,8 +19,8 @@ #include "paddle/phi/core/enforce.h" namespace brpc { -DECLARE_uint64(max_body_size); -DECLARE_int64(socket_max_unwritten_bytes); +PD_DECLARE_uint64(max_body_size); +PD_DECLARE_int64(socket_max_unwritten_bytes); } // namespace brpc namespace paddle { diff --git a/paddle/fluid/distributed/ps/table/common_graph_table.cc b/paddle/fluid/distributed/ps/table/common_graph_table.cc index 316b2295c3389..153c67317d54d 100644 --- a/paddle/fluid/distributed/ps/table/common_graph_table.cc +++ b/paddle/fluid/distributed/ps/table/common_graph_table.cc @@ -58,14 +58,14 @@ int32_t GraphTable::Load_to_ssd(const std::string &path, return 0; } -paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea( +::paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea( int gpu_id, std::vector &node_ids, int slot_num) { size_t shard_num = 64; std::vector> bags(shard_num); std::vector feature_array[shard_num]; std::vector slot_id_array[shard_num]; std::vector node_id_array[shard_num]; - std::vector node_fea_info_array[shard_num]; + std::vector<::paddle::framework::GpuPsFeaInfo> node_fea_info_array[shard_num]; for (size_t i = 0; i < shard_num; i++) { auto predsize = node_ids.size() / shard_num; bags[i].reserve(predsize * 1.2); @@ -92,7 +92,7 @@ paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea( if (bags[i].size() > 0) { tasks.push_back(_cpu_worker_pool[gpu_id]->enqueue([&, i, this]() -> int { uint64_t node_id; - paddle::framework::GpuPsFeaInfo x; + ::paddle::framework::GpuPsFeaInfo x; std::vector feature_ids; for (size_t j = 0; j < bags[i].size(); j++) { Node *v = find_node(GraphTableType::FEATURE_TABLE, bags[i][j]); @@ -134,7 +134,7 @@ paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea( tasks.clear(); - paddle::framework::GpuPsCommGraphFea res; + ::paddle::framework::GpuPsCommGraphFea res; uint64_t tot_len = 0; for (size_t i = 0; i < shard_num; i++) { tot_len += feature_array[i].size(); @@ -165,7 +165,7 @@ paddle::framework::GpuPsCommGraphFea GraphTable::make_gpu_ps_graph_fea( return res; } -paddle::framework::GpuPsCommGraph GraphTable::make_gpu_ps_graph( +::paddle::framework::GpuPsCommGraph GraphTable::make_gpu_ps_graph( int idx, const std::vector &ids) { std::vector> bags(task_pool_size_); for (int i = 0; i < task_pool_size_; i++) { @@ -179,7 +179,7 @@ paddle::framework::GpuPsCommGraph GraphTable::make_gpu_ps_graph( std::vector> tasks; std::vector node_array[task_pool_size_]; // node id list - std::vector info_array[task_pool_size_]; + std::vector<::paddle::framework::GpuPsNodeInfo> info_array[task_pool_size_]; std::vector edge_array[task_pool_size_]; // edge id list for (size_t i = 0; i < bags.size(); i++) { @@ -215,7 +215,7 @@ paddle::framework::GpuPsCommGraph GraphTable::make_gpu_ps_graph( tot_len += edge_array[i].size(); } - paddle::framework::GpuPsCommGraph res; + ::paddle::framework::GpuPsCommGraph res; res.init_on_cpu(tot_len, ids.size()); int64_t offset = 0, ind = 0; for (int i = 0; i < task_pool_size_; i++) { @@ -516,13 +516,13 @@ void GraphTable::release_graph() { build_graph_type_keys(); if (FLAGS_gpugraph_storage_mode == - paddle::framework::GpuGraphStorageMode::WHOLE_HBM) { + ::paddle::framework::GpuGraphStorageMode::WHOLE_HBM) { build_graph_total_keys(); } // clear graph - if (FLAGS_gpugraph_storage_mode == paddle::framework::GpuGraphStorageMode:: + if (FLAGS_gpugraph_storage_mode == ::paddle::framework::GpuGraphStorageMode:: MEM_EMB_FEATURE_AND_GPU_GRAPH || - FLAGS_gpugraph_storage_mode == paddle::framework::GpuGraphStorageMode:: + FLAGS_gpugraph_storage_mode == ::paddle::framework::GpuGraphStorageMode:: SSD_EMB_AND_MEM_FEATURE_GPU_GRAPH) { clear_edge_shard(); } else { @@ -532,7 +532,7 @@ void GraphTable::release_graph() { void GraphTable::release_graph_edge() { if (FLAGS_gpugraph_storage_mode == - paddle::framework::GpuGraphStorageMode::WHOLE_HBM) { + ::paddle::framework::GpuGraphStorageMode::WHOLE_HBM) { build_graph_total_keys(); } clear_edge_shard(); @@ -543,10 +543,12 @@ void GraphTable::release_graph_node() { if (FLAGS_graph_metapath_split_opt) { clear_feature_shard(); } else { - if (FLAGS_gpugraph_storage_mode != paddle::framework::GpuGraphStorageMode:: - MEM_EMB_FEATURE_AND_GPU_GRAPH && - FLAGS_gpugraph_storage_mode != paddle::framework::GpuGraphStorageMode:: - SSD_EMB_AND_MEM_FEATURE_GPU_GRAPH) { + if (FLAGS_gpugraph_storage_mode != + ::paddle::framework::GpuGraphStorageMode:: + MEM_EMB_FEATURE_AND_GPU_GRAPH && + FLAGS_gpugraph_storage_mode != + ::paddle::framework::GpuGraphStorageMode:: + SSD_EMB_AND_MEM_FEATURE_GPU_GRAPH) { clear_feature_shard(); } else { merge_feature_shard(); @@ -666,7 +668,7 @@ int32_t GraphTable::load_edges_to_ssd(const std::string &path, idx = edge_to_id[edge_type]; } total_memory_cost = 0; - auto paths = paddle::string::split_string(path, ";"); + auto paths = ::paddle::string::split_string(path, ";"); int64_t count = 0; std::string sample_type = "random"; for (auto path : paths) { @@ -674,11 +676,12 @@ int32_t GraphTable::load_edges_to_ssd(const std::string &path, std::string line; while (std::getline(file, line)) { VLOG(0) << "get a line from file " << line; - auto values = paddle::string::split_string(line, "\t"); + auto values = ::paddle::string::split_string(line, "\t"); count++; if (values.size() < 2) continue; auto src_id = std::stoll(values[0]); - auto dist_ids = paddle::string::split_string(values[1], ";"); + auto dist_ids = + ::paddle::string::split_string(values[1], ";"); std::vector dist_data; for (auto x : dist_ids) { dist_data.push_back(std::stoll(x)); @@ -798,7 +801,7 @@ int CompleteGraphSampler::run_graph_sampling() { sample_nodes.resize(gpu_num); sample_neighbors.resize(gpu_num); sample_res.resize(gpu_num); - std::vector>> + std::vector>> sample_nodes_ex(graph_table->task_pool_size_); std::vector>> sample_neighbors_ex( graph_table->task_pool_size_); @@ -812,7 +815,7 @@ int CompleteGraphSampler::run_graph_sampling() { graph_table->_shards_task_pool[i % graph_table->task_pool_size_] ->enqueue([&, i, this]() -> int { if (this->status == GraphSamplerStatus::terminating) return 0; - paddle::framework::GpuPsGraphNode node; + ::paddle::framework::GpuPsGraphNode node; std::vector &v = this->graph_table->shards[i]->get_bucket(); size_t ind = i % this->graph_table->task_pool_size_; @@ -962,7 +965,7 @@ int BasicBfsGraphSampler::run_graph_sampling() { sample_nodes.resize(gpu_num); sample_neighbors.resize(gpu_num); sample_res.resize(gpu_num); - std::vector>> + std::vector>> sample_nodes_ex(graph_table->task_pool_size_); std::vector>> sample_neighbors_ex( graph_table->task_pool_size_); @@ -977,7 +980,7 @@ int BasicBfsGraphSampler::run_graph_sampling() { if (this->status == GraphSamplerStatus::terminating) { return 0; } - paddle::framework::GpuPsGraphNode node; + ::paddle::framework::GpuPsGraphNode node; auto iter = sample_neighbors_map[i].begin(); size_t ind = i; for (; iter != sample_neighbors_map[i].end(); iter++) { @@ -1237,7 +1240,7 @@ int32_t GraphTable::Load(const std::string &path, const std::string ¶m) { } std::string GraphTable::get_inverse_etype(std::string &etype) { - auto etype_split = paddle::string::split_string(etype, "2"); + auto etype_split = ::paddle::string::split_string(etype, "2"); std::string res; if (etype_split.size() == 3) { res = etype_split[2] + "2" + etype_split[1] + "2" + etype_split[0]; @@ -1253,13 +1256,13 @@ int32_t GraphTable::parse_type_to_typepath( std::vector &res_type, std::unordered_map &res_type2path) { auto type2files_split = - paddle::string::split_string(type2files, ","); + ::paddle::string::split_string(type2files, ","); if (type2files_split.empty()) { return -1; } for (auto one_type2file : type2files_split) { auto one_type2file_split = - paddle::string::split_string(one_type2file, ":"); + ::paddle::string::split_string(one_type2file, ":"); auto type = one_type2file_split[0]; auto type_dir = one_type2file_split[1]; res_type.push_back(type); @@ -1304,17 +1307,17 @@ int32_t GraphTable::parse_edge_and_load( VLOG(1) << "only_load_reverse_edge is False, etype[" << etypes[i] << "], file_path[" << etype_path << "]"; } - auto etype_path_list = paddle::framework::localfs_list(etype_path); + auto etype_path_list = ::paddle::framework::localfs_list(etype_path); std::string etype_path_str; if (part_num > 0 && part_num < static_cast(etype_path_list.size())) { std::vector sub_etype_path_list( etype_path_list.begin(), etype_path_list.begin() + part_num); etype_path_str = - paddle::string::join_strings(sub_etype_path_list, delim); + ::paddle::string::join_strings(sub_etype_path_list, delim); } else { etype_path_str = - paddle::string::join_strings(etype_path_list, delim); + ::paddle::string::join_strings(etype_path_list, delim); } if (!only_load_reverse_edge) { this->load_edges(etype_path_str, false, etypes[i]); @@ -1345,14 +1348,14 @@ int32_t GraphTable::parse_node_and_load(std::string ntype2files, } std::string delim = ";"; std::string npath = node_to_nodedir[ntypes[0]]; - auto npath_list = paddle::framework::localfs_list(npath); + auto npath_list = ::paddle::framework::localfs_list(npath); std::string npath_str; if (part_num > 0 && part_num < static_cast(npath_list.size())) { std::vector sub_npath_list(npath_list.begin(), npath_list.begin() + part_num); - npath_str = paddle::string::join_strings(sub_npath_list, delim); + npath_str = ::paddle::string::join_strings(sub_npath_list, delim); } else { - npath_str = paddle::string::join_strings(npath_list, delim); + npath_str = ::paddle::string::join_strings(npath_list, delim); } if (ntypes.empty()) { @@ -1425,17 +1428,18 @@ int32_t GraphTable::load_node_and_edge_file( VLOG(1) << "only_load_reverse_edge is False, etype[" << etypes[i] << "], file_path[" << etype_path << "]"; } - auto etype_path_list = paddle::framework::localfs_list(etype_path); + auto etype_path_list = + ::paddle::framework::localfs_list(etype_path); std::string etype_path_str; if (part_num > 0 && part_num < static_cast(etype_path_list.size())) { std::vector sub_etype_path_list( etype_path_list.begin(), etype_path_list.begin() + part_num); etype_path_str = - paddle::string::join_strings(sub_etype_path_list, delim); + ::paddle::string::join_strings(sub_etype_path_list, delim); } else { etype_path_str = - paddle::string::join_strings(etype_path_list, delim); + ::paddle::string::join_strings(etype_path_list, delim); } if (!only_load_reverse_edge) { this->load_edges(etype_path_str, false, etypes[i]); @@ -1448,15 +1452,15 @@ int32_t GraphTable::load_node_and_edge_file( } } else { std::string npath = node_to_nodedir[ntypes[0]]; - auto npath_list = paddle::framework::localfs_list(npath); + auto npath_list = ::paddle::framework::localfs_list(npath); std::string npath_str; if (part_num > 0 && part_num < static_cast(npath_list.size())) { std::vector sub_npath_list( npath_list.begin(), npath_list.begin() + part_num); - npath_str = paddle::string::join_strings(sub_npath_list, delim); + npath_str = ::paddle::string::join_strings(sub_npath_list, delim); } else { - npath_str = paddle::string::join_strings(npath_list, delim); + npath_str = ::paddle::string::join_strings(npath_list, delim); } if (ntypes.empty()) { @@ -1553,14 +1557,14 @@ std::pair GraphTable::parse_node_file( uint64_t local_valid_count = 0; int num = 0; - std::vector vals; + std::vector<::paddle::string::str_ptr> vals; size_t n = node_type.length(); while (std::getline(file, line)) { if (strncmp(line.c_str(), node_type.c_str(), n) != 0) { continue; } vals.clear(); - num = paddle::string::split_string_ptr( + num = ::paddle::string::split_string_ptr( line.c_str() + n + 1, line.length() - n - 1, '\t', &vals); if (num == 0) { continue; @@ -1603,15 +1607,15 @@ std::pair GraphTable::parse_node_file( uint64_t local_valid_count = 0; int idx = 0; - auto path_split = paddle::string::split_string(path, "/"); + auto path_split = ::paddle::string::split_string(path, "/"); auto path_name = path_split[path_split.size() - 1]; int num = 0; - std::vector vals; + std::vector<::paddle::string::str_ptr> vals; while (std::getline(file, line)) { vals.clear(); - num = paddle::string::split_string_ptr( + num = ::paddle::string::split_string_ptr( line.c_str(), line.length(), '\t', &vals); if (vals.empty()) { continue; @@ -1654,7 +1658,7 @@ std::pair GraphTable::parse_node_file( // // TODO(danleifeng): opt load all node_types in once reading int32_t GraphTable::load_nodes(const std::string &path, std::string node_type) { - auto paths = paddle::string::split_string(path, ";"); + auto paths = ::paddle::string::split_string(path, ";"); uint64_t count = 0; uint64_t valid_count = 0; int idx = 0; @@ -1725,8 +1729,8 @@ std::pair GraphTable::parse_edge_file( uint64_t local_valid_count = 0; uint64_t part_num = 0; if (FLAGS_graph_load_in_parallel) { - auto path_split = paddle::string::split_string(path, "/"); - auto part_name_split = paddle::string::split_string( + auto path_split = ::paddle::string::split_string(path, "/"); + auto part_name_split = ::paddle::string::split_string( path_split[path_split.size() - 1], "-"); part_num = std::stoull(part_name_split[part_name_split.size() - 1]); } @@ -1793,7 +1797,7 @@ int32_t GraphTable::load_edges(const std::string &path, idx = edge_to_id[edge_type]; } - auto paths = paddle::string::split_string(path, ";"); + auto paths = ::paddle::string::split_string(path, ";"); uint64_t count = 0; uint64_t valid_count = 0; @@ -1865,7 +1869,7 @@ Node *GraphTable::find_node(GraphTableType table_type, uint64_t id) { table_type == GraphTableType::EDGE_TABLE ? edge_shards : feature_shards; for (auto &search_shard : search_shards) { PADDLE_ENFORCE_NOT_NULL(search_shard[index], - paddle::platform::errors::InvalidArgument( + ::paddle::platform::errors::InvalidArgument( "search_shard[%d] should not be null.", index)); node = search_shard[index]->find_node(id); if (node != nullptr) { @@ -1885,7 +1889,7 @@ Node *GraphTable::find_node(GraphTableType table_type, int idx, uint64_t id) { ? edge_shards[idx] : feature_shards[idx]; PADDLE_ENFORCE_NOT_NULL(search_shards[index], - paddle::platform::errors::InvalidArgument( + ::paddle::platform::errors::InvalidArgument( "search_shard[%d] should not be null.", index)); Node *node = search_shards[index]->find_node(id); return node; @@ -2164,8 +2168,8 @@ void string_vector_2_string(std::vector::iterator strs_begin, } void string_vector_2_string( - std::vector::iterator strs_begin, - std::vector::iterator strs_end, + std::vector<::paddle::string::str_ptr>::iterator strs_begin, + std::vector<::paddle::string::str_ptr>::iterator strs_end, char delim, std::string *output) { size_t i = 0; @@ -2184,19 +2188,19 @@ int GraphTable::parse_feature(int idx, FeatureNode *node) { // Return (feat_id, btyes) if name are in this->feat_name, else return (-1, // "") - thread_local std::vector fields; + thread_local std::vector<::paddle::string::str_ptr> fields; fields.clear(); char c = slot_feature_separator_.at(0); - paddle::string::split_string_ptr(feat_str, len, c, &fields); + ::paddle::string::split_string_ptr(feat_str, len, c, &fields); - thread_local std::vector fea_fields; + thread_local std::vector<::paddle::string::str_ptr> fea_fields; fea_fields.clear(); c = feature_separator_.at(0); - paddle::string::split_string_ptr(fields[1].ptr, - fields[1].len, - c, - &fea_fields, - FLAGS_gpugraph_slot_feasign_max_num); + ::paddle::string::split_string_ptr(fields[1].ptr, + fields[1].len, + c, + &fea_fields, + FLAGS_gpugraph_slot_feasign_max_num); std::string name = fields[0].to_string(); auto it = feat_id_map[idx].find(name); if (it != feat_id_map[idx].end()) { @@ -2522,14 +2526,14 @@ int32_t GraphTable::Initialize(const TableParameter &config, } void GraphTable::load_node_weight(int type_id, int idx, std::string path) { - auto paths = paddle::string::split_string(path, ";"); + auto paths = ::paddle::string::split_string(path, ";"); int64_t count = 0; auto &weight_map = node_weight[type_id][idx]; for (auto path : paths) { std::ifstream file(path); std::string line; while (std::getline(file, line)) { - auto values = paddle::string::split_string(line, "\t"); + auto values = ::paddle::string::split_string(line, "\t"); count++; if (values.size() < 2) continue; auto src_id = std::stoull(values[0]); @@ -2546,7 +2550,7 @@ int32_t GraphTable::Initialize(const GraphParameter &graph) { _db = NULL; search_level = graph.search_level(); if (search_level >= 2) { - _db = paddle::distributed::RocksDBHandler::GetInstance(); + _db = ::paddle::distributed::RocksDBHandler::GetInstance(); _db->initialize("./temp_gpups_db", task_pool_size_); } // gpups_mode = true; diff --git a/paddle/fluid/distributed/ps/table/common_graph_table.h b/paddle/fluid/distributed/ps/table/common_graph_table.h index 142327368281a..91967ccb07c25 100644 --- a/paddle/fluid/distributed/ps/table/common_graph_table.h +++ b/paddle/fluid/distributed/ps/table/common_graph_table.h @@ -712,9 +712,9 @@ class GraphTable : public Table { int &actual_size); // NOLINT virtual int32_t add_node_to_ssd( int type_id, int idx, uint64_t src_id, char *data, int len); - virtual paddle::framework::GpuPsCommGraph make_gpu_ps_graph( + virtual ::paddle::framework::GpuPsCommGraph make_gpu_ps_graph( int idx, const std::vector &ids); - virtual paddle::framework::GpuPsCommGraphFea make_gpu_ps_graph_fea( + virtual ::paddle::framework::GpuPsCommGraphFea make_gpu_ps_graph_fea( int gpu_id, std::vector &node_ids, int slot_num); // NOLINT int32_t Load_to_ssd(const std::string &path, const std::string ¶m); int64_t load_graph_to_memory_from_ssd(int idx, @@ -786,7 +786,7 @@ class GraphTable : public Table { std::shared_ptr rw_lock; #ifdef PADDLE_WITH_HETERPS // paddle::framework::GpuPsGraphTable gpu_graph_table; - paddle::distributed::RocksDBHandler *_db; + ::paddle::distributed::RocksDBHandler *_db; // std::shared_ptr<::ThreadPool> graph_sample_pool; // std::shared_ptr graph_sampler; // REGISTER_GRAPH_FRIEND_CLASS(2, CompleteGraphSampler, BasicBfsGraphSampler) @@ -847,8 +847,8 @@ class BasicBfsGraphSampler : public GraphSampler { namespace std { template <> -struct hash { - size_t operator()(const paddle::distributed::SampleKey &s) const { +struct hash<::paddle::distributed::SampleKey> { + size_t operator()(const ::paddle::distributed::SampleKey &s) const { return s.idx ^ s.node_key ^ s.sample_size; } }; diff --git a/paddle/fluid/distributed/ps/table/ctr_accessor.cc b/paddle/fluid/distributed/ps/table/ctr_accessor.cc index 61e748a541367..ca634572a462e 100644 --- a/paddle/fluid/distributed/ps/table/ctr_accessor.cc +++ b/paddle/fluid/distributed/ps/table/ctr_accessor.cc @@ -14,10 +14,9 @@ #include "paddle/fluid/distributed/ps/table/ctr_accessor.h" -#include - #include "glog/logging.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/ctr_double_accessor.cc b/paddle/fluid/distributed/ps/table/ctr_double_accessor.cc index 35c27242fe355..46d3ebf400d5f 100644 --- a/paddle/fluid/distributed/ps/table/ctr_double_accessor.cc +++ b/paddle/fluid/distributed/ps/table/ctr_double_accessor.cc @@ -14,10 +14,9 @@ #include "paddle/fluid/distributed/ps/table/ctr_double_accessor.h" -#include - #include "glog/logging.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/ctr_dymf_accessor.cc b/paddle/fluid/distributed/ps/table/ctr_dymf_accessor.cc index 4824ab8946b9d..a2943c2237cec 100644 --- a/paddle/fluid/distributed/ps/table/ctr_dymf_accessor.cc +++ b/paddle/fluid/distributed/ps/table/ctr_dymf_accessor.cc @@ -14,10 +14,9 @@ #include "paddle/fluid/distributed/ps/table/ctr_dymf_accessor.h" -#include - #include "glog/logging.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/depends/dense.h b/paddle/fluid/distributed/ps/table/depends/dense.h index 3f09376b42db3..272fb0b00504f 100644 --- a/paddle/fluid/distributed/ps/table/depends/dense.h +++ b/paddle/fluid/distributed/ps/table/depends/dense.h @@ -22,8 +22,8 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/distributed/common/utils.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/depends/feature_value.h b/paddle/fluid/distributed/ps/table/depends/feature_value.h index c91502a8552e8..ce01c8762253c 100644 --- a/paddle/fluid/distributed/ps/table/depends/feature_value.h +++ b/paddle/fluid/distributed/ps/table/depends/feature_value.h @@ -14,11 +14,12 @@ #pragma once -#include #include -#include "gflags/gflags.h" +#include + #include "paddle/fluid/distributed/common/chunk_allocator.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/depends/initializers.h b/paddle/fluid/distributed/ps/table/depends/initializers.h index 467227097674b..3ebe5549de4f7 100644 --- a/paddle/fluid/distributed/ps/table/depends/initializers.h +++ b/paddle/fluid/distributed/ps/table/depends/initializers.h @@ -21,9 +21,9 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/operators/truncated_gaussian_random_op.h" #include "paddle/phi/core/generator.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { @@ -124,13 +124,13 @@ class TruncatedGaussianInitializer : public Initializer { } float GetValue() override { - paddle::operators::TruncatedNormal truncated_normal(mean_, std_); + ::paddle::operators::TruncatedNormal truncated_normal(mean_, std_); float value = truncated_normal(dist_(*random_engine_)); return value; } void GetValue(float *value, int numel) { - paddle::operators::TruncatedNormal truncated_normal(mean_, std_); + ::paddle::operators::TruncatedNormal truncated_normal(mean_, std_); for (int x = 0; x < numel; ++x) { value[x] = truncated_normal(dist_(*random_engine_)); } diff --git a/paddle/fluid/distributed/ps/table/graph/class_macro.h b/paddle/fluid/distributed/ps/table/graph/class_macro.h index bf59dbacb2537..895e59d09af62 100644 --- a/paddle/fluid/distributed/ps/table/graph/class_macro.h +++ b/paddle/fluid/distributed/ps/table/graph/class_macro.h @@ -36,4 +36,4 @@ #define DECLARE_11_FRIEND_CLASS(a, ...) \ DECLARE_GRAPH_FRIEND_CLASS(a) DECLARE_10_FRIEND_CLASS(__VA_ARGS__) #define REGISTER_GRAPH_FRIEND_CLASS(n, ...) \ - DECLARE_##n##_FRIEND_CLASS(__VA_ARGS__) + PD_DECLARE_##n##_FRIEND_CLASS(__VA_ARGS__) diff --git a/paddle/fluid/distributed/ps/table/memory_sparse_table.cc b/paddle/fluid/distributed/ps/table/memory_sparse_table.cc index 5ee23010b526e..dbdff119141a5 100644 --- a/paddle/fluid/distributed/ps/table/memory_sparse_table.cc +++ b/paddle/fluid/distributed/ps/table/memory_sparse_table.cc @@ -26,16 +26,18 @@ // #include "boost/lexical_cast.hpp" #include "paddle/fluid/platform/enforce.h" -DEFINE_bool(pserver_print_missed_key_num_every_push, - false, - "pserver_print_missed_key_num_every_push"); -DEFINE_bool(pserver_create_value_when_push, - true, - "pserver create value when push"); -DEFINE_bool(pserver_enable_create_feasign_randomly, - false, - "pserver_enable_create_feasign_randomly"); -DEFINE_int32(pserver_table_save_max_retry, 3, "pserver_table_save_max_retry"); +PD_DEFINE_bool(pserver_print_missed_key_num_every_push, + false, + "pserver_print_missed_key_num_every_push"); +PD_DEFINE_bool(pserver_create_value_when_push, + true, + "pserver create value when push"); +PD_DEFINE_bool(pserver_enable_create_feasign_randomly, + false, + "pserver_enable_create_feasign_randomly"); +PD_DEFINE_int32(pserver_table_save_max_retry, + 3, + "pserver_table_save_max_retry"); namespace paddle { namespace distributed { @@ -333,7 +335,7 @@ int32_t MemorySparseTable::Save(const std::string &dirname, TopkCalculator tk(_real_local_shard_num, tk_size); std::string table_path = TableDir(dirname); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d-*", table_path.c_str(), _shard_idx)); std::atomic feasign_size_all{0}; @@ -350,15 +352,15 @@ int32_t MemorySparseTable::Save(const std::string &dirname, FsChannelConfig channel_config; if (_config.compress_in_save() && (save_param == 0 || save_param == 3)) { channel_config.path = - paddle::string::format_string("%s/part-%03d-%05d.gz", - table_path.c_str(), - _shard_idx, - file_start_idx + i); + ::paddle::string::format_string("%s/part-%03d-%05d.gz", + table_path.c_str(), + _shard_idx, + file_start_idx + i); } else { - channel_config.path = paddle::string::format_string("%s/part-%03d-%05d", - table_path.c_str(), - _shard_idx, - file_start_idx + i); + channel_config.path = ::paddle::string::format_string("%s/part-%03d-%05d", + table_path.c_str(), + _shard_idx, + file_start_idx + i); } channel_config.converter = _value_accesor->Converter(save_param).converter; channel_config.deconverter = @@ -385,7 +387,7 @@ int32_t MemorySparseTable::Save(const std::string &dirname, if (_value_accesor->Save(it.value().data(), save_param)) { std::string format_value = _value_accesor->ParseToString( it.value().data(), it.value().size()); - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", it.key(), format_value.c_str()))) { ++retry_num; is_write_failed = true; @@ -432,7 +434,7 @@ int32_t MemorySparseTable::SavePatch(const std::string &path, int save_param) { } size_t file_start_idx = _m_avg_local_shard_num * _shard_idx; std::string table_path = TableDir(path); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d-*", table_path.c_str(), _shard_idx)); int thread_num = _m_real_local_shard_num < 20 ? _m_real_local_shard_num : 20; @@ -442,10 +444,10 @@ int32_t MemorySparseTable::SavePatch(const std::string &path, int save_param) { #pragma omp parallel for schedule(dynamic) for (int i = 0; i < _m_real_local_shard_num; ++i) { FsChannelConfig channel_config; - channel_config.path = paddle::string::format_string("%s/part-%03d-%05d", - table_path.c_str(), - _shard_idx, - file_start_idx + i); + channel_config.path = ::paddle::string::format_string("%s/part-%03d-%05d", + table_path.c_str(), + _shard_idx, + file_start_idx + i); channel_config.converter = _value_accesor->Converter(save_param).converter; channel_config.deconverter = @@ -469,8 +471,9 @@ int32_t MemorySparseTable::SavePatch(const std::string &path, int save_param) { if (_value_accesor->Save(it.value().data(), save_param)) { std::string format_value = _value_accesor->ParseToString( it.value().data(), it.value().size()); - if (0 != write_channel->write_line(paddle::string::format_string( - "%lu %s", it.key(), format_value.c_str()))) { + if (0 != + write_channel->write_line(::paddle::string::format_string( + "%lu %s", it.key(), format_value.c_str()))) { ++retry_num; is_write_failed = true; LOG(ERROR) << "MemorySparseTable save failed, retry it! path:" @@ -503,10 +506,10 @@ int32_t MemorySparseTable::SavePatch(const std::string &path, int save_param) { feasign_size_all += feasign_size; } LOG(INFO) << "MemorySparseTable save patch success, path:" - << paddle::string::format_string("%s/%03d/part-%03d-", - path.c_str(), - _config.table_id(), - _shard_idx) + << ::paddle::string::format_string("%s/%03d/part-%03d-", + path.c_str(), + _config.table_id(), + _shard_idx) << " from " << file_start_idx << " to " << file_start_idx + _m_real_local_shard_num - 1 << ", feasign size: " << feasign_size_all; @@ -519,7 +522,7 @@ int64_t MemorySparseTable::CacheShuffle( double cache_threshold, std::function( int msg_type, int to_pserver_id, std::string &msg)> send_msg_func, - paddle::framework::Channel> + ::paddle::framework::Channel> &shuffled_channel, const std::vector &table_ptrs) { LOG(INFO) << "cache shuffle with cache threshold: " << cache_threshold; @@ -536,24 +539,24 @@ int64_t MemorySparseTable::CacheShuffle( int thread_num = _real_local_shard_num < 20 ? _real_local_shard_num : 20; std::vector< - paddle::framework::ChannelWriter>> + ::paddle::framework::ChannelWriter>> writers(_real_local_shard_num); std::vector>> datas( _real_local_shard_num); int feasign_size = 0; - std::vector>> + std::vector<::paddle::framework::Channel>> tmp_channels; for (int i = 0; i < _real_local_shard_num; ++i) { tmp_channels.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); } omp_set_num_threads(thread_num); #pragma omp parallel for schedule(dynamic) for (int i = 0; i < _real_local_shard_num; ++i) { - paddle::framework::ChannelWriter> &writer = - writers[i]; + ::paddle::framework::ChannelWriter> + &writer = writers[i]; writer.Reset(tmp_channels[i].get()); for (auto table_ptr : table_ptrs) { @@ -579,15 +582,15 @@ int64_t MemorySparseTable::CacheShuffle( // shard num: " << _real_local_shard_num; std::vector> local_datas; for (int idx_shard = 0; idx_shard < _real_local_shard_num; ++idx_shard) { - paddle::framework::ChannelWriter> &writer = - writers[idx_shard]; + ::paddle::framework::ChannelWriter> + &writer = writers[idx_shard]; auto channel = writer.channel(); std::vector> &data = datas[idx_shard]; - std::vector ars(shuffle_node_num); + std::vector<::paddle::framework::BinaryArchive> ars(shuffle_node_num); while (channel->Read(data)) { for (auto &t : data) { auto pserver_id = - paddle::distributed::local_random_engine()() % shuffle_node_num; + ::paddle::distributed::local_random_engine()() % shuffle_node_num; if (pserver_id != _shard_idx) { ars[pserver_id] << t; } else { @@ -618,7 +621,7 @@ int64_t MemorySparseTable::CacheShuffle( t.wait(); } ars.clear(); - ars = std::vector(shuffle_node_num); + ars = std::vector<::paddle::framework::BinaryArchive>(shuffle_node_num); data = std::vector>(); } } @@ -629,20 +632,20 @@ int64_t MemorySparseTable::CacheShuffle( int32_t MemorySparseTable::SaveCache( const std::string &path, const std::string ¶m, - paddle::framework::Channel> + ::paddle::framework::Channel> &shuffled_channel) { if (_shard_idx >= _config.sparse_table_cache_file_num()) { return 0; } int save_param = atoi(param.c_str()); // batch_model:0 xbox:1 - std::string table_path = paddle::string::format_string( + std::string table_path = ::paddle::string::format_string( "%s/%03d_cache/", path.c_str(), _config.table_id()); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d", table_path.c_str(), _shard_idx)); uint32_t feasign_size = 0; FsChannelConfig channel_config; // not compress cache model - channel_config.path = paddle::string::format_string( + channel_config.path = ::paddle::string::format_string( "%s/part-%03d", table_path.c_str(), _shard_idx); channel_config.converter = _value_accesor->Converter(save_param).converter; channel_config.deconverter = @@ -654,7 +657,7 @@ int32_t MemorySparseTable::SaveCache( while (shuffled_channel->Read(data)) { for (auto &t : data) { ++feasign_size; - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", t.first, t.second.c_str()))) { LOG(ERROR) << "Cache Table save failed, " "path:" diff --git a/paddle/fluid/distributed/ps/table/sparse_accessor.cc b/paddle/fluid/distributed/ps/table/sparse_accessor.cc index afa94703233cf..77460a8d17e47 100644 --- a/paddle/fluid/distributed/ps/table/sparse_accessor.cc +++ b/paddle/fluid/distributed/ps/table/sparse_accessor.cc @@ -14,10 +14,9 @@ #include "paddle/fluid/distributed/ps/table/sparse_accessor.h" -#include - #include "glog/logging.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { @@ -300,7 +299,7 @@ std::string SparseAccessor::ParseToString(const float* v, int param) { int SparseAccessor::ParseFromString(const std::string& str, float* value) { _embedx_sgd_rule->InitValue(value + sparse_feature_value.EmbedxWIndex(), value + sparse_feature_value.EmbedxG2SumIndex()); - auto ret = paddle::string::str_to_float(str.data(), value); + auto ret = ::paddle::string::str_to_float(str.data(), value); CHECK(ret >= 6) << "expect more than 6 real:" << ret; return ret; } diff --git a/paddle/fluid/distributed/ps/table/sparse_sgd_rule.cc b/paddle/fluid/distributed/ps/table/sparse_sgd_rule.cc index 6ab4506d29e4c..0c66e9d407aa4 100644 --- a/paddle/fluid/distributed/ps/table/sparse_sgd_rule.cc +++ b/paddle/fluid/distributed/ps/table/sparse_sgd_rule.cc @@ -14,11 +14,11 @@ #include "paddle/fluid/distributed/ps/table/sparse_sgd_rule.h" -#include - #include "glog/logging.h" -DEFINE_bool(enable_show_scale_gradient, true, "enable show scale gradient"); +#include "paddle/utils/flags.h" + +PD_DEFINE_bool(enable_show_scale_gradient, true, "enable show scale gradient"); namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/ssd_sparse_table.cc b/paddle/fluid/distributed/ps/table/ssd_sparse_table.cc index 7d96e0f49d1ad..bb6de81cbb357 100644 --- a/paddle/fluid/distributed/ps/table/ssd_sparse_table.cc +++ b/paddle/fluid/distributed/ps/table/ssd_sparse_table.cc @@ -20,11 +20,11 @@ #include "paddle/fluid/framework/archive.h" #include "paddle/fluid/platform/flags.h" #include "paddle/utils/string/string_helper.h" -DECLARE_bool(pserver_print_missed_key_num_every_push); -DECLARE_bool(pserver_create_value_when_push); -DECLARE_bool(pserver_enable_create_feasign_randomly); -DEFINE_bool(pserver_open_strict_check, false, "pserver_open_strict_check"); -DEFINE_int32(pserver_load_batch_size, 5000, "load batch size for ssd"); +PD_DECLARE_bool(pserver_print_missed_key_num_every_push); +PD_DECLARE_bool(pserver_create_value_when_push); +PD_DECLARE_bool(pserver_enable_create_feasign_randomly); +PD_DEFINE_bool(pserver_open_strict_check, false, "pserver_open_strict_check"); +PD_DEFINE_int32(pserver_load_batch_size, 5000, "load batch size for ssd"); PADDLE_DEFINE_EXPORTED_string(rocksdb_path, "database", "path of sparse table rocksdb file"); @@ -34,7 +34,7 @@ namespace distributed { int32_t SSDSparseTable::Initialize() { MemorySparseTable::Initialize(); - _db = paddle::distributed::RocksDBHandler::GetInstance(); + _db = ::paddle::distributed::RocksDBHandler::GetInstance(); _db->initialize(FLAGS_rocksdb_path, _real_local_shard_num); VLOG(0) << "initalize SSDSparseTable succ"; VLOG(0) << "SSD FLAGS_pserver_print_missed_key_num_every_push:" @@ -135,7 +135,7 @@ int32_t SSDSparseTable::PullSparse(float* pull_values, } else { data_size = tmp_string.size() / sizeof(float); memcpy(data_buffer_ptr, - paddle::string::str_to_float(tmp_string), + ::paddle::string::str_to_float(tmp_string), data_size * sizeof(float)); // from rocksdb to mem auto& feature_value = local_shard[key]; @@ -239,7 +239,7 @@ int32_t SSDSparseTable::PullSparsePtr(int shard_id, auto& feature_value = local_shard[cur_key]; feature_value.resize(data_size); memcpy(const_cast(feature_value.data()), - paddle::string::str_to_float( + ::paddle::string::str_to_float( cur_ctx->batch_values[idx].data()), data_size * sizeof(float)); _db->del_data(shard_id, @@ -302,7 +302,7 @@ int32_t SSDSparseTable::PullSparsePtr(int shard_id, feature_value.resize(data_size); memcpy( const_cast(feature_value.data()), - paddle::string::str_to_float(cur_ctx->batch_values[idx].data()), + ::paddle::string::str_to_float(cur_ctx->batch_values[idx].data()), data_size * sizeof(float)); _db->del_data( shard_id, reinterpret_cast(&cur_key), sizeof(uint64_t)); @@ -529,7 +529,7 @@ int32_t SSDSparseTable::Shrink(const std::string& param) { auto* it = _db->get_iterator(i); for (it->SeekToFirst(); it->Valid(); it->Next()) { if (_value_accesor->Shrink( - paddle::string::str_to_float(it->value().data()))) { + ::paddle::string::str_to_float(it->value().data()))) { _db->del_data(i, it->key().data(), it->key().size()); ssd_count++; } else { @@ -627,7 +627,7 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, VLOG(0) << "TopkCalculator top n:" << _cache_tk_size; size_t file_start_idx = _avg_local_shard_num * _shard_idx; std::string table_path = TableDir(path); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d-*", table_path.c_str(), _shard_idx)); #ifdef PADDLE_WITH_GPU_GRAPH int thread_num = _real_local_shard_num; @@ -640,12 +640,11 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, // feasign_size = 0; std::vector< - paddle::framework::Channel>>> + ::paddle::framework::Channel>>> fs_channel; for (int i = 0; i < _real_local_shard_num; i++) { - fs_channel.push_back( - paddle::framework::MakeChannel>>( - 10240)); + fs_channel.push_back(::paddle::framework::MakeChannel< + std::pair>>(10240)); } std::vector threads; threads.resize(_real_local_shard_num); @@ -659,29 +658,29 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, FsChannelConfig channel_config; if (_config.compress_in_save() && (save_param == 0 || save_param == 3)) { channel_config.path = - paddle::string::format_string("%s/part-%03d-%05d.gz", - table_path.c_str(), - _shard_idx, - file_start_idx + file_num); + ::paddle::string::format_string("%s/part-%03d-%05d.gz", + table_path.c_str(), + _shard_idx, + file_start_idx + file_num); } else { channel_config.path = - paddle::string::format_string("%s/part-%03d-%05d", - table_path.c_str(), - _shard_idx, - file_start_idx + file_num); + ::paddle::string::format_string("%s/part-%03d-%05d", + table_path.c_str(), + _shard_idx, + file_start_idx + file_num); } channel_config.converter = _value_accesor->Converter(save_param).converter; channel_config.deconverter = _value_accesor->Converter(save_param).deconverter; auto write_channel = _afs_client.open_w(channel_config, 1024 * 1024 * 40, &err_no); - paddle::framework::ChannelReader>> + ::paddle::framework::ChannelReader>> reader(fs_channel[file_num].get()); std::pair> out_str; while (reader >> out_str) { std::string format_value = _value_accesor->ParseToString( out_str.second.data(), out_str.second.size()); - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", out_str.first, format_value.c_str()))) { LOG(FATAL) << "SSDSparseTable save failed, retry it! path:" << channel_config.path; @@ -693,8 +692,8 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, threads[i] = std::thread(save_func, i); } - std::vector< - paddle::framework::ChannelWriter>>> + std::vector<::paddle::framework::ChannelWriter< + std::pair>>> writers(_real_local_shard_num); omp_set_num_threads(thread_num); #pragma omp parallel for schedule(dynamic) @@ -726,14 +725,14 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, auto* it = _db->get_iterator(i); for (it->SeekToFirst(); it->Valid(); it->Next()) { bool need_save = _value_accesor->Save( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); _value_accesor->UpdateStatAfterSave( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); if (need_save) { std::vector feature_value; feature_value.resize(it->value().size() / sizeof(float)); memcpy(const_cast(feature_value.data()), - paddle::string::str_to_float(it->value().data()), + ::paddle::string::str_to_float(it->value().data()), it->value().size()); writer << std::make_pair(*(reinterpret_cast( const_cast(it->key().data()))), @@ -766,10 +765,10 @@ int32_t SSDSparseTable::SaveWithString(const std::string& path, } VLOG(0) << "SSDSparseTable save success, feasign size:" << feasign_size_all << ", path:" - << paddle::string::format_string("%s/%03d/part-%03d-", - path.c_str(), - _config.table_id(), - _shard_idx) + << ::paddle::string::format_string("%s/%03d/part-%03d-", + path.c_str(), + _config.table_id(), + _shard_idx) << " from " << file_start_idx << " to " << file_start_idx + _real_local_shard_num - 1; _local_show_threshold = tk.top(); @@ -800,7 +799,7 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, VLOG(0) << "TopkCalculator top n:" << _cache_tk_size; size_t file_start_idx = _avg_local_shard_num * _shard_idx; std::string table_path = TableDir(path); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d-*", table_path.c_str(), _shard_idx)); #ifdef PADDLE_WITH_GPU_GRAPH int thread_num = _real_local_shard_num; @@ -809,17 +808,17 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, #endif std::atomic feasign_size_all{0}; - std::vector>> + std::vector<::paddle::framework::Channel>> busy_channel; - std::vector>> + std::vector<::paddle::framework::Channel>> free_channel; std::vector threads; for (int i = 0; i < _real_local_shard_num; i++) { busy_channel.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); free_channel.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); } threads.resize(_real_local_shard_num); @@ -848,14 +847,14 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, int split_num) { if (compress && (save_param == 0 || save_param == 3)) { // return - // paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d.gz", + // ::paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d.gz", // table_path, node_num, shard_num, part_num, split_num); - return paddle::string::format_string( + return ::paddle::string::format_string( "%s/part-%05d-%03d.gz", table_path, shard_num, split_num); } else { - // return paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d", + // return ::paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d", // table_path, node_num, shard_num, part_num, split_num); - return paddle::string::format_string( + return ::paddle::string::format_string( "%s/part-%05d-%03d", table_path, shard_num, split_num); } }; @@ -899,7 +898,7 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, int dim = len / sizeof(float); std::string format_value = _value_accesor->ParseToString(value, dim); - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", k, format_value.c_str()))) { VLOG(0) << "SSDSparseTable save failed, retry it! path:" << channel_config.path; @@ -985,9 +984,9 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, auto* it = _db->get_iterator(i); for (it->SeekToFirst(); it->Valid(); it->Next()) { bool need_save = _value_accesor->Save( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); _value_accesor->UpdateStatAfterSave( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); if (need_save) { uint32_t len = sizeof(uint64_t) + it->value().size() + sizeof(uint32_t); @@ -1052,10 +1051,10 @@ int32_t SSDSparseTable::SaveWithStringMultiOutput(const std::string& path, } VLOG(0) << "DownpourSparseSSDTable save success, feasign size:" << feasign_size_all << " ,path:" - << paddle::string::format_string("%s/%03d/part-%03d-", - path.c_str(), - _config.table_id(), - _shard_idx) + << ::paddle::string::format_string("%s/%03d/part-%03d-", + path.c_str(), + _config.table_id(), + _shard_idx) << " from " << file_start_idx << " to " << file_start_idx + _real_local_shard_num - 1; if (_config.enable_sparse_table_cache()) { @@ -1085,7 +1084,7 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, VLOG(0) << "TopkCalculator top n:" << _cache_tk_size; size_t file_start_idx = _avg_local_shard_num * _shard_idx; std::string table_path = TableDir(path); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d-*", table_path.c_str(), _shard_idx)); #ifdef PADDLE_WITH_GPU_GRAPH int thread_num = _real_local_shard_num; @@ -1094,17 +1093,17 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, #endif std::atomic feasign_size_all{0}; - std::vector>> + std::vector<::paddle::framework::Channel>> busy_channel; - std::vector>> + std::vector<::paddle::framework::Channel>> free_channel; std::vector threads; for (int i = 0; i < _real_local_shard_num; i++) { busy_channel.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); free_channel.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); } threads.resize(_real_local_shard_num); @@ -1132,19 +1131,19 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, int part_num, int split_num) { if (compress && (save_param == 0 || save_param == 3)) { - return paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d.gz", - table_path, - node_num, - shard_num, - part_num, - split_num); + return ::paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d.gz", + table_path, + node_num, + shard_num, + part_num, + split_num); } else { - return paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d", - table_path, - node_num, - shard_num, - part_num, - split_num); + return ::paddle::string::format_string("%s/part-%03d-%05d-%03d-%03d", + table_path, + node_num, + shard_num, + part_num, + split_num); } }; std::shared_ptr region = nullptr; @@ -1206,7 +1205,7 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, int dim = len / sizeof(float); std::string format_value = _value_accesor->ParseToString(value, dim); - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", k, format_value.c_str()))) { LOG(FATAL) << "SSDSparseTable save failed, retry it! path:" << channel_config.path; @@ -1277,9 +1276,9 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, auto* it = _db->get_iterator(i); for (it->SeekToFirst(); it->Valid(); it->Next()) { bool need_save = _value_accesor->Save( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); _value_accesor->UpdateStatAfterSave( - paddle::string::str_to_float(it->value().data()), save_param); + ::paddle::string::str_to_float(it->value().data()), save_param); if (need_save) { uint32_t len = sizeof(uint64_t) + it->value().size() + sizeof(uint32_t); @@ -1344,10 +1343,10 @@ int32_t SSDSparseTable::SaveWithBinary(const std::string& path, } VLOG(0) << "DownpourSparseSSDTable save success, feasign size:" << feasign_size_all << " ,path:" - << paddle::string::format_string("%s/%03d/part-%03d-", - path.c_str(), - _config.table_id(), - _shard_idx) + << ::paddle::string::format_string("%s/%03d/part-%03d-", + path.c_str(), + _config.table_id(), + _shard_idx) << " from " << file_start_idx << " to " << file_start_idx + _real_local_shard_num - 1; if (_config.enable_sparse_table_cache()) { @@ -1364,7 +1363,7 @@ int64_t SSDSparseTable::CacheShuffle( double cache_threshold, std::function( int msg_type, int to_pserver_id, std::string& msg)> send_msg_func, - paddle::framework::Channel>& + ::paddle::framework::Channel>& shuffled_channel, const std::vector& table_ptrs) { LOG(INFO) << "cache shuffle with cache threshold: " << cache_threshold @@ -1381,27 +1380,27 @@ int64_t SSDSparseTable::CacheShuffle( int thread_num = _real_local_shard_num < 20 ? _real_local_shard_num : 20; std::vector< - paddle::framework::ChannelWriter>> + ::paddle::framework::ChannelWriter>> writers(_real_local_shard_num); std::vector>> datas( _real_local_shard_num); int feasign_size = 0; - std::vector>> + std::vector<::paddle::framework::Channel>> tmp_channels; for (int i = 0; i < _real_local_shard_num; ++i) { tmp_channels.push_back( - paddle::framework::MakeChannel>()); + ::paddle::framework::MakeChannel>()); } omp_set_num_threads(thread_num); #pragma omp parallel for schedule(dynamic) for (int i = 0; i < _real_local_shard_num; ++i) { - paddle::framework::ChannelWriter>& writer = - writers[i]; - // std::shared_ptr>& + writer = writers[i]; + // std::shared_ptr<::paddle::framework::ChannelObject>> tmp_chan = - // paddle::framework::MakeChannel>(); writer.Reset(tmp_channels[i].get()); @@ -1426,15 +1425,15 @@ int64_t SSDSparseTable::CacheShuffle( << _real_local_shard_num; std::vector> local_datas; for (int idx_shard = 0; idx_shard < _real_local_shard_num; ++idx_shard) { - paddle::framework::ChannelWriter>& writer = - writers[idx_shard]; + ::paddle::framework::ChannelWriter>& + writer = writers[idx_shard]; auto channel = writer.channel(); std::vector>& data = datas[idx_shard]; - std::vector ars(shuffle_node_num); + std::vector<::paddle::framework::BinaryArchive> ars(shuffle_node_num); while (channel->Read(data)) { for (auto& t : data) { auto pserver_id = - paddle::distributed::local_random_engine()() % shuffle_node_num; + ::paddle::distributed::local_random_engine()() % shuffle_node_num; if (pserver_id != _shard_idx) { ars[pserver_id] << t; } else { @@ -1465,7 +1464,7 @@ int64_t SSDSparseTable::CacheShuffle( t.wait(); } ars.clear(); - ars = std::vector(shuffle_node_num); + ars = std::vector<::paddle::framework::BinaryArchive>(shuffle_node_num); data = std::vector>(); } } @@ -1477,20 +1476,20 @@ int64_t SSDSparseTable::CacheShuffle( int32_t SSDSparseTable::SaveCache( const std::string& path, const std::string& param, - paddle::framework::Channel>& + ::paddle::framework::Channel>& shuffled_channel) { if (_shard_idx >= _config.sparse_table_cache_file_num()) { return 0; } int save_param = atoi(param.c_str()); // batch_model:0 xbox:1 - std::string table_path = paddle::string::format_string( + std::string table_path = ::paddle::string::format_string( "%s/%03d_cache/", path.c_str(), _config.table_id()); - _afs_client.remove(paddle::string::format_string( + _afs_client.remove(::paddle::string::format_string( "%s/part-%03d", table_path.c_str(), _shard_idx)); uint32_t feasign_size = 0; FsChannelConfig channel_config; // not compress cache model - channel_config.path = paddle::string::format_string( + channel_config.path = ::paddle::string::format_string( "%s/part-%03d", table_path.c_str(), _shard_idx); channel_config.converter = _value_accesor->Converter(save_param).converter; channel_config.deconverter = @@ -1502,7 +1501,7 @@ int32_t SSDSparseTable::SaveCache( while (shuffled_channel->Read(data)) { for (auto& t : data) { ++feasign_size; - if (0 != write_channel->write_line(paddle::string::format_string( + if (0 != write_channel->write_line(::paddle::string::format_string( "%lu %s", t.first, t.second.c_str()))) { LOG(ERROR) << "Cache Table save failed, " "path:" @@ -1580,7 +1579,7 @@ int32_t SSDSparseTable::LoadWithString( #endif for (int i = 0; i < _real_local_shard_num; i++) { - _fs_channel.push_back(paddle::framework::MakeChannel(30000)); + _fs_channel.push_back(::paddle::framework::MakeChannel(30000)); } std::vector threads; @@ -1598,7 +1597,7 @@ int32_t SSDSparseTable::LoadWithString( std::string line_data; auto read_channel = _afs_client.open_r(channel_config, 0, &err_no); - paddle::framework::ChannelWriter writer( + ::paddle::framework::ChannelWriter writer( _fs_channel[file_num].get()); while (read_channel->read_line(line_data) == 0 && line_data.size() > 1) { writer << line_data; @@ -1638,7 +1637,8 @@ int32_t SSDSparseTable::LoadWithString( uint64_t filter_time = 0; uint64_t filter_begin = 0; - paddle::framework::ChannelReader reader(_fs_channel[i].get()); + ::paddle::framework::ChannelReader reader( + _fs_channel[i].get()); while (reader >> line_data) { uint64_t key = std::strtoul(line_data.data(), &end, 10); @@ -1724,8 +1724,8 @@ int32_t SSDSparseTable::LoadWithBinary(const std::string& path, int param) { _value_accesor->GetAccessorInfo().mf_size / sizeof(float); // task pool _file_num_one_shard default 7 auto task_pool = std::make_shared<::ThreadPool>(_real_local_shard_num * 7); - auto filelists = _afs_client.list( - paddle::string::format_string("%s/part-%03d*", path.c_str(), _shard_idx)); + auto filelists = _afs_client.list(::paddle::string::format_string( + "%s/part-%03d*", path.c_str(), _shard_idx)); // #pragma omp parallel for schedule(dynamic) std::vector> tasks; @@ -1736,7 +1736,7 @@ int32_t SSDSparseTable::LoadWithBinary(const std::string& path, int param) { // _value_accesor->Converter(param).deconverter; for (auto& filename : filelists) { std::vector split_filename_string = - paddle::string::split_string(filename, "-"); + ::paddle::string::split_string(filename, "-"); int file_split_idx = atoi(split_filename_string[split_filename_string.size() - 1].c_str()); int file_shard_idx = @@ -1798,10 +1798,10 @@ int32_t SSDSparseTable::LoadWithBinary(const std::string& path, int param) { int use_sst = 0; if (file_split_idx != 0) { std::string path = - paddle::string::format_string("%s_%d/part-%03d.sst", - FLAGS_rocksdb_path.c_str(), - shard_idx, - file_split_idx); + ::paddle::string::format_string("%s_%d/part-%03d.sst", + FLAGS_rocksdb_path.c_str(), + shard_idx, + file_split_idx); rocksdb::Status status = sst_writer.Open(path); if (!status.ok()) { VLOG(0) << "sst writer open " << path << "failed"; @@ -1925,7 +1925,7 @@ int32_t SSDSparseTable::LoadWithBinary(const std::string& path, int param) { } tasks.clear(); for (int shard_idx = 0; shard_idx < _real_local_shard_num; shard_idx++) { - auto sst_filelist = _afs_client.list(paddle::string::format_string( + auto sst_filelist = _afs_client.list(::paddle::string::format_string( "%s_%d/part-*", FLAGS_rocksdb_path.c_str(), shard_idx)); if (!sst_filelist.empty()) { int ret = _db->ingest_externel_file(shard_idx, sst_filelist); @@ -2034,10 +2034,10 @@ int32_t SSDSparseTable::CacheTable(uint16_t pass_id) { if (!datas.empty()) { rocksdb::SstFileWriter sst_writer(rocksdb::EnvOptions(), options); std::string filename = - paddle::string::format_string("%s_%d/cache-%05d.sst", - FLAGS_rocksdb_path.c_str(), - shard_id, - cache_table_count); + ::paddle::string::format_string("%s_%d/cache-%05d.sst", + FLAGS_rocksdb_path.c_str(), + shard_id, + cache_table_count); rocksdb::Status status = sst_writer.Open(filename); if (!status.ok()) { VLOG(0) << "sst writer open " << filename << "failed" diff --git a/paddle/fluid/distributed/ps/table/ssd_sparse_table.h b/paddle/fluid/distributed/ps/table/ssd_sparse_table.h index e5561c5e42b99..c003061d81516 100644 --- a/paddle/fluid/distributed/ps/table/ssd_sparse_table.h +++ b/paddle/fluid/distributed/ps/table/ssd_sparse_table.h @@ -14,9 +14,9 @@ #pragma once -#include "gflags/gflags.h" #include "paddle/fluid/distributed/ps/table/depends/rocksdb_warpper.h" #include "paddle/fluid/distributed/ps/table/memory_sparse_table.h" +#include "paddle/utils/flags.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/distributed/ps/table/table.h b/paddle/fluid/distributed/ps/table/table.h index b64e05e3b0a11..dc44831e891ca 100644 --- a/paddle/fluid/distributed/ps/table/table.h +++ b/paddle/fluid/distributed/ps/table/table.h @@ -118,7 +118,7 @@ class Table { virtual int32_t SaveCache( const std::string &path UNUSED, const std::string ¶m UNUSED, - paddle::framework::Channel> + ::paddle::framework::Channel> &shuffled_channel UNUSED) { return 0; } @@ -130,7 +130,7 @@ class Table { std::function( int msg_type, int to_pserver_id, std::string &msg)> // NOLINT send_msg_func UNUSED, - paddle::framework::Channel> + ::paddle::framework::Channel> &shuffled_channel UNUSED, const std::vector
&table_ptrs UNUSED) { return 0; @@ -161,7 +161,7 @@ class Table { virtual int32_t InitializeAccessor(); virtual int32_t InitializeShard() = 0; virtual std::string TableDir(const std::string &model_dir) { - return paddle::string::format_string( + return ::paddle::string::format_string( "%s/%03d/", model_dir.c_str(), _config.table_id()); } diff --git a/paddle/fluid/distributed/ps/wrapper/fleet.cc b/paddle/fluid/distributed/ps/wrapper/fleet.cc index d25ad89d504fa..87eb250545a5b 100644 --- a/paddle/fluid/distributed/ps/wrapper/fleet.cc +++ b/paddle/fluid/distributed/ps/wrapper/fleet.cc @@ -30,8 +30,10 @@ const uint32_t MAX_FEASIGN_NUM = 1024 * 100 * 100; std::shared_ptr FleetWrapper::s_instance_ = NULL; bool FleetWrapper::is_initialized_ = false; -std::shared_ptr FleetWrapper::pserver_ptr_ = NULL; -std::shared_ptr FleetWrapper::worker_ptr_ = NULL; +std::shared_ptr<::paddle::distributed::PSCore> FleetWrapper::pserver_ptr_ = + NULL; +std::shared_ptr<::paddle::distributed::PSClient> FleetWrapper::worker_ptr_ = + NULL; int FleetWrapper::RegisterHeterCallback(HeterCallBackFunc handler) { VLOG(0) << "RegisterHeterCallback support later"; @@ -76,8 +78,8 @@ void FleetWrapper::InitServer( const std::vector& server_sub_program) { if (!is_initialized_) { VLOG(3) << "Going to init server"; - pserver_ptr_ = std::shared_ptr( - new paddle::distributed::PSCore()); + pserver_ptr_ = std::shared_ptr<::paddle::distributed::PSCore>( + new ::paddle::distributed::PSCore()); pserver_ptr_->InitServer(dist_desc, &host_sign_list, host_sign_list.size(), @@ -92,7 +94,7 @@ void FleetWrapper::InitServer( void FleetWrapper::InitGFlag(const std::string& gflags) { VLOG(3) << "Init With Gflags:" << gflags; - std::vector flags = paddle::string::split_string(gflags); + std::vector flags = ::paddle::string::split_string(gflags); if (flags.empty()) { flags.push_back("-max_body_size=314217728"); flags.push_back("-bthread_concurrency=40"); @@ -107,7 +109,7 @@ void FleetWrapper::InitGFlag(const std::string& gflags) { } int params_cnt = flags.size(); char** params_ptr = &(flags_ptr[0]); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr, true); + ::paddle::flags::ParseCommandLineFlags(¶ms_cnt, ¶ms_ptr); } void FleetWrapper::InitWorker(const std::string& dist_desc, @@ -116,17 +118,17 @@ void FleetWrapper::InitWorker(const std::string& dist_desc, if (!is_initialized_) { // not used, just for psclient's init // TODO(zhaocaibei123): remove this later - std::map> + std::map> dense_pull_regions; if (worker_ptr_.get() == nullptr) { - paddle::distributed::PSParameter ps_param; + ::paddle::distributed::PSParameter ps_param; google::protobuf::TextFormat::ParseFromString(dist_desc, &ps_param); InitGFlag(ps_param.init_gflags()); int servers = host_sign_list.size(); ps_env_.SetPsServers(&host_sign_list, servers); - worker_ptr_ = std::shared_ptr( - paddle::distributed::PSClientFactory::Create(ps_param)); + worker_ptr_ = std::shared_ptr<::paddle::distributed::PSClient>( + ::paddle::distributed::PSClientFactory::Create(ps_param)); worker_ptr_->Configure(ps_param, dense_pull_regions, ps_env_, index); } dist_desc_ = dist_desc; @@ -392,7 +394,7 @@ void FleetWrapper::PullDenseVarsAsync( Variable* var = scope.FindVar(varname); phi::DenseTensor* tensor = var->GetMutable(); float* w = tensor->data(); - paddle::distributed::Region reg(w, tensor->numel()); + ::paddle::distributed::Region reg(w, tensor->numel()); regions[i] = std::move(reg); } @@ -412,7 +414,7 @@ void FleetWrapper::PullDenseVarsSync( phi::DenseTensor* tensor = var->GetMutable(); if (!platform::is_gpu_place(tensor->place())) { float* w = tensor->data(); - paddle::distributed::Region reg(w, tensor->numel()); + ::paddle::distributed::Region reg(w, tensor->numel()); regions.emplace_back(std::move(reg)); } } @@ -425,14 +427,14 @@ void FleetWrapper::PushDenseParamSync( const uint64_t table_id, const std::vector& var_names) { auto place = platform::CPUPlace(); - std::vector regions; + std::vector<::paddle::distributed::Region> regions; for (auto& t : var_names) { Variable* var = scope.FindVar(t); CHECK(var != nullptr) << "var[" << t << "] not found"; phi::DenseTensor* tensor = var->GetMutable(); if (!platform::is_gpu_place(tensor->place())) { float* g = tensor->mutable_data(place); - paddle::distributed::Region reg(g, tensor->numel()); + ::paddle::distributed::Region reg(g, tensor->numel()); regions.emplace_back(std::move(reg)); } } @@ -456,7 +458,7 @@ void FleetWrapper::PushDenseVarsAsync( float scale_datanorm, int batch_size) { auto place = platform::CPUPlace(); - std::vector regions; + std::vector<::paddle::distributed::Region> regions; for (auto& t : var_names) { Variable* var = scope.FindVar(t); CHECK(var != nullptr) << "var[" << t << "] not found"; @@ -479,7 +481,7 @@ void FleetWrapper::PushDenseVarsAsync( } } - paddle::distributed::Region reg(g, tensor->numel()); + ::paddle::distributed::Region reg(g, tensor->numel()); regions.emplace_back(std::move(reg)); VLOG(3) << "FleetWrapper::PushDenseVarsAsync Var " << t << " talbe_id " << table_id << " Temp_data[0] " << g[0] << " Temp_data[-1] " @@ -774,7 +776,7 @@ void FleetWrapper::ShrinkDenseTable(int table_id, std::vector var_list, float decay, int emb_dim) { - std::vector regions; + std::vector<::paddle::distributed::Region> regions; for (std::string& name : var_list) { if (name.find("batch_sum") != std::string::npos) { Variable* var = scope->FindVar(name); @@ -795,14 +797,14 @@ void FleetWrapper::ShrinkDenseTable(int table_id, for (int k = 0; k < tensor->numel(); k += emb_dim) { g[k] = g[k] + g_size[k] * log(decay); } - paddle::distributed::Region reg(g, tensor->numel()); + ::paddle::distributed::Region reg(g, tensor->numel()); regions.emplace_back(std::move(reg)); } else { Variable* var = scope->FindVar(name); CHECK(var != nullptr) << "var[" << name << "] not found"; phi::DenseTensor* tensor = var->GetMutable(); float* g = tensor->data(); - paddle::distributed::Region reg(g, tensor->numel()); + ::paddle::distributed::Region reg(g, tensor->numel()); regions.emplace_back(std::move(reg)); } } diff --git a/paddle/fluid/distributed/ps/wrapper/fleet.h b/paddle/fluid/distributed/ps/wrapper/fleet.h index 9bf6f3c84a945..22dc0f1af724a 100644 --- a/paddle/fluid/distributed/ps/wrapper/fleet.h +++ b/paddle/fluid/distributed/ps/wrapper/fleet.h @@ -295,7 +295,7 @@ class FleetWrapper { // FleetWrapper singleton static std::shared_ptr GetInstance() { if (NULL == s_instance_) { - s_instance_.reset(new paddle::distributed::FleetWrapper()); + s_instance_.reset(new ::paddle::distributed::FleetWrapper()); } return s_instance_; } @@ -322,13 +322,13 @@ class FleetWrapper { std::string PullFlStrategy(); //********** - static std::shared_ptr pserver_ptr_; - static std::shared_ptr worker_ptr_; + static std::shared_ptr<::paddle::distributed::PSCore> pserver_ptr_; + static std::shared_ptr<::paddle::distributed::PSClient> worker_ptr_; private: static std::shared_ptr s_instance_; std::string dist_desc_; - paddle::distributed::PaddlePSEnvironment ps_env_; + ::paddle::distributed::PaddlePSEnvironment ps_env_; size_t GetAbsoluteSum(size_t start, size_t end, size_t level, @@ -336,7 +336,7 @@ class FleetWrapper { protected: static bool is_initialized_; - std::map> regions_; + std::map> regions_; bool scale_sparse_gradient_with_batch_size_; int32_t sleep_seconds_before_fail_exit_; int client2client_request_timeout_ms_; diff --git a/paddle/fluid/distributed/rpc/CMakeLists.txt b/paddle/fluid/distributed/rpc/CMakeLists.txt index 76c6dc001104c..4042a6fe3ccfe 100644 --- a/paddle/fluid/distributed/rpc/CMakeLists.txt +++ b/paddle/fluid/distributed/rpc/CMakeLists.txt @@ -12,17 +12,7 @@ set_source_files_properties( set_source_files_properties(rpc_agent.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -set(PADDLE_RPC_DEPS - brpc - ssl - crypto - protobuf - zlib - leveldb - snappy - phi - glog - pybind) +set(PADDLE_RPC_DEPS ${EXTERNAL_BRPC_DEPS} zlib phi pybind) proto_library(paddle_rpc_proto SRCS rpc.proto) cc_library( paddle_rpc diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 182c83cc37bde..055163ed6206b 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -228,13 +228,13 @@ cc_test( set(BRPC_DEPS "") if(WITH_PSCORE) - set(BRPC_DEPS brpc ssl crypto) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS}) endif() if(WITH_PSLIB) if(WITH_PSLIB_BRPC) set(BRPC_DEPS pslib_brpc) elseif(NOT WITH_HETERPS) - set(BRPC_DEPS brpc ssl crypto) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS}) endif() if(WITH_ARM_BRPC) set(BRPC_DEPS arm_brpc) @@ -833,7 +833,7 @@ if(WITH_DISTRIBUTE) heter_service_proto fleet heter_server - brpc + ${${EXTERNAL_BRPC_DEPS}} phi) set(DISTRIBUTE_COMPILE_FLAGS "") if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) diff --git a/paddle/fluid/framework/async_executor.cc b/paddle/fluid/framework/async_executor.cc index c5d898beba1ae..055381f0d58aa 100644 --- a/paddle/fluid/framework/async_executor.cc +++ b/paddle/fluid/framework/async_executor.cc @@ -14,7 +14,6 @@ limitations under the License. */ #include "paddle/fluid/framework/async_executor.h" -#include "gflags/gflags.h" #include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" @@ -32,6 +31,7 @@ limitations under the License. */ #include "paddle/fluid/inference/io.h" #include "paddle/fluid/platform/place.h" #include "paddle/fluid/pybind/pybind.h" +#include "paddle/utils/flags.h" // phi #include "paddle/phi/kernels/declarations.h" diff --git a/paddle/fluid/framework/copy_same_tensor_test.cc b/paddle/fluid/framework/copy_same_tensor_test.cc index 10e0b76f00459..9b892c0c1b092 100644 --- a/paddle/fluid/framework/copy_same_tensor_test.cc +++ b/paddle/fluid/framework/copy_same_tensor_test.cc @@ -16,15 +16,15 @@ #include -#include "gflags/gflags.h" #include "gtest/gtest.h" #include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/ddim.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_system_allocator); +PD_DECLARE_bool(use_system_allocator); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/details/bkcl_op_handle.h b/paddle/fluid/framework/details/bkcl_op_handle.h index 4ca8bf4cb5874..8a5afcf04bf9a 100644 --- a/paddle/fluid/framework/details/bkcl_op_handle.h +++ b/paddle/fluid/framework/details/bkcl_op_handle.h @@ -24,7 +24,7 @@ #include "paddle/fluid/platform/device/xpu/bkcl_helper.h" #include "xpu/bkcl.h" -DECLARE_bool(sync_bkcl_allreduce); +PD_DECLARE_bool(sync_bkcl_allreduce); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/details/build_strategy.cc b/paddle/fluid/framework/details/build_strategy.cc index d295ff6ad5a68..5a6f4e6e70d4c 100644 --- a/paddle/fluid/framework/details/build_strategy.cc +++ b/paddle/fluid/framework/details/build_strategy.cc @@ -22,10 +22,10 @@ limitations under the License. */ #include "paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.h" #include "paddle/phi/core/flags.h" -DECLARE_bool(convert_all_blocks); +PD_DECLARE_bool(convert_all_blocks); PHI_DECLARE_bool(use_mkldnn); #ifdef PADDLE_WITH_CINN -DECLARE_bool(use_cinn); +PD_DECLARE_bool(use_cinn); #endif namespace paddle { diff --git a/paddle/fluid/framework/details/build_strategy_test.cc b/paddle/fluid/framework/details/build_strategy_test.cc index 7ec7d93ee6610..0990f134b3e1b 100644 --- a/paddle/fluid/framework/details/build_strategy_test.cc +++ b/paddle/fluid/framework/details/build_strategy_test.cc @@ -30,7 +30,7 @@ #include "paddle/fluid/framework/var_type_inference.h" #include "paddle/fluid/platform/place.h" -DECLARE_bool(convert_all_blocks); +PD_DECLARE_bool(convert_all_blocks); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc index 6642866150312..a075b4702e946 100644 --- a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc +++ b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc @@ -21,7 +21,7 @@ #include "paddle/phi/backends/device_memory_aligment.h" #include "paddle/phi/core/flags.h" -DEFINE_bool(skip_fused_all_reduce_check, false, ""); // NOLINT +PD_DEFINE_bool(skip_fused_all_reduce_check, false, ""); // NOLINT PHI_DECLARE_bool(allreduce_record_one_event); namespace paddle { diff --git a/paddle/fluid/framework/executor.cc b/paddle/fluid/framework/executor.cc index 50c8cc926dc7f..f18705ef099ab 100644 --- a/paddle/fluid/framework/executor.cc +++ b/paddle/fluid/framework/executor.cc @@ -31,7 +31,7 @@ limitations under the License. */ #include "paddle/fluid/framework/executor_gc_helper.h" #include "paddle/phi/core/flags.h" -DECLARE_bool(benchmark); +PD_DECLARE_bool(benchmark); PHI_DECLARE_bool(use_mkldnn); namespace paddle { diff --git a/paddle/fluid/framework/executor_thread_worker.cc b/paddle/fluid/framework/executor_thread_worker.cc index 4bbcba2151b26..a3fb850373a5a 100644 --- a/paddle/fluid/framework/executor_thread_worker.cc +++ b/paddle/fluid/framework/executor_thread_worker.cc @@ -17,7 +17,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/message.h" #include "google/protobuf/text_format.h" @@ -34,6 +33,7 @@ limitations under the License. */ #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/timer.h" #include "paddle/fluid/pybind/pybind.h" +#include "paddle/utils/flags.h" // phi #include "paddle/phi/kernels/declarations.h" diff --git a/paddle/fluid/framework/fleet/CMakeLists.txt b/paddle/fluid/framework/fleet/CMakeLists.txt index 7ebc58e61b588..e2ba9c6d4f43e 100644 --- a/paddle/fluid/framework/fleet/CMakeLists.txt +++ b/paddle/fluid/framework/fleet/CMakeLists.txt @@ -3,7 +3,7 @@ if(WITH_PSLIB) set(BRPC_DEPS pslib_brpc) else() if(NOT WITH_HETERPS) - set(BRPC_DEPS brpc) + set(BRPC_DEPS ${EXTERNAL_BRPC_DEPS}) endif() endif() cc_library( diff --git a/paddle/fluid/framework/garbage_collector.cc b/paddle/fluid/framework/garbage_collector.cc index 3a514d26ddb47..01f1baf9be191 100644 --- a/paddle/fluid/framework/garbage_collector.cc +++ b/paddle/fluid/framework/garbage_collector.cc @@ -16,10 +16,10 @@ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) #include "paddle/fluid/platform/cuda_device_guard.h" #endif -#include "gflags/gflags.h" #include "paddle/fluid/framework/garbage_collector.h" #include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" PHI_DECLARE_double(eager_delete_tensor_gb); PHI_DECLARE_double(memory_fraction_of_eager_deletion); diff --git a/paddle/fluid/framework/garbage_collector.h b/paddle/fluid/framework/garbage_collector.h index ec5766882e664..fa45bbcbdd231 100644 --- a/paddle/fluid/framework/garbage_collector.h +++ b/paddle/fluid/framework/garbage_collector.h @@ -20,9 +20,9 @@ #include // NOLINT #include -#include "gflags/gflags.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/stream_callback_manager.h" +#include "paddle/utils/flags.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/ir/graph.h b/paddle/fluid/framework/ir/graph.h index 3eb2df7011c7e..3596f4e0f0e29 100644 --- a/paddle/fluid/framework/ir/graph.h +++ b/paddle/fluid/framework/ir/graph.h @@ -14,8 +14,6 @@ limitations under the License. */ #pragma once -#include - #include #include #include @@ -27,8 +25,9 @@ limitations under the License. */ #include "paddle/fluid/platform/enforce.h" #include "paddle/utils/any.h" +#include "paddle/utils/flags.h" -DECLARE_bool(convert_all_blocks); +PD_DECLARE_bool(convert_all_blocks); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/ir/graph_helper.cc b/paddle/fluid/framework/ir/graph_helper.cc index aee64ba89ac0d..87a710cd036af 100644 --- a/paddle/fluid/framework/ir/graph_helper.cc +++ b/paddle/fluid/framework/ir/graph_helper.cc @@ -29,7 +29,7 @@ limitations under the License. */ #include "paddle/fluid/platform/collective_helper.h" #endif #include "paddle/fluid/platform/flags.h" -DECLARE_bool(convert_all_blocks); +PD_DECLARE_bool(convert_all_blocks); PADDLE_DEFINE_EXPORTED_string(print_sub_graph_dir, "", "FLAGS_print_sub_graph_dir is used " diff --git a/paddle/fluid/framework/ir/graph_to_program_pass.cc b/paddle/fluid/framework/ir/graph_to_program_pass.cc index f57cdd9d9746c..e1ed5ff5b041f 100644 --- a/paddle/fluid/framework/ir/graph_to_program_pass.cc +++ b/paddle/fluid/framework/ir/graph_to_program_pass.cc @@ -14,11 +14,10 @@ limitations under the License. */ #include "paddle/fluid/framework/ir/graph_to_program_pass.h" -#include - #include #include "paddle/fluid/framework/op_proto_maker.h" +#include "paddle/utils/flags.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/new_executor/executor_statistics.cc b/paddle/fluid/framework/new_executor/executor_statistics.cc index 14436e9c76358..9d789c9957e6c 100644 --- a/paddle/fluid/framework/new_executor/executor_statistics.cc +++ b/paddle/fluid/framework/new_executor/executor_statistics.cc @@ -28,7 +28,7 @@ #include "paddle/fluid/platform/os_info.h" #include "paddle/fluid/platform/profiler/utils.h" -DECLARE_bool(use_stream_safe_cuda_allocator); +PD_DECLARE_bool(use_stream_safe_cuda_allocator); PADDLE_DEFINE_EXPORTED_string(static_executor_perfstat_filepath, "", "FLAGS_static_executor_perfstat_filepath " diff --git a/paddle/fluid/framework/new_executor/interpreter/dependency_builder.h b/paddle/fluid/framework/new_executor/interpreter/dependency_builder.h index 4e3fb8d1b2450..18a26ea770cec 100644 --- a/paddle/fluid/framework/new_executor/interpreter/dependency_builder.h +++ b/paddle/fluid/framework/new_executor/interpreter/dependency_builder.h @@ -19,7 +19,7 @@ #include "paddle/fluid/framework/new_executor/new_executor_defs.h" -DECLARE_bool(new_executor_sequential_run); +PD_DECLARE_bool(new_executor_sequential_run); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/new_executor/interpreter/execution_config.cc b/paddle/fluid/framework/new_executor/interpreter/execution_config.cc index 1e6a6f02e2230..cf3195bb8c296 100644 --- a/paddle/fluid/framework/new_executor/interpreter/execution_config.cc +++ b/paddle/fluid/framework/new_executor/interpreter/execution_config.cc @@ -22,7 +22,7 @@ #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/backends/xpu/xpu_info.h" -DECLARE_bool(new_executor_serial_run); +PD_DECLARE_bool(new_executor_serial_run); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/new_executor/interpreter_base_impl.h b/paddle/fluid/framework/new_executor/interpreter_base_impl.h index 934dd44771e7f..88ac481d16c5b 100644 --- a/paddle/fluid/framework/new_executor/interpreter_base_impl.h +++ b/paddle/fluid/framework/new_executor/interpreter_base_impl.h @@ -20,8 +20,8 @@ #include #include #include -#include "gflags/gflags.h" #include "paddle/fluid/platform/flags.h" +#include "paddle/utils/flags.h" #include "paddle/fluid/framework/details/exception_holder.h" #include "paddle/fluid/framework/new_executor/garbage_collector/garbage_collector.h" @@ -38,14 +38,14 @@ #include "paddle/fluid/platform/device_event.h" #include "paddle/phi/backends/device_manager.h" -DECLARE_bool(new_executor_serial_run); -DECLARE_bool(new_executor_static_build); -DECLARE_bool(new_executor_use_inplace); -DECLARE_bool(new_executor_use_local_scope); +PD_DECLARE_bool(new_executor_serial_run); +PD_DECLARE_bool(new_executor_static_build); +PD_DECLARE_bool(new_executor_use_inplace); +PD_DECLARE_bool(new_executor_use_local_scope); PHI_DECLARE_bool(check_nan_inf); -DECLARE_bool(benchmark); -DECLARE_uint64(executor_log_deps_every_microseconds); +PD_DECLARE_bool(benchmark); +PD_DECLARE_uint64(executor_log_deps_every_microseconds); PHI_DECLARE_bool(new_executor_use_cuda_graph); PHI_DECLARE_bool(enable_new_ir_in_executor); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) diff --git a/paddle/fluid/framework/new_executor/interpretercore.h b/paddle/fluid/framework/new_executor/interpretercore.h index 191f0b92eb8b2..f01c12b27c3a1 100644 --- a/paddle/fluid/framework/new_executor/interpretercore.h +++ b/paddle/fluid/framework/new_executor/interpretercore.h @@ -15,7 +15,7 @@ #include "paddle/fluid/framework/new_executor/interpreter_base_impl.h" -DECLARE_bool(new_executor_use_local_scope); +PD_DECLARE_bool(new_executor_use_local_scope); namespace ir { class Program; diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index df9f398770e28..e026e914adb57 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -16,7 +16,7 @@ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/fluid/framework/details/nan_inf_utils.h" #include "paddle/fluid/framework/details/share_tensor_buffer_functor.h" diff --git a/paddle/fluid/framework/op_kernel_type.h b/paddle/fluid/framework/op_kernel_type.h index c7193c68d3c9c..558ae8f9b9078 100644 --- a/paddle/fluid/framework/op_kernel_type.h +++ b/paddle/fluid/framework/op_kernel_type.h @@ -16,7 +16,6 @@ limitations under the License. */ #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/data_layout.h" #include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/framework/library_type.h" @@ -25,8 +24,9 @@ limitations under the License. */ #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/kernel_factory.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 65a150b7ccbdf..0c03486fdd750 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -17,7 +17,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/data_transform.h" #include "paddle/fluid/framework/data_type_transform.h" @@ -43,6 +42,7 @@ limitations under the License. */ #include "paddle/phi/core/kernel_context.h" #include "paddle/phi/core/kernel_factory.h" #include "paddle/phi/ops/compat/signatures.h" +#include "paddle/utils/flags.h" namespace phi { class DenseTensor; @@ -62,9 +62,9 @@ class DenseTensor; #include "paddle/fluid/platform/device/gpu/gpu_dnn.h" #endif -DECLARE_bool(benchmark); +PD_DECLARE_bool(benchmark); PHI_DECLARE_bool(check_nan_inf); -DECLARE_bool(enable_unused_var_check); +PD_DECLARE_bool(enable_unused_var_check); PHI_DECLARE_bool(run_kp_kernel); PHI_DECLARE_bool(enable_host_event_recorder_hook); diff --git a/paddle/fluid/framework/operator_test.cc b/paddle/fluid/framework/operator_test.cc index 1d57efd875f06..baca5b3f06743 100644 --- a/paddle/fluid/framework/operator_test.cc +++ b/paddle/fluid/framework/operator_test.cc @@ -19,7 +19,7 @@ limitations under the License. */ #include "paddle/fluid/platform/errors.h" #include "paddle/fluid/platform/init.h" -DECLARE_bool(enable_unused_var_check); +PD_DECLARE_bool(enable_unused_var_check); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc b/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc index 9e2da446b2660..fd0dcff440b5d 100644 --- a/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc +++ b/paddle/fluid/framework/paddle2cinn/build_cinn_pass.cc @@ -24,7 +24,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/cinn/frontend/op_mapper_registry.h" #include "paddle/cinn/frontend/op_mappers/use_op_mappers.h" @@ -38,9 +37,10 @@ limitations under the License. */ #include "paddle/fluid/operators/cinn/cinn_launch_op.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/errors.h" +#include "paddle/utils/flags.h" -DECLARE_string(allow_cinn_ops); -DECLARE_string(deny_cinn_ops); +PD_DECLARE_string(allow_cinn_ops); +PD_DECLARE_string(deny_cinn_ops); namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc index 29ea6098088b6..e0ddafd37da70 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler.cc @@ -22,7 +22,6 @@ #include #include -#include "gflags/gflags.h" #include "paddle/cinn/auto_schedule/auto_tuner.h" #include "paddle/cinn/auto_schedule/tuning.h" #include "paddle/cinn/common/target.h" @@ -52,6 +51,7 @@ #include "paddle/ir/core/program.h" #include "paddle/ir/core/value.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" PHI_DECLARE_bool(enable_pe_launch_cinn); PHI_DECLARE_bool(enable_cinn_auto_tune); diff --git a/paddle/fluid/framework/paddle2cinn/cinn_compiler_test.cc b/paddle/fluid/framework/paddle2cinn/cinn_compiler_test.cc index 4c66bc787ef89..519b78115748b 100644 --- a/paddle/fluid/framework/paddle2cinn/cinn_compiler_test.cc +++ b/paddle/fluid/framework/paddle2cinn/cinn_compiler_test.cc @@ -23,7 +23,6 @@ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/cinn/common/target.h" @@ -38,6 +37,7 @@ #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" PHI_DECLARE_string(allow_cinn_ops); PHI_DECLARE_string(deny_cinn_ops); diff --git a/paddle/fluid/framework/scope.cc b/paddle/fluid/framework/scope.cc index 36b40657bb2b4..744ce8923a2d5 100644 --- a/paddle/fluid/framework/scope.cc +++ b/paddle/fluid/framework/scope.cc @@ -17,7 +17,7 @@ limitations under the License. */ #include "glog/logging.h" #include "paddle/fluid/framework/threadpool.h" #include "paddle/fluid/platform/flags.h" -DECLARE_bool(benchmark); +PD_DECLARE_bool(benchmark); PADDLE_DEFINE_EXPORTED_bool( eager_delete_scope, diff --git a/paddle/fluid/framework/unused_var_check.h b/paddle/fluid/framework/unused_var_check.h index cc4977e439c4c..55a3a020a06b5 100644 --- a/paddle/fluid/framework/unused_var_check.h +++ b/paddle/fluid/framework/unused_var_check.h @@ -19,7 +19,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/imperative/prepared_operator.cc b/paddle/fluid/imperative/prepared_operator.cc index 11a4d37d799f2..d336488a42327 100644 --- a/paddle/fluid/imperative/prepared_operator.cc +++ b/paddle/fluid/imperative/prepared_operator.cc @@ -36,7 +36,7 @@ #include "paddle/phi/core/flags.h" PHI_DECLARE_bool(check_nan_inf); -DECLARE_bool(benchmark); +PD_DECLARE_bool(benchmark); PHI_DECLARE_bool(run_kp_kernel); namespace paddle { diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index f1374bc8f7bd7..bd3bbf2108efc 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -38,7 +38,7 @@ PHI_DECLARE_bool(use_mkldnn); PHI_DECLARE_string(tracer_mkldnn_ops_on); PHI_DECLARE_string(tracer_mkldnn_ops_off); -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); namespace paddle { namespace imperative { diff --git a/paddle/fluid/imperative/tracer.h b/paddle/fluid/imperative/tracer.h index 5e426724aaec3..49b94c743fdb8 100644 --- a/paddle/fluid/imperative/tracer.h +++ b/paddle/fluid/imperative/tracer.h @@ -32,7 +32,7 @@ #include "paddle/fluid/platform/macros.h" #include "paddle/phi/core/compat/arg_map_context.h" -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); namespace paddle { namespace imperative { diff --git a/paddle/fluid/inference/CMakeLists.txt b/paddle/fluid/inference/CMakeLists.txt index 7b6175a975676..48c9f79f34de1 100644 --- a/paddle/fluid/inference/CMakeLists.txt +++ b/paddle/fluid/inference/CMakeLists.txt @@ -37,6 +37,10 @@ get_property(ir_targets GLOBAL PROPERTY IR_TARGETS) get_property(not_infer_modules GLOBAL PROPERTY NOT_INFER_MODULES) set(utils_modules pretty_log string_helper benchmark utf8proc) +if(NOT WITH_GFLAGS) + set(utils_modules ${utils_modules} paddle_flags) +endif() + add_subdirectory(api) # Create static inference library if needed diff --git a/paddle/fluid/inference/analysis/analyzer.h b/paddle/fluid/inference/analysis/analyzer.h index 3c05328366674..60f86ba10eb19 100644 --- a/paddle/fluid/inference/analysis/analyzer.h +++ b/paddle/fluid/inference/analysis/analyzer.h @@ -37,8 +37,8 @@ limitations under the License. */ #include -#include "gflags/gflags.h" #include "paddle/fluid/inference/analysis/analysis_pass.h" +#include "paddle/utils/flags.h" namespace paddle { namespace inference { diff --git a/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc b/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc index fa35ffc45c2a8..221b25cae0099 100644 --- a/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc +++ b/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc @@ -27,7 +27,7 @@ #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/dense_tensor.h" -DEFINE_bool( // NOLINT +PD_DEFINE_bool( // NOLINT custom_model_save_cpu, false, "Keep old mode for developers, the model is saved on cpu not device."); diff --git a/paddle/fluid/inference/analysis/ut_helper.h b/paddle/fluid/inference/analysis/ut_helper.h index 6c7690a4779bf..29d123d44ad45 100644 --- a/paddle/fluid/inference/analysis/ut_helper.h +++ b/paddle/fluid/inference/analysis/ut_helper.h @@ -18,9 +18,9 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/executor.h" #include "paddle/fluid/inference/analysis/helper.h" +#include "paddle/utils/flags.h" namespace paddle { namespace inference { @@ -30,7 +30,7 @@ extern void ReadBinaryFile(const std::string& filename, std::string* contents); namespace analysis { -DEFINE_string(inference_model_dir, "", "inference test model dir"); +PD_DEFINE_string(inference_model_dir, "", "inference test model dir"); } // namespace analysis } // namespace inference diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 7d60d203f2b28..1fb7e2c157134 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -1708,10 +1708,10 @@ CreatePaddlePredictor( auto SetGflags = [](const AnalysisConfig &config) { auto SetGflag = [](const char *name, const char *value) { - std::string ret = ::GFLAGS_NAMESPACE::SetCommandLineOption(name, value); + bool success = paddle::flags::SetFlagValue(name, value); PADDLE_ENFORCE_EQ( - ret.empty(), - false, + success, + true, platform::errors::InvalidArgument( "Fail to set gflag: %s, please make sure the gflag exists.", name)); @@ -3089,8 +3089,8 @@ std::tuple GetTrtRuntimeVersion() { #endif } -std::string UpdateDllFlag(const char *name, const char *value) { - return paddle::UpdateDllFlag(name, value); +void UpdateDllFlag(const char *name, const char *value) { + paddle::UpdateDllFlag(name, value); } void ConvertToMixedPrecision(const std::string &model_file, diff --git a/paddle/fluid/inference/api/api.cc b/paddle/fluid/inference/api/api.cc index 93b88632984a8..a15f8be18bf34 100644 --- a/paddle/fluid/inference/api/api.cc +++ b/paddle/fluid/inference/api/api.cc @@ -14,13 +14,13 @@ #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/commit.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/scope.h" #include "paddle/fluid/inference/api/paddle_inference_api.h" #include "paddle/fluid/inference/api/paddle_pass_builder.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/utils/flags.h" namespace paddle { @@ -134,20 +134,18 @@ std::string get_version() { return ss.str(); } -std::string UpdateDllFlag(const char *name, const char *value) { +void UpdateDllFlag(const char *name, const char *value) { std::string ret; LOG(WARNING) << "The function \"UpdateDllFlag\" is only used to update the flag " "on the Windows shared library"; - ret = ::GFLAGS_NAMESPACE::SetCommandLineOption(name, value); + bool success = paddle::flags::SetFlagValue(name, value); PADDLE_ENFORCE_EQ( - ret.empty(), - false, + success, + true, platform::errors::InvalidArgument( "Fail to update flag: %s, please make sure the flag exists.", name)); - LOG(INFO) << ret; - return ret; } #ifdef PADDLE_WITH_CRYPTO diff --git a/paddle/fluid/inference/api/api_impl.cc b/paddle/fluid/inference/api/api_impl.cc index a2c2d099d771a..d318042719a16 100644 --- a/paddle/fluid/inference/api/api_impl.cc +++ b/paddle/fluid/inference/api/api_impl.cc @@ -26,7 +26,7 @@ limitations under the License. */ #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/profiler.h" -DEFINE_bool(profile, false, "Turn on profiler for fluid"); // NOLINT +PD_DEFINE_bool(profile, false, "Turn on profiler for fluid"); // NOLINT namespace paddle { namespace { @@ -373,7 +373,6 @@ CreatePaddlePredictor( std::vector flags; if (config.fraction_of_gpu_memory >= 0.0f || config.fraction_of_gpu_memory <= 0.95f) { - flags.emplace_back("dummpy"); std::string flag = "--fraction_of_gpu_memory_to_use=" + num2str(config.fraction_of_gpu_memory); flags.push_back(flag); diff --git a/paddle/fluid/inference/api/demo_ci/onnxruntime_mobilenet_demo.cc b/paddle/fluid/inference/api/demo_ci/onnxruntime_mobilenet_demo.cc index fb5cee4e05034..04310139e5d02 100644 --- a/paddle/fluid/inference/api/demo_ci/onnxruntime_mobilenet_demo.cc +++ b/paddle/fluid/inference/api/demo_ci/onnxruntime_mobilenet_demo.cc @@ -87,7 +87,7 @@ void Main() { } // namespace paddle int main(int argc, char** argv) { - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); paddle::demo::Main(); return 0; } diff --git a/paddle/fluid/inference/api/demo_ci/simple_on_word2vec.cc b/paddle/fluid/inference/api/demo_ci/simple_on_word2vec.cc index 66f2bc7056a66..dca147e8353e3 100644 --- a/paddle/fluid/inference/api/demo_ci/simple_on_word2vec.cc +++ b/paddle/fluid/inference/api/demo_ci/simple_on_word2vec.cc @@ -133,7 +133,7 @@ void MainThreads(int num_threads, bool use_gpu) { } // namespace paddle int main(int argc, char** argv) { - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); paddle::demo::Main(false /* use_gpu*/); paddle::demo::MainThreads(1, false /* use_gpu*/); paddle::demo::MainThreads(4, false /* use_gpu*/); diff --git a/paddle/fluid/inference/api/demo_ci/trt_mobilenet_demo.cc b/paddle/fluid/inference/api/demo_ci/trt_mobilenet_demo.cc index b6b20a901b2bb..b0f05d4f268c0 100644 --- a/paddle/fluid/inference/api/demo_ci/trt_mobilenet_demo.cc +++ b/paddle/fluid/inference/api/demo_ci/trt_mobilenet_demo.cc @@ -73,7 +73,7 @@ void Main() { } // namespace paddle int main(int argc, char** argv) { - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); paddle::demo::Main(); return 0; } diff --git a/paddle/fluid/inference/api/demo_ci/vis_demo.cc b/paddle/fluid/inference/api/demo_ci/vis_demo.cc index 7850b4edb1098..022c1249af001 100644 --- a/paddle/fluid/inference/api/demo_ci/vis_demo.cc +++ b/paddle/fluid/inference/api/demo_ci/vis_demo.cc @@ -28,9 +28,6 @@ DEFINE_string(data, "path of data; each line is a record, format is " "'\t MakeCipher( const std::string& config_file); diff --git a/paddle/fluid/inference/api/paddle_inference_api.h b/paddle/fluid/inference/api/paddle_inference_api.h index 18b8b6dfd43ea..0366a33a5f5e0 100644 --- a/paddle/fluid/inference/api/paddle_inference_api.h +++ b/paddle/fluid/inference/api/paddle_inference_api.h @@ -235,7 +235,7 @@ PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype); PD_INFER_DECL std::string GetVersion(); PD_INFER_DECL std::tuple GetTrtCompileVersion(); PD_INFER_DECL std::tuple GetTrtRuntimeVersion(); -PD_INFER_DECL std::string UpdateDllFlag(const char* name, const char* value); +PD_INFER_DECL void UpdateDllFlag(const char* name, const char* value); PD_INFER_DECL void ConvertToMixedPrecision( const std::string& model_file, diff --git a/paddle/fluid/inference/io.cc b/paddle/fluid/inference/io.cc index fe502a15798aa..000d4f5430ed4 100644 --- a/paddle/fluid/inference/io.cc +++ b/paddle/fluid/inference/io.cc @@ -29,12 +29,12 @@ limitations under the License. */ // phi #include "paddle/phi/kernels/declarations.h" -DEFINE_string(devices, // NOLINT - "", - "The devices to be used which is joined by comma."); -DEFINE_int32(math_num_threads, - 1, - "Number of threads used to run math functions."); +PD_DEFINE_string(devices, // NOLINT + "", + "The devices to be used which is joined by comma."); +PD_DEFINE_int32(math_num_threads, + 1, + "Number of threads used to run math functions."); namespace paddle { namespace inference { diff --git a/paddle/fluid/inference/paddle_inference.map b/paddle/fluid/inference/paddle_inference.map index 93d90238e340a..191f5934166c4 100644 --- a/paddle/fluid/inference/paddle_inference.map +++ b/paddle/fluid/inference/paddle_inference.map @@ -45,6 +45,7 @@ *paddle::RegisterSymbolsFor*; *paddle::from_blob*; *paddle::InitPhi*; + *paddle::flags*; /* ut needs the following symbol, we need to modify all the ut to hidden such symbols */ diff --git a/paddle/fluid/inference/tensorrt/plugin/trt_plugin.h b/paddle/fluid/inference/tensorrt/plugin/trt_plugin.h index f08a8a75ba406..b939dfaadc94d 100644 --- a/paddle/fluid/inference/tensorrt/plugin/trt_plugin.h +++ b/paddle/fluid/inference/tensorrt/plugin/trt_plugin.h @@ -31,7 +31,7 @@ namespace nvinfer1 { class ITensor; } // namespace nvinfer1 -DECLARE_bool(profile); +PD_DECLARE_bool(profile); namespace paddle { namespace inference { diff --git a/paddle/fluid/memory/allocation/allocator_facade_abs_flags_test.cc b/paddle/fluid/memory/allocation/allocator_facade_abs_flags_test.cc index 1e09c43c4f12f..21f734c727266 100644 --- a/paddle/fluid/memory/allocation/allocator_facade_abs_flags_test.cc +++ b/paddle/fluid/memory/allocation/allocator_facade_abs_flags_test.cc @@ -22,7 +22,7 @@ PHI_DECLARE_double(fraction_of_gpu_memory_to_use); PHI_DECLARE_double(fraction_of_cuda_pinned_memory_to_use); PHI_DECLARE_uint64(initial_gpu_memory_in_mb); PHI_DECLARE_uint64(reallocate_gpu_memory_in_mb); -DECLARE_int64(gpu_allocator_retry_time); +PD_DECLARE_int64(gpu_allocator_retry_time); #endif PHI_DECLARE_string(allocator_strategy); diff --git a/paddle/fluid/memory/allocation/allocator_facade_frac_flags_test.cc b/paddle/fluid/memory/allocation/allocator_facade_frac_flags_test.cc index 63e3eab3256c9..049807f63888b 100644 --- a/paddle/fluid/memory/allocation/allocator_facade_frac_flags_test.cc +++ b/paddle/fluid/memory/allocation/allocator_facade_frac_flags_test.cc @@ -22,7 +22,7 @@ PHI_DECLARE_double(fraction_of_gpu_memory_to_use); PHI_DECLARE_double(fraction_of_cuda_pinned_memory_to_use); PHI_DECLARE_uint64(initial_gpu_memory_in_mb); PHI_DECLARE_uint64(reallocate_gpu_memory_in_mb); -DECLARE_int64(gpu_allocator_retry_time); +PD_DECLARE_int64(gpu_allocator_retry_time); #endif PHI_DECLARE_string(allocator_strategy); diff --git a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_facade_test.cc b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_facade_test.cc index bfd05b6b323fe..15dc505517907 100644 --- a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_facade_test.cc +++ b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_facade_test.cc @@ -26,7 +26,7 @@ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PHI_DECLARE_double(fraction_of_gpu_memory_to_use); PHI_DECLARE_double(fraction_of_cuda_pinned_memory_to_use); -DECLARE_int64(gpu_allocator_retry_time); +PD_DECLARE_int64(gpu_allocator_retry_time); #endif PHI_DECLARE_string(allocator_strategy); diff --git a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_test.cc b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_test.cc index 890ebde2aa3c9..350cdbd9b4c24 100644 --- a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_test.cc +++ b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_test.cc @@ -19,8 +19,8 @@ #include "gtest/gtest.h" #include "paddle/fluid/memory/allocation/aligned_allocator.h" -DECLARE_bool(free_idle_chunk); -DECLARE_bool(free_when_no_cache_hit); +PD_DECLARE_bool(free_idle_chunk); +PD_DECLARE_bool(free_when_no_cache_hit); namespace paddle { namespace memory { diff --git a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc index 09053ec5dede7..1493913f5b2a8 100644 --- a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc @@ -41,7 +41,7 @@ PADDLE_DEFINE_EXPORTED_bool( PHI_DECLARE_double(fraction_of_gpu_memory_to_use); PHI_DECLARE_uint64(initial_gpu_memory_in_mb); PHI_DECLARE_uint64(reallocate_gpu_memory_in_mb); -DECLARE_bool(benchmark); +PD_DECLARE_bool(benchmark); namespace paddle { namespace memory { diff --git a/paddle/fluid/operators/pscore/CMakeLists.txt b/paddle/fluid/operators/pscore/CMakeLists.txt index 5a397699951ca..dea89806bc202 100755 --- a/paddle/fluid/operators/pscore/CMakeLists.txt +++ b/paddle/fluid/operators/pscore/CMakeLists.txt @@ -37,15 +37,9 @@ else() ps_framework_proto framework_proto sendrecv_rpc - brpc - leveldb - ssl - crypto - protobuf + ${EXTERNAL_BRPC_DEPS} phi - glog zlib - snappy device_context) endif() diff --git a/paddle/fluid/operators/tdm_child_op.h b/paddle/fluid/operators/tdm_child_op.h index 0ebac190cda31..1a126b0a8853c 100644 --- a/paddle/fluid/operators/tdm_child_op.h +++ b/paddle/fluid/operators/tdm_child_op.h @@ -21,9 +21,9 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/phi/core/mixed_vector.h" +#include "paddle/utils/flags.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/tdm_sampler_op.h b/paddle/fluid/operators/tdm_sampler_op.h index 4baff82078471..f3b55c4a5cc34 100644 --- a/paddle/fluid/operators/tdm_sampler_op.h +++ b/paddle/fluid/operators/tdm_sampler_op.h @@ -21,10 +21,10 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/math/sampler.h" #include "paddle/phi/core/mixed_vector.h" +#include "paddle/utils/flags.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/platform/cpu_info_test.cc b/paddle/fluid/platform/cpu_info_test.cc index 6f7ccd03cff2f..fef343112dc03 100644 --- a/paddle/fluid/platform/cpu_info_test.cc +++ b/paddle/fluid/platform/cpu_info_test.cc @@ -15,10 +15,10 @@ #include -#include "gflags/gflags.h" #include "gtest/gtest.h" #include "paddle/fluid/string/printf.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" PHI_DECLARE_double(fraction_of_cpu_memory_to_use); diff --git a/paddle/fluid/platform/cuda_graph_with_memory_pool.cc b/paddle/fluid/platform/cuda_graph_with_memory_pool.cc index 5799863f0aad7..d15f67e9965fa 100644 --- a/paddle/fluid/platform/cuda_graph_with_memory_pool.cc +++ b/paddle/fluid/platform/cuda_graph_with_memory_pool.cc @@ -19,7 +19,7 @@ #include "paddle/phi/backends/context_pool.h" #include "paddle/phi/core/flags.h" -DECLARE_bool(use_stream_safe_cuda_allocator); +PD_DECLARE_bool(use_stream_safe_cuda_allocator); PHI_DECLARE_bool(new_executor_use_cuda_graph); namespace paddle { diff --git a/paddle/fluid/platform/device/gpu/gpu_info.cc b/paddle/fluid/platform/device/gpu/gpu_info.cc index 10ba4ce6e1d12..ba2494a2f4891 100644 --- a/paddle/fluid/platform/device/gpu/gpu_info.cc +++ b/paddle/fluid/platform/device/gpu/gpu_info.cc @@ -20,7 +20,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/memory/memory.h" #include "paddle/fluid/platform/cuda_device_guard.h" #include "paddle/fluid/platform/enforce.h" @@ -32,6 +31,7 @@ limitations under the License. */ #include "paddle/fluid/platform/profiler/mem_tracing.h" #include "paddle/fluid/string/split.h" #include "paddle/phi/backends/gpu/gpu_info.h" +#include "paddle/utils/flags.h" #ifdef PADDLE_WITH_HIP #include "paddle/fluid/platform/dynload/miopen.h" diff --git a/paddle/fluid/platform/device/xpu/xpu_info.cc b/paddle/fluid/platform/device/xpu/xpu_info.cc index 3193d5d881989..374166faeb6f7 100644 --- a/paddle/fluid/platform/device/xpu/xpu_info.cc +++ b/paddle/fluid/platform/device/xpu/xpu_info.cc @@ -14,7 +14,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/fluid/platform/device/xpu/enforce_xpu.h" #include "paddle/fluid/platform/device/xpu/xpu_header.h" @@ -23,6 +22,7 @@ limitations under the License. */ #include "paddle/fluid/platform/monitor.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/backends/xpu/xpu_info.h" +#include "paddle/utils/flags.h" namespace paddle { namespace platform { diff --git a/paddle/fluid/platform/dynload/dynamic_loader.cc b/paddle/fluid/platform/dynload/dynamic_loader.cc index 40f69a87f37f8..94a96b67cd6b1 100644 --- a/paddle/fluid/platform/dynload/dynamic_loader.cc +++ b/paddle/fluid/platform/dynload/dynamic_loader.cc @@ -16,8 +16,8 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/phi/backends/dynload/dynamic_loader.h" +#include "paddle/utils/flags.h" namespace paddle { namespace platform { diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 425d4939b565f..d9c9398461d5c 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -60,10 +60,10 @@ limitations under the License. */ #endif #define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/fluid/platform/errors.h" #include "paddle/fluid/platform/macros.h" +#include "paddle/utils/flags.h" #include "paddle/fluid/string/printf.h" #include "paddle/fluid/string/to_string.h" diff --git a/paddle/fluid/platform/flags.h b/paddle/fluid/platform/flags.h index b08aececd2b81..66d8f9557ef18 100644 --- a/paddle/fluid/platform/flags.h +++ b/paddle/fluid/platform/flags.h @@ -23,7 +23,7 @@ #define __PADDLE_DEFINE_EXPORTED_FLAG( \ __name, __is_writable, __cpp_type, __gflag_type, __default_value, __doc) \ - DEFINE_##__gflag_type(__name, __default_value, __doc); /* NOLINT */ \ + PD_DEFINE_##__gflag_type(__name, __default_value, __doc); /* NOLINT */ \ struct __PaddleRegisterFlag_##__name { \ __PaddleRegisterFlag_##__name() { \ using FlagDeclaredType = \ diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 2cc3dc7124a52..4e0803c59ce59 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -102,8 +102,8 @@ bool InitGflags(std::vector args) { << ", Init commandline: " << line; char **arr = argv.data(); - ::GFLAGS_NAMESPACE::AllowCommandLineReparsing(); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &arr, true); + paddle::flags::AllowUndefinedFlags(); + paddle::flags::ParseCommandLineFlags(&argc, &arr); successed = true; VLOG(1) << "After Parse: argc is " << argc; diff --git a/paddle/fluid/platform/init.h b/paddle/fluid/platform/init.h index 2d5c34002b4bb..4ff99f2866e60 100644 --- a/paddle/fluid/platform/init.h +++ b/paddle/fluid/platform/init.h @@ -17,8 +17,8 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index 54d27cbd7b755..efee8a264bc63 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -38,7 +38,9 @@ PADDLE_DEFINE_EXPORTED_bool(enable_rpc_profiler, false, "Enable rpc profiler or not."); -DEFINE_bool(enable_record_memory, false, "enable memory recorder"); // NOLINT +PD_DEFINE_bool(enable_record_memory, + false, + "enable memory recorder"); // NOLINT #if defined(_WIN32) && defined(PHI_SHARED) phi::ProfilerState phi::ProfilerHelper::g_state = phi::ProfilerState::kDisabled; diff --git a/paddle/fluid/platform/profiler/host_tracer.cc b/paddle/fluid/platform/profiler/host_tracer.cc index 6907ccd3b044b..56fe468838b83 100644 --- a/paddle/fluid/platform/profiler/host_tracer.cc +++ b/paddle/fluid/platform/profiler/host_tracer.cc @@ -17,17 +17,9 @@ #include "glog/logging.h" #include "paddle/fluid/framework/op_proto_maker.h" -#include "paddle/fluid/platform/flags.h" #include "paddle/fluid/platform/profiler/common_event.h" #include "paddle/fluid/platform/profiler/host_event_recorder.h" -// Used to filter events, works like glog VLOG(level). -// RecordEvent will works if host_trace_level >= level. -PADDLE_DEFINE_EXPORTED_int64(host_trace_level, - 1, - "RecordEvent will works " - "if host_trace_level >= level."); - namespace paddle { namespace platform { diff --git a/paddle/fluid/platform/profiler/profiler.cc b/paddle/fluid/platform/profiler/profiler.cc index 78e6443cbac6d..4f58b0e3ccef4 100644 --- a/paddle/fluid/platform/profiler/profiler.cc +++ b/paddle/fluid/platform/profiler/profiler.cc @@ -25,6 +25,7 @@ #include "paddle/fluid/platform/device/gpu/gpu_info.h" #endif #include "paddle/fluid/platform/enforce.h" +#include "paddle/fluid/platform/flags.h" #include "paddle/fluid/platform/profiler/cuda_tracer.h" #include "paddle/fluid/platform/profiler/custom_device/custom_tracer.h" #include "paddle/fluid/platform/profiler/extra_info.h" @@ -36,6 +37,13 @@ #include "paddle/phi/backends/device_manager.h" #endif +// Used to filter events, works like glog VLOG(level). +// RecordEvent will works if host_trace_level >= level. +PADDLE_DEFINE_EXPORTED_int64(host_trace_level, + 1, + "RecordEvent will works " + "if host_trace_level >= level."); + namespace paddle { namespace platform { diff --git a/paddle/fluid/platform/profiler/profiler.h b/paddle/fluid/platform/profiler/profiler.h index 28cf7a2d38572..4ab98bab53050 100644 --- a/paddle/fluid/platform/profiler/profiler.h +++ b/paddle/fluid/platform/profiler/profiler.h @@ -27,7 +27,7 @@ #include "paddle/fluid/platform/profiler/event_python.h" #include "paddle/fluid/platform/profiler/tracer_base.h" -DECLARE_int64(host_trace_level); +PD_DECLARE_int64(host_trace_level); namespace paddle { namespace platform { diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 42cdd9be3067c..478efa7a5af95 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -57,18 +57,7 @@ if(WITH_PSCORE) endif() endif() if(WITH_RPC) - set(PYBIND_DEPS - ${PYBIND_DEPS} - paddle_rpc - brpc - ssl - crypto - protobuf - zlib - leveldb - snappy - phi - glog) + set(PYBIND_DEPS ${PYBIND_DEPS} paddle_rpc ${EXTERNAL_BRPC_DEPS} zlib phi) endif() if(WITH_GPU OR WITH_ROCM) set(PYBIND_DEPS ${PYBIND_DEPS} dynload_cuda) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index f667602a49310..7d3037a076d57 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -67,7 +67,7 @@ typedef SSIZE_T ssize_t; #include "paddle/utils/pybind.h" PHI_DECLARE_bool(set_to_1d); -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); namespace paddle { namespace pybind { diff --git a/paddle/fluid/pybind/global_value_getter_setter.cc b/paddle/fluid/pybind/global_value_getter_setter.cc index 94e3ca1ba41bd..7f8cac9ee1edf 100644 --- a/paddle/fluid/pybind/global_value_getter_setter.cc +++ b/paddle/fluid/pybind/global_value_getter_setter.cc @@ -22,12 +22,12 @@ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/framework/python_headers.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/errors.h" #include "paddle/fluid/platform/macros.h" #include "paddle/phi/core/macros.h" +#include "paddle/utils/flags.h" #include "pybind11/stl.h" // FIXME(zengjinle): these 2 flags may be removed by the linker when compiling @@ -41,8 +41,8 @@ PADDLE_FORCE_LINK_FLAG(free_when_no_cache_hit); // NOTE: where are these 2 flags from? #ifdef PADDLE_WITH_DISTRIBUTE -DECLARE_int32(rpc_get_thread_num); -DECLARE_int32(rpc_prefetch_thread_num); +PD_DECLARE_int32(rpc_get_thread_num); +PD_DECLARE_int32(rpc_prefetch_thread_num); #endif namespace paddle { diff --git a/paddle/fluid/pybind/reader_py.cc b/paddle/fluid/pybind/reader_py.cc index 0ae52f7fdd74b..0e581e45b5970 100644 --- a/paddle/fluid/pybind/reader_py.cc +++ b/paddle/fluid/pybind/reader_py.cc @@ -23,7 +23,6 @@ #include "Python.h" -#include "gflags/gflags.h" #include "paddle/fluid/framework/reader.h" #include "paddle/fluid/imperative/layer.h" #include "paddle/fluid/imperative/tracer.h" @@ -33,6 +32,7 @@ #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" #include "pybind11/stl.h" PHI_DECLARE_bool(reader_queue_speed_test_mode); diff --git a/paddle/fluid/string/pretty_log.h b/paddle/fluid/string/pretty_log.h index d161b2a912fca..8e34cbb8ab9dc 100644 --- a/paddle/fluid/string/pretty_log.h +++ b/paddle/fluid/string/pretty_log.h @@ -18,5 +18,5 @@ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/utils/string/pretty_log.h" diff --git a/paddle/phi/CMakeLists.txt b/paddle/phi/CMakeLists.txt index 83d8006957ac4..cfbf8fec0adfd 100644 --- a/paddle/phi/CMakeLists.txt +++ b/paddle/phi/CMakeLists.txt @@ -36,7 +36,7 @@ endif() set(PHI_DEPS phi_profiler_proto auto_parallel_proto - gflags + ${flags_dep} glog warpctc warprnnt diff --git a/paddle/phi/api/lib/api_gen_utils.cc b/paddle/phi/api/lib/api_gen_utils.cc index 73a1ed4c7fdc8..80e169fae10c8 100644 --- a/paddle/phi/api/lib/api_gen_utils.cc +++ b/paddle/phi/api/lib/api_gen_utils.cc @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/api/lib/api_gen_utils.h" -#include "gflags/gflags.h" #include "paddle/phi/core/visit_type.h" #include "paddle/phi/kernels/strided_copy_kernel.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); #include "glog/logging.h" diff --git a/paddle/phi/api/lib/data_transform.cc b/paddle/phi/api/lib/data_transform.cc index 3d717c6dbfe05..a8eb379e359fc 100644 --- a/paddle/phi/api/lib/data_transform.cc +++ b/paddle/phi/api/lib/data_transform.cc @@ -16,7 +16,6 @@ limitations under the License. */ #include "glog/logging.h" -#include "gflags/gflags.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/api/lib/kernel_dispatch.h" #include "paddle/phi/api/lib/utils/allocator.h" @@ -28,8 +27,9 @@ limitations under the License. */ #include "paddle/phi/kernels/cast_kernel.h" #include "paddle/phi/kernels/contiguous_kernel.h" #include "paddle/phi/kernels/transfer_layout_kernel.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_stride_kernel); +PD_DECLARE_bool(use_stride_kernel); namespace paddle { namespace experimental { diff --git a/paddle/phi/api/profiler/device_tracer.cc b/paddle/phi/api/profiler/device_tracer.cc index b16579491f49d..e294130da7bab 100644 --- a/paddle/phi/api/profiler/device_tracer.cc +++ b/paddle/phi/api/profiler/device_tracer.cc @@ -21,11 +21,11 @@ limitations under the License. */ #include #include // NOLINT -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/core/enforce.h" +#include "paddle/utils/flags.h" -DECLARE_bool(enable_host_event_recorder_hook); +PD_DECLARE_bool(enable_host_event_recorder_hook); namespace phi { diff --git a/paddle/phi/api/yaml/generator/api_gen.py b/paddle/phi/api/yaml/generator/api_gen.py index 50ba2078b045f..5164ebda84035 100644 --- a/paddle/phi/api/yaml/generator/api_gen.py +++ b/paddle/phi/api/yaml/generator/api_gen.py @@ -360,7 +360,7 @@ def source_include(header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_custom_impl.h" #include "paddle/phi/api/lib/api_gen_utils.h" @@ -379,8 +379,8 @@ def source_include(header_file_path): #include "paddle/phi/api/profiler/event_tracing.h" #include "paddle/phi/api/profiler/supplement_tracing.h" -DECLARE_bool(conv2d_disable_cudnn); -DECLARE_int32(low_precision_op_list); +PD_DECLARE_bool(conv2d_disable_cudnn); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/backward_api_gen.py b/paddle/phi/api/yaml/generator/backward_api_gen.py index 183904f642c98..9347552dbb134 100644 --- a/paddle/phi/api/yaml/generator/backward_api_gen.py +++ b/paddle/phi/api/yaml/generator/backward_api_gen.py @@ -275,7 +275,7 @@ def source_include(header_file_path, fw_header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_custom_impl.h" #include "paddle/phi/api/lib/api_gen_utils.h" @@ -290,8 +290,8 @@ def source_include(header_file_path, fw_header_file_path): #include "paddle/phi/api/profiler/event_tracing.h" #include "paddle/phi/api/profiler/supplement_tracing.h" -DECLARE_bool(conv2d_disable_cudnn); -DECLARE_int32(low_precision_op_list); +PD_DECLARE_bool(conv2d_disable_cudnn); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py index ac6f5f2e916eb..95a6f94706eee 100644 --- a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py +++ b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py @@ -138,7 +138,7 @@ def source_include(header_file_path, fw_header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_custom_impl.h" #include "paddle/phi/api/lib/api_gen_utils.h" @@ -153,8 +153,8 @@ def source_include(header_file_path, fw_header_file_path): #include "paddle/phi/api/profiler/event_tracing.h" #include "paddle/phi/api/profiler/supplement_tracing.h" -DECLARE_bool(conv2d_disable_cudnn); -DECLARE_int32(low_precision_op_list); +PD_DECLARE_bool(conv2d_disable_cudnn); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/intermediate_api_gen.py b/paddle/phi/api/yaml/generator/intermediate_api_gen.py index e8f9c1cf75152..5c3b6ddd32d5d 100644 --- a/paddle/phi/api/yaml/generator/intermediate_api_gen.py +++ b/paddle/phi/api/yaml/generator/intermediate_api_gen.py @@ -36,7 +36,7 @@ def source_include(header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_custom_impl.h" #include "paddle/phi/api/lib/api_gen_utils.h" @@ -56,7 +56,7 @@ def source_include(header_file_path): #include "paddle/phi/api/profiler/event_tracing.h" #include "paddle/phi/api/profiler/supplement_tracing.h" -DECLARE_int32(low_precision_op_list); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/sparse_api_gen.py b/paddle/phi/api/yaml/generator/sparse_api_gen.py index af28fcb0948c9..9a017725d6888 100644 --- a/paddle/phi/api/yaml/generator/sparse_api_gen.py +++ b/paddle/phi/api/yaml/generator/sparse_api_gen.py @@ -426,7 +426,7 @@ def source_include(header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/api/lib/data_transform.h" @@ -442,7 +442,7 @@ def source_include(header_file_path): #include "paddle/phi/infermeta/sparse/binary.h" #include "paddle/phi/infermeta/sparse/multiary.h" -DECLARE_int32(low_precision_op_list); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py b/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py index 67a8514bf1937..064cf07d0dbf7 100644 --- a/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py +++ b/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py @@ -121,7 +121,7 @@ def source_include(header_file_path): #include #include "glog/logging.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/include/sparse_api.h" #include "paddle/phi/api/lib/api_gen_utils.h" @@ -137,7 +137,7 @@ def source_include(header_file_path): #include "paddle/phi/infermeta/sparse/binary.h" #include "paddle/phi/infermeta/sparse/backward.h" -DECLARE_int32(low_precision_op_list); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/strings_api_gen.py b/paddle/phi/api/yaml/generator/strings_api_gen.py index ed0bfe42bd8b7..4e66bd5f2fdc6 100644 --- a/paddle/phi/api/yaml/generator/strings_api_gen.py +++ b/paddle/phi/api/yaml/generator/strings_api_gen.py @@ -329,8 +329,8 @@ def source_include(header_file_path): return f""" #include "{header_file_path}" -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/core/kernel_context.h" @@ -340,7 +340,7 @@ def source_include(header_file_path): #include "paddle/phi/api/lib/kernel_dispatch.h" #include "paddle/phi/core/kernel_registry.h" -DECLARE_int32(low_precision_op_list); +PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/tensor_operants_gen.py b/paddle/phi/api/yaml/generator/tensor_operants_gen.py index f86efeaaefac8..1ca80a8bd76e8 100644 --- a/paddle/phi/api/yaml/generator/tensor_operants_gen.py +++ b/paddle/phi/api/yaml/generator/tensor_operants_gen.py @@ -441,16 +441,16 @@ class OperantsManager { #include "paddle/phi/api/include/operants_manager.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/errors.h" +#include "paddle/utils/flags.h" """ operants_manager_source_start = """ -DECLARE_string(tensor_operants_mode); +PD_DECLARE_string(tensor_operants_mode); namespace paddle { diff --git a/paddle/phi/backends/cpu/cpu_info.cc b/paddle/phi/backends/cpu/cpu_info.cc index 8d1d757dfd598..2eda010487754 100644 --- a/paddle/phi/backends/cpu/cpu_info.cc +++ b/paddle/phi/backends/cpu/cpu_info.cc @@ -35,9 +35,9 @@ limitations under the License. */ #include "paddle/phi/core/flags.h" -DECLARE_double(fraction_of_cpu_memory_to_use); -DECLARE_uint64(initial_cpu_memory_in_mb); -DECLARE_double(fraction_of_cuda_pinned_memory_to_use); +PD_DECLARE_double(fraction_of_cpu_memory_to_use); +PD_DECLARE_uint64(initial_cpu_memory_in_mb); +PD_DECLARE_double(fraction_of_cuda_pinned_memory_to_use); // If use_pinned_memory is true, CPUAllocator calls mlock, which // returns pinned and locked memory as staging areas for data exchange diff --git a/paddle/phi/backends/device_base.cc b/paddle/phi/backends/device_base.cc index 2b7d0411fedca..5b6b8fcfc2fe9 100644 --- a/paddle/phi/backends/device_base.cc +++ b/paddle/phi/backends/device_base.cc @@ -14,13 +14,13 @@ #include "paddle/phi/backends/device_base.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/core/enforce.h" +#include "paddle/utils/flags.h" -DECLARE_double(fraction_of_gpu_memory_to_use); -DECLARE_uint64(initial_gpu_memory_in_mb); -DECLARE_uint64(reallocate_gpu_memory_in_mb); +PD_DECLARE_double(fraction_of_gpu_memory_to_use); +PD_DECLARE_uint64(initial_gpu_memory_in_mb); +PD_DECLARE_uint64(reallocate_gpu_memory_in_mb); constexpr static float fraction_reserve_gpu_memory = 0.05f; diff --git a/paddle/phi/backends/dynload/cudnn_frontend.h b/paddle/phi/backends/dynload/cudnn_frontend.h index 4d0b67ce2285c..ef680d7cba035 100644 --- a/paddle/phi/backends/dynload/cudnn_frontend.h +++ b/paddle/phi/backends/dynload/cudnn_frontend.h @@ -14,13 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ #pragma once -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/backends/dynload/cudnn.h" #include "paddle/phi/backends/gpu/gpu_info.h" -DECLARE_bool(enable_cudnn_frontend); +PD_DECLARE_bool(enable_cudnn_frontend); // Redirect the CUDNN APIs in the cudnn_frontend namespace to // the functions in phi::dynload diff --git a/paddle/phi/backends/dynload/dynamic_loader.cc b/paddle/phi/backends/dynload/dynamic_loader.cc index 4621a9c3ddafa..6989f32b18e9e 100644 --- a/paddle/phi/backends/dynload/dynamic_loader.cc +++ b/paddle/phi/backends/dynload/dynamic_loader.cc @@ -103,7 +103,7 @@ PHI_DEFINE_string(rccl_dir, #endif #ifdef PADDLE_WITH_XPU -DEFINE_string(xpti_dir, "", "Specify path for loading libxpti.so."); +PD_DEFINE_string(xpti_dir, "", "Specify path for loading libxpti.so."); #endif namespace phi { diff --git a/paddle/phi/backends/gpu/cuda/cudnn_helper.h b/paddle/phi/backends/gpu/cuda/cudnn_helper.h index 8b6c04090d88e..651a4247a12df 100644 --- a/paddle/phi/backends/gpu/cuda/cudnn_helper.h +++ b/paddle/phi/backends/gpu/cuda/cudnn_helper.h @@ -17,7 +17,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/phi/backends/dynload/cudnn.h" #include "paddle/phi/common/bfloat16.h" #include "paddle/phi/common/float16.h" @@ -26,8 +25,9 @@ limitations under the License. */ #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/errors.h" #include "paddle/phi/core/macros.h" +#include "paddle/utils/flags.h" -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { namespace backends { diff --git a/paddle/phi/backends/gpu/gpu_info.cc b/paddle/phi/backends/gpu/gpu_info.cc index 417ff4c72e86c..f6ca9d4168b2c 100644 --- a/paddle/phi/backends/gpu/gpu_info.cc +++ b/paddle/phi/backends/gpu/gpu_info.cc @@ -17,12 +17,12 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/common/memory_utils.h" -DECLARE_string(selected_gpus); +PD_DECLARE_string(selected_gpus); namespace phi { namespace backends { diff --git a/paddle/phi/backends/gpu/rocm/miopen_helper.h b/paddle/phi/backends/gpu/rocm/miopen_helper.h index 095f32ba460d0..b8ce6e22e939b 100644 --- a/paddle/phi/backends/gpu/rocm/miopen_helper.h +++ b/paddle/phi/backends/gpu/rocm/miopen_helper.h @@ -17,7 +17,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/backends/dynload/miopen.h" #include "paddle/phi/common/bfloat16.h" @@ -31,7 +31,7 @@ limitations under the License. */ // MIOPEN do not have epslion definition #define CUDNN_BN_MIN_EPSILON 1e-05 -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { namespace backends { diff --git a/paddle/phi/core/enforce.cc b/paddle/phi/core/enforce.cc index 69bcbf91ef463..f47eb91e97d97 100644 --- a/paddle/phi/core/enforce.cc +++ b/paddle/phi/core/enforce.cc @@ -20,16 +20,16 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/common/scalar.h" #include "paddle/utils/blank.h" +#include "paddle/utils/flags.h" #ifdef PADDLE_WITH_CUDA #include "paddle/phi/core/external_error.pb.h" #endif // PADDLE_WITH_CUDA -DECLARE_int32(call_stack_level); +PD_DECLARE_int32(call_stack_level); namespace egr { class EagerVariable; diff --git a/paddle/phi/core/flags.cc b/paddle/phi/core/flags.cc index 2470981f6617c..41d2dc8003bb6 100644 --- a/paddle/phi/core/flags.cc +++ b/paddle/phi/core/flags.cc @@ -1056,19 +1056,21 @@ PHI_DEFINE_EXPORTED_uint64(executor_log_deps_every_microseconds, 0, "Enable new executor log deps every n microseconds"); -DEFINE_int32(record_pool_max_size, - 2000000, - "SlotRecordDataset slot record pool max size"); -DEFINE_int32(slotpool_thread_num, 1, "SlotRecordDataset slot pool thread num"); -DEFINE_bool(enable_slotpool_wait_release, // NOLINT - false, - "enable slotrecord object wait release, default false"); -DEFINE_bool(enable_slotrecord_reset_shrink, // NOLINT - false, - "enable slotrecord object reset shrink memory, default false"); -DEFINE_bool(enable_ins_parser_file, // NOLINT - false, - "enable parser ins file, default false"); +PD_DEFINE_int32(record_pool_max_size, + 2000000, + "SlotRecordDataset slot record pool max size"); +PD_DEFINE_int32(slotpool_thread_num, + 1, + "SlotRecordDataset slot pool thread num"); +PD_DEFINE_bool(enable_slotpool_wait_release, // NOLINT + false, + "enable slotrecord object wait release, default false"); +PD_DEFINE_bool(enable_slotrecord_reset_shrink, // NOLINT + false, + "enable slotrecord object reset shrink memory, default false"); +PD_DEFINE_bool(enable_ins_parser_file, // NOLINT + false, + "enable parser ins file, default false"); PHI_DEFINE_EXPORTED_bool( gpugraph_enable_hbm_table_collision_stat, false, diff --git a/paddle/phi/core/flags.h b/paddle/phi/core/flags.h index 278090ff97d26..776e268b201c7 100644 --- a/paddle/phi/core/flags.h +++ b/paddle/phi/core/flags.h @@ -20,9 +20,8 @@ #include #include -#include "gflags/gflags.h" #include "paddle/phi/core/macros.h" - +#include "paddle/utils/flags.h" #include "paddle/utils/variant.h" #if defined(_WIN32) @@ -33,6 +32,7 @@ #define PHI_IMPORT_FLAG #endif // _WIN32 +#ifdef PADDLE_WITH_GFLAGS // We redefine the gflags' macro for exporting global variable // ----------------------------DECLARE FLAGS---------------------------- @@ -127,6 +127,23 @@ clstring& FLAGS_##name = *FLAGS_no##name; \ } /* NOLINT */ \ using fLS::FLAGS_##name +#else // PADDLE_WITH_GFLAGS +#define PHI_DEFINE_bool(name, val, txt) PD_DEFINE_bool(name, val, txt) +#define PHI_DEFINE_int32(name, val, txt) PD_DEFINE_int32(name, val, txt) +#define PHI_DEFINE_uint32(name, val, txt) PD_DEFINE_uint32(name, val, txt) +#define PHI_DEFINE_int64(name, val, txt) PD_DEFINE_int64(name, val, txt) +#define PHI_DEFINE_uint64(name, val, txt) PD_DEFINE_uint64(name, val, txt) +#define PHI_DEFINE_double(name, val, txt) PD_DEFINE_double(name, val, txt) +#define PHI_DEFINE_string(name, val, txt) PD_DEFINE_string(name, val, txt) + +#define PHI_DECLARE_bool(name) PD_DECLARE_bool(name) +#define PHI_DECLARE_int32(name) PD_DECLARE_int32(name) +#define PHI_DECLARE_uint32(name) PD_DECLARE_uint32(name) +#define PHI_DECLARE_int64(name) PD_DECLARE_int64(name) +#define PHI_DECLARE_uint64(name) PD_DECLARE_uint64(name) +#define PHI_DECLARE_double(name) PD_DECLARE_double(name) +#define PHI_DECLARE_string(name) PD_DECLARE_string(name) +#endif namespace phi { diff --git a/paddle/phi/core/kernel_factory.cc b/paddle/phi/core/kernel_factory.cc index 543fe4fef7611..3ead251e33220 100644 --- a/paddle/phi/core/kernel_factory.cc +++ b/paddle/phi/core/kernel_factory.cc @@ -14,9 +14,9 @@ #include "paddle/phi/core/kernel_factory.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/core/enforce.h" +#include "paddle/utils/flags.h" #if defined(PADDLE_WITH_XPU) #include "paddle/phi/backends/xpu/xpu_op_list.h" #include "paddle/phi/common/data_type.h" @@ -34,9 +34,9 @@ PADDLE_DEFINE_EXPORTED_bool( true, "Whether to use strdie kernel if op support stride."); -DECLARE_int32(low_precision_op_list); -DECLARE_bool(enable_api_kernel_fallback); -DECLARE_bool(run_kp_kernel); +PD_DECLARE_int32(low_precision_op_list); +PD_DECLARE_bool(enable_api_kernel_fallback); +PD_DECLARE_bool(run_kp_kernel); namespace phi { const static Kernel empty_kernel; // NOLINT diff --git a/paddle/phi/core/threadpool.cc b/paddle/phi/core/threadpool.cc index 5e146023c29ce..7538087f4e855 100644 --- a/paddle/phi/core/threadpool.cc +++ b/paddle/phi/core/threadpool.cc @@ -16,14 +16,14 @@ #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/core/enforce.h" +#include "paddle/utils/flags.h" -DECLARE_int32(dist_threadpool_size); -DEFINE_int32(io_threadpool_size, - 100, - "number of threads used for doing IO, default 100"); +PD_DECLARE_int32(dist_threadpool_size); +PD_DEFINE_int32(io_threadpool_size, + 100, + "number of threads used for doing IO, default 100"); namespace phi { diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 6e04a149266ac..4c952bb3cd2bc 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -17,7 +17,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/common/type_traits.h" #include "paddle/phi/core/enforce.h" @@ -31,6 +30,7 @@ limitations under the License. */ #include "paddle/phi/kernels/funcs/unfold_functor.h" #include "paddle/phi/kernels/funcs/unsqueeze.h" #include "paddle/phi/kernels/impl/einsum_impl.h" +#include "paddle/utils/flags.h" namespace phi { diff --git a/paddle/phi/kernels/autotune/cache_cudnn_frontend.h b/paddle/phi/kernels/autotune/cache_cudnn_frontend.h index 095cedccb991c..4715efa1f776e 100644 --- a/paddle/phi/kernels/autotune/cache_cudnn_frontend.h +++ b/paddle/phi/kernels/autotune/cache_cudnn_frontend.h @@ -22,7 +22,7 @@ #include "paddle/phi/backends/dynload/cudnn_frontend.h" -DECLARE_int32(cudnn_cache_saturation_count); +PD_DECLARE_int32(cudnn_cache_saturation_count); namespace phi { namespace autotune { diff --git a/paddle/phi/kernels/autotune/switch_autotune.cc b/paddle/phi/kernels/autotune/switch_autotune.cc index 3742749b3bf03..e287705d08b79 100644 --- a/paddle/phi/kernels/autotune/switch_autotune.cc +++ b/paddle/phi/kernels/autotune/switch_autotune.cc @@ -14,10 +14,10 @@ #include "paddle/phi/kernels/autotune/switch_autotune.h" -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_autotune); +PD_DECLARE_bool(use_autotune); namespace phi { namespace autotune { diff --git a/paddle/phi/kernels/cpu/adam_kernel.cc b/paddle/phi/kernels/cpu/adam_kernel.cc index 083c9dab74001..1a63b779b02a1 100644 --- a/paddle/phi/kernels/cpu/adam_kernel.cc +++ b/paddle/phi/kernels/cpu/adam_kernel.cc @@ -24,7 +24,7 @@ #include "paddle/phi/kernels/funcs/adam_functors.h" #include "paddle/phi/kernels/funcs/jit/kernels.h" -DECLARE_int32(inner_op_parallelism); +PD_DECLARE_int32(inner_op_parallelism); namespace phi { diff --git a/paddle/phi/kernels/funcs/blas/blas_impl.cu.h b/paddle/phi/kernels/funcs/blas/blas_impl.cu.h index 84eea97da9f51..6e4f7e22781f1 100644 --- a/paddle/phi/kernels/funcs/blas/blas_impl.cu.h +++ b/paddle/phi/kernels/funcs/blas/blas_impl.cu.h @@ -17,8 +17,8 @@ #if defined(__NVCC__) #include #endif -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/backends/dynload/cublas.h" #include "paddle/phi/backends/gpu/gpu_context.h" diff --git a/paddle/phi/kernels/funcs/blas/blas_impl.hip.h b/paddle/phi/kernels/funcs/blas/blas_impl.hip.h index 805a718ab85ed..224cf4d6cb497 100644 --- a/paddle/phi/kernels/funcs/blas/blas_impl.hip.h +++ b/paddle/phi/kernels/funcs/blas/blas_impl.hip.h @@ -14,13 +14,13 @@ #pragma once -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/phi/backends/dynload/rocblas.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/kernels/funcs/math_function.h" -DECLARE_bool(enable_cublas_tensor_op_math); +PD_DECLARE_bool(enable_cublas_tensor_op_math); namespace phi { namespace funcs { diff --git a/paddle/phi/kernels/funcs/fused_gemm_epilogue.h b/paddle/phi/kernels/funcs/fused_gemm_epilogue.h index 6f4eb46bf4eb7..eb5f0fa540f8d 100644 --- a/paddle/phi/kernels/funcs/fused_gemm_epilogue.h +++ b/paddle/phi/kernels/funcs/fused_gemm_epilogue.h @@ -26,7 +26,6 @@ limitations under the License. */ #if CUDA_VERSION >= 11060 -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/backends/all_context.h" #include "paddle/phi/backends/dynload/cublasLt.h" @@ -38,9 +37,10 @@ limitations under the License. */ #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/scope_guard.h" #include "paddle/phi/kernels/funcs/blas/blaslt_impl.cu.h" +#include "paddle/utils/flags.h" #include "paddle/utils/optional.h" -DECLARE_int64(cublaslt_exhaustive_search_times); +PD_DECLARE_int64(cublaslt_exhaustive_search_times); namespace phi { namespace funcs { diff --git a/paddle/phi/kernels/funcs/jit/benchmark.cc b/paddle/phi/kernels/funcs/jit/benchmark.cc index ad7146db0d93a..d5d1da7385797 100644 --- a/paddle/phi/kernels/funcs/jit/benchmark.cc +++ b/paddle/phi/kernels/funcs/jit/benchmark.cc @@ -15,18 +15,18 @@ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/api/profiler/device_tracer.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/jit/kernels.h" +#include "paddle/utils/flags.h" -DEFINE_int32(burning, 10, "Burning times."); -DEFINE_int32(repeat, 3000, "Repeat times."); -DEFINE_int32(max_size, 1000, "The Max size would be tested."); -DEFINE_string(filter, "", "The Benchmark name would be run."); // NOLINT +PD_DEFINE_int32(burning, 10, "Burning times."); +PD_DEFINE_int32(repeat, 3000, "Repeat times."); +PD_DEFINE_int32(max_size, 1000, "The Max size would be tested."); +PD_DEFINE_string(filter, "", "The Benchmark name would be run."); // NOLINT class BenchJITKernel { public: @@ -546,7 +546,7 @@ BENCH_FP32_CPU(VBroadcast); // --max_size: the max size would be tested // --filter: the bench name would be run int main(int argc, char* argv[]) { - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + paddle::flags::ParseCommandLineFlags(&argc, &argv); google::InitGoogleLogging(argv[0]); LOG(INFO) << "Burning " << FLAGS_burning << " times, Repeat " << FLAGS_repeat << " times."; diff --git a/paddle/phi/kernels/funcs/jit/gen_base.h b/paddle/phi/kernels/funcs/jit/gen_base.h index dfad19eff3448..f96f08005ab03 100644 --- a/paddle/phi/kernels/funcs/jit/gen_base.h +++ b/paddle/phi/kernels/funcs/jit/gen_base.h @@ -22,9 +22,9 @@ #include // for _aligned_malloc #endif -#include "gflags/gflags.h" #include "paddle/phi/core/flags.h" #include "paddle/phi/kernels/funcs/jit/kernel_base.h" +#include "paddle/utils/flags.h" PHI_DECLARE_bool(dump_jitcode); diff --git a/paddle/phi/kernels/funcs/jit/test.cc b/paddle/phi/kernels/funcs/jit/test.cc index 0dd5f6c6ba5ae..d388d95975cff 100644 --- a/paddle/phi/kernels/funcs/jit/test.cc +++ b/paddle/phi/kernels/funcs/jit/test.cc @@ -16,15 +16,15 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/jit/kernels.h" +#include "paddle/utils/flags.h" -DEFINE_double(acc, 1e-5, "Test accuracy threshold."); +PD_DEFINE_double(acc, 1e-5, "Test accuracy threshold."); template void RandomVec(const int n, diff --git a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu index 0609bf945d9b3..31bad0987ba27 100644 --- a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu @@ -34,7 +34,7 @@ #define LAUNCH_BOUNDS(BlockDim) #endif -DECLARE_bool(cudnn_batchnorm_spatial_persistent); +PD_DECLARE_bool(cudnn_batchnorm_spatial_persistent); namespace phi { template diff --git a/paddle/phi/kernels/gpu/batch_norm_kernel.cu b/paddle/phi/kernels/gpu/batch_norm_kernel.cu index 1a07e5f0d4909..ad276ec6f1812 100644 --- a/paddle/phi/kernels/gpu/batch_norm_kernel.cu +++ b/paddle/phi/kernels/gpu/batch_norm_kernel.cu @@ -41,7 +41,7 @@ namespace cub = hipcub; #define LAUNCH_BOUNDS(BlockDim) #endif -DECLARE_bool(cudnn_batchnorm_spatial_persistent); +PD_DECLARE_bool(cudnn_batchnorm_spatial_persistent); namespace phi { diff --git a/paddle/phi/kernels/gpu/c_embedding_grad_kernel.cu b/paddle/phi/kernels/gpu/c_embedding_grad_kernel.cu index aaa5c6865be47..66057db357e50 100644 --- a/paddle/phi/kernels/gpu/c_embedding_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/c_embedding_grad_kernel.cu @@ -14,7 +14,6 @@ #include "paddle/phi/kernels/c_embedding_kernel.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/backends/gpu/gpu_context.h" @@ -22,8 +21,9 @@ #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/embedding_grad.h" +#include "paddle/utils/flags.h" -DECLARE_int64(embedding_deterministic); +PD_DECLARE_int64(embedding_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/embedding_grad_kernel.cu b/paddle/phi/kernels/gpu/embedding_grad_kernel.cu index 99ba12b1d6213..a7c75e64a462a 100644 --- a/paddle/phi/kernels/gpu/embedding_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/embedding_grad_kernel.cu @@ -15,7 +15,6 @@ #include "paddle/phi/kernels/embedding_grad_kernel.h" #include "paddle/phi/kernels/funcs/embedding_grad.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_primitives.h" @@ -26,8 +25,9 @@ #include "paddle/phi/core/mixed_vector.h" #include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/embedding_util.h" +#include "paddle/utils/flags.h" -DECLARE_int64(embedding_deterministic); +PD_DECLARE_int64(embedding_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu b/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu index 60a1c54d72678..7b76a5f458ddd 100644 --- a/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu @@ -24,7 +24,7 @@ #include "paddle/phi/kernels/gpu/flash_attn_utils.h" #include "paddle/phi/kernels/reshape_kernel.h" -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/flash_attn_kernel.cu b/paddle/phi/kernels/gpu/flash_attn_kernel.cu index bcf8791d3c17f..4bd5e28c09fed 100644 --- a/paddle/phi/kernels/gpu/flash_attn_kernel.cu +++ b/paddle/phi/kernels/gpu/flash_attn_kernel.cu @@ -24,7 +24,7 @@ #include "paddle/phi/kernels/gpu/flash_attn_utils.h" #include "paddle/phi/kernels/reshape_kernel.h" -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/gelu_grad_kernel.cu b/paddle/phi/kernels/gpu/gelu_grad_kernel.cu index b1ffa921f912b..2b847fb216bb9 100644 --- a/paddle/phi/kernels/gpu/gelu_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/gelu_grad_kernel.cu @@ -21,7 +21,7 @@ #include "paddle/phi/kernels/funcs/broadcast_function.h" #include "paddle/phi/kernels/gpu/gelu_funcs.h" -DECLARE_bool(use_fast_math); +PD_DECLARE_bool(use_fast_math); namespace phi { diff --git a/paddle/phi/kernels/gpu/gelu_kernel.cu b/paddle/phi/kernels/gpu/gelu_kernel.cu index e0792c387d751..8400b5d8cd5a5 100644 --- a/paddle/phi/kernels/gpu/gelu_kernel.cu +++ b/paddle/phi/kernels/gpu/gelu_kernel.cu @@ -25,7 +25,7 @@ #include "paddle/phi/kernels/gpu/gelu_funcs.h" // clang-format on -DECLARE_bool(use_fast_math); +PD_DECLARE_bool(use_fast_math); namespace phi { diff --git a/paddle/phi/kernels/gpu/index_add_kernel.cu b/paddle/phi/kernels/gpu/index_add_kernel.cu index 9ba5bde7f6136..8fd15d5435f98 100644 --- a/paddle/phi/kernels/gpu/index_add_kernel.cu +++ b/paddle/phi/kernels/gpu/index_add_kernel.cu @@ -14,15 +14,15 @@ #include "paddle/phi/kernels/index_add_kernel.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" #include "paddle/phi/backends/gpu/gpu_primitives.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/utils/data_type.h" +#include "paddle/utils/flags.h" -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/index_select_grad_kernel.cu b/paddle/phi/kernels/gpu/index_select_grad_kernel.cu index 64e3428cc9aaf..03f74888fca0f 100644 --- a/paddle/phi/kernels/gpu/index_select_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/index_select_grad_kernel.cu @@ -14,7 +14,6 @@ #include "paddle/phi/kernels/index_select_grad_kernel.h" -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" @@ -22,8 +21,9 @@ #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/utils/data_type.h" #include "paddle/phi/kernels/funcs/math_function.h" +#include "paddle/utils/flags.h" -DECLARE_bool(cudnn_deterministic); +PD_DECLARE_bool(cudnn_deterministic); namespace phi { diff --git a/paddle/phi/kernels/gpu/layer_norm_kernel.cu b/paddle/phi/kernels/gpu/layer_norm_kernel.cu index c5bb0c288f260..eb85d9ac826d0 100644 --- a/paddle/phi/kernels/gpu/layer_norm_kernel.cu +++ b/paddle/phi/kernels/gpu/layer_norm_kernel.cu @@ -13,13 +13,13 @@ // limitations under the License. #include "paddle/phi/kernels/layer_norm_kernel.h" -#include "gflags/gflags.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/layer_norm_impl.cu.h" #include "paddle/phi/kernels/funcs/layer_norm_util.h" +#include "paddle/utils/flags.h" -DECLARE_bool(use_fast_math); +PD_DECLARE_bool(use_fast_math); namespace phi { diff --git a/paddle/phi/kernels/gpu/randperm_kernel.cu b/paddle/phi/kernels/gpu/randperm_kernel.cu index 2ad512701e097..4c6597b93f91f 100644 --- a/paddle/phi/kernels/gpu/randperm_kernel.cu +++ b/paddle/phi/kernels/gpu/randperm_kernel.cu @@ -26,7 +26,6 @@ namespace cub = hipcub; #endif -#include "gflags/gflags.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" #include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/common/memory_utils.h" @@ -34,6 +33,7 @@ namespace cub = hipcub; #include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/funcs/for_range.h" #include "paddle/phi/kernels/randint_kernel.h" +#include "paddle/utils/flags.h" namespace phi { diff --git a/paddle/phi/kernels/gpu/uniform_inplace_kernel.cu b/paddle/phi/kernels/gpu/uniform_inplace_kernel.cu index 653a64b127a25..87bae7fe5647f 100644 --- a/paddle/phi/kernels/gpu/uniform_inplace_kernel.cu +++ b/paddle/phi/kernels/gpu/uniform_inplace_kernel.cu @@ -16,11 +16,11 @@ limitations under the License. */ #include -#include "gflags/gflags.h" #include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/distribution_helper.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" +#include "paddle/utils/flags.h" namespace phi { diff --git a/paddle/phi/kernels/gpu/uniform_kernel.cu b/paddle/phi/kernels/gpu/uniform_kernel.cu index 04217db0a74c1..2a514947bb717 100644 --- a/paddle/phi/kernels/gpu/uniform_kernel.cu +++ b/paddle/phi/kernels/gpu/uniform_kernel.cu @@ -16,10 +16,10 @@ #include -#include "gflags/gflags.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/distribution_helper.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" +#include "paddle/utils/flags.h" namespace phi { diff --git a/paddle/phi/kernels/gpudnn/conv_gpudnn_info.h b/paddle/phi/kernels/gpudnn/conv_gpudnn_info.h index 9b7b35f4357cd..0dd3d7f0cdea1 100644 --- a/paddle/phi/kernels/gpudnn/conv_gpudnn_info.h +++ b/paddle/phi/kernels/gpudnn/conv_gpudnn_info.h @@ -20,9 +20,9 @@ limitations under the License. */ #include "paddle/phi/backends/gpu/gpu_dnn.h" -DECLARE_int64(conv_workspace_size_limit); -DECLARE_bool(cudnn_exhaustive_search); -DECLARE_int64(cudnn_exhaustive_search_times); +PD_DECLARE_int64(conv_workspace_size_limit); +PD_DECLARE_bool(cudnn_exhaustive_search); +PD_DECLARE_int64(cudnn_exhaustive_search_times); namespace phi { diff --git a/paddle/phi/kernels/impl/conv_cudnn_impl.h b/paddle/phi/kernels/impl/conv_cudnn_impl.h index c918eeec83121..81a7c7726203e 100644 --- a/paddle/phi/kernels/impl/conv_cudnn_impl.h +++ b/paddle/phi/kernels/impl/conv_cudnn_impl.h @@ -30,9 +30,9 @@ #include "paddle/phi/kernels/funcs/batch_norm_utils.h" #include "paddle/phi/kernels/funcs/padding.h" -DECLARE_bool(cudnn_deterministic); -DECLARE_int64(conv_workspace_size_limit); -DECLARE_bool(cudnn_exhaustive_search); +PD_DECLARE_bool(cudnn_deterministic); +PD_DECLARE_int64(conv_workspace_size_limit); +PD_DECLARE_bool(cudnn_exhaustive_search); namespace phi { diff --git a/paddle/phi/kernels/impl/einsum_impl.h b/paddle/phi/kernels/impl/einsum_impl.h index d297e786845bc..e32f64f347f4c 100644 --- a/paddle/phi/kernels/impl/einsum_impl.h +++ b/paddle/phi/kernels/impl/einsum_impl.h @@ -15,8 +15,8 @@ #include -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/kernels/diagonal_kernel.h" @@ -27,7 +27,7 @@ #include "paddle/phi/kernels/transpose_kernel.h" #include "paddle/utils/string/string_helper.h" -DECLARE_bool(einsum_opt); +PD_DECLARE_bool(einsum_opt); namespace phi { diff --git a/paddle/phi/kernels/legacy/gpu/uniform_kernel.cu b/paddle/phi/kernels/legacy/gpu/uniform_kernel.cu index 609238435c96f..abf51cf61f2b5 100644 --- a/paddle/phi/kernels/legacy/gpu/uniform_kernel.cu +++ b/paddle/phi/kernels/legacy/gpu/uniform_kernel.cu @@ -16,10 +16,10 @@ #include -#include "gflags/gflags.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/distribution_helper.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" +#include "paddle/utils/flags.h" namespace phi { diff --git a/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc b/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc index c4732db041edf..b93975c188b01 100644 --- a/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc +++ b/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc @@ -14,8 +14,8 @@ #include "paddle/phi/kernels/selected_rows/adam_kernel.h" -#include "gflags/gflags.h" #include "glog/logging.h" +#include "paddle/utils/flags.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -24,7 +24,7 @@ #include "paddle/phi/kernels/funcs/adam_functors.h" #include "paddle/phi/kernels/funcs/selected_rows_functor.h" -DECLARE_int32(inner_op_parallelism); +PD_DECLARE_int32(inner_op_parallelism); namespace phi { namespace sr { diff --git a/paddle/testing/paddle_gtest_main.cc b/paddle/testing/paddle_gtest_main.cc index b22f0c3a45049..6701693545e49 100644 --- a/paddle/testing/paddle_gtest_main.cc +++ b/paddle/testing/paddle_gtest_main.cc @@ -12,15 +12,15 @@ 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. */ -#include "gflags/gflags.h" #include "gtest/gtest.h" #include "paddle/fluid/framework/phi_utils.h" #include "paddle/fluid/memory/allocation/allocator_strategy.h" #include "paddle/fluid/platform/init.h" #include "paddle/phi/core/flags.h" +#include "paddle/utils/flags.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -DECLARE_bool(enable_gpu_memory_usage_log); +PD_DECLARE_bool(enable_gpu_memory_usage_log); #endif int main(int argc, char** argv) { // NOLINT @@ -32,11 +32,8 @@ int main(int argc, char** argv) { // NOLINT } std::vector envs; - std::vector undefok; #if defined(PADDLE_WITH_DISTRIBUTE) && !defined(PADDLE_WITH_PSLIB) - std::string str_max_body_size; - if (::GFLAGS_NAMESPACE::GetCommandLineOption("max_body_size", - &str_max_body_size)) { + if (paddle::flags::FindFlag("max_body_size")) { setenv("FLAGS_max_body_size", "2147483647", 1); envs.push_back("max_body_size"); } @@ -45,18 +42,8 @@ int main(int argc, char** argv) { // NOLINT const auto& flag_map = phi::GetExportedFlagInfoMap(); for (const auto& pair : flag_map) { const std::string& name = pair.second.name; - // NOTE(zhiqiu): some names may not linked in some tests, so add to - // `undefok`. - // One way to handle that is to check each flag item by item, and put it in - // `envs` or `undefok`; - // another way is to add all flags to `envs` and `undeok`, basically it is - // not a good design, - // but it can simplify the procedure of creating new flag and seems no side - // effects. - // see details: https://gflags.github.io/gflags/#special if (pair.second.is_writable) { // means public envs.push_back(name); - undefok.push_back(name); } } @@ -72,20 +59,8 @@ int main(int argc, char** argv) { // NOLINT VLOG(1) << "gtest env_string:" << env_string; } - char* undefok_str = nullptr; - if (!undefok.empty()) { - std::string undefok_string = "--undefok="; - for (auto t : undefok) { - undefok_string += t + ","; - } - undefok_string = undefok_string.substr(0, undefok_string.length() - 1); - undefok_str = strdup(undefok_string.c_str()); - new_argv.push_back(undefok_str); - VLOG(1) << "gtest undefok_string:" << undefok_string; - } - #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (strstr(undefok_str, "enable_gpu_memory_usage_log")) { + if (strstr(env_str, "enable_gpu_memory_usage_log")) { VLOG(1) << "Set FLAGS_enable_gpu_memory_usage_log to true"; FLAGS_enable_gpu_memory_usage_log = true; } @@ -93,8 +68,8 @@ int main(int argc, char** argv) { // NOLINT int new_argc = static_cast(new_argv.size()); char** new_argv_address = new_argv.data(); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags( - &new_argc, &new_argv_address, false); + paddle::flags::AllowUndefinedFlags(); + paddle::flags::ParseCommandLineFlags(&new_argc, &new_argv_address); paddle::framework::InitMemoryMethod(); paddle::framework::InitDevices(); paddle::framework::InitDefaultKernelSignatureMap(); @@ -102,6 +77,5 @@ int main(int argc, char** argv) { // NOLINT int ret = RUN_ALL_TESTS(); if (env_str) free(env_str); - if (undefok_str) free(undefok_str); return ret; } diff --git a/paddle/utils/CMakeLists.txt b/paddle/utils/CMakeLists.txt index dbb53a3ac368a..bb177e7578bb4 100644 --- a/paddle/utils/CMakeLists.txt +++ b/paddle/utils/CMakeLists.txt @@ -23,3 +23,11 @@ if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) SRCS pybind.cc DEPS phi) endif() + +if(NOT WITH_GFLAGS) + cc_library(paddle_flags SRCS flags_native.cc) + cc_test( + flags_native_test + SRCS flags_native_test.cc + DEPS paddle_flags) +endif() diff --git a/paddle/utils/flags.h b/paddle/utils/flags.h new file mode 100644 index 0000000000000..5a019f1439b43 --- /dev/null +++ b/paddle/utils/flags.h @@ -0,0 +1,77 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#ifdef PADDLE_WITH_GFLAGS +#include "gflags/gflags.h" +#else +#include "paddle/utils/flags_native.h" +#endif + +#ifdef PADDLE_WITH_GFLAGS +#define PD_DEFINE_bool(name, val, txt) DEFINE_bool(name, val, txt) +#define PD_DEFINE_int32(name, val, txt) DEFINE_int32(name, val, txt) +#define PD_DEFINE_uint32(name, val, txt) DEFINE_uint32(name, val, txt) +#define PD_DEFINE_int64(name, val, txt) DEFINE_int64(name, val, txt) +#define PD_DEFINE_uint64(name, val, txt) DEFINE_uint64(name, val, txt) +#define PD_DEFINE_double(name, val, txt) DEFINE_double(name, val, txt) +#define PD_DEFINE_string(name, val, txt) DEFINE_string(name, val, txt) + +#define PD_DECLARE_bool(name) DECLARE_bool(name) +#define PD_DECLARE_int32(name) DECLARE_int32(name) +#define PD_DECLARE_uint32(name) DECLARE_uint32(name) +#define PD_DECLARE_int64(name) DECLARE_int64(name) +#define PD_DECLARE_uint64(name) DECLARE_uint64(name) +#define PD_DECLARE_double(name) DECLARE_double(name) +#define PD_DECLARE_string(name) DECLARE_string(name) +#endif + +namespace paddle { +namespace flags { + +#ifdef PADDLE_WITH_GFLAGS +inline void ParseCommandLineFlags(int* argc, char*** argv) { + gflags::ParseCommandLineFlags(argc, argv, true); +} +#else +using paddle::flags::ParseCommandLineFlags; +#endif + +#ifdef PADDLE_WITH_GFLAGS +inline bool SetFlagValue(const char* name, const char* value) { + std::string ret = gflags::SetCommandLineOption(name, value); + return ret.empty() ? false : true; +} +#else +using paddle::flags::SetFlagValue; +#endif + +#ifdef PADDLE_WITH_GFLAGS +inline bool FindFlag(const char* name) { + std::string value; + return gflags::GetCommandLineOption(name, &value); +} +#else +using paddle::flags::FindFlag; +#endif + +#ifdef PADDLE_WITH_GFLAGS +inline void AllowUndefinedFlags() { gflags::AllowCommandLineReparsing(); } +#else +using paddle::flags::AllowUndefinedFlags; +#endif + +} // namespace flags +} // namespace paddle diff --git a/paddle/utils/flags_native.cc b/paddle/utils/flags_native.cc new file mode 100644 index 0000000000000..05d90d8adf21c --- /dev/null +++ b/paddle/utils/flags_native.cc @@ -0,0 +1,484 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/utils/flags_native.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace paddle { +namespace flags { + +std::stringstream& ErrorStream() { + static std::stringstream err_ss; + return err_ss; +} + +inline void exit_with_errors() { + std::cerr << ErrorStream().str(); + exit(-1); +} + +#define LOG_FLAG_ERROR(message) \ + ErrorStream() << "paddle flags error: " << message << " (at " << __FILE__ \ + << ":" << __LINE__ << ")" << std::endl + +#define LOG_FLAG_FATAL_ERROR(message) \ + LOG_FLAG_ERROR(message); \ + exit_with_errors() + +enum class FlagType : uint8_t { + BOOL = 0, + INT32 = 1, + UINT32 = 2, + INT64 = 3, + UINT64 = 4, + DOUBLE = 5, + STRING = 6, + UNDEFINED = 7, +}; + +class Flag { + public: + Flag(std::string name, + std::string description, + std::string file, + FlagType type, + const void* default_value, + void* value) + : name_(name), + description_(description), + file_(file), + type_(type), + default_value_(default_value), + value_(value) {} + ~Flag() = default; + + // Summary: --name_: type_, description_ (default: default_value_) + std::string Summary() const; + + void SetValueFromString(const std::string& value); + + private: + friend class FlagRegistry; + + const std::string name_; + const std::string description_; + const std::string file_; + const FlagType type_; + const void* default_value_; + void* value_; +}; + +class FlagRegistry { + public: + static FlagRegistry* Instance() { + static FlagRegistry* global_registry_ = new FlagRegistry(); + return global_registry_; + } + + void RegisterFlag(Flag* flag); + + bool SetFlagValue(const std::string& name, const std::string& value); + + bool HasFlag(const std::string& name) const; + + void PrintAllFlagHelp(std::ostream& os) const; + + private: + FlagRegistry() = default; + + std::map flags_; + + struct FlagCompare { + bool operator()(const Flag* flag1, const Flag* flag2) const { + return flag1->name_ < flag2->name_; + } + }; + + std::map> flags_by_file_; + + std::mutex mutex_; +}; + +template +struct FlagTypeTraits { + static constexpr FlagType Type = FlagType::UNDEFINED; +}; + +#define DEFINE_FLAG_TYPE_TRAITS(type, flag_type) \ + template <> \ + struct FlagTypeTraits { \ + static constexpr FlagType Type = flag_type; \ + } + +DEFINE_FLAG_TYPE_TRAITS(bool, FlagType::BOOL); +DEFINE_FLAG_TYPE_TRAITS(int32_t, FlagType::INT32); +DEFINE_FLAG_TYPE_TRAITS(uint32_t, FlagType::UINT32); +DEFINE_FLAG_TYPE_TRAITS(int64_t, FlagType::INT64); +DEFINE_FLAG_TYPE_TRAITS(uint64_t, FlagType::UINT64); +DEFINE_FLAG_TYPE_TRAITS(double, FlagType::DOUBLE); +DEFINE_FLAG_TYPE_TRAITS(std::string, FlagType::STRING); + +#undef DEFINE_FLAG_TYPE_TRAITS + +template +FlagRegisterer::FlagRegisterer(std::string name, + std::string help, + std::string file, + const T* default_value, + T* value) { + FlagType type = FlagTypeTraits::Type; + Flag* flag = new Flag(name, help, file, type, default_value, value); + FlagRegistry::Instance()->RegisterFlag(flag); +} + +// Instantiate FlagRegisterer for supported types. +#define INSTANTIATE_FLAG_REGISTERER(type) \ + template FlagRegisterer::FlagRegisterer(std::string name, \ + std::string help, \ + std::string file, \ + const type* default_value, \ + type* value) + +INSTANTIATE_FLAG_REGISTERER(bool); +INSTANTIATE_FLAG_REGISTERER(int32_t); +INSTANTIATE_FLAG_REGISTERER(uint32_t); +INSTANTIATE_FLAG_REGISTERER(int64_t); +INSTANTIATE_FLAG_REGISTERER(uint64_t); +INSTANTIATE_FLAG_REGISTERER(double); +INSTANTIATE_FLAG_REGISTERER(std::string); + +#undef INSTANTIATE_FLAG_REGISTERER + +std::string FlagType2String(FlagType type) { + switch (type) { + case FlagType::BOOL: + return "bool"; + case FlagType::INT32: + return "int32"; + case FlagType::UINT32: + return "uint32"; + case FlagType::INT64: + return "int64"; + case FlagType::UINT64: + return "uint64"; + case FlagType::DOUBLE: + return "double"; + case FlagType::STRING: + return "string"; + default: + return "undefined"; + } +} + +std::string Value2String(const void* value, FlagType type) { + switch (type) { + case FlagType::BOOL: { + const bool* val = static_cast(value); + return *val ? "true" : "false"; + } + case FlagType::INT32: { + const int32_t* val = static_cast(value); + return std::to_string(*val); + } + case FlagType::UINT32: { + const uint32_t* val = static_cast(value); + return std::to_string(*val); + } + case FlagType::INT64: { + const int64_t* val = static_cast(value); + return std::to_string(*val); + } + case FlagType::UINT64: { + const uint64_t* val = static_cast(value); + return std::to_string(*val); + } + case FlagType::DOUBLE: { + const double* val = static_cast(value); + return std::to_string(*val); + } + case FlagType::STRING: { + const std::string* val = static_cast(value); + return *val; + } + default: + LOG_FLAG_FATAL_ERROR("flag type is undefined."); + return ""; + } +} + +std::string Flag::Summary() const { + return "--" + name_ + ": " + FlagType2String(type_) + ", " + description_ + + " (default: " + Value2String(default_value_, type_) + ")"; +} + +void Flag::SetValueFromString(const std::string& value) { + try { + switch (type_) { + case FlagType::BOOL: { + bool* val = static_cast(value_); + if (value == "true" || value == "True" || value == "TRUE" || + value == "1") { + *val = true; + } else if (value == "false" || value == "False" || value == "FALSE" || + value == "0") { + *val = false; + } else { + throw std::invalid_argument( + ", please use [true, True, TRUE, 1] or [false, False, FALSE, " + "0]."); + } + break; + } + case FlagType::INT32: { + int32_t* val = static_cast(value_); + *val = std::stoi(value); + break; + } + case FlagType::UINT32: { + uint32_t* val = static_cast(value_); + *val = std::stoul(value); + break; + } + case FlagType::INT64: { + int64_t* val = static_cast(value_); + *val = std::stoll(value); + break; + } + case FlagType::UINT64: { + uint64_t* val = static_cast(value_); + *val = std::stoull(value); + break; + } + case FlagType::DOUBLE: { + double* val = static_cast(value_); + *val = std::stod(value); + break; + } + case FlagType::STRING: { + std::string* val = static_cast(value_); + *val = value; + break; + } + default: { + LOG_FLAG_FATAL_ERROR("flag type is undefined."); + } + } + } catch (const std::exception& e) { + std::string error_msg = "value: \"" + value + "\" is invalid for " + + FlagType2String(type_) + " flag \"" + name_ + "\""; + if (type_ == FlagType::BOOL) { + error_msg += e.what(); + } else { + error_msg += "."; + } + LOG_FLAG_ERROR(error_msg); + } +} + +void FlagRegistry::RegisterFlag(Flag* flag) { + auto iter = flags_.find(flag->name_); + if (iter != flags_.end()) { + LOG_FLAG_FATAL_ERROR("illegal RegisterFlag, flag \"" + flag->name_ + + "\" has been defined in " + iter->second->file_); + } else { + std::lock_guard lock(mutex_); + flags_[flag->name_] = flag; + flags_by_file_[flag->file_].insert(flag); + } +} + +bool FlagRegistry::SetFlagValue(const std::string& name, + const std::string& value) { + if (HasFlag(name)) { + std::lock_guard lock(mutex_); + flags_[name]->SetValueFromString(value); + return true; + } else { + LOG_FLAG_ERROR("illegal SetFlagValue, flag \"" + name + + "\" is not defined."); + return false; + } +} + +bool FlagRegistry::HasFlag(const std::string& name) const { + return flags_.find(name) != flags_.end(); +} + +void FlagRegistry::PrintAllFlagHelp(std::ostream& os) const { + for (const auto& iter : flags_by_file_) { + os << std::endl << "Flags defined in " << iter.first << ":" << std::endl; + for (const auto& flag : iter.second) { + os << " " << flag->Summary() << std::endl; + } + } + os << std::endl; +} + +void PrintAllFlagHelp(bool to_file, const std::string& file_path) { + if (to_file) { + std::ofstream fout(file_path); + FlagRegistry::Instance()->PrintAllFlagHelp(fout); + } else { + FlagRegistry::Instance()->PrintAllFlagHelp(std::cout); + } +} + +bool SetFlagValue(const std::string& name, const std::string& value) { + return FlagRegistry::Instance()->SetFlagValue(name, value); +} + +bool FindFlag(const std::string& name) { + return FlagRegistry::Instance()->HasFlag(name); +} + +bool GetValueFromEnv(const std::string& name, std::string* value) { + const char* env_var = std::getenv(name.c_str()); + if (env_var == nullptr) { + return false; + } + *value = std::string(env_var); + return true; +} + +void SetFlagsFromEnv(const std::vector& flags, bool error_fatal) { + bool success = true; + for (const std::string& flag_name : flags) { + std::string env_var_name = std::string("FLAGS_") + flag_name; + std::string env_var_value; + if (GetValueFromEnv(env_var_name, &env_var_value)) { + success = + FlagRegistry::Instance()->SetFlagValue(flag_name, env_var_value); + } else if (error_fatal) { + LOG_FLAG_ERROR("environment variable \"" + env_var_name + + "\" is not set."); + success = false; + } + } + if (error_fatal && !success) { + exit_with_errors(); + } +} + +static bool allow_undefined_flags = false; + +void AllowUndefinedFlags() { allow_undefined_flags = true; } + +void ParseCommandLineFlags(int* pargc, char*** pargv) { + assert(*pargc > 0); + size_t argv_num = *pargc - 1; + std::vector argvs(*pargv + 1, *pargv + *pargc); + + std::string arg_format_help = + "please follow the formats: \"--help(h)\", \"--name=value\"" + " or \"--name value\"."; + for (size_t i = 0; i < argv_num; i++) { + const std::string& argv = argvs[i]; + + if (argv.size() < 2 || argv[0] != '-') { + LOG_FLAG_FATAL_ERROR("invalid commandline argument: \"" + argv + "\", " + + arg_format_help); + } + + // parse arg name and value + size_t hyphen_num = argv[1] == '-' ? 2 : 1; + std::string name, value; + size_t split_pos = argv.find('='); + if (split_pos == std::string::npos) { + // the argv format is "--name" or "--name value" + name = argv.substr(hyphen_num); + if (name.empty()) { + LOG_FLAG_FATAL_ERROR("invalid commandline argument: \"" + argv + + "\", " + arg_format_help); + } + + // print help message + if (name == "help" || name == "h") { + FlagRegistry::Instance()->PrintAllFlagHelp(std::cout); + exit(1); + } + + // get the value from next argv. + if (++i == argv_num) { + LOG_FLAG_FATAL_ERROR("expected value of flag \"" + name + + "\" but found none."); + } else { + value = argvs[i]; + } + } else { + // the argv format is "--name=value" + if (split_pos == hyphen_num || split_pos == argv.size() - 1) { + LOG_FLAG_FATAL_ERROR("invalid commandline argument: \"" + argv + + "\", " + arg_format_help); + } + name = argv.substr(hyphen_num, split_pos - hyphen_num); + value = argv.substr(split_pos + 1); + } + + // special case for flag value enclosed in "" + if (value[0] == '"') { + value = value.substr(1); + if (value.back() == '"') { + value.pop_back(); + } else { + while (i < argv_num) { + value += " "; + value += argvs[++i]; + if (value.back() == '"') { + break; + } + } + if (value.back() == '"') { + value.pop_back(); + } else { + LOG_FLAG_FATAL_ERROR("unexperted end of flag \"" + name + + "\" value while looking for matching `\"'"); + } + } + } + + if (name == "fromenv" || name == "tryfromenv") { + // Value of --fromenv or --tryfromenv should be + // a comma separated list of env var names. + std::vector env_flag_names; + for (size_t start_pos = 0, end_pos = 0; + start_pos < value.size() && end_pos != std::string::npos; + start_pos = end_pos + 1) { + end_pos = value.find(',', start_pos); + env_flag_names.push_back(value.substr(start_pos, end_pos - start_pos)); + } + if (name == "fromenv") { + SetFlagsFromEnv(env_flag_names, true); + } else { + SetFlagsFromEnv(env_flag_names, false); + } + continue; + } + + FlagRegistry::Instance()->SetFlagValue(name, value); + } + if (!allow_undefined_flags && !ErrorStream().str().empty()) { + exit_with_errors(); + } +} + +} // namespace flags +} // namespace paddle diff --git a/paddle/utils/flags_native.h b/paddle/utils/flags_native.h new file mode 100644 index 0000000000000..04814a4f67950 --- /dev/null +++ b/paddle/utils/flags_native.h @@ -0,0 +1,131 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include + +// This is a simple commandline flags tool for paddle, which is inspired by +// gflags but only implements the following necessary features: +// 1. Define or declare a flag. +// 2. Parse commandline flags. +// 3. Other utility functions. + +namespace paddle { +namespace flags { +/** + * @brief Parse commandline flags. + * + * It recieves commandline arguments passed in argc and argv from main function, + * argv[0] is the program name, and argv[1:] are the commandline arguments + * which matching the format "--name=value" or "--name value". After parsing, + * the corresponding flag value will be reset. + */ +void ParseCommandLineFlags(int* argc, char*** argv); + +/** + * @brief Allow undefined flags in ParseCommandLineFlags() + */ +void AllowUndefinedFlags(); + +/** + * @brief Set flags from environment variables. + * + * It recieves a list of flags name, and will find the corresponding environment + * variables named "FLAGS_name", if found, it will set the environment variable + * values to the flags. If error_fatal is true, the program will exit when the + * environment variable is not set or the flag is not defined, that is the same + * effect as using commandline argument "--fromenv=var_name1,var_name2,...". + * Otherwise, the errors above will be ignored, that is the same effect as using + * commandline argument "--tryfromenv=var_name1,var_name2,...". + */ +void SetFlagsFromEnv(const std::vector& flags, bool error_fatal); + +/** + * @brief Set Single flag value, return true if success. + */ +bool SetFlagValue(const std::string& name, const std::string& value); + +/** + * @brief Find flag by name, return true if found. + */ +bool FindFlag(const std::string& name); + +/** + * @brief Print all registered flags' help message. If to_file is true, + * write help message to file. + */ +void PrintAllFlagHelp(bool to_file = false, + const std::string& file_name = "all_flags.txt"); +} // namespace flags +} // namespace paddle + +// ----------------------------DECLARE FLAGS---------------------------- +#define PD_DECLARE_VARIABLE(type, name) \ + namespace paddle { \ + namespace flags { \ + extern type FLAGS_##name; \ + } \ + } \ + using paddle::flags::FLAGS_##name + +#define PD_DECLARE_bool(name) PD_DECLARE_VARIABLE(bool, name) +#define PD_DECLARE_int32(name) PD_DECLARE_VARIABLE(int32_t, name) +#define PD_DECLARE_uint32(name) PD_DECLARE_VARIABLE(uint32_t, name) +#define PD_DECLARE_int64(name) PD_DECLARE_VARIABLE(int64_t, name) +#define PD_DECLARE_uint64(name) PD_DECLARE_VARIABLE(uint64_t, name) +#define PD_DECLARE_double(name) PD_DECLARE_VARIABLE(double, name) +#define PD_DECLARE_string(name) PD_DECLARE_VARIABLE(std::string, name) + +namespace paddle { +namespace flags { +class FlagRegisterer { + public: + template + FlagRegisterer(std::string name, + std::string description, + std::string file, + const T* default_value, + T* value); +}; +} // namespace flags +} // namespace paddle + +// ----------------------------DEFINE FLAGS---------------------------- +#define PD_DEFINE_VARIABLE(type, name, default_value, description) \ + namespace paddle { \ + namespace flags { \ + static const type FLAGS_##name##_default = default_value; \ + type FLAGS_##name = default_value; \ + /* Register FLAG */ \ + static ::paddle::flags::FlagRegisterer flag_##name##_registerer( \ + #name, description, __FILE__, &FLAGS_##name##_default, &FLAGS_##name); \ + } \ + } \ + using paddle::flags::FLAGS_##name + +#define PD_DEFINE_bool(name, val, txt) PD_DEFINE_VARIABLE(bool, name, val, txt) +#define PD_DEFINE_int32(name, val, txt) \ + PD_DEFINE_VARIABLE(int32_t, name, val, txt) +#define PD_DEFINE_uint32(name, val, txt) \ + PD_DEFINE_VARIABLE(uint32_t, name, val, txt) +#define PD_DEFINE_int64(name, val, txt) \ + PD_DEFINE_VARIABLE(int64_t, name, val, txt) +#define PD_DEFINE_uint64(name, val, txt) \ + PD_DEFINE_VARIABLE(uint64_t, name, val, txt) +#define PD_DEFINE_double(name, val, txt) \ + PD_DEFINE_VARIABLE(double, name, val, txt) +#define PD_DEFINE_string(name, val, txt) \ + PD_DEFINE_VARIABLE(std::string, name, val, txt) diff --git a/paddle/utils/flags_native_test.cc b/paddle/utils/flags_native_test.cc new file mode 100644 index 0000000000000..2b26a8cd1e9db --- /dev/null +++ b/paddle/utils/flags_native_test.cc @@ -0,0 +1,104 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/utils/flags_native.h" + +#include +#include "gtest/gtest.h" + +PD_DEFINE_int32(paddle_test_int32, 1, "test int32 flag"); +PD_DEFINE_uint32(paddle_test_uint32, 2, "test uint32 flag"); +PD_DEFINE_string(paddle_test_string, "raw", "test string flag"); + +using namespace paddle::flags; // NOLINT + +void SplitCommandlineArg(const std::string& commandline, + int* argc, + char*** argv) { + static std::vector args; + args.clear(); + for (size_t start_pos = 0, end_pos = 0; + start_pos < commandline.size() && end_pos != std::string::npos; + start_pos = end_pos + 1) { + end_pos = commandline.find(' ', start_pos); + args.push_back(commandline.substr(start_pos, end_pos - start_pos)); + } + *argc = args.size(); + *argv = new char*[*argc]; + for (size_t i = 0; i < args.size(); i++) { + (*argv)[i] = const_cast(args[i].c_str()); + } +} + +TEST(flags_native_test, ParseCommandLineFlags) { + uint32_t test_int32 = 2; + ASSERT_EQ(FLAGS_paddle_test_int32, 1); + ASSERT_EQ(FLAGS_paddle_test_uint32, test_int32); + ASSERT_EQ(FLAGS_paddle_test_string, "raw"); + + // Construct commandline arguments input + std::string commandline = + "test --paddle_test_int32=3 --paddle_test_uint32=\"4\" " + "--paddle_test_string \"modified string\""; + int argc; + char** argv; + SplitCommandlineArg(commandline, &argc, &argv); + + // Parse commandline flags and check + ParseCommandLineFlags(&argc, &argv); + delete argv; + + test_int32 = 4; + ASSERT_EQ(FLAGS_paddle_test_int32, 3); + ASSERT_EQ(FLAGS_paddle_test_uint32, test_int32); + ASSERT_EQ(FLAGS_paddle_test_string, "modified string"); + + // test FindFlag and SetFlagValue + ASSERT_TRUE(FindFlag("paddle_test_int32")); + + SetFlagValue("paddle_test_int32", "9"); + ASSERT_EQ(FLAGS_paddle_test_int32, 9); +} + +#if defined(_POSIX_C_SOURCE) && \ + _POSIX_C_SOURCE >= 200112L // environment for use setenv +bool SetEnvVar(const std::string& var_name, const std::string& var_value) { + int res = setenv(var_name.c_str(), var_value.c_str(), 1); + return res == 0; +} + +PD_DEFINE_bool(paddle_test_env_bool, false, "test env bool flag"); +PD_DEFINE_double(paddle_test_env_double, 3.14, "test env double flag"); + +TEST(flags_native_test, SetFlagsFromEnv) { + ASSERT_EQ(FLAGS_paddle_test_env_bool, false); + ASSERT_EQ(FLAGS_paddle_test_env_double, 3.14); + + ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_bool", "true")); + ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_double", "2.71")); + + std::string commandline = + "test --fromenv=paddle_test_env_bool,paddle_test_env_double"; + int argc; + char** argv; + SplitCommandlineArg(commandline, &argc, &argv); + ParseCommandLineFlags(&argc, &argv); + delete argv; + + ASSERT_EQ(FLAGS_paddle_test_env_bool, true); + ASSERT_EQ(FLAGS_paddle_test_env_double, 2.71); +} +#endif + +TEST(flags_native_test, PrintAllFlagHelp) { PrintAllFlagHelp(); } diff --git a/paddle/utils/string/pretty_log.cc b/paddle/utils/string/pretty_log.cc index 44bbbd0cc1bb6..bb84ad201848c 100644 --- a/paddle/utils/string/pretty_log.cc +++ b/paddle/utils/string/pretty_log.cc @@ -14,9 +14,9 @@ #include "paddle/utils/string/pretty_log.h" -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" -DEFINE_bool(color, true, "Whether to turn on pretty log"); // NOLINT +PD_DEFINE_bool(color, true, "Whether to turn on pretty log"); // NOLINT namespace paddle { namespace string {} // namespace string diff --git a/paddle/utils/string/pretty_log.h b/paddle/utils/string/pretty_log.h index 9de7ce24abd72..546bf1eec7d1d 100644 --- a/paddle/utils/string/pretty_log.h +++ b/paddle/utils/string/pretty_log.h @@ -18,10 +18,10 @@ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/utils/string/printf.h" -DECLARE_bool(color); +PD_DECLARE_bool(color); namespace paddle { diff --git a/test/cpp/fluid/benchmark/op_tester.cc b/test/cpp/fluid/benchmark/op_tester.cc index 0ab90f03999da..6f68ab23a4566 100644 --- a/test/cpp/fluid/benchmark/op_tester.cc +++ b/test/cpp/fluid/benchmark/op_tester.cc @@ -16,7 +16,6 @@ limitations under the License. */ #include -#include "gflags/gflags.h" #include "gtest/gtest.h" #include "paddle/fluid/framework/op_info.h" #include "paddle/fluid/framework/op_registry.h" @@ -25,6 +24,7 @@ limitations under the License. */ #include "paddle/fluid/platform/profiler.h" #include "paddle/fluid/platform/timer.h" #include "paddle/fluid/pybind/pybind.h" +#include "paddle/utils/flags.h" // phi #include "paddle/phi/kernels/declarations.h" @@ -33,8 +33,8 @@ namespace paddle { namespace operators { namespace benchmark { -DEFINE_string(op_config_list, "", "Path of op config file."); // NOLINT -DEFINE_int32(specified_config_id, -1, "Test the specified op config."); +PD_DEFINE_string(op_config_list, "", "Path of op config file."); // NOLINT +PD_DEFINE_int32(specified_config_id, -1, "Test the specified op config."); void OpTester::Init(const std::string &filename) { Init(OpTesterConfig(filename)); @@ -57,13 +57,13 @@ void OpTester::Init(const OpTesterConfig &config) { } if (config_.device_id >= 0) { - place_ = paddle::platform::CUDAPlace(config_.device_id); + place_ = ::paddle::platform::CUDAPlace(config_.device_id); } else { - place_ = paddle::platform::CPUPlace(); + place_ = ::paddle::platform::CPUPlace(); } framework::InitDevices(); - scope_ = std::make_unique(); + scope_ = std::make_unique<::paddle::framework::Scope>(); op_ = framework::OpRegistry::CreateOp(op_desc_); CreateVariables(scope_.get()); @@ -318,7 +318,7 @@ void OpTester::SetupTensor(phi::DenseTensor *tensor, } if (!platform::is_cpu_place(place_)) { - paddle::framework::TensorCopySync(cpu_tensor, place_, tensor); + ::paddle::framework::TensorCopySync(cpu_tensor, place_, tensor); } } diff --git a/test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc b/test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc index 6f1ac7e56f304..d6ba2f7a0c232 100644 --- a/test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc +++ b/test/cpp/fluid/mkldnn/test_mkldnn_cpu_quantize_pass.cc @@ -29,7 +29,7 @@ using std::pair; using std::string; using std::unordered_map; -DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); +PD_DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); namespace paddle { namespace pass { diff --git a/test/cpp/fluid/pscore/CMakeLists.txt b/test/cpp/fluid/pscore/CMakeLists.txt index c19df6b469605..07d3efaa31110 100644 --- a/test/cpp/fluid/pscore/CMakeLists.txt +++ b/test/cpp/fluid/pscore/CMakeLists.txt @@ -18,7 +18,7 @@ if(WITH_ARM_BRPC) framework_proto sendrecv_rpc arm_brpc - gflags + ${flags_dep} glog snappy device_context) @@ -35,15 +35,9 @@ else() ps_framework_proto framework_proto sendrecv_rpc - brpc - leveldb - ssl - crypto - protobuf - gflags - glog + ${EXTERNAL_BRPC_DEPS} + ${flags_dep} zlib - snappy device_context) endif() diff --git a/test/cpp/fluid/pscore/switch_server_test.cc b/test/cpp/fluid/pscore/switch_server_test.cc index a5e6fff4804af..5ea2e28d4543b 100644 --- a/test/cpp/fluid/pscore/switch_server_test.cc +++ b/test/cpp/fluid/pscore/switch_server_test.cc @@ -21,18 +21,18 @@ limitations under the License. */ #include #include // NOLINT -#include "gflags/gflags.h" #include "gtest/gtest.h" #include "paddle/fluid/distributed/ps/service/heter_client.h" #include "paddle/fluid/distributed/ps/service/heter_server.h" +#include "paddle/utils/flags.h" namespace framework = paddle::framework; namespace platform = paddle::platform; namespace distributed = paddle::distributed; -DEFINE_string(switch_addr_inner, "127.0.0.1:6000", "addr of inner cluster"); -DEFINE_string(switch_addr_heter, "127.0.0.1:6100", "add of inter cluster"); -DEFINE_string(peer_switch_addr, "127.0.0.1:7100", "add of inter cluster"); +PD_DEFINE_string(switch_addr_inner, "127.0.0.1:6000", "addr of inner cluster"); +PD_DEFINE_string(switch_addr_heter, "127.0.0.1:6100", "add of inter cluster"); +PD_DEFINE_string(peer_switch_addr, "127.0.0.1:7100", "add of inter cluster"); void StartSwitchServer( std::shared_ptr& switch_server_ptr, // NOLINT @@ -61,7 +61,7 @@ int main(int argc, char* argv[]) { framework::ProgramDesc program; exe.Prepare(program, 0); // solve undefined symbol: tensor_table.cc - google::ParseCommandLineFlags(&argc, &argv, true); + paddle::flags::ParseCommandLineFlags(&argc, &argv); std::string switch_a_endpoint(FLAGS_switch_addr_inner); std::string switch_a_endpoint_inter(FLAGS_switch_addr_heter); diff --git a/test/cpp/inference/api/analysis_predictor_tester.cc b/test/cpp/inference/api/analysis_predictor_tester.cc index 2f794a4c784fa..35c07c3a83790 100644 --- a/test/cpp/inference/api/analysis_predictor_tester.cc +++ b/test/cpp/inference/api/analysis_predictor_tester.cc @@ -31,7 +31,7 @@ #include "paddle/phi/backends/cpu/cpu_info.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_string(dirname, "", "dirname to tests."); +PD_DEFINE_string(dirname, "", "dirname to tests."); namespace paddle { diff --git a/test/cpp/inference/api/analyzer_bfloat16_image_classification_tester.cc b/test/cpp/inference/api/analyzer_bfloat16_image_classification_tester.cc index f6d4d8e8f769f..fe7d2a3a6f6cf 100644 --- a/test/cpp/inference/api/analyzer_bfloat16_image_classification_tester.cc +++ b/test/cpp/inference/api/analyzer_bfloat16_image_classification_tester.cc @@ -16,7 +16,7 @@ limitations under the License. */ #include "paddle/phi/backends/cpu/cpu_info.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); +PD_DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_dam_tester.cc b/test/cpp/inference/api/analyzer_dam_tester.cc index 21276c71f63ec..d17f8670adcf4 100644 --- a/test/cpp/inference/api/analyzer_dam_tester.cc +++ b/test/cpp/inference/api/analyzer_dam_tester.cc @@ -130,7 +130,7 @@ void PrepareInputs(std::vector *input_slots, auto one_batch = data->NextBatch(); PADDLE_ENFORCE( !one_batch.response.empty(), - paddle::platform::errors::Fatal("The response of one batch is empty.")); + ::paddle::platform::errors::Fatal("The response of one batch is empty.")); int size = one_batch.response[0].size(); CHECK_EQ(size, kMaxTurnLen); // turn tensor assignment @@ -228,17 +228,17 @@ void profile(bool use_mkldnn = false) { if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) { PADDLE_ENFORCE_GT(outputs.size(), 0, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of outputs should be greater than 0.")); auto output = outputs.back(); PADDLE_ENFORCE_GT(output.size(), 0, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of output should be greater than 0.")); size_t size = GetSize(output[0]); PADDLE_ENFORCE_GT(size, 0, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of output should be greater than 0.")); float *result = static_cast(output[0].data.data()); for (size_t i = 0; i < size; i++) { diff --git a/test/cpp/inference/api/analyzer_detect_functional_mkldnn_tester.cc b/test/cpp/inference/api/analyzer_detect_functional_mkldnn_tester.cc index 389e0c9648d29..8d5627fce3cd8 100644 --- a/test/cpp/inference/api/analyzer_detect_functional_mkldnn_tester.cc +++ b/test/cpp/inference/api/analyzer_detect_functional_mkldnn_tester.cc @@ -20,8 +20,8 @@ limitations under the License. */ #include "paddle/phi/common/place.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_string(infer_shape, "", "data shape file"); -DEFINE_int32(sample, 20, "number of sample"); +PD_DEFINE_string(infer_shape, "", "data shape file"); +PD_DEFINE_int32(sample, 20, "number of sample"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_detect_tester.cc b/test/cpp/inference/api/analyzer_detect_tester.cc index 62b97d635b5e2..72e498521cd6a 100644 --- a/test/cpp/inference/api/analyzer_detect_tester.cc +++ b/test/cpp/inference/api/analyzer_detect_tester.cc @@ -19,8 +19,8 @@ limitations under the License. */ #include "test/cpp/inference/api/tester_helper.h" -DEFINE_string(infer_shape, "", "data shape file"); -DEFINE_int32(sample, 20, "number of sample"); +PD_DEFINE_string(infer_shape, "", "data shape file"); +PD_DEFINE_int32(sample, 20, "number of sample"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_image_classification_tester.cc b/test/cpp/inference/api/analyzer_image_classification_tester.cc index 93b8f92c797d9..1df6d4488614d 100644 --- a/test/cpp/inference/api/analyzer_image_classification_tester.cc +++ b/test/cpp/inference/api/analyzer_image_classification_tester.cc @@ -17,7 +17,7 @@ limitations under the License. */ #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(disable_mkldnn_fc, false, "Disable usage of MKL-DNN's FC op"); +PD_DEFINE_bool(disable_mkldnn_fc, false, "Disable usage of MKL-DNN's FC op"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_int8_image_classification_tester.cc b/test/cpp/inference/api/analyzer_int8_image_classification_tester.cc index 57552ccb82e60..77c12dcfe0f52 100644 --- a/test/cpp/inference/api/analyzer_int8_image_classification_tester.cc +++ b/test/cpp/inference/api/analyzer_int8_image_classification_tester.cc @@ -18,7 +18,7 @@ limitations under the License. */ #include "paddle/fluid/inference/api/paddle_analysis_config.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); +PD_DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); namespace paddle { namespace inference { @@ -53,7 +53,7 @@ TEST(Analyzer_int8_image_classification, quantization) { // prepare warmup batch from input data read earlier // warmup batch size can be different than batch size std::shared_ptr> warmup_data = - paddle::inference::GetWarmupData(input_slots_all); + ::paddle::inference::GetWarmupData(input_slots_all); // INT8 implies FC oneDNN passes to be used q_cfg.pass_builder()->AppendPass("fc_mkldnn_pass"); diff --git a/test/cpp/inference/api/analyzer_int8_object_detection_tester.cc b/test/cpp/inference/api/analyzer_int8_object_detection_tester.cc index a3b13dd691cd7..311fb0946ca00 100644 --- a/test/cpp/inference/api/analyzer_int8_object_detection_tester.cc +++ b/test/cpp/inference/api/analyzer_int8_object_detection_tester.cc @@ -18,7 +18,7 @@ limitations under the License. */ #include "paddle/fluid/inference/api/paddle_analysis_config.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); +PD_DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); // setting iterations to 0 means processing the whole dataset namespace paddle { @@ -153,7 +153,7 @@ std::shared_ptr> GetWarmupData( PADDLE_ENFORCE_LE( static_cast(num_images), iterations * test_data_batch_size, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The requested quantization warmup data size " + std::to_string(num_images) + " is bigger than all test data size.")); @@ -247,9 +247,9 @@ std::shared_ptr> GetWarmupData( PADDLE_ENFORCE_EQ( static_cast(num_objects), static_cast(objects_accum), - paddle::platform::errors::Fatal("The requested num of objects " + - std::to_string(num_objects) + - " is the same as objects_accum.")); + ::paddle::platform::errors::Fatal("The requested num of objects " + + std::to_string(num_objects) + + " is the same as objects_accum.")); auto warmup_data = std::make_shared>(4); (*warmup_data)[0] = std::move(images); diff --git a/test/cpp/inference/api/analyzer_lac_tester.cc b/test/cpp/inference/api/analyzer_lac_tester.cc index fb82bbc3b2d94..9bdb819e5fbd6 100644 --- a/test/cpp/inference/api/analyzer_lac_tester.cc +++ b/test/cpp/inference/api/analyzer_lac_tester.cc @@ -99,10 +99,10 @@ void GetOneBatch(std::vector *input_slots, input_tensor.name = "word"; input_tensor.dtype = PaddleDType::INT64; TensorAssignData(&input_tensor, {one_batch.data}, one_batch.lod); - PADDLE_ENFORCE_EQ( - batch_size, - static_cast(one_batch.lod.size() - 1), - paddle::platform::errors::Fatal("The lod size of one batch is invaild.")); + PADDLE_ENFORCE_EQ(batch_size, + static_cast(one_batch.lod.size() - 1), + ::paddle::platform::errors::Fatal( + "The lod size of one batch is invaild.")); input_slots->assign({input_tensor}); } @@ -145,19 +145,19 @@ TEST(Analyzer_LAC, profile) { 15, 44, 38, 39, 14, 15, 44, 22, 23, 23, 23, 23, 23, 23, 23}; PADDLE_ENFORCE_GT(outputs.size(), 0, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of output should be greater than 0.")); auto output = outputs.back(); PADDLE_ENFORCE_EQ(output.size(), 1UL, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of output should be equal to 1.")); size_t size = GetSize(output[0]); size_t batch1_size = sizeof(lac_ref_data) / sizeof(int64_t); PADDLE_ENFORCE_GE( size, batch1_size, - paddle::platform::errors::Fatal("The size of batch is invaild.")); + ::paddle::platform::errors::Fatal("The size of batch is invaild.")); int64_t *pdata = static_cast(output[0].data.data()); for (size_t i = 0; i < batch1_size; ++i) { EXPECT_EQ(pdata[i], lac_ref_data[i]); diff --git a/test/cpp/inference/api/analyzer_mmp_tester.cc b/test/cpp/inference/api/analyzer_mmp_tester.cc index a432d5c10b229..92345fc8950a5 100644 --- a/test/cpp/inference/api/analyzer_mmp_tester.cc +++ b/test/cpp/inference/api/analyzer_mmp_tester.cc @@ -18,8 +18,8 @@ #include "test/cpp/inference/api/tester_helper.h" // Here add missing commands -DEFINE_string(infer_model2, "", "model path"); -DEFINE_string(infer_model3, "", "model path"); +PD_DEFINE_string(infer_model2, "", "model path"); +PD_DEFINE_string(infer_model3, "", "model path"); namespace paddle { namespace inference { @@ -100,12 +100,12 @@ void compare(bool use_mkldnn = false) { xx2_output.begin(), [](const float& l, const float& r) { return fabs(l - r) < 1e-4; }); - PADDLE_ENFORCE_EQ( - result, - true, - paddle::platform::errors::Fatal("Results of model run independently " - "differs from results of the same model " - "run as a sequence of models")); + PADDLE_ENFORCE_EQ(result, + true, + ::paddle::platform::errors::Fatal( + "Results of model run independently " + "differs from results of the same model " + "run as a sequence of models")); } TEST(Analyzer_mmp, compare) { compare(); } diff --git a/test/cpp/inference/api/analyzer_quant_image_classification_tester.cc b/test/cpp/inference/api/analyzer_quant_image_classification_tester.cc index 69b627275cd05..e9b841ec772ab 100644 --- a/test/cpp/inference/api/analyzer_quant_image_classification_tester.cc +++ b/test/cpp/inference/api/analyzer_quant_image_classification_tester.cc @@ -18,7 +18,7 @@ limitations under the License. */ #include "paddle/fluid/inference/api/paddle_analysis_config.h" #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); +PD_DEFINE_bool(enable_mkldnn, true, "Enable MKLDNN"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_rnn1_tester.cc b/test/cpp/inference/api/analyzer_rnn1_tester.cc index 7f0f11f5515bd..c5c7df887dd5e 100644 --- a/test/cpp/inference/api/analyzer_rnn1_tester.cc +++ b/test/cpp/inference/api/analyzer_rnn1_tester.cc @@ -14,7 +14,7 @@ #include "test/cpp/inference/api/tester_helper.h" -DEFINE_bool(with_precision_check, true, "turn on test"); +PD_DEFINE_bool(with_precision_check, true, "turn on test"); namespace paddle { namespace inference { diff --git a/test/cpp/inference/api/analyzer_seq_pool1_tester_helper.h b/test/cpp/inference/api/analyzer_seq_pool1_tester_helper.h index 3c0ad4b5f823d..0d75eacbbdf4e 100644 --- a/test/cpp/inference/api/analyzer_seq_pool1_tester_helper.h +++ b/test/cpp/inference/api/analyzer_seq_pool1_tester_helper.h @@ -65,7 +65,7 @@ struct DataRecord { PADDLE_ENFORCE_EQ( slot_data.size() % 11, 0UL, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "line %d, %s should be divisible", num_lines, name)); datasets[name].emplace_back(std::move(slot_data)); } @@ -73,19 +73,19 @@ struct DataRecord { PADDLE_ENFORCE_EQ( num_samples * num_slots, static_cast(num_lines), - paddle::platform::errors::Fatal("num samples should be divisible")); + ::paddle::platform::errors::Fatal("num samples should be divisible")); PADDLE_ENFORCE_GT(num_samples, 0UL, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The num of samples should be greater than 0.")); } void Prepare(int bs) { for (auto it = datasets.begin(); it != datasets.end(); ++it) { - PADDLE_ENFORCE_EQ( - it->second.size(), - num_samples, - paddle::platform::errors::Fatal("size of each slot should be equal")); + PADDLE_ENFORCE_EQ(it->second.size(), + num_samples, + ::paddle::platform::errors::Fatal( + "size of each slot should be equal")); } size_t num_batches = num_samples / bs; EXPECT_GT(num_batches, 0UL); @@ -110,7 +110,7 @@ struct DataRecord { PADDLE_ENFORCE_EQ( len * 11, datas[id].size(), - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "%s %d size should be divisible", slot.name, id)); lod[k + 1] = lod[k] + len; } diff --git a/test/cpp/inference/api/analyzer_vis_tester.cc b/test/cpp/inference/api/analyzer_vis_tester.cc index cf79d26847f45..d8b15393ad601 100644 --- a/test/cpp/inference/api/analyzer_vis_tester.cc +++ b/test/cpp/inference/api/analyzer_vis_tester.cc @@ -64,7 +64,7 @@ void SetInput(std::vector> *inputs) { PADDLE_ENFORCE_EQ( FLAGS_test_all_data, 0, - paddle::platform::errors::Fatal("Only have single batch of data.")); + ::paddle::platform::errors::Fatal("Only have single batch of data.")); std::string line; std::ifstream file(FLAGS_infer_data); std::getline(file, line); @@ -107,7 +107,7 @@ void profile(bool use_mkldnn = false) { PADDLE_ENFORCE_GT(outputs.size(), 0, - paddle::platform::errors::Fatal( + ::paddle::platform::errors::Fatal( "The size of output should be greater than 0.")); auto &output = outputs.back().front(); size_t numel = output.data.length() / PaddleDtypeSize(output.dtype); diff --git a/test/cpp/inference/api/api_impl_tester.cc b/test/cpp/inference/api/api_impl_tester.cc index 934ff06535054..78e908189cc1d 100644 --- a/test/cpp/inference/api/api_impl_tester.cc +++ b/test/cpp/inference/api/api_impl_tester.cc @@ -17,9 +17,9 @@ limitations under the License. */ #include // NOLINT -#include "gflags/gflags.h" #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/inference/api/api_impl.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/test_helper.h" #ifdef __clang__ @@ -28,10 +28,10 @@ limitations under the License. */ #define ACC_DIFF 1e-3 #endif -DEFINE_string(word2vec_dirname, - "", - "Directory of the word2vec inference model."); -DEFINE_string(book_dirname, "", "Directory of the book inference model."); +PD_DEFINE_string(word2vec_dirname, + "", + "Directory of the word2vec inference model."); +PD_DEFINE_string(book_dirname, "", "Directory of the book inference model."); namespace paddle { @@ -67,11 +67,11 @@ NativeConfig GetConfig() { return config; } -void MainWord2Vec(const paddle::PaddlePlace& place) { +void MainWord2Vec(const ::paddle::PaddlePlace& place) { NativeConfig config = GetConfig(); auto predictor = CreatePaddlePredictor(config); - config.use_gpu = paddle::gpu_place_used(place); - config.use_xpu = paddle::xpu_place_used(place); + config.use_gpu = ::paddle::gpu_place_used(place); + config.use_xpu = ::paddle::xpu_place_used(place); phi::DenseTensor first_word, second_word, third_word, fourth_word; framework::LoD lod{{0, 1}}; @@ -105,7 +105,7 @@ void MainWord2Vec(const paddle::PaddlePlace& place) { cpu_feeds.push_back(&fourth_word); framework::FetchType output1; - std::vector cpu_fetchs1; + std::vector<::paddle::framework::FetchType*> cpu_fetchs1; cpu_fetchs1.push_back(&output1); TestInference(config.model_dir, cpu_feeds, cpu_fetchs1); @@ -118,12 +118,12 @@ void MainWord2Vec(const paddle::PaddlePlace& place) { } } -void MainImageClassification(const paddle::PaddlePlace& place) { +void MainImageClassification(const ::paddle::PaddlePlace& place) { int batch_size = 2; bool repeat = false; NativeConfig config = GetConfig(); - config.use_gpu = paddle::gpu_place_used(place); - config.use_xpu = paddle::xpu_place_used(place); + config.use_gpu = ::paddle::gpu_place_used(place); + config.use_xpu = ::paddle::xpu_place_used(place); config.model_dir = FLAGS_book_dirname + "/image_classification_resnet.inference.model"; @@ -163,10 +163,10 @@ void MainImageClassification(const paddle::PaddlePlace& place) { } } -void MainThreadsWord2Vec(const paddle::PaddlePlace& place) { +void MainThreadsWord2Vec(const ::paddle::PaddlePlace& place) { NativeConfig config = GetConfig(); - config.use_gpu = paddle::gpu_place_used(place); - config.use_xpu = paddle::xpu_place_used(place); + config.use_gpu = ::paddle::gpu_place_used(place); + config.use_xpu = ::paddle::xpu_place_used(place); auto main_predictor = CreatePaddlePredictor(config); // prepare inputs data and reference results @@ -186,7 +186,7 @@ void MainThreadsWord2Vec(const paddle::PaddlePlace& place) { // get reference result of each job std::vector ref_feeds; - std::vector ref_fetches(1, &refs[i]); + std::vector<::paddle::framework::FetchType*> ref_fetches(1, &refs[i]); for (auto& word : jobs[i]) { ref_feeds.push_back(&word); } @@ -225,12 +225,12 @@ void MainThreadsWord2Vec(const paddle::PaddlePlace& place) { } } -void MainThreadsImageClassification(const paddle::PaddlePlace& place) { +void MainThreadsImageClassification(const ::paddle::PaddlePlace& place) { constexpr int num_jobs = 4; // each job run 1 batch constexpr int batch_size = 1; NativeConfig config = GetConfig(); - config.use_gpu = paddle::gpu_place_used(place); - config.use_xpu = paddle::xpu_place_used(place); + config.use_gpu = ::paddle::gpu_place_used(place); + config.use_xpu = ::paddle::xpu_place_used(place); config.model_dir = FLAGS_book_dirname + "/image_classification_resnet.inference.model"; @@ -280,53 +280,53 @@ void MainThreadsImageClassification(const paddle::PaddlePlace& place) { } TEST(inference_api_native, word2vec_cpu) { - MainWord2Vec(paddle::PaddlePlace::kCPU); + MainWord2Vec(::paddle::PaddlePlace::kCPU); } TEST(inference_api_native, word2vec_cpu_threads) { - MainThreadsWord2Vec(paddle::PaddlePlace::kCPU); + MainThreadsWord2Vec(::paddle::PaddlePlace::kCPU); } TEST(inference_api_native, image_classification_cpu) { - MainImageClassification(paddle::PaddlePlace::kCPU); + MainImageClassification(::paddle::PaddlePlace::kCPU); } TEST(inference_api_native, image_classification_cpu_threads) { - MainThreadsImageClassification(paddle::PaddlePlace::kCPU); + MainThreadsImageClassification(::paddle::PaddlePlace::kCPU); } #ifdef PADDLE_WITH_XPU TEST(inference_api_native, word2vec_xpu) { - MainWord2Vec(paddle::PaddlePlace::kXPU); + MainWord2Vec(::paddle::PaddlePlace::kXPU); } TEST(inference_api_native, image_classification_xpu) { - MainImageClassification(paddle::PaddlePlace::kXPU); + MainImageClassification(::paddle::PaddlePlace::kXPU); } #endif #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) TEST(inference_api_native, word2vec_gpu) { - MainWord2Vec(paddle::PaddlePlace::kGPU); + MainWord2Vec(::paddle::PaddlePlace::kGPU); } // Turn off temporarily for the unstable result. // TEST(inference_api_native, word2vec_gpu_threads) { -// MainThreadsWord2Vec(paddle::PaddlePlace::kGPU); +// MainThreadsWord2Vec(::paddle::PaddlePlace::kGPU); // } TEST(inference_api_native, image_classification_gpu) { - MainImageClassification(paddle::PaddlePlace::kGPU); + MainImageClassification(::paddle::PaddlePlace::kGPU); } // Turn off temporarily for the unstable result. // TEST(inference_api_native, image_classification_gpu_threads) { -// MainThreadsImageClassification(paddle::PaddlePlace::kGPU); +// MainThreadsImageClassification(::paddle::PaddlePlace::kGPU); // } #endif #ifdef PADDLE_WITH_DNNL TEST(inference_api_native, image_classification_cpu_onednn) { FLAGS_use_mkldnn = true; - MainImageClassification(paddle::PaddlePlace::kCPU); + MainImageClassification(::paddle::PaddlePlace::kCPU); } TEST(inference_api_native, word2vec_cpu_onednn) { FLAGS_use_mkldnn = true; - MainWord2Vec(paddle::PaddlePlace::kCPU); + MainWord2Vec(::paddle::PaddlePlace::kCPU); } #endif diff --git a/test/cpp/inference/api/ipu_multi_model_profile.cc b/test/cpp/inference/api/ipu_multi_model_profile.cc index 3c5b1af1594e6..d5b8ce2532244 100644 --- a/test/cpp/inference/api/ipu_multi_model_profile.cc +++ b/test/cpp/inference/api/ipu_multi_model_profile.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/ipu_resnet50_fp16_test.cc b/test/cpp/inference/api/ipu_resnet50_fp16_test.cc index 99f0d58926dbd..1e3ddb51cb823 100644 --- a/test/cpp/inference/api/ipu_resnet50_fp16_test.cc +++ b/test/cpp/inference/api/ipu_resnet50_fp16_test.cc @@ -17,7 +17,7 @@ limitations under the License. */ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/ipu_resnet50_test.cc b/test/cpp/inference/api/ipu_resnet50_test.cc index 5a414bf9415bc..d45cd9bf49ae2 100644 --- a/test/cpp/inference/api/ipu_resnet50_test.cc +++ b/test/cpp/inference/api/ipu_resnet50_test.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/ipu_word2vec_sample.cc b/test/cpp/inference/api/ipu_word2vec_sample.cc index ba8f28ee5e19f..e43d03c510859 100644 --- a/test/cpp/inference/api/ipu_word2vec_sample.cc +++ b/test/cpp/inference/api/ipu_word2vec_sample.cc @@ -24,11 +24,11 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/fluid/inference/api/paddle_inference_api.h" +#include "paddle/utils/flags.h" -DEFINE_string(infer_model, "", "Directory of the inference model."); +PD_DEFINE_string(infer_model, "", "Directory of the inference model."); using paddle_infer::Config; using paddle_infer::CreatePredictor; @@ -70,7 +70,7 @@ void inference(std::string model_path, } int main(int argc, char *argv[]) { - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + ::paddle::flags::ParseCommandLineFlags(&argc, &argv); std::vector ipu_result; std::vector cpu_result; inference(FLAGS_infer_model, true, &ipu_result); diff --git a/test/cpp/inference/api/lite_mul_model_test.cc b/test/cpp/inference/api/lite_mul_model_test.cc index e600a3bab916c..3fa8e545a57da 100644 --- a/test/cpp/inference/api/lite_mul_model_test.cc +++ b/test/cpp/inference/api/lite_mul_model_test.cc @@ -19,7 +19,7 @@ limitations under the License. */ #include // NOLINT #include // NOLINT -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/lite_resnet50_test.cc b/test/cpp/inference/api/lite_resnet50_test.cc index e35e28388234d..dce9a8932fe32 100644 --- a/test/cpp/inference/api/lite_resnet50_test.cc +++ b/test/cpp/inference/api/lite_resnet50_test.cc @@ -17,7 +17,7 @@ limitations under the License. */ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/mkldnn_quantizer_tester.cc b/test/cpp/inference/api/mkldnn_quantizer_tester.cc index 5e699a8b4c6a5..8edad9fe27127 100644 --- a/test/cpp/inference/api/mkldnn_quantizer_tester.cc +++ b/test/cpp/inference/api/mkldnn_quantizer_tester.cc @@ -19,7 +19,7 @@ #include "paddle/fluid/inference/api/mkldnn_quantizer.h" #include "paddle/fluid/inference/api/paddle_inference_api.h" -DEFINE_string(dirname, "", "dirname to tests."); +PD_DEFINE_string(dirname, "", "dirname to tests."); namespace paddle { diff --git a/test/cpp/inference/api/paddle_infer_api_copy_tensor_tester.cc b/test/cpp/inference/api/paddle_infer_api_copy_tensor_tester.cc index 4674b77091a51..56b1b5b5d7fe0 100644 --- a/test/cpp/inference/api/paddle_infer_api_copy_tensor_tester.cc +++ b/test/cpp/inference/api/paddle_infer_api_copy_tensor_tester.cc @@ -19,10 +19,10 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/fluid/inference/api/paddle_infer_contrib.h" #include "paddle/fluid/platform/float16.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle_infer { diff --git a/test/cpp/inference/api/paddle_infer_api_errors_tester.cc b/test/cpp/inference/api/paddle_infer_api_errors_tester.cc index c716115ce2a4c..4fc1f6f0843c0 100644 --- a/test/cpp/inference/api/paddle_infer_api_errors_tester.cc +++ b/test/cpp/inference/api/paddle_infer_api_errors_tester.cc @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/fluid/inference/api/paddle_infer_contrib.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/utils/flags.h" namespace paddle_infer { namespace contrib { diff --git a/test/cpp/inference/api/paddle_infer_api_test.cc b/test/cpp/inference/api/paddle_infer_api_test.cc index c59ac40e5e5ec..e53473a520d80 100644 --- a/test/cpp/inference/api/paddle_infer_api_test.cc +++ b/test/cpp/inference/api/paddle_infer_api_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle_infer { diff --git a/test/cpp/inference/api/tester_helper.h b/test/cpp/inference/api/tester_helper.h index fe016abdaee02..a204c31cfd306 100644 --- a/test/cpp/inference/api/tester_helper.h +++ b/test/cpp/inference/api/tester_helper.h @@ -40,83 +40,87 @@ #include "test/cpp/inference/api/config_printer.h" #include "test/cpp/inference/test_helper.h" -DEFINE_string(model_name, "", "model name"); -DEFINE_string(infer_model, "", "model path"); -DEFINE_string(fp32_model, "", "FP32 model path"); -DEFINE_string(int8_model, "", "INT8 model path"); -DEFINE_string(infer_data, "", "data file"); -DEFINE_string(refer_result, "", "reference result for comparison"); -DEFINE_int32(batch_size, 1, "batch size"); -DEFINE_bool(ernie_large, false, "Test ernie large"); -DEFINE_bool(with_accuracy_layer, - true, - "Calculate the accuracy while label is in the input"); -DEFINE_bool(enable_fp32, true, "Enable FP32 type prediction"); -DEFINE_bool(enable_bf16, false, "Enable BF16 type prediction"); -DEFINE_bool(enable_int8_ptq, - false, - "Enable INT8 post-training quantization prediction"); -DEFINE_bool(enable_int8_qat, - false, - "Enable INT8 quant-aware training prediction"); -DEFINE_int32(warmup_batch_size, 100, "batch size for quantization warmup"); +PD_DEFINE_string(model_name, "", "model name"); +PD_DEFINE_string(infer_model, "", "model path"); +PD_DEFINE_string(fp32_model, "", "FP32 model path"); +PD_DEFINE_string(int8_model, "", "INT8 model path"); +PD_DEFINE_string(infer_data, "", "data file"); +PD_DEFINE_string(refer_result, "", "reference result for comparison"); +PD_DEFINE_int32(batch_size, 1, "batch size"); +PD_DEFINE_bool(ernie_large, false, "Test ernie large"); +PD_DEFINE_bool(with_accuracy_layer, + true, + "Calculate the accuracy while label is in the input"); +PD_DEFINE_bool(enable_fp32, true, "Enable FP32 type prediction"); +PD_DEFINE_bool(enable_bf16, false, "Enable BF16 type prediction"); +PD_DEFINE_bool(enable_int8_ptq, + false, + "Enable INT8 post-training quantization prediction"); +PD_DEFINE_bool(enable_int8_qat, + false, + "Enable INT8 quant-aware training prediction"); +PD_DEFINE_int32(warmup_batch_size, 100, "batch size for quantization warmup"); // setting iterations to 0 means processing the whole dataset -DEFINE_int32(iterations, 0, "number of batches to process"); -DEFINE_int32(repeat, 1, "Running the inference program repeat times."); -DEFINE_bool(test_all_data, false, "Test the all dataset in data file."); -DEFINE_int32(num_threads, 1, "Running the inference program in multi-threads."); -DEFINE_bool(use_analysis, - true, - "Running the inference program in analysis mode."); -DEFINE_bool(record_benchmark, - false, - "Record benchmark after profiling the model"); -DEFINE_double(accuracy, 1e-3, "Result Accuracy."); -DEFINE_double(quantized_accuracy, 1e-2, "Result Quantized Accuracy."); -DEFINE_bool(zero_copy, false, "Use ZeroCopy to speedup Feed/Fetch."); -DEFINE_bool(warmup, - false, - "Use warmup to calculate elapsed_time more accurately. " - "To reduce CI time, it sets false in default."); -DEFINE_int32(warmup_iters, 1, "Number of batches to process during warmup."); - -DEFINE_bool(enable_profile, false, "Turn on profiler for fluid"); -DEFINE_int32(cpu_num_threads, 1, "Number of threads for each paddle instance."); -DEFINE_bool(fuse_multi_gru, - false, - "Running the inference program with multi_gru_fuse_pass"); +PD_DEFINE_int32(iterations, 0, "number of batches to process"); +PD_DEFINE_int32(repeat, 1, "Running the inference program repeat times."); +PD_DEFINE_bool(test_all_data, false, "Test the all dataset in data file."); +PD_DEFINE_int32(num_threads, + 1, + "Running the inference program in multi-threads."); +PD_DEFINE_bool(use_analysis, + true, + "Running the inference program in analysis mode."); +PD_DEFINE_bool(record_benchmark, + false, + "Record benchmark after profiling the model"); +PD_DEFINE_double(accuracy, 1e-3, "Result Accuracy."); +PD_DEFINE_double(quantized_accuracy, 1e-2, "Result Quantized Accuracy."); +PD_DEFINE_bool(zero_copy, false, "Use ZeroCopy to speedup Feed/Fetch."); +PD_DEFINE_bool(warmup, + false, + "Use warmup to calculate elapsed_time more accurately. " + "To reduce CI time, it sets false in default."); +PD_DEFINE_int32(warmup_iters, 1, "Number of batches to process during warmup."); + +PD_DEFINE_bool(enable_profile, false, "Turn on profiler for fluid"); +PD_DEFINE_int32(cpu_num_threads, + 1, + "Number of threads for each paddle instance."); +PD_DEFINE_bool(fuse_multi_gru, + false, + "Running the inference program with multi_gru_fuse_pass"); // ipu related -DEFINE_int32(ipu_micro_batch_size, 1, "micro batch size"); -DEFINE_int32(ipu_device_num, 1, "device num"); -DEFINE_bool(ipu_enable_pipelining, false, "enable pipelining"); -DEFINE_int32(ipu_batches_per_step, - 1, - "the number of batches per run in pipelining"); -DEFINE_bool(ipu_enable_fp16, false, "enable fp16"); -DEFINE_int32(ipu_replica_num, 1, "replica num"); -DEFINE_double(ipu_available_memory_proportion, - 1.0, - "available memory proportion"); -DEFINE_bool(ipu_enable_half_partial, false, "enable half partial"); +PD_DEFINE_int32(ipu_micro_batch_size, 1, "micro batch size"); +PD_DEFINE_int32(ipu_device_num, 1, "device num"); +PD_DEFINE_bool(ipu_enable_pipelining, false, "enable pipelining"); +PD_DEFINE_int32(ipu_batches_per_step, + 1, + "the number of batches per run in pipelining"); +PD_DEFINE_bool(ipu_enable_fp16, false, "enable fp16"); +PD_DEFINE_int32(ipu_replica_num, 1, "replica num"); +PD_DEFINE_double(ipu_available_memory_proportion, + 1.0, + "available memory proportion"); +PD_DEFINE_bool(ipu_enable_half_partial, false, "enable half partial"); namespace paddle { namespace inference { -using paddle::framework::proto::VarType; -using float16 = paddle::platform::float16; +using ::paddle::framework::proto::VarType; +using float16 = ::paddle::platform::float16; template -constexpr paddle::PaddleDType GetPaddleDType(); +constexpr ::paddle::PaddleDType GetPaddleDType(); template <> -constexpr paddle::PaddleDType GetPaddleDType() { - return paddle::PaddleDType::INT64; +constexpr ::paddle::PaddleDType GetPaddleDType() { + return ::paddle::PaddleDType::INT64; } template <> -constexpr paddle::PaddleDType GetPaddleDType() { - return paddle::PaddleDType::FLOAT32; +constexpr ::paddle::PaddleDType GetPaddleDType() { + return ::paddle::PaddleDType::FLOAT32; } void PrintConfig(const PaddlePredictor::Config *config, bool use_analysis) { @@ -521,7 +525,7 @@ void PredictionWarmUp(PaddlePredictor *predictor, PrintTime( batch_size, 1, num_threads, tid, batch_latency, iterations, data_type); if (FLAGS_enable_profile) { - paddle::platform::ResetProfiler(); + ::paddle::platform::ResetProfiler(); } } @@ -749,7 +753,7 @@ float CompareAccuracyOne( if (output_slots[i][compared_idx].lod.size() > 0) throw std::invalid_argument("CompareAccuracy: output has nonempty LoD."); - if (output_slots[i][compared_idx].dtype != paddle::PaddleDType::FLOAT32) + if (output_slots[i][compared_idx].dtype != ::paddle::PaddleDType::FLOAT32) throw std::invalid_argument( "CompareAccuracy: output is of a wrong type."); @@ -1156,7 +1160,7 @@ static bool CompareTensor(const phi::DenseTensor &a, return true; } -void ConvertFP32toFP16(paddle::PaddleTensor &tensor // NOLINT +void ConvertFP32toFP16(::paddle::PaddleTensor &tensor // NOLINT ) { int num = 1; for (auto dim : tensor.shape) { @@ -1177,7 +1181,7 @@ void ConvertFP32toFP16(paddle::PaddleTensor &tensor // NOLINT tensor.dtype = PaddleDType::FLOAT16; } -void ConvertFP16toFP32(paddle::PaddleTensor &tensor // NOLINT +void ConvertFP16toFP32(::paddle::PaddleTensor &tensor // NOLINT ) { int num = 1; for (auto dim : tensor.shape) { diff --git a/test/cpp/inference/api/trt_cascade_rcnn_test.cc b/test/cpp/inference/api/trt_cascade_rcnn_test.cc index 86759c33e472e..710e6481d018c 100644 --- a/test/cpp/inference/api/trt_cascade_rcnn_test.cc +++ b/test/cpp/inference/api/trt_cascade_rcnn_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_dynamic_shape_ernie_fp16_serialize_deserialize_test.cc b/test/cpp/inference/api/trt_dynamic_shape_ernie_fp16_serialize_deserialize_test.cc index 806950ca8d6fd..34ddb8fa3c330 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_ernie_fp16_serialize_deserialize_test.cc +++ b/test/cpp/inference/api/trt_dynamic_shape_ernie_fp16_serialize_deserialize_test.cc @@ -23,7 +23,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.cc b/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.cc index bcf82d66f781e..7946b7c2428b2 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.cc +++ b/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.cc @@ -23,7 +23,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h b/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h index 09b20d23e9708..e046181dbf094 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h +++ b/test/cpp/inference/api/trt_dynamic_shape_ernie_serialize_deserialize_test.h @@ -25,7 +25,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_dynamic_shape_ernie_test.cc b/test/cpp/inference/api/trt_dynamic_shape_ernie_test.cc index 8abf7224a135e..fd31613c2b628 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_ernie_test.cc +++ b/test/cpp/inference/api/trt_dynamic_shape_ernie_test.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "paddle/fluid/inference/tensorrt/helper.h" #include "test/cpp/inference/api/trt_test_helper.h" diff --git a/test/cpp/inference/api/trt_dynamic_shape_test.cc b/test/cpp/inference/api/trt_dynamic_shape_test.cc index 505c0cdf083c0..8f284d75b7e3c 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_test.cc +++ b/test/cpp/inference/api/trt_dynamic_shape_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_dynamic_shape_transformer_prune_test.cc b/test/cpp/inference/api/trt_dynamic_shape_transformer_prune_test.cc index 2e71da39b5f45..ff8c60df00559 100644 --- a/test/cpp/inference/api/trt_dynamic_shape_transformer_prune_test.cc +++ b/test/cpp/inference/api/trt_dynamic_shape_transformer_prune_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_fc_prelu_test.cc b/test/cpp/inference/api/trt_fc_prelu_test.cc index a1ef33407596a..5f10c12bf3dd1 100644 --- a/test/cpp/inference/api/trt_fc_prelu_test.cc +++ b/test/cpp/inference/api/trt_fc_prelu_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_instance_norm_converter_test.cc b/test/cpp/inference/api/trt_instance_norm_converter_test.cc index b58ddb2d919d8..fc78219a9db6d 100644 --- a/test/cpp/inference/api/trt_instance_norm_converter_test.cc +++ b/test/cpp/inference/api/trt_instance_norm_converter_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc b/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc index 9c6a87a6d161a..d34d640cfaf40 100644 --- a/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc +++ b/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc @@ -12,7 +12,6 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_mobilenet_test.cc b/test/cpp/inference/api/trt_mobilenet_test.cc index 7cae99e0d3479..670eaa7b1169e 100644 --- a/test/cpp/inference/api/trt_mobilenet_test.cc +++ b/test/cpp/inference/api/trt_mobilenet_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_quant_int8_test.cc b/test/cpp/inference/api/trt_quant_int8_test.cc index f40b2197fb229..46c1fb7c9f742 100644 --- a/test/cpp/inference/api/trt_quant_int8_test.cc +++ b/test/cpp/inference/api/trt_quant_int8_test.cc @@ -17,7 +17,7 @@ limitations under the License. */ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_quant_int8_yolov3_r50_test.cc b/test/cpp/inference/api/trt_quant_int8_yolov3_r50_test.cc index ce058a1275c92..412aeae6ed75a 100644 --- a/test/cpp/inference/api/trt_quant_int8_yolov3_r50_test.cc +++ b/test/cpp/inference/api/trt_quant_int8_yolov3_r50_test.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_rebind_stream_test.cc b/test/cpp/inference/api/trt_rebind_stream_test.cc index 3a42af93427c5..8c8e78a51907e 100644 --- a/test/cpp/inference/api/trt_rebind_stream_test.cc +++ b/test/cpp/inference/api/trt_rebind_stream_test.cc @@ -16,8 +16,8 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "paddle/fluid/inference/api/paddle_inference_api.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_resnet50_test.cc b/test/cpp/inference/api/trt_resnet50_test.cc index 8dde6a0f5dd8c..085b64ef882b5 100644 --- a/test/cpp/inference/api/trt_resnet50_test.cc +++ b/test/cpp/inference/api/trt_resnet50_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_resnext_test.cc b/test/cpp/inference/api/trt_resnext_test.cc index a80058468d576..65e09d3532d86 100644 --- a/test/cpp/inference/api/trt_resnext_test.cc +++ b/test/cpp/inference/api/trt_resnext_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_split_converter_test.cc b/test/cpp/inference/api/trt_split_converter_test.cc index ca41ac5681e4e..8d87b98f6e34b 100644 --- a/test/cpp/inference/api/trt_split_converter_test.cc +++ b/test/cpp/inference/api/trt_split_converter_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/trt_test_helper.h" namespace paddle { diff --git a/test/cpp/inference/api/trt_test_helper.h b/test/cpp/inference/api/trt_test_helper.h index db446e64054c0..dccbb589bdb0b 100644 --- a/test/cpp/inference/api/trt_test_helper.h +++ b/test/cpp/inference/api/trt_test_helper.h @@ -17,17 +17,17 @@ limitations under the License. */ #include #include -#include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle { namespace inference { -DEFINE_bool(use_tensorrt, true, "Test the performance of TensorRT engine."); -DEFINE_string(prog_filename, "", "Name of model file."); -DEFINE_string(param_filename, "", "Name of parameters file."); +PD_DEFINE_bool(use_tensorrt, true, "Test the performance of TensorRT engine."); +PD_DEFINE_string(prog_filename, "", "Name of model file."); +PD_DEFINE_string(param_filename, "", "Name of parameters file."); template void SetConfig(ConfigType* config, diff --git a/test/cpp/inference/api/xpu_config_resnet50_test.cc b/test/cpp/inference/api/xpu_config_resnet50_test.cc index ce3796e420970..d118eef9e88fd 100644 --- a/test/cpp/inference/api/xpu_config_resnet50_test.cc +++ b/test/cpp/inference/api/xpu_config_resnet50_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" namespace paddle_infer { diff --git a/test/cpp/inference/api/xpu_runtime_config_resnet50_test.cc b/test/cpp/inference/api/xpu_runtime_config_resnet50_test.cc index b9ab6ea68d7b5..94de193c89513 100644 --- a/test/cpp/inference/api/xpu_runtime_config_resnet50_test.cc +++ b/test/cpp/inference/api/xpu_runtime_config_resnet50_test.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include #include -#include "gflags/gflags.h" +#include "paddle/utils/flags.h" #include "test/cpp/inference/api/tester_helper.h" #include "xpu/runtime.h" #include "xpu/xdnn.h" diff --git a/test/cpp/inference/infer_ut/test_LeViT.cc b/test/cpp/inference/infer_ut/test_LeViT.cc index 056371b0ae662..ed30f04e7301b 100644 --- a/test/cpp/inference/infer_ut/test_LeViT.cc +++ b/test/cpp/inference/infer_ut/test_LeViT.cc @@ -248,6 +248,6 @@ TEST(tensorrt_tester_LeViT, multi_stream_thread4_trt_fp32_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_det_mv3_db.cc b/test/cpp/inference/infer_ut/test_det_mv3_db.cc index a407251ccba66..474d0701225f5 100644 --- a/test/cpp/inference/infer_ut/test_det_mv3_db.cc +++ b/test/cpp/inference/infer_ut/test_det_mv3_db.cc @@ -183,6 +183,6 @@ TEST(mkldnn_tester_det_mv3_db, multi_thread2_mkl_fp32_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_ernie_text_cls.cc b/test/cpp/inference/infer_ut/test_ernie_text_cls.cc index 2ffeb604230f7..ddf2dbc49d897 100644 --- a/test/cpp/inference/infer_ut/test_ernie_text_cls.cc +++ b/test/cpp/inference/infer_ut/test_ernie_text_cls.cc @@ -134,6 +134,6 @@ TEST(mkldnn_tester_ernie_text_cls, multi_thread4_mkl_fp32_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_ernie_xnli_int8.cc b/test/cpp/inference/infer_ut/test_ernie_xnli_int8.cc index e3030d08021ce..4e9c96c530a10 100644 --- a/test/cpp/inference/infer_ut/test_ernie_xnli_int8.cc +++ b/test/cpp/inference/infer_ut/test_ernie_xnli_int8.cc @@ -192,7 +192,7 @@ TEST(tensorrt_tester_ernie_xnli, oss_varlen_truth_data_int8) { int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); #if IS_TRT_VERSION_GE(7200) return RUN_ALL_TESTS(); diff --git a/test/cpp/inference/infer_ut/test_mobilnetv1.cc b/test/cpp/inference/infer_ut/test_mobilnetv1.cc index 582c34e1b0b47..2660cc5cbd5d6 100644 --- a/test/cpp/inference/infer_ut/test_mobilnetv1.cc +++ b/test/cpp/inference/infer_ut/test_mobilnetv1.cc @@ -81,6 +81,6 @@ TEST(tensorrt_tester_mobilenetv1, tuned_dynamic_trt_fp32_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_ppyolo_mbv3.cc b/test/cpp/inference/infer_ut/test_ppyolo_mbv3.cc index a075192a58054..407e7c87dc97c 100644 --- a/test/cpp/inference/infer_ut/test_ppyolo_mbv3.cc +++ b/test/cpp/inference/infer_ut/test_ppyolo_mbv3.cc @@ -155,6 +155,6 @@ TEST(DISABLED_mkldnn_tester_ppyolo_mbv3, multi_thread4_mkl_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_ppyolov2_r50vd.cc b/test/cpp/inference/infer_ut/test_ppyolov2_r50vd.cc index ad91cb5bc9e85..c90256fb8b8c1 100644 --- a/test/cpp/inference/infer_ut/test_ppyolov2_r50vd.cc +++ b/test/cpp/inference/infer_ut/test_ppyolov2_r50vd.cc @@ -154,6 +154,6 @@ TEST(mkldnn_tester_ppyolov2_r50vd, multi_thread2_mkl_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_resnet50.cc b/test/cpp/inference/infer_ut/test_resnet50.cc index 1c9b70564929f..50b0b71c2e1da 100644 --- a/test/cpp/inference/infer_ut/test_resnet50.cc +++ b/test/cpp/inference/infer_ut/test_resnet50.cc @@ -242,6 +242,6 @@ TEST(DISABLED_tensorrt_tester_resnet50, profile_multi_thread_trt_fp32) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_resnet50_quant.cc b/test/cpp/inference/infer_ut/test_resnet50_quant.cc index 452cf31b31139..c2c44102f56b6 100644 --- a/test/cpp/inference/infer_ut/test_resnet50_quant.cc +++ b/test/cpp/inference/infer_ut/test_resnet50_quant.cc @@ -171,6 +171,6 @@ TEST(DISABLED_tensorrt_tester_resnet50_quant, multi_thread_multi_instance) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/inference/infer_ut/test_yolov3.cc b/test/cpp/inference/infer_ut/test_yolov3.cc index 4dc35af495935..4a3e65ba5a574 100644 --- a/test/cpp/inference/infer_ut/test_yolov3.cc +++ b/test/cpp/inference/infer_ut/test_yolov3.cc @@ -154,6 +154,6 @@ TEST(test_yolov3, multi_thread4_mkl_bz2) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - ::GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); + gflags::ParseCommandLineFlags(&argc, &argv, true); return RUN_ALL_TESTS(); } diff --git a/test/cpp/phi/api/scale_api.h b/test/cpp/phi/api/scale_api.h index 571ab0defbce7..104034d5effe9 100644 --- a/test/cpp/phi/api/scale_api.h +++ b/test/cpp/phi/api/scale_api.h @@ -14,7 +14,6 @@ #pragma once -#include "gflags/gflags.h" #include "glog/logging.h" #include "paddle/phi/api/include/tensor.h" #include "paddle/phi/api/lib/kernel_dispatch.h" @@ -26,6 +25,7 @@ #include "paddle/phi/core/meta_tensor.h" #include "paddle/phi/infermeta/unary.h" #include "paddle/phi/kernels/scale_kernel.h" +#include "paddle/utils/flags.h" PHI_DECLARE_int32(low_precision_op_list); namespace paddle { diff --git a/test/cpp/prim/test_static_prim.cc b/test/cpp/prim/test_static_prim.cc index e26f54a44be43..d4f5dcb8998ae 100644 --- a/test/cpp/prim/test_static_prim.cc +++ b/test/cpp/prim/test_static_prim.cc @@ -28,7 +28,7 @@ #include "paddle/phi/core/flags.h" #include "paddle/phi/core/kernel_registry.h" -DECLARE_bool(prim_enabled); +PD_DECLARE_bool(prim_enabled); PHI_DECLARE_string(tensor_operants_mode); PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT); From 987cb97e36e8f72c48c13d1913506313b8bc338b Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 30 Aug 2023 17:38:54 +0800 Subject: [PATCH 029/194] [NewIR] fix logical op infermeta (#56711) * fix logical op infermeta * add test * adpat inplace api --- paddle/ir/core/ir_printer.cc | 5 +++++ paddle/ir/core/program.h | 2 ++ paddle/phi/api/yaml/ops.yaml | 8 ++++---- paddle/phi/infermeta/binary.cc | 9 +++++++++ paddle/phi/infermeta/binary.h | 4 ++++ paddle/phi/infermeta/unary.cc | 7 +++++++ paddle/phi/infermeta/unary.h | 2 ++ test/cpp/ir/core/ir_program_test.cc | 10 +++++++++- 8 files changed, 42 insertions(+), 5 deletions(-) diff --git a/paddle/ir/core/ir_printer.cc b/paddle/ir/core/ir_printer.cc index 080e0bafc966a..25f23b31e2854 100644 --- a/paddle/ir/core/ir_printer.cc +++ b/paddle/ir/core/ir_printer.cc @@ -324,4 +324,9 @@ std::ostream& operator<<(std::ostream& os, Attribute attr) { return os; } +std::ostream& operator<<(std::ostream& os, const Program& prog) { + prog.Print(os); + return os; +} + } // namespace ir diff --git a/paddle/ir/core/program.h b/paddle/ir/core/program.h index 0e2ecb58d9181..6f44a3fe4699c 100644 --- a/paddle/ir/core/program.h +++ b/paddle/ir/core/program.h @@ -71,4 +71,6 @@ class IR_API Program { ParameterMap parameters_; }; +std::ostream& operator<<(std::ostream& os, const Program& prog); + } // namespace ir diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index eca5b93e24f88..fbc058ff64e78 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1515,7 +1515,7 @@ args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : LogicalBinaryInferMeta kernel : func : logical_and data_type : x @@ -1526,7 +1526,7 @@ args : (Tensor x) output : Tensor(out) infer_meta : - func : UnchangedInferMeta + func : LogicalNotInfermeta kernel : func : logical_not data_type : x @@ -1537,7 +1537,7 @@ args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : LogicalBinaryInferMeta kernel : func : logical_or data_type : x @@ -1548,7 +1548,7 @@ args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : LogicalBinaryInferMeta kernel : func : logical_xor data_type : x diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index 0118db6041203..a9b14d2df3d17 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -1942,6 +1942,15 @@ void LogLossInferMeta(const MetaTensor& input, out->share_lod(input); } +void LogicalBinaryInferMeta(const MetaTensor& x, + const MetaTensor& y, + MetaTensor* out) { + ElementwiseInferMeta(x, y, out); + if (!(out->is_same_tensor(x))) { + out->set_dtype(DataType::BOOL); + } +} + void LUUnpackInferMeta(const MetaTensor& x, const MetaTensor& pivots, bool unpack_ludata, diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 8aa4114e74046..9060d2abc6564 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -300,6 +300,10 @@ void IndexAddInferMeta(const MetaTensor& x, void KronInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out); +void LogicalBinaryInferMeta(const MetaTensor& x, + const MetaTensor& y, + MetaTensor* out); + void LogLossInferMeta(const MetaTensor& input, const MetaTensor& label, float epsilon, diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 4c952bb3cd2bc..28b80b58155fa 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2083,6 +2083,13 @@ void KthvalueInferMeta(const MetaTensor& x, indices->set_dtype(x.dtype()); } +void LogicalNotInfermeta(const MetaTensor& x, MetaTensor* out) { + UnchangedInferMeta(x, out); + if (!(out->is_same_tensor(x))) { + out->set_dtype(DataType::BOOL); + } +} + void LogsumexpInferMeta(const MetaTensor& input, const std::vector& axis, bool keepdim, diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 136b8c240e5f3..2bf90048d30d3 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -298,6 +298,8 @@ void KthvalueInferMeta(const MetaTensor& x, MetaTensor* indices, MetaConfig = MetaConfig()); +void LogicalNotInfermeta(const MetaTensor& x, MetaTensor* out); + void LogsumexpInferMeta(const MetaTensor& input, const std::vector& axis, bool keepdim, diff --git a/test/cpp/ir/core/ir_program_test.cc b/test/cpp/ir/core/ir_program_test.cc index dcc81e9c51782..c7729ae89fde8 100644 --- a/test/cpp/ir/core/ir_program_test.cc +++ b/test/cpp/ir/core/ir_program_test.cc @@ -14,6 +14,8 @@ #include +#include + #include "paddle/fluid/ir/dialect/paddle_dialect/interface/op_yaml_info.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" @@ -193,7 +195,13 @@ TEST(program_test, program) { EXPECT_EQ(program.block()->size() == 5, true); EXPECT_EQ(program.parameters_num() == 3, true); - program.Print(std::cout); + std::stringstream ss; + program.Print(ss); + + std::stringstream ss_ostram; + ss_ostram << program; + + EXPECT_EQ(ss.str(), ss_ostram.str()); } TEST(program_test, slice_combine_test) { From dbc9e5a8d0197243d4a5a19c580ecdf84f41eb52 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Thu, 31 Aug 2023 09:42:06 +0800 Subject: [PATCH 030/194] Add approval for using cc_test_old (#56715) * add cc_test_old approve,test=document_fix * add cc_test_old approve,test=document_fix * add cc_test_old approve,test=document_fix --- tools/check_file_diff_approvals.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index a78a545a5a660..5567fadfe7849 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -270,6 +270,12 @@ if [ "${HAS_MODIFIED_DECLARATIONS}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then check_approval 1 chenwhql zyfncg fi +HAS_USED_CCTESTOLD=`git diff -U0 upstream/$BRANCH |grep "cc_test_old" || true` +if [ "${HAS_USED_CCTESTOLD}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then + echo_line="You must be approved by phlrain or risemeup1 or zhangbo9674 for using cc_test_old. Thanks!\n" + check_approval 1 phlrain risemeup1 zhangbo9674 +fi + HAS_MODIFIED_API_COMPAT_YAML=`git diff --name-only upstream/$BRANCH | grep "paddle/phi/api/yaml/op_compat.yaml" || true` if [ "${HAS_MODIFIED_API_COMPAT_YAML}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then echo_line="You must be approved by chenwhql or zyfncg or heavyrain-lzy for paddle/phi/api/yaml/op_compat.yaml changes, which manages the extra params of Op and name mapping between Yaml and OpMaker. In order to ensure compatibility of framework, this file isn't allowed to be modified at will!\n" From 54fcd9a97285e8e7e6600c58e198467041c9afb3 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 31 Aug 2023 09:55:40 +0800 Subject: [PATCH 031/194] [AutoParallel] Adapt static spmd rules for dynamic graph (#56367) * move matmul spmd rules into phi * add basic infer spmd utils * addspmd factory * fix compile error * add unittest * refine infer spmd test and utils * debug infer spmd test * adapt python test * poish details * change to vector attr arg * revert needless change * update matmul spmd rule test * remove original rule * polish details * fix marco error * add comment * pass backward test * fix compile error * add cmake rule for spmd_rules_test * add dist meta tensor * update pybind impl * add marco for rules --- .../auto_parallel/spmd_rules/rules.h | 4 - paddle/fluid/pybind/auto_parallel_py.cc | 51 +++++ .../distributed/auto_parallel/CMakeLists.txt | 2 + .../auto_parallel/dist_meta_tensor.cc | 51 +++++ .../auto_parallel/dist_meta_tensor.h | 68 +++++++ .../auto_parallel/inferspmd_utils.cc | 97 +++++++++ .../auto_parallel/inferspmd_utils.h | 186 ++++++++++++++++++ paddle/phi/core/distributed/type_defs.h | 29 +++ paddle/phi/core/meta_tensor.cc | 5 +- paddle/phi/core/meta_tensor.h | 5 +- paddle/phi/infermeta/CMakeLists.txt | 5 + .../phi/infermeta/spmd_rules/CMakeLists.txt | 6 + .../infermeta/spmd_rules/matmul.cc} | 150 +++++++------- paddle/phi/infermeta/spmd_rules/matmul.h | 37 ++++ paddle/phi/infermeta/spmd_rules/rules.h | 47 +++++ paddle/phi/infermeta/spmd_rules/utils.cc | 159 +++++++++++++++ paddle/phi/infermeta/spmd_rules/utils.h | 65 ++++++ .../spmd_rules/test_matmul_rule.py | 58 +++--- test/auto_parallel/test_dist_tensor.py | 15 ++ test/cpp/auto_parallel/CMakeLists.txt | 3 +- test/cpp/auto_parallel/spmd_rule_test.cc | 168 +++++++++------- 21 files changed, 1022 insertions(+), 189 deletions(-) create mode 100644 paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.cc create mode 100644 paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h create mode 100644 paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc create mode 100644 paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h create mode 100644 paddle/phi/core/distributed/type_defs.h create mode 100644 paddle/phi/infermeta/spmd_rules/CMakeLists.txt rename paddle/{fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.cc => phi/infermeta/spmd_rules/matmul.cc} (69%) create mode 100644 paddle/phi/infermeta/spmd_rules/matmul.h create mode 100644 paddle/phi/infermeta/spmd_rules/rules.h create mode 100644 paddle/phi/infermeta/spmd_rules/utils.cc create mode 100644 paddle/phi/infermeta/spmd_rules/utils.h diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/rules.h b/paddle/fluid/distributed/auto_parallel/spmd_rules/rules.h index cf4046950964a..ba468269b8230 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/rules.h +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/rules.h @@ -19,7 +19,6 @@ #include "paddle/fluid/distributed/auto_parallel/spmd_rules/elementwise_spmd_rule.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/embedding_spmd_rule.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/layer_norm_spmd_rule.h" -#include "paddle/fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/reduction_spmd_rule.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/replicated_spmd_rule.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/reshape_spmd_rule.h" @@ -32,9 +31,6 @@ namespace paddle { namespace distributed { namespace auto_parallel { -// matmul rule -REGISTER_SPMD_RULE(matmul, MatmulSPMDRule); - // reduction rules REGISTER_SPMD_RULE(all, ReductionSPMDRule); REGISTER_SPMD_RULE(amax, ReductionSPMDRule); diff --git a/paddle/fluid/pybind/auto_parallel_py.cc b/paddle/fluid/pybind/auto_parallel_py.cc index e03292faa9e42..6f639f145dcea 100644 --- a/paddle/fluid/pybind/auto_parallel_py.cc +++ b/paddle/fluid/pybind/auto_parallel_py.cc @@ -15,13 +15,16 @@ #include #include +#include "paddle/fluid/framework/block_desc.h" #include "paddle/fluid/framework/op_desc.h" #include "paddle/fluid/framework/var_desc.h" #include "paddle/fluid/pybind/auto_parallel_py.h" +#include "paddle/fluid/pybind/pybind_variant_caster.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/distributed/auto_parallel/device_mesh.h" #include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" #include "paddle/phi/core/distributed/auto_parallel/dist_mapper.h" +#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" #include "paddle/phi/core/distributed/auto_parallel/process_mesh.h" #include "paddle/utils/optional.h" #include "paddle/utils/pybind.h" @@ -32,6 +35,10 @@ #include "paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.h" #include "paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.h" +#ifdef PADDLE_WITH_DISTRIBUTE +#include "paddle/phi/infermeta/spmd_rules/rules.h" +#endif + namespace py = pybind11; namespace paddle { @@ -42,6 +49,7 @@ using paddle::distributed::auto_parallel::kDefault; using paddle::distributed::auto_parallel::OperatorDistAttr; using paddle::distributed::auto_parallel::SPMDRuleBase; using paddle::distributed::auto_parallel::SPMDRuleMap; +using paddle::framework::BlockDesc; using paddle::framework::OpDesc; using paddle::framework::VarDesc; using phi::distributed::ProcessMesh; @@ -343,6 +351,41 @@ void BindAutoParallel(py::module *m) { &SPMDRuleBase::InferBackward)); // .def("infer_backward", &SPMDRuleBase::InferBackward) [revert in future] + py::class_(*m, "SpmdRule") + .def("infer_forward", + [](const phi::distributed::SpmdRule &self, + const std::vector &input_specs, + const std::vector &attrs) { + phi::distributed::InferSpmdContext ctx; + for (auto &spec : input_specs) { + ctx.EmplaceBackInput(phi::distributed::DistMetaTensor( + phi::make_ddim(spec.shape()), spec.dist_attr())); + } + for (auto &attr : attrs) { + ctx.EmplaceBackAttr(attr); + } + return self.InferForward(ctx); + }) + .def("infer_backward", + [](const phi::distributed::SpmdRule &self, + const std::vector &input_specs, + const std::vector &output_specs, + const std::vector &attrs) { + phi::distributed::InferSpmdContext ctx; + for (auto &spec : input_specs) { + ctx.EmplaceBackInput(phi::distributed::DistMetaTensor( + phi::make_ddim(spec.shape()), spec.dist_attr())); + } + for (auto &spec : output_specs) { + ctx.EmplaceBackInput(phi::distributed::DistMetaTensor( + phi::make_ddim(spec.shape()), spec.dist_attr())); + } + for (auto &attr : attrs) { + ctx.EmplaceBackAttr(attr); + } + return self.InferBackward(ctx); + }); + py::class_(*m, "DistTensorSpec") .def(py::init<>()) .def(py::init()) @@ -472,6 +515,14 @@ void BindAutoParallel(py::module *m) { }, py::return_value_policy::reference); + m->def( + "get_phi_spmd_rule", + [](const std::string op_type) { + return phi::distributed::SpmdRuleFactory::Instance().GetSpmdRule( + op_type); + }, + py::return_value_policy::reference); + // TODO(liuzhenhai): DistributedMapper is not used for now, but // dist_mapper_test need the symbols forch DistributedMapper to be linked, // remove it latter diff --git a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt index 0aee1b5363838..63fbe9ecd677c 100644 --- a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt +++ b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt @@ -9,6 +9,8 @@ collect_srcs( dist_mapper.cc reshard_utils.cc dist_tensor.cc + dist_meta_tensor.cc + inferspmd_utils.cc reshard_function.cc reshard_split_functor.cc reshard_concat_functor.cc diff --git a/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.cc b/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.cc new file mode 100644 index 0000000000000..dc5d6c20e62b3 --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.cc @@ -0,0 +1,51 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" + +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" + +namespace phi { +namespace distributed { + +phi::DDim DistMetaTensor::dims() const { + // member values in tensor_ have higher priority than those in DistMetaTensor + if (tensor_ != nullptr) { + PADDLE_ENFORCE_EQ(this->is_dist(), + true, + phi::errors::InvalidArgument( + "The current MetaTensor doesn't contains " + "DistTensor when call `dist_attr` method.")); + return MetaTensor::dims(); + } else { + return dims_; + } +} + +const distributed::TensorDistAttr& DistMetaTensor::dist_attr() const { + // member values in tensor_ have higher priority than those in DistMetaTensor + if (tensor_ != nullptr) { + PADDLE_ENFORCE_EQ(this->is_dist(), + true, + phi::errors::InvalidArgument( + "The current MetaTensor doesn't contains " + "DistTensor when call `dist_attr` method.")); + return static_cast(tensor_)->dist_attr(); + } else { + return dist_attr_; + } +} + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h b/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h new file mode 100644 index 0000000000000..efbf38d28f9f0 --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h @@ -0,0 +1,68 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#pragma once + +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/meta_tensor.h" + +namespace phi { +namespace distributed { + +class DistMetaTensor : public MetaTensor { + public: + // supporting implicit construction is easier to use + DistMetaTensor(TensorBase* tensor) // NOLINT + : MetaTensor(tensor) {} + DistMetaTensor(const TensorBase& tensor) // NOLINT + : MetaTensor(tensor) {} + DistMetaTensor(const TensorBase* tensor) // NOLINT + : MetaTensor(tensor) {} + DistMetaTensor(TensorBase& tensor) // NOLINT + : MetaTensor(tensor) {} + // For static mode only + DistMetaTensor(const phi::DDim& dims, const TensorDistAttr& dist_attr) + : dims_(dims), dist_attr_(dist_attr) {} + + DistMetaTensor(DistMetaTensor&&) = default; + DistMetaTensor& operator=(DistMetaTensor&&) = default; + DistMetaTensor(const DistMetaTensor&) = default; + DistMetaTensor& operator=(const DistMetaTensor&) = default; + + virtual ~DistMetaTensor() = default; + + DDim dims() const override; + + const distributed::TensorDistAttr& dist_attr() const; + + private: + /** + * Note: When using the semi-automatic parallel segmentation derivation rules + * of the static graph, in order to facilitate the packaging of the input + * parameters of the construction, the DistMetaTensor is inherited and + * encapsulated, and the class members dims_ and dist_attr_ are added to it. + * + * The information contained in these two members is also in the tensor of the + * meta_tensor of the base class, and there is redundancy. + * + * We need to pay attention when using it to ensure the consistency. + * These two members are read-only, and their values cannot be changed + * after construction. To change their values, they need to be set + * directly in tensor_*/ + phi::DDim dims_; + TensorDistAttr dist_attr_; +}; + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc new file mode 100644 index 0000000000000..3b94dc017e5e7 --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc @@ -0,0 +1,97 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" + +namespace phi { +namespace distributed { + +void InferSpmdContext::EmplaceBackInput(DistMetaTensor input) { + inputs_.emplace_back(std::move(input)); +} + +void InferSpmdContext::EmplaceBackAttr(Attribute attr) { + attrs_.emplace_back(std::move(attr)); +} + +const DistMetaTensor& InferSpmdContext::InputAt(size_t idx) const { + return inputs_.at(idx); +} + +template +AttrType InferSpmdContext::AttrAt(size_t idx) const { + try { + return paddle::get(attrs_.at(idx)); + } catch (paddle::bad_variant_access const& e) { + PADDLE_THROW(phi::errors::InvalidArgument( + "Attribute cast error in InferSpmd Context, the input attr type is " + "`%s`, but the expected attribute type is `%s`.", + attrs_.at(idx).type().name(), + std::type_index(typeid(AttrType)).name())); + } +} + +template <> +bool InferSpmdContext::AttrAt(size_t idx) const { + try { + auto attr = attrs_.at(idx); + if (attr.type() == typeid(int)) { + return static_cast(paddle::get(attr)); + } else { + return paddle::get(attr); + } + } catch (paddle::bad_variant_access const& e) { + PADDLE_THROW(phi::errors::InvalidArgument( + "Attribute cast error in InferSpmd Context, the input attr type is " + "`%s`, but the expected attribute type is `bool`.", + attrs_.at(idx).type().name())); + } +} + +const Attribute& InferSpmdContext::AttrAt(size_t idx) const { + return attrs_.at(idx); +} + +SpmdRuleFactory& SpmdRuleFactory::Instance() { + static SpmdRuleFactory g_spmd_rule_map; + return g_spmd_rule_map; +} + +bool SpmdRuleFactory::ContainsSpmdRule(const std::string& kernel_name) const { + return spmd_rule_map_.count(kernel_name) > 0; +} + +int SpmdRuleFactory::InsertSpmdRule(std::string kernel_name, SpmdRule rule) { + PADDLE_ENFORCE_NE( + ContainsSpmdRule(kernel_name), + true, + phi::errors::AlreadyExists( + "`%s` Kernel's Spmd rules has been registered.", kernel_name)); + spmd_rule_map_.insert({std::move(kernel_name), std::move(rule)}); + return 0; +} + +const SpmdRule& SpmdRuleFactory::GetSpmdRule( + const std::string& kernel_name) const { + auto it = spmd_rule_map_.find(kernel_name); + PADDLE_ENFORCE_NE( + it, + spmd_rule_map_.end(), + phi::errors::NotFound("`%s` Kernel's Spmd rules is not registered.", + kernel_name)); + return it->second; +} + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h new file mode 100644 index 0000000000000..bccee2bf5981a --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h @@ -0,0 +1,186 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#pragma once + +#include +#include +#include +#include + +#include "paddle/phi/common/int_array.h" +#include "paddle/phi/common/scalar.h" +#include "paddle/phi/core/attribute.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" +#include "paddle/phi/core/distributed/type_defs.h" +#include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/macros.h" +#include "paddle/phi/core/type_defs.h" +#include "paddle/utils/any.h" +#include "paddle/utils/flat_hash_map.h" +#include "paddle/utils/small_vector.h" + +namespace phi { +namespace distributed { + +class InferSpmdContext { + public: + InferSpmdContext() = default; + InferSpmdContext( + paddle::small_vector inputs, + paddle::small_vector attrs) + : inputs_(std::move(inputs)), attrs_(std::move(attrs)) {} + + void EmplaceBackInput(DistMetaTensor input); + void EmplaceBackAttr(Attribute attr); + + const DistMetaTensor& InputAt(size_t idx) const; + + template + AttrType AttrAt(size_t idx) const; + + const Attribute& AttrAt(size_t idx) const; + + private: + // Now we only need `inputs`, for backward, the `output` is passed as input + paddle::small_vector inputs_; + // Because the attribute arguments of dygraph do not have `attr name`, + // so we use vector instead of map + paddle::small_vector attrs_; +}; + +using InferSpmdFn = SpmdInfo (*)(const InferSpmdContext&); + +#define PD_INFER_SPMD(...) \ + ::phi::distributed::InferSpmdFnImpl::Call + +template +struct InferSpmdTypeTag {}; + +template +struct InferSpmdFnImpl; + +template +struct InferSpmdFnImpl { + static SpmdInfo Call(const InferSpmdContext& ctx) { + return InferSpmdFnCallHelper>:: + template Call<0, 0>(ctx); + } + + private: + template + struct InferSpmdFnCallHelper; + + // TODO(chenweihang): support other input type later as needed + template + struct InferSpmdFnCallHelper { + template + static SpmdInfo Call(const InferSpmdContext& ctx, PreviousArgs&... pargs) { + static_assert(attr_idx == 0, + "InferSpmd's Input should appear before Attributes."); + const DistMetaTensor& arg = ctx.InputAt(in_idx); + return InferSpmdFnCallHelper::template Call( + ctx, pargs..., arg); + } + }; + +#define PD_SPECIALIZE_InferSpmdFnCallHelper_FOR_ATTRIBUTE(attr_type) \ + template \ + struct InferSpmdFnCallHelper { \ + template \ + static SpmdInfo Call(const InferSpmdContext& ctx, \ + PreviousArgs&... pargs) { \ + attr_type arg = ctx.AttrAt(attr_idx); \ + return InferSpmdFnCallHelper::template Call( \ + ctx, pargs..., arg); \ + } \ + } + + // TODO(chenweihang): support other attr type later as needed + PD_SPECIALIZE_InferSpmdFnCallHelper_FOR_ATTRIBUTE(bool); + + /* End case */ + template + struct InferSpmdFnCallHelper> { + template + static SpmdInfo Call(const InferSpmdContext& ctx UNUSED, Args&... args) { + return infer_spmd_fn(args...); + } + }; +}; + +class SpmdRule { + public: + explicit SpmdRule(InferSpmdFn forward_fn) + : forward_fn_(forward_fn), backward_fn_(nullptr) {} + + SpmdRule(InferSpmdFn forward_fn, InferSpmdFn backward_fn) + : forward_fn_(forward_fn), backward_fn_(backward_fn) {} + + SpmdInfo InferForward(const InferSpmdContext& ctx) const { + PADDLE_ENFORCE_NE( + forward_fn_, + nullptr, + phi::errors::NotFound("Current SpmdRule's forward function is not " + "found, Please make sure " + "that you have registered the rule correctly.")); + return forward_fn_(ctx); + } + + SpmdInfo InferBackward(const InferSpmdContext& ctx) const { + PADDLE_ENFORCE_NE( + backward_fn_, + nullptr, + phi::errors::NotFound("Current SpmdRule's backward function is not " + "found, Please make sure " + "that you have registered the rule correctly.")); + return backward_fn_(ctx); + } + + private: + InferSpmdFn forward_fn_; + InferSpmdFn backward_fn_; +}; + +// SpmdRuleFactory manage the spmd rules and cache the propagate results +// TODO(chenweihang): Add spmd caching impl later +class SpmdRuleFactory { + public: + static SpmdRuleFactory& Instance(); + + bool ContainsSpmdRule(const std::string& kernel_name) const; + + int InsertSpmdRule(std::string kernel_name, SpmdRule rule); + + const SpmdRule& GetSpmdRule(const std::string& kernel_name) const; + + private: + SpmdRuleFactory() = default; + + paddle::flat_hash_map spmd_rule_map_; + + DISABLE_COPY_AND_ASSIGN(SpmdRuleFactory); +}; + +#define PD_REGISTER_SPMD_RULE(kernel_name, ...) \ + UNUSED static int ___registrar_spmd_rule_for_##kernel_name = \ + ::phi::distributed::SpmdRuleFactory::Instance().InsertSpmdRule( \ + #kernel_name, ::phi::distributed::SpmdRule(__VA_ARGS__)); + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/type_defs.h b/paddle/phi/core/distributed/type_defs.h new file mode 100644 index 0000000000000..cd201ac5c5aaf --- /dev/null +++ b/paddle/phi/core/distributed/type_defs.h @@ -0,0 +1,29 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include +#include + +namespace phi { +namespace distributed { +class TensorDistAttr; + +using SpmdInfo = + std::pair, std::vector>; + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/meta_tensor.cc b/paddle/phi/core/meta_tensor.cc index 4ef20d9958284..146e0bc4fc662 100644 --- a/paddle/phi/core/meta_tensor.cc +++ b/paddle/phi/core/meta_tensor.cc @@ -17,8 +17,6 @@ limitations under the License. */ #include "glog/logging.h" #include "paddle/phi/core/dense_tensor.h" - -#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/string_tensor.h" @@ -208,6 +206,9 @@ bool MetaTensor::is_dense() const { return DenseTensor::classof(tensor_); } bool MetaTensor::is_selected_rows() const { return SelectedRows::classof(tensor_); } +bool MetaTensor::is_dist() const { + return distributed::DistTensor::classof(tensor_); +} bool MetaTensor::is_tensor_array() const { return false; } bool MetaTensor::is_same_tensor(const MetaTensor& meta_tensor) const { diff --git a/paddle/phi/core/meta_tensor.h b/paddle/phi/core/meta_tensor.h index 900bfb3eb6b3f..e7ccc1a61c5f2 100644 --- a/paddle/phi/core/meta_tensor.h +++ b/paddle/phi/core/meta_tensor.h @@ -23,7 +23,6 @@ limitations under the License. */ namespace phi { -// TODO(chenweihang): add other flags if needed struct MetaConfig { bool is_runtime{true}; bool is_run_mkldnn_kernel{false}; @@ -82,6 +81,8 @@ class MetaTensor { virtual bool is_selected_rows() const; virtual bool is_dense() const; + virtual bool is_dist() const; + // TODO(YuanRisheng) This API is for compatible with Fluid // and it will be deleted in the future. virtual bool is_tensor_array() const; @@ -97,7 +98,7 @@ class MetaTensor { protected: static void unspecified_bool_true() {} - private: + protected: // Because the lod in compiletime and runtime is different, // so `LoD` cannot in public methods const LoD& lod() const; diff --git a/paddle/phi/infermeta/CMakeLists.txt b/paddle/phi/infermeta/CMakeLists.txt index f53f655b24409..ef68ac8632ce4 100644 --- a/paddle/phi/infermeta/CMakeLists.txt +++ b/paddle/phi/infermeta/CMakeLists.txt @@ -1,5 +1,10 @@ add_subdirectory(strings) add_subdirectory(sparse) + +if(WITH_DISTRIBUTE) + add_subdirectory(spmd_rules) +endif() + collect_srcs( infermeta_srcs SRCS diff --git a/paddle/phi/infermeta/spmd_rules/CMakeLists.txt b/paddle/phi/infermeta/spmd_rules/CMakeLists.txt new file mode 100644 index 0000000000000..c28cd85c718c8 --- /dev/null +++ b/paddle/phi/infermeta/spmd_rules/CMakeLists.txt @@ -0,0 +1,6 @@ +file( + GLOB spmd_rules_srcs + RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" + "*.cc") + +collect_srcs(infermeta_srcs SRCS ${spmd_rules_srcs}) diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.cc b/paddle/phi/infermeta/spmd_rules/matmul.cc similarity index 69% rename from paddle/fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.cc rename to paddle/phi/infermeta/spmd_rules/matmul.cc index d280ccec37d7a..088f9ab16363a 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.cc +++ b/paddle/phi/infermeta/spmd_rules/matmul.cc @@ -4,7 +4,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at -http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -12,22 +12,29 @@ 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. */ -#include "paddle/fluid/distributed/auto_parallel/spmd_rules/matmul_spmd_rule.h" +#include "paddle/phi/infermeta/spmd_rules/matmul.h" +#include "glog/logging.h" + +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" #include "paddle/phi/core/distributed/auto_parallel/utils.h" +#include "paddle/phi/infermeta/spmd_rules/utils.h" -namespace paddle { +namespace phi { namespace distributed { -namespace auto_parallel { + using phi::distributed::auto_parallel::str_join; -TensorDistAttr GetInferedDistAttr( +////////////////// Utils Functions ////////////////// + +TensorDistAttr GetMatmulInferedDistAttr( const TensorDistAttr& origin_dist_attr, const std::vector& shape, const std::string& tensor_axis, const std::unordered_map& axis_to_dim_map, - const bool trans_axis) { - TensorDistAttr dist_attr_ = CopyTensorDistAttrForOutput(origin_dist_attr); + bool trans_axis) { + TensorDistAttr dist_attr = CopyTensorDistAttrForOutput(origin_dist_attr); std::vector infered_dims_mapping; infered_dims_mapping.reserve(tensor_axis.size()); @@ -50,8 +57,8 @@ TensorDistAttr GetInferedDistAttr( infered_dims_mapping.end() - 1); } - dist_attr_.set_dims_mapping(infered_dims_mapping); - return dist_attr_; + dist_attr.set_dims_mapping(infered_dims_mapping); + return dist_attr; } void FillMatmulOperandNotation(const int x_ndim, @@ -105,42 +112,35 @@ void FillMatmulOperandNotation(const int x_ndim, } } -std::pair, std::vector> -MatmulSPMDRule::InferForward(const std::vector& input_specs, - const paddle::framework::AttributeMap& attrs) { - // step0: verify input args based on matmul logic - auto input_specs_size = input_specs.size(); - PADDLE_ENFORCE_EQ( - input_specs_size, - 2, - phi::errors::InvalidArgument( - "The size of InputSpec of matmul should be 2, but got [%d].", - input_specs_size)); - auto x_shape = input_specs[0].shape(); - auto y_shape = input_specs[1].shape(); +////////////////// InferMeta(Contains SPMD) Functions ////////////////// + +SpmdInfo MatmulSpmdInferForward(const DistMetaTensor& x, + const DistMetaTensor& y, + bool trans_x, + bool trans_y) { + // Step0: verify input args based on matmul logic + auto x_shape = phi::vectorize(x.dims()); + auto y_shape = phi::vectorize(y.dims()); int x_ndim = x_shape.size(); int y_ndim = y_shape.size(); - auto x_dist_attr_src = input_specs[0].dist_attr(); - auto y_dist_attr_src = input_specs[1].dist_attr(); + auto x_dist_attr_src = x.dist_attr(); + auto y_dist_attr_src = y.dist_attr(); std::vector x_dims_mapping = x_dist_attr_src.dims_mapping(); std::vector y_dims_mapping = y_dist_attr_src.dims_mapping(); PADDLE_ENFORCE_EQ( x_ndim, x_dims_mapping.size(), - phi::errors::InvalidArgument( - "Mismatch of X's tensor size: [%d] and X's dims_mapping size [%d].", - x_ndim, - x_dims_mapping.size())); + phi::errors::InvalidArgument("The Tensor X's rank [%d] and X's " + "dims_mapping size [%d] are not matched.", + x_ndim, + x_dims_mapping.size())); PADDLE_ENFORCE_EQ( y_ndim, y_dims_mapping.size(), - phi::errors::InvalidArgument( - "Mismatch of Y's tensor size: [%d] and Y's dims_mapping size [%d].", - y_ndim, - y_dims_mapping.size())); - - bool trans_x = ExtractAttr("trans_x", attrs); - bool trans_y = ExtractAttr("trans_y", attrs); + phi::errors::InvalidArgument("The Tensor Y's rank [%d] and Y's " + "dims_mapping size [%d] are not matched.", + y_ndim, + y_dims_mapping.size())); VLOG(6) << "MatmulSPMDRule InferForward Inputs: " << "X shape: [" << str_join(x_shape) << "], x_dims_mapping: [" @@ -151,37 +151,37 @@ MatmulSPMDRule::InferForward(const std::vector& input_specs, << "trans_y: " << "[" << (trans_y ? "true" : "false") << "]; "; - // step1: build Einsum Notation + // Step1: build Einsum Notation std::string x_axes; std::string y_axes; std::string out_axes; FillMatmulOperandNotation(x_ndim, y_ndim, &x_axes, &y_axes, &out_axes); - // step2: Sharding Propogation + // Step2: Sharding Propogation if (trans_x) { - PADDLE_ENFORCE_GE( - x_ndim, - 2, - phi::errors::InvalidArgument("When trans_x is True, the size of X " - "tensor should be 2, but got [%d].", - x_ndim)); + PADDLE_ENFORCE_GE(x_ndim, + 2, + phi::errors::InvalidArgument( + "When trans_x is True, the size of X " + "tensor should be greater than 2, but got [%d].", + x_ndim)); std::iter_swap(x_dims_mapping.end() - 2, x_dims_mapping.end() - 1); } if (trans_y) { - PADDLE_ENFORCE_GE( - y_ndim, - 2, - phi::errors::InvalidArgument("When trans_x is True, the size of X " - "tensor should be 2, but got [%d].", - y_ndim)); + PADDLE_ENFORCE_GE(y_ndim, + 2, + phi::errors::InvalidArgument( + "When trans_y is True, the size of Y " + "tensor should be greater than 2, but got [%d].", + y_ndim)); std::iter_swap(y_dims_mapping.end() - 2, y_dims_mapping.end() - 1); } - // step2.1: Sharding Merge + // Step2.1: Sharding Merge std::pair> x_pair(x_axes, x_dims_mapping); std::pair> y_pair(y_axes, y_dims_mapping); auto axis_to_dim_map = ShardingMergeForTensors({x_pair, y_pair}); - // step2.2: Infer Output's Dims Mapping. + // Step2.2: Infer Output's Dims Mapping. TensorDistAttr output_dist_attr_dst = CopyTensorDistAttrForOutput(x_dist_attr_src); std::vector out_dims_mapping; @@ -191,13 +191,13 @@ MatmulSPMDRule::InferForward(const std::vector& input_specs, } output_dist_attr_dst.set_dims_mapping(out_dims_mapping); - // step2.3: Merge and get Inputs' New Dims Mapping. - TensorDistAttr x_dist_attr_dst = GetInferedDistAttr( + // Step2.3: Merge and get Inputs' New Dims Mapping. + TensorDistAttr x_dist_attr_dst = GetMatmulInferedDistAttr( x_dist_attr_src, x_shape, x_axes, axis_to_dim_map, trans_x); - TensorDistAttr y_dist_attr_dst = GetInferedDistAttr( + TensorDistAttr y_dist_attr_dst = GetMatmulInferedDistAttr( y_dist_attr_src, y_shape, y_axes, axis_to_dim_map, trans_y); - // step2.3: Handle Partial + // Step2.3: Handle Partial // Step2.3.1 Output Partial std::vector partial_on_dims = ResoluteOutputPartialDimension(axis_to_dim_map, out_axes); @@ -221,24 +221,16 @@ MatmulSPMDRule::InferForward(const std::vector& input_specs, return {{x_dist_attr_dst, y_dist_attr_dst}, {output_dist_attr_dst}}; } -std::pair, std::vector> -MatmulSPMDRule::InferBackward(const std::vector& input_specs, - const std::vector& output_specs, - const paddle::framework::AttributeMap& attrs) { - // extra & verify input - auto output_specs_size = output_specs.size(); - PADDLE_ENFORCE_EQ( - output_specs_size, - 1, - phi::errors::InvalidArgument( - "The size of OutputSpec of matmul should be 1, but got [%d].", - output_specs_size)); - - auto out_shape = output_specs[0].shape(); +SpmdInfo MatmulSpmdInferBackward(const DistMetaTensor& x, + const DistMetaTensor& y, + const DistMetaTensor& out, + bool trans_x, + bool trans_y) { + auto out_shape = phi::vectorize(out.dims()); int out_ndim = out_shape.size(); - auto x_shape = input_specs[0].shape(); - auto y_shape = input_specs[1].shape(); + auto x_shape = phi::vectorize(x.dims()); + auto y_shape = phi::vectorize(y.dims()); int x_ndim = x_shape.size(); int y_ndim = y_shape.size(); int max_ndim = std::max(x_ndim, y_ndim); @@ -250,10 +242,7 @@ MatmulSPMDRule::InferBackward(const std::vector& input_specs, max_ndim, out_ndim)); - bool trans_x = ExtractAttr("trans_x", attrs); - bool trans_y = ExtractAttr("trans_y", attrs); - - auto out_dist_attr_src = output_specs[0].dist_attr(); + auto out_dist_attr_src = out.dist_attr(); std::vector out_dims_mapping = out_dist_attr_src.dims_mapping(); // step1: build Einsum Notation @@ -267,10 +256,10 @@ MatmulSPMDRule::InferBackward(const std::vector& input_specs, auto axis_to_dim_map = ShardingMergeForTensors({{out_axes, out_dims_mapping}}, false); - TensorDistAttr x_dist_attr_dst = GetInferedDistAttr( - input_specs[0].dist_attr(), x_shape, x_axes, axis_to_dim_map, trans_x); - TensorDistAttr y_dist_attr_dst = GetInferedDistAttr( - input_specs[1].dist_attr(), y_shape, y_axes, axis_to_dim_map, trans_y); + TensorDistAttr x_dist_attr_dst = GetMatmulInferedDistAttr( + x.dist_attr(), x_shape, x_axes, axis_to_dim_map, trans_x); + TensorDistAttr y_dist_attr_dst = GetMatmulInferedDistAttr( + y.dist_attr(), y_shape, y_axes, axis_to_dim_map, trans_y); // step3: Handle Partial // NOTE we skip the partial backward inference in Partial Stage-I. @@ -289,6 +278,5 @@ MatmulSPMDRule::InferBackward(const std::vector& input_specs, return {{x_dist_attr_dst, y_dist_attr_dst}, {out_dist_attr_src}}; } -} // namespace auto_parallel } // namespace distributed -} // namespace paddle +} // namespace phi diff --git a/paddle/phi/infermeta/spmd_rules/matmul.h b/paddle/phi/infermeta/spmd_rules/matmul.h new file mode 100644 index 0000000000000..64cfba26a7445 --- /dev/null +++ b/paddle/phi/infermeta/spmd_rules/matmul.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#pragma once + +#include + +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" +#include "paddle/phi/core/distributed/type_defs.h" + +namespace phi { +namespace distributed { + +SpmdInfo MatmulSpmdInferForward(const DistMetaTensor& x, + const DistMetaTensor& y, + bool trans_x, + bool trans_y); + +SpmdInfo MatmulSpmdInferBackward(const DistMetaTensor& x, + const DistMetaTensor& y, + const DistMetaTensor& out, + bool trans_x, + bool trans_y); + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/infermeta/spmd_rules/rules.h b/paddle/phi/infermeta/spmd_rules/rules.h new file mode 100644 index 0000000000000..ad519ff287a33 --- /dev/null +++ b/paddle/phi/infermeta/spmd_rules/rules.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#pragma once + +#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" + +#include "paddle/phi/infermeta/spmd_rules/matmul.h" + +/** + * Design Notes: + * + * 1. SPMD info is the special meta info of DistTensor, so we put Spmd infer + * functions in `infermeta` directory. + * + * 2. Since the infer functions of Spmd forward and backward are closely related + * and need to be registered together, we manage them together in one file. + * + * 3. SPMD rules are much smaller than infermeta function, and we manage files + * in operator units. + * + * 4. The previous registration used some compile-time regular matching methods, + * which was less flexible, and the registration of SPMD rules here is declare + * directly in the header file + */ + +namespace phi { +namespace distributed { + +// matmul rule +PD_REGISTER_SPMD_RULE(matmul, + PD_INFER_SPMD(phi::distributed::MatmulSpmdInferForward), + PD_INFER_SPMD(phi::distributed::MatmulSpmdInferBackward)); + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/infermeta/spmd_rules/utils.cc b/paddle/phi/infermeta/spmd_rules/utils.cc new file mode 100644 index 0000000000000..2252de98a78b3 --- /dev/null +++ b/paddle/phi/infermeta/spmd_rules/utils.cc @@ -0,0 +1,159 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#include "paddle/phi/infermeta/spmd_rules/utils.h" + +#include "glog/logging.h" + +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/utils.h" +#include "paddle/phi/core/enforce.h" + +namespace phi { +namespace distributed { + +using phi::distributed::auto_parallel::str_join; + +std::string GetBroadcastAxes(const int64_t& tenosr_ndim, + const int64_t& broadcast_ndim, + const std::string& alphabet) { + PADDLE_ENFORCE_GE( + alphabet.size(), + broadcast_ndim, + phi::errors::InvalidArgument( + "The size of alphabet [%d] is less than broadcast ndim [%d]", + alphabet.size(), + broadcast_ndim)); + PADDLE_ENFORCE_GE(broadcast_ndim, + tenosr_ndim, + phi::errors::InvalidArgument( + "The broadcast ndim [%d] is less than tenosr ndim [%d]", + broadcast_ndim, + tenosr_ndim)); + if (tenosr_ndim <= 0) { + return std::string(); + } + return alphabet.substr(broadcast_ndim - tenosr_ndim, tenosr_ndim); +} + +// Rule1: A repicated dimension could be merged by any sharded dimension. +// Rule2: A tensor axis could at most be sharded by one mesh dimension. +// (TODO trigger heuristics cost model and reshard to handle axis sharded by +// multiple dimension case.) +int64_t ShardingMergeForAxis(const std::string& axis, + const int64_t& mesh_dim1, + const int64_t& mesh_dim2) { + if (mesh_dim1 != mesh_dim2) { + if (mesh_dim1 == -1) { + return mesh_dim2; + } else if (mesh_dim2 == -1) { + return mesh_dim1; + } else { + // (TODO) local cost model here. + PADDLE_THROW( + phi::errors::Unimplemented("Tensor Axis[%s] is Sharded by two " + "different mesh dimension [%d] and [%d].", + axis, + mesh_dim1, + mesh_dim2)); + } + + } else { + return mesh_dim1; + } +} + +std::unordered_map ShardingMergeForTensors( + const std::vector>>& + tensor_axes_to_dim_pairs, + const bool merge_conflicts) { + std::unordered_map axis_to_dim_map; + std::unordered_map dim_to_axis_map; + int64_t merge_dim; + + for (auto& pair : tensor_axes_to_dim_pairs) { + for (size_t i = 0; i < pair.second.size(); ++i) { + auto tensor_axis = pair.first.substr(i, 1); + auto mesh_dim = pair.second[i]; + + if (axis_to_dim_map.count(tensor_axis) == 0) { + merge_dim = mesh_dim; + } else { + merge_dim = ShardingMergeForAxis( + tensor_axis, mesh_dim, axis_to_dim_map[tensor_axis]); + } + axis_to_dim_map[tensor_axis] = merge_dim; + if (merge_dim != -1) { + if (dim_to_axis_map.count(merge_dim) == 0) { + dim_to_axis_map.insert({merge_dim, tensor_axis}); + } else if (dim_to_axis_map[merge_dim].find(tensor_axis) == + std::string::npos) { + dim_to_axis_map[merge_dim] += tensor_axis; + } + } + } + } + + // Resolute "mesh_dim shard by more than one axis" confict. + // Now we just naive pick the first axis naively. + // (TODO) use local cost model to pick the axis with lowest cost(in concern of + // memory or communication or computation). + for (auto& it : dim_to_axis_map) { + if (it.second.size() > 1) { + if (merge_conflicts) { + VLOG(4) << "Sharding Conflict: Mesh_Dim [" << it.first + << "] are Sharding Multiple Tensor Axis: [" << it.second + << "]. The Axis: [" << it.second[0] << "] is Picked."; + for (size_t i = 1; i < it.second.size(); ++i) { + axis_to_dim_map[it.second.substr(i, 1)] = -1; + } + } else { + PADDLE_THROW(phi::errors::PreconditionNotMet( + "Multiple Tensor Axes [%s] is sharded by same mesh dimension [%d].", + str_join(it.second), + it.first)); + } + } + } + + return axis_to_dim_map; +} + +TensorDistAttr CopyTensorDistAttrForOutput( + const TensorDistAttr& src_dist_attr) { + TensorDistAttr new_dist_attr = TensorDistAttr(); + new_dist_attr.set_process_mesh(src_dist_attr.process_mesh()); + new_dist_attr.set_batch_dim(src_dist_attr.batch_dim()); + new_dist_attr.set_dynamic_dims(src_dist_attr.dynamic_dims()); + // new_dist_attr.set_annotated(false); TODO unset field is false by default. + return new_dist_attr; +} + +std::vector ResoluteOutputPartialDimension( + const std::unordered_map& axis_to_dim_map, + const std::string& tensor_axes) { + std::vector partial_on_dims; + + for (auto& it : axis_to_dim_map) { + if (tensor_axes.find(it.first) == std::string::npos) { + if (it.second > -1) { + partial_on_dims.push_back(it.second); + } + } + } + return partial_on_dims; +} + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/infermeta/spmd_rules/utils.h b/paddle/phi/infermeta/spmd_rules/utils.h new file mode 100644 index 0000000000000..5e3c3a3d0961c --- /dev/null +++ b/paddle/phi/infermeta/spmd_rules/utils.h @@ -0,0 +1,65 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#pragma once + +#include +#include +#include +#include + +namespace phi { +namespace distributed { +class TensorDistAttr; + +// Generate the axis notation of tensor for the einsum notation of a broadcast +// operation(alignment star from the rightmost axis). tenosr_ndim: the size of +// the tensor. broadcast_ndim: the maxium size of tensors in this broadcast +// operation. alphabet: the characters used to represent the axes of tensor. +// length of alphabet should >= broadcast_ndim. +std::string GetBroadcastAxes(const int64_t& tenosr_ndim, + const int64_t& broadcast_ndim, + const std::string& alphabet); + +// Merge the sharding specification (dims mapping) for one tensor Axis. +// Rule1: A repicated dimension could be merged by any sharded dimension. +// Rule2: A tensor axis could at most be sharded by one mesh dimension. +// (TODO trigger heuristics cost model and reshard to handle axis sharded by +// multiple dimension case.) +int64_t ShardingMergeForAxis(const std::string& axis, + const int64_t& mesh_dim1, + const int64_t& mesh_dim2); + +// Merge sharding specification (dims mapping) of given tensors. +// The same axes of different tensors will be merged. +std::unordered_map ShardingMergeForTensors( + const std::vector>>& + tensor_axes_to_dim_pairs, + const bool merge_conflicts = true); + +// Intend to use for generating the TensorDistAttr of output based on the input +// activation TensorDistAttr. The process_mesh, batch_dim, dynamic_dim are +// copied with annotated is forced to False, and dims_mapping is leave to be +// null. +TensorDistAttr CopyTensorDistAttrForOutput(const TensorDistAttr& src_dist_attr); + +// Resolute the partial mesh dimension of a output tensor, giving the +// merged sharding specifcation of input tensors and the axis names of output +// tensor. Input are +std::vector ResoluteOutputPartialDimension( + const std::unordered_map& axis_to_dim_map, + const std::string& tensor_axes); + +} // namespace distributed +} // namespace phi diff --git a/test/auto_parallel/spmd_rules/test_matmul_rule.py b/test/auto_parallel/spmd_rules/test_matmul_rule.py index 59e47113302db..1cf2f49860b33 100644 --- a/test/auto_parallel/spmd_rules/test_matmul_rule.py +++ b/test/auto_parallel/spmd_rules/test_matmul_rule.py @@ -13,23 +13,23 @@ # limitations under the License. import unittest +from collections import OrderedDict -from paddle.distributed.auto_parallel.static.completion import get_spmd_rule from paddle.distributed.auto_parallel.static.dist_attribute import ( DistTensorSpec, TensorDistAttr, ) from paddle.distributed.fleet import auto +from paddle.framework import core class TestMatmulSPMDRule(unittest.TestCase): def setUp(self): - self.rule = get_spmd_rule("matmul") + # After replaced all spmd rules by phi impl, we can recover the + # api name to `get_spmd_rule` + self.rule = core.get_phi_spmd_rule("matmul") - self.attrs = { - 'trans_x': False, - 'trans_y': False, - } + self.attrs = OrderedDict([('trans_x', False), ('trans_y', False)]) def test_matmul_infer_forward(self): # forward setup @@ -49,7 +49,8 @@ def test_matmul_infer_forward(self): # TODO test partial: mk[1, 0],kn[0, -1] --> mk[1, 0],kn[0, -1] = nm[1, -1] partial[0] result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -68,7 +69,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([1, -1]) self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -82,7 +84,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([1, -1]) self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -95,7 +98,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([-1, -1]) self.y_dist_tensor_spec.set_dims_mapping([-1, 0]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -108,7 +112,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([1, 0]) self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -122,7 +127,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([-1, -1]) self.y_dist_tensor_spec.set_dims_mapping([1, 0]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -137,7 +143,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([0, 1, -1, -1]) self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -154,7 +161,8 @@ def test_matmul_infer_forward(self): self.x_dist_tensor_spec.set_dims_mapping([1, -1, -1, 0]) self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -173,7 +181,8 @@ def test_matmul_infer_forward(self): self.y_dist_tensor_spec.set_dims_mapping([-1, -1]) self.attrs['trans_x'] = True result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -192,7 +201,8 @@ def test_matmul_infer_forward(self): self.attrs['trans_x'] = False self.attrs['trans_y'] = True result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -215,7 +225,8 @@ def test_matmul_infer_forward(self): self.attrs['trans_x'] = True self.attrs['trans_y'] = True result_dist_attrs = self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -239,7 +250,8 @@ def test_matmul_infer_forward(self): self.attrs['trans_y'] = True with self.assertRaises(NotImplementedError): self.rule.infer_forward( - [self.x_dist_tensor_spec, self.y_dist_tensor_spec], self.attrs + [self.x_dist_tensor_spec, self.y_dist_tensor_spec], + list(self.attrs.values()), ) def test_matmul_infer_backward(self): @@ -270,7 +282,7 @@ def test_matmul_infer_backward(self): result_dist_attrs = self.rule.infer_backward( [self.x_dist_tensor_spec, self.y_dist_tensor_spec], [self.out_dist_tensor_spec], - self.attrs, + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -307,7 +319,7 @@ def test_matmul_infer_backward(self): result_dist_attrs = self.rule.infer_backward( [self.x_dist_tensor_spec, self.y_dist_tensor_spec], [self.out_dist_tensor_spec], - self.attrs, + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -329,7 +341,7 @@ def test_matmul_infer_backward(self): result_dist_attrs = self.rule.infer_backward( [self.x_dist_tensor_spec, self.y_dist_tensor_spec], [self.out_dist_tensor_spec], - self.attrs, + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -354,7 +366,7 @@ def test_matmul_infer_backward(self): result_dist_attrs = self.rule.infer_backward( [self.x_dist_tensor_spec, self.y_dist_tensor_spec], [self.out_dist_tensor_spec], - self.attrs, + list(self.attrs.values()), ) infered_input_dist_attrs = result_dist_attrs[0] infered_output_dist_attrs = result_dist_attrs[1] @@ -376,7 +388,7 @@ def test_matmul_infer_backward(self): self.rule.infer_backward( [self.x_dist_tensor_spec, self.y_dist_tensor_spec], [self.out_dist_tensor_spec], - self.attrs, + list(self.attrs.values()), ) diff --git a/test/auto_parallel/test_dist_tensor.py b/test/auto_parallel/test_dist_tensor.py index 45aa8c9fbcaae..0bf2d88db4237 100644 --- a/test/auto_parallel/test_dist_tensor.py +++ b/test/auto_parallel/test_dist_tensor.py @@ -83,6 +83,21 @@ def test_relu_api_for_dist_tensor(self): dist_out.backward() self.check_tensor_eq(local_in.grad, dist_in.grad) + def test_matmul_api_for_dist_tensor(self): + x = np.random.random(size=[4, 4]).astype("float32") + y = np.random.random(size=[4, 4]).astype("float32") + local_x, dist_x = self.create_local_and_dist_tensor_pair(x) + local_y, dist_y = self.create_local_and_dist_tensor_pair(y) + local_out = paddle.matmul(local_x, local_y) + dist_out = paddle.matmul(dist_x, dist_y) + self.check_tensor_eq(local_out, dist_out) + + # test backward + local_out.backward() + dist_out.backward() + self.check_tensor_eq(local_x.grad, dist_x.grad) + self.check_tensor_eq(local_y.grad, dist_y.grad) + if __name__ == "__main__": unittest.main() diff --git a/test/cpp/auto_parallel/CMakeLists.txt b/test/cpp/auto_parallel/CMakeLists.txt index c5912a6fa1021..ae7300bf62f08 100644 --- a/test/cpp/auto_parallel/CMakeLists.txt +++ b/test/cpp/auto_parallel/CMakeLists.txt @@ -9,8 +9,7 @@ if(WITH_DISTRIBUTE) dist_tensor_test SRCS dist_tensor_test.cc DEPS phi) + cc_test_old(spmd_rule_test SRCS spmd_rule_test.cc DEPS spmd_rules) endif() cc_test_old(dist_mapper_test SRCS dist_mapper_test.cc DEPS phi) - -cc_test_old(spmd_rule_test SRCS spmd_rule_test.cc DEPS spmd_rules) diff --git a/test/cpp/auto_parallel/spmd_rule_test.cc b/test/cpp/auto_parallel/spmd_rule_test.cc index dfd8394faa16a..30907b707aa9e 100644 --- a/test/cpp/auto_parallel/spmd_rule_test.cc +++ b/test/cpp/auto_parallel/spmd_rule_test.cc @@ -14,12 +14,16 @@ limitations under the License. */ #include #include + +#include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/common.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/dist_tensor_spec.h" #include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/inferspmd_utils.h" #include "paddle/phi/core/distributed/auto_parallel/process_mesh.h" +#include "paddle/phi/infermeta/spmd_rules/rules.h" namespace paddle { namespace distributed { @@ -45,22 +49,20 @@ TEST(MatmulSPMDRule, Ctor) { y_dist_attr.set_dims_mapping(std::vector({-1, -1})); y_dist_attr.set_dynamic_dims(std::vector({false, false})); - DistTensorSpec x_dist_tensor_spec = DistTensorSpec(x_shape, x_dist_attr); - DistTensorSpec y_dist_tensor_spec = DistTensorSpec(y_shape, y_dist_attr); + size_t input_size = 2; + size_t output_size = 1; - paddle::framework::AttributeMap attrs; - attrs["trans_x"] = false; - attrs["trans_y"] = false; + phi::distributed::DistMetaTensor x(phi::make_ddim(x_shape), x_dist_attr); + phi::distributed::DistMetaTensor y(phi::make_ddim(y_shape), y_dist_attr); - SPMDRuleBase* matmul_rule = SPMDRuleMap::Instance().Get("matmul"); + auto matmul_spmd_rule = + phi::distributed::SpmdRuleFactory::Instance().GetSpmdRule("matmul"); // mk[1, -1],kn[-1, -1] --> mk[1, -1],kn[-1, -1] = nm[1, -1] partial[] - std::pair, std::vector> - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + phi::distributed::InferSpmdContext ctx( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + auto infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); - size_t input_size = 2; - size_t output_size = 1; EXPECT_EQ(infered_dist_attrs.first.size(), input_size); EXPECT_EQ(infered_dist_attrs.second.size(), output_size); @@ -74,10 +76,13 @@ TEST(MatmulSPMDRule, Ctor) { VLOG(4) << "test1 done." << std::endl << std::endl << std::endl; // mk[-1,-1],kn[-1,0] --> mk[-1,-1],kn[-1,0] = nm[-1,0] partial[] - x_dist_tensor_spec.set_dims_mapping({-1, -1}); - y_dist_tensor_spec.set_dims_mapping({-1, 0}); - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({-1, -1}); + y_dist_attr.set_dims_mapping({-1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({-1, -1})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -88,10 +93,13 @@ TEST(MatmulSPMDRule, Ctor) { VLOG(4) << "test2 done." << std::endl << std::endl << std::endl; // mk[1, 0],kn[-1,-1] --> mk[1, 0],kn[0, -1] = nm[1, -1] partial[0]: done - x_dist_tensor_spec.set_dims_mapping({1, 0}); - y_dist_tensor_spec.set_dims_mapping({-1, -1}); - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({1, 0}); + y_dist_attr.set_dims_mapping({-1, -1}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({1, 0})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -104,10 +112,13 @@ TEST(MatmulSPMDRule, Ctor) { VLOG(4) << "test3 done." << std::endl << std::endl << std::endl; // mk[-1,-1],kn[1,0] --> mk[-1, 1],kn[1, 0] = nm[-1, 0] partial[1]: done - x_dist_tensor_spec.set_dims_mapping({-1, -1}); - y_dist_tensor_spec.set_dims_mapping({1, 0}); - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({-1, -1}); + y_dist_attr.set_dims_mapping({1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({-1, 1})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -121,11 +132,14 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[1, 0, -1, -1],kn[-1, -1] --> abcmk[1, 0, -1, -1],kn[-1, -1] = // abcmn[1, 0, -1, -1] partial[]: done - x_dist_tensor_spec.set_shape({512, 48, 64, 32}); - x_dist_tensor_spec.set_dims_mapping({0, 1, -1, -1}); - y_dist_tensor_spec.set_dims_mapping({-1, -1}); - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_shape = {512, 48, 64, 32}; + x_dist_attr.set_dims_mapping({0, 1, -1, -1}); + y_dist_attr.set_dims_mapping({-1, -1}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({0, 1, -1, -1})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -137,10 +151,13 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[1, -1, -1, 0],kn[-1, -1] --> abcmk[1, -1, -1, 0],kn[0, -1] = abcmn[1, // -1, -1, -1] partial[0]: done - x_dist_tensor_spec.set_dims_mapping({1, -1, -1, 0}); - y_dist_tensor_spec.set_dims_mapping({-1, -1}); - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({1, -1, -1, 0}); + y_dist_attr.set_dims_mapping({-1, -1}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({1, -1, -1, 0})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -154,11 +171,13 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[1, -1, -1, 0], kn[-1, -1] --> abcmk[1, -1, -1, 0],kn[-1, -1] = // abcmn[1, -1, 0, -1] partial[]: done - x_dist_tensor_spec.set_dims_mapping({1, -1, -1, 0}); - y_dist_tensor_spec.set_dims_mapping({-1, -1}); - attrs["trans_x"] = true; - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({1, -1, -1, 0}); + y_dist_attr.set_dims_mapping({-1, -1}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/true, /*trans_x=*/false}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({1, -1, -1, 0})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -170,12 +189,13 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[-1, -1, -1, -1], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = // abcmn[-1, -1, -1, 1] partial[0]: done - x_dist_tensor_spec.set_dims_mapping({-1, -1, -1, -1}); - y_dist_tensor_spec.set_dims_mapping({1, 0}); - attrs["trans_x"] = false; - attrs["trans_y"] = true; - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({-1, -1, -1, -1}); + y_dist_attr.set_dims_mapping({1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/false, /*trans_x=*/true}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({-1, -1, -1, 0})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -191,12 +211,13 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[-1, -1, -1, -1], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = // abcmn[-1, -1, -1, 1] partial[0]: done - x_dist_tensor_spec.set_dims_mapping({-1, -1, 0, 1}); - y_dist_tensor_spec.set_dims_mapping({1, 0}); - attrs["trans_y"] = true; - attrs["trans_x"] = true; - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({-1, -1, 0, 1}); + y_dist_attr.set_dims_mapping({1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/true, /*trans_x=*/true}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({-1, -1, 0, 1})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), @@ -214,23 +235,25 @@ TEST(MatmulSPMDRule, Ctor) { // abcmk[-1, -1, 1, 0], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = // abcmn[-1, -1, -1, 1] partial[0]: done - x_dist_tensor_spec.set_dims_mapping({-1, -1, 1, 0}); - y_dist_tensor_spec.set_dims_mapping({1, 0}); - attrs["trans_y"] = true; - attrs["trans_x"] = true; - EXPECT_ANY_THROW(infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs)); + x_dist_attr.set_dims_mapping({-1, -1, 1, 0}); + y_dist_attr.set_dims_mapping({1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/true, /*trans_x=*/true}); + EXPECT_ANY_THROW(infered_dist_attrs = matmul_spmd_rule.InferForward(ctx)); // Error VLOG(4) << "test10 done." << std::endl << std::endl << std::endl; // abcmk[-1, -1, -1, -1], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = // abcmn[-1, -1, -1, 1] partial[0]: - x_dist_tensor_spec.set_dims_mapping({-1, -1, 0, 1}); - y_dist_tensor_spec.set_dims_mapping({1, 0}); - attrs["trans_y"] = true; - attrs["trans_x"] = true; - infered_dist_attrs = matmul_rule->InferForward( - {x_dist_tensor_spec, y_dist_tensor_spec}, attrs); + x_dist_attr.set_dims_mapping({-1, -1, 0, 1}); + y_dist_attr.set_dims_mapping({1, 0}); + x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); + y = phi::distributed::DistMetaTensor(phi::make_ddim(y_shape), y_dist_attr); + ctx = phi::distributed::InferSpmdContext( + {x, y}, {/*trans_x=*/true, /*trans_x=*/true}); + infered_dist_attrs = matmul_spmd_rule.InferForward(ctx); EXPECT_ANY_THROW(infered_dist_attrs.second[0].clean_partial_dims( std::vector({1}))); infered_dist_attrs.second[0].set_partial_status(std::vector({1})); @@ -242,7 +265,6 @@ TEST(MatmulSPMDRule, Ctor) { std::set({0})); infered_dist_attrs.second[0].clean_partial_dims(std::vector({0})); EXPECT_EQ(infered_dist_attrs.second[0].is_partial(), false); - VLOG(4) << "test11 done." << std::endl << std::endl << std::endl; } @@ -372,25 +394,21 @@ TEST(MatmulSPMDRuleInferBackward, Ctor) { out_dist_attr.set_dynamic_dims(std::vector({false, false})); out_dist_attr.set_partial_status(std::vector({0})); - DistTensorSpec x_dist_tensor_spec = DistTensorSpec(x_shape, x_dist_attr); - DistTensorSpec y_dist_tensor_spec = DistTensorSpec(y_shape, y_dist_attr); - DistTensorSpec out_dist_tensor_spec = - DistTensorSpec(out_shape, out_dist_attr); - - paddle::framework::AttributeMap attrs; - attrs["trans_x"] = false; - attrs["trans_y"] = false; + phi::distributed::DistMetaTensor x(phi::make_ddim(x_shape), x_dist_attr); + phi::distributed::DistMetaTensor y(phi::make_ddim(y_shape), y_dist_attr); + phi::distributed::DistMetaTensor out(phi::make_ddim(out_shape), + out_dist_attr); - SPMDRuleBase* matmul_rule = SPMDRuleMap::Instance().Get("matmul"); + auto matmul_spmd_rule = + phi::distributed::SpmdRuleFactory::Instance().GetSpmdRule("matmul"); // TODO(zyc) update in future: propogate the partial in inferbackward // abmn[-1, -1, 1, -1] + partial[0] --> abmk[-1, -1, 1, -1], a1kn[-1, -1, -1, // -1] + phi::distributed::InferSpmdContext ctx( + {x, y, out}, {/*trans_x=*/false, /*trans_x=*/false}); std::pair, std::vector> - infered_dist_attrs = - matmul_rule->InferBackward({x_dist_tensor_spec, y_dist_tensor_spec}, - {out_dist_tensor_spec}, - attrs); + infered_dist_attrs = matmul_spmd_rule.InferBackward(ctx); size_t input_size = 2; size_t output_size = 1; From 68f8176be92dc4e88be0fb64fb4584fe0c986a87 Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Thu, 31 Aug 2023 10:00:54 +0800 Subject: [PATCH 032/194] add backward check_approval (#56195) * add backward check_approval * change check to api_file --- tools/check_file_diff_approvals.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index 5567fadfe7849..24e6c0ce1028d 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -86,7 +86,9 @@ API_FILES=("CMakeLists.txt" "paddle/fluid/prim/api/manual_prim/prim_manual_api.h" "paddle/fluid/prim/api/api.yaml" "python/paddle/incubate/autograd/composite_rules.py" - "python/paddle/incubate/autograd/primitives.py" + "python/paddle/incubate/autograd/primitives.py" + "python/paddle/autograd/backward.py" + "python/paddle/autograd/backward_utils.py" ) approval_line=`curl -H "Authorization: token ${GITHUB_API_TOKEN}" https://api.github.com/repos/PaddlePaddle/Paddle/pulls/${GIT_PR_ID}/reviews?per_page=10000` @@ -218,6 +220,9 @@ for API_FILE in ${API_FILES[*]}; do elif [ "${API_FILE}" == "python/paddle/incubate/autograd/primitives.py" ] || [ "${API_FILE}" == "python/paddle/incubate/autograd/composite_rules.py" ]; then echo_line="You must have one RD (cyber-pioneer(chenzhuo), xiaoguoguo626807(wangruting), Charles-hit(wanghao), JiabinYang) approval for changing ${API_FILE} , which manages the composite rules.\n" check_approval 1 cyber-pioneer xiaoguoguo626807 Charles-hit JiabinYang + elif [ "${API_FILE}" == "python/paddle/autograd/backward.py" ] || [ "${API_FILE}" == "python/paddle/autograd/backward_utils.py" ]; then + echo_line="You must be approved by Aurelius84(zhangliujie) or cxxly(chenxiaoxu) or xiaoguoguo626807(wangruting) or changeyoung98(chenzhiyang) for python/paddle/autograd/backward.py or python/paddle/autograd/backward_utils.py changes.\n" + check_approval 1 Aurelius84 cxxly xiaoguoguo626807 changeyoung98 else echo_line="You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1,qili93,Aurelius84) approval for ${API_FILE}, which manages the underlying code for fluid.\n" check_approval 1 XiaoguangHu01 chenwhql zhiqiu Xreki luotao1 qili93 Aurelius84 From d76794262eb3f3fd0b507576ae8047ee20359630 Mon Sep 17 00:00:00 2001 From: ronnywang Date: Thu, 31 Aug 2023 10:09:52 +0800 Subject: [PATCH 033/194] [ROCM] Remove the constraint with a maximum number of threads per block of 256, P1 (#56699) --- paddle/fluid/operators/graph_khop_sampler_op.cu | 10 +--------- paddle/fluid/operators/math/unpooling.cu | 16 ---------------- .../optimizers/distributed_fused_lamb_op.cu | 8 -------- .../platform/device/gpu/gpu_launch_config.h | 9 --------- paddle/phi/backends/gpu/gpu_launch_config.h | 9 --------- paddle/phi/kernels/funcs/cross_entropy.cu | 4 ---- paddle/phi/kernels/funcs/elementwise_grad_base.h | 4 ---- paddle/phi/kernels/funcs/for_range.h | 5 +---- paddle/phi/kernels/gpu/lars_momentum_kernel.cu | 4 ---- 9 files changed, 2 insertions(+), 67 deletions(-) diff --git a/paddle/fluid/operators/graph_khop_sampler_op.cu b/paddle/fluid/operators/graph_khop_sampler_op.cu index e533960c8a648..b4e0f511f6d61 100644 --- a/paddle/fluid/operators/graph_khop_sampler_op.cu +++ b/paddle/fluid/operators/graph_khop_sampler_op.cu @@ -287,11 +287,7 @@ void FillHashTable(const framework::ExecutionContext& ctx, thrust::device_vector* keys, thrust::device_vector* values, thrust::device_vector* key_index) { -#ifdef PADDLE_WITH_HIP - int block = 256; -#else int block = 1024; -#endif const auto& dev_ctx = ctx.cuda_device_context(); int max_grid_dimx = dev_ctx.GetCUDAMaxGridDimSize()[0]; int grid_tmp = (num_input + block - 1) / block; @@ -377,12 +373,8 @@ void ReindexFunc(const framework::ExecutionContext& ctx, subset->resize(unique_items.size()); thrust::copy(unique_items.begin(), unique_items.end(), subset->begin()); -// Fill outputs with reindex result. -#ifdef PADDLE_WITH_HIP - int block = 256; -#else + // Fill outputs with reindex result. int block = 1024; -#endif const auto& dev_ctx = ctx.cuda_device_context(); int64_t max_grid_dimx = dev_ctx.GetCUDAMaxGridDimSize()[0]; int64_t grid_tmp = (outputs->size() + block - 1) / block; diff --git a/paddle/fluid/operators/math/unpooling.cu b/paddle/fluid/operators/math/unpooling.cu index 0ecac6c5fb07a..a386772405ab6 100644 --- a/paddle/fluid/operators/math/unpooling.cu +++ b/paddle/fluid/operators/math/unpooling.cu @@ -126,11 +126,7 @@ class Unpool2dMaxFunctor { const T* input_data = input.data(); const int* indices_data = indices.data(); T* output_data = output->mutable_data(context.GetPlace()); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool2dMax <<>>(input.numel(), @@ -167,11 +163,7 @@ class Unpool2dMaxGradFunctor { const T* output_data = output.data(); const T* output_grad_data = output_grad.data(); T* input_grad_data = input_grad->mutable_data(context.GetPlace()); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool2dMaxGrad <<>>(input.numel(), @@ -206,11 +198,7 @@ class Unpool3dMaxFunctor { const T* input_data = input.data(); const int* indices_data = indices.data(); T* output_data = output->mutable_data(context.GetPlace()); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool3dMax <<>>(input.numel(), @@ -251,11 +239,7 @@ class Unpool3dMaxGradFunctor { const T* output_data = output.data(); const T* output_grad_data = output_grad.data(); T* input_grad_data = input_grad->mutable_data(context.GetPlace()); -#ifdef __HIPCC__ - int threads = 256; -#else int threads = 1024; -#endif int grid = (input.numel() + threads - 1) / threads; KernelUnpool3dMaxGrad <<>>(input.numel(), diff --git a/paddle/fluid/operators/optimizers/distributed_fused_lamb_op.cu b/paddle/fluid/operators/optimizers/distributed_fused_lamb_op.cu index cad7e38ba1c1a..fdec898edbe91 100644 --- a/paddle/fluid/operators/optimizers/distributed_fused_lamb_op.cu +++ b/paddle/fluid/operators/optimizers/distributed_fused_lamb_op.cu @@ -170,11 +170,7 @@ static void MultiTensorL2Norm(const phi::GPUPlace &place, constexpr int kNumTensor = MaxTensorNumPerLaunch; constexpr int kNumChunk = MaxChunkNumPerLaunch; -#ifdef PADDLE_WITH_HIP - constexpr int kBlockDim = 256; -#else constexpr int kBlockDim = 512; -#endif int max_chunk_num = -1; int vec_size = 8; @@ -812,11 +808,7 @@ static void MultiTensorUpdateLambParamAndBetaPows( phi::errors::InvalidArgument("Beta2Pow should be nullptr.")); } -#ifdef PADDLE_WITH_HIP - const int block_dim = 256; -#else const int block_dim = 512; -#endif int vec_size = 8; for (int i = 0; i < n; ++i) { diff --git a/paddle/fluid/platform/device/gpu/gpu_launch_config.h b/paddle/fluid/platform/device/gpu/gpu_launch_config.h index d253a92c986ce..98c6e379342f2 100644 --- a/paddle/fluid/platform/device/gpu/gpu_launch_config.h +++ b/paddle/fluid/platform/device/gpu/gpu_launch_config.h @@ -32,14 +32,9 @@ #include "paddle/fluid/platform/device_context.h" -#ifdef __HIPCC__ -// HIP results in error or nan if > 256 -#define PREDEFINED_BLOCK_SIZE 256 -#else /* CUDA performs better as thread_per_block num is between [64, 512] */ #define PREDEFINED_BLOCK_SIZE 512 -#endif namespace paddle { namespace platform { @@ -58,11 +53,7 @@ static inline int RoundToPowerOfTwo(int n) { n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); -#ifdef __HIPCC__ - return std::min(256, std::max(32, (n + 1))); -#else return std::min(1024, std::max(32, (n + 1))); -#endif } #ifdef WITH_NV_JETSON diff --git a/paddle/phi/backends/gpu/gpu_launch_config.h b/paddle/phi/backends/gpu/gpu_launch_config.h index a7a7ad03ad664..fd712baf75480 100644 --- a/paddle/phi/backends/gpu/gpu_launch_config.h +++ b/paddle/phi/backends/gpu/gpu_launch_config.h @@ -34,13 +34,8 @@ #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/enforce.h" -#ifdef __HIPCC__ -// HIP results in error or nan if > 256 -#define PREDEFINED_BLOCK_SIZE 256 -#else // CUDA performs better when thread_per_block is between [64, 512] #define PREDEFINED_BLOCK_SIZE 512 -#endif namespace phi { namespace backends { @@ -69,11 +64,7 @@ inline int64_t RoundToNextHighPowOfTwo(int64_t n, int64_t min_val = 1) { inline int64_t RoundToPowerOfTwo(int64_t n) { constexpr int64_t min_val = 32; int64_t num = RoundToNextHighPowOfTwo(n, min_val); -#ifdef __HIPCC__ - int64_t max_val = 256; -#else int64_t max_val = 1024; -#endif return std::min(max_val, num); } diff --git a/paddle/phi/kernels/funcs/cross_entropy.cu b/paddle/phi/kernels/funcs/cross_entropy.cu index add838106bfe8..20a15f9e944fe 100644 --- a/paddle/phi/kernels/funcs/cross_entropy.cu +++ b/paddle/phi/kernels/funcs/cross_entropy.cu @@ -124,11 +124,7 @@ void CrossEntropyFunctor::operator()( int batch_size = prob->dims()[0]; int class_num = prob->dims()[1]; -#ifdef __HIPCC__ - constexpr int kMaxBlockDim = 256; -#else constexpr int kMaxBlockDim = 512; -#endif if (softLabel) { const T* label_data = labels->data(); diff --git a/paddle/phi/kernels/funcs/elementwise_grad_base.h b/paddle/phi/kernels/funcs/elementwise_grad_base.h index df9e93474446e..5ff70c86d5fe8 100644 --- a/paddle/phi/kernels/funcs/elementwise_grad_base.h +++ b/paddle/phi/kernels/funcs/elementwise_grad_base.h @@ -32,11 +32,7 @@ limitations under the License. */ #endif -#ifdef __HIPCC__ -constexpr int ELEMWISE_MAX_BLOCK_DIM = 256; -#else constexpr int ELEMWISE_MAX_BLOCK_DIM = 1024; -#endif #define BLOCK_X 32 #define BLOCK_Y 32 diff --git a/paddle/phi/kernels/funcs/for_range.h b/paddle/phi/kernels/funcs/for_range.h index 9648a7d845ff0..484fbd21dc770 100644 --- a/paddle/phi/kernels/funcs/for_range.h +++ b/paddle/phi/kernels/funcs/for_range.h @@ -65,10 +65,7 @@ struct ForRange { template inline void operator()(Function func) const { -#ifdef __HIPCC__ - // HIP will throw core dump when threads > 256 - constexpr int num_threads = 256; -#elif WITH_NV_JETSON +#if WITH_NV_JETSON // JETSON_NANO will throw core dump when threads > 128 int num_thread = 256; backends::gpu::ChangeThreadNum(dev_ctx_, &num_thread, 128); diff --git a/paddle/phi/kernels/gpu/lars_momentum_kernel.cu b/paddle/phi/kernels/gpu/lars_momentum_kernel.cu index 14b7f1ca32860..be8b1ff3796d0 100644 --- a/paddle/phi/kernels/gpu/lars_momentum_kernel.cu +++ b/paddle/phi/kernels/gpu/lars_momentum_kernel.cu @@ -25,11 +25,7 @@ #include #endif -#ifdef __HIPCC__ -#define LARS_BLOCK_SIZE 256 -#else #define LARS_BLOCK_SIZE 512 -#endif #define LARS_MAX_MERGED_OPS 60 From e358ddacfb0905128e3254817be747b0c51e00dc Mon Sep 17 00:00:00 2001 From: 6clc Date: Thu, 31 Aug 2023 10:12:36 +0800 Subject: [PATCH 034/194] [CINN] set jobs by machine's cpu (#56605) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参照setup.py中设置max_jobs方法,设置cinn的build脚本的编译线程数。并且删除一些之前工作人员留下的对他们repo的依赖 --- tools/cinn/build.sh | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/tools/cinn/build.sh b/tools/cinn/build.sh index e2c1d0d0025d2..a32ae972e340b 100755 --- a/tools/cinn/build.sh +++ b/tools/cinn/build.sh @@ -24,7 +24,10 @@ cinn_whl_path=python/dist/cinn-0.0.0-py3-none-any.whl #export LLVM11_DIR=${workspace}/THIRDS/usr -JOBS=8 +if [[ "" == ${JOBS} ]]; then + JOBS=`nproc` +fi + cuda_config=OFF cudnn_config=OFF @@ -118,19 +121,6 @@ function prepare_model { proxy_on mkdir -p $build_dir/paddle - cd $build_dir/paddle - if [[ ! -f "libexternal_kernels.so.tgz" ]]; then - wget https://github.com/T8T9/files/raw/main/libexternal_kernels.so.tgz - fi - tar -zxvf libexternal_kernels.so.tgz - if [[ ! -f "paddle_1.8_fc_model.tgz" ]]; then - wget https://github.com/T8T9/files/raw/main/paddle_1.8_fc_model.tgz - fi - tar -zxvf paddle_1.8_fc_model.tgz - if [[ ! -f "mkldnn.tgz" ]]; then - wget https://github.com/T8T9/files/raw/main/mkldnn.tgz - fi - tar -zxvf mkldnn.tgz cd $build_dir/third_party python${py_version} $workspace/test/cinn/fake_model/naive_mul.py python${py_version} $workspace/test/cinn/fake_model/naive_multi_fc.py From 0bc369ef64f65803abf58bff4cff22d6ae68e671 Mon Sep 17 00:00:00 2001 From: Zero Rains Date: Thu, 31 Aug 2023 10:26:29 +0800 Subject: [PATCH 035/194] [Fluid] Move distributed_fused_lamb_init to phi (#55993) --- .../distributed_fused_lamb_init_op.cc | 9 +- .../distributed_fused_lamb_init_op.cu | 797 ----------------- .../distributed_fused_lamb_init_op.h | 33 - .../distributed_fused_lamb_init_kernel.h | 52 ++ .../cpu/distributed_fused_lamb_init_kernel.cc | 80 ++ .../kernels/fusion/gpu}/cast_with_ptr.h | 29 +- .../gpu/distributed_fused_lamb_init_kernel.cu | 804 ++++++++++++++++++ .../compat/distributed_fused_lamb_init_sig.cc | 48 ++ 8 files changed, 997 insertions(+), 855 deletions(-) delete mode 100644 paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cu delete mode 100644 paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h create mode 100644 paddle/phi/kernels/distributed_fused_lamb_init_kernel.h create mode 100644 paddle/phi/kernels/fusion/cpu/distributed_fused_lamb_init_kernel.cc rename paddle/{fluid/operators/optimizers => phi/kernels/fusion/gpu}/cast_with_ptr.h (74%) create mode 100644 paddle/phi/kernels/fusion/gpu/distributed_fused_lamb_init_kernel.cu create mode 100644 paddle/phi/ops/compat/distributed_fused_lamb_init_sig.cc diff --git a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cc b/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cc index aeda36e537c7f..59313bc95af0a 100644 --- a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cc +++ b/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cc @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h" +#include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/framework/operator.h" namespace paddle { namespace operators { @@ -116,9 +117,3 @@ namespace ops = paddle::operators; REGISTER_OP_WITHOUT_GRADIENT(distributed_fused_lamb_init, ops::DistributedFusedLambInitOp, ops::DistributedFusedLambInitOpMaker); - -PD_REGISTER_STRUCT_KERNEL(distributed_fused_lamb_init, - CPU, - ALL_LAYOUT, - ops::DistributedFusedLambInitOpKernel, - float) {} diff --git a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cu b/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cu deleted file mode 100644 index 8841544366e87..0000000000000 --- a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.cu +++ /dev/null @@ -1,797 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h" -#include "paddle/fluid/memory/memcpy.h" -#include "paddle/fluid/operators/optimizers/cast_with_ptr.h" -#include "paddle/fluid/platform/device/gpu/gpu_launch_config.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/kernels/funcs/algorithm.h" -#include "paddle/phi/kernels/funcs/math_function.h" -#include "paddle/phi/kernels/funcs/tensor_to_string.h" - -namespace paddle { -namespace operators { - -using phi::funcs::FlattenToString; -using phi::funcs::ToVector; - -struct ParamGradInfo { - phi::DenseTensor *param_t{nullptr}; - phi::DenseTensor *grad_t{nullptr}; - size_t idx{0}; - size_t numel{0}; - size_t numel_with_padding{0}; - size_t numel_offset{0}; -}; - -static std::ostream &operator<<(std::ostream &os, const ParamGradInfo &info) { - return os << "{Param(" << info.param_t << "),Grad(" << info.grad_t << "),idx(" - << info.idx << "),numel(" << info.numel << "),numel_with_padding(" - << info.numel_with_padding << "),numel_offset(" << info.numel_offset - << "),padding(" << info.numel_offset + info.numel_with_padding - << "-" << info.numel_offset + info.numel << "=" - << info.numel_with_padding - info.numel << ")}"; -} - -struct ParamGradInfoNumelOffsetCompFunctor { - bool operator()(const ParamGradInfo &x, const ParamGradInfo &y) const { - return x.numel_offset < y.numel_offset; - } - - bool operator()(const ParamGradInfo &x, size_t y) const { - return x.numel_offset < y; - } - - bool operator()(size_t x, const ParamGradInfo &y) const { - return x < y.numel_offset; - } - - bool operator()(size_t x, size_t y) const { return x < y; } -}; - -static size_t GetAlignSize(size_t n, size_t alignment) { - auto remainder = n % alignment; - return remainder == 0 ? n : n + alignment - remainder; -} - -// Shard the ParamGradInfo list by the numel size [start_size, end_size) -// The final results should be: -// -// start_size = sum(infos[0:i].numel_with_padding) + start_numel_offset, where -// start_numel_offset <= infos[i].numel_with_padding -// -// end_size = sum(infos[0:j].numel_with_padding) + end_numel_offset, where -// end_numel_offset <= infos[j].numel_with_padding -static void GetParamGradShardInfo(const std::vector &infos, - size_t start_size, - size_t end_size, - size_t *start_idx, - size_t *end_idx, - size_t *start_numel_offset, - size_t *end_numel_offset) { - VLOG(10) << "NumelOffset: " - << string::join_strings(infos, ",", [](const ParamGradInfo &info) { - return info.numel_offset; - }); - VLOG(10) << "start_size = " << start_size << " , end_size = " << end_size; - - if (infos.empty()) { - PADDLE_ENFORCE_EQ( - start_size, - 0, - platform::errors::InvalidArgument("start_size should be 0.")); - PADDLE_ENFORCE_EQ( - end_size, - 0, - platform::errors::InvalidArgument("end_size should be 0.")); - *start_idx = 0; - *end_idx = 0; - *start_numel_offset = 0; - *end_numel_offset = 0; - return; - } - - PADDLE_ENFORCE_LT(start_size, - end_size, - platform::errors::InvalidArgument( - "start_size should be less than end_size.")); - size_t n = infos.size(); - ParamGradInfoNumelOffsetCompFunctor comp; - auto i = static_cast( - std::lower_bound(infos.begin(), infos.end(), start_size, comp) - - infos.begin()); - if (i == n || infos[i].numel_offset != start_size) { - PADDLE_ENFORCE_GT( - i, - 0, - platform::errors::InvalidArgument( - "Cannot find suitable sharding which is between [%d, %d)", - start_size, - end_size)); - --i; - } - PADDLE_ENFORCE_LT( - i, - n, - platform::errors::InvalidArgument( - "Cannot find suitable sharding which is between [%d, %d)", - start_size, - end_size)); - *start_idx = i; - *start_numel_offset = start_size - infos[i].numel_offset; - auto j = static_cast( - std::lower_bound(infos.begin(), infos.end(), end_size, comp) - - infos.begin()); - *end_idx = j - 1; - *end_numel_offset = end_size - infos[j - 1].numel_offset; - PADDLE_ENFORCE_GT(*end_numel_offset, - 0, - platform::errors::InvalidArgument( - "Internal error when sharding, this may be a bug " - "caused by empty parameter.")); - VLOG(10) << "Sharding [start_size=" << start_size << ", end_size=" << end_size - << "): " << (*start_idx) << ":" << (*start_numel_offset) << " -> " - << (*end_idx) << ":" << (*end_numel_offset); -} - -static size_t FillAlignmentPaddingInfo(std::vector *infos, - size_t alignment, - size_t nranks, - phi::DataType dtype) { - auto sizeof_dtype = phi::SizeOf(dtype); - PADDLE_ENFORCE_EQ( - alignment % sizeof_dtype, - 0, - platform::errors::InvalidArgument( - "The attr(alignment) should be exactly divided by sizeof(T) %d.", - sizeof_dtype)); - alignment /= sizeof_dtype; - - size_t total_numel_sum_with_padding = 0; - size_t n = infos->size(); - for (size_t i = 0; i < n; ++i) { - auto &info = (*infos)[i]; - size_t numel_with_padding; - if (i + 1 == n) { - // the total fused numel must be a factor of alignment * nranks - numel_with_padding = - GetAlignSize(info.numel + total_numel_sum_with_padding, - alignment * nranks) - - total_numel_sum_with_padding; - } else { - numel_with_padding = GetAlignSize(info.numel, alignment); - } - info.numel_with_padding = numel_with_padding; - info.numel_offset = total_numel_sum_with_padding; - total_numel_sum_with_padding += numel_with_padding; - } - return total_numel_sum_with_padding; -} - -template -static T *TensorFillConstant(const phi::GPUContext &dev_ctx, - phi::DenseTensor *tensor, - const framework::DDim &dims, - T value) { - tensor->Resize(dims); - auto *ptr = tensor->mutable_data(dev_ctx.GetPlace()); - phi::funcs::SetConstant set_constant; - set_constant(dev_ctx, tensor, value); - return ptr; -} - -static phi::DenseTensor CastDataForInitedTensor(const phi::GPUContext &dev_ctx, - phi::DenseTensor *origin, - phi::DenseTensor *fused_out, - size_t numel_offset) { - PADDLE_ENFORCE_EQ(origin->IsInitialized(), - true, - platform::errors::InvalidArgument( - "The tensor to be cast should be initialized.")); - - PADDLE_ENFORCE_EQ(fused_out->dtype(), - phi::DataType::FLOAT32, - platform::errors::InvalidArgument( - "The dst tensor to be cast should be FP32 tensor.")); - PADDLE_ENFORCE_EQ(origin->dtype(), - phi::DataType::FLOAT16, - platform::errors::InvalidArgument( - "The src tensor to be cast should be FP16 tensor.")); - auto *dst = fused_out->data() + numel_offset; - auto *src = origin->data(); - auto numel = origin->numel(); - LaunchCastKernel(dev_ctx, src, dst, numel); - VLOG(10) << "Cast from FP32 -> FP16, range: [" << numel_offset << ", " - << numel_offset + numel << ")" - << " , total: [0, " << fused_out->numel() << ")"; - framework::DDim fused_out_dim = fused_out->dims(); - auto fused_out_numel = fused_out->numel(); - fused_out->Resize({fused_out_numel}); - auto sliced_tensor = fused_out->Slice(numel_offset, numel + numel_offset); - fused_out->Resize(fused_out_dim); - return sliced_tensor; -} - -static phi::DenseTensor CopyAndShareBufferForInitedTensor( - phi::DenseTensor *origin, - phi::DenseTensor *fused_out, - size_t numel_offset, - gpuStream_t stream) { - PADDLE_ENFORCE_EQ( - origin->IsInitialized(), - true, - platform::errors::InvalidArgument( - "The tensor to be copied and shared data should be initialized.")); - auto dtype = fused_out->type(); - PADDLE_ENFORCE_EQ(origin->type(), - dtype, - platform::errors::InvalidArgument( - "The tensor to be copied and shared data should be " - "have the same data type.")); - auto place = fused_out->place(); - PADDLE_ENFORCE_EQ( - origin->place(), - place, - platform::errors::InvalidArgument("The tensor to be copied and shared " - "data should be have the same place.")); - PADDLE_ENFORCE_EQ( - platform::is_gpu_place(place), - true, - platform::errors::InvalidArgument( - "The tensor to be copied and shared data should be on GPU place.")); - - auto numel = origin->numel(); - framework::DDim fused_out_dim = fused_out->dims(); - auto fused_out_numel = fused_out->numel(); - auto sliced_tensor = fused_out->Resize({fused_out_numel}) - .Slice(numel_offset, numel + numel_offset); - memory::Copy(place, - sliced_tensor.data(), - place, - origin->data(), - numel * phi::SizeOf(dtype), - stream); - origin->ShareBufferWith(sliced_tensor); - fused_out->Resize(fused_out_dim); - VLOG(10) << "Copy and share buffer, range: [" << numel_offset << ", " - << numel_offset + numel << ") , total: [0, " << fused_out->numel() - << ") , dtype = " << dtype; - return sliced_tensor; -} - -static void ShareBufferForNonInitedTensor(phi::DenseTensor *origin, - phi::DenseTensor *fused_out, - size_t numel_offset, - const framework::DDim &dims) { - PADDLE_ENFORCE_EQ( - origin->IsInitialized(), - false, - platform::errors::InvalidArgument( - "The tensor to be shared data should not be initialized.")); - - framework::DDim fused_out_dim = fused_out->dims(); - auto fused_out_numel = fused_out->numel(); - auto numel = phi::product(dims); - *origin = fused_out->Resize({fused_out_numel}) - .Slice(numel_offset, numel + numel_offset); - origin->Resize(dims); - fused_out->Resize(fused_out_dim); - VLOG(10) << "Share buffer for non-inited, range: [" << numel_offset << ", " - << numel_offset + numel << "), total: [0, " << fused_out->numel() - << ") , dtype = " << fused_out->dtype(); -} - -template -static void CopyVectorToCPUTensor(const std::vector &src, - phi::DenseTensor *dst) { - dst->Resize({static_cast(src.size())}); - T *dst_ptr = dst->mutable_data(platform::CPUPlace()); - const T *src_ptr = src.data(); - auto nbytes = src.size() * sizeof(T); - std::memcpy(dst_ptr, src_ptr, nbytes); -} - -static size_t ReorderParamGradInfoList(const std::vector &flags, - std::vector *infos) { - size_t n = infos->size(); - std::vector cur_flags; - cur_flags.reserve(n); - for (size_t i = 0; i < n; ++i) { - auto idx = (*infos)[i].idx; - cur_flags.push_back(flags[idx]); - } - - auto origin_infos = *infos; - size_t j = 0; - for (size_t i = 0; i < n; ++i) { - if (cur_flags[i]) { - (*infos)[j] = origin_infos[i]; - ++j; - } - } - size_t ret_idx = j; - - for (size_t i = 0; i < n; ++i) { - if (!cur_flags[i]) { - (*infos)[j] = origin_infos[i]; - ++j; - } - } - return ret_idx; -} - -template -static T ClipByBound(T x, T low_value, T high_value) { - if (x < low_value) return low_value; - if (x > high_value) return high_value; - return x; -} - -template -class DistributedFusedLambInitOpKernel - : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext &ctx) const override { - VLOG(10) << "starts to run DistributedFusedLambInitOp"; - auto &dev_ctx = ctx.template device_context(); - auto place = ctx.GetPlace(); - auto stream = dev_ctx.stream(); - - // Step 1: Check Input(Param) and Output(ParamOut), Input(Grad) and - // Output(GradOut) - auto params = ctx.MultiInput("Param"); - auto grads = ctx.MultiInput("Grad"); - auto master_params = ctx.MultiOutput("MasterParamOut"); - std::vector fp32_infos, fp16_infos; - { - PADDLE_ENFORCE_EQ(params.size(), - grads.size(), - platform::errors::InvalidArgument( - "The parameter number and parameter gradient " - "number should be the same.")); - - auto params_out = ctx.MultiOutput("ParamOut"); - auto grads_out = ctx.MultiOutput("GradOut"); - PADDLE_ENFORCE_EQ( - params.size(), - params_out.size(), - platform::errors::InvalidArgument("Input(Param) and Output(ParamOut) " - "should have the same number.")); - PADDLE_ENFORCE_EQ( - grads.size(), - grads_out.size(), - platform::errors::InvalidArgument( - "Input(Grad) and Output(GradOut) should have the same number.")); - size_t n = params.size(); - VLOG(10) << "parameter number: " << n; - for (size_t i = 0; i < n; ++i) { - auto *p = params[i]; - auto *g = grads[i]; - auto *p_out = params_out[i]; - auto *g_out = grads_out[i]; - - PADDLE_ENFORCE_NOT_NULL( - p, - platform::errors::InvalidArgument( - "The %d-th parameter should not be nullptr.", i)); - PADDLE_ENFORCE_EQ(p->IsInitialized(), - true, - platform::errors::InvalidArgument( - "The %d-th parameter should be initialized.", i)); - PADDLE_ENFORCE_EQ( - p->place(), - place, - platform::errors::InvalidArgument( - "The %d-th parameter is not initialized on the right place.", - i)); - PADDLE_ENFORCE_EQ(p, - p_out, - platform::errors::InvalidArgument( - "The %d-th Input(Param) and Output(ParamOut) " - "should be the same tensor.", - i)); - - auto dtype = p->dtype(); - PADDLE_ENFORCE_NOT_NULL( - g, - platform::errors::InvalidArgument( - "The %d-th gradient should not be nullptr.", i)); - PADDLE_ENFORCE_EQ(g, - g_out, - platform::errors::InvalidArgument( - "The %d-th Input(Grad) and Output(Grad) should " - "be the same tensor.")); - auto numel = p->numel(); - PADDLE_ENFORCE_GT(numel, - 0, - platform::errors::InvalidArgument( - "The %d-th Input(Param) have no elements.")); - - void *g_data = nullptr; - if (g->IsInitialized()) { - PADDLE_ENFORCE_EQ(g->dtype(), - dtype, - platform::errors::InvalidArgument( - "The %d-th Input(Param) and Input(Grad) should " - "have the same data type %s.", - i, - dtype)); - PADDLE_ENFORCE_EQ(g->dims(), - p->dims(), - platform::errors::InvalidArgument( - "The %d-th Input(Param) and Input(Grad) should " - "have the same shape.", - i)); - g_data = g_out->data(); - } - - ParamGradInfo *info; - if (dtype == phi::DataType::FLOAT32) { - fp32_infos.emplace_back(); - info = &fp32_infos.back(); - } else if (dtype == phi::DataType::FLOAT16) { - fp16_infos.emplace_back(); - info = &fp16_infos.back(); - } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "Unsupported data type %s.", dtype)); - } - - VLOG(10) << "Found " << dtype << " parameter " << i << " shape=[" - << p_out->dims() << "] numel=" << numel - << " grad.IsInitialized()=" - << (g_out->IsInitialized() ? "true" : "false"); - - info->param_t = p_out; - info->grad_t = g_out; - info->idx = i; - info->numel = numel; - info->numel_with_padding = 0; // not determined yet - info->numel_offset = 0; // not determined yet - } - } - const auto &apply_weight_decay = - ctx.Attr>("apply_weight_decay"); - size_t fp32_wd_end_idx = - ReorderParamGradInfoList(apply_weight_decay, &fp32_infos); - size_t fp16_wd_end_idx = - ReorderParamGradInfoList(apply_weight_decay, &fp16_infos); - - auto *param_order_t = ctx.Output("ParamOrder"); - auto param_num = fp32_infos.size() + fp16_infos.size(); - param_order_t->Resize({static_cast(param_num)}); - auto *param_order = param_order_t->mutable_data(platform::CPUPlace()); - for (size_t i = 0; i < fp32_infos.size(); ++i) { - param_order[i] = static_cast(fp32_infos[i].idx); - } - for (size_t i = 0; i < fp16_infos.size(); ++i) { - param_order[i + fp32_infos.size()] = static_cast(fp16_infos[i].idx); - } - - VLOG(10) << "Fill ParamGradInfo ends"; - - // Step 2: determine the numel_with_padding and numel_offset - auto rank = ctx.Attr("rank"); - auto nranks = ctx.Attr("nranks"); - auto alignment = ctx.Attr("alignment"); - VLOG(10) << "rank = " << rank << ", nranks = " << nranks - << " , alignment = " << alignment; - if (alignment <= 0) { - alignment = platform::GpuMinChunkSize(); - } - PADDLE_ENFORCE_GE(alignment, - 1, - platform::errors::InvalidArgument( - "The attr(alignment) should be larger than 0.")); - PADDLE_ENFORCE_EQ(alignment & (alignment - 1), - 0, - platform::errors::InvalidArgument( - "The attr(alignment) should be the power of 2.")); - PADDLE_ENFORCE_GE( - rank, - 0, - platform::errors::InvalidArgument( - "The attr(rank) should be equal to or larger than 0.")); - PADDLE_ENFORCE_LT( - rank, - nranks, - platform::errors::InvalidArgument( - "The attr(rank) should be less than the attr(nranks).")); - // NOTE: We guarantee that both fp32_numel and fp16_numel can be exactly - // divided by alignment and nranks. - auto fp32_numel = FillAlignmentPaddingInfo( - &fp32_infos, alignment, nranks, phi::DataType::FLOAT32); - VLOG(10) << "FP32 ParamGradInfo: " << string::join_strings(fp32_infos, " "); - auto fp16_numel = FillAlignmentPaddingInfo( - &fp16_infos, alignment, nranks, phi::DataType::FLOAT16); - VLOG(10) << "FP16 ParamGradInfo: " << string::join_strings(fp16_infos, " "); - auto total_numel = fp32_numel + fp16_numel; - PADDLE_ENFORCE_LT( - total_numel, - std::numeric_limits::max(), - platform::errors::InvalidArgument("Too many parameter number.")); - - auto fp32_numel_each_device = fp32_numel / nranks; - auto fp16_numel_each_device = fp16_numel / nranks; - auto numel_each_device = fp32_numel_each_device + fp16_numel_each_device; - VLOG(10) << "Fill padding ends. total_numel = " << total_numel - << ", fp32_numel = " << fp32_numel - << ", fp16_numel = " << fp16_numel - << ", fp32_numel_each_device = " << fp32_numel_each_device - << ", fp16_numel_each_device = " << fp16_numel_each_device; - - // Step 3: allocate output tensor and do initialization - float *fused_fp32_param = nullptr, *fused_fp32_grad = nullptr; - platform::float16 *fused_fp16_param = nullptr, *fused_fp16_grad = nullptr; - phi::DenseTensor *fp32_p_t = nullptr, *fp16_p_t = nullptr, - *fp32_g_t = nullptr, *fp16_g_t = nullptr; - std::vector fp16_master_params; - if (total_numel > 0) { - fp32_p_t = ctx.Output("FP32FusedParam"); - fused_fp32_param = TensorFillConstant( - dev_ctx, fp32_p_t, {static_cast(total_numel)}, 0.0f); - } - - if (fp32_numel > 0) { - fp32_g_t = ctx.Output("FP32FusedGrad"); - fused_fp32_grad = TensorFillConstant( - dev_ctx, fp32_g_t, {static_cast(fp32_numel)}, 0.0f); - } - - if (fp16_numel > 0) { - fp16_p_t = ctx.Output("FP16FusedParam"); - fused_fp16_param = TensorFillConstant( - dev_ctx, - fp16_p_t, - {static_cast(fp16_numel)}, - static_cast(0)); - - fp16_g_t = ctx.Output("FP16FusedGrad"); - fused_fp16_grad = TensorFillConstant( - dev_ctx, - fp16_g_t, - {static_cast(fp16_numel)}, - static_cast(0)); - } - VLOG(10) << "Allocate FP32FusedParam/Grad, FP16FusedParam/Grad ends"; - - // (1) For FP32FusedParam, memcpy for fp32 param and then share data, cast - // for fp16 master weight - // (2) For FP16FusedParam, memcpy and then share data - // (3) For FP32FusedGrad/FP16FusedGrad, memcpy if gradient has been inited - for (const auto &info : fp32_infos) { - auto sliced_tensor = CopyAndShareBufferForInitedTensor( - info.param_t, fp32_p_t, info.numel_offset, stream); - master_params[info.idx]->Resize(info.param_t->dims()); - master_params[info.idx]->ShareBufferWith(sliced_tensor); - PADDLE_ENFORCE_EQ(master_params[info.idx]->mutable_data(place), - sliced_tensor.data(), - platform::errors::InvalidArgument( - "Invalid master weight tensor pointer.")); - if (info.grad_t->IsInitialized()) { - CopyAndShareBufferForInitedTensor( - info.grad_t, fp32_g_t, info.numel_offset, stream); - } else { - ShareBufferForNonInitedTensor( - info.grad_t, fp32_g_t, info.numel_offset, info.param_t->dims()); - } - } - - size_t fp16_numel_offset = 0; - if (fp32_numel > 0) { - auto last_fp32_info = fp32_infos.back(); - fp16_numel_offset = - last_fp32_info.numel_offset + last_fp32_info.numel_with_padding; - } - - for (const auto &info : fp16_infos) { - auto master_weight_offset = info.numel_offset + fp16_numel_offset; - auto sliced_tensor = CastDataForInitedTensor( - dev_ctx, info.param_t, fp32_p_t, master_weight_offset); - master_params[info.idx]->Resize(info.param_t->dims()); - master_params[info.idx]->ShareBufferWith(sliced_tensor); - - CopyAndShareBufferForInitedTensor( - info.param_t, fp16_p_t, info.numel_offset, stream); - PADDLE_ENFORCE_EQ(master_params[info.idx]->mutable_data(place), - sliced_tensor.data(), - platform::errors::InvalidArgument( - "Invalid master weight tensor pointer.")); - - if (info.grad_t->IsInitialized()) { - CopyAndShareBufferForInitedTensor( - info.grad_t, fp16_g_t, info.numel_offset, stream); - } else { - ShareBufferForNonInitedTensor( - info.grad_t, fp16_g_t, info.numel_offset, info.param_t->dims()); - } - } - VLOG(10) << "Copy/share data for Param/Grad ends"; - - // Step 4: For Moment1, Moment2, Beta1Pow, Beta2Pow, just fill constant - TensorFillConstant(dev_ctx, - ctx.Output("Moment1"), - {static_cast(numel_each_device)}, - 0.0f); - TensorFillConstant(dev_ctx, - ctx.Output("Moment2"), - {static_cast(numel_each_device)}, - 0.0f); - TensorFillConstant(dev_ctx, - ctx.Output("Beta1Pow"), - {1}, - ctx.Attr("beta1")); - TensorFillConstant(dev_ctx, - ctx.Output("Beta2Pow"), - {1}, - ctx.Attr("beta2")); - VLOG(10) << "Init Moment and BetaPow ends"; - - // Step 5: Do sharding - size_t fp32_start_idx, fp32_end_idx, fp32_start_numel_offset, - fp32_end_numel_offset; - GetParamGradShardInfo(fp32_infos, - rank * fp32_numel_each_device, - (rank + 1) * fp32_numel_each_device, - &fp32_start_idx, - &fp32_end_idx, - &fp32_start_numel_offset, - &fp32_end_numel_offset); - size_t fp16_start_idx, fp16_end_idx, fp16_start_numel_offset, - fp16_end_numel_offset; - GetParamGradShardInfo(fp16_infos, - rank * fp16_numel_each_device, - (rank + 1) * fp16_numel_each_device, - &fp16_start_idx, - &fp16_end_idx, - &fp16_start_numel_offset, - &fp16_end_numel_offset); - size_t fp32_local_param_num = - fp32_numel_each_device > 0 ? fp32_end_idx - fp32_start_idx + 1 : 0; - size_t fp16_local_param_num = - fp16_numel_each_device > 0 ? fp16_end_idx - fp16_start_idx + 1 : 0; - size_t total_local_param_num = fp32_local_param_num + fp16_local_param_num; - VLOG(10) << "Found the sharding arguments"; - - auto *param_info_t = ctx.Output("ParamInfo"); - param_info_t->Resize({8}); - auto *param_info = param_info_t->mutable_data(platform::CPUPlace()); - param_info[0] = static_cast(fp32_start_idx); - param_info[1] = static_cast(fp32_local_param_num); - param_info[2] = static_cast(fp32_infos.size()); - param_info[3] = ClipByBound(fp32_wd_end_idx, - fp32_start_idx, - fp32_start_idx + fp32_local_param_num) - - static_cast(fp32_start_idx); - param_info[4] = static_cast(fp16_start_idx + fp32_infos.size()); - param_info[5] = static_cast(fp16_local_param_num); - param_info[6] = static_cast(fp16_infos.size()); - param_info[7] = ClipByBound(fp16_wd_end_idx, - fp16_start_idx, - fp16_start_idx + fp16_local_param_num) - - static_cast(fp16_start_idx); - - VLOG(10) << "Start FP32 idx: " << param_info[0]; - VLOG(10) << "Local FP32 param num: " << param_info[1]; - VLOG(10) << "Global FP32 param num: " << param_info[2]; - - VLOG(10) << "Start FP16 idx: " << param_info[4]; - VLOG(10) << "Local FP16 param num: " << param_info[5]; - VLOG(10) << "Global FP16 param num: " << param_info[6]; - - std::vector numel_offsets; - numel_offsets.reserve(params.size() + 1); - for (const auto &info : fp32_infos) { - numel_offsets.push_back(info.numel_offset); - } - for (const auto &info : fp16_infos) { - numel_offsets.push_back(info.numel_offset + fp16_numel_offset); - } - numel_offsets.push_back(fp32_numel + fp16_numel); - PADDLE_ENFORCE_EQ(numel_offsets.size(), - params.size() + 1, - platform::errors::InvalidArgument( - "The numel_offsets number must be one larger than " - "the parameter number.")); - VLOG(10) << "Total numel offset: " << FlattenToString(numel_offsets); - - std::vector fp32_partial_numel_offsets; - fp32_partial_numel_offsets.reserve(fp32_local_param_num + 1); - fp32_partial_numel_offsets.push_back(0); - // Fill the partial_numel_offsets - for (size_t i = fp32_start_idx; i < fp32_start_idx + fp32_local_param_num; - ++i) { - size_t valid_start_n = 0; - if (i == fp32_start_idx) { - valid_start_n = fp32_start_numel_offset; - } - - size_t end_n = fp32_infos[i].numel_with_padding; - if (i + 1 == fp32_start_idx + fp32_local_param_num) { - end_n = std::min(end_n, fp32_end_numel_offset); - } - - PADDLE_ENFORCE_NE(valid_start_n, - end_n, - platform::errors::InvalidArgument( - "Indices sharding error. This may be a bug.")); - VLOG(10) << "FP32 Partial numel = [" - << valid_start_n + fp32_infos[i].numel << "," - << end_n + fp32_infos[i].numel; - auto len = end_n - valid_start_n; - fp32_partial_numel_offsets.push_back(fp32_partial_numel_offsets.back() + - len); - } - - std::vector fp16_partial_numel_offsets; - fp16_partial_numel_offsets.reserve(fp16_local_param_num + 1); - fp16_partial_numel_offsets.push_back(0); - for (size_t i = fp16_start_idx; i < fp16_start_idx + fp16_local_param_num; - ++i) { - size_t valid_start_n = 0; - if (i == fp16_start_idx) { - valid_start_n = fp16_start_numel_offset; - } - - size_t end_n = fp16_infos[i].numel_with_padding; - if (i + 1 == fp16_start_idx + fp16_local_param_num) { - end_n = std::min(end_n, fp16_end_numel_offset); - } - - PADDLE_ENFORCE_NE(valid_start_n, - end_n, - platform::errors::InvalidArgument( - "Indices sharding error. This may be a bug.")); - auto len = end_n - valid_start_n; - fp16_partial_numel_offsets.push_back(fp16_partial_numel_offsets.back() + - len); - } - - CopyVectorToCPUTensor(numel_offsets, - ctx.Output("FusedParamOffsets")); - CopyVectorToCPUTensor( - fp32_partial_numel_offsets, - ctx.Output("FP32ShardFusedParamOffsets")); - CopyVectorToCPUTensor( - fp16_partial_numel_offsets, - ctx.Output("FP16ShardFusedParamOffsets")); - - auto *global_scale = ctx.Output("GlobalScale"); - if (!global_scale->IsInitialized()) { - TensorFillConstant(dev_ctx, global_scale, {1}, 1.0f); - } - VLOG(10) << "Init global scale ends"; - - TensorFillConstant(dev_ctx, - ctx.Output("Step"), - {1}, - static_cast(0)); - - dev_ctx.Wait(); - VLOG(10) << "Wait for H2D copy"; - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -PD_REGISTER_STRUCT_KERNEL(distributed_fused_lamb_init, - GPU, - ALL_LAYOUT, - ops::DistributedFusedLambInitOpKernel, - float) {} diff --git a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h b/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h deleted file mode 100644 index 7c314cd9e3790..0000000000000 --- a/paddle/fluid/operators/optimizers/distributed_fused_lamb_init_op.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#pragma once - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/operator.h" - -namespace paddle { -namespace operators { - -template -class DistributedFusedLambInitOpKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext &ctx) const override { - PADDLE_THROW(platform::errors::Unimplemented( - "The distributed_fused_lamb_init operator does not support CPU yet.")); - } -}; - -} // namespace operators -} // namespace paddle diff --git a/paddle/phi/kernels/distributed_fused_lamb_init_kernel.h b/paddle/phi/kernels/distributed_fused_lamb_init_kernel.h new file mode 100644 index 0000000000000..182c79ab80319 --- /dev/null +++ b/paddle/phi/kernels/distributed_fused_lamb_init_kernel.h @@ -0,0 +1,52 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/phi/common/scalar.h" +#include "paddle/phi/core/dense_tensor.h" + +namespace phi { + +template +void DistributedFusedLambInitOpKernel( + const Context& dev_ctx, + const std::vector& param, + const std::vector& grad, + float beta1, + float beta2, + const std::vector& apply_weight_decay, + int alignment, + int rank, + int nranks, + DenseTensor* fp32_fused_param, + DenseTensor* fp32_fused_grad, + DenseTensor* fp16_fused_param, + DenseTensor* fp16_fused_grad, + DenseTensor* moment1, + DenseTensor* moment2, + DenseTensor* beta1_pow, + DenseTensor* beta2_pow, + DenseTensor* fused_param_offsets, + DenseTensor* fp32_shard_fused_param_offsets, + DenseTensor* fp16_shard_fused_param_offsets, + DenseTensor* param_info, + DenseTensor* param_order, + std::vector param_out, + std::vector master_param_out, + std::vector grad_out, + DenseTensor* global_scale, + DenseTensor* step); + +} // namespace phi diff --git a/paddle/phi/kernels/fusion/cpu/distributed_fused_lamb_init_kernel.cc b/paddle/phi/kernels/fusion/cpu/distributed_fused_lamb_init_kernel.cc new file mode 100644 index 0000000000000..3cb37ccf2ed89 --- /dev/null +++ b/paddle/phi/kernels/fusion/cpu/distributed_fused_lamb_init_kernel.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/kernels/distributed_fused_lamb_init_kernel.h" +#include "paddle/phi/core/errors.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { +namespace fusion { + +template +void DistributedFusedLambInitOpKernel( + const Context& dev_ctx, + const std::vector& param, + const std::vector& grad, + float beta1, + float beta2, + const std::vector& apply_weight_decay, + int alignment, + int rank, + int nranks, + DenseTensor* fp32_fused_param, + DenseTensor* fp32_fused_grad, + DenseTensor* fp16_fused_param, + DenseTensor* fp16_fused_grad, + DenseTensor* moment1, + DenseTensor* moment2, + DenseTensor* beta1_pow, + DenseTensor* beta2_pow, + DenseTensor* fused_param_offsets, + DenseTensor* fp32_shard_fused_param_offsets, + DenseTensor* fp16_shard_fused_param_offsets, + DenseTensor* param_info, + DenseTensor* param_order, + std::vector param_out, + std::vector master_param_out, + std::vector grad_out, + DenseTensor* global_scale, + DenseTensor* step) { + PADDLE_THROW(phi::errors::Unavailable( + "Do not support expert count op for cpu kernel now.")); +} +} // namespace fusion +} // namespace phi + +PD_REGISTER_KERNEL(distributed_fused_lamb_init, + CPU, + ALL_LAYOUT, + phi::fusion::DistributedFusedLambInitOpKernel, + float) { + kernel->OutputAt(0).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(1).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT16); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT16); + kernel->OutputAt(4).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(5).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(6).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(7).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(8).SetDataType(phi::DataType::INT32); + kernel->OutputAt(9).SetDataType(phi::DataType::INT32); + kernel->OutputAt(10).SetDataType(phi::DataType::INT32); + kernel->OutputAt(11).SetDataType(phi::DataType::INT32); + kernel->OutputAt(12).SetDataType(phi::DataType::INT32); + kernel->OutputAt(13).SetDataType(kernel_key.dtype()); + kernel->OutputAt(14).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(15).SetDataType(kernel_key.dtype()); + kernel->OutputAt(16).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(17).SetDataType(phi::DataType::INT64); +} diff --git a/paddle/fluid/operators/optimizers/cast_with_ptr.h b/paddle/phi/kernels/fusion/gpu/cast_with_ptr.h similarity index 74% rename from paddle/fluid/operators/optimizers/cast_with_ptr.h rename to paddle/phi/kernels/fusion/gpu/cast_with_ptr.h index 205eb2853a341..5ae8aed256ccd 100644 --- a/paddle/fluid/operators/optimizers/cast_with_ptr.h +++ b/paddle/phi/kernels/fusion/gpu/cast_with_ptr.h @@ -14,28 +14,24 @@ #pragma once -#include "paddle/fluid/platform/device/gpu/gpu_launch_config.h" -#include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/api/include/tensor.h" +#include "paddle/phi/backends/gpu/gpu_launch_config.h" #include "paddle/phi/core/ddim.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/elementwise_base.h" -namespace paddle { -namespace operators { -namespace details { +namespace phi { template struct CastFunctor { HOSTDEVICE OutT operator()(InT x) const { return static_cast(x); } }; - template static void VecCastKernel(const phi::GPUContext &ctx, const InT *x, OutT *y, size_t n) { - auto config = platform::GetGpuLaunchConfig1D(ctx, n, VecSize); + auto config = phi::backends::gpu::GetGpuLaunchConfig1D(ctx, n, VecSize); auto block = config.GetGridSize(); auto thread = config.GetBlockSize(); auto main_offset = n / (VecSize * thread) * VecSize * thread; @@ -50,8 +46,6 @@ static void VecCastKernel(const phi::GPUContext &ctx, in_arr, out_arr, n, main_offset, VecSize, FunctorT()); } -} // namespace details - template static void LaunchCastKernel(const phi::GPUContext &ctx, const InT *x, @@ -61,20 +55,19 @@ static void LaunchCastKernel(const phi::GPUContext &ctx, PADDLE_ENFORCE_NE( static_cast(x), static_cast(y), - platform::errors::InvalidArgument("Inplace cast is not supported yet.")); + errors::InvalidArgument("Inplace cast is not supported yet.")); int vec_size = std::min(phi::GetVectorizedSize(x), phi::GetVectorizedSize(y)); switch (vec_size) { case 4: - return details::VecCastKernel(ctx, x, y, n); + return VecCastKernel(ctx, x, y, n); case 2: - return details::VecCastKernel(ctx, x, y, n); + return VecCastKernel(ctx, x, y, n); case 1: - return details::VecCastKernel(ctx, x, y, n); + return VecCastKernel(ctx, x, y, n); default: - PADDLE_THROW(platform::errors::InvalidArgument( - "The vectorized size must be 1, 2 or 4.")); + PADDLE_THROW( + errors::InvalidArgument("The vectorized size must be 1, 2 or 4.")); } } -} // namespace operators -} // namespace paddle +} // namespace phi diff --git a/paddle/phi/kernels/fusion/gpu/distributed_fused_lamb_init_kernel.cu b/paddle/phi/kernels/fusion/gpu/distributed_fused_lamb_init_kernel.cu new file mode 100644 index 0000000000000..3ae7f0682bc75 --- /dev/null +++ b/paddle/phi/kernels/fusion/gpu/distributed_fused_lamb_init_kernel.cu @@ -0,0 +1,804 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/kernels/distributed_fused_lamb_init_kernel.h" +#include "paddle/phi/common/data_type.h" +#include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/algorithm.h" +#include "paddle/phi/kernels/funcs/math_function.h" +#include "paddle/phi/kernels/funcs/tensor_to_string.h" +#include "paddle/phi/kernels/fusion/gpu/cast_with_ptr.h" + +namespace phi { +namespace fusion { + +using phi::funcs::FlattenToString; +using phi::funcs::ToVector; + +struct ParamGradInfo { + DenseTensor *param_t{nullptr}; + DenseTensor *grad_t{nullptr}; + size_t idx{0}; + size_t numel{0}; + size_t numel_with_padding{0}; + size_t numel_offset{0}; +}; + +static std::ostream &operator<<(std::ostream &os, const ParamGradInfo &info) { + return os << "{Param(" << info.param_t << "),Grad(" << info.grad_t << "),idx(" + << info.idx << "),numel(" << info.numel << "),numel_with_padding(" + << info.numel_with_padding << "),numel_offset(" << info.numel_offset + << "),padding(" << info.numel_offset + info.numel_with_padding + << "-" << info.numel_offset + info.numel << "=" + << info.numel_with_padding - info.numel << ")}"; +} + +struct ParamGradInfoNumelOffsetCompFunctor { + bool operator()(const ParamGradInfo &x, const ParamGradInfo &y) const { + return x.numel_offset < y.numel_offset; + } + + bool operator()(const ParamGradInfo &x, size_t y) const { + return x.numel_offset < y; + } + + bool operator()(size_t x, const ParamGradInfo &y) const { + return x < y.numel_offset; + } + + bool operator()(size_t x, size_t y) const { return x < y; } +}; + +static size_t GetAlignSize(size_t n, size_t alignment) { + auto remainder = n % alignment; + return remainder == 0 ? n : n + alignment - remainder; +} + +// Shard the ParamGradInfo list by the numel size [start_size, end_size) +// The final results should be: +// +// start_size = sum(infos[0:i].numel_with_padding) + start_numel_offset, where +// start_numel_offset <= infos[i].numel_with_padding +// +// end_size = sum(infos[0:j].numel_with_padding) + end_numel_offset, where +// end_numel_offset <= infos[j].numel_with_padding +static void GetParamGradShardInfo(const std::vector &infos, + size_t start_size, + size_t end_size, + size_t *start_idx, + size_t *end_idx, + size_t *start_numel_offset, + size_t *end_numel_offset) { + VLOG(10) << "NumelOffset: " + << paddle::string::join_strings( + infos, ",", [](const ParamGradInfo &info) { + return info.numel_offset; + }); + VLOG(10) << "start_size = " << start_size << " , end_size = " << end_size; + + if (infos.empty()) { + PADDLE_ENFORCE_EQ( + start_size, 0, errors::InvalidArgument("start_size should be 0.")); + PADDLE_ENFORCE_EQ( + end_size, 0, errors::InvalidArgument("end_size should be 0.")); + *start_idx = 0; + *end_idx = 0; + *start_numel_offset = 0; + *end_numel_offset = 0; + return; + } + + PADDLE_ENFORCE_LT( + start_size, + end_size, + errors::InvalidArgument("start_size should be less than end_size.")); + size_t n = infos.size(); + ParamGradInfoNumelOffsetCompFunctor comp; + auto i = static_cast( + std::lower_bound(infos.begin(), infos.end(), start_size, comp) - + infos.begin()); + if (i == n || infos[i].numel_offset != start_size) { + PADDLE_ENFORCE_GT( + i, + 0, + errors::InvalidArgument( + "Cannot find suitable sharding which is between [%d, %d)", + start_size, + end_size)); + --i; + } + PADDLE_ENFORCE_LT( + i, + n, + errors::InvalidArgument( + "Cannot find suitable sharding which is between [%d, %d)", + start_size, + end_size)); + *start_idx = i; + *start_numel_offset = start_size - infos[i].numel_offset; + auto j = static_cast( + std::lower_bound(infos.begin(), infos.end(), end_size, comp) - + infos.begin()); + *end_idx = j - 1; + *end_numel_offset = end_size - infos[j - 1].numel_offset; + PADDLE_ENFORCE_GT( + *end_numel_offset, + 0, + errors::InvalidArgument("Internal error when sharding, this may be a bug " + "caused by empty parameter.")); + VLOG(10) << "Sharding [start_size=" << start_size << ", end_size=" << end_size + << "): " << (*start_idx) << ":" << (*start_numel_offset) << " -> " + << (*end_idx) << ":" << (*end_numel_offset); +} + +static size_t FillAlignmentPaddingInfo(std::vector *infos, + size_t alignment, + size_t nranks, + phi::DataType dtype) { + auto sizeof_dtype = phi::SizeOf(dtype); + PADDLE_ENFORCE_EQ( + alignment % sizeof_dtype, + 0, + errors::InvalidArgument( + "The attr(alignment) should be exactly divided by sizeof(T) %d.", + sizeof_dtype)); + alignment /= sizeof_dtype; + + size_t total_numel_sum_with_padding = 0; + size_t n = infos->size(); + for (size_t i = 0; i < n; ++i) { + auto &info = (*infos)[i]; + size_t numel_with_padding; + if (i + 1 == n) { + // the total fused numel must be a factor of alignment * nranks + numel_with_padding = + GetAlignSize(info.numel + total_numel_sum_with_padding, + alignment * nranks) - + total_numel_sum_with_padding; + } else { + numel_with_padding = GetAlignSize(info.numel, alignment); + } + info.numel_with_padding = numel_with_padding; + info.numel_offset = total_numel_sum_with_padding; + total_numel_sum_with_padding += numel_with_padding; + } + return total_numel_sum_with_padding; +} + +template +static T *TensorFillConstant(const phi::GPUContext &dev_ctx, + DenseTensor *tensor, + const DDim &dims, + T value) { + tensor->Resize(dims); + auto *ptr = dev_ctx.template Alloc(tensor); + phi::funcs::SetConstant set_constant; + set_constant(dev_ctx, tensor, value); + return ptr; +} + +static DenseTensor CastDataForInitedTensor(const phi::GPUContext &dev_ctx, + DenseTensor *origin, + DenseTensor *fused_out, + size_t numel_offset) { + PADDLE_ENFORCE_EQ( + origin->IsInitialized(), + true, + errors::InvalidArgument("The tensor to be cast should be initialized.")); + + PADDLE_ENFORCE_EQ(fused_out->dtype(), + phi::DataType::FLOAT32, + errors::InvalidArgument( + "The dst tensor to be cast should be FP32 tensor.")); + PADDLE_ENFORCE_EQ(origin->dtype(), + phi::DataType::FLOAT16, + errors::InvalidArgument( + "The src tensor to be cast should be FP16 tensor.")); + auto *dst = fused_out->data() + numel_offset; + auto *src = origin->data(); + auto numel = origin->numel(); + LaunchCastKernel(dev_ctx, src, dst, numel); + VLOG(10) << "Cast from FP32 -> FP16, range: [" << numel_offset << ", " + << numel_offset + numel << ")" + << " , total: [0, " << fused_out->numel() << ")"; + DDim fused_out_dim = fused_out->dims(); + auto fused_out_numel = fused_out->numel(); + fused_out->Resize({fused_out_numel}); + auto sliced_tensor = fused_out->Slice(numel_offset, numel + numel_offset); + fused_out->Resize(fused_out_dim); + return sliced_tensor; +} + +static DenseTensor CopyAndShareBufferForInitedTensor( + const phi::GPUContext &dev_ctx, + DenseTensor *origin, + DenseTensor *fused_out, + size_t numel_offset) { + PADDLE_ENFORCE_EQ( + origin->IsInitialized(), + true, + errors::InvalidArgument( + "The tensor to be copied and shared data should be initialized.")); + auto dtype = fused_out->type(); + PADDLE_ENFORCE_EQ(origin->type(), + dtype, + errors::InvalidArgument( + "The tensor to be copied and shared data should be " + "have the same data type.")); + auto place = fused_out->place(); + PADDLE_ENFORCE_EQ( + origin->place(), + place, + errors::InvalidArgument("The tensor to be copied and shared " + "data should be have the same place.")); + PADDLE_ENFORCE_EQ( + dev_ctx.GetPlace().GetType() == phi::AllocationType::GPU, + true, + errors::InvalidArgument( + "The tensor to be copied and shared data should be on GPU place.")); + + auto numel = origin->numel(); + DDim fused_out_dim = fused_out->dims(); + auto fused_out_numel = fused_out->numel(); + auto sliced_tensor = fused_out->Resize({fused_out_numel}) + .Slice(numel_offset, numel + numel_offset); + phi::Copy(dev_ctx, *origin, dev_ctx.GetPlace(), false, &sliced_tensor); + origin->ShareBufferWith(sliced_tensor); + fused_out->Resize(fused_out_dim); + VLOG(10) << "Copy and share buffer, range: [" << numel_offset << ", " + << numel_offset + numel << ") , total: [0, " << fused_out->numel() + << ") , dtype = " << dtype; + return sliced_tensor; +} + +static void ShareBufferForNonInitedTensor(DenseTensor *origin, + DenseTensor *fused_out, + size_t numel_offset, + const DDim &dims) { + PADDLE_ENFORCE_EQ( + origin->IsInitialized(), + false, + errors::InvalidArgument( + "The tensor to be shared data should not be initialized.")); + + DDim fused_out_dim = fused_out->dims(); + auto fused_out_numel = fused_out->numel(); + auto numel = phi::product(dims); + *origin = fused_out->Resize({fused_out_numel}) + .Slice(numel_offset, numel + numel_offset); + origin->Resize(dims); + fused_out->Resize(fused_out_dim); + VLOG(10) << "Share buffer for non-inited, range: [" << numel_offset << ", " + << numel_offset + numel << "), total: [0, " << fused_out->numel() + << ") , dtype = " << fused_out->dtype(); +} + +template +static void CopyVectorToCPUTensor(const phi::GPUContext &dev_ctx, + const std::vector &src, + DenseTensor *dst) { + dst->Resize({static_cast(src.size())}); + T *dst_ptr = dev_ctx.template HostAlloc(dst); + const T *src_ptr = src.data(); + auto nbytes = src.size() * sizeof(T); + std::memcpy(dst_ptr, src_ptr, nbytes); +} + +static size_t ReorderParamGradInfoList(const std::vector &flags, + std::vector *infos) { + size_t n = infos->size(); + std::vector cur_flags; + cur_flags.reserve(n); + for (size_t i = 0; i < n; ++i) { + auto idx = (*infos)[i].idx; + cur_flags.push_back(flags[idx]); + } + + auto origin_infos = *infos; + size_t j = 0; + for (size_t i = 0; i < n; ++i) { + if (cur_flags[i]) { + (*infos)[j] = origin_infos[i]; + ++j; + } + } + size_t ret_idx = j; + + for (size_t i = 0; i < n; ++i) { + if (!cur_flags[i]) { + (*infos)[j] = origin_infos[i]; + ++j; + } + } + return ret_idx; +} + +template +static T ClipByBound(T x, T low_value, T high_value) { + if (x < low_value) return low_value; + if (x > high_value) return high_value; + return x; +} + +template +void DistributedFusedLambInitOpKernel( + const Context &dev_ctx, + const std::vector ¶m, + const std::vector &grad, + float beta1, + float beta2, + const std::vector &apply_weight_decay, + int alignment, + int rank, + int nranks, + DenseTensor *fp32_fused_param, + DenseTensor *fp32_fused_grad, + DenseTensor *fp16_fused_param, + DenseTensor *fp16_fused_grad, + DenseTensor *moment1, + DenseTensor *moment2, + DenseTensor *beta1_pow, + DenseTensor *beta2_pow, + DenseTensor *fused_param_offsets, + DenseTensor *fp32_shard_fused_param_offsets, + DenseTensor *fp16_shard_fused_param_offsets, + DenseTensor *param_info, + DenseTensor *param_order, + std::vector param_out, + std::vector master_param_out, + std::vector grad_out, + DenseTensor *global_scale, + DenseTensor *step) { + VLOG(10) << "starts to run DistributedFusedLambInitOp"; + auto place = dev_ctx.GetPlace(); + auto stream = dev_ctx.stream(); + + // Step 1: Check Input(Param) and Output(ParamOut), Input(Grad) and + // Output(GradOut) + std::vector fp32_infos, fp16_infos; + { + PADDLE_ENFORCE_EQ( + param.size(), + grad.size(), + errors::InvalidArgument("The parameter number and parameter gradient " + "number should be the same.")); + + PADDLE_ENFORCE_EQ( + param.size(), + param_out.size(), + errors::InvalidArgument("Input(Param) and Output(ParamOut) " + "should have the same number.")); + PADDLE_ENFORCE_EQ( + grad.size(), + grad_out.size(), + errors::InvalidArgument( + "Input(Grad) and Output(GradOut) should have the same number.")); + size_t n = param.size(); + VLOG(10) << "parameter number: " << n; + for (size_t i = 0; i < n; ++i) { + auto *p = param[i]; + auto *g = grad[i]; + auto *p_out = param_out[i]; + auto *g_out = grad_out[i]; + + PADDLE_ENFORCE_NOT_NULL( + p, + errors::InvalidArgument("The %d-th parameter should not be nullptr.", + i)); + PADDLE_ENFORCE_EQ(p->IsInitialized(), + true, + errors::InvalidArgument( + "The %d-th parameter should be initialized.", i)); + PADDLE_ENFORCE_EQ( + p->place(), + place, + errors::InvalidArgument( + "The %d-th parameter is not initialized on the right place.", i)); + PADDLE_ENFORCE_EQ( + p, + p_out, + errors::InvalidArgument("The %d-th Input(Param) and Output(ParamOut) " + "should be the same tensor.", + i)); + + auto dtype = p->dtype(); + PADDLE_ENFORCE_NOT_NULL( + g, + errors::InvalidArgument("The %d-th gradient should not be nullptr.", + i)); + PADDLE_ENFORCE_EQ(g, + g_out, + errors::InvalidArgument( + "The %d-th Input(Grad) and Output(Grad) should " + "be the same tensor.")); + auto numel = p->numel(); + PADDLE_ENFORCE_GT( + numel, + 0, + errors::InvalidArgument("The %d-th Input(Param) have no elements.")); + + void *g_data = nullptr; + if (g->IsInitialized()) { + PADDLE_ENFORCE_EQ(g->dtype(), + dtype, + errors::InvalidArgument( + "The %d-th Input(Param) and Input(Grad) should " + "have the same data type %s.", + i, + dtype)); + PADDLE_ENFORCE_EQ(g->dims(), + p->dims(), + errors::InvalidArgument( + "The %d-th Input(Param) and Input(Grad) should " + "have the same shape.", + i)); + g_data = g_out->data(); + } + + ParamGradInfo *info; + if (dtype == phi::DataType::FLOAT32) { + fp32_infos.emplace_back(); + info = &fp32_infos.back(); + } else if (dtype == phi::DataType::FLOAT16) { + fp16_infos.emplace_back(); + info = &fp16_infos.back(); + } else { + PADDLE_THROW( + errors::InvalidArgument("Unsupported data type %s.", dtype)); + } + + VLOG(10) << "Found " << dtype << " parameter " << i << " shape=[" + << p_out->dims() << "] numel=" << numel + << " grad.IsInitialized()=" + << (g_out->IsInitialized() ? "true" : "false"); + + info->param_t = p_out; + info->grad_t = g_out; + info->idx = i; + info->numel = numel; + info->numel_with_padding = 0; // not determined yet + info->numel_offset = 0; // not determined yet + } + } + + size_t fp32_wd_end_idx = + ReorderParamGradInfoList(apply_weight_decay, &fp32_infos); + size_t fp16_wd_end_idx = + ReorderParamGradInfoList(apply_weight_decay, &fp16_infos); + + auto param_num = fp32_infos.size() + fp16_infos.size(); + param_order->Resize({static_cast(param_num)}); + auto *param_order_t = dev_ctx.template HostAlloc(param_order); + for (size_t i = 0; i < fp32_infos.size(); ++i) { + param_order_t[i] = static_cast(fp32_infos[i].idx); + } + for (size_t i = 0; i < fp16_infos.size(); ++i) { + param_order_t[i + fp32_infos.size()] = static_cast(fp16_infos[i].idx); + } + + VLOG(10) << "Fill ParamGradInfo ends"; + + // Step 2: determine the numel_with_padding and numel_offset + VLOG(10) << "rank = " << rank << ", nranks = " << nranks + << " , alignment = " << alignment; + if (alignment <= 0) { + alignment = phi::backends::gpu::GpuMinChunkSize(); + } + PADDLE_ENFORCE_GE( + alignment, + 1, + errors::InvalidArgument("The attr(alignment) should be larger than 0.")); + PADDLE_ENFORCE_EQ( + alignment & (alignment - 1), + 0, + errors::InvalidArgument("The attr(alignment) should be the power of 2.")); + PADDLE_ENFORCE_GE(rank, + 0, + errors::InvalidArgument( + "The attr(rank) should be equal to or larger than 0.")); + PADDLE_ENFORCE_LT( + rank, + nranks, + errors::InvalidArgument( + "The attr(rank) should be less than the attr(nranks).")); + // NOTE: We guarantee that both fp32_numel and fp16_numel can be exactly + // divided by alignment and nranks. + auto fp32_numel = FillAlignmentPaddingInfo( + &fp32_infos, alignment, nranks, phi::DataType::FLOAT32); + VLOG(10) << "FP32 ParamGradInfo: " + << paddle::string::join_strings(fp32_infos, " "); + auto fp16_numel = FillAlignmentPaddingInfo( + &fp16_infos, alignment, nranks, phi::DataType::FLOAT16); + VLOG(10) << "FP16 ParamGradInfo: " + << paddle::string::join_strings(fp16_infos, " "); + auto total_numel = fp32_numel + fp16_numel; + PADDLE_ENFORCE_LT(total_numel, + std::numeric_limits::max(), + errors::InvalidArgument("Too many parameter number.")); + + auto fp32_numel_each_device = fp32_numel / nranks; + auto fp16_numel_each_device = fp16_numel / nranks; + auto numel_each_device = fp32_numel_each_device + fp16_numel_each_device; + VLOG(10) << "Fill padding ends. total_numel = " << total_numel + << ", fp32_numel = " << fp32_numel << ", fp16_numel = " << fp16_numel + << ", fp32_numel_each_device = " << fp32_numel_each_device + << ", fp16_numel_each_device = " << fp16_numel_each_device; + + // Step 3: allocate output tensor and do initialization + float *fused_fp32_param = nullptr, *fused_fp32_grad = nullptr; + dtype::float16 *fused_fp16_param = nullptr, *fused_fp16_grad = nullptr; + DenseTensor *fp32_p_t = nullptr, *fp16_p_t = nullptr, *fp32_g_t = nullptr, + *fp16_g_t = nullptr; + std::vector fp16_master_params; + if (total_numel > 0) { + fp32_p_t = fp32_fused_param; + fused_fp32_param = TensorFillConstant( + dev_ctx, fp32_p_t, {static_cast(total_numel)}, 0.0f); + } + + if (fp32_numel > 0) { + fp32_g_t = fp32_fused_grad; + fused_fp32_grad = TensorFillConstant( + dev_ctx, fp32_g_t, {static_cast(fp32_numel)}, 0.0f); + } + + if (fp16_numel > 0) { + fp16_p_t = fp16_fused_param; + fused_fp16_param = + TensorFillConstant(dev_ctx, + fp16_p_t, + {static_cast(fp16_numel)}, + static_cast(0)); + + fp16_g_t = fp16_fused_grad; + fused_fp16_grad = + TensorFillConstant(dev_ctx, + fp16_g_t, + {static_cast(fp16_numel)}, + static_cast(0)); + } + VLOG(10) << "Allocate FP32FusedParam/Grad, FP16FusedParam/Grad ends"; + + // (1) For FP32FusedParam, memcpy for fp32 param and then share data, cast + // for fp16 master weight + // (2) For FP16FusedParam, memcpy and then share data + // (3) For FP32FusedGrad/FP16FusedGrad, memcpy if gradient has been inited + for (const auto &info : fp32_infos) { + auto sliced_tensor = CopyAndShareBufferForInitedTensor( + dev_ctx, info.param_t, fp32_p_t, info.numel_offset); + master_param_out[info.idx]->Resize(info.param_t->dims()); + master_param_out[info.idx]->ShareBufferWith(sliced_tensor); + float *master_param_tmp = + dev_ctx.template Alloc(master_param_out[info.idx]); + float *sliced_tensor_tmp = reinterpret_cast(sliced_tensor.data()); + PADDLE_ENFORCE_EQ( + master_param_tmp, + sliced_tensor_tmp, + errors::InvalidArgument("Invalid master weight tensor pointer.")); + + if (info.grad_t->IsInitialized()) { + CopyAndShareBufferForInitedTensor( + dev_ctx, info.grad_t, fp32_g_t, info.numel_offset); + } else { + ShareBufferForNonInitedTensor( + info.grad_t, fp32_g_t, info.numel_offset, info.param_t->dims()); + } + } + + size_t fp16_numel_offset = 0; + if (fp32_numel > 0) { + auto last_fp32_info = fp32_infos.back(); + fp16_numel_offset = + last_fp32_info.numel_offset + last_fp32_info.numel_with_padding; + } + + for (const auto &info : fp16_infos) { + auto master_weight_offset = info.numel_offset + fp16_numel_offset; + auto sliced_tensor = CastDataForInitedTensor( + dev_ctx, info.param_t, fp32_p_t, master_weight_offset); + master_param_out[info.idx]->Resize(info.param_t->dims()); + master_param_out[info.idx]->ShareBufferWith(sliced_tensor); + + CopyAndShareBufferForInitedTensor( + dev_ctx, info.param_t, fp16_p_t, info.numel_offset); + float *master_param_tmp = + dev_ctx.template Alloc(master_param_out[info.idx]); + float *sliced_tensor_tmp = reinterpret_cast(sliced_tensor.data()); + PADDLE_ENFORCE_EQ( + master_param_tmp, + sliced_tensor_tmp, + errors::InvalidArgument("Invalid master weight tensor pointer.")); + + if (info.grad_t->IsInitialized()) { + CopyAndShareBufferForInitedTensor( + dev_ctx, info.grad_t, fp16_g_t, info.numel_offset); + } else { + ShareBufferForNonInitedTensor( + info.grad_t, fp16_g_t, info.numel_offset, info.param_t->dims()); + } + } + VLOG(10) << "Copy/share data for Param/Grad ends"; + + // Step 4: For Moment1, Moment2, Beta1Pow, Beta2Pow, just fill constant + TensorFillConstant( + dev_ctx, moment1, {static_cast(numel_each_device)}, 0.0f); + TensorFillConstant( + dev_ctx, moment2, {static_cast(numel_each_device)}, 0.0f); + TensorFillConstant(dev_ctx, beta1_pow, {1}, beta1); + TensorFillConstant(dev_ctx, beta2_pow, {1}, beta2); + VLOG(10) << "Init Moment and BetaPow ends"; + + // Step 5: Do sharding + size_t fp32_start_idx, fp32_end_idx, fp32_start_numel_offset, + fp32_end_numel_offset; + GetParamGradShardInfo(fp32_infos, + rank * fp32_numel_each_device, + (rank + 1) * fp32_numel_each_device, + &fp32_start_idx, + &fp32_end_idx, + &fp32_start_numel_offset, + &fp32_end_numel_offset); + size_t fp16_start_idx, fp16_end_idx, fp16_start_numel_offset, + fp16_end_numel_offset; + GetParamGradShardInfo(fp16_infos, + rank * fp16_numel_each_device, + (rank + 1) * fp16_numel_each_device, + &fp16_start_idx, + &fp16_end_idx, + &fp16_start_numel_offset, + &fp16_end_numel_offset); + size_t fp32_local_param_num = + fp32_numel_each_device > 0 ? fp32_end_idx - fp32_start_idx + 1 : 0; + size_t fp16_local_param_num = + fp16_numel_each_device > 0 ? fp16_end_idx - fp16_start_idx + 1 : 0; + size_t total_local_param_num = fp32_local_param_num + fp16_local_param_num; + VLOG(10) << "Found the sharding arguments"; + + param_info->Resize({8}); + auto *param_info_t = dev_ctx.template HostAlloc(param_info); + param_info_t[0] = static_cast(fp32_start_idx); + param_info_t[1] = static_cast(fp32_local_param_num); + param_info_t[2] = static_cast(fp32_infos.size()); + param_info_t[3] = ClipByBound(fp32_wd_end_idx, + fp32_start_idx, + fp32_start_idx + fp32_local_param_num) - + static_cast(fp32_start_idx); + param_info_t[4] = static_cast(fp16_start_idx + fp32_infos.size()); + param_info_t[5] = static_cast(fp16_local_param_num); + param_info_t[6] = static_cast(fp16_infos.size()); + param_info_t[7] = ClipByBound(fp16_wd_end_idx, + fp16_start_idx, + fp16_start_idx + fp16_local_param_num) - + static_cast(fp16_start_idx); + + VLOG(10) << "Start FP32 idx: " << param_info_t[0]; + VLOG(10) << "Local FP32 param num: " << param_info_t[1]; + VLOG(10) << "Global FP32 param num: " << param_info_t[2]; + + VLOG(10) << "Start FP16 idx: " << param_info_t[4]; + VLOG(10) << "Local FP16 param num: " << param_info_t[5]; + VLOG(10) << "Global FP16 param num: " << param_info_t[6]; + + std::vector numel_offsets; + numel_offsets.reserve(param.size() + 1); + for (const auto &info : fp32_infos) { + numel_offsets.push_back(info.numel_offset); + } + for (const auto &info : fp16_infos) { + numel_offsets.push_back(info.numel_offset + fp16_numel_offset); + } + numel_offsets.push_back(fp32_numel + fp16_numel); + PADDLE_ENFORCE_EQ(numel_offsets.size(), + param.size() + 1, + errors::InvalidArgument( + "The numel_offsets number must be one larger than " + "the parameter number.")); + VLOG(10) << "Total numel offset: " << FlattenToString(numel_offsets); + + std::vector fp32_partial_numel_offsets; + fp32_partial_numel_offsets.reserve(fp32_local_param_num + 1); + fp32_partial_numel_offsets.push_back(0); + // Fill the partial_numel_offsets + for (size_t i = fp32_start_idx; i < fp32_start_idx + fp32_local_param_num; + ++i) { + size_t valid_start_n = 0; + if (i == fp32_start_idx) { + valid_start_n = fp32_start_numel_offset; + } + + size_t end_n = fp32_infos[i].numel_with_padding; + if (i + 1 == fp32_start_idx + fp32_local_param_num) { + end_n = std::min(end_n, fp32_end_numel_offset); + } + + PADDLE_ENFORCE_NE( + valid_start_n, + end_n, + errors::InvalidArgument("Indices sharding error. This may be a bug.")); + VLOG(10) << "FP32 Partial numel = [" << valid_start_n + fp32_infos[i].numel + << "," << end_n + fp32_infos[i].numel; + auto len = end_n - valid_start_n; + fp32_partial_numel_offsets.push_back(fp32_partial_numel_offsets.back() + + len); + } + + std::vector fp16_partial_numel_offsets; + fp16_partial_numel_offsets.reserve(fp16_local_param_num + 1); + fp16_partial_numel_offsets.push_back(0); + for (size_t i = fp16_start_idx; i < fp16_start_idx + fp16_local_param_num; + ++i) { + size_t valid_start_n = 0; + if (i == fp16_start_idx) { + valid_start_n = fp16_start_numel_offset; + } + + size_t end_n = fp16_infos[i].numel_with_padding; + if (i + 1 == fp16_start_idx + fp16_local_param_num) { + end_n = std::min(end_n, fp16_end_numel_offset); + } + + PADDLE_ENFORCE_NE( + valid_start_n, + end_n, + errors::InvalidArgument("Indices sharding error. This may be a bug.")); + auto len = end_n - valid_start_n; + fp16_partial_numel_offsets.push_back(fp16_partial_numel_offsets.back() + + len); + } + + CopyVectorToCPUTensor(dev_ctx, numel_offsets, fused_param_offsets); + CopyVectorToCPUTensor( + dev_ctx, fp32_partial_numel_offsets, fp32_shard_fused_param_offsets); + CopyVectorToCPUTensor( + dev_ctx, fp16_partial_numel_offsets, fp16_shard_fused_param_offsets); + + if (!global_scale->IsInitialized()) { + TensorFillConstant(dev_ctx, global_scale, {1}, 1.0f); + } + VLOG(10) << "Init global scale ends"; + + TensorFillConstant(dev_ctx, step, {1}, static_cast(0)); + + dev_ctx.Wait(); + VLOG(10) << "Wait for H2D copy"; +} + +} // namespace fusion +} // namespace phi + +PD_REGISTER_KERNEL(distributed_fused_lamb_init, + GPU, + ALL_LAYOUT, + phi::fusion::DistributedFusedLambInitOpKernel, + float) { + kernel->OutputAt(0).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(1).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT16); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT16); + kernel->OutputAt(4).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(5).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(6).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(7).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(8).SetDataType(phi::DataType::INT32); + kernel->OutputAt(9).SetDataType(phi::DataType::INT32); + kernel->OutputAt(10).SetDataType(phi::DataType::INT32); + kernel->OutputAt(11).SetDataType(phi::DataType::INT32); + kernel->OutputAt(12).SetDataType(phi::DataType::INT32); + kernel->OutputAt(13).SetDataType(kernel_key.dtype()); + kernel->OutputAt(14).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(15).SetDataType(kernel_key.dtype()); + kernel->OutputAt(16).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(17).SetDataType(phi::DataType::INT64); +} diff --git a/paddle/phi/ops/compat/distributed_fused_lamb_init_sig.cc b/paddle/phi/ops/compat/distributed_fused_lamb_init_sig.cc new file mode 100644 index 0000000000000..90c64a1d2ef7c --- /dev/null +++ b/paddle/phi/ops/compat/distributed_fused_lamb_init_sig.cc @@ -0,0 +1,48 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/core/compat/op_utils.h" + +namespace phi { + +KernelSignature DistributedFusedLambInitOpArgumentMapping( + const ArgumentMappingContext& ctx UNUSED) { + return KernelSignature( + "distributed_fused_lamb_init", + {"Param", "Grad"}, + {"beta1", "beta2", "apply_weight_decay", "alignment", "rank", "nranks"}, + {"FP32FusedParam", + "FP32FusedGrad", + "FP16FusedParam", + "FP16FusedGrad", + "Moment1", + "Moment2", + "Beta1Pow", + "Beta2Pow", + "FusedParamOffsets", + "FP32ShardFusedParamOffsets", + "FP16ShardFusedParamOffsets", + "ParamInfo", + "ParamOrder", + "ParamOut", + "MasterParamOut", + "GradOut", + "GlobalScale", + "Step"}); +} + +} // namespace phi + +PD_REGISTER_ARG_MAPPING_FN(distributed_fused_lamb_init, + phi::DistributedFusedLambInitOpArgumentMapping); From 5425ad7fafb45db927557c6a999985db8d71859d Mon Sep 17 00:00:00 2001 From: LiYuRio <63526175+LiYuRio@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:30:15 +0800 Subject: [PATCH 036/194] use macro instead of functor (#56726) --- paddle/fluid/pybind/communication.cc | 2 +- .../distributed/auto_parallel/CMakeLists.txt | 3 - .../auto_parallel/r_to_s_reshard_function.cc | 22 +-- .../reshard_all_gather_functor.cc | 73 ---------- .../reshard_all_gather_functor.h | 31 ----- .../auto_parallel/reshard_concat_functor.cc | 55 -------- .../auto_parallel/reshard_function.cc | 16 ++- .../auto_parallel/reshard_function.h | 9 +- .../auto_parallel/reshard_split_functor.cc | 59 --------- .../auto_parallel/reshard_split_functor.h | 32 ----- .../auto_parallel/reshard_utils.cc | 125 ++++-------------- .../distributed/auto_parallel/reshard_utils.h | 74 ++++++++--- .../auto_parallel/s_to_r_reshard_function.cc | 44 +++--- .../core/distributed/auto_parallel/utils.h | 1 + .../phi/core/distributed/store/CMakeLists.txt | 3 +- .../phi/core/distributed/store/store_utils.cc | 90 +++++++++++++ .../store_utils.h} | 18 ++- 17 files changed, 249 insertions(+), 408 deletions(-) delete mode 100644 paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.cc delete mode 100644 paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h delete mode 100644 paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.cc delete mode 100644 paddle/phi/core/distributed/auto_parallel/reshard_split_functor.cc delete mode 100644 paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h create mode 100644 paddle/phi/core/distributed/store/store_utils.cc rename paddle/phi/core/distributed/{auto_parallel/reshard_concat_functor.h => store/store_utils.h} (75%) diff --git a/paddle/fluid/pybind/communication.cc b/paddle/fluid/pybind/communication.cc index 59ad5779111f1..82408b5236936 100644 --- a/paddle/fluid/pybind/communication.cc +++ b/paddle/fluid/pybind/communication.cc @@ -28,8 +28,8 @@ limitations under the License. */ #include #include -#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" #include "paddle/phi/core/distributed/comm_context_manager.h" +#include "paddle/phi/core/distributed/store/store_utils.h" #include "paddle/phi/core/distributed/store/tcp_store.h" namespace py = pybind11; diff --git a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt index 63fbe9ecd677c..d9d1c27ed23f3 100644 --- a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt +++ b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt @@ -12,8 +12,5 @@ collect_srcs( dist_meta_tensor.cc inferspmd_utils.cc reshard_function.cc - reshard_split_functor.cc - reshard_concat_functor.cc - reshard_all_gather_functor.cc r_to_s_reshard_function.cc s_to_r_reshard_function.cc) diff --git a/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc index 3a60e22679362..c0c59e5d8c395 100644 --- a/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc @@ -15,12 +15,10 @@ #include "paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.h" #include "glog/logging.h" -#include "paddle/phi/api/lib/kernel_dispatch.h" #include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" -#include "paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h" #include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" -#include "paddle/phi/core/kernel_factory.h" +#include "paddle/phi/kernels/split_kernel.h" namespace phi { namespace distributed { @@ -73,17 +71,21 @@ void RToSReshardFunction::Eval(phi::DeviceContext* dev_ctx, BalancedSplit(in.dims()[split_axis], num_of_process); IntArray sections(split_num_vec); - std::vector split_out_vec = ReshardSplitFunctor( - *dev_ctx, in_physical_tensor_cur_rank, sections, split_axis); + std::vector split_out_vec; + auto dtype = in_physical_tensor_cur_rank.dtype(); + RESHARD_FUNCTOR(dev_ctx, + Split, + dtype, + in_physical_tensor_cur_rank, + sections, + split_axis, + &split_out_vec); VLOG(3) << "The current process will remain the idx " << coord_in_mesh[mesh_axis] << " piece of tensor"; - out_physical_tensor_cur_rank = split_out_vec[coord_in_mesh[mesh_axis]]; - VLOG(3) << "The shape of physical tensor after split is " - << out_physical_tensor_cur_rank.dims(); - - set_dist_props(out, out_physical_tensor_cur_rank, in.dims(), out_dist_attr); + SetValue(out, split_out_vec[coord_in_mesh[mesh_axis]]); + SetDistProps(out, in.dims(), out_dist_attr); } REGISTER_RESHARD_FUNC(RToSReshardFunction); diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.cc b/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.cc deleted file mode 100644 index 4cf18ca218138..0000000000000 --- a/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.cc +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h" - -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" -#include "paddle/phi/core/visit_type.h" -#include "paddle/phi/kernels/all_gather_kernel.h" - -namespace phi { -namespace distributed { - -DenseTensor ReshardAllGatherFunctor(DeviceContext* dev_ctx, - const DenseTensor& input, - const std::vector& process_ids) { - DenseTensor out; - - int64_t world_size = process_ids.size(); - auto* comm_context = CreateOrGetCommContext(*dev_ctx, process_ids); - dev_ctx->SetCommContext(comm_context); - - if (phi::CPUContext::classof(dev_ctx)) { - PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( - input.dtype(), "AllGather", ([&] { - AllGather(static_cast(*dev_ctx), - input, - world_size, - &out); - })); - return out; - } -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (phi::GPUContext::classof(dev_ctx)) { - PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( - input.dtype(), "AllGather", ([&] { - AllGather(static_cast(*dev_ctx), - input, - world_size, - &out); - })); - return out; - } -#endif -#ifdef PADDLE_WITH_CUSTOM_DEVICE - if (phi::CustomContext::classof(dev_ctx)) { - PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( - input.dtype(), "AllGather", ([&] { - AllGather(static_cast(*dev_ctx), - input, - world_size, - &out); - })); - return out; - } -#endif - PADDLE_THROW(phi::errors::Unimplemented( - "The all_gather in reshard only supported on CPU and GPU for now.")); -} - -} // namespace distributed -} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h b/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h deleted file mode 100644 index 311bd74765d2e..0000000000000 --- a/paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#pragma once - -#include -#include - -namespace phi { -class DenseTensor; -class DeviceContext; - -namespace distributed { - -DenseTensor ReshardAllGatherFunctor(DeviceContext* dev_ctx, - const DenseTensor& input, - const std::vector& process_ids); - -} // namespace distributed -} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.cc b/paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.cc deleted file mode 100644 index 49115dbffd0bd..0000000000000 --- a/paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.h" - -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/device_context.h" -#include "paddle/phi/core/visit_type.h" -#include "paddle/phi/kernels/concat_kernel.h" - -namespace phi { -namespace distributed { - -DenseTensor ReshardConcatFunctor(const DeviceContext& dev_ctx, - const std::vector& input, - int64_t axis) { - DenseTensor result; - auto dtype = (*input.begin())->dtype(); - - if (phi::CPUContext::classof(&dev_ctx)) { - PD_VISIT_ALL_TYPES( - dtype, "Concat", ([&] { - Concat( - static_cast(dev_ctx), input, axis, &result); - })); - return result; - } -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (phi::GPUContext::classof(&dev_ctx)) { - PD_VISIT_ALL_TYPES( - dtype, "Concat", ([&] { - Concat( - static_cast(dev_ctx), input, axis, &result); - })); - return result; - } -#endif - PADDLE_THROW(phi::errors::Unimplemented( - "The concat in reshard only supported on CPU and GPU for now.")); -} - -} // namespace distributed -} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/reshard_function.cc index 637af9641d3d3..63044549a2e37 100644 --- a/paddle/phi/core/distributed/auto_parallel/reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/reshard_function.cc @@ -29,20 +29,26 @@ std::shared_ptr ReshardFunction::Eval( return out; } -void ReshardFunction::set_dist_props(DistTensor* tensor, - const DenseTensor& value, - const DDim& dims, - const TensorDistAttr& dist_attr) { +void ReshardFunction::SetValue(DistTensor* tensor, const DenseTensor& value) { + tensor->value_ = value; +} + +void ReshardFunction::SetDistProps(DistTensor* tensor, + const DDim& dims, + const TensorDistAttr& dist_attr) { PADDLE_ENFORCE_EQ(dist_attr.verify(vectorize(dims)), true, phi::errors::InvalidArgument( "The input dist_attr and dims are improper.")); - tensor->value_ = value; tensor->dims_ = dims; tensor->dist_attr_ = dist_attr; } +DenseTensor* ReshardFunction::GetMutableTensor(DistTensor* tensor) { + return &tensor->value_; +} + ReshardFunction* ChooseProperReshardFunction( const DistTensor& in, const TensorDistAttr& out_dist_attr) { for (const auto& func : GetReshardFunctionList()) { diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_function.h b/paddle/phi/core/distributed/auto_parallel/reshard_function.h index 305a9af337c24..48d9fe64eabcc 100644 --- a/paddle/phi/core/distributed/auto_parallel/reshard_function.h +++ b/paddle/phi/core/distributed/auto_parallel/reshard_function.h @@ -44,10 +44,11 @@ class ReshardFunction { DistTensor* out) = 0; protected: - void set_dist_props(DistTensor* tensor, - const DenseTensor& value, - const DDim& dims, - const TensorDistAttr& dist_attr); + void SetValue(DistTensor* tensor, const DenseTensor& value); + void SetDistProps(DistTensor* tensor, + const DDim& dims, + const TensorDistAttr& dist_attr); + DenseTensor* GetMutableTensor(DistTensor* tensor); }; std::vector>& GetReshardFunctionList(); diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.cc b/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.cc deleted file mode 100644 index 4d0818eed4c0a..0000000000000 --- a/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.cc +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h" - -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/device_context.h" -#include "paddle/phi/core/visit_type.h" -#include "paddle/phi/kernels/split_kernel.h" - -namespace phi { -namespace distributed { - -std::vector ReshardSplitFunctor(const DeviceContext& dev_ctx, - const DenseTensor& input, - const IntArray& sections, - int64_t axis) { - std::vector result; - - if (phi::CPUContext::classof(&dev_ctx)) { - PD_VISIT_ALL_TYPES(input.dtype(), "Split", ([&] { - Split(static_cast(dev_ctx), - input, - sections, - axis, - &result); - })); - return result; - } -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - if (phi::GPUContext::classof(&dev_ctx)) { - PD_VISIT_ALL_TYPES(input.dtype(), "Split", ([&] { - Split(static_cast(dev_ctx), - input, - sections, - axis, - &result); - })); - return result; - } -#endif - PADDLE_THROW(phi::errors::Unimplemented( - "The split in reshard only supported on CPU and GPU for now.")); -} - -} // namespace distributed -} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h b/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h deleted file mode 100644 index 87b9f2301ad0b..0000000000000 --- a/paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#pragma once - -#include -#include -#include "paddle/phi/common/int_array.h" - -namespace phi { -class DeviceContext; -class DenseTensor; - -namespace distributed { -std::vector ReshardSplitFunctor(const DeviceContext& dev_ctx, - const DenseTensor& input, - const IntArray& sections, - int64_t axis); - -} // namespace distributed -} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_utils.cc b/paddle/phi/core/distributed/auto_parallel/reshard_utils.cc index c1b569b1dbcb0..0c86eb864ed2b 100644 --- a/paddle/phi/core/distributed/auto_parallel/reshard_utils.cc +++ b/paddle/phi/core/distributed/auto_parallel/reshard_utils.cc @@ -14,22 +14,31 @@ #include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" -#include - -// the needs to be included before , otherwise -// there will be symbol redefinition error on windows -#include "paddle/phi/core/distributed/store/tcp_store.h" - #include "glog/logging.h" -#include "paddle/phi/backends/all_context.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/distributed/auto_parallel/process_mesh.h" -#include "paddle/phi/core/distributed/auto_parallel/utils.h" #include "paddle/phi/core/distributed/comm_context_manager.h" +#include "paddle/phi/core/distributed/store/store_utils.h" namespace phi { namespace distributed { -using auto_parallel::str_split; + +namespace { +int64_t GetLocalRankInParticipate(const std::vector& process_ids) { + int64_t cur_global_rank = GetCurGlobalRank(); + auto iter = + std::find(process_ids.begin(), process_ids.end(), cur_global_rank); + return iter - process_ids.begin(); +} + +std::string GenUniqueCommKey(const std::vector& process_ids) { + std::string unique_comm_key = "ReshardGroup"; + for (const auto& id : process_ids) { + unique_comm_key += "/" + std::to_string(id); + } + return unique_comm_key; +} +} // namespace bool IsDimsMappingShard(const std::vector& dims_mapping) { return std::any_of(dims_mapping.begin(), @@ -70,93 +79,6 @@ std::vector GetCurRankCoordInMesh(const ProcessMesh& process_mesh) { return coord; } -std::map GetSplitAxisWithDimsMapping( - const std::vector& dims_mapping) { - std::map split_axis_to_mesh_axis; - for (size_t i = 0; i < dims_mapping.size(); ++i) { - if (dims_mapping[i] != -1) { - split_axis_to_mesh_axis.emplace(i, dims_mapping[i]); - } - } - return split_axis_to_mesh_axis; -} - -int64_t GetCurGlobalRank() { - const char* cur_rank = std::getenv("PADDLE_TRAINER_ID"); - PADDLE_ENFORCE_NOT_NULL( - cur_rank, - phi::errors::NotFound( - "The environment variable 'PADDLE_TRAINER_ID' cannot be found.")); - return std::atoi(cur_rank); -} - -int64_t GetGlobalWorldSize() { - const char* world_size = std::getenv("PADDLE_TRAINERS_NUM"); - PADDLE_ENFORCE_NOT_NULL( - world_size, - phi::errors::NotFound( - "The environment variable 'PADDLE_TRAINERS_NUM' cannot be found.")); - return std::atoi(world_size); -} - -namespace { -std::string GetMasterEndpoint() { - const char* master_endpoint = std::getenv("PADDLE_MASTER"); - if (!master_endpoint) { - const char* trainer_endpoints = std::getenv("PADDLE_TRAINER_ENDPOINTS"); - PADDLE_ENFORCE_NOT_NULL( - trainer_endpoints, - phi::errors::NotFound("The environment variable " - "'PADDLE_TRAINER_ENDPOINTS' cannot be found.")); - return str_split(trainer_endpoints, ",")[0]; - } - - PADDLE_ENFORCE_NOT_NULL( - master_endpoint, - phi::errors::NotFound( - "The environment variable 'PADDLE_MASTER' cannot be found.")); - return master_endpoint; -} - -std::string GenUniqueCommKey(const std::vector& process_ids) { - std::string unique_comm_key = "ReshardGroup"; - for (const auto& id : process_ids) { - unique_comm_key += "/" + std::to_string(id); - } - return unique_comm_key; -} - -int64_t GetLocalRankInParticipate(const std::vector& process_ids) { - int64_t cur_global_rank = GetCurGlobalRank(); - auto iter = - std::find(process_ids.begin(), process_ids.end(), cur_global_rank); - return iter - process_ids.begin(); -} - -} // namespace - -std::string GetMasterAddr() { - std::string master_endpoint = GetMasterEndpoint(); - return str_split(master_endpoint, ":")[0]; -} - -uint16_t GetMasterPort() { - std::string master_endpoint = GetMasterEndpoint(); - return std::stoi(str_split(master_endpoint, ":")[1]); -} - -std::shared_ptr CreateOrGetGlobalTCPStore() { - std::string host = GetMasterAddr(); - uint16_t port = GetMasterPort(); - int64_t cur_rank = GetCurGlobalRank(); - int64_t world_size = GetGlobalWorldSize(); - bool is_master = (cur_rank == 0); - - static std::shared_ptr store = - std::make_shared(host, port, is_master, world_size); - return store; -} - CommContext* CreateOrGetCommContext(const DeviceContext& dev_ctx, const std::vector& process_ids) { std::string unique_comm_key = GenUniqueCommKey(process_ids); @@ -202,6 +124,17 @@ CommContext* CreateOrGetCommContext(const DeviceContext& dev_ctx, return comm_context; } +std::map GetSplitAxisWithDimsMapping( + const std::vector& dims_mapping) { + std::map split_axis_to_mesh_axis; + for (size_t i = 0; i < dims_mapping.size(); ++i) { + if (dims_mapping[i] != -1) { + split_axis_to_mesh_axis.emplace(i, dims_mapping[i]); + } + } + return split_axis_to_mesh_axis; +} + std::vector BalancedSplit(int64_t total_nums, int64_t num_of_pieces) { std::vector result(num_of_pieces, total_nums / num_of_pieces); int64_t remain_nums = total_nums % num_of_pieces; diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_utils.h b/paddle/phi/core/distributed/auto_parallel/reshard_utils.h index f50d9496d39fe..ebd54544f15e3 100644 --- a/paddle/phi/core/distributed/auto_parallel/reshard_utils.h +++ b/paddle/phi/core/distributed/auto_parallel/reshard_utils.h @@ -20,13 +20,14 @@ #include #include +#include "paddle/phi/backends/all_context.h" +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/core/visit_type.h" + namespace phi { class DeviceContext; namespace distributed { -class CommContext; -class TCPStore; - class ProcessMesh; bool IsDimsMappingShard(const std::vector& dims_mapping); @@ -46,6 +47,11 @@ std::vector GetCurRankCoordInMesh(const ProcessMesh& process_mesh); std::map GetSplitAxisWithDimsMapping( const std::vector& dims_mapping); +// If given a number, balance split it to multiple pieces. +// For example, the input value is 12, split it to 5 pieces, then return +// {3, 3, 2, 2, 2}. +std::vector BalancedSplit(int64_t total_nums, int64_t num_of_pieces); + // Create a comm context of the input process_ids. Once the newly comm context // created, it will be cached in the global instance, and get from the global // cache later. If the input dev_ctx is GPU, then nccl comm context will be @@ -53,20 +59,54 @@ std::map GetSplitAxisWithDimsMapping( CommContext* CreateOrGetCommContext(const DeviceContext& dev_ctx, const std::vector& process_ids); -int64_t GetCurGlobalRank(); - -std::string GetMasterAddr(); - -int64_t GetGlobalWorldSize(); - -uint16_t GetMasterPort(); - -std::shared_ptr CreateOrGetGlobalTCPStore(); - -// If given a number, balance split it to multiple pieces. -// For example, the input value is 12, split it to 5 pieces, then return -// {3, 3, 2, 2, 2}. -std::vector BalancedSplit(int64_t total_nums, int64_t num_of_pieces); +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#define RESHARD_FUNCTOR_IMPL(dev_ctx, fn_name, dtype, ...) \ + do { \ + if (phi::CPUContext::classof(dev_ctx)) { \ + PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( \ + dtype, #fn_name, ([&] { \ + fn_name(static_cast(*dev_ctx), \ + __VA_ARGS__); \ + })); \ + } else if (phi::GPUContext::classof(dev_ctx)) { \ + PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( \ + dtype, #fn_name, ([&] { \ + fn_name(static_cast(*dev_ctx), \ + __VA_ARGS__); \ + })); \ + } else { \ + PADDLE_THROW(phi::errors::Unimplemented( \ + "The %s in reshard only supported on CPU and GPU for now.", \ + #fn_name)); \ + } \ + } while (0) +#else +#define RESHARD_FUNCTOR_IMPL(dev_ctx, fn_name, dtype, ...) \ + do { \ + if (phi::CPUContext::classof(dev_ctx)) { \ + PD_VISIT_FLOATING_AND_INTEGRAL_TYPES( \ + dtype, #fn_name, ([&] { \ + fn_name(static_cast(*dev_ctx), \ + __VA_ARGS__); \ + })); \ + } else { \ + PADDLE_THROW(phi::errors::Unimplemented( \ + "The %s in reshard only supported on CPU for now.", #fn_name)); \ + } \ + } while (0) +#endif + +#define RESHARD_FUNCTOR_WITH_COMM(dev_ctx, fn_name, dtype, process_ids, ...) \ + do { \ + auto* comm_context = CreateOrGetCommContext(*dev_ctx, process_ids); \ + dev_ctx->SetCommContext(comm_context); \ + RESHARD_FUNCTOR_IMPL(dev_ctx, fn_name, dtype, __VA_ARGS__); \ + } while (0) + +#define RESHARD_FUNCTOR(dev_ctx, fn_name, dtype, ...) \ + do { \ + RESHARD_FUNCTOR_IMPL(dev_ctx, fn_name, dtype, __VA_ARGS__); \ + } while (0) } // namespace distributed } // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc index e10587237ba3e..61f57e1d6695c 100644 --- a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc @@ -18,10 +18,10 @@ #include "paddle/phi/common/int_array.h" #include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" -#include "paddle/phi/core/distributed/auto_parallel/reshard_all_gather_functor.h" -#include "paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.h" -#include "paddle/phi/core/distributed/auto_parallel/reshard_split_functor.h" #include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" +#include "paddle/phi/kernels/all_gather_kernel.h" +#include "paddle/phi/kernels/concat_kernel.h" +#include "paddle/phi/kernels/split_kernel.h" namespace phi { namespace distributed { @@ -60,17 +60,22 @@ void SToRReshardFunction::Eval(DeviceContext* dev_ctx, const DistTensor& in, const TensorDistAttr& out_dist_attr, DistTensor* out) { - const DenseTensor& in_physical_tensor_cur_rank = in.value(); const auto& in_dist_attr = in.dist_attr(); const auto& in_dims_mapping = in_dist_attr.dims_mapping(); const auto& in_process_mesh = in_dist_attr.process_mesh(); const auto& in_process_ids = in_process_mesh.process_ids(); + auto dtype = in.dtype(); // Since the precondition ensure the out_process_ids is equal to the // in_process_ids, so the participate process ids mush equal to either // in_process_ids or out_process_ids. - DenseTensor out_all_gather = ReshardAllGatherFunctor( - dev_ctx, in_physical_tensor_cur_rank, in_process_ids); + RESHARD_FUNCTOR_WITH_COMM(dev_ctx, + AllGather, + dtype, + in_process_ids, + in.value(), + in_process_ids.size(), + GetMutableTensor(out)); std::map split_axis_to_mesh_axis = GetSplitAxisWithDimsMapping(in_dims_mapping); @@ -79,7 +84,7 @@ void SToRReshardFunction::Eval(DeviceContext* dev_ctx, if (split_axis == 0) { // If the input dist tensor is shard(0), the subsequent split // and concat is unnecessary. - set_dist_props(out, out_all_gather, out_all_gather.dims(), out_dist_attr); + SetDistProps(out, in.dims(), out_dist_attr); } else { // Since the result of all_gather always concat the tensor on axis 0, // first we need to split the result on axis 0, @@ -88,21 +93,30 @@ void SToRReshardFunction::Eval(DeviceContext* dev_ctx, int64_t num_of_process = in_process_ids.size(); IntArray sections(std::vector( - num_of_process, - in_physical_tensor_cur_rank.dims()[default_split_axis])); - std::vector split_out_vec = ReshardSplitFunctor( - *dev_ctx, out_all_gather, sections, default_split_axis); + num_of_process, in.value().dims()[default_split_axis])); + std::vector split_out_vec; + RESHARD_FUNCTOR(dev_ctx, + Split, + dtype, + out->value(), + sections, + default_split_axis, + &split_out_vec); // Concat the result after split on correct axis. std::vector concat_input_vec; for (const auto& tensor : split_out_vec) { concat_input_vec.emplace_back(&tensor); } - DenseTensor concat_out_tensor = - ReshardConcatFunctor(*dev_ctx, concat_input_vec, split_axis); - set_dist_props( - out, concat_out_tensor, concat_out_tensor.dims(), out_dist_attr); + RESHARD_FUNCTOR(dev_ctx, + Concat, + dtype, + concat_input_vec, + split_axis, + GetMutableTensor(out)); + + SetDistProps(out, in.dims(), out_dist_attr); } } diff --git a/paddle/phi/core/distributed/auto_parallel/utils.h b/paddle/phi/core/distributed/auto_parallel/utils.h index c9e69dd550abb..915c156529670 100644 --- a/paddle/phi/core/distributed/auto_parallel/utils.h +++ b/paddle/phi/core/distributed/auto_parallel/utils.h @@ -14,6 +14,7 @@ limitations under the License. */ #pragma once #include +#include #include #include #include diff --git a/paddle/phi/core/distributed/store/CMakeLists.txt b/paddle/phi/core/distributed/store/CMakeLists.txt index 8eaa76eac1c67..3b62a1367eea9 100644 --- a/paddle/phi/core/distributed/store/CMakeLists.txt +++ b/paddle/phi/core/distributed/store/CMakeLists.txt @@ -1,4 +1,5 @@ -set(STORE_COMMON_SRCS tcp_store.cc tcp_utils.cc socket.cpp store.cc) +set(STORE_COMMON_SRCS tcp_store.cc tcp_utils.cc socket.cpp store.cc + store_utils.cc) if(WITH_GLOO) list(APPEND STORE_COMMON_SRCS gloo_store.cc) diff --git a/paddle/phi/core/distributed/store/store_utils.cc b/paddle/phi/core/distributed/store/store_utils.cc new file mode 100644 index 0000000000000..c2679ef2192a3 --- /dev/null +++ b/paddle/phi/core/distributed/store/store_utils.cc @@ -0,0 +1,90 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/core/distributed/store/store_utils.h" + +#include + +// the needs to be included before , otherwise +// there will be symbol redefinition error on windows +#include "paddle/phi/core/distributed/store/tcp_store.h" + +#include "paddle/phi/core/distributed/auto_parallel/utils.h" + +namespace phi { +namespace distributed { +using auto_parallel::str_split; + +namespace { +std::string GetMasterEndpoint() { + const char* master_endpoint = std::getenv("PADDLE_MASTER"); + if (!master_endpoint) { + const char* trainer_endpoints = std::getenv("PADDLE_TRAINER_ENDPOINTS"); + PADDLE_ENFORCE_NOT_NULL( + trainer_endpoints, + phi::errors::NotFound("The environment variable " + "'PADDLE_TRAINER_ENDPOINTS' cannot be found.")); + return str_split(trainer_endpoints, ",")[0]; + } + + PADDLE_ENFORCE_NOT_NULL( + master_endpoint, + phi::errors::NotFound( + "The environment variable 'PADDLE_MASTER' cannot be found.")); + return master_endpoint; +} +} // namespace + +int64_t GetCurGlobalRank() { + const char* cur_rank = std::getenv("PADDLE_TRAINER_ID"); + PADDLE_ENFORCE_NOT_NULL( + cur_rank, + phi::errors::NotFound( + "The environment variable 'PADDLE_TRAINER_ID' cannot be found.")); + return std::atoi(cur_rank); +} + +int64_t GetGlobalWorldSize() { + const char* world_size = std::getenv("PADDLE_TRAINERS_NUM"); + PADDLE_ENFORCE_NOT_NULL( + world_size, + phi::errors::NotFound( + "The environment variable 'PADDLE_TRAINERS_NUM' cannot be found.")); + return std::atoi(world_size); +} + +std::string GetMasterAddr() { + std::string master_endpoint = GetMasterEndpoint(); + return str_split(master_endpoint, ":")[0]; +} + +uint16_t GetMasterPort() { + std::string master_endpoint = GetMasterEndpoint(); + return std::stoi(str_split(master_endpoint, ":")[1]); +} + +std::shared_ptr CreateOrGetGlobalTCPStore() { + std::string host = GetMasterAddr(); + uint16_t port = GetMasterPort(); + int64_t cur_rank = GetCurGlobalRank(); + int64_t world_size = GetGlobalWorldSize(); + bool is_master = (cur_rank == 0); + + static std::shared_ptr store = + std::make_shared(host, port, is_master, world_size); + return store; +} + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.h b/paddle/phi/core/distributed/store/store_utils.h similarity index 75% rename from paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.h rename to paddle/phi/core/distributed/store/store_utils.h index ce4798458bde7..3aad27a46b5ea 100644 --- a/paddle/phi/core/distributed/auto_parallel/reshard_concat_functor.h +++ b/paddle/phi/core/distributed/store/store_utils.h @@ -15,16 +15,22 @@ #pragma once #include -#include +#include +#include namespace phi { -class DeviceContext; -class DenseTensor; namespace distributed { +class Store; -DenseTensor ReshardConcatFunctor(const DeviceContext& dev_ctx, - const std::vector& input, - int64_t axis); +int64_t GetCurGlobalRank(); + +std::string GetMasterAddr(); + +int64_t GetGlobalWorldSize(); + +uint16_t GetMasterPort(); + +std::shared_ptr CreateOrGetGlobalTCPStore(); } // namespace distributed } // namespace phi From 3c2276e665f9d5b02c108f4f35db3ba4087c3c15 Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:41:37 +0800 Subject: [PATCH 037/194] [xdoctest] reformat example code with google style in No.284-285 (#56784) --- python/paddle/incubate/nn/loss.py | 8 ++++---- .../incubate/operators/graph_khop_sampler.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/python/paddle/incubate/nn/loss.py b/python/paddle/incubate/nn/loss.py index 09d41e3f82de7..d31fe41d8ce3f 100644 --- a/python/paddle/incubate/nn/loss.py +++ b/python/paddle/incubate/nn/loss.py @@ -49,10 +49,10 @@ def identity_loss(x, reduction="none"): .. code-block:: python - import paddle - paddle.enable_static() - loss = paddle.static.data(name="loss", shape=[-1, 1], dtype="float32") - out = paddle.incubate.identity_loss(loss, reduction=1) + >>> import paddle + >>> paddle.enable_static() + >>> loss = paddle.static.data(name="loss", shape=[-1, 1], dtype="float32") + >>> out = paddle.incubate.identity_loss(loss, reduction=1) """ if isinstance(reduction, str): reduction = {"sum": 0, "mean": 1, "none": 2}.get(reduction.lower()) diff --git a/python/paddle/incubate/operators/graph_khop_sampler.py b/python/paddle/incubate/operators/graph_khop_sampler.py index 06b079d7f1c36..9cade59f1fff3 100644 --- a/python/paddle/incubate/operators/graph_khop_sampler.py +++ b/python/paddle/incubate/operators/graph_khop_sampler.py @@ -70,17 +70,17 @@ def graph_khop_sampler( Examples: .. code-block:: python - import paddle + >>> import paddle - row = [3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7] - colptr = [0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13] - nodes = [0, 8, 1, 2] - sample_sizes = [2, 2] - row = paddle.to_tensor(row, dtype="int64") - colptr = paddle.to_tensor(colptr, dtype="int64") - nodes = paddle.to_tensor(nodes, dtype="int64") + >>> row = [3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7] + >>> colptr = [0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13] + >>> nodes = [0, 8, 1, 2] + >>> sample_sizes = [2, 2] + >>> row = paddle.to_tensor(row, dtype="int64") + >>> colptr = paddle.to_tensor(colptr, dtype="int64") + >>> nodes = paddle.to_tensor(nodes, dtype="int64") - edge_src, edge_dst, sample_index, reindex_nodes = paddle.incubate.graph_khop_sampler(row, colptr, nodes, sample_sizes, False) + >>> edge_src, edge_dst, sample_index, reindex_nodes = paddle.incubate.graph_khop_sampler(row, colptr, nodes, sample_sizes, False) """ From 487660a11aa2b3d14739179c6783ab11e9e05094 Mon Sep 17 00:00:00 2001 From: engineer1109 <1292846099@qq.com> Date: Thu, 31 Aug 2023 10:49:56 +0800 Subject: [PATCH 038/194] fix debug error (#56787) fix style --- paddle/ir/core/value.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/paddle/ir/core/value.cc b/paddle/ir/core/value.cc index d38bcdca36314..58622a4754023 100644 --- a/paddle/ir/core/value.cc +++ b/paddle/ir/core/value.cc @@ -264,10 +264,7 @@ uint32_t OpResultImpl::GetResultIndex() const { return ir::dyn_cast(this)->GetResultIndex(); } -OpResultImpl::~OpResultImpl() { - assert(use_empty() && - owner()->name() + " operation destroyed but still has uses."); -} +OpResultImpl::~OpResultImpl() { assert(use_empty()); } ir::Operation *OpResultImpl::owner() const { // For inline result, pointer offset index to obtain the address of op. From f3fa2ed394182926f41d71451e0c3891d259423e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:52:28 +0800 Subject: [PATCH 039/194] [xdoctest] reformat example code with google style in No. 309 (#56596) * input.py * Update python/paddle/nn/functional/input.py * Update input.py * Update all_gather.py * Update all_gather.py * xdoc * Apply suggestions from code review * Update python/paddle/distributed/models/moe/utils.py * Apply suggestions from code review Co-authored-by: Nyakku Shigure * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Nyakku Shigure --- python/paddle/distributed/models/moe/utils.py | 89 ++++++++++--------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/python/paddle/distributed/models/moe/utils.py b/python/paddle/distributed/models/moe/utils.py index 62e8c189bd9bc..5a2009b2fd0f2 100644 --- a/python/paddle/distributed/models/moe/utils.py +++ b/python/paddle/distributed/models/moe/utils.py @@ -27,17 +27,17 @@ def _number_count(numbers, upper_range): out (Tensor): The output expert count. Examples: .. code-block:: python - # required: distributed - import paddle - - numbers = [ - [0, 2], - [0, 2] - ] - upper_range = 6 - numbers = paddle.to_tensor(numbers, dtype="int32") - number_count = paddle.distributed.utils.number_count(numbers, upper_range) - print(number_count) # the result: [2, 0, 2, 0, 0, 0] + + >>> # doctest: +REQUIRES(env: DISTRIBUTED) + >>> import paddle + >>> from paddle.distributed.models.moe import utils + >>> numbers = [[0, 2], [0, 2]] + >>> upper_range = 6 + >>> numbers = paddle.to_tensor(numbers, dtype="int64") + >>> number_count = utils._number_count(numbers, upper_range) + >>> print(number_count) + Tensor(shape=[6], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [2, 0, 2, 0, 0, 0]) """ if in_dynamic_mode(): return _legacy_C_ops.number_count(numbers, 'upper_range', upper_range) @@ -73,18 +73,18 @@ def _assign_pos(x, cum_count): Examples: .. code-block:: python - # required: distributed - import paddle - number_count = [2, 0, 2, 0] - numbers = [ - [0, 2], - [0, 2] - ] - number_count = paddle.to_tensor(number_count) - numbers = paddle.to_tensor(numbers, dtype="int32") - num_cum = paddle.cumsum(number_count) - pos = paddle.distributed.utils.assign_pos(x=numbers, cum_count=num_cum) - print(pos) # the result: (2, 0, 3, 1) + >>> # doctest: +REQUIRES(env: DISTRIBUTED) + >>> import paddle + >>> from paddle.distributed.models.moe import utils + >>> number_count = [2, 0, 2, 0] + >>> numbers = [[0, 2], [0, 2]] + >>> number_count = paddle.to_tensor(number_count, dtype="int64") + >>> numbers = paddle.to_tensor(numbers, dtype="int64") + >>> num_cum = paddle.cumsum(number_count) + >>> pos = utils._assign_pos(x=numbers, cum_count=num_cum) + >>> print(pos) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [2, 0, 3, 1]) """ if in_dynamic_mode(): return _legacy_C_ops.assign_pos(x, cum_count, cum_count[-1]) @@ -140,15 +140,19 @@ def _limit_by_capacity(expert_count, capacity, n_worker): out (Tensor): The output expert count limit by capacity. Examples: .. code-block:: python - # required: distributed - import paddle - expert_count = [1, 2, 2, 8, 3, 6] - capacity = [5, 5, 5] - n_work = 2 - expert_count = paddle.to_tensor(expert_count, dtype="int32") - capacity = paddle.to_tensor(capacity, dtype="int32") - out = paddle.distributed.utils.limit_by_capacity(expert_count, capacity, n_work) - print(out) # the result: [1, 2, 2, 4, 3, 3] + + >>> # doctest: +REQUIRES(env: DISTRIBUTED) + >>> import paddle + >>> from paddle.distributed.models.moe import utils + >>> expert_count = [1, 2, 2, 8, 3, 6] + >>> capacity = [5, 5, 5] + >>> n_work = 2 + >>> expert_count = paddle.to_tensor(expert_count, dtype="int64") + >>> capacity = paddle.to_tensor(capacity, dtype="int64") + >>> out = utils._limit_by_capacity(expert_count, capacity, n_work) + >>> print(out) + Tensor(shape=[6], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 2, 2, 4, 3, 3]) """ if in_dynamic_mode(): return _legacy_C_ops.limit_by_capacity( @@ -186,14 +190,19 @@ def _prune_gate_by_capacity(gate_idx, expert_count, n_expert, n_worker): Examples: .. code-block:: python - import paddle - gate_idx = paddle.to_tensor([1, 3, 3, 3, 3, 2, 1, 1], dtype='int32') - expert_count = paddle.to_tensor([0, 3, 1, 3, 0, 0, 0, 0], dtype='int32') - n_worker = 1 - new_gate_id = paddle.distributed.utils.prune_gate_by_capacity(gate_idx, expert_count, n_expert, n_worker) - print(new_gate_id) - # Tensor(shape=[8], dtype=int32, place=CUDAPlace(0), stop_gradient=True, - [1, 3, 3, 3, -1, 2, 1, 1]) + >>> # doctest: +REQUIRES(env: DISTRIBUTED) + >>> import paddle + >>> from paddle.distributed.models.moe import utils + >>> gate_idx = paddle.to_tensor([1, 3, 3, 3, 3, 2, 1, 1], dtype='int64') + >>> expert_count = paddle.to_tensor([0, 3, 1, 3, 0, 0, 0, 0], dtype='int64') + >>> n_worker = 1 + >>> n_expert = 8 + >>> new_gate_id = utils._prune_gate_by_capacity( + ... gate_idx, expert_count, n_expert, n_worker + ... ) + >>> print(new_gate_id) + Tensor(shape=[8], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 3, 3, 3, -1, 2, 1, 1]) """ if in_dynamic_mode(): return _legacy_C_ops.prune_gate_by_capacity( From 5043708cc91d0771fd32b1a12222c8c37d764d80 Mon Sep 17 00:00:00 2001 From: JYChen Date: Thu, 31 Aug 2023 10:55:45 +0800 Subject: [PATCH 040/194] fix var order in paddle.jit.save (#56510) * fix var order in paddle.jit.save * support complex/bfloat16 for scale in static-mode --- python/paddle/jit/api.py | 8 ++++---- python/paddle/tensor/math.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 58d49065d3c1c..aaadbc4d66a53 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -1248,10 +1248,10 @@ def save(layer, path, input_spec=None, **configs): file_prefix = file_prefix + '.' + attr_func file_prefix = os.path.join(model_path, file_prefix) with scope_guard(scope): - input_vars = [] - for var in concrete_program.main_program.clone().list_vars(): - if var.name in input_var_names: - input_vars.append(var) + input_vars = [ + concrete_program.main_program.global_block().var(name) + for name in input_var_names + ] save_inference_model( path_prefix=file_prefix, feed_vars=input_vars, diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 5bca583fe6066..d620b57124207 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -270,6 +270,7 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): "x", [ 'float16', + 'bfloat16', 'uint16', 'float32', 'float64', @@ -278,6 +279,8 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): 'int32', 'int64', 'uint8', + 'complex64', + 'complex128', ], "scale", ) From 23bc4c2699fec1ad6d125077c64edddbd52eb339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:06:23 +0800 Subject: [PATCH 041/194] [xdoctest] reformat example code with google style in No. 300 (#56598) * Update communicator.py * xdoc * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --- python/paddle/incubate/optimizer/lookahead.py | 159 +++++++++--------- 1 file changed, 78 insertions(+), 81 deletions(-) diff --git a/python/paddle/incubate/optimizer/lookahead.py b/python/paddle/incubate/optimizer/lookahead.py index bfa08c40556be..40e0fd55e4b50 100644 --- a/python/paddle/incubate/optimizer/lookahead.py +++ b/python/paddle/incubate/optimizer/lookahead.py @@ -50,66 +50,63 @@ class LookAhead(Optimizer): .. code-block:: python - import numpy as np - import paddle - import paddle.nn as nn - - BATCH_SIZE = 16 - BATCH_NUM = 4 - EPOCH_NUM = 4 - - IMAGE_SIZE = 784 - CLASS_NUM = 10 - # define a random dataset - class RandomDataset(paddle.io.Dataset): - def __init__(self, num_samples): - self.num_samples = num_samples - - def __getitem__(self, idx): - image = np.random.random([IMAGE_SIZE]).astype('float32') - label = np.random.randint(0, CLASS_NUM - 1, - (1, )).astype('int64') - return image, label - - def __len__(self): - return self.num_samples - - class LinearNet(nn.Layer): - def __init__(self): - super().__init__() - self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) - self.bias = self._linear.bias - - @paddle.jit.to_static - def forward(self, x): - return self._linear(x) - - def train(layer, loader, loss_fn, opt): - for epoch_id in range(EPOCH_NUM): - for batch_id, (image, label) in enumerate(loader()): - out = layer(image) - loss = loss_fn(out, label) - loss.backward() - opt.step() - opt.clear_grad() - print("Train Epoch {} batch {}: loss = {}".format( - epoch_id, batch_id, np.mean(loss.numpy()))) - - layer = LinearNet() - loss_fn = nn.CrossEntropyLoss() - optimizer = paddle.optimizer.SGD(learning_rate=0.1, parameters=layer.parameters()) - lookahead = paddle.incubate.LookAhead(optimizer, alpha=0.2, k=5) - - # create data loader - dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) - loader = paddle.io.DataLoader( - dataset, - batch_size=BATCH_SIZE, - shuffle=True, - drop_last=True, - num_workers=2) - - train(layer, loader, loss_fn, lookahead) + >>> import numpy as np + >>> import paddle + >>> import paddle.nn as nn + + >>> BATCH_SIZE = 16 + >>> BATCH_NUM = 4 + >>> EPOCH_NUM = 4 + + >>> IMAGE_SIZE = 784 + >>> CLASS_NUM = 10 + >>> # define a random dataset + >>> class RandomDataset(paddle.io.Dataset): + ... def __init__(self, num_samples): + ... self.num_samples = num_samples + ... def __getitem__(self, idx): + ... image = np.random.random([IMAGE_SIZE]).astype('float32') + ... label = np.random.randint(0, CLASS_NUM - 1, + ... (1, )).astype('int64') + ... return image, label + ... def __len__(self): + ... return self.num_samples + + >>> class LinearNet(nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) + ... self.bias = self._linear.bias + ... @paddle.jit.to_static + ... def forward(self, x): + ... return self._linear(x) + + >>> def train(layer, loader, loss_fn, opt): + ... for epoch_id in range(EPOCH_NUM): + ... for batch_id, (image, label) in enumerate(loader()): + ... out = layer(image) + ... loss = loss_fn(out, label) + ... loss.backward() + ... opt.step() + ... opt.clear_grad() + ... print("Train Epoch {} batch {}: loss = {}".format( + ... epoch_id, batch_id, np.mean(loss.numpy()))) + >>> layer = LinearNet() + >>> loss_fn = nn.CrossEntropyLoss() + >>> optimizer = paddle.optimizer.SGD(learning_rate=0.1, parameters=layer.parameters()) + >>> lookahead = paddle.incubate.LookAhead(optimizer, alpha=0.2, k=5) + + >>> # create data loader + >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) + >>> loader = paddle.io.DataLoader( + ... dataset, + ... batch_size=BATCH_SIZE, + ... shuffle=True, + ... drop_last=True, + ... num_workers=2) + + >>> # doctest: +SKIP('The run time is too long to pass the CI check.') + >>> train(layer, loader, loss_fn, lookahead) """ _slow_str = "slow" @@ -161,16 +158,16 @@ def step(self): .. code-block:: python - import paddle - inp = paddle.rand([1,10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - lookahead = paddle.incubate.LookAhead(sgd, alpha=0.2, k=5) - loss.backward() - lookahead.step() - lookahead.clear_grad() + >>> import paddle + >>> inp = paddle.rand([1,10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + >>> lookahead = paddle.incubate.LookAhead(sgd, alpha=0.2, k=5) + >>> loss.backward() + >>> lookahead.step() + >>> lookahead.clear_grad() """ self.inner_optimizer.step() @@ -274,17 +271,17 @@ def minimize( .. code-block:: python - import paddle - - inp = paddle.rand([1, 10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - lookahead = paddle.incubate.LookAhead(sgd, alpha=0.2, k=5) - loss.backward() - lookahead.minimize(loss) - lookahead.clear_grad() + >>> import paddle + + >>> inp = paddle.rand([1, 10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + >>> lookahead = paddle.incubate.LookAhead(sgd, alpha=0.2, k=5) + >>> loss.backward() + >>> lookahead.minimize(loss) + >>> lookahead.clear_grad() """ assert isinstance(loss, Variable), "The loss should be an Tensor." From bb2310a6862bceb6e9b4529904ecf40fc062921f Mon Sep 17 00:00:00 2001 From: Yuang Liu Date: Thu, 31 Aug 2023 11:07:53 +0800 Subject: [PATCH 042/194] recompute support tuple (#56793) --- .../distributed/fleet/recompute/recompute.py | 82 +++++++++++++---- .../test_recompute_with_tuple_input.py | 89 +++++++++++++++++++ 2 files changed, 155 insertions(+), 16 deletions(-) create mode 100644 test/legacy_test/test_recompute_with_tuple_input.py diff --git a/python/paddle/distributed/fleet/recompute/recompute.py b/python/paddle/distributed/fleet/recompute/recompute.py index b3bf3889a347b..ba22372e79147 100755 --- a/python/paddle/distributed/fleet/recompute/recompute.py +++ b/python/paddle/distributed/fleet/recompute/recompute.py @@ -31,10 +31,24 @@ def detach_variable(inputs): out = [] for inp in inputs: - if not isinstance(inp, core.eager.Tensor): + if not isinstance(inp, core.eager.Tensor) and ( + type(inp) is not tuple or not isinstance(inp[0], core.eager.Tensor) + ): + # the inp is not a tensor or not a tuple of tensors out.append(inp) continue + if type(inp) is tuple: + detach_inp = [] + for i in inp: + # detach all tensors in the tuple + assert isinstance(i, core.eager.Tensor) + tmp_i = i.detach() + tmp_i.stop_gradient = i.stop_gradient + detach_inp.append(tmp_i) + out.append(tuple(detach_inp)) + continue + x = inp.detach() x.stop_gradient = inp.stop_gradient out.append(x) @@ -42,11 +56,16 @@ def detach_variable(inputs): def check_recompute_necessary(inputs): - if not any( - not input_.stop_gradient - for input_ in inputs - if isinstance(input_, (core.eager.Tensor, paddle.Tensor)) - ): + necessary_for_each_input = [] + for input_ in inputs: + if isinstance(input_, (core.eager.Tensor, paddle.Tensor)): + necessary_for_each_input.append(input_.stop_gradient) + elif type(input_) is tuple: + for i in input_: + # traverse all tensors in the tuple + if isinstance(i, (core.eager.Tensor, paddle.Tensor)): + necessary_for_each_input.append(i.stop_gradient) + if all(necessary_for_each_input): logger.warning( "[Recompute]: None of the inputs to current recompute block need grad, " "therefore there is NO need to recompute this block in backward !" @@ -81,12 +100,37 @@ def forward(ctx, run_function, preserve_rng_state, *args, **kwargs): # save input for backward ctx.inputs = [] ctx.tensor_indices = [] + ctx.duplicate_tensor = [False for _ in range(len(args))] tensor_inputs = [] for i, arg in enumerate(args): if paddle.is_tensor(arg): tensor_inputs.append(arg) ctx.tensor_indices.append(i) ctx.inputs.append(None) + elif type(arg) is tuple: + is_tensors = [paddle.is_tensor(a) for a in arg] + if all(is_tensors): + # the tuple is a tuple of tensors + tensors_stop_gradient = [a.stop_gradient for a in arg] + if not all(tensors_stop_gradient) and any( + tensors_stop_gradient + ): + # tensors in the tuple have different stop_gradient value, which pylayer doesn't support + raise ValueError( + "Recompute receive a tuple containing tensor holds different stop gradient." + ) + tensor_inputs.append(arg) + ctx.tensor_indices.append(i) + # Mark the tuple is a tuple of tensors + ctx.duplicate_tensor[i] = True + ctx.inputs.append(None) + elif any(is_tensors): + # the tuple contains tensors and non-tensor values + raise ValueError( + "Recompute receive a tuple containing tensor and non-tensor at same time." + ) + else: + ctx.inputs.append(arg) else: ctx.inputs.append(arg) ctx.save_for_backward(*tensor_inputs) @@ -132,6 +176,7 @@ def backward(ctx, *args): # Restore inputs inputs = list(ctx.inputs) tensor_indices = ctx.tensor_indices + duplicate_tensor = ctx.duplicate_tensor tensors = ctx.saved_tensor() for i, idx in enumerate(tensor_indices): inputs[idx] = tensors[i] @@ -198,18 +243,23 @@ def backward(ctx, *args): forward_outputs_with_grad, backward_inputs_with_grad ) + grads = [] + for idx, inp in enumerate(detached_inputs): + if isinstance(inp, core.eager.Tensor): + grads.append(inp._grad_ivar()) + elif type(inp) is tuple and duplicate_tensor[idx]: + # input is a tuple and is a tuple of tensors + if all(i.stop_gradient for i in inp): + # all tensors in the tuple doesn't need grad, only return a None for the whole tuple + grads.append(None) + else: + # all tensors in the tuple nees grad, should return a tuple of grads + grads.append(tuple(i._grad_ivar() for i in inp)) + if in_dynamic_mode(): - grads = tuple( - inp._grad_ivar() - for inp in detached_inputs - if isinstance(inp, core.eager.Tensor) - ) + grads = tuple(grads) else: - grads = [ - inp._grad_ivar() - for inp in detached_inputs - if isinstance(inp, core.eager.Tensor) - ] + grads = list(grads) return grads diff --git a/test/legacy_test/test_recompute_with_tuple_input.py b/test/legacy_test/test_recompute_with_tuple_input.py new file mode 100644 index 0000000000000..90b6c37dca14a --- /dev/null +++ b/test/legacy_test/test_recompute_with_tuple_input.py @@ -0,0 +1,89 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import paddle +from paddle.distributed.fleet.utils import recompute + + +class Layer(paddle.nn.Layer): + def __init__(self): + super().__init__() + self.linear1 = paddle.nn.Linear(10, 10) + self.linear2 = paddle.nn.Linear(10, 10) + self.linear3 = paddle.nn.Linear(10, 10) + self.silu1 = paddle.nn.Silu() + self.silu2 = paddle.nn.Silu() + self.silu3 = paddle.nn.Silu() + + def forward(self, x, y): + assert type(x) is tuple + assert len(x) == 2 + o1 = self.silu1(self.linear1(x[0])) + o2 = self.silu2(self.linear2(x[1])) + o3 = self.silu3(self.linear3(y)) + o = o1 + o2 + o3 + return o + + +class TestPyLayer(unittest.TestCase): + def test_tuple_input(self): + layer = Layer() + x1 = paddle.rand(shape=[10, 10]) + x1.stop_gradient = False + x2 = paddle.rand(shape=[10, 10]) + x2.stop_gradient = False + y = paddle.rand(shape=[10, 10]) + y.stop_gradient = False + o = recompute(layer, (x1, x2), y) + loss = paddle.mean(o, keepdim=True) + loss.backward() + + def test_tuple_input_with_non_tensor(self): + layer = Layer() + x1 = paddle.rand(shape=[10, 10]) + x1.stop_gradient = False + y = paddle.rand(shape=[10, 10]) + y.stop_gradient = False + try: + o = recompute(layer, (x1, True), y) + except ValueError: + pass + + def test_tuple_input_with_different_stop_gradient(self): + layer = Layer() + x1 = paddle.rand(shape=[10, 10]) + x1.stop_gradient = False + x2 = paddle.rand(shape=[10, 10]) + y = paddle.rand(shape=[10, 10]) + y.stop_gradient = False + try: + o = recompute(layer, (x1, True), y) + except ValueError: + pass + + def test_tuple_input_all_no_gradient(self): + layer = Layer() + x1 = paddle.rand(shape=[10, 10]) + x2 = paddle.rand(shape=[10, 10]) + y = paddle.rand(shape=[10, 10]) + y.stop_gradient = False + o = recompute(layer, (x1, x2), y) + loss = paddle.mean(o, keepdim=True) + loss.backward() + + +if __name__ == '__main__': + unittest.main() From 71e28b12815018a8420962fc6a20a3526085938d Mon Sep 17 00:00:00 2001 From: Tian Zheng Date: Thu, 31 Aug 2023 11:08:54 +0800 Subject: [PATCH 043/194] Add fused_scale_bias_relu_conv_bnstats OP (#55026) * Add fused_scale_bias_relu_conv_bnstats op * Review changes * Fix no CUDNN Frontend build * Fix PADDLE_ENFORCE format * Fix PADDLE_ENFORCE CI error * Rename kernel filename * Refactor unittest to use paddle eager_op_test * Fix padding bugs * Review changes * test=cuda117 * test=cuda117 --- paddle/phi/api/yaml/fused_ops.yaml | 10 + paddle/phi/infermeta/fusion.cc | 134 ++++ paddle/phi/infermeta/fusion.h | 28 + paddle/phi/kernels/CMakeLists.txt | 5 + paddle/phi/kernels/autotune/cache.cc | 5 + paddle/phi/kernels/autotune/cache.h | 9 +- .../kernels/autotune/cache_cudnn_frontend.h | 73 ++- ...sed_scale_bias_relu_conv_bnstats_kernel.cu | 618 ++++++++++++++++++ .../phi/kernels/gpudnn/conv_cudnn_frontend.h | 46 +- paddle/phi/kernels/gpudnn/conv_kernel.cu | 3 +- test/legacy_test/CMakeLists.txt | 4 + ...t_fused_scale_bias_relu_conv_bnstats_op.py | 239 +++++++ test/white_list/op_accuracy_white_list.py | 1 + 13 files changed, 1154 insertions(+), 21 deletions(-) create mode 100644 paddle/phi/kernels/fusion/gpu/fused_scale_bias_relu_conv_bnstats_kernel.cu create mode 100644 test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index 648384422ca8a..09ccd2fe7d87d 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -160,6 +160,16 @@ backward: fused_rotary_position_embedding_grad support_dygraph_mode : true +- op : fused_scale_bias_relu_conv_bnstats + args : (Tensor x, Tensor w, Tensor scale, Tensor bias, Tensor bn_scale, Tensor bn_bias, Tensor input_running_mean, Tensor input_running_var, int[] paddings, int[] dilations, int[] strides, str padding_algorithm, int groups, str data_format, float momentum, float epsilon, bool fuse_prologue, bool exhaustive_search, int64_t accumulation_count = 0) + optional : scale, bias + output : Tensor(out), Tensor(out_running_mean), Tensor(out_running_var), Tensor(saved_mean), Tensor(saved_var), Tensor(eq_scale), Tensor(eq_bias) + infer_meta : + func : FusedScaleBiasReluConvBnstatsInferMeta + kernel : + func : fused_scale_bias_relu_conv_bnstats + data_type : x + - op : generate_sequence_xpu args : (Tensor x, DataType dtype) output : Tensor diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 3143c5cde2e1e..993fb5d5887b8 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -821,4 +821,138 @@ void FastLayernormXPUInferMeta(const MetaTensor& x, out->set_layout(x.layout()); } +void FusedScaleBiasReluConvBnstatsInferMeta( + const MetaTensor& x, + const MetaTensor& w, + const MetaTensor& scale, + const MetaTensor& bias, + const MetaTensor& bn_scale, + const MetaTensor& bn_bias, + const MetaTensor& input_running_mean, + const MetaTensor& input_running_var, + const std::vector& paddings, + const std::vector& dilations, + const std::vector& strides, + const std::string& padding_algorithm, + int groups, + const std::string& data_format, + float momentum, + float epsilon, + bool fuse_prologue, + bool exhaustive_search, + int64_t accumulation_count, + MetaTensor* out, + MetaTensor* out_running_mean, + MetaTensor* out_running_var, + MetaTensor* saved_mean, + MetaTensor* saved_var, + MetaTensor* eq_scale, + MetaTensor* eq_bias) { + auto in_dims = x.dims(); + auto filter_dims = w.dims(); + // do some checks + PADDLE_ENFORCE_EQ( + in_dims.size(), + 4, + phi::errors::InvalidArgument( + "The input of Op(FusedScaleBiasReluConvBnstats) should be a 4-D " + "Tensor. But " + "received: input's dimension is %u, input's shape is [%s].", + in_dims.size(), + in_dims)); + + PADDLE_ENFORCE_EQ( + in_dims.size(), + filter_dims.size(), + phi::errors::InvalidArgument( + "The input's dimension and filter's dimension of " + "Op(FusedScaleBiasReluConvBnstats) should be equal. But received: " + "the input's" + " shape is [%s], " + "the input's dimension is %d; the filter's shape is [%s], " + "the filter's dimension is %d.", + in_dims, + in_dims.size(), + filter_dims, + filter_dims.size())); + + // Check if data format is NHWC + PADDLE_ENFORCE_EQ( + data_format, + "NHWC", + phi::errors::InvalidArgument( + "Operator(FusedScaleBiasReluConvBnstats) only supports data format " + "of " + "channel last (NHWC) now. But recieved: data_format = '%s'.", + data_format)); + + PADDLE_ENFORCE_EQ( + groups, + 1, + phi::errors::InvalidArgument("Expect group to be 1, got %d.", groups)); + + const auto input_channels = in_dims[in_dims.size() - 1]; + int dilation_size = dilations.size(); + for (int i = 0; i < dilation_size; ++i) { + PADDLE_ENFORCE_GT( + dilations[i], + 0, + phi::errors::InvalidArgument( + "The dilation of Op(Conv) should be larget than 0, but received " + "dilation is %d.", + dilations[i])); + } + + PADDLE_ENFORCE_EQ( + input_channels, + filter_dims[1] * groups, + phi::errors::InvalidArgument( + "The number of input's channels should be equal to filter's channels " + "* groups for Op(FusedScaleBiasReluConvBnstats). But received: the " + "input's" + " channels is %d, " + "the input's shape is [%s]; the filter's channels is %d, the " + "filter's shape is [%s]; the groups is %d. ", + input_channels, + in_dims, + filter_dims[1], + filter_dims, + groups)); + + // update paddings and dilations accoring to padding_algorithm + std::vector paddings_vec = paddings; + std::vector dilations_vec = dilations; + // get "HW" from "NHWC" + DDim in_data_dims = phi::slice_ddim(in_dims, 1, in_dims.size() - 1); + DDim filter_data_dims = phi::slice_ddim(filter_dims, 2, filter_dims.size()); + std::vector ksize = phi::vectorize(filter_data_dims); + phi::UpdatePaddingAndDilation(&paddings_vec, + &dilations_vec, + padding_algorithm, + in_data_dims, + strides, + ksize); + + std::vector out_shape({in_dims[0]}); + for (size_t i = 0; i < strides.size(); ++i) { + out_shape.push_back(ConvOutSize(in_dims[i + 1], + filter_dims[i + 2], + dilations[i], + paddings_vec[i * 2], + paddings_vec[i * 2 + 1], + strides[i])); + } + out_shape.push_back(filter_dims[0]); + // make shape for other outputs + auto c_dims = phi::make_ddim({filter_dims[0]}); + // set output and output max dims + out->set_dims(DDim(out_shape.data(), out_shape.size())); + out_running_mean->set_dims(c_dims); + out_running_var->set_dims(c_dims); + saved_mean->set_dims(c_dims); + saved_var->set_dims(c_dims); + eq_scale->set_dims(c_dims); + eq_bias->set_dims(c_dims); +} + } // namespace phi diff --git a/paddle/phi/infermeta/fusion.h b/paddle/phi/infermeta/fusion.h index 25c27bdd406b9..3d7ba19c4ec3f 100644 --- a/paddle/phi/infermeta/fusion.h +++ b/paddle/phi/infermeta/fusion.h @@ -201,4 +201,32 @@ void FastLayernormXPUInferMeta(const MetaTensor& x, float epsilon, MetaTensor* out); +void FusedScaleBiasReluConvBnstatsInferMeta( + const MetaTensor& x, + const MetaTensor& w, + const MetaTensor& scale, + const MetaTensor& bias, + const MetaTensor& bn_scale, + const MetaTensor& bn_bias, + const MetaTensor& input_running_mean, + const MetaTensor& input_running_var, + const std::vector& paddings, + const std::vector& dilations, + const std::vector& strides, + const std::string& padding_algorithm, + int groups, + const std::string& data_format, + float momentum, + float epsilon, + bool fuse_prologue, + bool exhaustive_search, + int64_t accumulation_count, + MetaTensor* out, + MetaTensor* out_running_mean, + MetaTensor* out_running_var, + MetaTensor* saved_mean, + MetaTensor* saved_var, + MetaTensor* eq_scale, + MetaTensor* eq_bias); + } // namespace phi diff --git a/paddle/phi/kernels/CMakeLists.txt b/paddle/phi/kernels/CMakeLists.txt index 9a917004c8314..cc8df692a1267 100644 --- a/paddle/phi/kernels/CMakeLists.txt +++ b/paddle/phi/kernels/CMakeLists.txt @@ -94,6 +94,11 @@ if(WITH_CUTLASS) list(APPEND kernel_cu ${cutlass_cu}) endif() +if(NOT WITH_CUDNN_FRONTEND) + list(REMOVE_ITEM kernel_cu + "fusion/gpu/fused_scale_bias_relu_conv_bnstats_kernel.cu") +endif() + set(cc_search_pattern "*.cc" "cpu/*.cc" diff --git a/paddle/phi/kernels/autotune/cache.cc b/paddle/phi/kernels/autotune/cache.cc index 6ff1296b5135e..ba48e2e00ce54 100644 --- a/paddle/phi/kernels/autotune/cache.cc +++ b/paddle/phi/kernels/autotune/cache.cc @@ -47,6 +47,11 @@ std::string AlgorithmTypeString(int64_t algo_type) { } else if (algo_type == static_cast(AlgorithmType::kConvBackwardFilterV8)) { return "conv_backward_filter_v8"; + } else if (algo_type == + static_cast(AlgorithmType::kScaleBiasReluConvBNstats)) { + return "scale_bias_relu_conv_bnstats"; + } else if (algo_type == static_cast(AlgorithmType::kBNFinalize)) { + return "bn_finalize"; } #endif return std::to_string(algo_type); diff --git a/paddle/phi/kernels/autotune/cache.h b/paddle/phi/kernels/autotune/cache.h index 188faaed71be3..34b98e28f50c7 100644 --- a/paddle/phi/kernels/autotune/cache.h +++ b/paddle/phi/kernels/autotune/cache.h @@ -55,7 +55,9 @@ enum class AlgorithmType { kConvForwardV8 = 10, kConvBackwardDataV8 = 11, kConvBackwardFilterV8 = 12, - kAlgorithmCount = 13 + kScaleBiasReluConvBNstats = 13, + kBNFinalize = 14, + kAlgorithmCount = 15 #endif }; @@ -178,9 +180,8 @@ class AutoTuneCache { conv_auto_tune_map_[key] = cache; } #ifdef PADDLE_WITH_CUDNN_FRONTEND - } else if (algo_type == AlgorithmType::kConvForwardV8 || - algo_type == AlgorithmType::kConvBackwardDataV8 || - algo_type == AlgorithmType::kConvBackwardFilterV8) { + } else if (algo_type >= AlgorithmType::kConvForwardV8 && + algo_type <= AlgorithmType::kBNFinalize) { int64_t key = static_cast(algo_type); if (cudnn_v8_auto_tune_map_.find(key) == cudnn_v8_auto_tune_map_.end()) { CudnnFrontendPlanCache cache; diff --git a/paddle/phi/kernels/autotune/cache_cudnn_frontend.h b/paddle/phi/kernels/autotune/cache_cudnn_frontend.h index 4715efa1f776e..cfd16e5143393 100644 --- a/paddle/phi/kernels/autotune/cache_cudnn_frontend.h +++ b/paddle/phi/kernels/autotune/cache_cudnn_frontend.h @@ -79,10 +79,10 @@ class CudnnFrontendPlanCache { return ret; } - void GetPlan(const cudnn_frontend::feature_vector_t &feature, - const cudnn_frontend::ExecutionPlan **plan, - int64_t *workspace_size, - cudnnHandle_t handle) { + void GetPlanAndWorkspaceSize(const cudnn_frontend::feature_vector_t &feature, + const cudnn_frontend::ExecutionPlan **plan, + int64_t *workspace_size, + cudnnHandle_t handle) { // Note(tizheng): CUDNNv8 execution plan is not thread-safe. // A shared plan being executed by different threads is // generally not safe (for now). @@ -90,11 +90,11 @@ class CudnnFrontendPlanCache { auto &local_map = map_[hasher(std::this_thread::get_id())]; auto it = local_map.find(GetExtendedFeature(feature, handle)); - if (it == local_map.end()) { - PADDLE_THROW(phi::errors::InvalidArgument( - "[cudnn_frontend] Cached Plan Not Found.")); - return; - } + PADDLE_ENFORCE_NE(it, + local_map.end(), + phi::errors::InvalidArgument( + "[cudnn_frontend] Cached Plan Not Found.")); + *plan = &(it->second); *workspace_size = (*plan)->getWorkspaceSize(); VLOG(4) << "Cached execution plan found." << (*plan)->getTag() @@ -133,11 +133,12 @@ class CudnnFrontendPlanCache { return FindPlan(op_graph.getFeatureVector(), handle); } - void GetPlan(const cudnn_frontend::OperationGraph &op_graph, - const cudnn_frontend::ExecutionPlan **plan, - int64_t *workspace_size, - cudnnHandle_t handle) { - GetPlan(op_graph.getFeatureVector(), plan, workspace_size, handle); + void GetPlanAndWorkspaceSize(const cudnn_frontend::OperationGraph &op_graph, + const cudnn_frontend::ExecutionPlan **plan, + int64_t *workspace_size, + cudnnHandle_t handle) { + GetPlanAndWorkspaceSize( + op_graph.getFeatureVector(), plan, workspace_size, handle); } void InsertPlan(const cudnn_frontend::OperationGraph &op_graph, @@ -176,5 +177,49 @@ class CudnnFrontendPlanCache { int64_t cache_misses_{0}; }; // class CudnnFrontendPlanCache +template +inline void BuildFeatureVectorSingle(cudnn_frontend::feature_vector_t *v, + const T &value) { + v->push_back(static_cast(value)); +} + +template <> +inline void BuildFeatureVectorSingle(cudnn_frontend::feature_vector_t *v, + const float &value) { + int64_t val = 0; + memcpy(&val, &value, sizeof(float)); + v->push_back(val); +} + +template <> +inline void BuildFeatureVectorSingle>( + cudnn_frontend::feature_vector_t *v, const std::vector &value) { + v->insert(v->end(), value.begin(), value.end()); +} + +template <> +inline void BuildFeatureVectorSingle>( + cudnn_frontend::feature_vector_t *v, const std::vector &value) { + for (auto &val : value) { + v->push_back(static_cast(val)); + } +} + +template <> +inline void BuildFeatureVectorSingle( + cudnn_frontend::feature_vector_t *v, const std::string &value) { + v->push_back(std::hash()(value)); +} + +inline void BuildFeatureVector(cudnn_frontend::feature_vector_t *v) { return; } + +template +inline void BuildFeatureVector(cudnn_frontend::feature_vector_t *v, + const T &value, + Args... args) { + BuildFeatureVectorSingle(v, value); + BuildFeatureVector(v, args...); +} + } // namespace autotune } // namespace phi diff --git a/paddle/phi/kernels/fusion/gpu/fused_scale_bias_relu_conv_bnstats_kernel.cu b/paddle/phi/kernels/fusion/gpu/fused_scale_bias_relu_conv_bnstats_kernel.cu new file mode 100644 index 0000000000000..e19996d63c791 --- /dev/null +++ b/paddle/phi/kernels/fusion/gpu/fused_scale_bias_relu_conv_bnstats_kernel.cu @@ -0,0 +1,618 @@ +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. */ + +#include + +#include "paddle/phi/backends/gpu/cuda/cudnn_helper.h" +#include "paddle/phi/backends/gpu/gpu_dnn.h" +#include "paddle/phi/backends/gpu/gpu_info.h" +#include "paddle/phi/core/flags.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/autotune/cache.h" +#include "paddle/phi/kernels/cpu/conv_util.h" +#include "paddle/phi/kernels/funcs/batch_norm_utils.h" +#include "paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h" + +DECLARE_bool(cudnn_deterministic); +DECLARE_bool(cudnn_exhaustive_search); + +namespace phi { +namespace fusion { + +using helper = phi::CudnnFrontendConvHelper; +template +using CudnnDataType = phi::backends::gpu::CudnnDataType; + +/* + * Implements Scale + Bias + ReLU + Conv + BNStats fusion pattern. + * Same as the following (x and output are in NHWC format): + * ``` + * output = conv2d(relu(x * scale + bias), w) + * sum_output, sqsum_output = bnstats(output) + * ``` + * Here, bnstats generates per-channel statistics, same as: + * ``` + * sum_output = output.sum(axis=[0,1,2]) + * sqsum_output = (output ** 2).sum(axis=[0,1,2]) + * ``` + * More details: + * https://docs.nvidia.com/deeplearning/cudnn/developer-guide/index.html#genstats-runtime-fusion-engine + */ +template +void FusedScaleBiasReluConvBnstatsImpl( + const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& w, + const paddle::optional& scale, + const paddle::optional& bias, + const std::vector& paddings, + const std::vector& dilations, + const std::vector& strides, + const std::string& padding_algorithm, + bool fuse_prologue, + bool exhaustive_search, + bool deterministic, + DenseTensor* output, + DenseTensor* sum_output, + DenseTensor* sqsum_output) { + auto& plan_cache = phi::autotune::AutoTuneCache::Instance().GetConvV8( + phi::autotune::AlgorithmType::kScaleBiasReluConvBNstats); + + // transformed tensor + DenseTensor w_transformed(w.dtype()); + // Assume input and output already in NHWC. + // No transformation is needed for them. + VLOG(3) << "Transform filter tensor from NCHW to NHWC."; + ResizeToChannelLast(dev_ctx, &w, &w_transformed); + TransToChannelLast(dev_ctx, &w, &w_transformed); + + // update padding and dilation + std::vector paddings_vec = paddings; + std::vector dilations_vec = dilations; + auto in_dims = x.dims(); + auto filter_dims = w_transformed.dims(); + DDim in_data_dims = slice_ddim(in_dims, 1, in_dims.size() - 1); + DDim filter_data_dims = slice_ddim(filter_dims, 1, filter_dims.size() - 1); + std::vector ksize = phi::vectorize(filter_data_dims); + phi::UpdatePaddingAndDilation(&paddings_vec, + &dilations_vec, + padding_algorithm, + in_data_dims, + strides, + ksize); + + int data_dim = strides.size(); // 2d only + + std::vector pre_padding(data_dim, 0); + std::vector post_padding(data_dim, 0); + for (size_t i = 0; i < data_dim; ++i) { + pre_padding[i] = static_cast(paddings_vec[2 * i]); + post_padding[i] = static_cast(paddings_vec[2 * i + 1]); + } + + // input pointers + T* input_data = const_cast(x.data()); + T* filter_data = w_transformed.data(); + + // output pointers + T* output_data = output->data(); + float* sum_output_data = sum_output->data(); + float* sqsum_output_data = sqsum_output->data(); + + auto handle = dev_ctx.cudnn_handle(); + auto workspace_handle = dev_ctx.cudnn_workspace_handle(); + + // build tensors + cudnnTensorFormat_t layout_format = CUDNN_TENSOR_NHWC; + auto tensor_format = phi::backends::gpu::ToCudnnDataType(x.dtype()); + + auto tensor_format_math = CUDNN_DATA_FLOAT; + auto compute_dtype = CUDNN_DATA_FLOAT; + + // get dims in CUDNN manner: [N, C, H, W] + auto dim_x = + phi::backends::gpu::TransformDimOrder(phi::vectorize(in_dims)); + auto dim_filt = phi::backends::gpu::TransformDimOrder( + phi::vectorize(filter_dims)); + auto dim_y = phi::backends::gpu::TransformDimOrder( + phi::vectorize(output->dims())); + std::vector dim_scale(dim_x.size(), 1); + dim_scale[1] = dim_x[1]; // [1, C, 1, 1] + std::vector dim_sum(dim_x.size(), 1); // [1, K, 1, 1] + dim_sum[1] = dim_filt[0]; + + std::vector data_ptrs; + std::vector uids; + int64_t uid = 100; + + // inputs + auto input_desc = helper::GetGeneralTensorDescriptor( + dim_x, layout_format, ++uid, 16, tensor_format); + data_ptrs.push_back(input_data); + uids.push_back(uid); + + auto filter_desc = helper::GetGeneralTensorDescriptor( + dim_filt, layout_format, ++uid, 16, tensor_format); + data_ptrs.push_back(filter_data); + uids.push_back(uid); + + // dispensable inputs + auto scale_desc = helper::GetGeneralTensorDescriptor( + dim_scale, layout_format, ++uid, 16, tensor_format); + if (fuse_prologue) { + data_ptrs.push_back(const_cast(scale->data())); + uids.push_back(uid); + } + + auto bias_desc = helper::GetGeneralTensorDescriptor( + dim_scale, layout_format, ++uid, 16, tensor_format); + if (fuse_prologue) { + data_ptrs.push_back(const_cast(bias->data())); + uids.push_back(uid); + } + + // outputs + auto output_desc = helper::GetGeneralTensorDescriptor( + dim_y, layout_format, ++uid, 16, tensor_format); + data_ptrs.push_back(output_data); + uids.push_back(uid); + + auto sum_output_desc = helper::GetGeneralTensorDescriptor( + dim_sum, layout_format, ++uid, 16, tensor_format_math); + data_ptrs.push_back(sum_output_data); + uids.push_back(uid); + + auto sqsum_output_desc = helper::GetGeneralTensorDescriptor( + dim_sum, layout_format, ++uid, 16, tensor_format_math); + data_ptrs.push_back(sqsum_output_data); + uids.push_back(uid); + + // virtual outputs + auto after_scale = helper::GetGeneralTensorDescriptor( + dim_x, layout_format, ++uid, 16, tensor_format_math, true); + auto after_bias = helper::GetGeneralTensorDescriptor( + dim_x, layout_format, ++uid, 16, tensor_format_math, true); + auto after_relu = helper::GetGeneralTensorDescriptor( + dim_x, layout_format, ++uid, 16, tensor_format_math, true); + + // create ops + auto scale_op = helper::MakePointwiseOp( + CUDNN_POINTWISE_MUL, compute_dtype, input_desc, scale_desc, after_scale); + + auto bias_op = helper::MakePointwiseOp( + CUDNN_POINTWISE_ADD, compute_dtype, after_scale, bias_desc, after_bias); + + auto relu_desc = cudnn_frontend::PointWiseDescBuilder() + .setMode(CUDNN_POINTWISE_RELU_FWD) + .setComputeType(compute_dtype) + .build(); + + auto relu_op = cudnn_frontend::OperationBuilder( + CUDNN_BACKEND_OPERATION_POINTWISE_DESCRIPTOR) + .setxDesc(after_bias) + .setyDesc(after_relu) + .setpwDesc(relu_desc) + .build(); + VLOG(6) << relu_op.describe(); + + std::vector stride_int64 = helper::GetInt64Array(strides); + std::vector dilation_int64 = helper::GetInt64Array(dilations_vec); + auto conv_desc = cudnn_frontend::ConvDescBuilder() + .setComputeType(compute_dtype) + .setMathMode(CUDNN_CROSS_CORRELATION) + .setSpatialDimCount(data_dim) + .setSpatialStride(data_dim, stride_int64.data()) + .setPrePadding(data_dim, pre_padding.data()) + .setPostPadding(data_dim, post_padding.data()) + .setDilation(data_dim, dilation_int64.data()) + .build(); + + float alpha = 1.0f; + float beta = 0.0f; + cudnn_frontend::Tensor* input_to_conv = + fuse_prologue ? &after_relu : &input_desc; + auto conv_op = cudnn_frontend::OperationBuilder( + CUDNN_BACKEND_OPERATION_CONVOLUTION_FORWARD_DESCRIPTOR) + .setxDesc(*input_to_conv) + .setwDesc(filter_desc) + .setyDesc(output_desc) + .setcDesc(conv_desc) + .setAlpha(alpha) + .setBeta(beta) + .build(); + VLOG(6) << conv_op.describe(); + + auto genstat_op = cudnn_frontend::OperationBuilder( + CUDNN_BACKEND_OPERATION_GEN_STATS_DESCRIPTOR) + .setxDesc(output_desc) + .setComputeType(compute_dtype) + .setGenStatsMode(CUDNN_GENSTATS_SUM_SQSUM) + .setSumDesc(sum_output_desc) + .setSqSumDesc(sqsum_output_desc) + .build(); + VLOG(6) << genstat_op.describe(); + + // build op graph + std::vector ops; + if (fuse_prologue) { + ops = std::vector( + {&scale_op, &bias_op, &relu_op, &conv_op, &genstat_op}); + } else { + ops = + std::vector({&conv_op, &genstat_op}); + } + + auto op_graph = cudnn_frontend::OperationGraphBuilder() + .setHandle(handle) + .setOperationGraph(ops.size(), ops.data()) + .build(); + VLOG(6) << op_graph.describe(); + + cudnn_frontend::feature_vector_t feature_vector; + phi::autotune::BuildFeatureVector(&feature_vector, + dim_x, + dim_filt, + strides, + paddings, + dilations, + pre_padding, + post_padding, + fuse_prologue); + + helper::QueryCacheAndExecute(handle, + &workspace_handle, + &op_graph, + &data_ptrs, + &uids, + exhaustive_search, + deterministic, + feature_vector, + &plan_cache); +} + +/* + * Implements BNFinalize pattern. It works with aforementioned bnstats node: + * ``` + * y = bn_finalize(genstats(conv_out)) + * ``` + * is the same as: + * ``` + * y = batchnorm2d(conv_out) + * ``` + */ +template +void BNFinalizeImpl(const Context& dev_ctx, + const DenseTensor& sum_tensor, + const DenseTensor& sqsum_tensor, + const DenseTensor& bn_scale, + const DenseTensor& bn_bias, + const DenseTensor& input_running_mean, + const DenseTensor& input_running_var, + int64_t accumulation_count, + float exp_decay, + float epsilon, + bool exhaustive_search, + bool deterministic, + DenseTensor* out_running_mean, + DenseTensor* out_running_var, + DenseTensor* saved_mean, + DenseTensor* saved_var, + DenseTensor* eq_scale, + DenseTensor* eq_bias) { + auto& plan_cache = phi::autotune::AutoTuneCache::Instance().GetConvV8( + phi::autotune::AlgorithmType::kBNFinalize); + + auto handle = dev_ctx.cudnn_handle(); + auto workspace_handle = dev_ctx.cudnn_workspace_handle(); + // set dtypes + cudnnTensorFormat_t layout_format = CUDNN_TENSOR_NHWC; + auto tensor_format_bn = + phi::backends::gpu::ToCudnnDataType(sum_tensor.dtype()); + auto tensor_format = phi::backends::gpu::ToCudnnDataType(eq_scale->dtype()); + auto compute_dtype = CUDNN_DATA_FLOAT; + // create tensor descriptors + auto dim_input = phi::vectorize(sum_tensor.dims()); + std::vector dim_c = {1, dim_input[0], 1, 1}; // [1, C, 1, 1] + std::vector dim_scalar = {1, 1, 1, 1}; + std::vector stride_scalar = {1, 1, 1, 1}; + + std::vector data_ptrs; + std::vector uids; + int64_t uid = 100; + + // inputs + auto sum_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(sum_tensor.data())); + uids.push_back(uid); + + auto sqsum_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(sqsum_tensor.data())); + uids.push_back(uid); + + auto scale_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(bn_scale.data())); + uids.push_back(uid); + + auto bias_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(bn_bias.data())); + uids.push_back(uid); + + auto input_running_mean_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(input_running_mean.data())); + uids.push_back(uid); + + auto input_running_var_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(const_cast(input_running_var.data())); + uids.push_back(uid); + + // outputs + auto updated_running_mean_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(out_running_mean->data()); + uids.push_back(uid); + + auto updated_running_var_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(out_running_var->data()); + uids.push_back(uid); + + auto saved_mean_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(saved_mean->data()); + uids.push_back(uid); + + auto saved_inv_var_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format_bn); + data_ptrs.push_back(saved_var->data()); + uids.push_back(uid); + + auto eq_scale_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format); + data_ptrs.push_back(eq_scale->data()); + uids.push_back(uid); + + auto eq_bias_desc = helper::GetGeneralTensorDescriptor( + dim_c, layout_format, ++uid, 16, tensor_format); + data_ptrs.push_back(eq_bias->data()); + uids.push_back(uid); + + // scalar descriptors + auto epsilon_desc = cudnn_frontend::TensorBuilder() + .setDim(dim_scalar.size(), dim_scalar.data()) + .setStride(stride_scalar.size(), stride_scalar.data()) + .setId(++uid) + .setAlignment(16) + .setDataType(CUDNN_DATA_FLOAT) + .setByValue(true) + .build(); + data_ptrs.push_back(&epsilon); + uids.push_back(uid); + + auto exp_decay_desc = + cudnn_frontend::TensorBuilder() + .setDim(dim_scalar.size(), dim_scalar.data()) + .setStride(stride_scalar.size(), stride_scalar.data()) + .setId(++uid) + .setAlignment(16) + .setDataType(CUDNN_DATA_FLOAT) + .setByValue(true) + .build(); + data_ptrs.push_back(&exp_decay); + uids.push_back(uid); + + auto accum_count_desc = + cudnn_frontend::TensorBuilder() + .setDim(dim_scalar.size(), dim_scalar.data()) + .setStride(stride_scalar.size(), stride_scalar.data()) + .setId(++uid) + .setAlignment(16) + .setDataType(CUDNN_DATA_INT64) + .setByValue(true) + .build(); + data_ptrs.push_back(&accumulation_count); + uids.push_back(uid); + + // build ops + auto finalize_stat_op = + cudnn_frontend::OperationBuilder( + CUDNN_BACKEND_OPERATION_BN_FINALIZE_STATISTICS_DESCRIPTOR) + .setComputeType(compute_dtype) + .setBNFinalizeMode(CUDNN_BN_FINALIZE_STATISTICS_TRAINING) + .setSumDesc(sum_desc) + .setSqSumDesc(sqsum_desc) + .setScaleAndBias(scale_desc, bias_desc) + .setEqScaleAndBias(eq_scale_desc, eq_bias_desc) + .setPrevRunningMeanAndVar(input_running_mean_desc, + input_running_var_desc) + .setNextRunningMeanAndVar(updated_running_mean_desc, + updated_running_var_desc) + .setSavedMeanAndInvVar(saved_mean_desc, saved_inv_var_desc) + .setEpsilonTensor(epsilon_desc) + .setAccumCountTensor(accum_count_desc) + .setExpDecayFactorTensor(exp_decay_desc) + .build(); + + std::array ops = {&finalize_stat_op}; + auto op_graph = cudnn_frontend::OperationGraphBuilder() + .setHandle(handle) + .setOperationGraph(ops.size(), ops.data()) + .build(); + VLOG(6) << op_graph.describe(); + + cudnn_frontend::feature_vector_t feature_vector; + phi::autotune::BuildFeatureVector( + &feature_vector, dim_input, accumulation_count, exp_decay, epsilon); + + helper::QueryCacheAndExecute(handle, + &workspace_handle, + &op_graph, + &data_ptrs, + &uids, + exhaustive_search, + deterministic, + feature_vector, + &plan_cache); +} + +template +void FusedScaleBiasReluConvBnstatsKernel( + const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& w, + const paddle::optional& scale, + const paddle::optional& bias, + const DenseTensor& bn_scale, + const DenseTensor& bn_bias, + const DenseTensor& input_running_mean, + const DenseTensor& input_running_var, + const std::vector& paddings, + const std::vector& dilations, + const std::vector& strides, + const std::string& padding_algorithm, + int groups, + const std::string& data_format, + float momentum, + float epsilon, + bool fuse_prologue, + bool exhaustive_search, + int64_t accumulation_count, + DenseTensor* out, + DenseTensor* out_running_mean, + DenseTensor* out_running_var, + DenseTensor* saved_mean, + DenseTensor* saved_var, + DenseTensor* eq_scale, + DenseTensor* eq_bias) { + auto cudnn_version = phi::backends::gpu::DnnVersion(); + PADDLE_ENFORCE_GE(cudnn_version, + 8800, + phi::errors::PreconditionNotMet( + "This op only supports CUDNN version >= 8800, " + "but got %d.", + cudnn_version)); + PADDLE_ENFORCE_GE(dev_ctx.GetComputeCapability(), + 80, + phi::errors::PreconditionNotMet( + "This op only supports Ampere and later devices, " + "but got compute capability: %d.", + dev_ctx.GetComputeCapability())); + // attr + float exp_decay = 1. - momentum; + if (epsilon <= CUDNN_BN_MIN_EPSILON - FLT_EPSILON) { + LOG(ERROR) << "Provided epsilon is smaller than " + << "CUDNN_BN_MIN_EPSILON. Setting it to " + << "CUDNN_BN_MIN_EPSILON instead."; + } + epsilon = + std::max(epsilon, static_cast(CUDNN_BN_MIN_EPSILON + FLT_EPSILON)); + // exhaustive search + exhaustive_search = exhaustive_search || FLAGS_cudnn_exhaustive_search; + bool deterministic = FLAGS_cudnn_deterministic; + PADDLE_ENFORCE_EQ(exhaustive_search && deterministic, + false, + phi::errors::InvalidArgument( + "Cann't set exhaustive_search True and " + "FLAGS_cudnn_deterministic True at same time.")); + // check optional inputs + if (fuse_prologue) { + PADDLE_ENFORCE_EQ( + scale && bias, + true, + phi::errors::InvalidArgument( + "\"scale\" and \"bias\" must be provided " + "when fuse_prologue = true. Got scale = %d; bias = %d.", + scale, + bias)); + } + + // alloc output variables + dev_ctx.template Alloc(out); + dev_ctx.template Alloc(out_running_mean); + dev_ctx.template Alloc(out_running_var); + dev_ctx.template Alloc(saved_mean); + dev_ctx.template Alloc(saved_var); + dev_ctx.template Alloc(eq_scale); + dev_ctx.template Alloc(eq_bias); + + // deal with strides, dilations and paddings + if (accumulation_count == 0) { + // dim_out = [N, H, W, C] + // accumulation_count = N * H * W + auto dim_out = phi::vectorize(out->dims()); + accumulation_count = dim_out[0] * dim_out[1] * dim_out[2]; + } + + // Step 1: Scale Bias ReLU Conv BNStats + auto bn_dims = bn_scale.dims(); + DenseTensor sum_tensor(bn_scale.dtype()); + DenseTensor sqsum_tensor(bn_scale.dtype()); + sum_tensor.Resize(bn_dims); + sqsum_tensor.Resize(bn_dims); + dev_ctx.template Alloc(&sum_tensor); + dev_ctx.template Alloc(&sqsum_tensor); + FusedScaleBiasReluConvBnstatsImpl(dev_ctx, + x, + w, + scale, + bias, + paddings, + dilations, + strides, + padding_algorithm, + fuse_prologue, + exhaustive_search, + deterministic, + out, + &sum_tensor, + &sqsum_tensor); + // Step 2: BN Finalize + BNFinalizeImpl(dev_ctx, + sum_tensor, + sqsum_tensor, + bn_scale, + bn_bias, + input_running_mean, + input_running_var, + accumulation_count, + exp_decay, + epsilon, + exhaustive_search, + deterministic, + out_running_mean, + out_running_var, + saved_mean, + saved_var, + eq_scale, + eq_bias); +} + +} // namespace fusion +} // namespace phi + +PD_REGISTER_KERNEL(fused_scale_bias_relu_conv_bnstats, + GPU, + ALL_LAYOUT, + phi::fusion::FusedScaleBiasReluConvBnstatsKernel, + phi::dtype::float16) { + kernel->OutputAt(1).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(3).SetDataType(phi::DataType::FLOAT32); + kernel->OutputAt(4).SetDataType(phi::DataType::FLOAT32); +} diff --git a/paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h b/paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h index ef8e606e547ce..d0bdcc10beaa8 100644 --- a/paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h +++ b/paddle/phi/kernels/gpudnn/conv_cudnn_frontend.h @@ -367,6 +367,48 @@ class CudnnFrontendConvHelper { plan_cache); } + static void QueryCacheAndExecute( + cudnnHandle_t handle, + phi::DnnWorkspaceHandle* workspace_handle, + cudnn_frontend::OperationGraph* op_graph_pointer, + std::vector* data_ptrs, + std::vector* uids, + bool exhaustive_search, + bool deterministic, + const cudnn_frontend::feature_vector_t& feature_vector, + phi::autotune::CudnnFrontendPlanCache* plan_cache) { + if (plan_cache->FindPlan(feature_vector, handle)) { + const cudnn_frontend::ExecutionPlan* cached_plan = nullptr; + int64_t workspace_size = 0; + plan_cache->GetPlanAndWorkspaceSize( + feature_vector, &cached_plan, &workspace_size, handle); + ExecutePlan(handle, + workspace_handle, + data_ptrs, + uids, + cached_plan->get_raw_desc(), + workspace_size); + return; + } + + auto plans = FindExecutionPlans(op_graph_pointer, + exhaustive_search, + deterministic, + data_ptrs, + uids, + handle, + workspace_handle); + + ExecutePlansAndCache(handle, + workspace_handle, + data_ptrs, + uids, + &plans, + exhaustive_search, + feature_vector, + plan_cache); + } + static cudnn_frontend::Operation MakePointwiseOp( cudnnPointwiseMode_t mode, cudnnDataType_t dtype, @@ -435,7 +477,7 @@ void CudnnConvBwdDataV8(const DenseTensor* dy_tensor, if (plan_cache_bwd_data.FindPlan(op_graph, handle)) { const cudnn_frontend::ExecutionPlan* cached_plan = nullptr; int64_t workspace_size = 0; - plan_cache_bwd_data.GetPlan( + plan_cache_bwd_data.GetPlanAndWorkspaceSize( op_graph, &cached_plan, &workspace_size, handle); helper::ExecutePlan(handle, workspace_handle, @@ -509,7 +551,7 @@ void CudnnConvBwdFilterV8(const DenseTensor* x_tensor, if (plan_cache_bwd_filter.FindPlan(op_graph, handle)) { const cudnn_frontend::ExecutionPlan* cached_plan = nullptr; int64_t workspace_size = 0; - plan_cache_bwd_filter.GetPlan( + plan_cache_bwd_filter.GetPlanAndWorkspaceSize( op_graph, &cached_plan, &workspace_size, handle); helper::ExecutePlan(handle, workspace_handle, diff --git a/paddle/phi/kernels/gpudnn/conv_kernel.cu b/paddle/phi/kernels/gpudnn/conv_kernel.cu index 6dc7fc9e6131d..65418673827cd 100644 --- a/paddle/phi/kernels/gpudnn/conv_kernel.cu +++ b/paddle/phi/kernels/gpudnn/conv_kernel.cu @@ -264,7 +264,8 @@ void ConvCudnnKernelImplV8(const DenseTensor* input_tensor, if (plan_cache.FindPlan(op_graph, handle)) { const cudnn_frontend::ExecutionPlan* cached_plan = nullptr; int64_t workspace_size = 0; - plan_cache.GetPlan(op_graph, &cached_plan, &workspace_size, handle); + plan_cache.GetPlanAndWorkspaceSize( + op_graph, &cached_plan, &workspace_size, handle); helper::ExecutePlan(handle, &workspace_handle, input_data, diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 079233a9c1676..86443907bd9aa 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -503,6 +503,10 @@ if(NOT WITH_GPU list(REMOVE_ITEM TEST_OPS test_build_strategy_fusion_group_pass) endif() +if(NOT WITH_CUDNN_FRONTEND) + list(REMOVE_ITEM TEST_OPS test_fused_scale_bias_relu_conv_bnstats_op) +endif() + # Some ops need to check results when gc is enabled # Currently, only ops that register NoNeedBufferVarsInference need to do this test set(TEST_OPS_WITH_GC diff --git a/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py b/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py new file mode 100644 index 0000000000000..cbed4e5b33fcf --- /dev/null +++ b/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py @@ -0,0 +1,239 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + + +import unittest + +import numpy as np +from eager_op_test import OpTest, skip_check_grad_ci + +import paddle +from paddle import nn +from paddle.fluid import core + + +def skip_unit_test(): + return ( + not paddle.is_compiled_with_cuda() + or paddle.device.cuda.get_device_capability()[0] < 8 + or paddle.get_cudnn_version() < 8800 + ) + + +skip_msg = ( + "only support with cuda and CUDNN 8.8 or later," + " and only Ampere or later devices are supported" +) + + +@skip_check_grad_ci(reason="no grap op") +@unittest.skipIf(skip_unit_test(), skip_msg) +class TestFusedScaleBiasReluConvBnstatsOp(OpTest): + def setUp(self): + self.__class__.op_type = "fused_scale_bias_relu_conv_bnstats" + self.dtype = np.float16 + self.outputs = None + self.padding_algorithm = "EXIPLICIT" + self.data_format = "NHWC" + self.groups = 1 + self.init_attr() + self.init_test_case() + self.rtol = 1e-5 + self.atol = 2e-2 + + self.attrs = { + 'fuse_prologue': self.fuse_prologue, + 'strides': self.stride, + 'paddings': self.pad, + 'dilations': self.dilations, + 'data_format': self.data_format, + 'padding_algorithm': self.padding_algorithm, + 'accumulation_count': self.accumulation_count, + 'momentum': self.momentum, + 'epsilon': self.epsilon, + 'exhaustive_search': self.exhaustive_search, + 'groups': self.groups, + } + + # prepare inputs + np.random.seed(0) + self.x_input = np.random.random(self.x_size).astype(self.dtype) + self.bias_input = np.random.random(self.in_channel_num).astype( + self.dtype + ) + self.scale_input = np.random.random(self.in_channel_num).astype( + self.dtype + ) + + self.x_input_prologue = self.x_input.astype(np.float32) + if self.fuse_prologue: + self.x_input_prologue *= self.scale_input.reshape( + (1, 1, 1, self.in_channel_num) + ).astype( + np.float32 + ) # scale + self.x_input_prologue += self.bias_input.reshape( + (1, 1, 1, self.in_channel_num) + ).astype( + np.float32 + ) # bias + self.x_input_prologue = np.maximum(self.x_input_prologue, 0) # relu + self.x_input_prologue = self.x_input_prologue.astype(self.dtype) + + paddle.disable_static() + paddle.seed(0) + paddle.set_default_dtype(self.dtype) + + self.conv = nn.Conv2D( + in_channels=self.x_size[-1], + out_channels=self.filter_size[0], + kernel_size=self.filter_size[-1], + stride=self.stride, + padding=self.pad, + groups=self.groups, + bias_attr=False, + data_format=self.data_format, + ) + + self.bn = nn.BatchNorm( + self.filter_size[0], + momentum=self.momentum, + epsilon=self.epsilon, + data_layout=self.data_format, + ) + + self.w_input = self.conv.weight.numpy().astype(self.dtype) + self.bn_scale_input = self.bn.weight.numpy() + self.bn_bias_input = self.bn.bias.numpy() + self.bn_running_mean_input = self.bn._mean.numpy() + self.bn_running_var_input = self.bn._variance.numpy() + + ( + y_ref, + running_mean_out_ref, + running_var_out_ref, + saved_mean_out_ref, + saved_invvar_out_ref, + eqscale_ref, + eqbias_ref, + ) = self.calc_ref() + + self.inputs = { + 'x': self.x_input, + 'w': self.w_input, + 'bn_scale': self.bn_scale_input, + 'bn_bias': self.bn_bias_input, + 'input_running_mean': self.bn_running_mean_input, + 'input_running_var': self.bn_running_var_input, + } + if self.fuse_prologue: + extra_inputs = { + 'bias': self.bias_input, + 'scale': self.scale_input, + } + self.inputs.update(extra_inputs) + + self.outputs = { + 'out': y_ref, + 'out_running_mean': running_mean_out_ref, + 'out_running_var': running_var_out_ref, + 'saved_mean': saved_mean_out_ref, + 'saved_var': saved_invvar_out_ref, + 'eq_scale': eqscale_ref, + 'eq_bias': eqbias_ref, + } + + def calc_ref(self): + # Calculate normal (scale + bias + relu +) Conv + BN + x_input_np = self.x_input + if self.fuse_prologue: + x_input_np = self.x_input_prologue + x_tensor = paddle.to_tensor(x_input_np, stop_gradient=False) + after_conv = self.conv(x_tensor) + after_bn = self.bn(after_conv) + # Calculate reference for saved_mean and saved_invvar + after_conv_np = ( + after_conv.numpy() + .astype(np.float32) + .reshape((-1, after_conv.shape[-1])) + ) + mean_np = after_conv_np.mean(axis=0) + var_np = after_conv_np.var(axis=0) + invstd_np = 1 / np.sqrt(var_np + self.epsilon) + # Calculate reference for eqscale and eqbias + eqscale_np = self.bn_scale_input * invstd_np + eqbias_np = ( + self.bn_bias_input - self.bn_scale_input * mean_np * invstd_np + ) + return ( + after_conv.numpy().astype(self.dtype), + self.bn._mean.numpy(), + self.bn._variance.numpy(), + mean_np, + invstd_np, + eqscale_np, + eqbias_np, + ) + + def has_cuda(self): + return core.is_compiled_with_cuda() + + def test_check_output(self): + if self.has_cuda(): + place = core.CUDAPlace(0) + self.check_output_with_place( + place, atol=self.atol, rtol=self.rtol, check_dygraph=False + ) + + def init_test_case(self): + self.pad = [0, 0] + self.stride = [1, 1] + self.dilations = [1, 1] + + self.x_size = [8, 16, 16, 32] # NHWC + self.filter_size = [64, 32, 1, 1] + self.y_size = [8, 16, 16, 64] + self.in_channel_num = self.x_size[-1] + self.out_channel_num = self.y_size[-1] + self.scale_size = [self.in_channel_num] + self.bn_size = [self.out_channel_num] + self.momentum = 0.9 + self.epsilon = 1e-5 + self.accumulation_count = ( + self.y_size[0] * self.y_size[1] * self.y_size[2] + ) + + def init_attr(self): + self.fuse_prologue = True + self.exhaustive_search = False + + +class TestFusedScaleBiasReluConvBnstatsOpNoPrologue( + TestFusedScaleBiasReluConvBnstatsOp +): + def init_attr(self): + self.fuse_prologue = False + self.exhaustive_search = False + + +class TestFusedScaleBiasReluConvBnstatsOpExhaustive( + TestFusedScaleBiasReluConvBnstatsOp +): + def init_attr(self): + self.fuse_prologue = True + self.exhaustive_search = True + + +if __name__ == '__main__': + unittest.main() diff --git a/test/white_list/op_accuracy_white_list.py b/test/white_list/op_accuracy_white_list.py index 12a97a160aab6..49b501e765b54 100644 --- a/test/white_list/op_accuracy_white_list.py +++ b/test/white_list/op_accuracy_white_list.py @@ -92,6 +92,7 @@ NO_FP16_COMPARED_WITH_FP32_OP_LIST = [ 'fake_quantize_moving_average_abs_max', + 'fused_scale_bias_relu_conv_bnstats', 'p_norm', ] From 0a15b0db9f9d476897ce572040fe9efb2e94a52c Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:09:42 +0800 Subject: [PATCH 044/194] [xdoctest] reformat example code with google style in No.36-43 (#56440) --- python/paddle/nn/functional/activation.py | 516 +++++++++------- python/paddle/nn/functional/common.py | 564 ++++++++++-------- python/paddle/nn/functional/conv.py | 118 ++-- python/paddle/nn/functional/distance.py | 15 +- python/paddle/nn/functional/extension.py | 126 ++-- .../paddle/nn/functional/flash_attention.py | 20 +- python/paddle/nn/initializer/uniform.py | 45 +- python/paddle/nn/initializer/xavier.py | 89 +-- 8 files changed, 843 insertions(+), 650 deletions(-) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 26d81c7e38d1c..dbbed03b3aac4 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -49,12 +49,15 @@ def celu(x, alpha=1.0, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - x = paddle.to_tensor([[-1., 6.], [1., 15.6]]) - out = F.celu(x, alpha=0.2) - # [[-0.19865242, 6. ], - # [ 1. , 15.60000038]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x = paddle.to_tensor([[-1., 6.], [1., 15.6]]) + >>> out = F.celu(x, alpha=0.2) + >>> print(out) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.19865242, 6. ], + [ 1. , 15.60000038]]) """ if alpha == 0: raise ZeroDivisionError("alpha cannot be 0 for celu") @@ -100,13 +103,15 @@ def elu(x, alpha=1.0, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([[-1., 6.], [1., 15.6]]) - out = F.elu(x, alpha=0.2) - # [[-0.12642411 6. ] - # [ 1. 15.6 ]] + >>> x = paddle.to_tensor([[-1., 6.], [1., 15.6]]) + >>> out = F.elu(x, alpha=0.2) + >>> print(out) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.12642412, 6. ], + [ 1. , 15.60000038]]) """ if in_dynamic_mode(): @@ -168,16 +173,20 @@ def gelu(x, approximate=False, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - x = paddle.to_tensor([[-1, 0.5], [1, 1.5]]) - out1 = F.gelu(x) - # [[-0.15865529, 0.34573123], - # [ 0.84134471, 1.39978933]] - out2 = F.gelu(x, True) - # [[-0.15880799, 0.34571400], - # [ 0.84119201, 1.39957154]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x = paddle.to_tensor([[-1, 0.5], [1, 1.5]]) + >>> out1 = F.gelu(x) + >>> print(out1) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.15865529, 0.34573123], + [ 0.84134471, 1.39978933]]) + >>> out2 = F.gelu(x, True) + >>> print(out2) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.15880796, 0.34571400], + [ 0.84119201, 1.39957154]]) """ if in_dynamic_mode(): @@ -223,11 +232,15 @@ def hardshrink(x, threshold=0.5, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x = paddle.to_tensor([-1, 0.3, 2.5]) + >>> out = F.hardshrink(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-1. , 0. , 2.50000000]) - x = paddle.to_tensor([-1, 0.3, 2.5]) - out = F.hardshrink(x) # [-1., 0., 2.5] """ if in_dynamic_mode(): @@ -274,11 +287,14 @@ def hardtanh(x, min=-1.0, max=1.0, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-1.5, 0.3, 2.5]) - out = F.hardtanh(x) # [-1., 0.3, 1.] + >>> x = paddle.to_tensor([-1.5, 0.3, 2.5]) + >>> out = F.hardtanh(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-1. , 0.30000001, 1. ]) """ if in_dynamic_mode(): @@ -338,11 +354,14 @@ def hardsigmoid(x, slope=0.1666667, offset=0.5, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-4., 5., 1.]) - out = F.hardsigmoid(x) # [0., 1., 0.666667] + >>> x = paddle.to_tensor([-4., 5., 1.]) + >>> out = F.hardsigmoid(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 1. , 0.66666669]) """ if in_dynamic_mode(): @@ -390,11 +409,14 @@ def hardswish(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-4., 5., 1.]) - out = F.hardswish(x) # [0., 5., 0.666667] + >>> x = paddle.to_tensor([-4., 5., 1.]) + >>> out = F.hardswish(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0. , 5. , 0.66666669]) """ if in_dynamic_mode(): return _C_ops.hardswish(x) @@ -442,13 +464,14 @@ def leaky_relu(x, negative_slope=0.01, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-2., 0., 1.]) - out = F.leaky_relu(x) - print(out) - # [-0.02, 0., 1.] + >>> x = paddle.to_tensor([-2., 0., 1.]) + >>> out = F.leaky_relu(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.02000000, 0. , 1. ]) """ if in_dynamic_mode(): @@ -502,25 +525,26 @@ def prelu(x, weight, data_format="NCHW", name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - data = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0], - [ 3.0, -4.0, 5.0, -6.0], - [-7.0, -8.0, 8.0, 9.0]], - [[ 1.0, -2.0, -3.0, 4.0], - [-5.0, 6.0, 7.0, -8.0], - [ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32') - - w = paddle.to_tensor([0.25], dtype='float32') - out = F.prelu(data, w) - print(out) - # [[[[-0.5 , 3. , -1. , 5. ], - # [ 3. , -1. , 5. , -1.5 ], - # [-1.75, -2. , 8. , 9. ]], - # [[ 1. , -0.5 , -0.75, 4. ], - # [-1.25, 6. , 7. , -2. ], - # [ 6. , 7. , 8. , 9. ]]]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> data = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0], + ... [ 3.0, -4.0, 5.0, -6.0], + ... [-7.0, -8.0, 8.0, 9.0]], + ... [[ 1.0, -2.0, -3.0, 4.0], + ... [-5.0, 6.0, 7.0, -8.0], + ... [ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32') + + >>> w = paddle.to_tensor([0.25], dtype='float32') + >>> out = F.prelu(data, w) + >>> print(out) + Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[-0.50000000, 3. , -1. , 5. ], + [ 3. , -1. , 5. , -1.50000000], + [-1.75000000, -2. , 8. , 9. ]], + [[ 1. , -0.50000000, -0.75000000, 4. ], + [-1.25000000, 6. , 7. , -2. ], + [ 6. , 7. , 8. , 9. ]]]]) """ assert ( len(weight.shape) == 0 or len(weight.shape) == 1 @@ -634,24 +658,24 @@ def rrelu(x, lower=1.0 / 8.0, upper=1.0 / 3.0, training=True, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input_tensor = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0], - [ 3.0, -4.0, 5.0, -6.0], - [-7.0, -8.0, 8.0, 9.0]], - [[ 1.0, -2.0, -3.0, 4.0], - [-5.0, 6.0, 7.0, -8.0], - [ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32') - - out = F.rrelu(input_tensor, 0.1, 0.3) - print(out) - #[[[[-0.20000899 3. -0.8810822 5. ] - # [ 3. -0.55175185 5. -1.0776101 ] - # [-1.0680687 -1.9896201 8. 9. ]] - # [[ 1. -0.5238267 -0.65515125 4. ] - # [-1.3766339 6. 7. -2.3465784 ] - # [ 6. 7. 8. 9. ]]]] + >>> import paddle + >>> import paddle.nn.functional as F + >>> paddle.seed(1) + >>> input_tensor = paddle.to_tensor([[[[-2.0, 3.0, -4.0, 5.0], + ... [ 3.0, -4.0, 5.0, -6.0], + ... [-7.0, -8.0, 8.0, 9.0]], + ... [[ 1.0, -2.0, -3.0, 4.0], + ... [-5.0, 6.0, 7.0, -8.0], + ... [ 6.0, 7.0, 8.0, 9.0]]]], dtype='float32') + >>> out = F.rrelu(input_tensor, 0.1, 0.3) + >>> print(out) + Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[-0.20715050, 3. , -1.01193857, 5. ], + [ 3. , -0.94084597, 5. , -0.65544695], + [-1.24268556, -2.34339547, 8. , 9. ]], + [[ 1. , -0.44942653, -0.68969047, 4. ], + [-1.03736508, 6. , 7. , -0.95799232], + [ 6. , 7. , 8. , 9. ]]]]) """ if not isinstance(lower, float) or not isinstance(upper, float): raise TypeError( @@ -722,13 +746,14 @@ def relu(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-2, 0, 1], dtype='float32') - out = F.relu(x) - print(out) - # [0., 0., 1.] + >>> x = paddle.to_tensor([-2, 0, 1], dtype='float32') + >>> out = F.relu(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0., 0., 1.]) """ if in_dynamic_mode(): @@ -770,11 +795,14 @@ def log_sigmoid(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) - out = F.log_sigmoid(x) # [-0.313262 -0.126928 -0.0485874 -0.0181499] + >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) + >>> out = F.log_sigmoid(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.31326166, -0.12692805, -0.04858733, -0.01814996]) """ if in_dynamic_mode(): @@ -830,20 +858,25 @@ def maxout(x, groups, axis=1, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - x = paddle.rand([1, 2, 3, 4]) - # [[[[0.5002636 0.22272532 0.17402348 0.2874594 ] - # [0.95313174 0.6228939 0.7129065 0.7087491 ] - # [0.02879342 0.88725346 0.61093384 0.38833922]] - # [[0.5231306 0.03807496 0.91661984 0.15602879] - # [0.666127 0.616567 0.30741522 0.24044901] - # [0.7142536 0.7351477 0.31588817 0.23782359]]]] - out = F.maxout(x, groups=2) - # [[[[0.5231306 0.22272532 0.91661984 0.2874594 ] - # [0.95313174 0.6228939 0.7129065 0.7087491 ] - # [0.7142536 0.88725346 0.61093384 0.38833922]]]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> paddle.seed(2023) + >>> x = paddle.rand([1, 2, 3, 4]) + >>> print(x) + Tensor(shape=[1, 2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[0.86583614, 0.52014720, 0.25960937, 0.90525323], + [0.42400089, 0.40641287, 0.97020894, 0.74437362], + [0.51785129, 0.73292869, 0.97786582, 0.04315904]], + [[0.42639419, 0.71958369, 0.20811461, 0.19731510], + [0.38424349, 0.14603184, 0.22713774, 0.44607511], + [0.21657862, 0.67685395, 0.46460176, 0.92382854]]]]) + >>> out = F.maxout(x, groups=2) + >>> print(out) + Tensor(shape=[1, 1, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[0.86583614, 0.71958369, 0.25960937, 0.90525323], + [0.42400089, 0.40641287, 0.97020894, 0.74437362], + [0.51785129, 0.73292869, 0.97786582, 0.92382854]]]]) """ if in_dynamic_mode(): return _C_ops.maxout(x, groups, axis) @@ -888,13 +921,14 @@ def relu6(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-1, 0.3, 6.5]) - out = F.relu6(x) - print(out) - # [0, 0.3, 6] + >>> x = paddle.to_tensor([-1, 0.3, 6.5]) + >>> out = F.relu6(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 0.30000001, 6. ]) """ threshold = 6.0 if in_dynamic_mode(): @@ -945,13 +979,15 @@ def selu( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([[0.0, 1.0],[2.0, 3.0]]) - out = F.selu(x) - print(out) - # [[0, 1.050701],[2.101402, 3.152103]] + >>> x = paddle.to_tensor([[0.0, 1.0],[2.0, 3.0]]) + >>> out = F.selu(x) + >>> print(out) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 1.05070102], + [2.10140204, 3.15210295]]) """ if scale <= 1.0: raise ValueError( @@ -1000,11 +1036,14 @@ def silu(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) - out = F.silu(x) # [ 0.731059, 1.761594, 2.857722, 3.928055 ] + >>> x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) + >>> out = F.silu(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.73105860, 1.76159406, 2.85772228, 3.92805505]) """ if in_dynamic_mode(): @@ -1111,25 +1150,35 @@ def softmax(x, axis=-1, dtype=None, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0], - [3.0, 4.0, 5.0, 6.0], - [7.0, 8.0, 8.0, 9.0]], - [[1.0, 2.0, 3.0, 4.0], - [5.0, 6.0, 7.0, 8.0], - [6.0, 7.0, 8.0, 9.0]]],dtype='float32') - out1 = F.softmax(x) - out2 = F.softmax(x, dtype='float64') - # out1's data type is float32; out2's data type is float64 - # out1 and out2's value is as follows: - # [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426], - # [0.0320586 , 0.08714432, 0.23688282, 0.64391426], - # [0.07232949, 0.19661193, 0.19661193, 0.53444665]], - # [[0.0320586 , 0.08714432, 0.23688282, 0.64391426], - # [0.0320586 , 0.08714432, 0.23688282, 0.64391426], - # [0.0320586 , 0.08714432, 0.23688282, 0.64391426]]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0], + ... [3.0, 4.0, 5.0, 6.0], + ... [7.0, 8.0, 8.0, 9.0]], + ... [[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [6.0, 7.0, 8.0, 9.0]]],dtype='float32') + >>> out1 = F.softmax(x) + >>> out2 = F.softmax(x, dtype='float64') + >>> #out1's data type is float32; out2's data type is float64 + >>> #out1 and out2's value is as follows: + >>> print(out1) + >>> print(out2) + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.03205860, 0.08714432, 0.23688284, 0.64391428], + [0.03205860, 0.08714432, 0.23688284, 0.64391428], + [0.07232949, 0.19661194, 0.19661194, 0.53444666]], + [[0.03205860, 0.08714432, 0.23688284, 0.64391428], + [0.03205860, 0.08714432, 0.23688284, 0.64391428], + [0.03205860, 0.08714432, 0.23688284, 0.64391428]]]) + Tensor(shape=[2, 3, 4], dtype=float64, place=Place(cpu), stop_gradient=True, + [[[0.03205860, 0.08714432, 0.23688282, 0.64391426], + [0.03205860, 0.08714432, 0.23688282, 0.64391426], + [0.07232949, 0.19661193, 0.19661193, 0.53444665]], + [[0.03205860, 0.08714432, 0.23688282, 0.64391426], + [0.03205860, 0.08714432, 0.23688282, 0.64391426], + [0.03205860, 0.08714432, 0.23688282, 0.64391426]]]) """ if (dtype is not None) and (not isinstance(dtype, core.VarDesc.VarType)): @@ -1214,11 +1263,14 @@ def softplus(x, beta=1, threshold=20, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3], dtype='float32') - out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355] + >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3], dtype='float32') + >>> out = F.softplus(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.51301527, 0.59813893, 0.74439669, 0.85435522]) """ if in_dynamic_mode(): @@ -1264,14 +1316,14 @@ def softshrink(x, threshold=0.5, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-0.9, -0.2, 0.1, 0.8]) - out = F.softshrink(x) - print(out) - # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [-0.39999998, 0. , 0. , 0.30000001]) + >>> x = paddle.to_tensor([-0.9, -0.2, 0.1, 0.8]) + >>> out = F.softshrink(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.39999998, 0. , 0. , 0.30000001]) """ if threshold < 0: raise ValueError( @@ -1315,14 +1367,14 @@ def softsign(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) - out = F.softsign(x) - print(out) - # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [-0.28571430, -0.16666666, 0.09090909, 0.23076925]) + >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) + >>> out = F.softsign(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.28571430, -0.16666666, 0.09090909, 0.23076925]) """ if in_dynamic_mode(): return _C_ops.softsign(x) @@ -1354,14 +1406,14 @@ def swish(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-2., 0., 1.]) - out = F.swish(x) - print(out) - # Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [-0.23840584, 0. , 0.73105854]) + >>> x = paddle.to_tensor([-2., 0., 1.]) + >>> out = F.swish(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.23840584, 0. , 0.73105860]) """ if in_dynamic_mode(): return _C_ops.swish(x) @@ -1403,11 +1455,14 @@ def mish(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-5., 0., 5.]) - out = F.mish(x) # [-0.03357624, 0., 4.99955208] + >>> x = paddle.to_tensor([-5., 0., 5.]) + >>> out = F.mish(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.03357624, 0. , 4.99955177]) """ if in_dynamic_mode(): return _C_ops.mish(x, 20) @@ -1439,14 +1494,14 @@ def tanhshrink(x, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) - out = F.tanhshrink(x) - print(out) - # Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [-0.02005106, -0.00262468, 0.00033200, 0.00868741]) + >>> x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3]) + >>> out = F.tanhshrink(x) + >>> print(out) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [-0.02005100, -0.00262472, 0.00033201, 0.00868741]) """ if in_dynamic_mode(): return _C_ops.tanh_shrink(x) @@ -1488,14 +1543,14 @@ def thresholded_relu(x, threshold=1.0, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.to_tensor([2., 0., 1.]) - out = F.thresholded_relu(x) - print(out) - # Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [2., 0., 0.]) + >>> x = paddle.to_tensor([2., 0., 1.]) + >>> out = F.thresholded_relu(x) + >>> print(out) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [2., 0., 0.]) """ if in_dynamic_mode(): @@ -1561,26 +1616,35 @@ def log_softmax(x, axis=-1, dtype=None, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - x = [[[-2.0, 3.0, -4.0, 5.0], - [3.0, -4.0, 5.0, -6.0], - [-7.0, -8.0, 8.0, 9.0]], - [[1.0, -2.0, -3.0, 4.0], - [-5.0, 6.0, 7.0, -8.0], - [6.0, 7.0, 8.0, 9.0]]] - x = paddle.to_tensor(x) - out1 = F.log_softmax(x) - out2 = F.log_softmax(x, dtype='float64') - # out1's data type is float32; out2's data type is float64 - # out1 and out2's value is as follows: - # [[[ -7.1278396 -2.1278396 -9.127839 -0.12783948] - # [ -2.1270514 -9.127051 -0.12705144 -11.127051 ] - # [-16.313261 -17.313261 -1.3132617 -0.31326184]] - # [[ -3.0518122 -6.051812 -7.051812 -0.051812 ] - # [-12.313267 -1.3132664 -0.3132665 -15.313267 ] - # [ -3.4401896 -2.4401896 -1.4401896 -0.44018966]]] + >>> import paddle + >>> import paddle.nn.functional as F + >>> x = [[[-2.0, 3.0, -4.0, 5.0], + ... [3.0, -4.0, 5.0, -6.0], + ... [-7.0, -8.0, 8.0, 9.0]], + ... [[1.0, -2.0, -3.0, 4.0], + ... [-5.0, 6.0, 7.0, -8.0], + ... [6.0, 7.0, 8.0, 9.0]]] + >>> x = paddle.to_tensor(x) + >>> out1 = F.log_softmax(x) + >>> out2 = F.log_softmax(x, dtype='float64') + >>> #out1's data type is float32; out2's data type is float64 + >>> #out1 and out2's value is as follows: + >>> print(out1) + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[-7.12783957 , -2.12783957 , -9.12783909 , -0.12783945 ], + [-2.12705135 , -9.12705135 , -0.12705141 , -11.12705135], + [-16.31326103, -17.31326103, -1.31326187 , -0.31326184 ]], + [[-3.05181193 , -6.05181217 , -7.05181217 , -0.05181199 ], + [-12.31326675, -1.31326652 , -0.31326646 , -15.31326675], + [-3.44018984 , -2.44018984 , -1.44018972 , -0.44018975 ]]]) + >>> print(out2) + Tensor(shape=[2, 3, 4], dtype=float64, place=Place(cpu), stop_gradient=True, + [[[-7.12783948 , -2.12783948 , -9.12783948 , -0.12783948 ], + [-2.12705141 , -9.12705141 , -0.12705141 , -11.12705141], + [-16.31326180, -17.31326180, -1.31326180 , -0.31326180 ]], + [[-3.05181198 , -6.05181198 , -7.05181198 , -0.05181198 ], + [-12.31326640, -1.31326640 , -0.31326640 , -15.31326640], + [-3.44018970 , -2.44018970 , -1.44018970 , -0.44018970 ]]]) """ if (dtype is not None) and (not isinstance(dtype, core.VarDesc.VarType)): @@ -1655,17 +1719,16 @@ def glu(x, axis=-1, name=None): Examples: .. code-block:: python - import paddle - from paddle.nn import functional as F - - x = paddle.to_tensor( - [[-0.22014759, -1.76358426, 0.80566144, 0.04241343], - [-1.94900405, -1.89956081, 0.17134808, -1.11280477]] - ) - print(F.glu(x)) - # Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[-0.15216254, -0.90048921], - # [-1.05778778, -0.46985325]]) + >>> import paddle + >>> from paddle.nn import functional as F + >>> x = paddle.to_tensor( + ... [[-0.22014759, -1.76358426, 0.80566144, 0.04241343], + ... [-1.94900405, -1.89956081, 0.17134808, -1.11280477]] + ... ) + >>> print(F.glu(x)) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.15216254, -0.90048921], + [-1.05778778, -0.46985325]]) """ check_variable_and_dtype( @@ -1727,18 +1790,19 @@ def gumbel_softmax(x, temperature=1.0, hard=False, axis=-1, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - logits = paddle.randn([4, 6]) - temperature = 0.01 - gumbel_softmax = F.gumbel_softmax(logits, temperature) - print(gumbel_softmax) - # out's value is as follows: - # [[0.00000001, 1. , 0.00000000, 0.00000000, 0.00000006, 0.00000000], - # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 1. ], - # [0.00000062, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.99999940], - # [0.00000000, 0.00000000, 0.00000000, 0.00001258, 0.99998736, 0.00000000]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> paddle.seed(2023) + >>> logits = paddle.randn([4, 6]) + >>> temperature = 0.01 + >>> gumbel_softmax = F.gumbel_softmax(logits, temperature) + >>> print(gumbel_softmax) + Tensor(shape=[4, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.00000000, 1. , 0.00000000, 0.00000000, 0.00000000, 0.00000000], + [0.00000000, 0.00000000, 1. , 0.00000000, 0.00000000, 0.00000000], + [0.00000000, 0.00000004, 0.00000000, 0.00000000, 1. , 0.00000000], + [0.00000000, 1. , 0.00000000, 0.00000000, 0.00000000, 0.00000000]]) """ if in_dynamic_mode(): diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index e513fb0670ef7..bc43e73c4163d 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -95,11 +95,11 @@ def unfold(x, kernel_sizes, strides=1, paddings=0, dilations=1, name=None): .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.randn((100,3,224,224)) - y = F.unfold(x, [3, 3], 1, 1, 1) + >>> x = paddle.randn((100,3,224,224)) + >>> y = F.unfold(x, [3, 3], 1, 1, 1) """ helper = LayerHelper("unfold", **locals()) @@ -348,23 +348,21 @@ def interpolate( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input_data = paddle.randn(shape=(2,3,6,10)).astype(paddle.float32) - output_1 = F.interpolate(x=input_data, size=[12,12]) - print(output_1.shape) - # [2L, 3L, 12L, 12L] - - # given scale - output_2 = F.interpolate(x=input_data, scale_factor=[2,1]) - print(output_2.shape) - # [2L, 3L, 12L, 10L] - - # bilinear interp - output_3 = F.interpolate(x=input_data, scale_factor=[2,1], mode="bilinear") - print(output_2.shape) - # [2L, 3L, 12L, 10L] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> input_data = paddle.randn(shape=(2,3,6,10)).astype(paddle.float32) + >>> output_1 = F.interpolate(x=input_data, size=[12,12]) + >>> print(output_1.shape) + [2, 3, 12, 12] + >>> # given scale + >>> output_2 = F.interpolate(x=input_data, scale_factor=[2,1]) + >>> print(output_2.shape) + [2, 3, 12, 10] + >>> # bilinear interp + >>> output_3 = F.interpolate(x=input_data, scale_factor=[2,1], mode="bilinear") + >>> print(output_2.shape) + [2, 3, 12, 10] """ data_format = data_format.upper() resample = mode.upper() @@ -877,15 +875,14 @@ def upsample( Examples: .. code-block:: python - import paddle - import paddle.nn as nn - - input_data = paddle.randn(shape=(2,3,6,10)).astype(paddle.float32) - upsample_out = paddle.nn.Upsample(size=[12,12]) + >>> import paddle + >>> import paddle.nn as nn - output = upsample_out(x=input_data) - print(output.shape) - # [2L, 3L, 12L, 12L] + >>> input_data = paddle.randn(shape=(2,3,6,10)).astype(paddle.float32) + >>> upsample_out = paddle.nn.Upsample(size=[12,12]) + >>> output = upsample_out(x=input_data) + >>> print(output.shape) + [2, 3, 12, 12] """ return interpolate( @@ -913,17 +910,16 @@ def bilinear(x1, x2, weight, bias=None, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x1 = paddle.randn((5, 5)).astype(paddle.float32) - x2 = paddle.randn((5, 4)).astype(paddle.float32) - w = paddle.randn((1000, 5, 4)).astype(paddle.float32) - b = paddle.randn((1, 1000)).astype(paddle.float32) - - result = F.bilinear(x1, x2, w, b) - print(result.shape) - # [5, 1000] + >>> x1 = paddle.randn((5, 5)).astype(paddle.float32) + >>> x2 = paddle.randn((5, 4)).astype(paddle.float32) + >>> w = paddle.randn((1000, 5, 4)).astype(paddle.float32) + >>> b = paddle.randn((1, 1000)).astype(paddle.float32) + >>> result = F.bilinear(x1, x2, w, b) + >>> print(result.shape) + [5, 1000] """ if in_dynamic_mode(): @@ -1061,39 +1057,38 @@ def dropout( .. code-block:: python - import paddle - - x = paddle.to_tensor([[1,2,3], [4,5,6]]).astype(paddle.float32) - y_train = paddle.nn.functional.dropout(x, 0.5) - y_test = paddle.nn.functional.dropout(x, 0.5, training=False) - y_0 = paddle.nn.functional.dropout(x, axis=0) - y_1 = paddle.nn.functional.dropout(x, axis=1) - y_01 = paddle.nn.functional.dropout(x, axis=[0,1]) - print(x) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[1., 2., 3.], - # [4., 5., 6.]]) - print(y_train) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[2. , 0. , 6. ], - # [8. , 0. , 12.]]) - print(y_test) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[1., 2., 3.], - # [4., 5., 6.]]) - print(y_0) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[0. , 0. , 0. ], - # [8. , 10., 12.]]) - print(y_1) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[2. , 0. , 6. ], - # [8. , 0. , 12.]]) - print(y_01) - # Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[0. , 0. , 0. ], - # [8. , 0. , 12.]]) - + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.to_tensor([[1,2,3], [4,5,6]]).astype(paddle.float32) + >>> y_train = paddle.nn.functional.dropout(x, 0.5) + >>> y_test = paddle.nn.functional.dropout(x, 0.5, training=False) + >>> y_0 = paddle.nn.functional.dropout(x, axis=0) + >>> y_1 = paddle.nn.functional.dropout(x, axis=1) + >>> y_01 = paddle.nn.functional.dropout(x, axis=[0,1]) + >>> print(x) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1., 2., 3.], + [4., 5., 6.]]) + >>> print(y_train) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[2., 4., 0.], + [8., 0., 0.]]) + >>> print(y_test) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1., 2., 3.], + [4., 5., 6.]]) + >>> print(y_0) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[2., 4., 6.], + [8. , 10., 12.]]) + >>> print(y_1) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[2. , 4. , 6. ], + [8. , 10., 12.]]) + >>> print(y_01) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0., 0., 6.], + [0., 0., 0.]]) """ if not isinstance(p, (float, int, Variable)): raise TypeError("p argument should be a number or Variable") @@ -1258,17 +1253,106 @@ def dropout2d(x, p=0.5, training=True, data_format='NCHW', name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn(shape=(2, 3, 4, 5)).astype(paddle.float32) - y_train = paddle.nn.functional.dropout2d(x) #train - y_test = paddle.nn.functional.dropout2d(x, training=False) #test - for i in range(2): - for j in range(3): - print(x[i,j,:,:]) - print(y_train[i,j,:,:]) # may all 0 - print(y_test[i,j,:,:]) - + >>> import paddle + >>> paddle.seed(1) + >>> x = paddle.randn(shape=(2, 3, 4, 5)).astype(paddle.float32) + >>> y_train = paddle.nn.functional.dropout2d(x) #train + >>> y_test = paddle.nn.functional.dropout2d(x, training=False) #test + >>> for i in range(2): + ... for j in range(3): + ... print(x[i,j,:,:]) + ... print(y_train[i,j,:,:]) # may all 0 + ... print(y_test[i,j,:,:]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.30557564, 0.11855337, 0.41220093, -0.09968963, 1.50014710], + [ 1.24004936, -0.92485696, 0.08612321, 1.15149164, -0.09276631], + [ 1.22873247, -1.46587241, -1.30802727, 0.19496460, 1.73776841], + [ 0.40092674, 0.67630458, 0.72265440, 1.31720388, -1.41899264]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.61115128, 0.23710674, 0.82440186, -0.19937925, 3.00029421], + [ 2.48009872, -1.84971392, 0.17224643, 2.30298328, -0.18553263], + [ 2.45746493, -2.93174481, -2.61605453, 0.38992921, 3.47553682], + [ 0.80185348, 1.35260916, 1.44530880, 2.63440776, -2.83798528]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.30557564, 0.11855337, 0.41220093, -0.09968963, 1.50014710], + [ 1.24004936, -0.92485696, 0.08612321, 1.15149164, -0.09276631], + [ 1.22873247, -1.46587241, -1.30802727, 0.19496460, 1.73776841], + [ 0.40092674, 0.67630458, 0.72265440, 1.31720388, -1.41899264]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.88350385, -1.14767575, 0.51043051, -0.10051888, -0.61305630], + [-0.12084112, 0.48506257, -1.13189507, 0.62806708, -0.80003673], + [ 0.51513153, -0.08890446, 0.22753835, 0.11557858, 0.78117645], + [ 1.47505593, 0.84618902, -0.38528305, -1.05887091, 0.16592593]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 1.76700771, -2.29535151, 1.02086103, -0.20103776, -1.22611260], + [-0.24168225, 0.97012514, -2.26379013, 1.25613415, -1.60007346], + [ 1.03026307, -0.17780893, 0.45507669, 0.23115715, 1.56235290], + [ 2.95011187, 1.69237804, -0.77056611, -2.11774182, 0.33185187]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.88350385, -1.14767575, 0.51043051, -0.10051888, -0.61305630], + [-0.12084112, 0.48506257, -1.13189507, 0.62806708, -0.80003673], + [ 0.51513153, -0.08890446, 0.22753835, 0.11557858, 0.78117645], + [ 1.47505593, 0.84618902, -0.38528305, -1.05887091, 0.16592593]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-1.46668839, -0.38117948, 1.18678427, 0.38740095, 0.29117522], + [-0.13538910, -0.14527084, -0.04912176, -0.26063353, 0.23640174], + [ 0.45643106, 0.60587281, -1.03242552, -0.45319262, -1.57911122], + [-0.08732958, -0.75898546, 0.14563090, -1.73751652, -0.89109969]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0., -0., 0. , 0. , 0. ], + [-0., -0., -0., -0., 0. ], + [0. , 0. , -0., -0., -0.], + [-0., -0., 0. , -0., -0.]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-1.46668839, -0.38117948, 1.18678427, 0.38740095, 0.29117522], + [-0.13538910, -0.14527084, -0.04912176, -0.26063353, 0.23640174], + [ 0.45643106, 0.60587281, -1.03242552, -0.45319262, -1.57911122], + [-0.08732958, -0.75898546, 0.14563090, -1.73751652, -0.89109969]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.32110816, -0.76044011, 0.34456784, -0.39410326, 0.37896338], + [ 0.52747023, 0.72711533, 0.29204839, 0.72493637, 0.31128070], + [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693], + [ 0.96669912, 0.43670532, -0.38109761, 0.78405094, -2.17882323]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0., -0., 0. , -0., 0. ], + [0. , 0. , 0. , 0. , 0. ], + [0. , -0., -0., -0., -0.], + [0. , 0. , -0., 0. , -0.]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.32110816, -0.76044011, 0.34456784, -0.39410326, 0.37896338], + [ 0.52747023, 0.72711533, 0.29204839, 0.72493637, 0.31128070], + [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693], + [ 0.96669912, 0.43670532, -0.38109761, 0.78405094, -2.17882323]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.17168395, 0.45112833, 0.63307828, 2.38763475, -1.27247131], + [ 0.56171960, -1.09584677, 0.38300961, -0.57512099, 0.31011426], + [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209], + [ 2.22819090, 1.12403083, -0.04198794, -1.51167727, -0.42699185]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 0. , 0. , 0. , -0.], + [0. , -0., 0. , -0., 0. ], + [-0., -0., -0., -0., -0.], + [0. , 0. , -0., -0., -0.]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.17168395, 0.45112833, 0.63307828, 2.38763475, -1.27247131], + [ 0.56171960, -1.09584677, 0.38300961, -0.57512099, 0.31011426], + [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209], + [ 2.22819090, 1.12403083, -0.04198794, -1.51167727, -0.42699185]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704], + [ 1.04561794, 1.08428383, -0.52219963, -1.56003857, 0.89213932], + [-0.16578521, 0.14524542, -0.45563069, 0.48180851, 1.35843253], + [ 1.07669640, -0.84535235, -1.18651557, 0.79144061, -0.45565742]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , -0., -0., -0., -0.], + [0. , 0. , -0., -0., 0. ], + [-0., 0. , -0., 0. , 0. ], + [0. , -0., -0., 0. , -0.]]) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704], + [ 1.04561794, 1.08428383, -0.52219963, -1.56003857, 0.89213932], + [-0.16578521, 0.14524542, -0.45563069, 0.48180851, 1.35843253], + [ 1.07669640, -0.84535235, -1.18651557, 0.79144061, -0.45565742]]) """ input_shape = x.shape if len(input_shape) != 4: @@ -1317,14 +1401,14 @@ def dropout3d(x, p=0.5, training=True, data_format='NCDHW', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.randn(shape=(2, 3, 4, 5, 6)).astype(paddle.float32) - y_train = paddle.nn.functional.dropout3d(x) #train - y_test = paddle.nn.functional.dropout3d(x, training=False) #test - print(x[0,0,:,:,:]) - print(y_train[0,0,:,:,:]) # may all 0 - print(y_test[0,0,:,:,:]) + >>> x = paddle.randn(shape=(2, 3, 4, 5, 6)).astype(paddle.float32) + >>> y_train = paddle.nn.functional.dropout3d(x) #train + >>> y_test = paddle.nn.functional.dropout3d(x, training=False) #test + >>> print(x[0,0,:,:,:]) + >>> print(y_train[0,0,:,:,:]) # may all 0 + >>> print(y_test[0,0,:,:,:]) """ @@ -1371,19 +1455,19 @@ def alpha_dropout(x, p=0.5, training=True, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[-1, 1], [-1, 1]]).astype(paddle.float32) - y_train = paddle.nn.functional.alpha_dropout(x, 0.5) - y_test = paddle.nn.functional.alpha_dropout(x, 0.5, training=False) - print(y_train) - # Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[-0.10721093, -0.77919382], - # [-0.10721093, 1.66559887]]) (randomly) - print(y_test) - # Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[-1., 1.], - # [-1., 1.]]) + >>> import paddle + >>> paddle.seed(1) + >>> x = paddle.to_tensor([[-1, 1], [-1, 1]]).astype(paddle.float32) + >>> y_train = paddle.nn.functional.alpha_dropout(x, 0.5) + >>> y_test = paddle.nn.functional.alpha_dropout(x, 0.5, training=False) + >>> print(y_train) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.77919382, 1.66559887], + [-0.10721093, -0.77919382]]) + >>> print(y_test) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-1., 1.], + [-1., 1.]]) """ if not isinstance(p, (float, int)): raise TypeError("p argument should be a float or int") @@ -1516,32 +1600,35 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - # example 1 - x_shape = (1, 1, 3) - x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 - y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL") - print(y) - # [[[1. 1. 1. 2. 3. 1. 1. 1.]]] - - # example 2 - x_shape = (1, 1, 3) - x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 - y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL") - print(y) - # [[[1. 1. 1. 2. 3. 1. 1. 1.]]] - - # example 3 - x_shape = (1, 1, 2, 3) - x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 - y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular') - print(y) - # [[[[6. 4. 5. 6. 4. 5.] - # [3. 1. 2. 3. 1. 2.] - # [6. 4. 5. 6. 4. 5.] - # [3. 1. 2. 3. 1. 2.]]]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> # example 1 + >>> x_shape = (1, 1, 3) + >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 + >>> y = F.pad(x, [0, 0, 0, 0, 2, 3], value=1, mode='constant', data_format="NCL") + >>> print(y) + Tensor(shape=[1, 1, 8], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[1., 1., 1., 2., 3., 1., 1., 1.]]]) + + >>> # example 2 + >>> x_shape = (1, 1, 3) + >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 + >>> y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL") + >>> print(y) + Tensor(shape=[1, 1, 8], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[1., 1., 1., 2., 3., 1., 1., 1.]]]) + + >>> # example 3 + >>> x_shape = (1, 1, 2, 3) + >>> x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 + >>> y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular') + >>> print(y) + Tensor(shape=[1, 1, 4, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[6., 4., 5., 6., 4., 5.], + [3., 1., 2., 3., 1., 2.], + [6., 4., 5., 6., 4., 5.], + [3., 1., 2., 3., 1., 2.]]]]) """ assert mode in [ 'reflect', @@ -1713,16 +1800,18 @@ def zeropad2d(x, padding, data_format="NCHW", name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - x_shape = paddle.to_tensor([1, 1, 2, 3]) - x = paddle.arange(paddle.prod(x_shape), dtype="float32").reshape(x_shape) + 1 - y = F.zeropad2d(x, [1, 2, 1, 1]) - print(y) - # [[[[0. 0. 0. 0. 0. 0.] - # [0. 1. 2. 3. 0. 0.] - # [0. 4. 5. 6. 0. 0.] - # [0. 0. 0. 0. 0. 0.]]]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x_shape = paddle.to_tensor([1, 1, 2, 3]) + >>> x = paddle.arange(paddle.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + >>> y = F.zeropad2d(x, [1, 2, 1, 1]) + >>> print(y) + Tensor(shape=[1, 1, 4, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[0., 0., 0., 0., 0., 0.], + [0., 1., 2., 3., 0., 0.], + [0., 4., 5., 6., 0., 0.], + [0., 0., 0., 0., 0., 0.]]]]) """ return pad( @@ -1767,16 +1856,17 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): Code Examples: .. code-block:: python - import paddle - import paddle.nn as nn + >>> import paddle + >>> import paddle.nn as nn - paddle.seed(1) - x1 = paddle.randn(shape=[2, 3]) - x2 = paddle.randn(shape=[2, 3]) + >>> paddle.seed(1) + >>> x1 = paddle.randn(shape=[2, 3]) + >>> x2 = paddle.randn(shape=[2, 3]) - result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0) - print(result) - # [0.97689527, 0.99996042, -0.55138415] + >>> result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0) + >>> print(result) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [ 0.97689527, 0.99996042, -0.55138415]) """ w12 = sum(paddle.multiply(x1, x2), axis=axis) @@ -1822,21 +1912,29 @@ def linear(x, weight, bias=None, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn((3, 2), dtype="float32") - # x: [[-0.32342386 -1.200079 ] - # [ 0.7979031 -0.90978354] - # [ 0.40597573 1.8095392 ]] - weight = paddle.full(shape=[2, 4], fill_value="0.5", dtype="float32", name="weight") - # weight: [[0.5 0.5 0.5 0.5] - # [0.5 0.5 0.5 0.5]] - bias = paddle.ones(shape=[4], dtype="float32", name="bias") - # bias: [1. 1. 1. 1.] - y = paddle.nn.functional.linear(x, weight, bias) - # y: [[0.23824859 0.23824859 0.23824859 0.23824859] - # [0.9440598 0.9440598 0.9440598 0.9440598 ] - # [2.1077576 2.1077576 2.1077576 2.1077576 ]] + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.randn((3, 2), dtype="float32") + >>> print(x) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.06132207, 1.11349595], + [ 0.41906244, -0.24858207], + [-1.85169315, -1.50370061]]) + >>> weight = paddle.full(shape=[2, 4], fill_value="0.5", dtype="float32", name="weight") + >>> print(weight) + Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.50000000, 0.50000000, 0.50000000, 0.50000000], + [0.50000000, 0.50000000, 0.50000000, 0.50000000]]) + >>> bias = paddle.ones(shape=[4], dtype="float32", name="bias") + >>> print(bias) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [1., 1., 1., 1.]) + >>> y = paddle.nn.functional.linear(x, weight, bias) + >>> print(y) + Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 1.58740902, 1.58740902, 1.58740902, 1.58740902], + [ 1.08524013, 1.08524013, 1.08524013, 1.08524013], + [-0.67769694, -0.67769694, -0.67769694, -0.67769694]]) """ if in_dynamic_mode(): # TODO(jiabin): using addmm for fast forward route @@ -1921,17 +2019,17 @@ def label_smooth(label, prior_dist=None, epsilon=0.1, name=None): Examples: .. code-block:: python - import paddle - paddle.disable_static() + >>> import paddle + >>> paddle.disable_static() - x = paddle.to_tensor([[[0, 1, 0], - [ 1, 0, 1]]], dtype="float32", stop_gradient=False) + >>> x = paddle.to_tensor([[[0, 1, 0], + >>> [ 1, 0, 1]]], dtype="float32", stop_gradient=False) - output = paddle.nn.functional.label_smooth(x) - print(output) - # Tensor(shape=[1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=False, - # [[[0.03333334, 0.93333334, 0.03333334], - # [0.93333334, 0.03333334, 0.93333334]]]) + >>> output = paddle.nn.functional.label_smooth(x) + >>> print(output) + Tensor(shape=[1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=False, + [[[0.03333334, 0.93333334, 0.03333334], + [0.93333334, 0.03333334, 0.93333334]]]) """ if epsilon > 1.0 or epsilon < 0.0: raise ValueError("The value of epsilon must be between 0 and 1.") @@ -2002,67 +2100,64 @@ class centers and the shape of sampled_class_center will be [num_positive_class_ .. code-block:: python :name: code-example1 - # CPU or single GPU - import paddle - num_classes = 20 - batch_size = 10 - num_samples = 6 - label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64') - remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes, num_samples) - - print(label) - print(remapped_label) - print(sampled_class_index) - - # the output is - #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True, - # [11, 5 , 1 , 3 , 12, 2 , 15, 19, 18, 19]) - #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True, - # [4, 3, 0, 2, 5, 1, 6, 8, 7, 8]) - #Tensor(shape=[9], dtype=int64, place=CPUPlace, stop_gradient=True, - # [1 , 2 , 3 , 5 , 11, 12, 15, 18, 19]) + >>> # CPU or single GPU + >>> import paddle + >>> num_classes = 20 + >>> batch_size = 10 + >>> num_samples = 6 + >>> paddle.seed(2023) + >>> label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64') + >>> remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes, num_samples) + >>> print(label) + Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True, + [17, 10, 5 , 18, 8 , 8 , 19, 14, 10, 14]) + >>> print(remapped_label) + Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True, + [4, 2, 0, 5, 1, 1, 6, 3, 2, 3]) + >>> print(sampled_class_index) + Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True, + [5 , 8 , 10, 14, 17, 18, 19]) .. code-block:: python :name: code-example2 - # required: distributed - # Multi GPU, test_class_center_sample.py - import paddle - import paddle.distributed as dist - strategy = dist.fleet.DistributedStrategy() - dist.fleet.init(is_collective=True, strategy=strategy) - batch_size = 10 - num_samples = 6 - rank_id = dist.get_rank() - # num_classes of each GPU can be different, e.g num_classes_list = [10, 8] - num_classes_list = [10, 10] - num_classes = paddle.sum(paddle.to_tensor(num_classes_list)) - label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64') - label_list = [] - dist.all_gather(label_list, label) - label = paddle.concat(label_list, axis=0) - remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes_list[rank_id], num_samples) - - print(label) - print(remapped_label) - print(sampled_class_index) - - #python -m paddle.distributed.launch --gpus=0,1 test_class_center_sample.py - # rank 0 output: - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) - #Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [0, 2, 4, 8, 9, 3]) - - # rank 1 output: - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, - # [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, - # [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) - #Tensor(shape=[7], dtype=int64, place=CUDAPlace(1), stop_gradient=True, - # [0, 1, 2, 3, 5, 7, 8]) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # required: distributed + >>> # Multi GPU, test_class_center_sample.py + >>> import paddle + >>> import paddle.distributed as dist + >>> strategy = dist.fleet.DistributedStrategy() + >>> dist.fleet.init(is_collective=True, strategy=strategy) + >>> batch_size = 10 + >>> num_samples = 6 + >>> rank_id = dist.get_rank() + >>> # num_classes of each GPU can be different, e.g num_classes_list = [10, 8] + >>> num_classes_list = [10, 10] + >>> num_classes = paddle.sum(paddle.to_tensor(num_classes_list)) + >>> label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64') + >>> label_list = [] + >>> dist.all_gather(label_list, label) + >>> label = paddle.concat(label_list, axis=0) + >>> remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes_list[rank_id], num_samples) + + >>> print(label) + >>> print(remapped_label) + >>> print(sampled_class_index) + >>> #python -m paddle.distributed.launch --gpus=0,1 test_class_center_sample.py + >>> # rank 0 output: + Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) + Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) + Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [0, 2, 4, 8, 9, 3]) + >>> # rank 1 output: + Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, + [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) + Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, + [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) + Tensor(shape=[7], dtype=int64, place=CUDAPlace(1), stop_gradient=True, + [0, 1, 2, 3, 5, 7, 8]) """ if not (group is False or group is None or hasattr(group, 'is_member')): raise ValueError( @@ -2216,12 +2311,15 @@ def fold( .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.randn([2,3*2*2,12]) - y = F.fold(x, output_sizes=[4, 5], kernel_sizes=2) - # y.shape = [2,3,4,5] + >>> x = paddle.randn([2,3*2*2,12]) + >>> y = F.fold(x, output_sizes=[4, 5], kernel_sizes=2) + >>> x = paddle.randn([2,3*2*2,12]) + >>> y = F.fold(x, output_sizes=[4, 5], kernel_sizes=2) + >>> print(y.shape) + [2, 3, 4, 5] """ diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 29bcbf880c259..81af0d9be5f4f 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -368,24 +368,24 @@ def conv1d( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - x = paddle.to_tensor([[[4, 8, 1, 9], - [7, 2, 0, 9], - [6, 9, 2, 6]]], dtype="float32") - w = paddle.to_tensor([[[9, 3, 4], - [0, 0, 7], - [2, 5, 6]], - [[0, 3, 4], - [2, 9, 7], - [5, 6, 8]]], dtype="float32") - - y = F.conv1d(x, w) - print(y) - # Tensor(shape=[1, 2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[133., 238.], - # [160., 211.]]]) + >>> import paddle + >>> import paddle.nn.functional as F + + >>> x = paddle.to_tensor([[[4, 8, 1, 9], + ... [7, 2, 0, 9], + ... [6, 9, 2, 6]]], dtype="float32") + >>> w = paddle.to_tensor([[[9, 3, 4], + ... [0, 0, 7], + ... [2, 5, 6]], + ... [[0, 3, 4], + ... [2, 9, 7], + ... [5, 6, 8]]], dtype="float32") + + >>> y = F.conv1d(x, w) + >>> print(y) + Tensor(shape=[1, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[133., 238.], + [160., 211.]]]) """ cudnn_version = get_cudnn_version() if cudnn_version is not None: @@ -632,16 +632,16 @@ def conv2d( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x_var = paddle.randn((2, 3, 8, 8), dtype='float32') - w_var = paddle.randn((6, 3, 3, 3), dtype='float32') + >>> x_var = paddle.randn((2, 3, 8, 8), dtype='float32') + >>> w_var = paddle.randn((6, 3, 3, 3), dtype='float32') - y_var = F.conv2d(x_var, w_var) + >>> y_var = F.conv2d(x_var, w_var) - print(y_var.shape) - # [2, 6, 6, 6] + >>> print(y_var.shape) + [2, 6, 6, 6] """ # entry checks if data_format not in ["NCHW", "NHWC"]: @@ -887,20 +887,20 @@ def conv1d_transpose( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - # shape: (1, 2, 4) - x = paddle.to_tensor([[[4, 0, 9, 7], - [8, 0, 9, 2,]]], dtype="float32") - # shape: (2, 1, 2) - w = paddle.to_tensor([[[7, 0]], - [[4, 2]]], dtype="float32") + >>> # shape: (1, 2, 4) + >>> x = paddle.to_tensor([[[4, 0, 9, 7], + >>> [8, 0, 9, 2,]]], dtype="float32") + >>> # shape: (2, 1, 2) + >>> w = paddle.to_tensor([[[7, 0]], + >>> [[4, 2]]], dtype="float32") - y = F.conv1d_transpose(x, w) - print(y) - # Tensor(shape=[1, 1, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[60., 16., 99., 75., 4. ]]]) + >>> y = F.conv1d_transpose(x, w) + >>> print(y) + Tensor(shape=[1, 1, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[60., 16., 99., 75., 4. ]]]) """ cudnn_version = get_cudnn_version() if cudnn_version is not None: @@ -1183,16 +1183,16 @@ def conv2d_transpose( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x_var = paddle.randn((2, 3, 8, 8), dtype='float32') - w_var = paddle.randn((3, 6, 3, 3), dtype='float32') + >>> x_var = paddle.randn((2, 3, 8, 8), dtype='float32') + >>> w_var = paddle.randn((3, 6, 3, 3), dtype='float32') - y_var = F.conv2d_transpose(x_var, w_var) + >>> y_var = F.conv2d_transpose(x_var, w_var) - print(y_var.shape) - # [2, 6, 10, 10] + >>> print(y_var.shape) + [2, 6, 10, 10] """ if data_format not in ['NCHW', 'NHWC']: @@ -1476,16 +1476,16 @@ def conv3d( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32') - w_var = paddle.randn((6, 3, 3, 3, 3), dtype='float32') + >>> x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32') + >>> w_var = paddle.randn((6, 3, 3, 3, 3), dtype='float32') - y_var = F.conv3d(x_var, w_var) + >>> y_var = F.conv3d(x_var, w_var) - print(y_var.shape) - # [2, 6, 6, 6, 6] + >>> print(y_var.shape) + [2, 6, 6, 6, 6] """ # entry check if data_format not in ["NCDHW", "NDHWC"]: @@ -1688,18 +1688,18 @@ def conv3d_transpose( variable storing transposed convolution and non-linearity activation result. Examples: - .. code-block:: python + .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32') - w_var = paddle.randn((3, 6, 3, 3, 3), dtype='float32') + >>> x_var = paddle.randn((2, 3, 8, 8, 8), dtype='float32') + >>> w_var = paddle.randn((3, 6, 3, 3, 3), dtype='float32') - y_var = F.conv3d_transpose(x_var, w_var) + >>> y_var = F.conv3d_transpose(x_var, w_var) - print(y_var.shape) - # [2, 6, 10, 10, 10] + >>> print(y_var.shape) + [2, 6, 10, 10, 10] """ # entry checks if data_format not in ["NCDHW", "NDHWC"]: diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index cb004fe9b622f..64352cd051dea 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -59,14 +59,13 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None): Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([[1., 3.], [3., 5.]], dtype=paddle.float64) - y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64) - distance = paddle.nn.functional.pairwise_distance(x, y) - print(distance) - # Tensor(shape=[2], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # [4.99999860, 4.99999860]) - + >>> import paddle + >>> x = paddle.to_tensor([[1., 3.], [3., 5.]], dtype=paddle.float64) + >>> y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64) + >>> distance = paddle.nn.functional.pairwise_distance(x, y) + >>> print(distance) + Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, + [4.99999860, 4.99999860]) """ if in_dynamic_mode(): sub = _C_ops.subtract(x, y) diff --git a/python/paddle/nn/functional/extension.py b/python/paddle/nn/functional/extension.py index a5c1e2ef42e3a..adfb976293987 100644 --- a/python/paddle/nn/functional/extension.py +++ b/python/paddle/nn/functional/extension.py @@ -55,48 +55,46 @@ def diag_embed(input, offset=0, dim1=-2, dim2=-1): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - diag_embed_input = paddle.arange(6) - - diag_embed_output1 = F.diag_embed(diag_embed_input) - print(diag_embed_output1) - # Tensor(shape=[6, 6], dtype=int64, place=Place(cpu), stop_gradient=True, - # [[0, 0, 0, 0, 0, 0], - # [0, 1, 0, 0, 0, 0], - # [0, 0, 2, 0, 0, 0], - # [0, 0, 0, 3, 0, 0], - # [0, 0, 0, 0, 4, 0], - # [0, 0, 0, 0, 0, 5]]) - - diag_embed_output2 = F.diag_embed(diag_embed_input, offset=-1, dim1=0,dim2=1 ) - print(diag_embed_output2) - # Tensor(shape=[7, 7], dtype=int64, place=Place(cpu), stop_gradient=True, - # [[0, 0, 0, 0, 0, 0, 0], - # [0, 0, 0, 0, 0, 0, 0], - # [0, 1, 0, 0, 0, 0, 0], - # [0, 0, 2, 0, 0, 0, 0], - # [0, 0, 0, 3, 0, 0, 0], - # [0, 0, 0, 0, 4, 0, 0], - # [0, 0, 0, 0, 0, 5, 0]]) - - diag_embed_input_2dim = paddle.reshape(diag_embed_input,[2,3]) - print(diag_embed_input_2dim) - # Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, - # [[0, 1, 2], - # [3, 4, 5]]) - diag_embed_output3 = F.diag_embed(diag_embed_input_2dim,offset= 0, dim1=0, dim2=2 ) - print(diag_embed_output3) - # Tensor(shape=[3, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, - # [[[0, 0, 0], - # [3, 0, 0]], - - # [[0, 1, 0], - # [0, 4, 0]], - - # [[0, 0, 2], - # [0, 0, 5]]]) + >>> import paddle + >>> import paddle.nn.functional as F + + >>> diag_embed_input = paddle.arange(6) + + >>> diag_embed_output1 = F.diag_embed(diag_embed_input) + >>> print(diag_embed_output1) + Tensor(shape=[6, 6], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [0, 0, 2, 0, 0, 0], + [0, 0, 0, 3, 0, 0], + [0, 0, 0, 0, 4, 0], + [0, 0, 0, 0, 0, 5]]) + + >>> diag_embed_output2 = F.diag_embed(diag_embed_input, offset=-1, dim1=0,dim2=1 ) + >>> print(diag_embed_output2) + Tensor(shape=[7, 7], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0], + [0, 0, 2, 0, 0, 0, 0], + [0, 0, 0, 3, 0, 0, 0], + [0, 0, 0, 0, 4, 0, 0], + [0, 0, 0, 0, 0, 5, 0]]) + + >>> diag_embed_input_2dim = paddle.reshape(diag_embed_input,[2,3]) + >>> print(diag_embed_input_2dim) + Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 1, 2], + [3, 4, 5]]) + >>> diag_embed_output3 = F.diag_embed(diag_embed_input_2dim,offset= 0, dim1=0, dim2=2 ) + >>> print(diag_embed_output3) + Tensor(shape=[3, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[0, 0, 0], + [3, 0, 0]], + [[0, 1, 0], + [0, 4, 0]], + [[0, 0, 2], + [0, 0, 5]]]) """ if not isinstance(input, Variable): input = assign(input) @@ -200,16 +198,16 @@ def sequence_mask(x, maxlen=None, dtype='int64', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - lengths = paddle.to_tensor([10, 9, 8]) - mask = paddle.nn.functional.sequence_mask(lengths) + >>> lengths = paddle.to_tensor([10, 9, 8]) + >>> mask = paddle.nn.functional.sequence_mask(lengths) - print(mask) - # Tensor(shape=[3, 10], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], - # [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], - # [1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]) + >>> print(mask) + Tensor(shape=[3, 10], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]) """ @@ -296,14 +294,24 @@ def gather_tree(ids, parents): Examples: .. code-block:: python - import paddle + >>> import paddle - ids = paddle.to_tensor([[[2, 2], [6, 1]], [[3, 9], [6, 1]], [[0, 1], [9, 0]]]) + >>> ids = paddle.to_tensor([[[2, 2], [6, 1]], [[3, 9], [6, 1]], [[0, 1], [9, 0]]]) - parents = paddle.to_tensor([[[0, 0], [1, 1]], [[1, 0], [1, 0]], [[0, 0], [0, 1]]]) + >>> parents = paddle.to_tensor([[[0, 0], [1, 1]], [[1, 0], [1, 0]], [[0, 0], [0, 1]]]) + + >>> final_sequences = paddle.nn.functional.gather_tree(ids, parents) + >>> [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]] + >>> final_sequences = paddle.nn.functional.gather_tree(ids, parents) + >>> print(final_sequences) + Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[2, 2], + [1, 6]], + [[3, 3], + [6, 1]], + [[0, 1], + [9, 0]]]) - final_sequences = paddle.nn.functional.gather_tree(ids, parents) - # [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]] """ if ids.ndim != 3: @@ -388,11 +396,11 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None, data_format="NCHW"): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - input = paddle.randn([6, 4, 2, 2]) - out = F.temporal_shift(x=input, seg_num=2, shift_ratio=0.2) + >>> input = paddle.randn([6, 4, 2, 2]) + >>> out = F.temporal_shift(x=input, seg_num=2, shift_ratio=0.2) """ if data_format not in ["NCHW", "NHWC"]: raise ValueError( diff --git a/python/paddle/nn/functional/flash_attention.py b/python/paddle/nn/functional/flash_attention.py index d6eb44e66e251..822348d5be852 100644 --- a/python/paddle/nn/functional/flash_attention.py +++ b/python/paddle/nn/functional/flash_attention.py @@ -181,13 +181,12 @@ def flash_attention( Examples: .. code-block:: python - # required: skiptest - import paddle + >>> import paddle - q = paddle.rand((1, 128, 2, 16), dtype=paddle.float16) + >>> paddle.seed(1) + >>> q = paddle.rand((1, 128, 2, 16)) - output = paddle.nn.functional.flash_attention(q, q, q, 0.9, False, False) - print(output) + >>> output = paddle.nn.functional.flash_attention.flash_attention(q, q, q, 0.9, False, False) """ head_dim = query.shape[3] sdp_func_name = _select_sdp(head_dim) @@ -340,13 +339,12 @@ def flash_attn_unpadded( Examples: .. code-block:: python - # required: skiptest - import paddle - - q = paddle.rand((1, 128, 2, 16), dtype=paddle.float16) + >>> import paddle + >>> paddle.seed(1) + >>> q = paddle.rand((1, 128, 2, 16)) - output = paddle.nn.functional.flash_attn_unpadded(q, q, q, 0.9, False, False) - print(output) + >>> output = paddle.nn.functional.flash_attention.flash_attn_unpadded(q, q, q, 0.9, False, False) + >>> print(output) """ if in_dynamic_mode(): ( diff --git a/python/paddle/nn/initializer/uniform.py b/python/paddle/nn/initializer/uniform.py index cd64a15b7519e..a46ae2a41e3cd 100644 --- a/python/paddle/nn/initializer/uniform.py +++ b/python/paddle/nn/initializer/uniform.py @@ -158,24 +158,33 @@ class Uniform(UniformInitializer): Examples: .. code-block:: python - import paddle - - data = paddle.ones(shape=[3, 1, 2], dtype='float32') - weight_attr = paddle.framework.ParamAttr( - name="linear_weight", - initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5)) - bias_attr = paddle.framework.ParamAttr( - name="linear_bias", - initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5)) - linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) - # linear.weight: [[-0.46245047 0.05260676] - # [ 0.38054508 0.29169726]] - # linear.bias: [-0.2734719 0.23939109] - - res = linear(data) - # res: [[[-0.3553773 0.5836951]] - # [[-0.3553773 0.5836951]] - # [[-0.3553773 0.5836951]]] + >>> import paddle + >>> paddle.seed(1) + >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32') + >>> weight_attr = paddle.framework.ParamAttr( + ... name="linear_weight", + ... initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5)) + >>> bias_attr = paddle.framework.ParamAttr( + ... name="linear_bias", + ... initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5)) + >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) + >>> print(linear.weight) + Parameter containing: + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[-0.48212373, 0.26492310], + [ 0.17605734, -0.45379421]]) + + >>> print(linear.bias) + Parameter containing: + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False, + [-0.11236754, 0.46462214]) + + >>> res = linear(data) + >>> print(res) + Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[[-0.41843393, 0.27575102]], + [[-0.41843393, 0.27575102]], + [[-0.41843393, 0.27575102]]]) """ def __init__(self, low=-1.0, high=1.0, name=None): diff --git a/python/paddle/nn/initializer/xavier.py b/python/paddle/nn/initializer/xavier.py index 6d17c029f587c..60242ecf5b27c 100644 --- a/python/paddle/nn/initializer/xavier.py +++ b/python/paddle/nn/initializer/xavier.py @@ -214,24 +214,33 @@ class XavierNormal(XavierInitializer): Examples: .. code-block:: python - import paddle - - data = paddle.ones(shape=[3, 1, 2], dtype='float32') - weight_attr = paddle.framework.ParamAttr( - name="linear_weight", - initializer=paddle.nn.initializer.XavierNormal()) - bias_attr = paddle.framework.ParamAttr( - name="linear_bias", - initializer=paddle.nn.initializer.XavierNormal()) - linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) - # inear.weight: [[ 0.06910077 -0.18103665] - # [-0.02546741 -1.0402188 ]] - # linear.bias: [-0.5012929 0.12418364] - - res = linear(data) - # res: [[[-0.4576595 -1.0970719]] - # [[-0.4576595 -1.0970719]] - # [[-0.4576595 -1.0970719]]] + >>> import paddle + >>> paddle.seed(1) + >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32') + >>> weight_attr = paddle.framework.ParamAttr( + ... name="linear_weight", + ... initializer=paddle.nn.initializer.XavierNormal()) + >>> bias_attr = paddle.framework.ParamAttr( + ... name="linear_bias", + ... initializer=paddle.nn.initializer.XavierNormal()) + >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) + >>> print(linear.weight) + Parameter containing: + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[-0.21607460, 0.08382989], + [ 0.29147008, -0.07049121]]) + + >>> print(linear.bias) + Parameter containing: + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False, + [1.06076419, 0.87684733]) + + >>> res = linear(data) + >>> print(res) + Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[[1.13615966, 0.89018601]], + [[1.13615966, 0.89018601]], + [[1.13615966, 0.89018601]]]) """ def __init__(self, fan_in=None, fan_out=None, name=None): @@ -266,24 +275,32 @@ class XavierUniform(XavierInitializer): Examples: .. code-block:: python - import paddle - - data = paddle.ones(shape=[3, 1, 2], dtype='float32') - weight_attr = paddle.framework.ParamAttr( - name="linear_weight", - initializer=paddle.nn.initializer.XavierUniform()) - bias_attr = paddle.framework.ParamAttr( - name="linear_bias", - initializer=paddle.nn.initializer.XavierUniform()) - linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) - # linear.weight: [[-0.04229349 -1.1248565 ] - # [-0.10789523 -0.5938053 ]] - # linear.bias: [ 1.1983747 -0.40201235] - - res = linear(data) - # res: [[[ 1.0481861 -2.1206741]] - # [[ 1.0481861 -2.1206741]] - # [[ 1.0481861 -2.1206741]]] + >>> import paddle + >>> paddle.seed(1) + >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32') + >>> weight_attr = paddle.framework.ParamAttr( + ... name="linear_weight", + ... initializer=paddle.nn.initializer.XavierUniform()) + >>> bias_attr = paddle.framework.ParamAttr( + ... name="linear_bias", + ... initializer=paddle.nn.initializer.XavierUniform()) + >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr) + >>> print(linear.weight) + Parameter containing: + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[-1.18095720, 0.64892638], + [ 0.43125069, -1.11156428]]) + >>> print(linear.bias) + Parameter containing: + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False, + [-0.27524316, 1.13808715]) + + >>> res = linear(data) + >>> print(res) + Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False, + [[[-1.02494967, 0.67544925]], + [[-1.02494967, 0.67544925]], + [[-1.02494967, 0.67544925]]]) """ def __init__(self, fan_in=None, fan_out=None, name=None): From 258202169666de15f8ec6cac56d1ed0b77e28d55 Mon Sep 17 00:00:00 2001 From: Ruibiao Chen Date: Thu, 31 Aug 2023 11:12:42 +0800 Subject: [PATCH 045/194] Throw error for NVCC lazy in 1F1B pipeline (#56725) --- python/paddle/distributed/auto_parallel/static/engine.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/paddle/distributed/auto_parallel/static/engine.py b/python/paddle/distributed/auto_parallel/static/engine.py index 0e3c316a5470c..d1261eca247f1 100644 --- a/python/paddle/distributed/auto_parallel/static/engine.py +++ b/python/paddle/distributed/auto_parallel/static/engine.py @@ -242,6 +242,14 @@ def __init__( elif self._strategy.pipeline.enable: self._acc_steps = self._strategy.pipeline.accumulate_steps + if ( + self._strategy.pipeline.enable + and self._strategy.pipeline.schedule_mode == "1F1B" + ): + assert ( + os.getenv("CUDA_MODULE_LOADING") != "LAZY" + ), "EXP_CUDA_MODULE_LOADING_LAZY not supported in 1F1B pipeline." + self.history = None paddle.framework.set_flags({'FLAGS_new_executor_sequential_run': 1}) From a34bdb646fb690075efbd9a90828ea70f26d27c4 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:13:11 +0800 Subject: [PATCH 046/194] [NewIR]New ir using kernel registrer type (#56789) * update * fix batch norm grad args def * fix bug * fix combine slice bug * fix slice bug * update builtin split * disable using kernel resigter dtype * polish code * disable some test --- .../ir/transforms/pd_op_to_kernel_pass.cc | 191 +++++++----------- .../phi/kernels/gpu/batch_norm_grad_kernel.cu | 2 - test/ir/new_ir/test_standalone_new_ir.py | 20 ++ 3 files changed, 91 insertions(+), 122 deletions(-) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 64d37c95c1aee..bf499d7ace211 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -210,6 +210,50 @@ ir::OpResult AddPlaceTransferOp(ir::OpResult in, } } +ir::Type BuildOutputType(ir::Type type, + const phi::Place& place, + phi::DataType data_type, + ir::IrContext* ctx) { + if (type.isa()) { + auto dense_tensor_type = type.dyn_cast(); + auto out_dtype = dense_tensor_type.dtype(); + + // TODO(phlrain): open this after fix pr(55509) confict + // if (data_type != phi::DataType::UNDEFINED) { + // out_dtype = TransToIrDataType(data_type, ctx); + // } + + return dialect::AllocatedDenseTensorType::get( + ctx, + place, + out_dtype, + dense_tensor_type.dims(), + dense_tensor_type.data_layout(), + dense_tensor_type.lod(), + dense_tensor_type.offset()); + + } else if (type.isa()) { + auto selected_rows_type = type.dyn_cast(); + auto out_dtype = selected_rows_type.dtype(); + + // TODO(phlrain): open this after fix pr(55509) confict + // if (data_type != phi::DataType::UNDEFINED) { + // out_dtype = TransToIrDataType(data_type, ctx); + // } + return dialect::AllocatedSelectedRowsType::get( + ctx, + place, + out_dtype, + selected_rows_type.dims(), + selected_rows_type.data_layout(), + selected_rows_type.lod(), + selected_rows_type.offset()); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "BuildOutputType only support DenseTensorType and SelectedRowsType")); + } +} + phi::DataType GetKernelDataTypeByYamlInfo( const ir::Operation* op, const std::unordered_map& map_value_pair, @@ -525,6 +569,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, std::vector out_places; // Copy op inputs std::vector vec_inputs; + std::vector vec_inner_types; if (op_item->num_operands() > 0) { for (size_t i = 0; i < op_item->num_operands(); ++i) { auto cur_in = op_item->operand_source(i); @@ -540,6 +585,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, op_item->name())); auto new_in = map_value_pair.at(cur_in); vec_inputs.push_back(new_in); + vec_inner_types.push_back(new_in.type()); if (new_in.type().isa()) { out_places.push_back( new_in.type() @@ -553,49 +599,9 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, } // Copy op output type std::vector op_output_types; - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - auto result_type = op_item->result(i).type(); - if (!result_type) { - op_output_types.push_back(result_type); - } else if (result_type.isa()) { - std::vector vec_inner_types; - auto base_types = result_type.dyn_cast().data(); - for (size_t idx = 0; idx < base_types.size(); idx++) { - auto& base_type = base_types[idx]; - if (base_type) { - if (base_type.isa()) { - auto allocated_dense_tensor_dtype = - paddle::dialect::AllocatedDenseTensorType::get( - ctx, - out_places[idx], - base_type.dyn_cast()); - vec_inner_types.push_back(allocated_dense_tensor_dtype); - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "only support dense tensor in vector type for now")); - } - } else { - // NOTE(phlrain), kernel not support a nullptr in output - ir::Type fp32_dtype = ir::Float32Type::get(ctx); - phi::DDim dims = {}; - phi::DataLayout data_layout = phi::DataLayout::NCHW; - phi::LoD lod = {{}}; - size_t offset = 0; - auto dense_tensor_dtype = paddle::dialect::DenseTensorType::get( - ctx, fp32_dtype, dims, data_layout, lod, offset); - vec_inner_types.push_back(dense_tensor_dtype); - } - } - ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types); - op_output_types.push_back(t1); - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "builtin.combine Result type only support " - "VectorType")); - } - } - } + ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types); + op_output_types.push_back(t1); + // Get op info ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); // Generate new op @@ -614,9 +620,8 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, } if (op_item->name() == "builtin.slice") { - phi::Place out_place = place; - // Copy op inputs std::vector vec_inputs; + std::vector op_output_types; if (op_item->num_operands() > 0) { for (size_t i = 0; i < op_item->num_operands(); ++i) { auto cur_in = op_item->operand_source(i); @@ -635,39 +640,18 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, if (new_in.type().isa()) { auto vec_types = new_in.type().dyn_cast().data(); - out_place = - vec_types[op_item->attributes() - .at("index") - .dyn_cast() - .data()] - .dyn_cast() - .place(); + auto index = op_item->attributes() + .at("index") + .dyn_cast() + .data(); + op_output_types.push_back(vec_types[index]); } else { PADDLE_THROW( phi::errors::Unimplemented("only support vector type for now")); } } } - // Copy op output type - std::vector op_output_types; - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - auto result_type = op_item->result(i).type(); - if (!result_type) { - op_output_types.push_back(result_type); - } else if (result_type.isa()) { - auto allocated_dense_tensor_dtype = - paddle::dialect::AllocatedDenseTensorType::get( - ctx, - out_place, - result_type.dyn_cast()); - op_output_types.push_back(allocated_dense_tensor_dtype); - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "builtin.slice Result type only support DenseTensorType")); - } - } - } + // Get op info ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); // Generate new op @@ -689,6 +673,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, std::vector out_places(op_item->num_results()); // Copy op inputs std::vector vec_inputs; + std::vector op_output_types; if (op_item->num_operands() > 0) { for (size_t i = 0; i < op_item->num_operands(); ++i) { auto cur_in = op_item->operand_source(i); @@ -708,10 +693,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, if (new_in.type().isa()) { auto vec_types = new_in.type().dyn_cast().data(); for (uint64_t idx = 0; idx < vec_types.size(); idx++) { - out_places[idx] = - vec_types[idx] - .dyn_cast() - .place(); + op_output_types.push_back(vec_types[idx]); } } else { PADDLE_THROW( @@ -719,26 +701,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, } } } - // Copy op output type - std::vector op_output_types; - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - auto result_type = op_item->result(i).type(); - if (!result_type) { - op_output_types.push_back(result_type); - } else if (result_type.isa()) { - auto allocated_dense_tensor_dtype = - paddle::dialect::AllocatedDenseTensorType::get( - ctx, - out_places[i], - result_type.dyn_cast()); - op_output_types.push_back(allocated_dense_tensor_dtype); - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "builtin.split Result type only support DenseTensorType")); - } - } - } + // Get op info ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); // Generate new op @@ -805,36 +768,30 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, } for (size_t i = 0; i < op_item->num_results(); ++i) { - phi::Place out_place; + phi::Place out_place = phi::TransToPhiPlace(kernel_key.backend()); + + phi::DataType out_phi_dtype = phi::DataType::UNDEFINED; if ((!UnchangeOutputOps.count(op_item->name())) && (!IsLegacyOp(op_item->name())) && phi_kernel.IsValid()) { out_place = phi::TransToPhiPlace(output_defs[i].backend); - } else { - out_place = phi::TransToPhiPlace(kernel_key.backend()); + out_phi_dtype = output_defs[i].dtype; } auto result_type = op_item->result(i).type(); if (!result_type) { op_output_types.push_back(result_type); - } else if (result_type.isa()) { - auto allocated_dense_tensor_dtype = - paddle::dialect::AllocatedDenseTensorType::get( - ctx, - out_place, - result_type.dyn_cast()); - op_output_types.push_back(allocated_dense_tensor_dtype); + } else if (result_type.isa() || + result_type.isa()) { + op_output_types.push_back( + BuildOutputType(result_type, out_place, out_phi_dtype, ctx)); } else if (result_type.isa()) { std::vector vec_inner_types; auto base_types = result_type.dyn_cast().data(); for (auto& base_type : base_types) { if (base_type) { if (base_type.isa()) { - auto allocated_dense_tensor_dtype = - paddle::dialect::AllocatedDenseTensorType::get( - ctx, - out_place, - base_type.dyn_cast()); - vec_inner_types.push_back(allocated_dense_tensor_dtype); + vec_inner_types.push_back( + BuildOutputType(base_type, out_place, out_phi_dtype, ctx)); } else { PADDLE_THROW(phi::errors::Unimplemented( "only support dense tensor in vector type for now")); @@ -857,16 +814,10 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types); op_output_types.push_back(t1); - } else if (result_type.isa()) { - auto allocated_selected_rows_dtype = - paddle::dialect::AllocatedSelectedRowsType::get( - ctx, - out_place, - result_type.dyn_cast()); - op_output_types.emplace_back(allocated_selected_rows_dtype); } else { PADDLE_THROW(phi::errors::Unimplemented( - "Result type only support DenseTensorType and VectorType")); + "Result type only support DenseTensorType, SelectedRowType and " + "VectorType")); } } } diff --git a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu index 31bad0987ba27..78c3723ceedcb 100644 --- a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu @@ -1387,7 +1387,6 @@ PD_REGISTER_KERNEL(batch_norm_grad, phi::dtype::float16) { if (kernel_key.dtype() == phi::DataType::FLOAT16 || kernel_key.dtype() == phi::DataType::BFLOAT16) { - kernel->OutputAt(0).SetDataType(phi::DataType::FLOAT32); // x_grad kernel->OutputAt(1).SetDataType(phi::DataType::FLOAT32); // scale_grad kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); // bias_grad } @@ -1405,7 +1404,6 @@ PD_REGISTER_KERNEL(batch_norm_grad, double, phi::dtype::float16) { if (kernel_key.dtype() == phi::DataType::FLOAT16) { - kernel->OutputAt(0).SetDataType(phi::DataType::FLOAT32); // x_grad kernel->OutputAt(1).SetDataType(phi::DataType::FLOAT32); // scale_grad kernel->OutputAt(2).SetDataType(phi::DataType::FLOAT32); // bias_grad } diff --git a/test/ir/new_ir/test_standalone_new_ir.py b/test/ir/new_ir/test_standalone_new_ir.py index fd5ee67570769..51843b8b5037e 100644 --- a/test/ir/new_ir/test_standalone_new_ir.py +++ b/test/ir/new_ir/test_standalone_new_ir.py @@ -345,6 +345,26 @@ def func(x, y): np.testing.assert_array_equal(z.numpy(), gold_res) +# TODO(phlrain): open this after fix pr(55509) confict +# class TestNewIrLogicalDygraph(unittest.TestCase): +# def test_with_new_ir(self): +# paddle.disable_static() + +# @paddle.jit.to_static +# def func(x, y, z): +# a = paddle.logical_and(x, y) +# return z + a.cast("float32") + +# x = paddle.ones([2, 2], dtype='float32') +# y = paddle.ones([2, 2], dtype='float32') +# z = paddle.ones([2, 2], dtype='float32') + +# z = func(x, y, z) + +# gold_res = np.ones([2, 2], dtype="float32") * 2 +# np.testing.assert_array_equal(z.numpy(), gold_res) + + if __name__ == "__main__": paddle.enable_static() unittest.main() From 657b64019568d6d0b9e4bc90e62cefa1389e2411 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Thu, 31 Aug 2023 11:13:31 +0800 Subject: [PATCH 047/194] fix some typos like y ... name='x' (#56724) --- python/paddle/fluid/framework.py | 2 +- python/paddle/ir/core.py | 2 +- test/legacy_test/test_isclose_op.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 01f63eca8576a..39d589cb2a900 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -7456,7 +7456,7 @@ def default_main_program(): paddle.enable_static() # Sample Network: x = paddle.static.data(name='x', shape=[100, 100], dtype='float32') - y = paddle.static.data(name='x', shape=[100, 100], dtype='float32') + y = paddle.static.data(name='y', shape=[100, 100], dtype='float32') out = paddle.add(x, y) #print the number of blocks in the program, 1 in this case diff --git a/python/paddle/ir/core.py b/python/paddle/ir/core.py index fe496b55f9129..c7f2a73f2ad5e 100644 --- a/python/paddle/ir/core.py +++ b/python/paddle/ir/core.py @@ -141,7 +141,7 @@ def default_main_program(): paddle.enable_static() # Sample Network: x = paddle.static.data(name='x', shape=[100, 100], dtype='float32') - y = paddle.static.data(name='x', shape=[100, 100], dtype='float32') + y = paddle.static.data(name='y', shape=[100, 100], dtype='float32') out = paddle.add(x, y) #print the number of blocks in the program, 1 in this case diff --git a/test/legacy_test/test_isclose_op.py b/test/legacy_test/test_isclose_op.py index c09d7fd7751a6..3cb84a2b3c1e3 100644 --- a/test/legacy_test/test_isclose_op.py +++ b/test/legacy_test/test_isclose_op.py @@ -214,7 +214,7 @@ def test_fp16(self): y_data = np.random.rand(10, 10).astype('float16') with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data(shape=[10, 10], name='x', dtype='float16') - y = paddle.static.data(shape=[10, 10], name='x', dtype='float16') + y = paddle.static.data(shape=[10, 10], name='y', dtype='float16') out = paddle.isclose(x, y, rtol=1e-05, atol=1e-08) if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) From 97b09e817c7b811c682b036ac3c79007ab00d458 Mon Sep 17 00:00:00 2001 From: Leo Chen <39020268+leo0519@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:24:39 +0800 Subject: [PATCH 048/194] Add elementwise_add into Paddle-TRT NHWC support (#56795) * Add elementwise_add support into NHWC IR --- .../framework/ir/trt_support_nhwc_pass.cc | 2 +- .../inference/test_trt_support_nhwc_pass.py | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/ir/trt_support_nhwc_pass.cc b/paddle/fluid/framework/ir/trt_support_nhwc_pass.cc index 0b12559e31deb..0403330f77cd1 100644 --- a/paddle/fluid/framework/ir/trt_support_nhwc_pass.cc +++ b/paddle/fluid/framework/ir/trt_support_nhwc_pass.cc @@ -161,7 +161,7 @@ void TrtSupportNHWCPass::ApplyImpl(Graph *graph) const { "affine_channel", "softmax", "temporal_shift"}; // OPs unrelated to layout are consistent according to the layout of input // var! - std::unordered_set any_layout_ops{"relu"}; + std::unordered_set any_layout_ops{"relu", "elementwise_add"}; // // // TODO(liuyuanle): Add other op if needed! diff --git a/test/ir/inference/test_trt_support_nhwc_pass.py b/test/ir/inference/test_trt_support_nhwc_pass.py index 7c0a6eb4b4a89..0648202aba30c 100644 --- a/test/ir/inference/test_trt_support_nhwc_pass.py +++ b/test/ir/inference/test_trt_support_nhwc_pass.py @@ -12,7 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import shutil +import tempfile import unittest import numpy as np @@ -53,6 +55,15 @@ def __init__(self): data_format='NHWC', ) self.relu3 = nn.ReLU() + self.conv4 = nn.Conv2D( + in_channels=2, + out_channels=1, + kernel_size=3, + stride=2, + padding=0, + data_format='NHWC', + ) + self.relu4 = nn.ReLU() self.flatten = nn.Flatten() self.fc = nn.Linear(729, 10) self.softmax = nn.Softmax() @@ -62,8 +73,12 @@ def forward(self, x): x = self.relu1(x) x = self.conv2(x) x = self.relu2(x) + res = x x = self.conv3(x) x = self.relu3(x) + res = self.conv4(res) + res = self.relu4(res) + x = x + res x = self.flatten(x) x = self.fc(x) x = self.softmax(x) @@ -73,7 +88,11 @@ def forward(self, x): class TRTNHWCConvertTest(unittest.TestCase): def setUp(self): self.place = paddle.CUDAPlace(0) - self.path = './inference_pass/nhwc_convert/infer_model' + self.temp_dir = tempfile.TemporaryDirectory() + self.path = os.path.join( + self.temp_dir.name, 'inference_pass', 'nhwc_converter', '' + ) + self.model_prefix = self.path + 'infer_model' def create_model(self): image = static.data( @@ -82,11 +101,13 @@ def create_model(self): predict = SimpleNet()(image) exe = paddle.static.Executor(self.place) exe.run(paddle.static.default_startup_program()) - paddle.static.save_inference_model(self.path, [image], [predict], exe) + paddle.static.save_inference_model( + self.model_prefix, [image], [predict], exe + ) def create_predictor(self): config = paddle.inference.Config( - self.path + '.pdmodel', self.path + '.pdiparams' + self.model_prefix + '.pdmodel', self.model_prefix + '.pdiparams' ) config.enable_memory_optim() config.enable_use_gpu(100, 0) @@ -123,7 +144,7 @@ def test_nhwc_convert(self): result = self.infer(predictor, img=[img]) def tearDown(self): - shutil.rmtree('./inference_pass/nhwc_convert/') + shutil.rmtree(self.path) if __name__ == '__main__': From 2ea7a6a389f546927195763cd411482ebd951bc5 Mon Sep 17 00:00:00 2001 From: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:44:01 +0800 Subject: [PATCH 049/194] [AutoParallel]organize dataloder in engine (#56788) --- .../auto_parallel/static/engine.py | 89 ++++++++++++------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/python/paddle/distributed/auto_parallel/static/engine.py b/python/paddle/distributed/auto_parallel/static/engine.py index d1261eca247f1..2b66ac4e16a22 100644 --- a/python/paddle/distributed/auto_parallel/static/engine.py +++ b/python/paddle/distributed/auto_parallel/static/engine.py @@ -949,6 +949,7 @@ def fit( ... batch_size=64) """ self._mode = 'train' + self._inputs_spec, self._labels_spec = self._prepare_data_spec( train_data, train_sample_split, batch_size ) @@ -1011,14 +1012,14 @@ def fit( logs = {} cbks.on_epoch_begin(epoch) - for step, data in enumerate(train_dataloader): + for step, batch in enumerate(train_dataloader): if auto_utils.use_new_executor(): - feeds = self._validate_feed(data) + batches = self._validate_batch(batch) else: - feeds = [{}] + batches = [{}] try: - for micro_feed in feeds: + for micro_batch in batches: with paddle.profiler.utils._nvprof_range( iter_id=step, start=nvprof_range[0], @@ -1027,7 +1028,7 @@ def fit( cbks.on_batch_begin('train', step, logs) outs = self._executor.run( self.main_program, - feed=micro_feed, + feed=micro_batch, fetch_list=fetch_names, use_program_cache=self._strategy.use_cache, return_numpy=self._strategy.return_numpy, @@ -1136,12 +1137,13 @@ def evaluate( self._inputs_spec, self._labels_spec = self._prepare_data_spec( valid_data, valid_sample_split, batch_size ) - micro_batch_size = self._validate_batch_size(batch_size) + if not self._has_prepared[self._mode]: self._prepare_program(self._mode) else: self._switch_mode(self._mode) + micro_batch_size = self._validate_batch_size(batch_size) valid_dataloader = self._prepare_dataloader_from_generator( dataset=valid_data, capacity=70, @@ -1243,12 +1245,13 @@ def predict( self._inputs_spec, self._labels_spec = self._prepare_data_spec( test_data, test_sample_split, batch_size ) - micro_batch_size = self._validate_batch_size(batch_size) + if not self._has_prepared[self._mode]: self._prepare_program(self._mode) else: self._switch_mode(self._mode) + micro_batch_size = self._validate_batch_size(batch_size) test_dataloader = self._prepare_dataloader_from_generator( dataset=test_data, capacity=70, @@ -1304,19 +1307,21 @@ def dataloader( ): if mode is not None: self.to_mode(mode) + self._inputs_spec, self._labels_spec = self._prepare_data_spec( dataset, sample_split, batch_size ) - micro_batch_size = self._validate_batch_size(batch_size) + if not self._has_prepared[self._mode]: self._prepare_program(self._mode) else: self._switch_mode(self._mode) + batch_size = self._validate_batch_size(batch_size) dataloader = self._prepare_dataloader( dataset, return_list=False, - batch_size=micro_batch_size, + batch_size=batch_size, shuffle=shuffle, drop_last=drop_last, collate_fn=collate_fn, @@ -1351,12 +1356,13 @@ def dataloader_from_generator( self._inputs_spec, self._labels_spec = self._prepare_data_spec( dataset, sample_split, batch_size ) - micro_batch_size = self._validate_batch_size(batch_size) + if not self._has_prepared[self._mode]: self._prepare_program(self._mode) else: self._switch_mode(self._mode) + micro_batch_size = self._validate_batch_size(batch_size) dataloader = self._prepare_dataloader_from_generator( dataset=dataset, capacity=capacity, @@ -1582,33 +1588,48 @@ def _tune(self, tune_data, tune_sample_split=None, batch_size=1): def _validate_batch_size(self, batch_size): if batch_size is None: return None - if self._strategy.pipeline.enable and auto_utils.use_new_executor(): - return batch_size - assert ( - batch_size % self._acc_steps == 0 - ), "Requires batch_size:[{}] to be divisible by acc_steps:[{}].".format( - batch_size, self._acc_steps - ) - return batch_size // self._acc_steps - def _validate_feed(self, feed): - if feed is None: + if auto_utils.use_new_executor(): + assert ( + len(set(self._dp_world_sizes)) == 1 + ), "DistributedBatchSampler only support one data parallel group, but got [{}] different data parallel groups".format( + len(set(self._dp_world_sizes)) + ) + assert ( + batch_size % self._dp_world_sizes[0] == 0 + ), "batch_size [{}] is not divisible by dp_world_size [{}]".format( + str(batch_size), str(self._dp_world_sizes[0]) + ) + return batch_size // self._dp_world_sizes[0] + else: + assert ( + batch_size % self._acc_steps == 0 + ), "Requires batch_size:[{}] to be divisible by acc_steps:[{}].".format( + batch_size, self._acc_steps + ) + return batch_size // self._acc_steps + + def _validate_batch(self, batch): + if batch is None: return [None] - # pp with schedule or navie-pp + if self._strategy.pipeline.enable or self._acc_steps == 1: - return feed - - # split feed data with gradient_merge k_steps - feed_names = [] - split_feeds = [] - for feed_name, cur_feed in feed[0].items(): - feed_names.append(feed_name) - split_feeds.append(np.split(np.array(cur_feed), self._acc_steps, 0)) - micro_feeds = [] - for i in range(self._acc_steps): - split_feed = [sf[i] for sf in split_feeds] - micro_feeds.append(dict(zip(feed_names, split_feed))) - return micro_feeds + # pp with schedule or navie-pp + return batch + else: + # split feed data with gradient_merge k_steps + feed_names = [] + split_batches = [] + for feed_name, cur_feed in batch[0].items(): + feed_names.append(feed_name) + split_batches.append( + np.split(np.array(cur_feed), self._acc_steps, 0) + ) + baches = [] + for i in range(self._acc_steps): + micro_batch = [split_batch[i] for split_batch in split_batches] + baches.append(dict(zip(feed_names, micro_batch))) + return baches def _validate_spec(self, specs): specs = auto_utils.to_list(specs) From a31cf3d2764e310759cb545bb39619dce7a13530 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Thu, 31 Aug 2023 11:45:19 +0800 Subject: [PATCH 050/194] skip data_transfer for save op (#56775) --- paddle/fluid/operators/save_op.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/operators/save_op.cc b/paddle/fluid/operators/save_op.cc index bc1f5a0d34f60..2450d64542a78 100644 --- a/paddle/fluid/operators/save_op.cc +++ b/paddle/fluid/operators/save_op.cc @@ -101,7 +101,9 @@ PD_REGISTER_KERNEL(save, int16_t, int64_t, phi::dtype::float16, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} PD_REGISTER_KERNEL(save_sr, CPU, @@ -115,7 +117,9 @@ PD_REGISTER_KERNEL(save_sr, int16_t, int64_t, phi::dtype::float16, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PD_REGISTER_KERNEL(save, @@ -130,7 +134,9 @@ PD_REGISTER_KERNEL(save, int16_t, int64_t, phi::dtype::float16, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} PD_REGISTER_KERNEL(save_sr, GPU, @@ -144,5 +150,7 @@ PD_REGISTER_KERNEL(save_sr, int16_t, int64_t, phi::dtype::float16, - phi::dtype::bfloat16) {} + phi::dtype::bfloat16) { + kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND); +} #endif From e4699231bd4cc814266a45e3fdba1de3abc476df Mon Sep 17 00:00:00 2001 From: Scotty Date: Thu, 31 Aug 2023 11:51:55 +0800 Subject: [PATCH 051/194] fix: remove complex128 relative error constraints (#56822) --- test/legacy_test/eager_op_test.py | 8 -------- test/legacy_test/test_elementwise_div_op.py | 6 ++++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index c8b7ec70b70a7..8b4730abdfb04 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -2567,14 +2567,6 @@ def check_grad_with_place( numeric_grad_delta = 1e-5 max_relative_error = 1e-7 - if ( - self.dtype == np.complex128 - and self.op_type - not in op_threshold_white_list.NEED_FIX_FP64_CHECK_GRAD_THRESHOLD_OP_LIST - ): - numeric_grad_delta = 1e-5 - max_relative_error = 1e-6 - cache_list = None if hasattr(self, "cache_name_list"): cache_list = self.cache_name_list diff --git a/test/legacy_test/test_elementwise_div_op.py b/test/legacy_test/test_elementwise_div_op.py index 5972f8089fd55..7fb7ec87e4501 100644 --- a/test/legacy_test/test_elementwise_div_op.py +++ b/test/legacy_test/test_elementwise_div_op.py @@ -538,6 +538,8 @@ def test_check_grad_normal(self): self.check_grad( ['X', 'Y'], 'Out', + numeric_grad_delta=1e-5, + max_relative_error=1e-6, ) def test_check_grad_ingore_x(self): @@ -545,6 +547,8 @@ def test_check_grad_ingore_x(self): ['Y'], 'Out', no_grad_set=set("X"), + numeric_grad_delta=1e-5, + max_relative_error=1e-6, ) def test_check_grad_ingore_y(self): @@ -552,6 +556,8 @@ def test_check_grad_ingore_y(self): ['X'], 'Out', no_grad_set=set('Y'), + numeric_grad_delta=1e-5, + max_relative_error=1e-6, ) From 2d50a64d02ed39b29ae4c7b2f099f7617fbf2f72 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Thu, 31 Aug 2023 12:04:00 +0800 Subject: [PATCH 052/194] [xdoctest][task 292] reformat example code with google style in `python/paddle/nn/functional/norm.py` (#56825) --- python/paddle/nn/functional/norm.py | 142 ++++++++++++++++------------ 1 file changed, 81 insertions(+), 61 deletions(-) diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 1a556a7c5106e..81ae8efd37b05 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -53,28 +53,29 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): .. code-block:: python - import paddle - import paddle.nn.functional as F - - paddle.disable_static() - x = paddle.arange(6, dtype="float32").reshape([2,3]) - y = F.normalize(x) - print(y) - # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0. , 0.44721359, 0.89442718], - # [0.42426404, 0.56568539, 0.70710671]]) - - y = F.normalize(x, p=1.5) - print(y) - # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0. , 0.40862012, 0.81724024], - # [0.35684016, 0.47578689, 0.59473360]]) - - y = F.normalize(x, axis=0) - print(y) - # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0. , 0.24253564, 0.37139067], - # [1. , 0.97014254, 0.92847669]]) + >>> import paddle + >>> import paddle.nn.functional as F + + >>> paddle.disable_static() + >>> x = paddle.arange(6, dtype="float32").reshape([2,3]) + >>> y = F.normalize(x) + >>> print(y) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 0.44721359, 0.89442718], + [0.42426404, 0.56568539, 0.70710671]]) + + >>> y = F.normalize(x, p=1.5) + >>> print(y) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 0.40862012, 0.81724024], + [0.35684016, 0.47578689, 0.59473360]]) + + >>> y = F.normalize(x, axis=0) + >>> print(y) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 0.24253564, 0.37139067], + [1. , 0.97014254, 0.92847669]]) + """ if in_dygraph_mode(): @@ -148,31 +149,29 @@ def batch_norm( Examples: .. code-block:: python - import paddle - - x = paddle.arange(12, dtype="float32").reshape([2, 1, 2, 3]) - print(x) - # Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[[0. , 1. , 2. ], - # [3. , 4. , 5. ]]], - - # [[[6. , 7. , 8. ], - # [9. , 10., 11.]]]]) - - running_mean = paddle.to_tensor([0], dtype="float32") - running_variance = paddle.to_tensor([1], dtype="float32") - weight = paddle.to_tensor([2], dtype="float32") - bias = paddle.to_tensor([1], dtype="float32") - - batch_norm_out = paddle.nn.functional.batch_norm(x, running_mean, - running_variance, weight, bias) - print(batch_norm_out) - # Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[[1. , 2.99998999 , 4.99997997 ], - # [6.99996948 , 8.99995995 , 10.99994946]]], + >>> import paddle + + >>> x = paddle.arange(12, dtype="float32").reshape([2, 1, 2, 3]) + >>> print(x) + Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[0. , 1. , 2. ], + [3. , 4. , 5. ]]], + [[[6. , 7. , 8. ], + [9. , 10., 11.]]]]) + >>> running_mean = paddle.to_tensor([0], dtype="float32") + >>> running_variance = paddle.to_tensor([1], dtype="float32") + >>> weight = paddle.to_tensor([2], dtype="float32") + >>> bias = paddle.to_tensor([1], dtype="float32") + + >>> batch_norm_out = paddle.nn.functional.batch_norm(x, running_mean, + ... running_variance, weight, bias) + >>> print(batch_norm_out) + Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[1. , 2.99998999 , 4.99997997 ], + [6.99996948 , 8.99995995 , 10.99994946]]], + [[[12.99993896, 14.99992943, 16.99991989], + [18.99990845, 20.99989891, 22.99988937]]]]) - # [[[12.99993896, 14.99992943, 16.99991989], - # [18.99990845, 20.99989891, 22.99988937]]]]) """ assert len(x.shape) >= 2, "input dim must be larger than 1" @@ -300,11 +299,21 @@ def layer_norm( .. code-block:: python - import paddle + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.rand((2, 2, 2, 3)) + >>> layer_norm_out = paddle.nn.functional.layer_norm(x, x.shape[1:]) + >>> print(layer_norm_out) + Tensor(shape=[2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[ 0.87799639, -0.32706568, -1.23529339], + [ 1.01540327, -0.66222906, -0.72354043]], + [[ 1.24183702, 0.45458138, -0.33506915], + [ 0.41468468, 1.26852870, -1.98983312]]], + [[[ 0.02837803, 1.27684665, -0.90110683], + [-0.94709367, -0.15110941, -1.16546965]], + [[-0.82010198, 0.11218392, -0.86506516], + [ 1.09489357, 0.19107464, 2.14656854]]]]) - x = paddle.rand((2, 2, 2, 3)) - layer_norm_out = paddle.nn.functional.layer_norm(x, x.shape[1:]) - print(layer_norm_out) """ input_shape = list(x.shape) input_ndim = len(input_shape) @@ -415,12 +424,21 @@ def instance_norm( .. code-block:: python - import paddle - - x = paddle.rand((2, 2, 2, 3)) - instance_norm_out = paddle.nn.functional.instance_norm(x) - - print(instance_norm_out) + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.rand((2, 2, 2, 3)) + >>> instance_norm_out = paddle.nn.functional.instance_norm(x) + + >>> print(instance_norm_out) + Tensor(shape=[2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[ 1.25768495, -0.18054862, -1.26451230], + [ 1.42167914, -0.58056390, -0.65373862]], + [[ 0.95882601, 0.25075224, -0.45947552], + [ 0.21486834, 0.98283297, -1.94780385]]], + [[[ 0.40697321, 1.90885782, -0.71117985], + [-0.76650119, 0.19105314, -1.02920341]], + [[-1.06926346, -0.18710862, -1.11180890], + [ 0.74275863, -0.11246002, 1.73788261]]]]) """ if in_dygraph_mode(): @@ -510,13 +528,15 @@ def local_response_norm( Examples: - .. code-block:: python + .. code-block:: python + + >>> import paddle - import paddle + >>> x = paddle.rand(shape=(3, 3, 112, 112), dtype="float32") + >>> y = paddle.nn.functional.local_response_norm(x, size=5) + >>> print(y.shape) + [3, 3, 112, 112] - x = paddle.rand(shape=(3, 3, 112, 112), dtype="float32") - y = paddle.nn.functional.local_response_norm(x, size=5) - print(y.shape) # [3, 3, 112, 112] """ if not in_dynamic_mode(): check_variable_and_dtype( From 266c6f0d96e40ed3714c6a9b963183011924fc3c Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Thu, 31 Aug 2023 14:42:45 +0800 Subject: [PATCH 053/194] =?UTF-8?q?=E3=80=90New=20IR=E3=80=91New=20ir=20op?= =?UTF-8?q?=20test=20v1.0=20(#56668)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add reference of lbfgs * add reference of lbfgs * new ir op test v1.0 * fix new ir optest bug1.0 * modify two testcase bug * add new ir white list & pass test_mean_op.py * rename white list * add new_ir_guard * rename backward.grad as ir_backward.grad * check place for new ir * fix test_build_model env bug * fix test_prim_program backward bug * change backward to ir_backward in check_appr --------- Co-authored-by: wangruting --- python/paddle/__init__.py | 5 +- .../autograd/{backward.py => ir_backward.py} | 8 +- python/paddle/new_ir_utils.py | 72 ++- python/paddle/static/input.py | 69 +-- test/ir/new_ir/test_build_model.py | 15 +- test/ir/new_ir/test_ir_backward.py | 2 +- test/legacy_test/eager_op_test.py | 410 ++++++++++++++++++ test/prim/new_ir_prim/test_prim_program.py | 2 +- .../new_ir_python_api_grad_white_list.py | 17 + tools/check_file_diff_approvals.sh | 6 +- 10 files changed, 548 insertions(+), 58 deletions(-) rename python/paddle/autograd/{backward.py => ir_backward.py} (99%) create mode 100644 test/white_list/new_ir_python_api_grad_white_list.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index ca7ab3485e52d..b59aadf5cc0df 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -478,9 +478,10 @@ disable_static() -from .new_ir_utils import _switch_to_new_ir # noqa: F401 +from .new_ir_utils import IrChange # noqa: F401 -_switch_to_new_ir() +ir_change = IrChange() +ir_change._switch_to_new_ir() __all__ = [ # noqa 'iinfo', diff --git a/python/paddle/autograd/backward.py b/python/paddle/autograd/ir_backward.py similarity index 99% rename from python/paddle/autograd/backward.py rename to python/paddle/autograd/ir_backward.py index 4439d5e172efd..fc2619bbdcf5b 100644 --- a/python/paddle/autograd/backward.py +++ b/python/paddle/autograd/ir_backward.py @@ -707,26 +707,26 @@ def grad( outputs, 'outputs', ((paddle.ir.Value, paddle.ir.OpResult), list, tuple), - 'paddle.autograd.backward.grad', + 'paddle.autograd.ir_backward.grad', ) check_type( inputs, 'inputs', ((paddle.ir.Value, paddle.ir.OpResult), list, tuple), - 'paddle.autograd.backward.grad', + 'paddle.autograd.ir_backward.grad', ) check_type( grad_outputs, 'grad_outputs', ((paddle.ir.Value, paddle.ir.OpResult), list, tuple, type(None)), - 'paddle.autograd.backward.grad', + 'paddle.autograd.ir_backward.grad', ) check_type( no_grad_vars, 'no_grad_vars', ((paddle.ir.Value, paddle.ir.OpResult), list, tuple, set, type(None)), - 'paddle.autograd.backward.grad', + 'paddle.autograd.ir_backward.grad', ) outputs = _as_list(outputs) inputs = _as_list(inputs) diff --git a/python/paddle/new_ir_utils.py b/python/paddle/new_ir_utils.py index 443ac48ae829c..83c9b5f826d8d 100644 --- a/python/paddle/new_ir_utils.py +++ b/python/paddle/new_ir_utils.py @@ -12,17 +12,67 @@ # See the License for the specific language governing permissions and # limitations under the License. + import paddle +from .fluid.wrapped_decorator import signature_safe_contextmanager + + +class IrChange: + def __init__(self): + old_flag = paddle.fluid.framework.get_flags("FLAGS_enable_new_ir_api") + paddle.fluid.framework.set_flags({"FLAGS_enable_new_ir_api": False}) + if not paddle.ir.core._use_new_ir_api(): + self.old_Program = paddle.static.Program + self.old_program_guard = paddle.fluid.program_guard + self.old_default_main_program = paddle.static.default_main_program + else: + raise RuntimeError( + "IrChange only init when paddle.ir.core._use_new_ir_api() is false, \ + please set FLAGS_enable_new_ir_api = false" + ) + paddle.fluid.framework.set_flags(old_flag) + + def _switch_to_new_ir(self): + if paddle.ir.core._use_new_ir_api(): + paddle.framework.set_flags( + {"FLAGS_enable_new_ir_in_executor": True} + ) + paddle.ir.register_paddle_dialect() + paddle.static.Program = paddle.ir.Program + paddle.fluid.Program = paddle.ir.Program + paddle.fluid.program_guard = paddle.ir.core.program_guard + paddle.static.program_guard = paddle.ir.core.program_guard + paddle.framework.default_main_program = ( + paddle.ir.core.default_main_program + ) + + def _switch_to_old_ir(self): + if not paddle.ir.core._use_new_ir_api(): + paddle.framework.set_flags( + {"FLAGS_enable_new_ir_in_executor": False} + ) + paddle.static.Program = self.old_Program + paddle.fluid.Program = self.old_Program + paddle.fluid.program_guard = self.old_program_guard + paddle.static.program_guard = self.old_program_guard + paddle.framework.default_main_program = ( + self.old_default_main_program + ) + else: + raise RuntimeError( + "IrChange._switch_to_old_ir only work when paddle.ir.core._use_new_ir_api() is false, \ + please set FLAGS_enable_new_ir_api = false" + ) + -def _switch_to_new_ir(): - if paddle.ir.core._use_new_ir_api(): - paddle.framework.set_flags({"FLAGS_enable_new_ir_in_executor": True}) - paddle.ir.register_paddle_dialect() - paddle.static.Program = paddle.ir.Program - paddle.fluid.Program = paddle.ir.Program - paddle.fluid.program_guard = paddle.ir.core.program_guard - paddle.static.program_guard = paddle.ir.core.program_guard - paddle.framework.default_main_program = ( - paddle.ir.core.default_main_program - ) +@signature_safe_contextmanager +def _newir_guard(): + ir_change = IrChange() + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) + ir_change._switch_to_new_ir() + try: + yield + finally: + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) + ir_change._switch_to_old_ir() diff --git a/python/paddle/static/input.py b/python/paddle/static/input.py index 8cdcb5f551a7c..30a853336c976 100644 --- a/python/paddle/static/input.py +++ b/python/paddle/static/input.py @@ -98,44 +98,47 @@ def data(name, shape, dtype=None, lod_level=0): [2.]]], dtype=float32)] """ - helper = LayerHelper('data', **locals()) - check_type(name, 'name', (bytes, str), 'data') - check_type(shape, 'shape', (list, tuple), 'data') - - shape = list(shape) - for i in range(len(shape)): - if shape[i] is None: - shape[i] = -1 - - if dtype: - out = helper.create_global_variable( - name=name, - shape=shape, - dtype=dtype, - type=core.VarDesc.VarType.LOD_TENSOR, - stop_gradient=True, - lod_level=lod_level, - is_data=True, - need_check_feed=True, - ) - - else: - out = helper.create_global_variable( - name=name, - shape=shape, - dtype=paddle.get_default_dtype(), - type=core.VarDesc.VarType.LOD_TENSOR, - stop_gradient=True, - lod_level=lod_level, - is_data=True, - need_check_feed=True, - ) - dtype = paddle.get_default_dtype() if paddle.ir.core._use_new_ir_api(): + if not dtype: + dtype = paddle.get_default_dtype() ir_dtype = paddle.ir.core.convert_np_dtype_to_dtype_(dtype) return paddle._ir_ops.data(name, shape, ir_dtype, core.Place()) + else: + helper = LayerHelper('data', **locals()) + check_type(name, 'name', (bytes, str), 'data') + check_type(shape, 'shape', (list, tuple), 'data') + + shape = list(shape) + for i in range(len(shape)): + if shape[i] is None: + shape[i] = -1 + + if dtype: + out = helper.create_global_variable( + name=name, + shape=shape, + dtype=dtype, + type=core.VarDesc.VarType.LOD_TENSOR, + stop_gradient=True, + lod_level=lod_level, + is_data=True, + need_check_feed=True, + ) + + else: + out = helper.create_global_variable( + name=name, + shape=shape, + dtype=paddle.get_default_dtype(), + type=core.VarDesc.VarType.LOD_TENSOR, + stop_gradient=True, + lod_level=lod_level, + is_data=True, + need_check_feed=True, + ) + is_new_ir_mode = os.environ.get("FLAGS_enable_new_ir_in_executor", None) if evaluate_flag(is_new_ir_mode): helper = LayerHelper('data', **locals()) diff --git a/test/ir/new_ir/test_build_model.py b/test/ir/new_ir/test_build_model.py index 67c634821fd82..8c9cbb32aa28f 100644 --- a/test/ir/new_ir/test_build_model.py +++ b/test/ir/new_ir/test_build_model.py @@ -11,7 +11,6 @@ # 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. - import unittest import numpy as np @@ -29,15 +28,25 @@ def test_basic_network(self): y = paddle.static.data('y', [4, 4], dtype='float32') divide_out = paddle.divide(x, y) sum_out = paddle.sum(divide_out) - exe = paddle.static.Executor() x_feed = np.ones([4, 4], dtype=np.float32) * 10 y_feed = np.ones([4, 4], dtype=np.float32) * 2 (sum_value,) = exe.run( - feed={'x': x_feed, 'y': y_feed}, fetch_list=[sum_out] + main_program, + feed={'x': x_feed, 'y': y_feed}, + fetch_list=[sum_out], ) self.assertEqual(sum_value, 5 * 4 * 4) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.static.data('x', [4, 4], dtype='float32') + out = paddle.mean(x) + exe = paddle.static.Executor() + x_feed = np.ones([4, 4], dtype=np.float32) * 10 + (sum_value,) = exe.run(feed={'x': x_feed}, fetch_list=[out]) + self.assertEqual(sum_value, 10) + if __name__ == "__main__": unittest.main() diff --git a/test/ir/new_ir/test_ir_backward.py b/test/ir/new_ir/test_ir_backward.py index be29baa1069d2..5d858fc2b7602 100644 --- a/test/ir/new_ir/test_ir_backward.py +++ b/test/ir/new_ir/test_ir_backward.py @@ -16,7 +16,7 @@ import paddle from paddle import ir -from paddle.autograd.backward import grad +from paddle.autograd.ir_backward import grad paddle.enable_static() diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index 8b4730abdfb04..3c50f3d6f5a6f 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -31,6 +31,7 @@ from white_list import ( check_shape_white_list, compile_vs_runtime_white_list, + new_ir_python_api_grad_white_list, no_check_set_white_list, no_grad_set_white_list, op_accuracy_white_list, @@ -39,6 +40,7 @@ import paddle from paddle import fluid +from paddle.autograd.ir_backward import grad as ir_grad from paddle.fluid import core, unique_name from paddle.fluid.backward import append_backward from paddle.fluid.executor import Executor @@ -1201,6 +1203,164 @@ def _calc_dygraph_output( ) return outputs + def get_kernel_signature(self, place, egr_inps=None, egr_oups=None): + with fluid.dygraph.base.guard(place=place): + block = fluid.default_main_program().global_block() + op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) + # prepare input variable + dygraph_tensor_inputs = ( + egr_inps + if egr_inps + else self.append_input_output_for_dygraph( + op_proto, self.inputs, True, False, block + ) + ) + # prepare output variable + dygraph_tensor_outputs = ( + egr_oups + if egr_oups + else self.append_input_output_for_dygraph( + op_proto, self.outputs, False, False, block + ) + ) + + # prepare attributes + attrs_outputs = {} + if hasattr(self, "attrs"): + for attrs_name in self.attrs: + if self.attrs[attrs_name] is not None: + attrs_outputs[attrs_name] = self.attrs[attrs_name] + + kernel_sig = OpTestUtils._get_kernel_signature( + self.op_type, + dygraph_tensor_inputs, + dygraph_tensor_outputs, + canonicalize_attrs(attrs_outputs, op_proto), + ) + if not kernel_sig or ( + len(kernel_sig[0]) == 0 + and len(kernel_sig[1]) == 0 + and len(kernel_sig[2]) == 0 + ): + return None + if not hasattr(self, "python_api"): + print(kernel_sig) + assert hasattr(self, "python_api"), ( + "Detect there is KernelSignature for `%s` op, please set the `self.python_api` if you set check_dygraph = True" + % self.op_type + ) + return kernel_sig + + def get_ir_input_attr_dict_and_feed(self, stop_gradient): + attrs_outputs = {} + if hasattr(self, "attrs"): + for attrs_name in self.attrs: + if self.attrs[attrs_name] is not None: + attrs_outputs[attrs_name] = self.attrs[attrs_name] + input_dict = {} + static_inputs = defaultdict(list) + feed = {} + for name, item in self.inputs.items(): + if isinstance(item, list): + for tup in item: + dtype = ( + "bfloat16" + if OpTestUtils.is_bfloat16_type(tup[1].dtype) + else tup[1].dtype + ) + x = paddle.static.data( + name=str(tup[0]), shape=tup[1].shape, dtype=dtype + ) + x.stop_gradient = stop_gradient + static_inputs[name].append(x) + feed.update({str(tup[0]): tup[1]}) + input_dict.update({str(tup[0]): x}) + else: + dtype = ( + "bfloat16" + if OpTestUtils.is_bfloat16_type(item.dtype) + else item.dtype + ) + x = paddle.static.data(name=name, shape=item.shape, dtype=dtype) + x.stop_gradient = stop_gradient + static_inputs[name].append(x) + feed.update({name: item}) + input_dict.update({name: x}) + return static_inputs, attrs_outputs, input_dict, feed + + def _calc_new_ir_output( + self, place, no_check_set=None, inps=None, oups=None + ): + """set egr_inps and egr_oups = None if you want to create it by yourself.""" + + def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): + if hasattr(self, "python_out_sig"): + output_sig = self.python_out_sig + if not isinstance(ret_tuple, (tuple, list)): + ret_tuple = [ret_tuple] + if len(output_sig) == len(ret_tuple): + # [assumption]: we assume {"Out": [Tensor]} + return {a: [b] for a, b in zip(output_sig, ret_tuple)} + else: + # [assumption]: return multi-Tensor in a single output. such as paddle.split() + assert ( + len(output_sig) == 1 + ), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)" + return {output_sig[0]: ret_tuple} + + # get kernel signature + kernel_sig = self.get_kernel_signature(place) + ir_program = paddle.static.Program() + with paddle.static.program_guard(ir_program): + # prepare inps attributes feed + ( + static_inputs, + attrs, + input_dict, + feed, + ) = self.get_ir_input_attr_dict_and_feed(stop_gradient=True) + # prepare args + args = OpTestUtils.prepare_python_api_arguments( + self.python_api, + static_inputs, + attrs, + kernel_sig, + ) + inputs_sig, attrs_sig, outputs_sig = kernel_sig + args = OpTestUtils.assumption_assert_and_transform( + args, len(inputs_sig) + ) + ret_tuple = self.python_api(*args) + result = construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig) + if hasattr(self, "python_out_sig_sub_name"): + for key in self.python_out_sig_sub_name.keys(): + for i in range(len(self.python_out_sig_sub_name[key])): + result[key][0][i].name = self.python_out_sig_sub_name[ + key + ][i] + fetch_list = getattr(self, "fetch_list", []) + # if the fetch_list is customized by user, we use it directly. + # if not, fill the fetch_list by the user configured outputs in test. + + if len(fetch_list) == 0: + for var in result.items(): + if no_check_set is not None and var in no_check_set: + continue + if isinstance(var[1], list): + for v in var[1]: + fetch_list.append(v) + else: + fetch_list.append(var[1]) + + # executor run + executor = Executor(place) + (outs,) = executor.run( + ir_program, + feed=feed, + fetch_list=fetch_list, + ) + return outs + def _check_ir_output(self, place, program, feed_map, fetch_list, outs): if os.getenv("FLAGS_NEW_IR_OPTEST") is None: return @@ -2123,6 +2283,114 @@ def _is_skip_name(self, name): return True return super()._is_skip_name(name) + class NewIRChecker(Checker): + def init(self): + self.checker_name = "new ir checker" + + def calculate_output(self): + self.is_python_api_test = True + new_ir_outs = self.op_test._calc_new_ir_output(place) + if new_ir_outs is None: + self.is_python_api_test = False + # missing KernelSignature, fall back to eager middle output. + new_ir_outs = self.op_test._calc_dygraph_output( + place, no_check_set=no_check_set + ) + self.outputs = new_ir_outs + if self.op_test.is_compared_with_fp32(): + self.op_test.enable_cal_ref_output() + self.is_python_api_test = True + self.ref_outputs = self.op_test._calc_new_ir_output(place) + if self.ref_outputs is None: + self.is_python_api_test = False + # missing KernelSignature, fall back to eager middle output. + self.ref_outputs = self.op_test._calc_dygraph_output( + place, no_check_set=no_check_set + ) + self.op_test.disable_cal_ref_output() + + def _compare_numpy(self, name, actual_np, expect_np): + expect_np = np.array(expect_np) + assert ( + actual_np.shape == expect_np.shape + ), "Operator ({}) : Output ({}) shape mismatch, expect shape is {}, but actual shape is {}".format( + self.op_type, name, expect_np.shape, actual_np.shape + ) + np.testing.assert_allclose( + actual_np, + expect_np, + atol=atol, + rtol=self.rtol if hasattr(self, 'rtol') else rtol, + equal_nan=equal_nan, + err_msg=( + "Operator (" + + self.op_type + + ") Output (" + + name + + ") has diff at " + + str(place) + + " in " + + self.checker_name + ), + ) + + def convert_uint16_to_float_ifneed(self, actual_np, expect_np): + if actual_np.dtype == np.uint16: + self.rtol = 1.0e-2 + elif actual_np.dtype == np.float16: + self.rtol = 1.0e-3 + else: + self.rtol = 1.0e-5 + if self.op_test.is_bfloat16_op(): + if actual_np.dtype == np.uint16: + actual_np = convert_uint16_to_float(actual_np) + if expect_np.dtype == np.uint16: + expect_np = convert_uint16_to_float(expect_np) + return actual_np, expect_np + + def find_actual_value(self, target_name): + with paddle.ir.core.program_guard( + paddle.ir.core.default_main_program() + ): + actual = self.outputs + actual_t = np.array(actual) + return actual, actual_t + + def find_expect_value(self, name): + with paddle.ir.core.program_guard( + paddle.ir.core.default_main_program() + ): + expect = self.ref_outputs + expect_t = np.array(expect) + return expect, expect_t + + def _compare_list(self, name, actual, expect): + """if expect is a tuple, we need to compare list.""" + with paddle.ir.core.program_guard(place=place): + self.op_test.assertListEqual( + actual.value() + .get_tensor() + .recursive_sequence_lengths(), + expect[1], + "Operator (" + + self.op_type + + ") Output (" + + name + + ") has different lod at " + + str(place) + + " in dygraph mode", + ) + + def _is_skip_name(self, name): + # if in final state and kernel signature don't have name, then skip it. + if ( + self.is_python_api_test + and hasattr(self.op_test, "python_out_sig") + and name not in self.op_test.python_out_sig + ): + return True + return super()._is_skip_name(name) + # set some flags by the combination of arguments. if self.is_float16_op(): self.dtype = np.float16 @@ -2184,6 +2452,21 @@ def _is_skip_name(self, name): dygraph_checker.check() dygraph_dygraph_outs = dygraph_checker.outputs + if ( + self.op_type + in new_ir_python_api_grad_white_list.new_ir_python_api_grad_white_list + ): + if ( + type(place) is paddle.fluid.libpaddle.CPUPlace + or type(place) is paddle.fluid.libpaddle.CUDAPlace + ): + print("New IR checker begins...........") + with paddle.new_ir_utils._newir_guard(): + new_ir_checker = NewIRChecker(self, self.outputs) + new_ir_checker.check() + + print("New IR checker ends...........") + # Note(zhiqiu): inplace_atol should be only set when op doesn't ensure # computational consistency. # For example, group_norm uses AtomicAdd on CUDAPlace, which do not ensure @@ -2720,6 +3003,33 @@ def check_grad_with_place( "Gradient Check On %s" % str(place), atol=atol, ) + # get new ir gradient + if ( + self.op_type + in new_ir_python_api_grad_white_list.new_ir_python_api_grad_white_list + ): + if ( + type(place) is paddle.fluid.libpaddle.CPUPlace + or type(place) is paddle.fluid.libpaddle.CUDAPlace + ): + print("New IR gradient begins...........") + with paddle.new_ir_utils._newir_guard(): + new_ir_grad = self._get_ir_gradient( + inputs_to_check, + place, + output_names, + user_defined_grad_outputs, + no_grad_set, + ) + print("New IR gradient ends...........") + self._assert_is_close( + numeric_grads, + [new_ir_grad], + inputs_to_check, + max_relative_error, + "Gradient Check On %s" % str(place), + atol=atol, + ) def _find_var_in_dygraph(self, output_vars, name): if name in output_vars: @@ -3065,6 +3375,106 @@ def _get_gradient( return res + def _get_ir_gradient( + self, + inputs_to_check, + place, + output_names, + user_defined_grad_outputs=None, + no_grad_set=None, + ): + def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): + if hasattr(self, "python_out_sig"): + output_sig = self.python_out_sig + if not isinstance(ret_tuple, (tuple, list)): + ret_tuple = [ret_tuple] + if len(output_sig) == len(ret_tuple): + # [assumption]: we assume {"Out": [Tensor]} + return {a: [b] for a, b in zip(output_sig, ret_tuple)} + else: + # [assumption]: return multi-Tensor in a single output. such as paddle.split() + assert ( + len(output_sig) == 1 + ), "Don't support multi-output with multi-tensor output. (May be you can use set `python_out_sig`, see `test_squeeze2_op` as a example.)" + return {output_sig[0]: ret_tuple} + + # get kernel signature + kernel_sig = self.get_kernel_signature(place) + ir_program = paddle.static.Program() + with paddle.static.program_guard(ir_program): + # prepare inps attributes feed + ( + static_inputs, + attrs, + input_dict, + feed, + ) = self.get_ir_input_attr_dict_and_feed(stop_gradient=False) + # prepare args + args = OpTestUtils.prepare_python_api_arguments( + self.python_api, + static_inputs, + attrs, + kernel_sig, + ) + inputs_sig, attrs_sig, outputs_sig = kernel_sig + args = OpTestUtils.assumption_assert_and_transform( + args, len(inputs_sig) + ) + ret_tuple = self.python_api(*args) + result = construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig) + if hasattr(self, "python_out_sig_sub_name"): + for key in self.python_out_sig_sub_name.keys(): + for i in range(len(self.python_out_sig_sub_name[key])): + result[key][0][i].name = self.python_out_sig_sub_name[ + key + ][i] + fetch_list = getattr(self, "fetch_list", []) + if len(fetch_list) == 0: + for var in result.items(): + if isinstance(var[1], list): + for v in var[1]: + fetch_list.append(v) + else: + fetch_list.append(var[1]) + outputs = result + outputs_valid = outputs + grad_inputs = inputs_to_check + if user_defined_grad_outputs is None: + if len(outputs_valid) == 1: + for outputs_valid_key in outputs_valid: + loss = paddle.mean(outputs_valid[outputs_valid_key][0]) + grad_inputs = ir_grad( + outputs=paddle.utils.flatten(loss), + inputs=paddle.utils.flatten(static_inputs), + grad_outputs=None, + ) + else: + # user_defined_grad_outputs here are numpy arrays + if not isinstance(user_defined_grad_outputs, list): + user_defined_grad_outputs = [user_defined_grad_outputs] + grad_outputs = [] + for grad_out_value in user_defined_grad_outputs: + grad_outputs.append(paddle.to_tensor(grad_out_value)) + # delete the inputs which no need to calculate grad + for no_grad_val in no_grad_set: + del static_inputs[no_grad_val] + + grad_inputs = ir_grad( + outputs=paddle.utils.flatten(outputs), + inputs=paddle.utils.flatten(static_inputs), + grad_outputs=grad_outputs, + ) + fetch_list = list(grad_inputs) + + # executor run + executor = paddle.static.Executor() + (outs,) = executor.run( + ir_program, + feed=feed, + fetch_list=fetch_list, + ) + return outs + class OpTestTool: @classmethod diff --git a/test/prim/new_ir_prim/test_prim_program.py b/test/prim/new_ir_prim/test_prim_program.py index 594f65baa9b2e..c4cc0187b1ad8 100644 --- a/test/prim/new_ir_prim/test_prim_program.py +++ b/test/prim/new_ir_prim/test_prim_program.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.autograd.backward import grad +from paddle.autograd.ir_backward import grad from paddle.decomposition import decompose from paddle.framework import core diff --git a/test/white_list/new_ir_python_api_grad_white_list.py b/test/white_list/new_ir_python_api_grad_white_list.py new file mode 100644 index 0000000000000..81ab325a12aeb --- /dev/null +++ b/test/white_list/new_ir_python_api_grad_white_list.py @@ -0,0 +1,17 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +new_ir_python_api_grad_white_list = [ + "mean", +] diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index 24e6c0ce1028d..73428595c7d7e 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -87,7 +87,7 @@ API_FILES=("CMakeLists.txt" "paddle/fluid/prim/api/api.yaml" "python/paddle/incubate/autograd/composite_rules.py" "python/paddle/incubate/autograd/primitives.py" - "python/paddle/autograd/backward.py" + "python/paddle/autograd/ir_backward.py" "python/paddle/autograd/backward_utils.py" ) @@ -220,8 +220,8 @@ for API_FILE in ${API_FILES[*]}; do elif [ "${API_FILE}" == "python/paddle/incubate/autograd/primitives.py" ] || [ "${API_FILE}" == "python/paddle/incubate/autograd/composite_rules.py" ]; then echo_line="You must have one RD (cyber-pioneer(chenzhuo), xiaoguoguo626807(wangruting), Charles-hit(wanghao), JiabinYang) approval for changing ${API_FILE} , which manages the composite rules.\n" check_approval 1 cyber-pioneer xiaoguoguo626807 Charles-hit JiabinYang - elif [ "${API_FILE}" == "python/paddle/autograd/backward.py" ] || [ "${API_FILE}" == "python/paddle/autograd/backward_utils.py" ]; then - echo_line="You must be approved by Aurelius84(zhangliujie) or cxxly(chenxiaoxu) or xiaoguoguo626807(wangruting) or changeyoung98(chenzhiyang) for python/paddle/autograd/backward.py or python/paddle/autograd/backward_utils.py changes.\n" + elif [ "${API_FILE}" == "python/paddle/autograd/ir_backward.py" ] || [ "${API_FILE}" == "python/paddle/autograd/backward_utils.py" ]; then + echo_line="You must be approved by Aurelius84(zhangliujie) or cxxly(chenxiaoxu) or xiaoguoguo626807(wangruting) or changeyoung98(chenzhiyang) for python/paddle/autograd/ir_backward.py or python/paddle/autograd/backward_utils.py changes.\n" check_approval 1 Aurelius84 cxxly xiaoguoguo626807 changeyoung98 else echo_line="You must have one RD (XiaoguangHu01,chenwhql,zhiqiu,Xreki,luotao1,qili93,Aurelius84) approval for ${API_FILE}, which manages the underlying code for fluid.\n" From 6ae176356f46ce7c882806ec042d7bb466a1f3ca Mon Sep 17 00:00:00 2001 From: JYChen Date: Thu, 31 Aug 2023 14:43:23 +0800 Subject: [PATCH 054/194] check using fluid API in static CI (#56792) --- tools/check_api_approvals.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/check_api_approvals.sh b/tools/check_api_approvals.sh index 6c86c2d5e3222..140ba2091e140 100644 --- a/tools/check_api_approvals.sh +++ b/tools/check_api_approvals.sh @@ -108,6 +108,19 @@ if [ "$inference_approve" != "" ]; then check_approval 1 qingqing01 heavengate fi +filter_fluid=`git diff --name-only upstream/develop | grep "py$" | grep "^test/"` +filter_fluid+=" `git diff --name-only upstream/develop | grep "py$" | grep -v "^python/paddle/fluid"| grep "^python/paddle"`" +has_fluid=`git diff -U0 upstream/$BRANCH -- $filter_fluid | grep '^\+' | grep -v '^++' | grep -E "(fluid\.)|(paddle\.fluid)"` +if [ "${has_fluid}" != "" ]; then + for fluid in "${has_fluid}"; + do + echo "${fluid}" + done + echo_line="You must have one RD (zoooo0820(Recommend), or jeff41404) approval for using fluid API, because fluid API is going to be removed.\n" + check_approval 1 zoooo0820 jeff41404 +fi + + DEV_OP_USE_DEFAULT_GRAD_MAKER_SPEC=${PADDLE_ROOT}/paddle/fluid/op_use_default_grad_maker_DEV.spec PR_OP_USE_DEFAULT_GRAD_MAKER_SPEC=${PADDLE_ROOT}/paddle/fluid/op_use_default_grad_maker_PR.spec ADDED_OP_USE_DEFAULT_GRAD_MAKER=`python ${PADDLE_ROOT}/tools/diff_use_default_grad_op_maker.py ${DEV_OP_USE_DEFAULT_GRAD_MAKER_SPEC} ${PR_OP_USE_DEFAULT_GRAD_MAKER_SPEC}` From 323566d52613d55edad78d258b4ea02a018bce02 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Thu, 31 Aug 2023 15:24:32 +0800 Subject: [PATCH 055/194] [NewIR]Fix install check bug (#56768) * fix install check bug * fix bug --- .../ir/transforms/pd_op_to_kernel_pass.cc | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index bf499d7ace211..84f18baa55aea 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -94,6 +94,28 @@ bool NeedFallBackCpu(const ir::Operation* op, return false; } +phi::Backend GetDstBackend(const std::string& op_name, + phi::Place place, + OpYamlInfoParser* op_yaml_info_parser, + phi::Backend kernel_def_backend, + size_t input_index) { + if (op_name == "builtin.set_parameter" && + place.GetType() == phi::AllocationType::GPU) { + // NOTE: align old executor, all the paramter are initilizered + // on backend of executor place defined + return phi::TransToPhiBackend(place); + } + + auto dst_backend = kernel_def_backend; + if (op_yaml_info_parser != nullptr && + op_yaml_info_parser->IsTensorAttribute(input_index)) { + // Tensor Attribute should on cpu backend for better performance + dst_backend = phi::Backend::CPU; + } + + return dst_backend; +} + bool NeedFallBackFromGPUDNN2GPU(ir::Operation* op, const phi::KernelKey kernel_key) { // NOTE(phlrain): keep the same kernel select strategy with @@ -182,6 +204,10 @@ ir::OpResult AddPlaceTransferOp(ir::OpResult in, ir::Operation* op = ir::Operation::Create({in}, op_attribute, {out_type}, op_info); + if (in.GetDefiningOp()->HasAttribute(kAttrIsPersisable)) { + op->set_attribute(kAttrIsPersisable, + in.GetDefiningOp()->attribute(kAttrIsPersisable)); + } program->block()->push_back(op); auto new_in = op->result(0); @@ -844,10 +870,14 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, auto& kernel = phi::KernelFactory::Instance().SelectKernelWithGPUDNN( kernel_fn_str, kernel_key); - if (kernel.IsValid() && (!UnchangeOutputOps.count(op_item->name()))) { + bool check_place_transfer = + (op_item->name() == "builtin.set_parameter") || + (kernel.IsValid() && (!UnchangeOutputOps.count(op_item->name()))); + + if (check_place_transfer) { if (new_in_type.isa()) { // allocated type - auto place = + auto in_place = new_in_type.dyn_cast() .place(); @@ -855,17 +885,21 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, auto args_def = kernel.args_def(); auto input_defs = args_def.input_defs(); + auto dst_backend = GetDstBackend(op_item->name(), + place, + op_info_parser.get(), + kernel.InputAt(i).backend, + i); + bool need_trans = - (place.GetType() != phi::AllocationType::UNDEFINED) && - (op_info_parser != nullptr && - !op_info_parser->IsTensorAttribute(i)) && + (in_place.GetType() != phi::AllocationType::UNDEFINED) && (paddle::experimental::NeedTransformPlace( - place, kernel.InputAt(i).backend, {})); + in_place, dst_backend, {})); if (need_trans) { - VLOG(6) << "need trans from " << place << " to " + VLOG(6) << "need trans from " << in_place << " to " << kernel_key.backend(); // build memcopy op - auto out_place = phi::TransToPhiPlace(kernel.InputAt(i).backend); + auto out_place = phi::TransToPhiPlace(dst_backend); auto new_in_alloc_type = new_in_type.dyn_cast(); auto out_type = dialect::AllocatedDenseTensorType::get( @@ -878,7 +912,7 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, new_in_alloc_type.offset()); new_in = AddPlaceTransferOp(new_in, out_type, - place, + in_place, out_place, kernel_key, program.get()); From 3f5d00834ad1d846945f4a4e5372fa1532b6dd1c Mon Sep 17 00:00:00 2001 From: RichardWooSJTU <37864677+RichardWooSJTU@users.noreply.github.com> Date: Thu, 31 Aug 2023 02:57:09 -0500 Subject: [PATCH 056/194] [PaddleInference] Fix llm inference dy2static error (#56688) * fix llm inference dy2static error * use kwargs instead of default argument --- python/paddle/jit/api.py | 6 ++++++ python/paddle/static/io.py | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index aaadbc4d66a53..dd96a1001eae5 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -418,6 +418,9 @@ def __init__(self): # when need to save a prune model, use input_names_after_prune to specify the inputs left after pruning self.input_names_after_prune = None + # in the scene of llm-inference, prunning program can cause unexpectable result, an option to skip prune is necessary + self.skip_prune_program = False + @property def output_spec(self): return self._output_spec @@ -497,6 +500,7 @@ def _parse_save_configs(configs): "clip_extra", "skip_forward", "input_names_after_prune", + "skip_prune_program", ] # input check @@ -517,6 +521,7 @@ def _parse_save_configs(configs): inner_config.input_names_after_prune = configs.get( "input_names_after_prune", None ) + inner_config.skip_prune_program = configs.get("skip_prune_program", False) return inner_config @@ -1259,6 +1264,7 @@ def save(layer, path, input_spec=None, **configs): executor=Executor(_current_expected_place()), program=concrete_program.main_program.clone(), clip_extra=configs.clip_extra, + skip_prune_program=configs.skip_prune_program, ) if combine_params: diff --git a/python/paddle/static/io.py b/python/paddle/static/io.py index feaaf905bc70b..1b6f65e28e5ed 100644 --- a/python/paddle/static/io.py +++ b/python/paddle/static/io.py @@ -187,7 +187,7 @@ def append_fetch_ops( ) -def normalize_program(program, feed_vars, fetch_vars): +def normalize_program(program, feed_vars, fetch_vars, **kwargs): """ Normalize/Optimize a program according to feed_vars and fetch_vars. @@ -196,6 +196,8 @@ def normalize_program(program, feed_vars, fetch_vars): program(Program): Specify a program you want to optimize. feed_vars(Tensor | list[Tensor]): Variables needed by inference. fetch_vars(Tensor | list[Tensor]): Variables returned by inference. + kwargs: Supported keys including ``skip_prune_program``. + - skip_prune_program(bool): whether to skip prunning program. Defaults to False. Returns: Program: Normalized/Optimized program. @@ -277,9 +279,12 @@ def normalize_program(program, feed_vars, fetch_vars): copy_program.desc.flush() feed_var_names = [var.name for var in feed_vars] - copy_program = copy_program._prune_with_input( - feeded_var_names=feed_var_names, targets=fetch_vars - ) + + skip_prune_program = kwargs.get('skip_prune_program', False) + if not skip_prune_program: + copy_program = copy_program._prune_with_input( + feeded_var_names=feed_var_names, targets=fetch_vars + ) copy_program = copy_program._inference_optimize(prune_read_op=True) fetch_var_names = [var.name for var in fetch_vars] prepend_feed_ops(copy_program, feed_var_names) @@ -569,7 +574,12 @@ def save_inference_model( program = _get_valid_program(kwargs.get('program', None)) clip_extra = kwargs.get('clip_extra', True) - program = normalize_program(program, feed_vars, fetch_vars) + program = normalize_program( + program, + feed_vars, + fetch_vars, + skip_prune_program=kwargs.get('skip_prune_program', False), + ) # serialize and save program legacy_format = kwargs.get('legacy_format', False) From d53972fd7fa1fab02859e95c5246cf9da19f0f03 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Thu, 31 Aug 2023 19:31:23 +0800 Subject: [PATCH 057/194] =?UTF-8?q?=E3=80=90complex=20op=E3=80=91No.7=20ad?= =?UTF-8?q?d=20complex=20support=20for=20isclose=20(#56723)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add complex support for isclose * add complex test for isclose * fix template complie issue * fix cuda compilation error * fix type typo * fix error for complex's abs * add complex dtype into input * fix ut --- paddle/phi/kernels/cpu/isclose_kernel.cc | 10 ++- paddle/phi/kernels/gpu/isclose_kernel.cu | 4 +- paddle/phi/kernels/impl/isclose_kernel_impl.h | 87 +++++++++++++++++++ python/paddle/tensor/logic.py | 14 ++- test/legacy_test/test_isclose_op.py | 63 ++++++++++++++ 5 files changed, 171 insertions(+), 7 deletions(-) diff --git a/paddle/phi/kernels/cpu/isclose_kernel.cc b/paddle/phi/kernels/cpu/isclose_kernel.cc index dca21494b3ee9..33457921df61e 100644 --- a/paddle/phi/kernels/cpu/isclose_kernel.cc +++ b/paddle/phi/kernels/cpu/isclose_kernel.cc @@ -18,5 +18,11 @@ #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/impl/isclose_kernel_impl.h" -PD_REGISTER_KERNEL( - isclose, CPU, ALL_LAYOUT, phi::IscloseKernel, float, double) {} +PD_REGISTER_KERNEL(isclose, + CPU, + ALL_LAYOUT, + phi::IscloseKernel, + float, + double, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/isclose_kernel.cu b/paddle/phi/kernels/gpu/isclose_kernel.cu index cfae8d0bbda29..1242269242e0b 100644 --- a/paddle/phi/kernels/gpu/isclose_kernel.cu +++ b/paddle/phi/kernels/gpu/isclose_kernel.cu @@ -25,4 +25,6 @@ PD_REGISTER_KERNEL(isclose, phi::IscloseKernel, float, double, - phi::dtype::float16) {} + phi::dtype::float16, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/impl/isclose_kernel_impl.h b/paddle/phi/kernels/impl/isclose_kernel_impl.h index de59cb0c32ca1..93dfb7790b4ab 100644 --- a/paddle/phi/kernels/impl/isclose_kernel_impl.h +++ b/paddle/phi/kernels/impl/isclose_kernel_impl.h @@ -19,6 +19,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/amp_type_traits.h" +#include "paddle/phi/common/complex.h" #include "paddle/phi/common/data_type.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/dense_tensor.h" @@ -86,6 +87,40 @@ struct IscloseFunctor { } }; +template +struct IscloseFunctor> { + void operator()(const phi::CPUContext& ctx, + const DenseTensor& in, + const DenseTensor& other, + const double rtol, + const double atol, + bool equal_nan, + DenseTensor* output) { + auto* in_a = in.data>(); + auto* in_b = other.data>(); + auto* out_data = ctx.template Alloc(output); + auto num = in.numel(); + // *out_data = true; + for (int i = 0; i < num; i++) { + out_data[i] = true; + } + for (int i = 0; i < num; i++) { + const phi::dtype::complex a = in_a[i], b = in_b[i]; + bool val; + if (std::isnan(a) || std::isnan(b)) { + val = equal_nan && std::isnan(a) == std::isnan(b); + } else { + T left = abs(a - b); + T right = atol + rtol * abs(b); + T diff = abs(left - right); + val = a == b || left <= right || diff <= 1e-15; + // *out_data &= val; + out_data[i] = val; + } + } + } +}; + #if defined(__NVCC__) || defined(__HIPCC__) template __global__ void IscloseCUDAKernel(const T* in_data, @@ -113,7 +148,59 @@ __global__ void IscloseCUDAKernel(const T* in_data, // if (!val) *out_data = false; } } +template <> +__global__ void IscloseCUDAKernel>( + const phi::dtype::complex* in_data, + const phi::dtype::complex* other_data, + const double rtol, + const double atol, + bool equal_nan, + int num, + bool* out_data) { + unsigned int idx = threadIdx.x + blockIdx.x * blockDim.x; + bool val; + for (int i = idx; i < num; i += blockDim.x * gridDim.x) { + const phi::dtype::complex a = in_data[i]; + const phi::dtype::complex b = other_data[i]; + if (isnan(a) || isnan(b)) { + val = equal_nan && isnan(a) == isnan(b); + } else { + float left = abs(a - b); + float right = atol + rtol * abs(b); + float diff = abs(left - right); + val = a == b || left <= right || diff <= 1e-15; + } + out_data[i] = val; + // if (!val) *out_data = false; + } +} +template <> +__global__ void IscloseCUDAKernel>( + const phi::dtype::complex* in_data, + const phi::dtype::complex* other_data, + const double rtol, + const double atol, + bool equal_nan, + int num, + bool* out_data) { + unsigned int idx = threadIdx.x + blockIdx.x * blockDim.x; + bool val; + for (int i = idx; i < num; i += blockDim.x * gridDim.x) { + const phi::dtype::complex a = in_data[i]; + const phi::dtype::complex b = other_data[i]; + if (isnan(a) || isnan(b)) { + val = equal_nan && isnan(a) == isnan(b); + } else { + double left = abs(a - b); + double right = atol + rtol * abs(b); + double diff = abs(left - right); + val = a == b || left <= right || diff <= 1e-15; + } + out_data[i] = val; + // if (!val) *out_data = false; + } +} template struct GetTensorValue { T operator()(const phi::GPUContext& dev_ctx, diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 6632738695d7e..58a3b1fc0ea89 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -1320,8 +1320,8 @@ def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): two tensors are elementwise equal within a tolerance. Args: - x(Tensor): The input tensor, it's data type should be float16, float32, float64. - y(Tensor): The input tensor, it's data type should be float16, float32, float64. + x(Tensor): The input tensor, it's data type should be float16, float32, float64, complex64, complex128. + y(Tensor): The input tensor, it's data type should be float16, float32, float64, complex64, complex128. rtol(rtoltype, optional): The relative tolerance. Default: :math:`1e-5` . atol(atoltype, optional): The absolute tolerance. Default: :math:`1e-8` . equal_nan(equalnantype, optional): If :math:`True` , then two :math:`NaNs` will be compared as equal. Default: :math:`False` . @@ -1359,10 +1359,16 @@ def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): return _C_ops.isclose(x, y, rtol, atol, equal_nan) else: check_variable_and_dtype( - x, "input", ['float16', 'float32', 'float64'], 'isclose' + x, + "input", + ['float16', 'float32', 'float64', 'complex64', 'complex128'], + 'isclose', ) check_variable_and_dtype( - y, "input", ['float16', 'float32', 'float64'], 'isclose' + y, + "input", + ['float16', 'float32', 'float64', 'complex64', 'complex128'], + 'isclose', ) check_type(rtol, 'rtol', float, 'isclose') check_type(atol, 'atol', float, 'isclose') diff --git a/test/legacy_test/test_isclose_op.py b/test/legacy_test/test_isclose_op.py index 3cb84a2b3c1e3..2074a160c5b3d 100644 --- a/test/legacy_test/test_isclose_op.py +++ b/test/legacy_test/test_isclose_op.py @@ -259,6 +259,69 @@ def test_check_output(self): self.check_output() +class TestIscloseOpCp64(unittest.TestCase): + def test_cp64(self): + x_data = ( + np.random.rand(10, 10) + 1.0j * np.random.rand(10, 10) + ).astype(np.complex64) + y_data = ( + np.random.rand(10, 10) + 1.0j * np.random.rand(10, 10) + ).astype(np.complex64) + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data(shape=[10, 10], name='x', dtype=np.complex64) + y = paddle.static.data(shape=[10, 10], name='y', dtype=np.complex64) + out = paddle.isclose(x, y, rtol=1e-05, atol=1e-08) + if core.is_compiled_with_cuda(): + place = paddle.CUDAPlace(0) + exe = paddle.static.Executor(place) + exe.run(paddle.static.default_startup_program()) + out = exe.run(feed={'x': x_data, 'y': y_data}, fetch_list=[out]) + + +class TestIscloseOpCp128(unittest.TestCase): + def test_cp128(self): + x_data = ( + np.random.rand(10, 10) + 1.0j * np.random.rand(10, 10) + ).astype(np.complex128) + y_data = ( + np.random.rand(10, 10) + 1.0j * np.random.rand(10, 10) + ).astype(np.complex128) + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data( + shape=[10, 10], name='x', dtype=np.complex128 + ) + y = paddle.static.data( + shape=[10, 10], name='y', dtype=np.complex128 + ) + out = paddle.isclose(x, y, rtol=1e-05, atol=1e-08) + if core.is_compiled_with_cuda(): + place = paddle.CUDAPlace(0) + exe = paddle.static.Executor(place) + exe.run(paddle.static.default_startup_program()) + out = exe.run(feed={'x': x_data, 'y': y_data}, fetch_list=[out]) + + +class TestIscloseOpComplex64(TestIscloseOp): + def set_args(self): + self.input = np.array([10.1 + 0.1j]).astype(np.complex64) + self.other = np.array([10 + 0j]).astype(np.complex64) + self.rtol = np.array([0.01]).astype("float64") + self.atol = np.array([0]).astype("float64") + self.equal_nan = False + + +class TestIscloseOpComplex128(TestIscloseOp): + def set_args(self): + self.input = np.array([10.1 + 0.1j]).astype(np.complex128) + self.other = np.array([10 + 0j]).astype(np.complex128) + self.rtol = np.array([0.01]).astype("float64") + self.atol = np.array([0]).astype("float64") + self.equal_nan = False + + def test_check_output(self): + self.check_output() + + class TestIscloseOpLargeDimInput(TestIscloseOp): def set_args(self): self.input = np.array(np.zeros([2048, 1024])).astype("float64") From 51ba2a0f03a3343f64670041973f17a1168c1387 Mon Sep 17 00:00:00 2001 From: caozhou <48191911+Caozhou1995@users.noreply.github.com> Date: Thu, 31 Aug 2023 21:06:06 +0800 Subject: [PATCH 058/194] add op cost interface (#56803) --- .../auto_parallel/static/cluster.py | 2 + .../auto_parallel/static/cost/__init__.py | 1 + .../auto_parallel/static/cost/base_cost.py | 65 +++++- test/auto_parallel/CMakeLists.txt | 1 + test/auto_parallel/test_cost_interface.py | 185 ++++++++++++++++++ 5 files changed, 250 insertions(+), 4 deletions(-) create mode 100644 test/auto_parallel/test_cost_interface.py diff --git a/python/paddle/distributed/auto_parallel/static/cluster.py b/python/paddle/distributed/auto_parallel/static/cluster.py index b9a37cf66b2e6..3145153893f47 100644 --- a/python/paddle/distributed/auto_parallel/static/cluster.py +++ b/python/paddle/distributed/auto_parallel/static/cluster.py @@ -429,6 +429,7 @@ def __init__(self): # This property only be valid when the cluster consists of machines, # which have the same number accelerators. self._num_devices_per_machine = None + self._gpu_model = None def gen_default_config_cluster( self, @@ -451,6 +452,7 @@ def gen_default_config_cluster( dcu_models = ["DCU"] all_gpu_models = gpu_models + xpu_models + dcu_models self._num_devices_per_machine = device_count + self._gpu_model = gpu_model def _convert_to_type(gpu_model): type = None diff --git a/python/paddle/distributed/auto_parallel/static/cost/__init__.py b/python/paddle/distributed/auto_parallel/static/cost/__init__.py index e8ba0300d45df..33af1093c746d 100644 --- a/python/paddle/distributed/auto_parallel/static/cost/__init__.py +++ b/python/paddle/distributed/auto_parallel/static/cost/__init__.py @@ -22,6 +22,7 @@ from .base_cost import build_comm_desc_from_dist_op from .base_cost import build_comm_costs_from_descs from .base_cost import build_comp_costs_from_descs +from .base_cost import calc_time_by_cost_model from .comp_op_cost import EmbeddingOpCost from .comp_op_cost import EmbeddingGradOpCost diff --git a/python/paddle/distributed/auto_parallel/static/cost/base_cost.py b/python/paddle/distributed/auto_parallel/static/cost/base_cost.py index 77f590b0ce855..197656d6ea845 100644 --- a/python/paddle/distributed/auto_parallel/static/cost/base_cost.py +++ b/python/paddle/distributed/auto_parallel/static/cost/base_cost.py @@ -19,7 +19,7 @@ import paddle from paddle.utils.flops import flops -from ..cluster import LinkType +from ..cluster import LinkType, get_default_cluster from ..dist_tensor import DistributedTensor from ..process_group import get_process_group from ..utils import _get_comm_group, _get_idx_in_axis @@ -785,9 +785,12 @@ def comm_count(self): if self.op is not None: vars = self.op.block.vars # NOTE: The tensor communicated input_name is "X" in default. Otherwise, this function should be overrided - var_name = self.op.input("X")[0] + try: + var_name = self.op.input("X")[0] + except: + var_name = self.op.output("Out")[0] var = get_var_with_recursion( - var_name, self.op.block, self.program + var_name, self.op.block, self.op.block.program ) dtype = var.dtype shape = var.shape @@ -838,7 +841,7 @@ def group_ranks(self): if self.op_desc is not None: self._group_ranks = self.op_desc["group_ranks"] elif self.op is not None: - ring_id = self.op.attrs("ring_id") + ring_id = self.op.attr("ring_id") process_group = get_process_group(ring_id) if process_group is None: raise ValueError( @@ -921,3 +924,57 @@ def calc_time_by_modeling(op=None, desc=None, cluster=None): ) time = op_cost.calc_time() return time + + +def calc_time_by_cost_model(op, cluster=None): + """Calc op time by cost model and the unit is microsecond.""" + if not isinstance(op, paddle.fluid.framework.Operator): + raise TypeError( + "OP must be paddle.fluid.framework.Operator, but got {}.".format( + type(op) + ) + ) + if not cluster: + cluster = get_default_cluster() + time = 0.0 + op_type = op.type + # calc comp op time by flops + if op_type not in NON_COMP_TYPE: + attrs = op.all_attrs() + # build comp op inputs desc to calc flops. + # for example, a matmul op inputs desc will be {"X": [(1024, 1024)], "Y": [(1024, 1024)]} + inputs = {} + for input_name in op.input_names: + var_names = op.input(input_name) + inputs[input_name] = [] + for var_name in var_names: + var = op.block._var_recursive(var_name) + inputs[input_name].append(var.shape) + + # the time of grad operator is twice than its forward operator empirically + if "_grad" in op_type: + op_type = op_type[: len(op_type) - 5] + flops_count = 2 * flops(op_type, inputs, attrs) + else: + flops_count = flops(op_type, inputs, attrs) + + if cluster._gpu_model == "V100": + time = flops_count * 2.9e-7 * 2.6 + elif cluster._gpu_model == "A100": + time = flops_count * 2.9e-7 + else: + raise ValueError( + "Only A100 and V100 gpu has been supported currently." + ) + + # calc comm op time by communication modeling formula + elif op_type in COMM_OP_TYPE: + op_cost = _g_op_cost_factory[op_type]( + op=op, comm_context=CommContext(cluster) + ) + time = op_cost.calc_time() + + else: + raise ValueError(f"The {op_type} has not been supported now.") + + return time diff --git a/test/auto_parallel/CMakeLists.txt b/test/auto_parallel/CMakeLists.txt index 817508e57034c..eef02ecb28cc9 100644 --- a/test/auto_parallel/CMakeLists.txt +++ b/test/auto_parallel/CMakeLists.txt @@ -162,6 +162,7 @@ if(WITH_DISTRIBUTE AND WITH_GPU) py_test_modules(test_rule_based_tuner MODULES test_rule_based_tuner) py_test_modules(test_dist_tensor MODULES test_dist_tensor) py_test_modules(test_shard_tensor_api MODULES test_shard_tensor_api) + py_test_modules(test_cost_interface MODULES test_cost_interface) # End of unittests WITH single card WITHOUT timeout endif() diff --git a/test/auto_parallel/test_cost_interface.py b/test/auto_parallel/test_cost_interface.py new file mode 100644 index 0000000000000..cdd63143fd1aa --- /dev/null +++ b/test/auto_parallel/test_cost_interface.py @@ -0,0 +1,185 @@ +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import paddle +import paddle.nn.functional as F +from paddle import nn, static, utils +from paddle.distributed import fleet +from paddle.distributed.auto_parallel.static.cluster import Cluster +from paddle.distributed.auto_parallel.static.completion import Completer +from paddle.distributed.auto_parallel.static.cost import calc_time_by_cost_model +from paddle.distributed.auto_parallel.static.dist_context import ( + DistributedContext, +) +from paddle.distributed.auto_parallel.static.parallelizer import ( + AutoParallelizer, +) +from paddle.distributed.auto_parallel.static.partitioner import Partitioner +from paddle.distributed.auto_parallel.static.reshard import Resharder +from paddle.distributed.fleet import auto + +paddle.enable_static() +_global_parallel_strategy = "dp_mp_pp" +_global_process_mesh = auto.ProcessMesh( + [[[0, 1], [4, 5]], [[2, 3], [6, 7]]], dim_names=["x", "y", "z"] +) +PP_MESH_0 = auto.ProcessMesh([[0, 1], [4, 5]], dim_names=["x", "y"]) +PP_MESH_1 = auto.ProcessMesh([[2, 3], [6, 7]], dim_names=["x", "y"]) + + +class MLPLayer(nn.Layer): + def __init__( + self, + hidden_size=1024, + intermediate_size=4 * 1024, + initializer_range=0.02, + ): + super().__init__() + d_model = hidden_size + dim_feedforward = intermediate_size + weight_attr = paddle.ParamAttr( + initializer=nn.initializer.Normal(mean=0.0, std=initializer_range) + ) + bias_attr = None + + self.linear0 = nn.Linear( + d_model, dim_feedforward, weight_attr, bias_attr=bias_attr + ) + self.linear1 = nn.Linear( + dim_feedforward, d_model, weight_attr, bias_attr=bias_attr + ) + self.norm = nn.LayerNorm(d_model, epsilon=1e-5) + + def forward(self, input): + auto.shard_tensor(self.linear0.weight, PP_MESH_0, [None, "y"]) + auto.shard_tensor(self.linear1.weight, PP_MESH_1, ["y", None]) + + out = self.norm(input) + out = self.linear0(out) + out = F.gelu(out, approximate=True) + out = self.linear1(out) + param = paddle.create_parameter([1024, 4096], paddle.float32) + auto.shard_tensor(param, PP_MESH_1, [None, "y"]) + out = paddle.matmul(out, param) + + return out + + +def mlp_forward(train_program, start_program): + with static.program_guard( + train_program, start_program + ), utils.unique_name.guard(): + batch_size = 4 + hidden_size = 1024 + sequence_len = 512 + input = static.data( + name="input", shape=[batch_size, hidden_size], dtype='float32' + ) + label = static.data( + name="label", shape=[batch_size, 1], dtype='float32' + ) + + auto.shard_tensor(input, PP_MESH_0, ["x", None]) + auto.shard_tensor(label, PP_MESH_1, ["x", None]) + + mlp = MLPLayer( + hidden_size=hidden_size, + intermediate_size=4 * hidden_size, + initializer_range=0.02, + ) + + predict = mlp(input) + error_cost = paddle.nn.functional.square_error_cost(predict, label) + loss = paddle.mean(error_cost) + + return loss, train_program, start_program + + +def get_dist_prog(train_program, startup_program, dist_context, rank_id): + global _global_process_mesh + dist_context.process_mesh = _global_process_mesh + loss, train_program, startup_program = mlp_forward( + train_program, startup_program + ) + + fleet._user_defined_strategy = fleet.DistributedStrategy() + fleet.user_defined_optimizer = paddle.optimizer.Adam() + parallelizer = AutoParallelizer(fleet) + parallelizer._dist_context = dist_context + + # serial forward & backward completion + completer = Completer(dist_context) + complete_train_program = completer.complete_forward_annotation( + train_program + ) + dist_context.block_state.parse_forward_blocks(complete_train_program) + params_grads = parallelizer._generate_backward( + complete_train_program, + startup_program, + loss, + parameter_list=None, + no_grad_set=None, + callbacks=None, + ) + + # logical partition + partitioner = Partitioner(dist_context, rank_id) + ( + auto_parallel_main_prog, + auto_parallel_startup_prog, + dist_params_grads, + ) = partitioner.partition( + complete_train_program, startup_program, params_grads + ) + + partitioned_optimize_ops = parallelizer._apply_optimize( + auto_parallel_main_prog, auto_parallel_startup_prog, dist_params_grads + ) + + return ( + auto_parallel_main_prog, + auto_parallel_startup_prog, + dist_params_grads, + ) + + +class TestCostInterface(unittest.TestCase): + def test_cost_interface(self): + train_program = paddle.static.Program() + startup_program = paddle.static.Program() + dist_context = DistributedContext() + rank_id = 2 + dist_main_prog, dist_startup_prog, dist_params_grads = get_dist_prog( + train_program, startup_program, dist_context, rank_id + ) + + resharder = Resharder( + dist_main_prog, + dist_startup_prog, + rank_id, + dist_context, + dist_params_grads, + ) + resharder.reshard() + cluster = Cluster() + cluster.gen_default_config_cluster(node_count=1, device_count=8) + for op in dist_main_prog.global_block().ops: + time = calc_time_by_cost_model(op, cluster) + assert time > -1 + + +if __name__ == "__main__": + unittest.main() From 0d0813572e3a150b0bde9ad7157f63026e895d8b Mon Sep 17 00:00:00 2001 From: Yuang Liu Date: Fri, 1 Sep 2023 07:50:22 +0800 Subject: [PATCH 059/194] change default value of sharding related env (#56835) --- python/paddle/distributed/fleet/utils/tensor_fusion_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/distributed/fleet/utils/tensor_fusion_helper.py b/python/paddle/distributed/fleet/utils/tensor_fusion_helper.py index 80494b48ae92c..3cb993790d2b2 100644 --- a/python/paddle/distributed/fleet/utils/tensor_fusion_helper.py +++ b/python/paddle/distributed/fleet/utils/tensor_fusion_helper.py @@ -371,7 +371,7 @@ def fused_parameters( :param scale_after_comm: if enable comm overlap, specify the location of grad scale :return: param storage if fused, comm buffers is comm overlap """ - g_shard_use_reduce = int(os.environ.get("FLAGS_shard_use_reduce", 0)) + g_shard_use_reduce = int(os.environ.get("FLAGS_shard_use_reduce", 1)) act = ( HOOK_ACTION.ALL_REDUCE if not g_shard_use_reduce else HOOK_ACTION.REDUCE ) From 84ec80925dc67a42d768e8205ca95be15870a942 Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Fri, 1 Sep 2023 09:46:22 +0800 Subject: [PATCH 060/194] [IR] Refine Int64 attribute translator logic (#56842) * fix bug * fix bug --- .../ir_adaptor/translator/attribute_translator.cc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc index cc471b4139662..f6a4b94f2bfdf 100644 --- a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc @@ -176,6 +176,16 @@ class Int64ArrayAttributeVisitor : public AttributeVisitor { } }; +class Int64AttributeVisitor : public AttributeVisitor { + public: + using AttributeVisitor::AttributeVisitor; + + ir::Attribute operator()(int is) override { + VLOG(10) << "translating int to Int64Attribute"; + return ir::Int64Attribute::get(ctx, is); + } +}; + class IntArrayAttributeVisitor : public AttributeVisitor { public: using AttributeVisitor::AttributeVisitor; @@ -229,6 +239,7 @@ AttributeTranslator::AttributeTranslator() { new PlaceAttributeVisitor(); special_visitors["ir::ArrayAttribute"] = new Int64ArrayAttributeVisitor(); + special_visitors["ir::Int64Attribute"] = new Int64AttributeVisitor(); } ir::Attribute AttributeTranslator::operator()( From ddc81cc2ca542a9f4ee5bfaad9f7ec0df90b7d55 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 1 Sep 2023 09:58:23 +0800 Subject: [PATCH 061/194] Fix custom device compile error caused by dist marco changing (#56760) * fix custom device errro by dist * polish details --- paddle/phi/core/meta_tensor.cc | 1 + paddle/phi/core/tensor_utils.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/core/meta_tensor.cc b/paddle/phi/core/meta_tensor.cc index 146e0bc4fc662..5ea5a07960923 100644 --- a/paddle/phi/core/meta_tensor.cc +++ b/paddle/phi/core/meta_tensor.cc @@ -17,6 +17,7 @@ limitations under the License. */ #include "glog/logging.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/string_tensor.h" diff --git a/paddle/phi/core/tensor_utils.h b/paddle/phi/core/tensor_utils.h index 42efe249cfbc2..4d9b50d34f8f5 100644 --- a/paddle/phi/core/tensor_utils.h +++ b/paddle/phi/core/tensor_utils.h @@ -16,7 +16,6 @@ limitations under the License. */ #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/device_context.h" -#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" From 9d59c04ee2b85f550330d4006604190ceaa71b58 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 1 Sep 2023 10:16:14 +0800 Subject: [PATCH 062/194] [Dy2St]Fix test_lac and test_sentiment UT error (#52109) --- test/dygraph_to_static/test_lac.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dygraph_to_static/test_lac.py b/test/dygraph_to_static/test_lac.py index 37715f8392fb1..e3382d26964e4 100644 --- a/test/dygraph_to_static/test_lac.py +++ b/test/dygraph_to_static/test_lac.py @@ -79,7 +79,7 @@ def forward(self, inputs): # input_ = inputs[:, j:j+1, :] # original code input_ = paddle.slice(inputs, axes=[1], starts=[j], ends=[j + 1]) input_ = paddle.reshape(input_, [-1, input_.shape[2]]) - hidden, reset, gate = self.gru_unit(input_, hidden) + hidden, reset = self.gru_unit(input_, hidden) hidden_ = paddle.reshape(hidden, [-1, 1, hidden.shape[1]]) res.append(hidden_) From 167d048d2cbdf870acdc53e7012cf78076cbe1a3 Mon Sep 17 00:00:00 2001 From: tianshuo78520a <707759223@qq.com> Date: Fri, 1 Sep 2023 10:21:31 +0800 Subject: [PATCH 063/194] Update approval;test=document_fix (#56804) * Update approval;test=document_fix * test=document_fix --- tools/check_file_diff_approvals.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index 73428595c7d7e..16ee5aafc26b1 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -126,8 +126,8 @@ if [[ $changed_env_var_count -gt 0 ]]; then fi if [[ $git_files -gt 19 || $git_count -gt 999 ]];then - echo_line="You must have Dianhai or XiaoguangHu01 approval for change 20+ files or add than 1000+ lines of content.\n" - check_approval 1 Dianhai XiaoguangHu01 + echo_line="You must have raindrops2sea or XiaoguangHu01 approval for change 20+ files or add than 1000+ lines of content.\n" + check_approval 1 raindrops2sea XiaoguangHu01 fi for API_FILE in ${API_FILES[*]}; do @@ -135,7 +135,7 @@ for API_FILE in ${API_FILES[*]}; do if [ "${API_CHANGE}" ] && [ "${GIT_PR_ID}" != "" ]; then # NOTE: per_page=10000 should be ok for all cases, a PR review > 10000 is not human readable. # You can use http://caius.github.io/github_id/ to find Github user id. - # approval_user_list: XiaoguangHu01 46782768,Xreki 12538138,luotao1 6836917,qingqing01 7845005,guoshengCS 14105589,heavengate 12605721,kuke 3064195,Superjomn 328693,lanxianghit 47554610,cyj1986 39645414,hutuxian 11195205,frankwhzhang 20274488,nepeplwu 45024560,Dianhai 38231817,chenwhql 22561442,zhiqiu 6888866,seiriosPlus 5442383,gongweibao 10721757,saxon-zh 2870059, zhouwei25 52485244, Aurelius84 9301846, liym27 33742067, zhhsplendid 7913861, kolinwei 22165420, liuwei1031 46661762, dingjiaweiww 23093488, juncaipeng 52520497, zhangting2020 26615455, Shixiaowei02 39303645, Heeenrrry 28379894,XieYunshen 32428676, Dong Daxiang 35550832, phlrain 43953930, qili93 16605440. + # approval_user_list: XiaoguangHu01 46782768,Xreki 12538138,luotao1 6836917,qingqing01 7845005,guoshengCS 14105589,heavengate 12605721,kuke 3064195,Superjomn 328693,lanxianghit 47554610,cyj1986 39645414,hutuxian 11195205,frankwhzhang 20274488,nepeplwu 45024560,raindrops2sea 38231817,chenwhql 22561442,zhiqiu 6888866,seiriosPlus 5442383,gongweibao 10721757,saxon-zh 2870059, zhwesky2010 52485244, Aurelius84 9301846, liym27 33742067, zhhsplendid 7913861, kolinwei 22165420, liuwei1031 46661762, dingjiaweiww 23093488, juncaipeng 52520497, zhangting2020 26615455, Shixiaowei02 39303645, Heeenrrry 28379894,XieYunshen 32428676, Dong Daxiang 35550832, phlrain 43953930, qili93 16605440. if [ "${API_FILE}" == "CMakeLists.txt" ];then echo_line="You must have one RD (wanghuancoder, luotao1, Aurelius84, XiaoguangHu01 or qili93) approval for CMakeLists.txt, which manages the compilation parameter.\n" check_approval 1 wanghuancoder luotao1 Aurelius84 XiaoguangHu01 qili93 @@ -200,11 +200,11 @@ for API_FILE in ${API_FILES[*]}; do echo_line="You must have (fuyinno4 (Recommend), raindrops2sea) approval for ${API_FILE} changes.\n" check_approval 1 fuyinno4 raindrops2sea elif [ "${API_FILE}" == "paddle/scripts/paddle_build.bat" ] || [ "${API_FILE}" == "tools/windows/run_unittests.sh" ]; then - echo_line="You must have one RD (zhouwei25 (Recommend), wanghuancoder, luotao1 or Aurelius84) approval for ${API_FILE} changes, which manages the Paddle CI task on Windows.\n" - check_approval 1 zhouwei25 wanghuancoder luotao1 Aurelius84 + echo_line="You must have one RD (zhwesky2010 (Recommend), wanghuancoder, luotao1 or Aurelius84) approval for ${API_FILE} changes, which manages the Paddle CI task on Windows.\n" + check_approval 1 zhwesky2010 wanghuancoder luotao1 Aurelius84 elif [ "${API_FILE}" == "tools/parallel_UT_rule.py" ]; then - echo_line="You must have one RD (zhouwei25 (Recommend), wanghuancoder, luotao1 or Aurelius84) approval for ${API_FILE} changes, which manages the rule of running unittest with a same GPU. If the unittest failed due to Insufficient GPU memory or CUBLAS_STATUS_ALLOC_FAILED, you can remove it from ${API_FILE}.\n" - check_approval 1 zhouwei25 wanghuancoder luotao1 Aurelius84 + echo_line="You must have one RD (zhwesky2010 (Recommend), wanghuancoder, luotao1 or Aurelius84) approval for ${API_FILE} changes, which manages the rule of running unittest with a same GPU. If the unittest failed due to Insufficient GPU memory or CUBLAS_STATUS_ALLOC_FAILED, you can remove it from ${API_FILE}.\n" + check_approval 1 zhwesky2010 wanghuancoder luotao1 Aurelius84 elif [ "${API_FILE}" == "python/paddle/fluid/parallel_executor.py" ]; then echo_line="You must have one RD (Xreki, luotao1, zhhsplendid or Aurelius84) approval for ${API_FILE}, which manages the underlying code for PaddlePaddle.\n" check_approval 1 Xreki luotao1 zhhsplendid Aurelius84 @@ -565,13 +565,13 @@ fi UNITYBUILD_RULE_CHANGED=$(git diff --name-only upstream/$BRANCH | grep "unity_build_rule.cmake" || true) if [ -n "${UNITYBUILD_RULE_CHANGED}" -a -n "${GIT_PR_ID}" ]; then - echo_line="You must have one RD (Avin0323(Recommend) or zhouwei25 or + echo_line="You must have one RD (Avin0323(Recommend) or zhwesky2010 or wanghuancoder or luotao1 or Aurelius84) approval for modifying unity_build_rule.cmake which the rules of Unity Build." echo_line=$(echo ${echo_line}) - # Avin0323(23427135) zhouwei25(52485244) + # Avin0323(23427135) zhwesky2010(52485244) # wanghuancoder(26922892) luotao1(6836917) - check_approval 1 Avin0323 zhouwei25 wanghuancoder luotao1 Aurelius84 + check_approval 1 Avin0323 zhwesky2010 wanghuancoder luotao1 Aurelius84 fi if [ -n "${echo_list}" ];then From 8cee5690d76f6b32119f22266a6da196f1b6c178 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:22:35 +0800 Subject: [PATCH 064/194] fix coverage kill 9 bug (#54905) * set make -j18,test=document_fix * set make -j18,test=document_fix * set make -j18,test=document_fix --- paddle/scripts/paddle_build.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index f9df3a542ffff..286909b73e263 100644 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -3552,9 +3552,11 @@ EOF if [ "$3" != "" ]; then parallel_number=$3 fi - export MAX_JOBS=${parallel_number} + # reset ccache zero stats for collect PR's actual hit rate - + if [ "${MAX_JOBS}" == "" ]; then + export MAX_JOBS=${parallel_number} + fi ccache -z cd .. if [ "${PYTHON_EXECUTABLE}" != "" ];then From 61abe526b6b3483d3fdd97d9b70b98c6067186f9 Mon Sep 17 00:00:00 2001 From: Galaxy1458 <55453380+Galaxy1458@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:29:43 +0800 Subject: [PATCH 065/194] [flags.cmake] add some options in flags.cmake (#56742) * add [-W -gc-sections] in flags.cmake * add [-W -gc-sections] in flags.cmake * add [-W -gc-sections] in flags.cmake * add [-W -gc-sections] in flags.cmake * add [-W -gc-sections] in flags.cmake * add [-W -gc-sections] in flags.cmake --- cmake/flags.cmake | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmake/flags.cmake b/cmake/flags.cmake index a32dea08e5bff..7a4956e6e1556 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -141,6 +141,11 @@ if(NOT WIN32) set(COMMON_FLAGS -fPIC -fno-omit-frame-pointer + -pipe + -ffunction-sections + -fdata-sections + -Wl + -gc-sections -Werror -Wall -Wextra From 962f67d21542a91f1c7ce3224795901ff25e6d11 Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:37:05 +0800 Subject: [PATCH 066/194] [IR] Generate pd_op.parsed.yaml from pd_op.yaml (#56674) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Generate pd_op.parsed.yaml from pd_op.yaml * Generate pd_op.parsed.yaml from pd_op.yaml * fix bug * bug fix * bug fix * bug fix * 向pd_ops.yaml中新增算子 & 修改pd_ops.parsed.yaml存放路径 * 修复路径依赖bug & 添加 .gitignore文件 * fix bug - compat input args in save_combine op * fix compat file * fix set_value_with_tensor yaml * split backward op in original yaml file * add send_v2 & recv_v2 --- .../ir/dialect/paddle_dialect/ir/.gitignore | 2 + .../dialect/paddle_dialect/ir/CMakeLists.txt | 56 +++++- .../ir/dialect/paddle_dialect/ir/pd_ops.yaml | 160 ++++++++++++++++++ .../paddle_dialect/ir/pd_ops_backward.yaml | 9 + .../fluid/operators/generator/CMakeLists.txt | 2 +- .../fluid/operators/generator/parse_utils.py | 23 ++- .../fluid/operators/generator/type_mapping.py | 6 +- paddle/phi/api/yaml/op_compat.yaml | 4 + 8 files changed, 243 insertions(+), 19 deletions(-) create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/.gitignore create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops_backward.yaml diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/.gitignore b/paddle/fluid/ir/dialect/paddle_dialect/ir/.gitignore new file mode 100644 index 0000000000000..83769fe882624 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/.gitignore @@ -0,0 +1,2 @@ +generated/* +generated/** diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index d7269369ae1a3..83d38a676e5bd 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -2,6 +2,9 @@ set(PD_DIALECT_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/fluid/ir/dialect/paddle_dialect/ir") # Generate pd_dialect files defining op using op_gen_file +set(op_gen_parsed_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parse_op.py) + set(op_gen_file ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/op_generator/op_gen.py) set(op_compat_yaml_file ${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml) @@ -23,11 +26,22 @@ set(fused_op_forward_yaml_file set(fused_op_backward_yaml_file ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parsed_ops/fused_backward.parsed.yaml ) -set(op_yaml_file3 - ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml) + +set(pd_op_forward_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml) + +set(pd_op_backward_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops_backward.yaml +) + +set(parsed_op_dir + ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/paddle_dialect/ir/generated) + +set(op_yaml_file3 ${parsed_op_dir}/pd_ops.parsed.yaml) +set(op_yaml_file4 ${parsed_op_dir}/pd_ops_backward.parsed.yaml) set(op_yaml_files - ${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${fused_op_forward_yaml_file},${fused_op_backward_yaml_file},${op_yaml_file3} + ${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${fused_op_forward_yaml_file},${fused_op_backward_yaml_file},${op_yaml_file3},${op_yaml_file4} ) set(op_namespace paddle,dialect) set(dialect_name pd) @@ -39,6 +53,15 @@ set(op_source_file_tmp ${op_source_file}.tmp) set(op_vjp_source_file ${PD_DIALECT_BINARY_DIR}/pd_op_vjp.cc) set(op_vjp_source_file_tmp ${op_vjp_source_file}.tmp) +add_custom_command( + OUTPUT ${op_yaml_file3} ${op_yaml_file4} + COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_op_dir} + COMMAND ${PYTHON_EXECUTABLE} ${op_gen_parsed_yaml_file} --op_yaml_path + ${pd_op_forward_yaml_file} --output_path ${op_yaml_file3} + COMMAND ${PYTHON_EXECUTABLE} ${op_gen_parsed_yaml_file} --op_yaml_path + ${pd_op_backward_yaml_file} --output_path ${op_yaml_file4} --backward + VERBATIM) + add_custom_command( OUTPUT ${op_header_file} ${op_source_file} ${op_vjp_source_file} COMMAND @@ -55,9 +78,14 @@ add_custom_command( ${op_vjp_source_file} COMMENT "copy_if_different ${op_header_file} ${op_source_file} ${op_vjp_source_file}" - DEPENDS ${op_gen_file} ${op_forward_yaml_file1} ${op_forward_yaml_file2} - ${op_backward_yaml_file1} ${op_backward_yaml_file2} + DEPENDS ${op_gen_file} + ${op_forward_yaml_file1} + ${op_forward_yaml_file2} + ${op_backward_yaml_file1} + ${op_backward_yaml_file2} ${op_compat_yaml_file} + ${op_yaml_file3} + ${op_yaml_file4} VERBATIM) set(api_gen_file @@ -79,9 +107,14 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy_if_different ${api_source_file_tmp} ${api_source_file} COMMENT "copy_if_different ${api_header_file} ${api_source_file}" - DEPENDS ${api_gen_file} ${op_forward_yaml_file1} ${op_forward_yaml_file2} - ${op_backward_yaml_file1} ${op_backward_yaml_file2} + DEPENDS ${api_gen_file} + ${op_forward_yaml_file1} + ${op_forward_yaml_file2} + ${op_backward_yaml_file1} + ${op_backward_yaml_file2} ${op_compat_yaml_file} + ${op_yaml_file3} + ${op_yaml_file4} VERBATIM) set(python_c_gen_file @@ -105,9 +138,14 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E copy_if_different ${python_c_source_file_tmp} ${python_c_source_file} COMMENT "copy_if_different ${python_c_header_file} ${python_c_source_file}" - DEPENDS ${python_c_gen_file} ${op_forward_yaml_file1} ${op_forward_yaml_file2} - ${op_backward_yaml_file1} ${op_backward_yaml_file2} + DEPENDS ${python_c_gen_file} + ${op_forward_yaml_file1} + ${op_forward_yaml_file2} + ${op_backward_yaml_file1} + ${op_backward_yaml_file2} ${op_compat_yaml_file} + ${op_yaml_file3} + ${op_yaml_file4} VERBATIM) add_custom_target(static_op_function_gen ALL DEPENDS ${python_c_header_file} diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml new file mode 100644 index 0000000000000..a5d2f42fc1ba1 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml @@ -0,0 +1,160 @@ +- op : add_n_ + args : (Tensor[] inputs) + output : Tensor(out) + infer_meta: + func: AddNInferMeta + param: [inputs] + kernel: + func: add_n + param: [inputs] + backward: add_n_grad + +- op : add_n_with_kernel + args : (Tensor[] inputs) + output : Tensor(out) + infer_meta: + func: AddNInferMeta + param: [inputs] + kernel: + func: add_n + param: [inputs] + backward: add_n_grad + +- op : assert + args : (Tensor cond, Tensor[] data, int64_t summarize = -1) + output : + kernel : + func : assert + param : [cond, data, summarize] + data_type : cond + +- op : assign_value + args : (int[] shape, DataType dtype, Scalar[] values, Place place = {}) + output : Tensor(out) + infer_meta : + func : AssignValueInferMeta + param: [shape, dtype] + kernel : + func : assign_value + param : [shape, dtype, values] + backend: place> + data_type : dtype + +- op : embedding_grad_sparse + args : (Tensor x, Tensor weight, Tensor out_grad, int64_t padding_idx = -1, bool sparse = false) + output : SelectedRows(weight_grad) + infer_meta: + func: EmbeddingGradSparseInferMeta + param: [weight] + kernel: + func: embedding_sparse_grad + param: [x, weight, out_grad, padding_idx, sparse] + data_type : weight + +- op : feed + args : (str name, int col) + output : Tensor(out) + +- op : fetch + args : (Tensor x, str name, int col) + output : Tensor(out) + infer_meta : + func : UnchangedInferMeta + param : [x] + kernel : + func : fetch + param : [x] + +- op : load_combine + args : (str file_path, bool load_as_fp16, bool model_from_memory) + output : Tensor[](Out) + kernel: + func: load_combine + param: [file_path, load_as_fp16, model_from_memory] + optional : Out + +- op : lod_array_length + args : (Tensor[] x) + output : Tensor(out) + +- op : print + args : (Tensor in, int first_n, str message, int summarize, bool print_tensor_name = true, bool print_tensor_type = true, bool print_tensor_shape = true, bool print_tensor_layout = true, bool print_tensor_lod = true, str print_phase = "BOTH", bool is_forward = true) + output : Tensor(out) + infer_meta: + func: UnchangedInferMeta + param: [in] + kernel : + func : print_kernel + param: [in, first_n, message, summarize, print_tensor_name, print_tensor_type, print_tensor_shape, print_tensor_layout, print_tensor_lod, print_phase, is_forward] + +- op : recv_v2 + args : (int[] out_shape = {}, DataType dtype = DataType::FLOAT32, int peer = 0, int ring_id = 0, bool use_calc_stream = false, bool dynamic_shape = false) + output : Tensor(out) + infer_meta: + func: RecvV2InferMeta + param: [peer, dtype, out_shape] + kernel : + func : recv_v2 + param : [ring_id, dynamic_shape, peer, out_shape, dtype, use_calc_stream] + data_type : dtype + +- op : save_combine + args : (Tensor[] x, str file_path, bool overwrite, bool save_as_fp16, bool save_to_memory) + output : Tensor(out) + kernel: + func: save_combine_tensor + param: [x, file_path, overwrite, save_as_fp16, save_to_memory] + optional : out + +- op : send_v2 + args : (Tensor x, int ring_id = 0, int peer = 0, bool use_calc_stream = false, bool dynamic_shape = false) + output : + infer_meta: + func: SendV2InferMeta + param: [peer, ring_id] + kernel : + func : send_v2 + param : [x, ring_id, dynamic_shape, peer, use_calc_stream] + +- op : set_value + args : (Tensor x, int64_t[] starts, int64_t[] ends, int64_t[] steps, int64_t[] axes, int64_t[] decrease_axes, int64_t[] none_axes, int64_t[] shape, Scalar[] values) + output : Tensor(out) + infer_meta: + func: SetValueInferMeta + param: [x] + kernel: + func: set_value + param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values] + inplace: (x -> out) + backward: set_value_grad + +- op : set_value_with_tensor + args : (Tensor x, Tensor values, int64_t[] starts, int64_t[] ends, int64_t[] steps, int64_t[] axes, int64_t[] decrease_axes, int64_t[] none_axes) + output : Tensor(out) + infer_meta: + func: SetValueInferMeta + param: [x] + kernel: + func: set_value_with_tensor + param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] + inplace: (x -> out) + backward: set_value_grad + +- op : shadow_feed + args : (Tensor x) + output : Tensor(out) + infer_meta: + func: UnchangedInferMeta + param: [x] + kernel: + func: shadow_feed + param: [x] + +- op : share_buffer_ + args : (Tensor[] x, bool[] share_dims_and_dtype = {}) + output : Tensor[](out){x.size()}, Tensor[](xout){x.size()} + +- op : write_to_array + args : (Tensor i, Tensor x) + output : Tensor[](out) + backward: write_to_array_grad diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops_backward.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops_backward.yaml new file mode 100644 index 0000000000000..c1cab00b20406 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops_backward.yaml @@ -0,0 +1,9 @@ +- backward_op : set_value_grad + args : (Tensor out_grad, Tensor values, int64_t[] starts, int64_t[] ends, int64_t[] steps, int64_t[] axes, int64_t[] decrease_axes, int64_t[] none_axes) + output : Tensor(x_grad), Tensor(values_grad) + infer_meta: + func: SetValueGradInferMeta + param: [out_grad, values] + kernel: + func: set_value_grad + param: [out_grad, starts, ends, steps, axes, decrease_axes, none_axes] diff --git a/paddle/fluid/operators/generator/CMakeLists.txt b/paddle/fluid/operators/generator/CMakeLists.txt index 91a975c8cf597..af346e402bf83 100644 --- a/paddle/fluid/operators/generator/CMakeLists.txt +++ b/paddle/fluid/operators/generator/CMakeLists.txt @@ -122,7 +122,6 @@ set(generated_static_argument_mapping_path ${CMAKE_SOURCE_DIR}/paddle/phi/ops/compat/generated_static_sig.cc) set(generated_sparse_argument_mapping_path ${CMAKE_SOURCE_DIR}/paddle/phi/ops/compat/generated_sparse_sig.cc) - execute_process( WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/fluid/operators/generator COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_op_dir} RESULTS_VARIABLE @@ -139,6 +138,7 @@ message( - ${legacy_bw_op_yaml_file} - ${fused_op_yaml_file} - ${static_op_yaml_file}") + execute_process( WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/paddle/fluid/operators/generator COMMAND ${PYTHON_EXECUTABLE} parse_op.py --op_yaml_path ${op_yaml_file} diff --git a/paddle/fluid/operators/generator/parse_utils.py b/paddle/fluid/operators/generator/parse_utils.py index ed061f9967a97..92834be0f01d5 100644 --- a/paddle/fluid/operators/generator/parse_utils.py +++ b/paddle/fluid/operators/generator/parse_utils.py @@ -150,6 +150,8 @@ def parse_output(op_name: str, s: str) -> Dict[str, str]: def parse_outputs(op_name: str, outputs: str) -> List[Dict]: + if outputs is None: + return [] outputs = parse_plain_list(outputs, sep=",") output_items = [] for output in outputs: @@ -169,6 +171,7 @@ def parse_candidates(s: str) -> Dict[str, Any]: delimiter = ">" if ">" in s else "," ordered = delimiter == ">" candidates = parse_plain_list(s, delimiter) + candidates = list(filter(None, candidates)) return {"ordered": ordered, "candidates": candidates} @@ -523,14 +526,20 @@ def parse_op_entry(op_entry: Dict[str, Any], name_field="op"): if is_base_op: # kernel - kernel = parse_kernel(op_name, op_entry["kernel"]) - if kernel["param"] is None: - kernel["param"] = input_names + attr_names + if "kernel" in op_entry: + kernel = parse_kernel(op_name, op_entry["kernel"]) + if kernel["param"] is None: + kernel["param"] = input_names + attr_names + op.update({"kernel": kernel}) # infer meta - infer_meta = parse_infer_meta(op_entry["infer_meta"]) - if infer_meta["param"] is None: - infer_meta["param"] = copy(kernel["param"]) + if "infer_meta" in op_entry: + infer_meta = parse_infer_meta(op_entry["infer_meta"]) + if infer_meta["param"] is None: + infer_meta["param"] = copy(kernel["param"]) + op.update({"infer_meta": infer_meta}) + # else: + # assert(outputs == []), f"No infer_meta is given in {op_name}." # inplace if "inplace" in op_entry: @@ -544,8 +553,6 @@ def parse_op_entry(op_entry: Dict[str, Any], name_field="op"): view_pairs = None op.update( { - "infer_meta": infer_meta, - "kernel": kernel, "inplace": inplace_pairs, "view": view_pairs, } diff --git a/paddle/fluid/operators/generator/type_mapping.py b/paddle/fluid/operators/generator/type_mapping.py index e6b59b7823abe..a2e1b8efb2565 100644 --- a/paddle/fluid/operators/generator/type_mapping.py +++ b/paddle/fluid/operators/generator/type_mapping.py @@ -79,7 +79,11 @@ 'str[]': 'std::vector', } -output_type_map = {'Tensor': 'Tensor', 'Tensor[]': 'std::vector'} +output_type_map = { + 'Tensor': 'Tensor', + 'Tensor[]': 'std::vector', + 'SelectedRows': 'SelectedRows', +} # ------------------------------ phi attr ------------------------------ phi_attr_types_map = attr_types_map.copy() diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 575b01c33afef..29c5983176d1d 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2375,6 +2375,10 @@ extra : attrs : [bool use_mkldnn = false, bool use_cudnn = false] +- op : save_combine + inputs : + {x : X} + - op : scale backward : scale_grad inputs : From 17e4be21cc802cb72a4a23cc6d81cbe266724f16 Mon Sep 17 00:00:00 2001 From: cyberslack_lee Date: Fri, 1 Sep 2023 10:49:26 +0800 Subject: [PATCH 067/194] [clang-tidy] No.34,36 enable performance-noexcept-move-constructor,modernize-use-transparent-functors (#56261) * fix * fix * CI * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * CI * fix * CI --- .clang-tidy | 4 +-- .../dist_model_tensor_wrapper.cc | 5 +-- .../dist_model_tensor_wrapper.h | 4 +-- .../eager_deletion_pass.cc | 2 +- .../memory_optimize_pass/memory_reuse_pass.cc | 2 +- .../ir/mkldnn/conv_bias_mkldnn_fuse_pass.cc | 5 +-- .../shuffle_channel_mkldnn_detect_pass.cc | 34 ++++++++++--------- .../ir/shuffle_channel_detect_pass.cc | 34 ++++++++++--------- .../analysis/passes/memory_optimize_pass.cc | 6 ++-- paddle/fluid/inference/api/api.cc | 4 +-- paddle/fluid/inference/api/paddle_api.h | 4 +-- .../fluid/operators/fused/resnet_unit_op.cc | 9 ++--- paddle/ir/core/op_base.cc | 4 +-- paddle/ir/core/op_base.h | 4 +-- paddle/phi/api/include/tensor.h | 4 +-- paddle/phi/api/lib/tensor.cc | 2 +- paddle/phi/backends/cpu/cpu_context.cc | 4 +-- paddle/phi/backends/gpu/gpu_context.cc | 4 +-- paddle/phi/core/dense_tensor.cc | 2 +- paddle/phi/core/dense_tensor.h | 2 +- paddle/phi/core/device_context.cc | 6 ++-- paddle/phi/core/device_context.h | 4 +-- paddle/phi/core/sparse_coo_tensor.cc | 2 +- paddle/phi/core/sparse_coo_tensor.h | 2 +- paddle/phi/core/string_tensor.cc | 2 +- paddle/phi/core/string_tensor.h | 2 +- .../kernels/cpu/class_center_sample_kernel.cc | 2 +- .../cpu/weighted_sample_neighbors_kernel.cc | 2 +- 28 files changed, 84 insertions(+), 77 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c0d5b09ac1f16..9934d5012bcd7 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -186,7 +186,7 @@ modernize-use-equals-default, -modernize-use-noexcept, modernize-use-nullptr, modernize-use-override, --modernize-use-transparent-functors, +modernize-use-transparent-functors, -modernize-use-uncaught-exceptions, performance-faster-string-find, -performance-for-range-copy, @@ -197,7 +197,7 @@ performance-inefficient-string-concatenation, -performance-move-const-arg, -performance-move-constructor-init, -performance-no-automatic-move, --performance-noexcept-move-constructor, +performance-noexcept-move-constructor, -performance-trivially-destructible, -performance-type-promotion-in-math-fn, -performance-unnecessary-copy-initialization, diff --git a/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.cc b/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.cc index 72a544f9f1013..87832c33bf7fd 100644 --- a/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.cc +++ b/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.cc @@ -78,7 +78,8 @@ DistModelDataBuf& DistModelDataBuf::operator=(const DistModelDataBuf& other) { return *this; } -DistModelDataBuf& DistModelDataBuf::operator=(DistModelDataBuf&& other) { +DistModelDataBuf& DistModelDataBuf::operator=( + DistModelDataBuf&& other) noexcept { data_ = other.data_; memory_owned_ = other.memory_owned_; length_ = other.length_; @@ -88,7 +89,7 @@ DistModelDataBuf& DistModelDataBuf::operator=(DistModelDataBuf&& other) { return *this; } -DistModelDataBuf::DistModelDataBuf(DistModelDataBuf&& other) +DistModelDataBuf::DistModelDataBuf(DistModelDataBuf&& other) noexcept : data_(other.data_), length_(other.length_), memory_owned_(other.memory_owned_) { diff --git a/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.h b/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.h index 459e609762d84..ef175aa46b03d 100644 --- a/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.h +++ b/paddle/fluid/distributed/fleet_executor/dist_model_tensor_wrapper.h @@ -61,8 +61,8 @@ class DistModelDataBuf { void Resize(size_t length); DistModelDataBuf& operator=(const DistModelDataBuf& other); - DistModelDataBuf& operator=(DistModelDataBuf&& other); - DistModelDataBuf(DistModelDataBuf&& other); + DistModelDataBuf& operator=(DistModelDataBuf&& other) noexcept; + DistModelDataBuf(DistModelDataBuf&& other) noexcept; DistModelDataBuf(const DistModelDataBuf& other); private: diff --git a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc index ef0f6dde3d7f6..b85a88eb7e0b6 100644 --- a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc +++ b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc @@ -70,7 +70,7 @@ static int64_t GetMemorySize( std::accumulate(dims.begin(), dims.end(), static_cast(1), - std::multiplies()); + std::multiplies()); // NOLINT } // Split all variables in the graph into phi::DenseTensor and diff --git a/paddle/fluid/framework/ir/memory_optimize_pass/memory_reuse_pass.cc b/paddle/fluid/framework/ir/memory_optimize_pass/memory_reuse_pass.cc index 520ab133b45ed..e53687d095588 100644 --- a/paddle/fluid/framework/ir/memory_optimize_pass/memory_reuse_pass.cc +++ b/paddle/fluid/framework/ir/memory_optimize_pass/memory_reuse_pass.cc @@ -121,7 +121,7 @@ int64_t MemoryReusePass::GetMemorySize(const details::VarHandle &var) const { return std::accumulate(shapes.begin(), shapes.end(), static_cast(1), - std::multiplies()) * + std::multiplies<>()) * sizeof_dtype; } diff --git a/paddle/fluid/framework/ir/mkldnn/conv_bias_mkldnn_fuse_pass.cc b/paddle/fluid/framework/ir/mkldnn/conv_bias_mkldnn_fuse_pass.cc index bd61d58a3b2b0..b2903a1337f3f 100644 --- a/paddle/fluid/framework/ir/mkldnn/conv_bias_mkldnn_fuse_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/conv_bias_mkldnn_fuse_pass.cc @@ -354,8 +354,9 @@ void ConvBiasFusePass::FuseConvBias(ir::Graph* graph, "must have same shape, but they are different: %s, %s.", conv_bias_tensor->dims(), eltwise_bias_tensor->dims())); - *conv_bias_tensor = tensor_apply_eltwise( - *conv_bias_tensor, *eltwise_bias_tensor, std::plus()); + *conv_bias_tensor = tensor_apply_eltwise(*conv_bias_tensor, + *eltwise_bias_tensor, + std::plus()); // NOLINT conv->Op()->SetOutput("Output", std::vector({eltwise_out->Name()})); diff --git a/paddle/fluid/framework/ir/mkldnn/shuffle_channel_mkldnn_detect_pass.cc b/paddle/fluid/framework/ir/mkldnn/shuffle_channel_mkldnn_detect_pass.cc index 1e5a1c8265f15..5cf9a973061f0 100644 --- a/paddle/fluid/framework/ir/mkldnn/shuffle_channel_mkldnn_detect_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/shuffle_channel_mkldnn_detect_pass.cc @@ -141,14 +141,15 @@ void ShuffleChannelMKLDNNDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape1_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape1_shape[i] = std::accumulate(x_shape1.begin(), - x_shape1.end(), - static_cast(1), - std::multiplies()) / - std::accumulate(reshape1_shape.begin(), - reshape1_shape.end(), - static_cast(-1), - std::multiplies()); + reshape1_shape[i] = + std::accumulate(x_shape1.begin(), + x_shape1.end(), + static_cast(1), + std::multiplies()) / // NOLINT + std::accumulate(reshape1_shape.begin(), + reshape1_shape.end(), + static_cast(-1), + std::multiplies()); // NOLINT break; } } @@ -160,14 +161,15 @@ void ShuffleChannelMKLDNNDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape2_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape2_shape[i] = std::accumulate(x_shape2.begin(), - x_shape2.end(), - static_cast(1), - std::multiplies()) / - std::accumulate(reshape2_shape.begin(), - reshape2_shape.end(), - static_cast(-1), - std::multiplies()); + reshape2_shape[i] = + std::accumulate(x_shape2.begin(), + x_shape2.end(), + static_cast(1), + std::multiplies()) / // NOLINT + std::accumulate(reshape2_shape.begin(), + reshape2_shape.end(), + static_cast(-1), + std::multiplies()); // NOLINT break; } } diff --git a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc index 1157fdd91a07c..5d38a9c536ad1 100644 --- a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc +++ b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc @@ -141,14 +141,15 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape1_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape1_shape[i] = std::accumulate(x_shape1.begin(), - x_shape1.end(), - static_cast(1), - std::multiplies()) / - std::accumulate(reshape1_shape.begin(), - reshape1_shape.end(), - static_cast(-1), - std::multiplies()); + reshape1_shape[i] = + std::accumulate(x_shape1.begin(), + x_shape1.end(), + static_cast(1), + std::multiplies()) / // NOLINT + std::accumulate(reshape1_shape.begin(), + reshape1_shape.end(), + static_cast(-1), + std::multiplies()); // NOLINT break; } } @@ -160,14 +161,15 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape2_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape2_shape[i] = std::accumulate(x_shape2.begin(), - x_shape2.end(), - static_cast(1), - std::multiplies()) / - std::accumulate(reshape2_shape.begin(), - reshape2_shape.end(), - static_cast(-1), - std::multiplies()); + reshape2_shape[i] = + std::accumulate(x_shape2.begin(), + x_shape2.end(), + static_cast(1), + std::multiplies()) / // NOLINT + std::accumulate(reshape2_shape.begin(), + reshape2_shape.end(), + static_cast(-1), + std::multiplies()); // NOLINT break; } } diff --git a/paddle/fluid/inference/analysis/passes/memory_optimize_pass.cc b/paddle/fluid/inference/analysis/passes/memory_optimize_pass.cc index 9fe1ba1f15d02..b1f7cd5c301da 100644 --- a/paddle/fluid/inference/analysis/passes/memory_optimize_pass.cc +++ b/paddle/fluid/inference/analysis/passes/memory_optimize_pass.cc @@ -97,7 +97,7 @@ void MemoryOptimizePass::CollectLifeCycle( auto var_bytes = std::accumulate(in_shape.begin(), in_shape.end(), (int64_t)1, - std::multiplies()); + std::multiplies<>()); persis_byte += paddle::framework::SizeOfType(node->Var()->GetDataType()) * var_bytes; @@ -183,8 +183,8 @@ void MemoryOptimizePass::CollectVarMemorySize( if (v < 0) v = fake_batch_size; } - int size = std::accumulate( - shape.begin(), shape.end(), 1, std::multiplies()); + int size = + std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<>()); (*space_table)[node->Var()->Name()] = size * paddle::framework::SizeOfType(node->Var()->GetDataType()); } diff --git a/paddle/fluid/inference/api/api.cc b/paddle/fluid/inference/api/api.cc index a15f8be18bf34..a1b61ac3552e7 100644 --- a/paddle/fluid/inference/api/api.cc +++ b/paddle/fluid/inference/api/api.cc @@ -40,7 +40,7 @@ int PaddleDtypeSize(PaddleDType dtype) { } } -PaddleBuf::PaddleBuf(PaddleBuf &&other) +PaddleBuf::PaddleBuf(PaddleBuf &&other) noexcept : data_(other.data_), length_(other.length_), memory_owned_(other.memory_owned_) { @@ -74,7 +74,7 @@ PaddleBuf &PaddleBuf::operator=(const PaddleBuf &other) { return *this; } -PaddleBuf &PaddleBuf::operator=(PaddleBuf &&other) { +PaddleBuf &PaddleBuf::operator=(PaddleBuf &&other) noexcept { // only the buffer with external memory can be copied data_ = other.data_; length_ = other.length_; diff --git a/paddle/fluid/inference/api/paddle_api.h b/paddle/fluid/inference/api/paddle_api.h index ba439a5646999..af16aead74129 100644 --- a/paddle/fluid/inference/api/paddle_api.h +++ b/paddle/fluid/inference/api/paddle_api.h @@ -137,9 +137,9 @@ class PD_INFER_DECL PaddleBuf { ~PaddleBuf() { Free(); } PaddleBuf& operator=(const PaddleBuf&); - PaddleBuf& operator=(PaddleBuf&&); + PaddleBuf& operator=(PaddleBuf&&) noexcept; PaddleBuf() = default; - PaddleBuf(PaddleBuf&& other); + PaddleBuf(PaddleBuf&& other) noexcept; private: void Free(); diff --git a/paddle/fluid/operators/fused/resnet_unit_op.cc b/paddle/fluid/operators/fused/resnet_unit_op.cc index 1547f20801b56..948e8fdc9491e 100644 --- a/paddle/fluid/operators/fused/resnet_unit_op.cc +++ b/paddle/fluid/operators/fused/resnet_unit_op.cc @@ -21,10 +21,11 @@ namespace operators { // Shape of bitmask static framework::DDim GetBitmaskDims(std::vector out_shape) { int c = out_shape.back(); - int64_t nhw = - std::accumulate( - out_shape.begin(), out_shape.end(), 1, std::multiplies()) / - c; + int64_t nhw = std::accumulate(out_shape.begin(), + out_shape.end(), + 1, + std::multiplies()) / // NOLINT + c; int32_t c_int32_elems = ((c + 63) & ~63) / 32; int32_t nhw_int32_elems = ((nhw + 31) & ~31); std::vector bitmask_shape = {nhw_int32_elems, c_int32_elems, 1}; diff --git a/paddle/ir/core/op_base.cc b/paddle/ir/core/op_base.cc index 7ddc948c66a2d..6f6dca0cdc125 100644 --- a/paddle/ir/core/op_base.cc +++ b/paddle/ir/core/op_base.cc @@ -18,13 +18,13 @@ InterfaceValue::~InterfaceValue() { if (model_) free(model_); } -InterfaceValue::InterfaceValue(InterfaceValue&& val) { +InterfaceValue::InterfaceValue(InterfaceValue&& val) noexcept { type_id_ = val.type_id_; model_ = val.model_; val.model_ = nullptr; } -InterfaceValue& InterfaceValue::operator=(InterfaceValue&& val) { +InterfaceValue& InterfaceValue::operator=(InterfaceValue&& val) noexcept { swap(std::move(val)); return *this; } diff --git a/paddle/ir/core/op_base.h b/paddle/ir/core/op_base.h index bfa7705e6ff4c..1b4690f9099e9 100644 --- a/paddle/ir/core/op_base.h +++ b/paddle/ir/core/op_base.h @@ -43,9 +43,9 @@ class IR_API InterfaceValue { InterfaceValue() = default; explicit InterfaceValue(TypeId type_id) : type_id_(type_id) {} InterfaceValue(const InterfaceValue &) = delete; - InterfaceValue(InterfaceValue &&); + InterfaceValue(InterfaceValue &&) noexcept; InterfaceValue &operator=(const InterfaceValue &) = delete; - InterfaceValue &operator=(InterfaceValue &&); + InterfaceValue &operator=(InterfaceValue &&) noexcept; ~InterfaceValue(); void swap(InterfaceValue &&val) { using std::swap; diff --git a/paddle/phi/api/include/tensor.h b/paddle/phi/api/include/tensor.h index afb17f62035d1..39e6258e7e86c 100644 --- a/paddle/phi/api/include/tensor.h +++ b/paddle/phi/api/include/tensor.h @@ -96,7 +96,7 @@ class PADDLE_API Tensor final { /** * @brief Construct a new Tensor object by move */ - Tensor(Tensor&&) = default; + Tensor(Tensor&&) noexcept = default; /** * @brief Construct a new Tensor object by a TensorBase pointer @@ -522,7 +522,7 @@ class PADDLE_API Tensor final { * @param x * @return Tensor& */ - Tensor& operator=(Tensor&& x) &; + Tensor& operator=(Tensor&& x) & noexcept; /** * @brief Tensor operants diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index c6b055acf4e88..d43ebe846fcd8 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -399,7 +399,7 @@ void Tensor::reset() { Tensor &Tensor::operator=(const Tensor &x) & = default; -Tensor &Tensor::operator=(Tensor &&x) & { +Tensor &Tensor::operator=(Tensor &&x) &noexcept { impl_ = std::move(x.impl_); autograd_meta_ = std::move(x.autograd_meta_); name_ = std::move(x.name_); diff --git a/paddle/phi/backends/cpu/cpu_context.cc b/paddle/phi/backends/cpu/cpu_context.cc index d42189e00eeb8..1a3ae7ae351d6 100644 --- a/paddle/phi/backends/cpu/cpu_context.cc +++ b/paddle/phi/backends/cpu/cpu_context.cc @@ -65,9 +65,9 @@ CPUContext::CPUContext(const Place& place) CPUContext::~CPUContext() = default; -CPUContext::CPUContext(CPUContext&&) = default; +CPUContext::CPUContext(CPUContext&&) = default; // NOLINT -CPUContext& CPUContext::operator=(CPUContext&&) = default; +CPUContext& CPUContext::operator=(CPUContext&&) = default; // NOLINT Eigen::DefaultDevice* CPUContext::eigen_device() const { return impl_->GetEigenDevice(); diff --git a/paddle/phi/backends/gpu/gpu_context.cc b/paddle/phi/backends/gpu/gpu_context.cc index 20b3525ba822e..d6ce3e750f65f 100644 --- a/paddle/phi/backends/gpu/gpu_context.cc +++ b/paddle/phi/backends/gpu/gpu_context.cc @@ -814,9 +814,9 @@ struct GPUContext::Impl { thread_local AttributeMap GPUContext::Impl::dnn_attrs_ = {}; -GPUContext::GPUContext(GPUContext&&) = default; +GPUContext::GPUContext(GPUContext&&) = default; // NOLINT -GPUContext& GPUContext::operator=(GPUContext&&) = default; +GPUContext& GPUContext::operator=(GPUContext&&) = default; // NOLINT GPUContext::GPUContext(const GPUPlace& place, bool init, int stream_priority) : DeviceContext(), impl_(std::make_unique(place)) { diff --git a/paddle/phi/core/dense_tensor.cc b/paddle/phi/core/dense_tensor.cc index 89f422cfdf406..20b1da1efe39d 100644 --- a/paddle/phi/core/dense_tensor.cc +++ b/paddle/phi/core/dense_tensor.cc @@ -79,7 +79,7 @@ DenseTensor& DenseTensor::operator=(const DenseTensor& other) { return *this; } -DenseTensor& DenseTensor::operator=(DenseTensor&& other) { +DenseTensor& DenseTensor::operator=(DenseTensor&& other) noexcept { meta_ = std::move(other.meta_); std::swap(holder_, other.holder_); storage_properties_ = std::move(other.storage_properties_); diff --git a/paddle/phi/core/dense_tensor.h b/paddle/phi/core/dense_tensor.h index 6b028fda6c432..cf5803852a1fe 100644 --- a/paddle/phi/core/dense_tensor.h +++ b/paddle/phi/core/dense_tensor.h @@ -69,7 +69,7 @@ class DenseTensor : public TensorBase, /// \brief DenseTensor shallow copy assignment. DenseTensor& operator=(const DenseTensor& other); - DenseTensor& operator=(DenseTensor&& other); + DenseTensor& operator=(DenseTensor&& other) noexcept; DenseTensor(); diff --git a/paddle/phi/core/device_context.cc b/paddle/phi/core/device_context.cc index 1715c0965a799..7f636cd2a4831 100644 --- a/paddle/phi/core/device_context.cc +++ b/paddle/phi/core/device_context.cc @@ -312,12 +312,12 @@ DeviceContext::DeviceContext(const DeviceContext& other) { #endif } -DeviceContext::DeviceContext(DeviceContext&& other) { +DeviceContext::DeviceContext(DeviceContext&& other) noexcept { impl_ = std::move(other.impl_); } -DeviceContext& DeviceContext::operator=(DeviceContext&& other) = default; - +DeviceContext& DeviceContext::operator=(DeviceContext&& other) noexcept = + default; DeviceContext::~DeviceContext() = default; void DeviceContext::SetAllocator(const Allocator* allocator) { diff --git a/paddle/phi/core/device_context.h b/paddle/phi/core/device_context.h index b670392551799..e0d555fe01b3e 100644 --- a/paddle/phi/core/device_context.h +++ b/paddle/phi/core/device_context.h @@ -48,12 +48,12 @@ class PADDLE_API DeviceContext { /** * @brief Move construct. */ - DeviceContext(DeviceContext&&); + DeviceContext(DeviceContext&&) noexcept; /** * @brief Move assign operator. */ - DeviceContext& operator=(DeviceContext&&); + DeviceContext& operator=(DeviceContext&&) noexcept; /** * @brief Default destruct. diff --git a/paddle/phi/core/sparse_coo_tensor.cc b/paddle/phi/core/sparse_coo_tensor.cc index 4d34c8f1d7336..f8517fb0ff007 100644 --- a/paddle/phi/core/sparse_coo_tensor.cc +++ b/paddle/phi/core/sparse_coo_tensor.cc @@ -21,7 +21,7 @@ SparseCooTensor::SparseCooTensor() { this->SetMember(non_zero_indices, non_zero_elements, {1}, true); } -SparseCooTensor::SparseCooTensor(SparseCooTensor&& other) { // NOLINT +SparseCooTensor::SparseCooTensor(SparseCooTensor&& other) noexcept { this->non_zero_elements_ = other.non_zero_elements_; this->non_zero_indices_ = other.non_zero_indices_; this->coalesced_ = other.coalesced_; diff --git a/paddle/phi/core/sparse_coo_tensor.h b/paddle/phi/core/sparse_coo_tensor.h index 48b001cbf1620..d0759bedcf557 100644 --- a/paddle/phi/core/sparse_coo_tensor.h +++ b/paddle/phi/core/sparse_coo_tensor.h @@ -55,7 +55,7 @@ class SparseCooTensor : public TensorBase, SparseCooTensor(const SparseCooTensor& other); /// \brief move constructor - SparseCooTensor(SparseCooTensor&& other); + SparseCooTensor(SparseCooTensor&& other) noexcept; /// \brief SparseCooTensor shallow copy assignment. SparseCooTensor& operator=(const SparseCooTensor& other); diff --git a/paddle/phi/core/string_tensor.cc b/paddle/phi/core/string_tensor.cc index 29af747c55ed9..35c59c2d8d787 100644 --- a/paddle/phi/core/string_tensor.cc +++ b/paddle/phi/core/string_tensor.cc @@ -49,7 +49,7 @@ StringTensor& StringTensor::operator=(const StringTensor& other) { return *this; } -StringTensor& StringTensor::operator=(StringTensor&& other) { // NOLINT +StringTensor& StringTensor::operator=(StringTensor&& other) noexcept { meta_ = std::move(other.meta_); std::swap(holder_, other.holder_); return *this; diff --git a/paddle/phi/core/string_tensor.h b/paddle/phi/core/string_tensor.h index ccf89d88e4283..4fecd83d557cf 100644 --- a/paddle/phi/core/string_tensor.h +++ b/paddle/phi/core/string_tensor.h @@ -56,7 +56,7 @@ class StringTensor : public TensorBase, /// \brief StringTensor shallow copy assignment. StringTensor& operator=(const StringTensor& other); - StringTensor& operator=(StringTensor&& other); + StringTensor& operator=(StringTensor&& other) noexcept; /// \brief Destroy the tensor object and release exclusive resources. virtual ~StringTensor() = default; diff --git a/paddle/phi/kernels/cpu/class_center_sample_kernel.cc b/paddle/phi/kernels/cpu/class_center_sample_kernel.cc index 5693c5da72baa..d70a11c2cf185 100644 --- a/paddle/phi/kernels/cpu/class_center_sample_kernel.cc +++ b/paddle/phi/kernels/cpu/class_center_sample_kernel.cc @@ -63,7 +63,7 @@ void ClassCenterSampleKernel(const Context& dev_ctx, auto* label_ptr = label.data(); // get unique positive class center by ascending - std::set> unique_label; + std::set> unique_label; // NOLINT for (int64_t i = 0; i < numel; ++i) { unique_label.insert(label_ptr[i]); } diff --git a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc index 713aad7332d63..f72f98f5cf5bd 100644 --- a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc +++ b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc @@ -63,7 +63,7 @@ void SampleWeightedNeighbors( bool return_eids) { std::priority_queue, std::vector>, - std::greater>> + std::greater>> // NOLINT min_heap; for (size_t i = 0; i < out_src.size(); i++) { float weight_key = log2(dice_distribution(rng)) * (1 / out_weight[i]); From 0213a19c1d8d949164761da772fd99958ed86e57 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Fri, 1 Sep 2023 11:16:25 +0800 Subject: [PATCH 068/194] =?UTF-8?q?=E3=80=90NewIR=E3=80=91enable=20dy2st?= =?UTF-8?q?=20new=20IR=20test=20(#56631)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * enable dy2st newIR test * fix 2023-08-29 * fix_2 --- .../dygraph_to_static_util.py | 25 +++++++++---------- test/dygraph_to_static/test_assert.py | 1 + test/dygraph_to_static/test_ast_util.py | 2 ++ .../test_backward_without_params.py | 3 +++ .../test_basic_api_transformation.py | 8 ++++++ test/dygraph_to_static/test_cinn.py | 2 ++ test/dygraph_to_static/test_cinn_prim.py | 7 +++++- .../test_closure_analysis.py | 6 +++++ .../test_convert_call_generator.py | 3 ++- .../test_cpu_cuda_to_tensor.py | 6 +++++ 10 files changed, 48 insertions(+), 15 deletions(-) diff --git a/test/dygraph_to_static/dygraph_to_static_util.py b/test/dygraph_to_static/dygraph_to_static_util.py index ca04f8d0c2046..69a7c3ba93fc9 100644 --- a/test/dygraph_to_static/dygraph_to_static_util.py +++ b/test/dygraph_to_static/dygraph_to_static_util.py @@ -153,19 +153,18 @@ def impl(*args, **kwargs): ir_outs = test_with_new_ir(func)(*args, **kwargs) if not need_check_output: return outs - for i in range(len(outs)): - np.testing.assert_array_equal( - outs[i], - ir_outs[i], - err_msg='Dy2St Unittest Check (' - + func.__name__ - + ') has diff ' - + '\nExpect ' - + str(outs[i]) - + '\n' - + 'But Got' - + str(ir_outs[i]), - ) + np.testing.assert_array_equal( + outs, + ir_outs, + err_msg='Dy2St Unittest Check (' + + func.__name__ + + ') has diff ' + + '\nExpect ' + + str(outs) + + '\n' + + 'But Got' + + str(ir_outs), + ) return outs return impl diff --git a/test/dygraph_to_static/test_assert.py b/test/dygraph_to_static/test_assert.py index f9dc0dc0cca17..d57084aa06f62 100644 --- a/test/dygraph_to_static/test_assert.py +++ b/test/dygraph_to_static/test_assert.py @@ -66,6 +66,7 @@ def test_bool_variable(self): dyfunc_assert_variable, x=numpy.array([True]), with_exception=False ) + @test_and_compare_with_new_ir(False) def test_int_variable(self): self._run_dy_static( dyfunc_assert_variable, x=numpy.array([0]), with_exception=True diff --git a/test/dygraph_to_static/test_ast_util.py b/test/dygraph_to_static/test_ast_util.py index a682c1fa88159..d17780029bd81 100644 --- a/test/dygraph_to_static/test_ast_util.py +++ b/test/dygraph_to_static/test_ast_util.py @@ -17,6 +17,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir from ifelse_simple_func import ( dyfunc_with_if_else, dyfunc_with_if_else2, @@ -60,6 +61,7 @@ def test_ast2func_dygraph(self): test_ret = self._ast2func(func)(x_v).numpy() self.assertTrue((true_ret == test_ret).all()) + @test_and_compare_with_new_ir(False) def test_ast2func_static(self): paddle.enable_static() diff --git a/test/dygraph_to_static/test_backward_without_params.py b/test/dygraph_to_static/test_backward_without_params.py index 192adff191078..af70b9e7a2f95 100644 --- a/test/dygraph_to_static/test_backward_without_params.py +++ b/test/dygraph_to_static/test_backward_without_params.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir import paddle @@ -30,6 +31,7 @@ def forward(self, x): class TestBackwardWithoutParams(unittest.TestCase): + @test_and_compare_with_new_ir(False) def test_run(self): net = Net() @@ -54,6 +56,7 @@ def forward(self, x): class TestZeroSizeNet(unittest.TestCase): + @test_and_compare_with_new_ir(False) def test_run(self): net = ZeroSizeNet() x = paddle.ones([2, 2]) diff --git a/test/dygraph_to_static/test_basic_api_transformation.py b/test/dygraph_to_static/test_basic_api_transformation.py index 88c77e2cc5262..17e72b793f144 100644 --- a/test/dygraph_to_static/test_basic_api_transformation.py +++ b/test/dygraph_to_static/test_basic_api_transformation.py @@ -16,6 +16,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir import paddle from paddle import fluid, to_tensor @@ -91,6 +92,7 @@ def get_dygraph_output(self): res = self.dygraph_func(self.input).numpy() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): main_program = fluid.Program() main_program.random_seed = SEED @@ -245,6 +247,7 @@ def get_dygraph_output(self): return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED @@ -278,6 +281,7 @@ def get_dygraph_output(self): res = self.dygraph_func(self.input1, self.input2).numpy() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED @@ -403,6 +407,7 @@ def get_dygraph_output(self): res = self.dygraph_func().numpy() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED @@ -433,6 +438,7 @@ def get_dygraph_output(self): res = self.dygraph_func() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED @@ -459,6 +465,7 @@ def get_dygraph_output(self): res = self.dygraph_func() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED @@ -485,6 +492,7 @@ def get_dygraph_output(self): res = self.dygraph_func() return res + @test_and_compare_with_new_ir(True) def get_static_output(self): startup_program = fluid.Program() startup_program.random_seed = SEED diff --git a/test/dygraph_to_static/test_cinn.py b/test/dygraph_to_static/test_cinn.py index d52364462791d..59a114d0aae58 100644 --- a/test/dygraph_to_static/test_cinn.py +++ b/test/dygraph_to_static/test_cinn.py @@ -15,6 +15,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir import paddle @@ -78,6 +79,7 @@ def train(self, use_cinn): return res + @test_and_compare_with_new_ir(False) def test_cinn(self): dy_res = self.train(use_cinn=False) cinn_res = self.train(use_cinn=True) diff --git a/test/dygraph_to_static/test_cinn_prim.py b/test/dygraph_to_static/test_cinn_prim.py index f72ddbbffc2cb..960e9bcfc6c0e 100644 --- a/test/dygraph_to_static/test_cinn_prim.py +++ b/test/dygraph_to_static/test_cinn_prim.py @@ -15,7 +15,11 @@ import unittest import numpy as np -from dygraph_to_static_util import ast_only_test, dy2static_unittest +from dygraph_to_static_util import ( + ast_only_test, + dy2static_unittest, + test_and_compare_with_new_ir, +) import paddle import paddle.nn.functional as F @@ -169,6 +173,7 @@ def test_cinn_prim(self): class TestBackend(unittest.TestCase): + @test_and_compare_with_new_ir(False) def test_backend(self): x = paddle.randn([2, 4]) out1 = self.forward(x, 'CINN') diff --git a/test/dygraph_to_static/test_closure_analysis.py b/test/dygraph_to_static/test_closure_analysis.py index 58ae9e2901a7d..95234565a6922 100644 --- a/test/dygraph_to_static/test_closure_analysis.py +++ b/test/dygraph_to_static/test_closure_analysis.py @@ -16,6 +16,7 @@ import os import unittest +from dygraph_to_static_util import test_and_compare_with_new_ir from numpy import append import paddle @@ -260,6 +261,7 @@ def init_dygraph_func(self): class TestPushPopTrans(unittest.TestCase): + @test_and_compare_with_new_ir(False) def test(self): def vlist_of_dict(x): ma = {'a': []} @@ -271,6 +273,7 @@ def vlist_of_dict(x): print(paddle.jit.to_static(vlist_of_dict).code) print(paddle.jit.to_static(vlist_of_dict)(x)) + @test_and_compare_with_new_ir(False) def test2(self): import numpy as np @@ -284,6 +287,7 @@ def vlist_of_dict(x): print(paddle.jit.to_static(vlist_of_dict).code) print(paddle.jit.to_static(vlist_of_dict)(x)) + @test_and_compare_with_new_ir(False) def test3(self): import numpy as np @@ -297,6 +301,7 @@ def vlist_of_dict(x): print(paddle.jit.to_static(vlist_of_dict).code) print(paddle.jit.to_static(vlist_of_dict)(x)) + @test_and_compare_with_new_ir(False) def test4(self): import numpy as np @@ -310,6 +315,7 @@ def vlist_of_dict(x): print(paddle.jit.to_static(vlist_of_dict).code) print(paddle.jit.to_static(vlist_of_dict)(x)) + @test_and_compare_with_new_ir(False) def test5(self): import numpy as np diff --git a/test/dygraph_to_static/test_convert_call_generator.py b/test/dygraph_to_static/test_convert_call_generator.py index aaab80c182ed5..b33a41576498d 100644 --- a/test/dygraph_to_static/test_convert_call_generator.py +++ b/test/dygraph_to_static/test_convert_call_generator.py @@ -14,7 +14,7 @@ import unittest -from dygraph_to_static_util import ast_only_test +from dygraph_to_static_util import ast_only_test, test_and_compare_with_new_ir import paddle from paddle.jit import to_static @@ -35,6 +35,7 @@ def main_func(): class TestConvertGenerator(unittest.TestCase): # fallback will ok. @ast_only_test + @test_and_compare_with_new_ir(False) def test_raise_error(self): translator_logger.verbosity_level = 1 with self.assertLogs( diff --git a/test/dygraph_to_static/test_cpu_cuda_to_tensor.py b/test/dygraph_to_static/test_cpu_cuda_to_tensor.py index dfd2aae4ed62c..3596af0254cc1 100644 --- a/test/dygraph_to_static/test_cpu_cuda_to_tensor.py +++ b/test/dygraph_to_static/test_cpu_cuda_to_tensor.py @@ -19,6 +19,7 @@ ast_only_test, dy2static_unittest, sot_only_test, + test_and_compare_with_new_ir, ) import paddle @@ -38,6 +39,7 @@ def func(x): class TestToTensor(unittest.TestCase): + @test_and_compare_with_new_ir(False) def test_to_tensor_with_variable_list(self): def func(x): ones = paddle.to_tensor(1) @@ -57,6 +59,7 @@ def func(x): @dy2static_unittest class TestToTensor1(unittest.TestCase): @ast_only_test + @test_and_compare_with_new_ir(False) def test_to_tensor_with_variable_list(self): def func(x): ones = paddle.to_tensor([1]) @@ -75,6 +78,7 @@ def func(x): ) @sot_only_test + @test_and_compare_with_new_ir(False) def test_to_tensor_with_variable_list_sot(self): def func(x): ones = paddle.to_tensor([1]) @@ -96,6 +100,7 @@ def func(x): @dy2static_unittest class TestToTensor2(unittest.TestCase): @ast_only_test + @test_and_compare_with_new_ir(False) def test_to_tensor_with_variable_list(self): def func(x): x = paddle.to_tensor([[1], [2], [3], [4]]) @@ -109,6 +114,7 @@ def func(x): ) @sot_only_test + @test_and_compare_with_new_ir(False) def test_to_tensor_with_variable_list_sot(self): def func(x): x = paddle.to_tensor([[1], [2], [3], [4]]) From a277fe4dc1aa81da30b3af507b0c1de0d72706dc Mon Sep 17 00:00:00 2001 From: David Nicolas <37790151+liyongchao911@users.noreply.github.com> Date: Fri, 1 Sep 2023 11:17:01 +0800 Subject: [PATCH 069/194] [Doctest]fix No.163,164,166, test=docs_preview (#56527) --- python/paddle/vision/models/squeezenet.py | 60 +-- python/paddle/vision/models/vgg.py | 100 ++--- python/paddle/vision/transforms/functional.py | 365 ++++++++---------- 3 files changed, 247 insertions(+), 278 deletions(-) diff --git a/python/paddle/vision/models/squeezenet.py b/python/paddle/vision/models/squeezenet.py index 97b1eefe45941..08cc1a6463324 100644 --- a/python/paddle/vision/models/squeezenet.py +++ b/python/paddle/vision/models/squeezenet.py @@ -90,20 +90,20 @@ class SqueezeNet(nn.Layer): Examples: .. code-block:: python - import paddle - from paddle.vision.models import SqueezeNet + >>> import paddle + >>> from paddle.vision.models import SqueezeNet - # build v1.0 model - model = SqueezeNet(version='1.0') + >>> # build v1.0 model + >>> model = SqueezeNet(version='1.0') - # build v1.1 model - # model = SqueezeNet(version='1.1') + >>> # build v1.1 model + >>> # model = SqueezeNet(version='1.1') - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ def __init__(self, version, num_classes=1000, with_pool=True): @@ -233,20 +233,20 @@ def squeezenet1_0(pretrained=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import squeezenet1_0 + >>> import paddle + >>> from paddle.vision.models import squeezenet1_0 - # build model - model = squeezenet1_0() + >>> # build model + >>> model = squeezenet1_0() - # build model and load imagenet pretrained weight - # model = squeezenet1_0(pretrained=True) + >>> # build model and load imagenet pretrained weight + >>> # model = squeezenet1_0(pretrained=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ return _squeezenet('squeezenet1_0', '1.0', pretrained, **kwargs) @@ -267,19 +267,19 @@ def squeezenet1_1(pretrained=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import squeezenet1_1 + >>> import paddle + >>> from paddle.vision.models import squeezenet1_1 - # build model - model = squeezenet1_1() + >>> # build model + >>> model = squeezenet1_1() - # build model and load imagenet pretrained weight - # model = squeezenet1_1(pretrained=True) + >>> # build model and load imagenet pretrained weight + >>> # model = squeezenet1_1(pretrained=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ return _squeezenet('squeezenet1_1', '1.1', pretrained, **kwargs) diff --git a/python/paddle/vision/models/vgg.py b/python/paddle/vision/models/vgg.py index b0b62f445d0b4..b354089025e51 100644 --- a/python/paddle/vision/models/vgg.py +++ b/python/paddle/vision/models/vgg.py @@ -46,21 +46,21 @@ class VGG(nn.Layer): Examples: .. code-block:: python - import paddle - from paddle.vision.models import VGG - from paddle.vision.models.vgg import make_layers + >>> import paddle + >>> from paddle.vision.models import VGG + >>> from paddle.vision.models.vgg import make_layers - vgg11_cfg = [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'] + >>> vgg11_cfg = [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'] - features = make_layers(vgg11_cfg) + >>> features = make_layers(vgg11_cfg) - vgg11 = VGG(features) + >>> vgg11 = VGG(features) - x = paddle.rand([1, 3, 224, 224]) - out = vgg11(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = vgg11(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ def __init__(self, features, num_classes=1000, with_pool=True): @@ -212,20 +212,20 @@ def vgg11(pretrained=False, batch_norm=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import vgg11 + >>> import paddle + >>> from paddle.vision.models import vgg11 - # build model - model = vgg11() + >>> # build model + >>> model = vgg11() - # build vgg11 model with batch_norm - model = vgg11(batch_norm=True) + >>> # build vgg11 model with batch_norm + >>> model = vgg11(batch_norm=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ model_name = 'vgg11' if batch_norm: @@ -249,20 +249,20 @@ def vgg13(pretrained=False, batch_norm=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import vgg13 + >>> import paddle + >>> from paddle.vision.models import vgg13 - # build model - model = vgg13() + >>> # build model + >>> model = vgg13() - # build vgg13 model with batch_norm - model = vgg13(batch_norm=True) + >>> # build vgg13 model with batch_norm + >>> model = vgg13(batch_norm=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ model_name = 'vgg13' if batch_norm: @@ -286,20 +286,20 @@ def vgg16(pretrained=False, batch_norm=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import vgg16 + >>> import paddle + >>> from paddle.vision.models import vgg16 - # build model - model = vgg16() + >>> # build model + >>> model = vgg16() - # build vgg16 model with batch_norm - model = vgg16(batch_norm=True) + >>> # build vgg16 model with batch_norm + >>> model = vgg16(batch_norm=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ model_name = 'vgg16' if batch_norm: @@ -323,20 +323,20 @@ def vgg19(pretrained=False, batch_norm=False, **kwargs): Examples: .. code-block:: python - import paddle - from paddle.vision.models import vgg19 + >>> import paddle + >>> from paddle.vision.models import vgg19 - # build model - model = vgg19() + >>> # build model + >>> model = vgg19() - # build vgg19 model with batch_norm - model = vgg19(batch_norm=True) + >>> # build vgg19 model with batch_norm + >>> model = vgg19(batch_norm=True) - x = paddle.rand([1, 3, 224, 224]) - out = model(x) + >>> x = paddle.rand([1, 3, 224, 224]) + >>> out = model(x) - print(out.shape) - # [1, 1000] + >>> print(out.shape) + [1, 1000] """ model_name = 'vgg19' if batch_norm: diff --git a/python/paddle/vision/transforms/functional.py b/python/paddle/vision/transforms/functional.py index cde720e828383..40a708da5b1f6 100644 --- a/python/paddle/vision/transforms/functional.py +++ b/python/paddle/vision/transforms/functional.py @@ -70,16 +70,14 @@ def to_tensor(pic, data_format='CHW'): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - tensor = F.to_tensor(fake_img) - print(tensor.shape) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> tensor = F.to_tensor(fake_img) + >>> print(tensor.shape) + [3, 256, 300] """ if not ( @@ -127,21 +125,18 @@ def resize(img, size, interpolation='bilinear'): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - converted_img = F.resize(fake_img, 224) - print(converted_img.size) - # (262, 224) - - converted_img = F.resize(fake_img, (200, 150)) - print(converted_img.size) - # (150, 200) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> converted_img = F.resize(fake_img, 224) + >>> print(converted_img.size) + (262, 224) + + >>> converted_img = F.resize(fake_img, (200, 150)) + >>> print(converted_img.size) + (150, 200) """ if not ( _is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img) @@ -196,19 +191,18 @@ def pad(img, padding, fill=0, padding_mode='constant'): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - padded_img = F.pad(fake_img, padding=1) - print(padded_img.size) - - padded_img = F.pad(fake_img, padding=(2, 1)) - print(padded_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> padded_img = F.pad(fake_img, padding=1) + >>> print(padded_img.size) + (302, 258) + + >>> padded_img = F.pad(fake_img, padding=(2, 1)) + >>> print(padded_img.size) + (304, 258) """ if not ( _is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img) @@ -244,16 +238,14 @@ def crop(img, top, left, height, width): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - cropped_img = F.crop(fake_img, 56, 150, 200, 100) - print(cropped_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> cropped_img = F.crop(fake_img, 56, 150, 200, 100) + >>> print(cropped_img.size) + (100, 200) """ if not ( @@ -287,16 +279,14 @@ def center_crop(img, output_size): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - cropped_img = F.center_crop(fake_img, (150, 100)) - print(cropped_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> cropped_img = F.center_crop(fake_img, (150, 100)) + >>> print(cropped_img.size) + (100, 150) """ if not ( _is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img) @@ -327,16 +317,14 @@ def hflip(img): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - flpped_img = F.hflip(fake_img) - print(flpped_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> flipped_img = F.hflip(fake_img) + >>> print(flipped_img.size) + (300, 256) """ if not ( @@ -368,16 +356,14 @@ def vflip(img): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - flpped_img = F.vflip(fake_img) - print(flpped_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> flipped_img = F.vflip(fake_img) + >>> print(flipped_img.size) + (300, 256) """ if not ( @@ -411,20 +397,26 @@ def adjust_brightness(img, brightness_factor): Examples: .. code-block:: python - :name: code-example1 + :name: code-example1 + + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> np.random.seed(2023) + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> print(fake_img.size) + (300, 256) + >>> print(fake_img.load()[1,1]) + (61, 155, 171) + >>> converted_img = F.adjust_brightness(fake_img, 0.5) + >>> print(converted_img.size) + (300, 256) + >>> print(converted_img.load()[1,1]) + (30, 77, 85) - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - fake_img = Image.fromarray(fake_img) - print(fake_img.size) # (300, 256) - print(fake_img.load()[1,1]) # (95, 127, 202) - converted_img = F.adjust_brightness(fake_img, 0.5) - print(converted_img.size) # (300, 256) - print(converted_img.load()[1,1]) # (47, 63, 101) """ @@ -460,16 +452,14 @@ def adjust_contrast(img, contrast_factor): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - converted_img = F.adjust_contrast(fake_img, 0.4) - print(converted_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> converted_img = F.adjust_contrast(fake_img, 0.4) + >>> print(converted_img.size) + (300, 256) """ if not ( _is_pil_image(img) or _is_numpy_image(img) or _is_tensor_image(img) @@ -503,16 +493,14 @@ def adjust_saturation(img, saturation_factor): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - converted_img = F.adjust_saturation(fake_img, 0.4) - print(converted_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> converted_img = F.adjust_saturation(fake_img, 0.4) + >>> print(converted_img.size) + (300, 256) """ if not ( @@ -556,16 +544,14 @@ def adjust_hue(img, hue_factor): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - converted_img = F.adjust_hue(fake_img, 0.4) - print(converted_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> converted_img = F.adjust_hue(fake_img, 0.4) + >>> print(converted_img.size) + (300, 256) """ if not ( @@ -657,13 +643,12 @@ def affine( Examples: .. code-block:: python - import paddle - from paddle.vision.transforms import functional as F - - fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) - - affined_img = F.affine(fake_img, 45, translate=[0.2, 0.2], scale=0.5, shear=[-10, 10]) - print(affined_img.shape) + >>> import paddle + >>> from paddle.vision.transforms import functional as F + >>> fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) + >>> affined_img = F.affine(fake_img, 45, translate=[0.2, 0.2], scale=0.5, shear=[-10, 10]) + >>> print(affined_img.shape) + [3, 256, 300] """ if not ( @@ -789,16 +774,14 @@ def rotate( Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - rotated_img = F.rotate(fake_img, 90) - print(rotated_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> rotated_img = F.rotate(fake_img, 90) + >>> print(rotated_img.size) + (300, 256) """ if not ( @@ -897,16 +880,14 @@ def perspective(img, startpoints, endpoints, interpolation='nearest', fill=0): Examples: .. code-block:: python - import paddle - from paddle.vision.transforms import functional as F - - fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) - - startpoints = [[0, 0], [33, 0], [33, 25], [0, 25]] - endpoints = [[3, 2], [32, 3], [30, 24], [2, 25]] - - perspectived_img = F.perspective(fake_img, startpoints, endpoints) - print(perspectived_img.shape) + >>> import paddle + >>> from paddle.vision.transforms import functional as F + >>> fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) + >>> startpoints = [[0, 0], [33, 0], [33, 25], [0, 25]] + >>> endpoints = [[3, 2], [32, 3], [30, 24], [2, 25]] + >>> perspectived_img = F.perspective(fake_img, startpoints, endpoints) + >>> print(perspectived_img.shape) + [3, 256, 300] """ if not ( @@ -946,16 +927,14 @@ def to_grayscale(img, num_output_channels=1): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - gray_img = F.to_grayscale(fake_img) - print(gray_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> gray_img = F.to_grayscale(fake_img) + >>> print(gray_img.size) + (300, 256) """ if not ( @@ -993,19 +972,16 @@ def normalize(img, mean, std, data_format='CHW', to_rgb=False): Examples: .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import functional as F - - fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') - - fake_img = Image.fromarray(fake_img) - - mean = [127.5, 127.5, 127.5] - std = [127.5, 127.5, 127.5] - - normalized_img = F.normalize(fake_img, mean, std, data_format='HWC') - print(normalized_img.max(), normalized_img.min()) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import functional as F + >>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8') + >>> fake_img = Image.fromarray(fake_img) + >>> mean = [127.5, 127.5, 127.5] + >>> std = [127.5, 127.5, 127.5] + >>> normalized_img = F.normalize(fake_img, mean, std, data_format='HWC') + >>> print(normalized_img.max(), normalized_img.min()) + 0.99215686 -1.0 """ @@ -1039,35 +1015,28 @@ def erase(img, i, j, h, w, v, inplace=False): Examples: .. code-block:: python - import paddle - - fake_img = paddle.randn((3, 2, 4)).astype(paddle.float32) - print(fake_img) - - #Tensor(shape=[3, 2, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[ 0.02169025, -0.97859967, -1.39175487, -1.07478464], - # [ 0.20654772, 1.74624777, 0.32268861, -0.13857445]], - # - # [[-0.14993843, 1.10793507, -0.40056887, -1.94395220], - # [ 0.41686651, 0.44551995, -0.09356714, -0.60898107]], - # - # [[-0.24998808, -1.47699273, -0.88838995, 0.42629015], - # [ 0.56948012, -0.96200180, 0.53355658, 3.20450878]]]) - - values = paddle.zeros((1,1,1), dtype=paddle.float32) - result = paddle.vision.transforms.erase(fake_img, 0, 1, 1, 2, values) - - print(result) - - #Tensor(shape=[3, 2, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[ 0.02169025, 0. , 0. , -1.07478464], - # [ 0.20654772, 1.74624777, 0.32268861, -0.13857445]], - # - # [[-0.14993843, 0. , 0. , -1.94395220], - # [ 0.41686651, 0.44551995, -0.09356714, -0.60898107]], - # - # [[-0.24998808, 0. , 0. , 0.42629015], - # [ 0.56948012, -0.96200180, 0.53355658, 3.20450878]]]) + >>> import paddle + >>> paddle.seed(2023) + >>> fake_img = paddle.randn((3, 2, 4)).astype(paddle.float32) + >>> print(fake_img) + Tensor(shape=[3, 2, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[ 0.06132207, 1.11349595, 0.41906244, -0.24858207], + [-1.85169315, -1.50370061, 1.73954511, 0.13331604]], + [[ 1.66359663, -0.55764782, -0.59911072, -0.57773495], + [-1.03176904, -0.33741450, -0.29695082, -1.50258386]], + [[ 0.67233968, -1.07747352, 0.80170447, -0.06695852], + [-1.85003340, -0.23008066, 0.65083790, 0.75387722]]]) + + >>> values = paddle.zeros((1,1,1), dtype=paddle.float32) + >>> result = paddle.vision.transforms.erase(fake_img, 0, 1, 1, 2, values) + >>> print(result) + Tensor(shape=[3, 2, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[ 0.06132207, 0. , 0. , -0.24858207], + [-1.85169315, -1.50370061, 1.73954511, 0.13331604]], + [[ 1.66359663, 0. , 0. , -0.57773495], + [-1.03176904, -0.33741450, -0.29695082, -1.50258386]], + [[ 0.67233968, 0. , 0. , -0.06695852], + [-1.85003340, -0.23008066, 0.65083790, 0.75387722]]]) """ if _is_tensor_image(img): From aa28e4461a2cb851dbc8f351c79823e6bce42dcd Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Fri, 1 Sep 2023 11:48:47 +0800 Subject: [PATCH 070/194] [xdoctest][task 291] reformat example code with google style in `python/paddle/incubate/operators/unzip.py` (#56826) * [Doctest] fix No.291, test=docs_preview * fix * Update python/paddle/incubate/operators/unzip.py * switch to gpu --------- Co-authored-by: Nyakku Shigure --- python/paddle/incubate/operators/unzip.py | 53 +++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/python/paddle/incubate/operators/unzip.py b/python/paddle/incubate/operators/unzip.py index c8ed13ed0142a..68a491300c5d2 100644 --- a/python/paddle/incubate/operators/unzip.py +++ b/python/paddle/incubate/operators/unzip.py @@ -33,34 +33,33 @@ def unzip(input, lod): Examples: .. code-block:: python - import numpy as np - import paddle - import paddle.fluid as fluid - paddle.enable_static() - input_np = np.array([ - [1.0, 2.0, 3.0, 4.0], - [10.0, 20.0, 30.0, 40.0], - [100.0, 200.0, 300.0, 400.0] - ]) - lod_np = np.array([0, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12]) - input = paddle.to_tensor(input_np, "int64") - lod = paddle.to_tensor(lod_np, "int64") - unzipped_input = paddle.incubate.unzip(input, lod) - ''' - unzipped_input is [ - [1.0, 2.0, 3.0, 4.0], - [0.0, 0.0, 0.0, 0.0], - [10.0, 20.0, 30.0, 40.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [100.0, 200.0, 300.0, 400.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0] - ] - ''' + >>> # doctest: +REQUIRES(env:GPU) + >>> import numpy as np + >>> import paddle + >>> paddle.set_device('gpu') + >>> input_np = np.array([ + ... [1.0, 2.0, 3.0, 4.0], + ... [10.0, 20.0, 30.0, 40.0], + ... [100.0, 200.0, 300.0, 400.0] + ... ]) + >>> lod_np = np.array([0, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12]) + >>> input = paddle.to_tensor(input_np, "int64") + >>> lod = paddle.to_tensor(lod_np, "int64") + >>> unzipped_input = paddle.incubate.operators.unzip(input, lod) + >>> print(unzipped_input) + Tensor(shape=[10, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[1 , 2 , 3 , 4 ], + [0 , 0 , 0 , 0 ], + [10, 20, 30, 40], + [0 , 0 , 0 , 0 ], + [0 , 0 , 0 , 0 ], + [0 , 0 , 0 , 0 ], + [100, 200, 300, 400], + [0 , 0 , 0 , 0 ], + [0 , 0 , 0 , 0 ], + [0 , 0 , 0 , 0 ]]) + """ helper = LayerHelper('unzip', **locals()) out = helper.create_variable(dtype=input.dtype) From 15728a7949582062dec07bc574dc644ea2b58689 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Fri, 1 Sep 2023 11:50:17 +0800 Subject: [PATCH 071/194] [3rd-party] update openBLAS to fix compile error on Raptor Lake (#56617) * [3rd-party] update openBLAS to fix compile error on Raptor Lake * modify comments and remove the gcc version limit * switch third-party/openblas to v0.3.23 * fix --- cmake/external/openblas.cmake | 14 +++++--------- third_party/openblas | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake index 72ebd724ef4e0..6f9f6a11ab2e4 100644 --- a/cmake/external/openblas.cmake +++ b/cmake/external/openblas.cmake @@ -20,16 +20,12 @@ set(CBLAS_SOURCE_DIR ${PADDLE_SOURCE_DIR}/third_party/openblas) set(CBLAS_TAG v0.3.7) set(CMAKE_VERBOSE_MAKEFILE 1) -# Why use v0.3.18? The IDG business line encountered a random openblas error, -# which can be resolved after upgrading openblas. -# And why compile when gcc>8.2? Please refer to -# https://github.com/spack/spack/issues/19932#issuecomment-733452619 -# v0.3.18 only support gcc>=8.3 or gcc>=7.4 -if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.2 +# OpenBLAS support Raptor Lake from v0.3.22 +if(UNIX + AND NOT APPLE + AND NOT WITH_ROCM AND NOT WITH_XPU) - # We only compile with openblas 0.3.18 when gcc >= 8.3 - set(CBLAS_TAG v0.3.18) + set(CBLAS_TAG v0.3.23) endif() if(APPLE AND WITH_ARM) diff --git a/third_party/openblas b/third_party/openblas index 5f36f18148603..394a9fbafe901 160000 --- a/third_party/openblas +++ b/third_party/openblas @@ -1 +1 @@ -Subproject commit 5f36f18148603facb6c3540e673610d6b24cbfbb +Subproject commit 394a9fbafe9010b76a2615c562204277a956eb52 From e8a96347786a041627058ae351d7a80e2ad2414a Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Fri, 1 Sep 2023 11:51:03 +0800 Subject: [PATCH 072/194] [clang-tidy] enable bugprone-incorrect-roundings check (#56747) --- .clang-tidy | 2 +- paddle/fluid/operators/detection/mask_util.cc | 8 ++++---- paddle/phi/kernels/cpu/interpolate_grad_kernel.cc | 14 +++++++------- paddle/phi/kernels/cpu/interpolate_kernel.cc | 10 +++++----- paddle/phi/kernels/cpu/roi_align_grad_kernel.cc | 4 ++-- paddle/phi/kernels/cpu/roi_align_kernel.cc | 7 ++++--- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 9934d5012bcd7..c0b30cf39be72 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -12,7 +12,7 @@ bugprone-exception-escape, -bugprone-fold-init-type, -bugprone-forwarding-reference-overload, -bugprone-inaccurate-erase, --bugprone-incorrect-roundings, +bugprone-incorrect-roundings, -bugprone-infinite-loop, bugprone-integer-division, -bugprone-macro-repeated-side-effects, diff --git a/paddle/fluid/operators/detection/mask_util.cc b/paddle/fluid/operators/detection/mask_util.cc index 09d5fcf040e85..4a44f0eb401f1 100644 --- a/paddle/fluid/operators/detection/mask_util.cc +++ b/paddle/fluid/operators/detection/mask_util.cc @@ -51,9 +51,9 @@ void Poly2Mask(const float* xy, int k, int h, int w, uint8_t* mask) { x = reinterpret_cast(xptr->ptr()); y = x + (k + 1); - for (j = 0; j < k; j++) x[j] = static_cast(scale * xy[j * 2 + 0] + .5); + for (j = 0; j < k; j++) x[j] = std::lround(scale * xy[j * 2 + 0]); x[k] = x[0]; - for (j = 0; j < k; j++) y[j] = static_cast(scale * xy[j * 2 + 1] + .5); + for (j = 0; j < k; j++) y[j] = std::lround(scale * xy[j * 2 + 1]); y[k] = y[0]; for (j = 0; j < k; j++) { m += UMax(abs(x[j] - x[j + 1]), abs(y[j] - y[j + 1])) + 1; @@ -82,7 +82,7 @@ void Poly2Mask(const float* xy, int k, int h, int w, uint8_t* mask) { for (d = 0; d <= dx; d++) { t = flip ? dx - d : d; u[m] = t + xs; - v[m] = static_cast(ys + s * t + .5); + v[m] = std::lround(ys + s * t); m++; } } else { @@ -90,7 +90,7 @@ void Poly2Mask(const float* xy, int k, int h, int w, uint8_t* mask) { for (d = 0; d <= dy; d++) { t = flip ? dy - d : d; v[m] = t + ys; - u[m] = static_cast(xs + s * t + .5); + u[m] = std::lround(xs + s * t); m++; } } diff --git a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc index f39a66ff60484..ff0ef722ee7ba 100644 --- a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc @@ -359,17 +359,17 @@ static void NearestNeighbor3DInterpolateGrad(const DenseTensor& output_grad, auto output_grad_t = EigenTensor::From(output_grad); for (int d = 0; d < out_d; d++) { - int in_d = static_cast(align_corners - ? (ratio_d * static_cast(d) + 0.5f) - : (ratio_d * static_cast(d))); + int in_d = static_cast( + align_corners ? std::lround(ratio_d * static_cast(d)) + : (ratio_d * static_cast(d))); for (int k = 0; k < out_h; k++) { // loop for images - int in_k = static_cast(align_corners - ? (ratio_h * static_cast(k) + 0.5f) - : (ratio_h * static_cast(k))); + int in_k = static_cast( + align_corners ? std::lround(ratio_h * static_cast(k)) + : (ratio_h * static_cast(k))); for (int l = 0; l < out_w; l++) { int in_l = static_cast( - align_corners ? (ratio_w * static_cast(l) + 0.5f) + align_corners ? std::lround(ratio_w * static_cast(l)) : (ratio_w * static_cast(l))); for (int i = 0; i < n; i++) { // loop for batches diff --git a/paddle/phi/kernels/cpu/interpolate_kernel.cc b/paddle/phi/kernels/cpu/interpolate_kernel.cc index 80bad644bd5b3..895410a75a414 100644 --- a/paddle/phi/kernels/cpu/interpolate_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_kernel.cc @@ -232,12 +232,12 @@ static void NearestNeighborInterpolate(const DenseTensor& input, for (int k = 0; k < out_h; k++) { // loop for images int in_k = (align_corners) - ? static_cast(ratio_h * static_cast(k) + 0.5) + ? std::lround(ratio_h * static_cast(k)) : static_cast(ratio_h * static_cast(k)); for (int l = 0; l < out_w; l++) { int in_l = (align_corners) - ? static_cast(ratio_w * static_cast(l) + 0.5) + ? std::lround(ratio_w * static_cast(l)) : static_cast(ratio_w * static_cast(l)); for (int i = 0; i < n; i++) { // loop for batches @@ -515,16 +515,16 @@ static void NearestNeighbor3DInterpolate(const DenseTensor& input, auto output_t = EigenTensor::From(*output); for (int d = 0; d < out_d; d++) { // loop for images int in_d = (align_corners) - ? static_cast(ratio_d * static_cast(d) + 0.5) + ? std::lround(ratio_d * static_cast(d)) : static_cast(ratio_d * static_cast(d)); for (int k = 0; k < out_h; k++) { int in_k = (align_corners) - ? static_cast(ratio_h * static_cast(k) + 0.5) + ? std::lround(ratio_h * static_cast(k)) : static_cast(ratio_h * static_cast(k)); for (int l = 0; l < out_w; l++) { int in_l = (align_corners) - ? static_cast(ratio_w * static_cast(l) + 0.5) + ? std::lround(ratio_w * static_cast(l)) : static_cast(ratio_w * static_cast(l)); for (int i = 0; i < n; i++) { // loop for batches diff --git a/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc b/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc index 77ac6a7234a87..aa1e06c60a4e7 100644 --- a/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc @@ -170,11 +170,11 @@ void RoiAlignGradKernel(const Context& dev_ctx, T count = roi_bin_grid_h * roi_bin_grid_w; for (int iy = 0; iy < roi_bin_grid_h; iy++) { const T y = roi_ymin + ph * bin_size_h + - static_cast(iy + .5f) * bin_size_h / + static_cast(iy + .5f) * bin_size_h / // NOLINT static_cast(roi_bin_grid_h); for (int ix = 0; ix < roi_bin_grid_w; ix++) { const T x = roi_xmin + pw * bin_size_w + - static_cast(ix + .5f) * bin_size_w / + static_cast(ix + .5f) * bin_size_w / // NOLINT static_cast(roi_bin_grid_w); bilinear_interpolate_gradient(height, width, diff --git a/paddle/phi/kernels/cpu/roi_align_kernel.cc b/paddle/phi/kernels/cpu/roi_align_kernel.cc index 46a43a3a51767..d2c3f9a51b7c7 100644 --- a/paddle/phi/kernels/cpu/roi_align_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_align_kernel.cc @@ -79,12 +79,13 @@ std::vector> GetIndexesAndRatios( for (std::size_t px = 0; px < pooled_width; px++) { for (std::size_t iy = 0; iy < roi_bin_grid_h; iy++) { // calculate x of sample points - auto y = roi_ymin + bin_h * (py + static_cast(iy + .5f) / + auto y = roi_ymin + bin_h * (py + static_cast(iy + .5f) / // NOLINT static_cast(roi_bin_grid_h)); for (std::size_t ix = 0; ix < roi_bin_grid_w; ix++) { // calculate x of sample points - auto x = roi_xmin + bin_w * (px + static_cast(ix + .5f) / - static_cast(roi_bin_grid_w)); + auto x = + roi_xmin + bin_w * (px + static_cast(ix + .5f) / // NOLINT + static_cast(roi_bin_grid_w)); // deal with elements out of map if (y < -1.0 || y > height || x < -1.0 || x > width) { From 396fd4c088e3df97c254b8d424fe178c78fec318 Mon Sep 17 00:00:00 2001 From: Yuang Liu Date: Fri, 1 Sep 2023 12:42:35 +0800 Subject: [PATCH 073/194] change some default values of optim flags (#56847) --- .../distributed/fleet/layers/mpu/mp_layers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python/paddle/distributed/fleet/layers/mpu/mp_layers.py b/python/paddle/distributed/fleet/layers/mpu/mp_layers.py index 1a0cb322cf499..61ccbbcc448b9 100644 --- a/python/paddle/distributed/fleet/layers/mpu/mp_layers.py +++ b/python/paddle/distributed/fleet/layers/mpu/mp_layers.py @@ -450,6 +450,14 @@ def __init__( and mp_configs.mp_async_allreduce and mp_configs.mp_fused_linear_param_grad_add ) + if ( + self.mp_async_allreduce + or self.mp_skip_c_identity + or self.mp_fused_linear_param_grad_add + ): + assert ( + paddle.in_dynamic_mode() + ), "mp_async_allreduce, mp_skip_c_identity and mp_fused_linear_param_grad_add are only available under dygraph mode" if self.fuse_matmul_bias: if not is_fused_matmul_bias_supported(): raise NotImplementedError( @@ -614,6 +622,14 @@ def __init__( and mp_configs.mp_async_allreduce and mp_configs.mp_fused_linear_param_grad_add ) + if ( + self.mp_async_allreduce + or self.mp_skip_c_identity + or self.mp_fused_linear_param_grad_add + ): + assert ( + paddle.in_dynamic_mode() + ), "mp_async_allreduce, mp_skip_c_identity and mp_fused_linear_param_grad_add are only available under dygraph mode" assert in_features % self.world_size == 0, ( "Number of row of the weight for linear ({}) must be" " divisible by model parallel size ({})".format( From 1dd6f6fa66a1c1c4a73f5d9dcd41d810996f88b0 Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Fri, 1 Sep 2023 13:08:25 +0800 Subject: [PATCH 074/194] [IR] Add test for LegacyKernelOp (#56818) * add test for legacy_op * add test for legacy_op * add test * change test legacy op : pd.c_concat * fix codestyle * add legacy kernel op test * Update ir_kernel_dialect_pass_test.cc * Update ir_kernel_dialect_pass_test.cc * Update ir_kernel_dialect_pass_test.cc --- paddle/ir/core/operation.cc | 2 +- .../ir_kernel_dialect_pass_test.cc | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/paddle/ir/core/operation.cc b/paddle/ir/core/operation.cc index 94722d1ada0b5..3d316847d9fc1 100644 --- a/paddle/ir/core/operation.cc +++ b/paddle/ir/core/operation.cc @@ -121,7 +121,7 @@ Operation *Operation::Create(const std::vector &inputs, // Call destructors for Region , OpResults, Operation, and OpOperands in // sequence, and finally free memory. void Operation::Destroy() { - VLOG(6) << "Destroy Operation [" << name() << "] ..."; + VLOG(10) << "Destroy Operation [" << name() << "] ..."; // 1. Deconstruct Regions. if (num_regions_ > 0) { for (size_t idx = 0; idx < num_regions_; idx++) { diff --git a/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc b/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc index d76495b26c117..72dbbf73b9bc2 100644 --- a/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc +++ b/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc @@ -26,6 +26,7 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_adaptor.h" #include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h" #include "paddle/fluid/platform/init.h" @@ -127,3 +128,46 @@ TEST(dialect_attr, attr) { ss.str() == "", true); } + +ir::AttributeMap CreateAttributeMap(std::vector attribute_names, + std::vector attributes, + std::string attr_name, + phi::KernelKey kernel_key) { + ir::IrContext* ctx = ir::IrContext::Instance(); + ir::AttributeMap attr_map; + for (size_t i = 0; i < attribute_names.size(); i++) { + ir::Attribute attr_value = ir::StrAttribute::get(ctx, attributes[i]); + attr_map.insert( + std::pair(attribute_names[i], attr_value)); + } + auto attr = paddle::dialect::KernelAttribute::get(ctx, kernel_key); + attr_map.insert(std::pair(attr_name, attr)); + return attr_map; +} + +TEST(kernel_dialect, legacy_op_test) { + // (1) Init environment. + + ir::IrContext* ctx = ir::IrContext::Instance(); + ir::Program program((ctx)); + + ctx->GetOrRegisterDialect(); + phi::KernelKey kernel_key( + phi::Backend::CPU, phi::DataLayout::ALL_LAYOUT, phi::DataType::FLOAT32); + + ir::OpInfo kernel_op_info = + ctx->GetRegisteredOpInfo(paddle::dialect::LegacyKernelOp::name()); + ir::OperationArgument argument(kernel_op_info); + argument.attributes = CreateAttributeMap({"op_name", "kernel_name"}, + {"pd.kernel_op", "kernel_op"}, + "kernel_key", + kernel_key); + + ir::Operation* op = ir::Operation::Create(std::move(argument)); + EXPECT_EQ("pd.kernel_op", + op->dyn_cast().op_name()); + EXPECT_EQ("kernel_op", + op->dyn_cast().kernel_name()); + EXPECT_EQ(kernel_key, + op->dyn_cast().kernel_key()); +} From 7adb4703825b1ce6ee21817cf00305be4b3a25bc Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 1 Sep 2023 14:05:28 +0800 Subject: [PATCH 075/194] [NewIR]Part-2.1 Refactor NewIRCompiler to support Group Ops (#56762) * [NewIR]Part-2.1 Refactor NewIRCompiler to support Group Ops * fix gflags link error * fix include ir_printer.h * fix unittest * fix conflict * fix flags * fix comment --- CMakeLists.txt | 1 + cmake/cinn.cmake | 8 +- paddle/cinn/auto_schedule/auto_tuner.cc | 4 +- paddle/cinn/auto_schedule/auto_tuner.h | 4 +- .../auto_schedule/measure/measurer_test.cc | 7 +- .../auto_gen_rule/auto_inline_test.cc | 10 +- .../search_space/auto_gen_rule/test_helper.cc | 3 +- .../evolutionary_search_test.cc | 7 +- .../auto_schedule/task/task_registry_test.cc | 7 +- paddle/cinn/auto_schedule/task/tune_task.cc | 2 +- paddle/cinn/auto_schedule/task/tune_task.h | 9 +- .../cinn/auto_schedule/task/tune_task_test.cc | 9 +- .../tests/performance_comparison_test.cc | 12 +- paddle/cinn/backends/compiler.cc | 1 + paddle/cinn/hlir/framework/CMakeLists.txt | 3 +- .../cinn/hlir/framework/new_ir/CMakeLists.txt | 4 + paddle/cinn/hlir/framework/new_ir/group.h | 52 ++ .../hlir/framework/new_ir/op_lowering_impl.cc | 451 ++++++++++++++++++ .../hlir/framework/new_ir/op_lowering_impl.h | 162 +++++++ paddle/cinn/hlir/framework/new_ir/utils.cc | 96 ++++ paddle/cinn/hlir/framework/new_ir/utils.h | 52 ++ paddle/cinn/hlir/framework/new_ir_compiler.cc | 164 +------ paddle/cinn/hlir/framework/new_ir_compiler.h | 26 +- paddle/cinn/hlir/framework/op_lowering.h | 182 ++----- .../{op_lowering.cc => op_lowering_impl.cc} | 39 +- paddle/cinn/hlir/framework/op_lowering_impl.h | 177 +++++++ .../hlir/framework/op_lowering_impl_base.h | 43 ++ .../cinn/hlir/framework/op_lowering_test.cc | 2 +- paddle/cinn/hlir/framework/op_lowering_util.h | 2 +- .../cinn/hlir/framework/parallel_compiler.cc | 3 +- .../cinn/hlir/framework/parallel_compiler.h | 1 - paddle/cinn/hlir/op/op_util.cc | 50 +- .../cinn/ir/test/schedule_block_graph_test.cc | 3 +- paddle/fluid/framework/scope.cc | 8 +- paddle/fluid/platform/profiler.cc | 15 +- paddle/fluid/platform/profiler/profiler.cc | 7 - paddle/fluid/platform/profiler/profiler.h | 2 +- paddle/phi/core/flags.cc | 15 + test/CMakeLists.txt | 3 + test/cpp/ir/cinn/new_ir_compiler_test.cc | 39 +- 40 files changed, 1243 insertions(+), 442 deletions(-) create mode 100755 paddle/cinn/hlir/framework/new_ir/CMakeLists.txt create mode 100644 paddle/cinn/hlir/framework/new_ir/group.h create mode 100644 paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc create mode 100644 paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h create mode 100644 paddle/cinn/hlir/framework/new_ir/utils.cc create mode 100644 paddle/cinn/hlir/framework/new_ir/utils.h rename paddle/cinn/hlir/framework/{op_lowering.cc => op_lowering_impl.cc} (95%) create mode 100644 paddle/cinn/hlir/framework/op_lowering_impl.h create mode 100644 paddle/cinn/hlir/framework/op_lowering_impl_base.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 318c9df489311..f20a52522acdb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -605,6 +605,7 @@ if(WITH_CINN) add_definitions(-DPADDLE_WITH_CINN) if(CINN_ONLY) + add_definitions(-DCINN_WITH_ONLY) if(WITH_PYTHON) add_subdirectory(python) endif() diff --git a/cmake/cinn.cmake b/cmake/cinn.cmake index ca25a7d5d30a5..ed7735e3c7d91 100644 --- a/cmake/cinn.cmake +++ b/cmake/cinn.cmake @@ -168,8 +168,8 @@ cinn_cc_library( add_dependencies(cinnapi GEN_LLVM_RUNTIME_IR_HEADER ZLIB::ZLIB) add_dependencies(cinnapi GEN_LLVM_RUNTIME_IR_HEADER ${core_deps}) if(NOT CINN_ONLY) - target_link_libraries(cinnapi phi) - add_dependencies(cinnapi phi) + target_link_libraries(cinnapi pd_dialect phi) + add_dependencies(cinnapi pd_dialect phi) endif() target_link_libraries(cinnapi ${PYTHON_LIBRARIES}) @@ -226,8 +226,8 @@ function(gen_cinncore LINKTYPE) add_dependencies(${CINNCORE_TARGET} GEN_LLVM_RUNTIME_IR_HEADER ZLIB::ZLIB) add_dependencies(${CINNCORE_TARGET} GEN_LLVM_RUNTIME_IR_HEADER ${core_deps}) if(NOT CINN_ONLY) - target_link_libraries(${CINNCORE_TARGET} phi) - add_dependencies(${CINNCORE_TARGET} phi) + target_link_libraries(${CINNCORE_TARGET} pd_dialect phi) + add_dependencies(${CINNCORE_TARGET} pd_dialect phi) endif() add_dependencies(${CINNCORE_TARGET} pybind) diff --git a/paddle/cinn/auto_schedule/auto_tuner.cc b/paddle/cinn/auto_schedule/auto_tuner.cc index 68f5b6d199d49..d8280af500089 100644 --- a/paddle/cinn/auto_schedule/auto_tuner.cc +++ b/paddle/cinn/auto_schedule/auto_tuner.cc @@ -63,8 +63,8 @@ void AutoTuner::Initialize(const Config& config, const auto& shape_dict = graph_->GetAttrs< absl::flat_hash_map>("infershape"); - op_lowerer_ = std::make_unique( - dtype_dict, shape_dict, target_); + op_lowerer_ = std::make_unique>( + new hlir::framework::OpLowererImpl(dtype_dict, shape_dict, target_)); InitialTaskRegistry* task_registry = InitialTaskRegistry::Global(); for (auto i = 0; i < tasks_.size(); ++i) { auto&& task = tasks_[i]; diff --git a/paddle/cinn/auto_schedule/auto_tuner.h b/paddle/cinn/auto_schedule/auto_tuner.h index 1a4e3c8c60d8e..9875e5dfcdd00 100644 --- a/paddle/cinn/auto_schedule/auto_tuner.h +++ b/paddle/cinn/auto_schedule/auto_tuner.h @@ -30,11 +30,11 @@ namespace cinn { namespace auto_schedule { - // This class is entrance of auto-tune, users can use it // to tune graph (not supported yet) and search a series of schedules // that maybe more likely to obtain better performance. // Internally, it creates necessary components and use them to perform tuning. +using GroupPtr = hlir::framework::GroupPtr; class AutoTuner { public: // configure how to perform auto-tune, such as @@ -58,7 +58,7 @@ class AutoTuner { private: const common::Target& target_; hlir::framework::Graph* graph_; - std::unique_ptr op_lowerer_; + std::unique_ptr> op_lowerer_; // Tasks to tune std::vector tasks_; diff --git a/paddle/cinn/auto_schedule/measure/measurer_test.cc b/paddle/cinn/auto_schedule/measure/measurer_test.cc index e1399cc03614e..89a2feece5aea 100644 --- a/paddle/cinn/auto_schedule/measure/measurer_test.cc +++ b/paddle/cinn/auto_schedule/measure/measurer_test.cc @@ -26,6 +26,7 @@ #include "paddle/cinn/frontend/syntax.h" #include "paddle/cinn/hlir/framework/graph_compiler.h" #include "paddle/cinn/hlir/framework/graph_compiler_util.h" +#include "paddle/cinn/hlir/framework/op_lowering.h" #include "paddle/cinn/runtime/flags.h" namespace cinn { @@ -75,12 +76,12 @@ class TestMeasurer : public ::testing::Test { absl::flat_hash_map>( "infershape"); - auto op_lowerer = std::make_unique( - dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); inputs.reserve(tasks.size()); for (int i = 0; i < tasks.size(); ++i) { auto* task = &tasks[i]; - task->Initialize(shape_dict, dtype_dict, op_lowerer.get()); + task->Initialize(shape_dict, dtype_dict, &op_lowerer); MeasureInput input; input.task = task; input.lowered_funcs = task->lowered_funcs; diff --git a/paddle/cinn/auto_schedule/search_space/auto_gen_rule/auto_inline_test.cc b/paddle/cinn/auto_schedule/search_space/auto_gen_rule/auto_inline_test.cc index d68d901905228..0e18e1b7b70e3 100644 --- a/paddle/cinn/auto_schedule/search_space/auto_gen_rule/auto_inline_test.cc +++ b/paddle/cinn/auto_schedule/search_space/auto_gen_rule/auto_inline_test.cc @@ -161,14 +161,14 @@ TEST(AutoInline, AddReluInline) { "inferdtype"); const auto& shape_dict = graph->GetAttrs< absl::flat_hash_map>("infershape"); - auto op_lowerer = std::make_unique( - dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); EXPECT_EQ(graph->fusion_groups.size(), 1UL); std::vector funcs = - op_lowerer->Lower(graph->fusion_groups[0], - /*apply_op_schedule = */ false, - /*apply_group_schedule=*/false); + op_lowerer.Lower(graph->fusion_groups[0], + /*apply_op_schedule = */ false, + /*apply_group_schedule=*/false); VLOG(6) << "Expr before auto inline: " << funcs[0]->body; diff --git a/paddle/cinn/auto_schedule/search_space/auto_gen_rule/test_helper.cc b/paddle/cinn/auto_schedule/search_space/auto_gen_rule/test_helper.cc index 19a9534dfd694..ef7f2a4ab6dc5 100644 --- a/paddle/cinn/auto_schedule/search_space/auto_gen_rule/test_helper.cc +++ b/paddle/cinn/auto_schedule/search_space/auto_gen_rule/test_helper.cc @@ -61,7 +61,8 @@ ir::IRSchedule TestAutoGenRuleBase::MakeIRSchedule( "inferdtype"); auto& shape_dict = graph->GetMutableAttrs< absl::flat_hash_map>("infershape"); - hlir::framework::OpLowerer op_lowerer(dtype_dict, shape_dict, target_); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target_); lowered_funcs_ = op_lowerer.Lower(graph->fusion_groups.front(), diff --git a/paddle/cinn/auto_schedule/search_strategy/evolutionary_search_test.cc b/paddle/cinn/auto_schedule/search_strategy/evolutionary_search_test.cc index 25a9e1f72192e..539be166f28cf 100644 --- a/paddle/cinn/auto_schedule/search_strategy/evolutionary_search_test.cc +++ b/paddle/cinn/auto_schedule/search_strategy/evolutionary_search_test.cc @@ -27,6 +27,7 @@ #include "paddle/cinn/auto_schedule/task/task_registry.h" #include "paddle/cinn/auto_schedule/task/tune_task.h" #include "paddle/cinn/auto_schedule/tuning.h" +#include "paddle/cinn/hlir/framework/op_lowering.h" #include "paddle/cinn/ir/ir_base.h" #include "paddle/cinn/ir/schedule/ir_schedule.h" #include "test/cpp/cinn/program_builder.h" @@ -44,11 +45,11 @@ std::vector CreateTasks(const frontend::Program& program, "inferdtype"); const auto& shape_dict = graph->GetAttrs< absl::flat_hash_map>("infershape"); - auto op_lowerer = std::make_unique( - dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); InitialTaskRegistry* task_registry = InitialTaskRegistry::Global(); for (auto i = 0; i < tasks.size(); ++i) { - tasks[i].Initialize(shape_dict, dtype_dict, op_lowerer.get()); + tasks[i].Initialize(shape_dict, dtype_dict, &op_lowerer); task_registry->Regist(tasks[i].serialized_key, ir::ModuleExpr(tasks[i].GetLoweredFuncBodyExprs())); } diff --git a/paddle/cinn/auto_schedule/task/task_registry_test.cc b/paddle/cinn/auto_schedule/task/task_registry_test.cc index ade9b495578a6..26e790b25bd08 100644 --- a/paddle/cinn/auto_schedule/task/task_registry_test.cc +++ b/paddle/cinn/auto_schedule/task/task_registry_test.cc @@ -45,11 +45,10 @@ std::vector CreateTasks(hlir::framework::Graph* graph, const auto& shape_dict = graph->GetAttrs< absl::flat_hash_map>("infershape"); - std::unique_ptr op_lowerer = - std::make_unique( - dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); for (TuneTask& task : tasks) { - task.Initialize(shape_dict, dtype_dict, op_lowerer.get()); + task.Initialize(shape_dict, dtype_dict, &op_lowerer); VLOG(3) << "Add a task with serialized_key:\n" << task.serialized_key; } diff --git a/paddle/cinn/auto_schedule/task/tune_task.cc b/paddle/cinn/auto_schedule/task/tune_task.cc index c0e150990dc2f..f2c2b720b6f06 100644 --- a/paddle/cinn/auto_schedule/task/tune_task.cc +++ b/paddle/cinn/auto_schedule/task/tune_task.cc @@ -34,7 +34,7 @@ void TuneTask::Initialize( const absl::flat_hash_map& shape_dict, const absl::flat_hash_map& dtype_dict, - hlir::framework::OpLowerer* lower_handler) { + hlir::framework::OpLowerer* lower_handler) { CHECK(lower_handler != nullptr) << "op_lowerer can't be nullptr"; op_lowerer = lower_handler; diff --git a/paddle/cinn/auto_schedule/task/tune_task.h b/paddle/cinn/auto_schedule/task/tune_task.h index 033c7ccf397d8..92bf5c73ca3f3 100644 --- a/paddle/cinn/auto_schedule/task/tune_task.h +++ b/paddle/cinn/auto_schedule/task/tune_task.h @@ -34,16 +34,17 @@ namespace cinn { namespace auto_schedule { class TuneTask { + using GroupPtr = hlir::framework::GroupPtr; + public: TuneTask() = default; - explicit TuneTask(std::shared_ptr group) - : subgraph(group) {} + explicit TuneTask(GroupPtr group) : subgraph(group) {} // Initialize a task void Initialize( const absl::flat_hash_map& shape_dict, const absl::flat_hash_map& dtype_dict, - hlir::framework::OpLowerer* lower_handler); + hlir::framework::OpLowerer* lower_handler); // Extract bodies in lowered_funcs() and return std::vector GetLoweredFuncBodyExprs() const; @@ -51,7 +52,7 @@ class TuneTask { // sub-graph (if an op won't be fused, it will be a Group with size=1). std::shared_ptr subgraph; // Lower handler, Not owned - hlir::framework::OpLowerer* op_lowerer; + hlir::framework::OpLowerer* op_lowerer; // target of this task common::Target target; // stores the initial (un-optimized) LoweredFuncs diff --git a/paddle/cinn/auto_schedule/task/tune_task_test.cc b/paddle/cinn/auto_schedule/task/tune_task_test.cc index 853bcc4a19e0b..fbc3d907fc5e5 100644 --- a/paddle/cinn/auto_schedule/task/tune_task_test.cc +++ b/paddle/cinn/auto_schedule/task/tune_task_test.cc @@ -75,7 +75,8 @@ TEST(TuneTask, GraphToUnoptLoweredFunc_NoPass) { const auto& dtype_dict = graph->GetAttrs>( "inferdtype"); - OpLowerer op_lowerer(dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); std::stringstream ss; for (TuneTask& task : tasks) { @@ -187,7 +188,8 @@ TEST(TuneTask, GraphToUnoptLoweredFunc_ApplyPass) { graph->GetAttrs>( "inferdtype"); - OpLowerer op_lowerer(dtype_dict, shape_dict, target); + OpLowerer op_lowerer( + new hlir::framework::OpLowererImpl(dtype_dict, shape_dict, target)); std::stringstream ss; for (TuneTask& task : tasks) { @@ -291,7 +293,8 @@ TEST(TuneTask, SerializeToString) { const auto& dtype_dict = graph->GetAttrs>( "inferdtype"); - OpLowerer op_lowerer(dtype_dict, shape_dict, target); + OpLowerer op_lowerer( + new hlir::framework::OpLowererImpl(dtype_dict, shape_dict, target)); ASSERT_EQ(single_tasks.size(), 2UL); for (auto&& task : single_tasks) { task.Initialize(shape_dict, dtype_dict, &op_lowerer); diff --git a/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc b/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc index d36a25193a612..bfa152ce558b5 100644 --- a/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc +++ b/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc @@ -27,6 +27,7 @@ #include "paddle/cinn/hlir/framework/graph_compiler.h" #include "paddle/cinn/hlir/framework/graph_compiler_util.h" #include "paddle/cinn/hlir/framework/node.h" +#include "paddle/cinn/hlir/framework/op_lowering.h" #include "paddle/cinn/hlir/framework/pass.h" #include "paddle/cinn/ir/ir_base.h" #include "paddle/cinn/runtime/flags.h" @@ -143,9 +144,8 @@ class PerformanceTester : public ::testing::Test { absl::flat_hash_map>( "infershape"); - std::shared_ptr op_lowerer = - std::make_unique( - dtype_dict, shape_dict, target_); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target_); CompilationContext& context = graph_compiler->GetCompilationContext(); context.with_instantiate_variables = true; @@ -157,9 +157,9 @@ class PerformanceTester : public ::testing::Test { for (auto group : graph->fusion_groups) { context.lowered_funcs.push_back( - op_lowerer->Lower(group, - /*apply_op_schedule = */ false, - /*apply_group_schedule=*/false)); + op_lowerer.Lower(group, + /*apply_op_schedule = */ false, + /*apply_group_schedule=*/false)); } VLOG(3) << "===========================No Schedule LoweredFunc " diff --git a/paddle/cinn/backends/compiler.cc b/paddle/cinn/backends/compiler.cc index cd6a38ec16c28..1e806db8b92f4 100644 --- a/paddle/cinn/backends/compiler.cc +++ b/paddle/cinn/backends/compiler.cc @@ -19,6 +19,7 @@ #include "paddle/cinn/backends/llvm/runtime_symbol_registry.h" #include "paddle/cinn/common/context.h" #include "paddle/cinn/hlir/framework/visualize_helper.h" +#include "paddle/cinn/ir/utils/ir_printer.h" #ifdef CINN_WITH_CUDA #include "paddle/cinn/backends/codegen_cuda_dev.h" #include "paddle/cinn/backends/codegen_cuda_host.h" diff --git a/paddle/cinn/hlir/framework/CMakeLists.txt b/paddle/cinn/hlir/framework/CMakeLists.txt index 9753168130d78..d14ffa70234fc 100755 --- a/paddle/cinn/hlir/framework/CMakeLists.txt +++ b/paddle/cinn/hlir/framework/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(new_ir) core_gather_headers() gather_srcs( @@ -17,8 +18,8 @@ gather_srcs( node.cc pass.cc op_strategy.cc - op_lowering.cc op_lowering_util.cc + op_lowering_impl.cc accuracy_checker.cc visualize_helper.cc) diff --git a/paddle/cinn/hlir/framework/new_ir/CMakeLists.txt b/paddle/cinn/hlir/framework/new_ir/CMakeLists.txt new file mode 100755 index 0000000000000..e08baf06dbd13 --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/CMakeLists.txt @@ -0,0 +1,4 @@ +if(NOT CINN_ONLY) + core_gather_headers() + gather_srcs(cinnapi_src SRCS utils.cc op_lowering_impl.cc) +endif() diff --git a/paddle/cinn/hlir/framework/new_ir/group.h b/paddle/cinn/hlir/framework/new_ir/group.h new file mode 100644 index 0000000000000..2462fb8c4ce91 --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/group.h @@ -0,0 +1,52 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once +#include +#include + +#include "paddle/cinn/hlir/framework/op.h" +#include "paddle/ir/core/operation.h" + +namespace cinn { +namespace hlir { +namespace framework { +namespace newir { +using framework::OpPatternKind; + +// TODO(Aurelius84): Need to be replaced with CinnGroupOp +struct Group { + public: + explicit Group(const std::vector<::ir::Operation*>& group_ops) + : ops(group_ops) { + op_pattern_kind = OpPatternKind::kElementWise; + fn_name = "fn_"; + for (auto& op : group_ops) { + fn_name += "_" + op->name(); + } + } + + std::vector<::ir::Operation*> ops; + std::vector input_names; + std::vector output_names; + int group_id; + // FIXME(Aurelius84): This should be refactored with CinnGroupOp + OpPatternKind op_pattern_kind; + std::string fn_name; +}; + +} // namespace newir +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc new file mode 100644 index 0000000000000..882d6409c3678 --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc @@ -0,0 +1,451 @@ +// Copyright (c) 2022 CINN Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h" + +#include +#include "paddle/cinn/hlir/framework/op_lowering_util.h" +#include "paddle/cinn/hlir/op/external_api_registry.h" +#include "paddle/cinn/ir/schedule/ir_schedule.h" +#include "paddle/cinn/optim/transform_gpu_forloop.h" + +#include "paddle/cinn/hlir/framework/new_ir/utils.h" +#include "paddle/cinn/lang/placeholder.h" +#include "paddle/cinn/utils/attribute_util.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" +#include "paddle/phi/core/ddim.h" + +DECLARE_bool(cinn_use_cuda_vectorize); + +namespace cinn { +namespace hlir { +namespace framework { +namespace newir { + +using cinn::hlir::op::ExternalApiRegistry; +using common::Type; +using framework::OpPatternKind; +using framework::StrategyFunction; + +namespace details { +ir::Tensor GetTensor(const ::ir::Value& value) { + auto type_info = value.type().dyn_cast(); + auto in_shape = phi::vectorize(type_info.dims()); + auto dtype = type_info.dtype(); + std::string input_id = CompatibleInfo::InputName(value); + return lang::CreatePlaceHolder( + in_shape, utils::ConvertIRType(dtype), input_id); +} + +std::vector CollectInputTensor( + const ::ir::Operation* op, + std::vector* func_args, + std::unordered_map<::ir::Value, ir::Tensor>* tensor_map) { + std::vector tensors; + for (auto& operand : op->operands()) { + CHECK(operand); + auto in_value = operand.source(); + ir::Tensor tensor; + if (!tensor_map->count(in_value)) { + tensor = details::GetTensor(in_value); + // record tensor. + (*tensor_map)[in_value] = tensor; + // record func input args + if (func_args != nullptr) func_args->push_back(tensor); + } else { + tensor = tensor_map->at(in_value); + } + tensors.push_back(tensor); + } + return tensors; +} + +void CollectOutputInfo(const ::ir::Operation* op, + std::vector* out_types, + std::vector>* out_shapes) { + auto op_results = op->results(); + for (auto& out_value : op_results) { + std::string output_id = CompatibleInfo::OutputName(out_value); + // group->output_names.push_back(output_id); + auto type_info = + out_value.type().dyn_cast(); + + out_types->push_back(utils::ConvertIRType(type_info.dtype())); + auto out_shape = phi::vectorize(type_info.dims()); + out_shapes->push_back(std::move(out_shape)); + } +} + +NodeAttr CollectAttrs(const ::ir::Operation& op) { + NodeAttr node_attrs; + VLOG(4) << "op.attributes():" << op.attributes().size(); + auto attrs = utils::ConvertAttributes(op.attributes()); + node_attrs.node_name = CompatibleInfo::OpName(op); + node_attrs.attr_store = std::move(attrs); + + return node_attrs; +} + +} // namespace details + +OpLowererImpl::OpLowererImpl(const Target& target) : target_(target) {} + +std::vector OpLowererImpl::Lower(const GroupPtr& group, + bool apply_op_schedule, + bool apply_group_schedule) { + VLOG(3) << "Lowering Group : " << group->group_id + << " , Op Pattern : " << group->op_pattern_kind; + group->input_names.clear(); + group->output_names.clear(); + switch (group->op_pattern_kind) { + case framework::kElementWise: + case framework::kBroadcast: + case framework::kInjective: + return LowerGroup(group, + apply_op_schedule, + apply_group_schedule, + &OpLowererImpl::ElementwiseScheduleDetermineFunction); + case framework::kReduction: + return LowerGroup(group, + apply_op_schedule, + apply_group_schedule, + &OpLowererImpl::ReduceScheduleDetermineFunction); + case framework::kOutFusible: + LOG(FATAL) << "Group Pattern Kind kOutFusible Is Not Implemented!"; + case framework::kNonFusible: + return LowerGroup(group, + apply_op_schedule, + apply_group_schedule, + &OpLowererImpl::NonFusibleScheduleDetermineFunction); + default: + LOG(FATAL) << "Group Pattern Kind Is Unknown!"; + } +} + +bool OpLowererImpl::ElementwiseScheduleDetermineFunction(::ir::Operation* op) { + return true; +} + +bool OpLowererImpl::ReduceScheduleDetermineFunction(::ir::Operation* op) { + // TODO(Aurelius84): Support this. + // auto& op_pattern_dict = Operator::GetAttrs("OpPattern"); + // return op_pattern_dict[op] == framework::kReduction; + return true; +} + +bool OpLowererImpl::NonFusibleScheduleDetermineFunction(::ir::Operation* op) { + return true; +} + +std::vector OpLowererImpl::LowerGroup( + const GroupPtr& group, + bool apply_op_schedule, + bool apply_group_schedule, + ScheduleDetermineFunction schedule_determine_func) { + // 1.Do compute, lower and schedule for each op. + auto& ops = group->ops; + if (ops.size() == 1 && ops[0]->name() == "custom_call") { + return LowerCustomCall(group); + } + std::vector group_func_arg_tensors; + std::unordered_map<::ir::Value, ir::Tensor> tensor_map; + bool do_op_schedule = apply_group_schedule || apply_op_schedule; + std::vector func_bodies = LowerOps(ops, + do_op_schedule, + schedule_determine_func, + &group_func_arg_tensors, + &tensor_map); + + // 2.Do group schedule. + ir::ModuleExpr mod_expr(func_bodies); + ir::IRSchedule ir_sch(mod_expr); + ir_sch.MergeExprs(); + VLOG(3) << "After lower, ir is: \n" << ir_sch.GetModule().GetExprs().at(0); + // TODO(Aurelius84): Support this. + // if (apply_group_schedule) { + // DoGroupSchedule(ir_sch, group, tensor_map); + // VLOG(3) << "After group schedule, ir is: \n" + // << ir_sch.GetModule().GetExprs().at(0); + // } + + // 3.Do post-processing, + // including preparing function args and temporary variables, + // applying low-level optimization passes, etc. + return PostProcess( + group, tensor_map, do_op_schedule, &ir_sch, &group_func_arg_tensors); +} + +std::vector OpLowererImpl::LowerCustomCall( + const GroupPtr& group) { + auto& ops = group->ops; + CHECK_EQ(ops.size(), 1); + ::ir::Operation* op = ops[0]; + std::unordered_map<::ir::Value, ir::Tensor> tensor_map; + std::vector op_func_arg_tensors = + details::CollectInputTensor(op, nullptr, &tensor_map); + VLOG(4) << "inputs.size(): " << op_func_arg_tensors.size(); + + std::vector out_types; + std::vector> out_shapes; + details::CollectOutputInfo(op, &out_types, &out_shapes); + VLOG(4) << "out_types.size(): " << out_types.size(); + + NodeAttr node_attrs = details::CollectAttrs(*op); + + auto& cinn_strategy = Operator::GetAttrs("CINNStrategy"); + const hlir::framework::Operator* cinn_op = + Operator::Get(node_attrs.node_name); + auto impl = OpStrategy::SelectImpl(cinn_strategy[cinn_op]( + node_attrs, op_func_arg_tensors, out_types, out_shapes, target_)); + + // TODO(Arelius84): Support extern API + std::string external_api; + // if (node_attrs.attr_store.count("custom_call")) { + // external_api = + // absl::get(node_attrs.attr_store.at("custom_call")); + // } else { + // external_api = ExternalApiRegistry::Global()->GetExternalApi(node, + // target_); + // } + std::vector compute_args = { + common::CINNValue(group->fn_name), common::CINNValue(external_api)}; + common::CINNValuePack pack = + impl->fcompute(common::CINNValuePack{compute_args}); + CHECK_EQ(pack.size(), 1UL); + // reset input names as extern api input args can't be remove duplicate. + // group->input_names.clear(); + // for (auto& inode : node->inlinks_in_order()) { + // group->input_names.push_back(inode->source()->as()->id()); + // } + return {pack[0].operator ir::Expr().as_lowered_func_ref()}; +} + +std::vector OpLowererImpl::PostProcess( + const GroupPtr& group, + const std::unordered_map<::ir::Value, ir::Tensor>& tensor_map, + bool done_op_schedule, + ir::IRSchedule* ir_sch, + std::vector* group_func_arg_tensors) { + // 1.Prepare function args + group->input_names.clear(); + std::vector group_func_args; + std::unordered_set arg_name_set; + for (auto& arg_tensor : *group_func_arg_tensors) { + // input data name. + group->input_names.push_back(arg_tensor->name); + // input args + group_func_args.emplace_back(arg_tensor->buffer, ir::Argument::IO::kInput); + arg_name_set.insert(arg_tensor->buffer->name); + } + + group->output_names.clear(); + // FIXME(Aurelius84): Do we need to use output_ops? + // Currently we regards all ops as output_ops. + for (auto& op : group->ops) { + // collect all output tensor. + for (auto opresult : op->results()) { + if (tensor_map.count(opresult) == 0) { + continue; + } + auto tensor = tensor_map.at(opresult); + if (arg_name_set.count(tensor->buffer->name) != 0) { + continue; + } + // output arg tensors + group_func_arg_tensors->push_back(tensor); + // output args + group_func_args.emplace_back(tensor->buffer, ir::Argument::IO::kOutput); + arg_name_set.insert(tensor->buffer->name); + } + } + + if (!done_op_schedule) { + std::unordered_set args_set; + for (auto arg : group_func_args) { + args_set.insert(arg.name()); + } + + for (auto& tensor_pair : tensor_map) { + if (args_set.count("_" + tensor_pair.second->name)) { + continue; + } + group_func_arg_tensors->push_back(tensor_pair.second); + // use the underlying tensor name to be consistent with the argument name + // in the lowered function + group->output_names.push_back(tensor_pair.second->name); + group_func_args.emplace_back(tensor_pair.second->buffer, + ir::Argument::IO::kOutput); + } + } + + auto func_body = ir_sch->GetModule().GetExprs().at(0); +#ifdef CINN_WITH_CUDA + optim::OptimizeExprGPU(&(func_body)); +#endif + + // 2.Prepare temp buffers + poly::StageMap stages; + auto temp_buffers = + lang::GetTempBuffers(*group_func_arg_tensors, stages, func_body); + // 3.Building LoweredFunc + auto func = ir::_LoweredFunc_::Make(group->fn_name, + group_func_args, + ir_sch->GetModule().GetExprs().at(0), + temp_buffers); + if (!done_op_schedule) { + func->PrepareBufferCastExprs(); + } + // 4.Apply low level pass + func = optim::Optimize(Expr(func), target_, false).as_lowered_func_ref(); + return {func}; +} + +std::vector OpLowererImpl::LowerOps( + const std::vector<::ir::Operation*>& ops, + bool apply_op_schedule, + ScheduleDetermineFunction schedule_determine_func, + std::vector* group_func_arg_tensors, + std::unordered_map<::ir::Value, ir::Tensor>* tensor_map) { + auto& strategy = Operator::GetAttrs("CINNStrategy"); + std::vector func_bodies; + for (auto* op : ops) { + // 1.Select Op impl + std::vector out_types; + std::vector> out_shapes; + details::CollectOutputInfo(op, &out_types, &out_shapes); + VLOG(4) << "out_types.size(): " << out_types.size(); + NodeAttr node_attrs = details::CollectAttrs(*op); + + std::vector op_func_arg_tensors = + details::CollectInputTensor(op, group_func_arg_tensors, tensor_map); + std::string cinn_op_name = CompatibleInfo::OpName(*op); + const hlir::framework::Operator* cinn_op = Operator::Get(cinn_op_name); + auto op_impl = OpStrategy::SelectImpl(strategy[cinn_op]( + node_attrs, op_func_arg_tensors, out_types, out_shapes, this->target_)); + + // 2.Perform the lower process of Op + std::vector funcs = + DoOpLower(op_impl, op, tensor_map, &op_func_arg_tensors); + + if (apply_op_schedule && (this->*schedule_determine_func)(op)) { + // 3.Perform the schedule of Op + func_bodies.push_back(DoOpSchedule(op_impl, op_func_arg_tensors, funcs)); + } else { + for (const ir::LoweredFunc& func : funcs) { + func_bodies.push_back(func->body); + } + } + } + + return func_bodies; +} + +std::vector OpLowererImpl::DoOpLower( + std::shared_ptr op_impl, + const ::ir::Operation* op, + std::unordered_map<::ir::Value, ir::Tensor>* tensor_map, + std::vector* op_func_arg_tensors) { + VLOG(4) << "Do lower with Compute, op: " << op->name(); + std::vector cinn_inputs; + for (const ir::Tensor& tensor : *op_func_arg_tensors) { + cinn_inputs.push_back(common::CINNValue(ir::Expr(tensor))); + } + // set tensor name = operand hash name + auto op_results = op->results(); + for (const auto& result : op_results) { + std::string output_id = CompatibleInfo::OutputName(result); + cinn_inputs.push_back(common::CINNValue(output_id)); + } + + // 1.Do compute + common::CINNValuePack pack = + op_impl->fcompute(common::CINNValuePack{cinn_inputs}); + + poly::StageMap tmp_stages = pack.back(); + std::string post = ""; + for (int idx = 0; idx < pack.size() - 1; ++idx) { + Expr expr = pack[idx]; + // Insert the output tensor defined by Compute into the tensor_map + if (pack.size() - 1 > op_results.size()) { + // Some op may output multiple temp tensors in their Compute + // definition, but only one output in the graph, and we use id + + // "_0"/"_1" as key. + // FIXME(Aurelius84): It seems that the implementation is relate with + // string name. + // (*tensor_map)[op_results[0] + post] = expr.as_tensor_ref(); + // post = "_" + std::to_string(idx); + } else { + // If the number of output tensors defined by Compute is less equal than + // the output node_data on the graph, then there is a one-to-one + // correspondence, and the redundant output node_data contact empty. + (*tensor_map)[op_results[idx]] = expr.as_tensor_ref(); + } + + // Insert output tensors into function arg + if (!expr.as_tensor_ref()->buffer.defined() || + this->target_ != common::DefaultNVGPUTarget()) { + op_func_arg_tensors->push_back(expr.as_tensor_ref()); + expr.as_tensor_ref()->WithBuffer(); + } + } + + // 2.Do lower + std::string lower_fn_name = CompatibleInfo::OpFuncName(*op); + std::vector funcs = lang::LowerVec(lower_fn_name, + tmp_stages, + *op_func_arg_tensors, + {}, + {}, + nullptr, + this->target_, + true); + VLOG(4) << "Lower op: " << lower_fn_name << ", get " << funcs.size() + << " LoweredFunc:\n"; + + op_func_arg_tensors->clear(); + for (int idx = 0; idx < pack.size() - 1; ++idx) { + CHECK(pack[idx].is_tensor()); + op_func_arg_tensors->push_back( + pack[idx].operator ir::Expr().as_tensor_ref()); + } + + return funcs; +} + +ir::Expr OpLowererImpl::DoOpSchedule( + std::shared_ptr op_impl, + const std::vector& op_func_arg_tensors, + const std::vector& lowered_funcs) { + VLOG(4) << "Do op schedule"; + std::vector schedule_inputs; + // 1.Collect tensors + for (const ir::Tensor& op_func_arg_tensor : op_func_arg_tensors) { + schedule_inputs.push_back(common::CINNValue(op_func_arg_tensor)); + } + // 2.Collect bodies to be scheduled + for (const ir::LoweredFunc& func : lowered_funcs) { + schedule_inputs.push_back(common::CINNValue(func->body)); + } + // 3.Do schedule on AST + common::CINNValuePack expr_pack = + op_impl->fschedule(common::CINNValuePack{schedule_inputs}); + VLOG(4) << "After op schedule: " << expr_pack[0].operator ir::Expr(); + + return expr_pack[0].operator ir::Expr(); +} + +} // namespace newir +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h new file mode 100644 index 0000000000000..ffa6218299100 --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h @@ -0,0 +1,162 @@ +// Copyright (c) 2022 CINN Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include + +#include "paddle/cinn/common/target.h" +#include "paddle/cinn/hlir/framework/instruction.h" +#include "paddle/cinn/hlir/framework/new_ir/group.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl_base.h" +#include "paddle/cinn/hlir/framework/op_strategy.h" +#include "paddle/cinn/ir/lowered_func.h" +#include "paddle/cinn/ir/schedule/ir_schedule.h" +#include "paddle/cinn/ir/schedule/ir_schedule_util.h" +#include "paddle/cinn/lang/packed_func.h" +#include "paddle/ir/core/operation.h" + +// Fusion Op lowering, there are four kinds of lowering function: +// Elementwise/Broadcast/Injective,Reduce,OutEWiseFusable,NonFusible. +// Elementwise/Broadcast/Injective Ops is with same shcedule. +// Reduce,OutEWiseFusable,NonFusible are using different schedule. + +namespace cinn { +namespace hlir { +namespace framework { +namespace newir { + +using GroupPtr = std::shared_ptr; + +using common::Target; +class OpLowererImpl; + +typedef bool (OpLowererImpl::*ScheduleDetermineFunction)(::ir::Operation*); + +class OpLowererImpl : public OpLowererImplBase { + public: + explicit OpLowererImpl(const Target&); + + /** + * @brief Lower a group to CINN IR. + * @param group The group to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param apply_group_schedule Whether to schedule at group level. + * @return The lowered funcs. + */ + std::vector Lower(const GroupPtr& group, + bool apply_op_schedule = true, + bool apply_group_schedule = true); + + private: + /** + * @brief Lower a group to CINN IR. + * @param group The group to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param apply_group_schedule Whether to schedule at group level. + * @param schedule_determine_func Function used to determine which Ops to + * schedule. + * @return The lowered funcs. + */ + std::vector LowerGroup( + const GroupPtr& group, + bool apply_op_schedule, + bool apply_group_schedule, + ScheduleDetermineFunction schedule_determine_func); + + /** + * @brief Lower a group composed of CustomCall Op. + * @param group The group to be lowered. + * @return The lowered funcs. + */ + std::vector LowerCustomCall(const GroupPtr& group); + + /** + * @brief Post processing, including preparing function args and temporary + * variables, applying low-level optimization passes, etc. + * @param group The group to be lowered. + * @param tensor_map All tensors used for calculating the group. + * @param done_op_schedule Mark whether the Op level schedule has been + * applied. + * @param ir_sch The IRSchedule object of group. + * @param group_func_arg_tensors Tensors used as the group function arguments. + * @return The lowered funcs after the post processing. + */ + std::vector PostProcess( + const GroupPtr& group, + const std::unordered_map<::ir::Value, ir::Tensor>& tensor_map, + bool done_op_schedule, + ir::IRSchedule* ir_sch, + std::vector* group_func_arg_tensors); + + /** + * @brief Lower an Op set to CINN IR. + * Compute, Lower and optional Schedule will be performed one by one + * for each Op. + * @param ops The Op to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param schedule_determine_func Function used to determine which Ops to + * schedule. + * @param group_func_arg_tensors Tensors used as the group function arguments. + * @param tensor_map All tensors used for calculating the group. + * @return The lowered func bodies of Op set. + */ + std::vector LowerOps( + const std::vector<::ir::Operation*>& ops, + bool apply_op_schedule, + ScheduleDetermineFunction schedule_determine_func, + std::vector* group_func_arg_tensors, + std::unordered_map<::ir::Value, ir::Tensor>* tensor_map); + + /** + * @brief Lower an Op to CINN IR. The Compute and Lower processes will be + * called sequentially. + * @param op_impl The Op implementation defining Compute and Schedule. + * @param op The Op to be lowered. + * @param tensor_map All tensors used for calculating the group. + * @param op_func_arg_tensors Tensors used as the Op function arguments. + * @return The lowered func of the Op. + */ + std::vector DoOpLower( + std::shared_ptr op_impl, + const ::ir::Operation* op, + std::unordered_map<::ir::Value, ir::Tensor>* tensor_map, + std::vector* op_func_arg_tensors); + + /** + * @brief Apply schedule on an Op. + * @param op_impl The Op implementation defining Compute and Schedule. + * @param op_func_arg_tensors Tensors used as the Op function arguments. + * @param lowered_funcs The lowered funcs of an Op to be scheduled. + * @return The lowered func body after schedule of the Op. + */ + ir::Expr DoOpSchedule(std::shared_ptr op_impl, + const std::vector& op_func_arg_tensors, + const std::vector& lowered_funcs); + + // Functions used to determine which Ops to schedule at op level, define a + // policy for each type of group. + inline bool ReduceScheduleDetermineFunction(::ir::Operation* op); + inline bool ElementwiseScheduleDetermineFunction(::ir::Operation* op); + inline bool NonFusibleScheduleDetermineFunction(::ir::Operation* op); + + private: + Target target_; +}; + +} // namespace newir +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/new_ir/utils.cc b/paddle/cinn/hlir/framework/new_ir/utils.cc new file mode 100644 index 0000000000000..12b3783e7c85d --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/utils.cc @@ -0,0 +1,96 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/cinn/hlir/framework/new_ir/utils.h" + +namespace cinn { +namespace hlir { +namespace framework { +namespace newir { + +const std::unordered_map CompatibleInfo::OP_NAMES = { + {"pd.full", "fill_constant"}}; + +std::string CompatibleInfo::OpName(const ::ir::Operation& op) { + std::string name = op.name(); + if (OP_NAMES.count(name)) { + return OP_NAMES.at(name); + } + auto pos = name.find("."); + if (pos == std::string::npos) { + return name; + } + auto cinn_op_name = name.substr(pos + 1); + VLOG(4) << "GetOpName: " << name << " -> " << cinn_op_name; + return cinn_op_name; +} + +std::string CompatibleInfo::InputName(const ::ir::Value& value) { + return CompatibleInfo::kInputPrefix + + std::to_string(std::hash<::ir::Value>()(value)); +} + +std::string CompatibleInfo::OutputName(const ::ir::Value& value) { + return CompatibleInfo::kOutputPrefix + + std::to_string(std::hash<::ir::Value>()(value)); +} + +std::string CompatibleInfo::OpFuncName(const ::ir::Operation& op) { + std::string op_name = OpName(op); + std::string func_name = + cinn::common::Context::Global().NewName("fn_" + op_name); + return func_name; +} + +std::string CompatibleInfo::GroupOpsName( + const std::vector<::ir::Operation*>& ops) { + std::string name = "fn_"; + for (auto* op : ops) { + std::string op_name = OpName(*op); + name += cinn::common::Context::Global().NewName(op_name); + } + return name; +} + +std::vector CompatibleInfo::InputNames(const ::ir::Operation& op, + bool allow_duplicate) { + std::vector names; + std::unordered_set repeat; + for (int i = 0; i < op.num_operands(); ++i) { + auto value = op.operand_source(i); + std::string name = CompatibleInfo::InputName(value); + if (!allow_duplicate && repeat.count(name)) { + continue; + } + repeat.insert(name); + names.push_back(name); + } + return names; +} + +std::vector CompatibleInfo::OutputNames( + const ::ir::Operation& op) { + std::vector names; + for (int i = 0; i < op.num_results(); ++i) { + auto value = op.result(i); + std::string name = CompatibleInfo::OutputName(value); + names.push_back(std::move(name)); + } + return names; +} + +} // namespace newir +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/new_ir/utils.h b/paddle/cinn/hlir/framework/new_ir/utils.h new file mode 100644 index 0000000000000..7796899ce3482 --- /dev/null +++ b/paddle/cinn/hlir/framework/new_ir/utils.h @@ -0,0 +1,52 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once +#include +#include +#include "paddle/cinn/common/context.h" +#include "paddle/ir/core/operation.h" + +namespace cinn { +namespace hlir { +namespace framework { +namespace newir { + +struct CompatibleInfo { + static constexpr char* kInputPrefix = "input_"; + static constexpr char* kOutputPrefix = "output_"; + // TODO(Aurelius): Need add name mapping logic in REGISTER_CINN_OP + // macros or attempt to unify Op name with Paddle and CINN. + static const std::unordered_map OP_NAMES; + + static std::string OpName(const ::ir::Operation& op); + + static std::string InputName(const ::ir::Value& value); + + static std::string OutputName(const ::ir::Value& value); + + static std::string OpFuncName(const ::ir::Operation& op); + + static std::string GroupOpsName(const std::vector<::ir::Operation*>& ops); + + static std::vector InputNames(const ::ir::Operation& op, + bool allow_duplicate = false); + + static std::vector OutputNames(const ::ir::Operation& op); +}; + +} // namespace newir +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/new_ir_compiler.cc b/paddle/cinn/hlir/framework/new_ir_compiler.cc index f9f2cb460bf5a..f6954514ace6f 100644 --- a/paddle/cinn/hlir/framework/new_ir_compiler.cc +++ b/paddle/cinn/hlir/framework/new_ir_compiler.cc @@ -15,9 +15,7 @@ #include "paddle/cinn/hlir/framework/new_ir_compiler.h" #include -#include "paddle/cinn/hlir/framework/op_strategy.h" -#include "paddle/cinn/lang/lower.h" -#include "paddle/cinn/lang/placeholder.h" +#include "paddle/cinn/hlir/framework/new_ir/utils.h" #include "paddle/cinn/utils/attribute_util.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/ir/core/builtin_type.h" @@ -25,25 +23,31 @@ namespace cinn { namespace hlir { namespace framework { - -const std::unordered_map CompatibleInfo::OP_NAMES = { - {"pd.full", "fill_constant"}, {"pd.matmul", "matmul"}}; +using newir::CompatibleInfo; // TODO(Aurelius84): Need abstract this logic to implement Proxy for // the co-existance with GraphCompiler. std::unique_ptr NewIRCompiler::Build() { m_builder_.Clear(); // NOTE(Aurelius84): Currently only support each op for one group - std::vector> groups; + std::vector groups; for (auto it = program_.block()->begin(); it != program_.block()->end(); ++it) { - groups.push_back({*it}); + std::vector<::ir::Operation*> ops = {*it}; + groups.push_back(std::make_shared(ops)); + groups.back()->fn_name = CompatibleInfo::GroupOpsName(groups.back()->ops); } VLOG(4) << "Groups size: " << groups.size(); + return std::move(Build(groups)); +} + +std::unique_ptr NewIRCompiler::Build( + const std::vector& groups) { + auto op_lowerer = CreateOpLowerer(target_); std::vector> lowered_funcs; for (int i = 0; i < groups.size(); ++i) { - lowered_funcs.emplace_back(GetOpFunc(*groups[i][0], i)); + lowered_funcs.emplace_back(op_lowerer.Lower(groups[i])); } for (auto&& lowered_func : lowered_funcs) { @@ -70,83 +74,6 @@ std::unique_ptr NewIRCompiler::Build() { return std::make_unique(scope_, std::move(instructions)); } -std::vector NewIRCompiler::GetOpFunc(const ::ir::Operation& op, - int idx) { - std::vector inputs; - std::vector cinn_inputs; - auto op_name = op.name(); - VLOG(4) << "GetOpFunc for op: " << op_name; - // step 1: Deal with Oprands - for (int i = 0; i < op.num_operands(); ++i) { - auto in_value = op.operand_source(i); - // TODO(Aurelius84): For now, use addr as name but it's not wise. - std::string input_id = CompatibleInfo::kInputPrefix + - std::to_string(std::hash<::ir::Value>()(in_value)); - auto type_info = - in_value.type().dyn_cast(); - - auto in_shape = phi::vectorize(type_info.dims()); - auto dtype = type_info.dtype(); - ir::Tensor temp = lang::CreatePlaceHolder( - in_shape, utils::ConvertIRType(dtype), input_id); - inputs.push_back(temp); - cinn_inputs.push_back(common::CINNValue(temp)); - } - for (auto out_name : OpGetOutputNames(op)) { - cinn_inputs.push_back(common::CINNValue(out_name)); - } - - VLOG(4) << "inputs.size(): " << inputs.size(); - - // step 2: Deal with OpResult - std::vector out_types; - std::vector> out_shapes; - for (int i = 0; i < op.num_results(); ++i) { - auto out_value = op.result(i); - auto type_info = - out_value.type().dyn_cast(); - out_types.push_back(utils::ConvertIRType(type_info.dtype())); - auto out_shape = phi::vectorize(type_info.dims()); - out_shapes.push_back(std::move(out_shape)); - } - VLOG(4) << "out_types.size(): " << out_types.size(); - - NodeAttr node_attrs; - { - VLOG(4) << "op.attributes():" << op.attributes().size(); - auto attrs = utils::ConvertAttributes(op.attributes()); - node_attrs.node_name = CompatibleInfo::OP_NAMES.at(op_name); - node_attrs.attr_store = std::move(attrs); - } - auto& strategy = Operator::GetAttrs("CINNStrategy"); - // NOTE(Aurelius84): Do we need replace all hlir::framework Operator with - // ::ir::Program ? - const hlir::framework::Operator* cinn_op = - Operator::Get(CompatibleInfo::OP_NAMES.at(op_name)); - auto impl = OpStrategy::SelectImpl( - strategy[cinn_op](node_attrs, inputs, out_types, out_shapes, target_)); - common::CINNValuePack C = impl->fcompute(common::CINNValuePack{cinn_inputs}); - poly::StageMap stages = C.back(); - // make sure all the tensors in the stages before schedule launch. - for (int i = 0; i < C->size() - 1; i++) { - ir::Expr temp = C[i]; - stages->InsertLazily(temp.as_tensor_ref()); - } - C = impl->fschedule(C); - for (int i = 0; i < C->size() - 1; i++) { - ir::Expr temp = C[i]; - // checkout whether the tensor is with buffer. - if ((!temp.as_tensor_ref()->buffer.defined() || - this->target_ != common::DefaultNVGPUTarget()) && - !stages[temp.as_tensor_ref()]->inlined()) { - inputs.push_back(temp.as_tensor_ref()); - } - } - auto func = lang::LowerVec( - GenOpFuncName(op, idx), stages, inputs, {}, {}, nullptr, target_); - return func; -} - void NewIRCompiler::ProcessFunction( const std::vector& lowered_funcs) { for (auto&& func : lowered_funcs) { @@ -173,71 +100,32 @@ void NewIRCompiler::ProcessFunction( } std::vector> NewIRCompiler::BuildInstructions( - const std::vector>& groups) { + const std::vector& groups) { std::vector> instructions; for (int idx = 0; idx < groups.size(); ++idx) { // TODO(Aurelius84): only support single op in groups - auto& op = *groups[idx][0]; - auto instr_name = op.name(); - auto instr = - std::unique_ptr(new Instruction(target_, - scope_.get(), - OpGetInputNames(op), - OpGetOutputNames(op), - instr_name)); - auto& op_func_name = GenOpFuncName(op, idx); - auto* fn_ptr = compiler_->Lookup(op_func_name); + auto& op = *(groups[idx]->ops[0]); + + auto& fn_name = groups[idx]->fn_name; + auto instr = std::unique_ptr( + new Instruction(target_, + scope_.get(), + CompatibleInfo::InputNames(op), + CompatibleInfo::OutputNames(op), + fn_name)); + VLOG(1) << "Lookup kernel name: " << fn_name; + auto* fn_ptr = compiler_->Lookup(fn_name); CHECK(fn_ptr); - instr->SetLoweredFunc(reinterpret_cast(fn_ptr), op_func_name); + instr->SetLoweredFunc(reinterpret_cast(fn_ptr), fn_name); // As some instruction like reduce, will generate more than one kernel. // So try to find the rest kernel, if it exists. // SetSubKernels(instr.get(), op_func_name); - instr->Finalize(); instructions.push_back(std::move(instr)); } return instructions; } -const std::string& NewIRCompiler::GenOpFuncName(const ::ir::Operation& op, - int idx) { - // TODO(Aurelius84): . will raise compiler error in pd.xxx, need more - // elegant way to generate function name. - std::string op_name = op.name().substr(3) + "_" + std::to_string(idx); - std::string func_name = Context::Global().NewName("fn_" + op_name); - func_names_.try_emplace(op_name, func_name); - return func_names_.at(op_name); -} - -std::vector NewIRCompiler::OpGetInputNames( - const ::ir::Operation& op) { - std::vector names; - std::unordered_set repeat; - for (int i = 0; i < op.num_operands(); ++i) { - auto value = op.operand_source(i); - std::string name = CompatibleInfo::kInputPrefix + - std::to_string(std::hash<::ir::Value>()(value)); - if (repeat.count(name)) { - continue; - } - repeat.insert(name); - names.push_back(name); - } - return names; -} - -std::vector NewIRCompiler::OpGetOutputNames( - const ::ir::Operation& op) { - std::vector names; - for (int i = 0; i < op.num_results(); ++i) { - auto value = op.result(i); - std::string name = CompatibleInfo::kOutputPrefix + - std::to_string(std::hash<::ir::Value>()(value)); - names.push_back(std::move(name)); - } - return names; -} - std::shared_ptr BuildScope(const Target& target, const ::ir::Program& program) { std::unordered_set<::ir::Value> visited; diff --git a/paddle/cinn/hlir/framework/new_ir_compiler.h b/paddle/cinn/hlir/framework/new_ir_compiler.h index fc9d86cbd46ca..c9a430e39c57a 100644 --- a/paddle/cinn/hlir/framework/new_ir_compiler.h +++ b/paddle/cinn/hlir/framework/new_ir_compiler.h @@ -20,19 +20,12 @@ #include "paddle/ir/core/program.h" #include "paddle/cinn/hlir/framework/graph_compiler.h" +#include "paddle/cinn/hlir/framework/op_lowering.h" namespace cinn { namespace hlir { namespace framework { -struct CompatibleInfo { - static constexpr char* kInputPrefix = "input_"; - static constexpr char* kOutputPrefix = "output_"; - // TODO(Aurelius): Need add name mapping logic in REGISTER_CINN_OP - // macros or attempt to unify Op name with Paddle and CINN. - static const std::unordered_map OP_NAMES; -}; - // TODO(Aurelius84): Need abstract this logic to implement Proxy for // the co-existance with GraphCompiler. class NewIRCompiler final { @@ -46,21 +39,18 @@ class NewIRCompiler final { scope_(scope) {} std::unique_ptr Build(); - std::vector GetOpFunc(const ::ir::Operation& op, int idx); - void ProcessFunction(const std::vector& lowered_funcs); - std::vector> BuildInstructions( - const std::vector>& groups); + private: + CINN_DISALLOW_COPY_AND_ASSIGN(NewIRCompiler); - protected: - const std::string& GenOpFuncName(const ::ir::Operation& op, int idx); + std::unique_ptr Build(const std::vector& groups); - std::vector OpGetInputNames(const ::ir::Operation& op); + std::vector GetOpFunc(const ::ir::Operation& op, int idx); - std::vector OpGetOutputNames(const ::ir::Operation& op); + void ProcessFunction(const std::vector& lowered_funcs); - private: - CINN_DISALLOW_COPY_AND_ASSIGN(NewIRCompiler); + std::vector> BuildInstructions( + const std::vector& groups); const ::ir::Program& program_; ir::Module::Builder m_builder_; diff --git a/paddle/cinn/hlir/framework/op_lowering.h b/paddle/cinn/hlir/framework/op_lowering.h index d8cf1825df791..b0e0ad7d97b11 100644 --- a/paddle/cinn/hlir/framework/op_lowering.h +++ b/paddle/cinn/hlir/framework/op_lowering.h @@ -1,4 +1,4 @@ -// Copyright (c) 2022 CINN Authors. All Rights Reserved. +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -13,166 +13,66 @@ // limitations under the License. #pragma once - +#include #include #include -#include "paddle/cinn/common/target.h" #include "paddle/cinn/hlir/framework/graph.h" -#include "paddle/cinn/hlir/framework/instruction.h" -#include "paddle/cinn/hlir/framework/op_strategy.h" -#include "paddle/cinn/ir/lowered_func.h" -#include "paddle/cinn/ir/schedule/ir_schedule.h" -#include "paddle/cinn/ir/schedule/ir_schedule_util.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl_base.h" #include "paddle/cinn/lang/packed_func.h" - -// Fusion Op lowering, there are four kinds of lowering function: -// Elementwise/Broadcast/Injective,Reduce,OutEWiseFusable,NonFusible. -// Elementwise/Broadcast/Injective Ops is with same shcedule. -// Reduce,OutEWiseFusable,NonFusible are using different schedule. +#ifndef CINN_WITH_ONLY +#include "paddle/cinn/hlir/framework/new_ir/op_lowering_impl.h" +#endif namespace cinn { namespace hlir { namespace framework { -using GroupPtr = std::shared_ptr; using common::Target; +using GroupPtr = std::shared_ptr; -class OpLowerer; - -typedef bool (OpLowerer::*ScheduleDetermineFunction)(Node*); - +template class OpLowerer { public: - OpLowerer(const absl::flat_hash_map&, - const absl::flat_hash_map&, - const Target&); + explicit OpLowerer(OpLowererImplBase* impl) { impl_.reset(impl); } + ~OpLowerer() {} - /** - * @brief Lower a group to CINN IR. - * @param group The group to be lowered. - * @param apply_op_schedule Whether to schedule at Op level. - * @param apply_group_schedule Whether to schedule at group level. - * @return The lowered funcs. - */ - std::vector Lower(const GroupPtr& group, + std::vector Lower(const T& group, bool apply_op_schedule = true, - bool apply_group_schedule = true); + bool apply_group_schedule = true) { + return impl_->Lower(group, apply_op_schedule, apply_group_schedule); + } private: - /** - * @brief Lower a group to CINN IR. - * @param group The group to be lowered. - * @param apply_op_schedule Whether to schedule at Op level. - * @param apply_group_schedule Whether to schedule at group level. - * @param schedule_determine_func Function used to determine which Ops to - * schedule. - * @return The lowered funcs. - */ - std::vector LowerGroup( - const GroupPtr& group, - bool apply_op_schedule, - bool apply_group_schedule, - ScheduleDetermineFunction schedule_determine_func); - - /** - * @brief Lower a group composed of CustomCall Op. - * @param group The group to be lowered. - * @return The lowered funcs. - */ - std::vector LowerCustomCall(const GroupPtr& group); - - /** - * @brief Post processing, including preparing function args and temporary - * variables, applying low-level optimization passes, etc. - * @param group The group to be lowered. - * @param tensor_map All tensors used for calculating the group. - * @param done_op_schedule Mark whether the Op level schedule has been - * applied. - * @param ir_sch The IRSchedule object of group. - * @param group_func_arg_tensors Tensors used as the group function arguments. - * @return The lowered funcs after the post processing. - */ - std::vector PostProcess( - const GroupPtr& group, - const std::unordered_map& tensor_map, - bool done_op_schedule, - ir::IRSchedule* ir_sch, - std::vector* group_func_arg_tensors); - - /** - * @brief Lower an Op set to CINN IR. - * Compute, Lower and optional Schedule will be performed one by one - * for each Op. - * @param nodes The Op nodes to be lowered. - * @param apply_op_schedule Whether to schedule at Op level. - * @param schedule_determine_func Function used to determine which Ops to - * schedule. - * @param group_func_arg_tensors Tensors used as the group function arguments. - * @param tensor_map All tensors used for calculating the group. - * @return The lowered func bodies of Op set. - */ - std::vector LowerOps( - const std::vector& nodes, - bool apply_op_schedule, - ScheduleDetermineFunction schedule_determine_func, - std::vector* group_func_arg_tensors, - std::unordered_map* tensor_map); - - /** - * @brief Lower an Op to CINN IR. The Compute and Lower processes will be - * called sequentially. - * @param op_impl The Op implementation defining Compute and Schedule. - * @param node The Op node to be lowered. - * @param tensor_map All tensors used for calculating the group. - * @param op_func_arg_tensors Tensors used as the Op function arguments. - * @return The lowered func of the Op node. - */ - std::vector DoOpLower( - std::shared_ptr op_impl, - Node* node, - std::unordered_map* tensor_map, - std::vector* op_func_arg_tensors); - - /** - * @brief Apply schedule on an Op. - * @param op_impl The Op implementation defining Compute and Schedule. - * @param op_func_arg_tensors Tensors used as the Op function arguments. - * @param lowered_funcs The lowered funcs of an Op to be scheduled. - * @return The lowered func body after schedule of the Op. - */ - ir::Expr DoOpSchedule(std::shared_ptr op_impl, - const std::vector& op_func_arg_tensors, - const std::vector& lowered_funcs); - - /** - * @brief Apply schedule on a group. - * @param ir_sch The IRSchedule containing the entire group's lowered func - * bodies. - * @param group The group to be scheduled. - * @param tensor_map All tensors used for calculating the group. - * @return The lowered func body after schedule of the group. - */ - ir::Expr DoGroupSchedule( - ir::IRSchedule& ir_sch, // NOLINT - const GroupPtr& group, - const std::unordered_map& tensor_map); - - // Functions used to determine which Ops to schedule at op level, define a - // policy for each type of group. - inline bool ReduceScheduleDetermineFunction(Node* node); - inline bool ElementwiseScheduleDetermineFunction(Node* node); - inline bool NonFusibleScheduleDetermineFunction(Node* node); - - private: - Target target_; - const absl::flat_hash_map& type_dict_; - const absl::flat_hash_map& shape_dict_; - - // fucntion name prefix - const std::string func_name_prefix = "fn_"; + std::shared_ptr> impl_; }; +template +OpLowerer CreateOpLowerer(const absl::flat_hash_map&, + const absl::flat_hash_map&, + const Target&); + +template <> +inline OpLowerer CreateOpLowerer( + const absl::flat_hash_map& type_dict, + const absl::flat_hash_map& shape_dict, + const Target& target) { + auto* impl_base = new OpLowererImpl(type_dict, shape_dict, target); + return OpLowerer(impl_base); +} + +#ifndef CINN_WITH_ONLY +template +OpLowerer CreateOpLowerer(const Target&); + +template <> +inline OpLowerer CreateOpLowerer(const Target& target) { + auto* impl_base = new newir::OpLowererImpl(target); + return OpLowerer(impl_base); +} +#endif + } // namespace framework } // namespace hlir } // namespace cinn diff --git a/paddle/cinn/hlir/framework/op_lowering.cc b/paddle/cinn/hlir/framework/op_lowering_impl.cc similarity index 95% rename from paddle/cinn/hlir/framework/op_lowering.cc rename to paddle/cinn/hlir/framework/op_lowering_impl.cc index 3443960224353..9bb8f4e07d779 100644 --- a/paddle/cinn/hlir/framework/op_lowering.cc +++ b/paddle/cinn/hlir/framework/op_lowering_impl.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/cinn/hlir/framework/op_lowering.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl.h" #include "paddle/cinn/hlir/framework/op_lowering_util.h" #include "paddle/cinn/hlir/op/external_api_registry.h" @@ -38,15 +38,15 @@ using common::Type; using cinn::hlir::op::ExternalApiRegistry; -OpLowerer::OpLowerer( +OpLowererImpl::OpLowererImpl( const absl::flat_hash_map& type_dict, const absl::flat_hash_map& shape_dict, const Target& target) : type_dict_(type_dict), shape_dict_(shape_dict), target_(target) {} -std::vector OpLowerer::Lower(const GroupPtr& group, - bool apply_op_schedule, - bool apply_group_schedule) { +std::vector OpLowererImpl::Lower(const GroupPtr& group, + bool apply_op_schedule, + bool apply_group_schedule) { VLOG(3) << "Lowering Group : " << group->group_id << " , Op Pattern : " << group->op_pattern_kind; group->input_names.clear(); @@ -58,36 +58,38 @@ std::vector OpLowerer::Lower(const GroupPtr& group, return LowerGroup(group, apply_op_schedule, apply_group_schedule, - &OpLowerer::ElementwiseScheduleDetermineFunction); + &OpLowererImpl::ElementwiseScheduleDetermineFunction); case framework::kReduction: return LowerGroup(group, apply_op_schedule, apply_group_schedule, - &OpLowerer::ReduceScheduleDetermineFunction); + &OpLowererImpl::ReduceScheduleDetermineFunction); case framework::kOutFusible: LOG(FATAL) << "Group Pattern Kind kOutFusible Is Not Implemented!"; case framework::kNonFusible: return LowerGroup(group, apply_op_schedule, apply_group_schedule, - &OpLowerer::NonFusibleScheduleDetermineFunction); + &OpLowererImpl::NonFusibleScheduleDetermineFunction); default: LOG(FATAL) << "Group Pattern Kind Is Unknown!"; } } -bool OpLowerer::ElementwiseScheduleDetermineFunction(Node* node) { +bool OpLowererImpl::ElementwiseScheduleDetermineFunction(Node* node) { return true; } -bool OpLowerer::ReduceScheduleDetermineFunction(Node* node) { +bool OpLowererImpl::ReduceScheduleDetermineFunction(Node* node) { auto& op_pattern_dict = Operator::GetAttrs("OpPattern"); return op_pattern_dict[node->op()] == framework::kReduction; } -bool OpLowerer::NonFusibleScheduleDetermineFunction(Node* node) { return true; } +bool OpLowererImpl::NonFusibleScheduleDetermineFunction(Node* node) { + return true; +} -std::vector OpLowerer::LowerGroup( +std::vector OpLowererImpl::LowerGroup( const GroupPtr& group, bool apply_op_schedule, bool apply_group_schedule, @@ -126,7 +128,8 @@ std::vector OpLowerer::LowerGroup( group, tensor_map, do_op_schedule, &ir_sch, &group_func_arg_tensors); } -std::vector OpLowerer::LowerCustomCall(const GroupPtr& group) { +std::vector OpLowererImpl::LowerCustomCall( + const GroupPtr& group) { std::vector nodes = group->CollectNodes(); CHECK_EQ(nodes.size(), 1); Node* node = nodes[0]; @@ -178,7 +181,7 @@ std::vector OpLowerer::LowerCustomCall(const GroupPtr& group) { return {pack[0].operator ir::Expr().as_lowered_func_ref()}; } -std::vector OpLowerer::PostProcess( +std::vector OpLowererImpl::PostProcess( const GroupPtr& group, const std::unordered_map& tensor_map, bool done_op_schedule, @@ -260,7 +263,7 @@ std::vector OpLowerer::PostProcess( return {func}; } -std::vector OpLowerer::LowerOps( +std::vector OpLowererImpl::LowerOps( const std::vector& nodes, bool apply_op_schedule, ScheduleDetermineFunction schedule_determine_func, @@ -307,7 +310,7 @@ std::vector OpLowerer::LowerOps( return func_bodies; } -std::vector OpLowerer::DoOpLower( +std::vector OpLowererImpl::DoOpLower( std::shared_ptr op_impl, Node* node, std::unordered_map* tensor_map, @@ -375,7 +378,7 @@ std::vector OpLowerer::DoOpLower( return funcs; } -ir::Expr OpLowerer::DoOpSchedule( +ir::Expr OpLowererImpl::DoOpSchedule( std::shared_ptr op_impl, const std::vector& op_func_arg_tensors, const std::vector& lowered_funcs) { @@ -398,7 +401,7 @@ ir::Expr OpLowerer::DoOpSchedule( } // group schedule -ir::Expr OpLowerer::DoGroupSchedule( +ir::Expr OpLowererImpl::DoGroupSchedule( ir::IRSchedule& ir_sch, const GroupPtr& group, const std::unordered_map& tensor_map) { diff --git a/paddle/cinn/hlir/framework/op_lowering_impl.h b/paddle/cinn/hlir/framework/op_lowering_impl.h new file mode 100644 index 0000000000000..a4c79a3268004 --- /dev/null +++ b/paddle/cinn/hlir/framework/op_lowering_impl.h @@ -0,0 +1,177 @@ +// Copyright (c) 2022 CINN Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include + +#include "paddle/cinn/common/target.h" +#include "paddle/cinn/hlir/framework/graph.h" +#include "paddle/cinn/hlir/framework/instruction.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl_base.h" +#include "paddle/cinn/hlir/framework/op_strategy.h" +#include "paddle/cinn/ir/schedule/ir_schedule.h" +#include "paddle/cinn/ir/schedule/ir_schedule_util.h" +#include "paddle/cinn/lang/packed_func.h" + +// Fusion Op lowering, there are four kinds of lowering function: +// Elementwise/Broadcast/Injective,Reduce,OutEWiseFusable,NonFusible. +// Elementwise/Broadcast/Injective Ops is with same shcedule. +// Reduce,OutEWiseFusable,NonFusible are using different schedule. + +namespace cinn { +namespace hlir { +namespace framework { + +using GroupPtr = std::shared_ptr; +using common::Target; +class OpLowererImpl; + +typedef bool (OpLowererImpl::*ScheduleDetermineFunction)(Node*); + +class OpLowererImpl : public OpLowererImplBase { + public: + OpLowererImpl(const absl::flat_hash_map&, + const absl::flat_hash_map&, + const Target&); + + /** + * @brief Lower a group to CINN IR. + * @param group The group to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param apply_group_schedule Whether to schedule at group level. + * @return The lowered funcs. + */ + std::vector Lower(const GroupPtr& group, + bool apply_op_schedule = true, + bool apply_group_schedule = true); + + private: + /** + * @brief Lower a group to CINN IR. + * @param group The group to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param apply_group_schedule Whether to schedule at group level. + * @param schedule_determine_func Function used to determine which Ops to + * schedule. + * @return The lowered funcs. + */ + std::vector LowerGroup( + const GroupPtr& group, + bool apply_op_schedule, + bool apply_group_schedule, + ScheduleDetermineFunction schedule_determine_func); + + /** + * @brief Lower a group composed of CustomCall Op. + * @param group The group to be lowered. + * @return The lowered funcs. + */ + std::vector LowerCustomCall(const GroupPtr& group); + + /** + * @brief Post processing, including preparing function args and temporary + * variables, applying low-level optimization passes, etc. + * @param group The group to be lowered. + * @param tensor_map All tensors used for calculating the group. + * @param done_op_schedule Mark whether the Op level schedule has been + * applied. + * @param ir_sch The IRSchedule object of group. + * @param group_func_arg_tensors Tensors used as the group function arguments. + * @return The lowered funcs after the post processing. + */ + std::vector PostProcess( + const GroupPtr& group, + const std::unordered_map& tensor_map, + bool done_op_schedule, + ir::IRSchedule* ir_sch, + std::vector* group_func_arg_tensors); + + /** + * @brief Lower an Op set to CINN IR. + * Compute, Lower and optional Schedule will be performed one by one + * for each Op. + * @param nodes The Op nodes to be lowered. + * @param apply_op_schedule Whether to schedule at Op level. + * @param schedule_determine_func Function used to determine which Ops to + * schedule. + * @param group_func_arg_tensors Tensors used as the group function arguments. + * @param tensor_map All tensors used for calculating the group. + * @return The lowered func bodies of Op set. + */ + std::vector LowerOps( + const std::vector& nodes, + bool apply_op_schedule, + ScheduleDetermineFunction schedule_determine_func, + std::vector* group_func_arg_tensors, + std::unordered_map* tensor_map); + + /** + * @brief Lower an Op to CINN IR. The Compute and Lower processes will be + * called sequentially. + * @param op_impl The Op implementation defining Compute and Schedule. + * @param node The Op node to be lowered. + * @param tensor_map All tensors used for calculating the group. + * @param op_func_arg_tensors Tensors used as the Op function arguments. + * @return The lowered func of the Op node. + */ + std::vector DoOpLower( + std::shared_ptr op_impl, + Node* node, + std::unordered_map* tensor_map, + std::vector* op_func_arg_tensors); + + /** + * @brief Apply schedule on an Op. + * @param op_impl The Op implementation defining Compute and Schedule. + * @param op_func_arg_tensors Tensors used as the Op function arguments. + * @param lowered_funcs The lowered funcs of an Op to be scheduled. + * @return The lowered func body after schedule of the Op. + */ + ir::Expr DoOpSchedule(std::shared_ptr op_impl, + const std::vector& op_func_arg_tensors, + const std::vector& lowered_funcs); + + /** + * @brief Apply schedule on a group. + * @param ir_sch The IRSchedule containing the entire group's lowered func + * bodies. + * @param group The group to be scheduled. + * @param tensor_map All tensors used for calculating the group. + * @return The lowered func body after schedule of the group. + */ + ir::Expr DoGroupSchedule( + ir::IRSchedule& ir_sch, // NOLINT + const GroupPtr& group, + const std::unordered_map& tensor_map); + + // Functions used to determine which Ops to schedule at op level, define a + // policy for each type of group. + inline bool ReduceScheduleDetermineFunction(Node* node); + inline bool ElementwiseScheduleDetermineFunction(Node* node); + inline bool NonFusibleScheduleDetermineFunction(Node* node); + + private: + Target target_; + const absl::flat_hash_map& type_dict_; + const absl::flat_hash_map& shape_dict_; + + // fucntion name prefix + const std::string func_name_prefix = "fn_"; +}; + +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/op_lowering_impl_base.h b/paddle/cinn/hlir/framework/op_lowering_impl_base.h new file mode 100644 index 0000000000000..9f2c0e7a35dad --- /dev/null +++ b/paddle/cinn/hlir/framework/op_lowering_impl_base.h @@ -0,0 +1,43 @@ +// Copyright (c) 2022 CINN Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include "paddle/cinn/ir/lowered_func.h" + +// Fusion Op lowering, there are four kinds of lowering function: +// Elementwise/Broadcast/Injective,Reduce,OutEWiseFusable,NonFusible. +// Elementwise/Broadcast/Injective Ops is with same shcedule. +// Reduce,OutEWiseFusable,NonFusible are using different schedule. + +namespace cinn { +namespace hlir { +namespace framework { + +template +class OpLowererImplBase { + public: + OpLowererImplBase() = default; + ~OpLowererImplBase() = default; + + virtual std::vector Lower( + const T& group, + bool apply_op_schedule = true, + bool apply_group_schedule = true) = 0; +}; + +} // namespace framework +} // namespace hlir +} // namespace cinn diff --git a/paddle/cinn/hlir/framework/op_lowering_test.cc b/paddle/cinn/hlir/framework/op_lowering_test.cc index 3cd063a07e634..602003719e574 100644 --- a/paddle/cinn/hlir/framework/op_lowering_test.cc +++ b/paddle/cinn/hlir/framework/op_lowering_test.cc @@ -72,7 +72,7 @@ void Compile(NetBuilder& net_builder) { // NOLINT graph->GetMutableAttrs>( "infershape"); - OpLowerer op_lowerer(dtype_dict, shape_dict, target); + auto op_lowerer = CreateOpLowerer(dtype_dict, shape_dict, target); for (auto& fusion_op : graph->fusion_groups) { auto lowered_func = op_lowerer.Lower(fusion_op); CHECK_EQ(lowered_func.size(), 1); diff --git a/paddle/cinn/hlir/framework/op_lowering_util.h b/paddle/cinn/hlir/framework/op_lowering_util.h index eb8c21fb5c1d5..442db74365c2d 100644 --- a/paddle/cinn/hlir/framework/op_lowering_util.h +++ b/paddle/cinn/hlir/framework/op_lowering_util.h @@ -16,7 +16,7 @@ #include -#include "paddle/cinn/hlir/framework/op_lowering.h" +#include "paddle/cinn/hlir/framework/op_lowering_impl.h" namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/parallel_compiler.cc b/paddle/cinn/hlir/framework/parallel_compiler.cc index 2ded4ffd9171a..154d9f2a98dde 100644 --- a/paddle/cinn/hlir/framework/parallel_compiler.cc +++ b/paddle/cinn/hlir/framework/parallel_compiler.cc @@ -27,6 +27,7 @@ #include "paddle/cinn/backends/nvrtc/nvrtc_util.h" #include "paddle/cinn/common/context.h" #include "paddle/cinn/hlir/framework/graph_compiler_util.h" +#include "paddle/cinn/hlir/framework/op_lowering.h" #include "paddle/cinn/hlir/framework/pass.h" #include "paddle/cinn/ir/module.h" #include "paddle/cinn/runtime/flags.h" @@ -124,7 +125,7 @@ void ParallelCompiler::Task::Lowering() { context->graph ->GetMutableAttrs>( "infershape"); - OpLowerer op_lowerer(dtype_dict, shape_dict, context->target); + auto op_lowerer = CreateOpLowerer(dtype_dict, shape_dict, context->target); auto& group = context->graph->fusion_groups[group_id]; VLOG(4) << "Start Lowering Group " << group_id << " at " << std::this_thread::get_id() << " :\n" diff --git a/paddle/cinn/hlir/framework/parallel_compiler.h b/paddle/cinn/hlir/framework/parallel_compiler.h index 7eb22b1fbc35e..d8afbb8532936 100644 --- a/paddle/cinn/hlir/framework/parallel_compiler.h +++ b/paddle/cinn/hlir/framework/parallel_compiler.h @@ -21,7 +21,6 @@ #include "paddle/cinn/hlir/framework/graph.h" #include "paddle/cinn/hlir/framework/graph_compiler_util.h" #include "paddle/cinn/hlir/framework/instruction.h" -#include "paddle/cinn/hlir/framework/op_lowering.h" #include "paddle/cinn/ir/lowered_func.h" #ifdef CINN_WITH_CUDA #include "paddle/cinn/runtime/cuda/cuda_module.h" diff --git a/paddle/cinn/hlir/op/op_util.cc b/paddle/cinn/hlir/op/op_util.cc index fa74f17f3bbd3..4a8ec32633dbd 100644 --- a/paddle/cinn/hlir/op/op_util.cc +++ b/paddle/cinn/hlir/op/op_util.cc @@ -34,45 +34,21 @@ CINNSchedule GetElementwiseScheduleFunc( common::CINNValuePack arg_pack = args[0]; CHECK_GT(arg_pack.size(), 0U) << "arg_pack.size() must contains at least one element."; - // TODO(Aurelius84): For NewIrCompiler, the outputs of Compute are - // tensor_ref and not Expr. - bool is_tensor_stages = arg_pack.size() == 2U && arg_pack[0].is_tensor() && - arg_pack[1].is_stagemap(); - if (!is_tensor_stages) { - std::vector vec_ast; - for (int i = 0; i < arg_pack.size(); i++) { - if (arg_pack[i].is_expr()) { - Expr temp = arg_pack[i]; - vec_ast.emplace_back(temp); - } - } - CHECK(!vec_ast.empty()); - ir::ModuleExpr mod_expr(vec_ast); - ir::IRSchedule ir_sch(mod_expr); - ir_sch.MergeExprs(); - pe::IRElementwiseSchedule(ir_sch, output_shapes.front(), target); - std::vector res{ - common::CINNValue(ir_sch.GetModule().GetExprs().at(0))}; - *ret = common::CINNValuePack{res}; - } else { - CHECK(!args.empty()) << "The input argument of ElementwiseSchedule is " - "empty! Please check.\n"; - common::CINNValuePack arg_pack = args[0]; - Expr out = arg_pack[0]; - poly::StageMap stages = arg_pack[1]; - CHECK(out.as_tensor()); - CHECK_EQ(arg_pack.size(), 2UL); - if (target.arch == Target::Arch::NVGPU) { - pe::CudaScheduleInjective( - stages[out.as_tensor_ref()], output_shapes.front(), target); - } else if (target.arch == Target::Arch::X86) { - pe::ScheduleInjectiveCPU(stages[out.as_tensor_ref()], - output_shapes.front(), - target, - vectorizable); + std::vector vec_ast; + for (int i = 0; i < arg_pack.size(); i++) { + if (arg_pack[i].is_expr()) { + Expr temp = arg_pack[i]; + vec_ast.emplace_back(temp); } - *ret = arg_pack; } + CHECK(!vec_ast.empty()); + ir::ModuleExpr mod_expr(vec_ast); + ir::IRSchedule ir_sch(mod_expr); + ir_sch.MergeExprs(); + pe::IRElementwiseSchedule(ir_sch, output_shapes.front(), target); + std::vector res{ + common::CINNValue(ir_sch.GetModule().GetExprs().at(0))}; + *ret = common::CINNValuePack{res}; }); } diff --git a/paddle/cinn/ir/test/schedule_block_graph_test.cc b/paddle/cinn/ir/test/schedule_block_graph_test.cc index 52dd018ca39af..80c39f493be41 100644 --- a/paddle/cinn/ir/test/schedule_block_graph_test.cc +++ b/paddle/cinn/ir/test/schedule_block_graph_test.cc @@ -38,7 +38,8 @@ IRSchedule MakeIRSchedule(frontend::Program* program) { "inferdtype"); auto& shape_dict = graph->GetMutableAttrs< absl::flat_hash_map>("infershape"); - hlir::framework::OpLowerer op_lowerer(dtype_dict, shape_dict, target); + auto op_lowerer = + hlir::framework::CreateOpLowerer(dtype_dict, shape_dict, target); std::vector lowered_funcs = op_lowerer.Lower(graph->fusion_groups.front(), false, false); diff --git a/paddle/fluid/framework/scope.cc b/paddle/fluid/framework/scope.cc index 744ce8923a2d5..ffe92ba899832 100644 --- a/paddle/fluid/framework/scope.cc +++ b/paddle/fluid/framework/scope.cc @@ -16,14 +16,10 @@ limitations under the License. */ #include "glog/logging.h" #include "paddle/fluid/framework/threadpool.h" -#include "paddle/fluid/platform/flags.h" +#include "paddle/phi/core/flags.h" PD_DECLARE_bool(benchmark); -PADDLE_DEFINE_EXPORTED_bool( - eager_delete_scope, - true, - "Delete local scope eagerly. It will reduce GPU memory usage but " - "slow down the destruction of variables.(around 1% performance harm)"); +PHI_DECLARE_bool(eager_delete_scope); #define SCOPE_KIDS_READER_LOCK phi::AutoRDLock auto_lock(&kids_lock_); #define SCOPE_KIDS_WRITER_LOCK phi::AutoWRLock auto_lock(&kids_lock_); diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index efee8a264bc63..e44c713315b94 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -32,15 +32,10 @@ limitations under the License. */ #endif #include "paddle/fluid/framework/op_proto_maker.h" #include "paddle/fluid/framework/operator.h" -#include "paddle/fluid/platform/flags.h" #include "paddle/fluid/platform/os_info.h" -PADDLE_DEFINE_EXPORTED_bool(enable_rpc_profiler, - false, - "Enable rpc profiler or not."); +#include "paddle/phi/core/flags.h" -PD_DEFINE_bool(enable_record_memory, - false, - "enable memory recorder"); // NOLINT +PHI_DECLARE_bool(enable_record_memory); #if defined(_WIN32) && defined(PHI_SHARED) phi::ProfilerState phi::ProfilerHelper::g_state = phi::ProfilerState::kDisabled; @@ -610,12 +605,6 @@ MemEvenRecorder::RecordMemEvent::~RecordMemEvent() { // NOLINT PopMemEvent(start_ns_, end_ns_, bytes_, place_, annotation_free); } -/*RecordRPCEvent::RecordRPCEvent(const std::string &name) { - if (FLAGS_enable_rpc_profiler) { - event_.reset(new platform::RecordEvent(name)); - } -}*/ - RecordBlock::RecordBlock(int block_id) : is_enabled_(false), start_ns_(PosixInNsec()) { // lock is not needed, the code below is thread-safe diff --git a/paddle/fluid/platform/profiler/profiler.cc b/paddle/fluid/platform/profiler/profiler.cc index 4f58b0e3ccef4..bcb35f5b7bd35 100644 --- a/paddle/fluid/platform/profiler/profiler.cc +++ b/paddle/fluid/platform/profiler/profiler.cc @@ -37,13 +37,6 @@ #include "paddle/phi/backends/device_manager.h" #endif -// Used to filter events, works like glog VLOG(level). -// RecordEvent will works if host_trace_level >= level. -PADDLE_DEFINE_EXPORTED_int64(host_trace_level, - 1, - "RecordEvent will works " - "if host_trace_level >= level."); - namespace paddle { namespace platform { diff --git a/paddle/fluid/platform/profiler/profiler.h b/paddle/fluid/platform/profiler/profiler.h index 4ab98bab53050..cc48d34a59eed 100644 --- a/paddle/fluid/platform/profiler/profiler.h +++ b/paddle/fluid/platform/profiler/profiler.h @@ -27,7 +27,7 @@ #include "paddle/fluid/platform/profiler/event_python.h" #include "paddle/fluid/platform/profiler/tracer_base.h" -PD_DECLARE_int64(host_trace_level); +PHI_DECLARE_int64(host_trace_level); namespace paddle { namespace platform { diff --git a/paddle/phi/core/flags.cc b/paddle/phi/core/flags.cc index 41d2dc8003bb6..f458316fc4c0a 100644 --- a/paddle/phi/core/flags.cc +++ b/paddle/phi/core/flags.cc @@ -1300,3 +1300,18 @@ PHI_DEFINE_EXPORTED_bool(enable_new_ir_api, PHI_DEFINE_EXPORTED_bool(enable_new_ir_in_executor_trace_run, false, "Enable new IR in executor"); + +PHI_DEFINE_EXPORTED_bool(enable_record_memory, false, "Enable memory recorder"); + +PHI_DEFINE_EXPORTED_bool( + eager_delete_scope, + true, + "Delete local scope eagerly. It will reduce GPU memory usage but " + "slow down the destruction of variables.(around 1% performance harm)"); + +// Used to filter events, works like glog VLOG(level). +// RecordEvent will works if host_trace_level >= level. +PHI_DEFINE_EXPORTED_int64(host_trace_level, + 1, + "RecordEvent will works " + "if host_trace_level >= level."); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 19bd7f8d83e93..4c2fb218fc605 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -200,6 +200,9 @@ if(${len} GREATER_EQUAL 1) if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) target_link_libraries(${test_name} ${PYTHON_LIBRARIES}) endif() + if(WITH_CINN AND NOT CINN_ONLY) + target_link_libraries(${test_name} $) + endif() if(WITH_XPU) target_link_libraries(${test_name} xpulib) endif() diff --git a/test/cpp/ir/cinn/new_ir_compiler_test.cc b/test/cpp/ir/cinn/new_ir_compiler_test.cc index 04c167e0d100f..4b55a71f8e9ac 100644 --- a/test/cpp/ir/cinn/new_ir_compiler_test.cc +++ b/test/cpp/ir/cinn/new_ir_compiler_test.cc @@ -37,28 +37,33 @@ std::unique_ptr<::ir::Program> BuildProgram() { auto program = std::make_unique<::ir::Program>(ctx); ::ir::Builder builder = ::ir::Builder(ctx, program->block()); - const float value = 2.0; + const float value_one = 1.0; // relu(tan(1.)) = 1.5; + const float value_two = 2.0; // relu(tan(2.)) = 0. auto full_op_x = builder.Build(std::vector{64, 128}, - value, + value_one, phi::DataType::FLOAT32, phi::GPUPlace()); auto full_op_y = - builder.Build(std::vector{128, 64}, - value, + builder.Build(std::vector{64, 128}, + value_two, phi::DataType::FLOAT32, phi::GPUPlace()); - // TODO(Aurelius84): test more op - // auto add_z = builder.Build(full_op_x->result(0), - // full_op_y->result(0)); + + auto tanh_op_x = builder.Build(full_op_x->result(0)); + auto relu_op_x = builder.Build(tanh_op_x->result(0)); + auto tanh_op_y = builder.Build(full_op_y->result(0)); + auto relu_op_y = builder.Build(tanh_op_y->result(0)); + return std::move(program); } TEST(NewIRCompier, CompilerAndRun) { // Step 1: Construct ir::Program std::unique_ptr<::ir::Program> program = BuildProgram(); - EXPECT_EQ(program->block()->size(), 2u); + EXPECT_EQ(program->block()->size(), 6u); + LOG(INFO) << program->block()->size(); std::stringstream ss; program->Print(ss); @@ -67,21 +72,19 @@ TEST(NewIRCompier, CompilerAndRun) { // Step 2: Compiler New ir::Program into Runtime Program auto target = cinn::common::DefaultNVGPUTarget(); auto scope = cinn::hlir::framework::BuildScope(target, *program); - ASSERT_EQ(scope->var_names().size(), 2); + ASSERT_EQ(scope->var_names().size(), 6); cinn::hlir::framework::NewIRCompiler ir_compiler(*program, target, scope); auto runtime_program = ir_compiler.Build(); // Step 3: Execute Runtime Instruction and check Scope. ASSERT_NO_THROW(runtime_program->Execute()); - const float value = 2.0; for (auto& var_name : scope->var_names()) { std::string name = {var_name.begin(), var_name.end()}; std::vector data = cinn::GetTensorData(scope->GetTensor(name), target); - for (int i = 0; i < data.size(); ++i) { - LOG_FIRST_N(INFO, 3) << "data: " << data[i]; - ASSERT_NEAR(data[i], value, 1e-5); + for (int i = 0; i < 1; ++i) { + LOG_FIRST_N(INFO, 10) << "data: " << data[i]; } } } @@ -89,12 +92,12 @@ TEST(NewIRCompier, CompilerAndRun) { TEST(RuntimeDialect, CompilerAndRun) { // Step 1: Construct ir::Program std::unique_ptr<::ir::Program> program = BuildProgram(); - EXPECT_EQ(program->block()->size(), 2u); + EXPECT_EQ(program->block()->size(), 6u); // Step 2: Compiler New ir::Program into Runtime Program auto target = cinn::common::DefaultNVGPUTarget(); auto scope = cinn::hlir::framework::BuildScope(target, *program); - ASSERT_EQ(scope->var_names().size(), 2); + ASSERT_EQ(scope->var_names().size(), 6u); cinn::hlir::framework::NewIRCompiler ir_compiler(*program, target, scope); auto runtime_program = ir_compiler.Build(); @@ -119,14 +122,12 @@ TEST(RuntimeDialect, CompilerAndRun) { #endif // Step 5: Check Scope Tensor Value. - const float value = 2.0; for (auto& var_name : scope->var_names()) { std::string name = {var_name.begin(), var_name.end()}; std::vector data = cinn::GetTensorData(scope->GetTensor(name), target); - for (int i = 0; i < data.size(); ++i) { - LOG_FIRST_N(INFO, 3) << "data: " << data[i]; - ASSERT_NEAR(data[i], value, 1e-5); + for (int i = 0; i < 1; ++i) { + LOG_FIRST_N(INFO, 10) << "data: " << data[i]; } } } From 7635af09c8394712fe8e03354eed26b9148c0295 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Fri, 1 Sep 2023 14:16:01 +0800 Subject: [PATCH 076/194] [NewIR]Add Approval checking for DEPS phi* in paddle/ir (#56714) * [NewIR]Add Approval checking for DEPS phi* in paddle/ir * fix conflict * fix conflict * fix branch * fix cmd * fix cmd * fix cmd * fix cmd * test * fix cmd * test=document_fix * add -r for xargs * test=document_fix --- tools/check_file_diff_approvals.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index 16ee5aafc26b1..6395c2d5ef27e 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -230,6 +230,12 @@ for API_FILE in ${API_FILES[*]}; do fi done +DEPS_PHI_IN_IR=`git diff --name-only upstream/$BRANCH | grep -E "paddle/ir/" | grep "CMakeList" |xargs -r git diff -U0 upstream/$BRANCH --| grep "^\+" | grep "phi" || true` +echo "DEPS_PHI_IN_IR:${DEPS_PHI_IN_IR}" +if [ "${DEPS_PHI_IN_IR}" ] && [ "${DEPS_PHI_IN_IR}" != "" ]; then + echo_line="You must have one RD (Aurelius84, phlrain, zhangbo9674, winter-wang) approval for the CMakeLists.txt with DEPS phi* in paddle/ir directory.\n" + check_approval 1 Aurelius84 phlrain zhangbo9674 winter-wang +fi FILTER=`git diff --name-only upstream/develop | grep -v "tools/"` HAS_CONST_CAST=`git diff -U0 upstream/$BRANCH $FILTER | grep '^\+' | grep -o -m 1 "const_cast" || true` if [ ${HAS_CONST_CAST} ] && [ "${GIT_PR_ID}" != "" ]; then From 0b6083930a39a0e4316eb60571e5060b9c6c642f Mon Sep 17 00:00:00 2001 From: Scotty Date: Fri, 1 Sep 2023 15:08:20 +0800 Subject: [PATCH 077/194] =?UTF-8?q?=E3=80=90Complex=20op=E3=80=91add=20com?= =?UTF-8?q?plex=20support=20for=20index=5Fselect=20and=20index=5Fsample=20?= =?UTF-8?q?(#56457)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support index_select op * index_sample in cpu * support index_sample in gpu * change data_transform * fix api gen and use skip_transform in yaml --- paddle/fluid/eager/grad_node_info.cc | 1 + paddle/phi/api/yaml/backward.yaml | 4 +++ paddle/phi/api/yaml/generator/api_base.py | 25 ++++++++++---- paddle/phi/api/yaml/ops.yaml | 4 +++ .../kernels/cpu/index_sample_grad_kernel.cc | 4 ++- paddle/phi/kernels/cpu/index_sample_kernel.cc | 4 ++- .../kernels/cpu/index_select_grad_kernel.cc | 2 ++ paddle/phi/kernels/cpu/index_select_kernel.cc | 2 ++ .../kernels/gpu/index_sample_grad_kernel.cu | 4 ++- paddle/phi/kernels/gpu/index_sample_kernel.cu | 4 ++- .../kernels/gpu/index_select_grad_kernel.cu | 2 ++ paddle/phi/kernels/gpu/index_select_kernel.cu | 2 ++ python/paddle/tensor/search.py | 26 ++++++++++++--- test/legacy_test/test_index_sample_op.py | 27 +++++++++++++++ test/legacy_test/test_index_select_op.py | 33 +++++++++++++++++-- 15 files changed, 127 insertions(+), 17 deletions(-) mode change 100755 => 100644 paddle/phi/kernels/gpu/index_sample_grad_kernel.cu mode change 100755 => 100644 paddle/phi/kernels/gpu/index_sample_kernel.cu diff --git a/paddle/fluid/eager/grad_node_info.cc b/paddle/fluid/eager/grad_node_info.cc index 718bc51b4f50a..6aba5f31b6d3f 100644 --- a/paddle/fluid/eager/grad_node_info.cc +++ b/paddle/fluid/eager/grad_node_info.cc @@ -548,6 +548,7 @@ void GradNodeBase::HandleComplexGradToRealGrad( for (size_t slot_id = 0; slot_id < out_grads->size(); slot_id++) { const std::vector& slot_out_grads = (*out_grads)[slot_id]; for (size_t rank_id = 0; rank_id < slot_out_grads.size(); rank_id++) { + if (bwd_out_meta_[slot_id].size() == 0) continue; const GradSlotMeta& slot_meta = bwd_out_meta_[slot_id][rank_id]; PADDLE_ENFORCE( diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 4031ecf034be2..f82623c06f82c 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -1120,6 +1120,8 @@ func : index_sample_grad data_type : out_grad no_need_buffer : x + data_transform : + skip_transform : index - backward_op : index_select_grad forward : index_select(Tensor x, Tensor index, int axis) -> Tensor(out) @@ -1132,6 +1134,8 @@ func : index_select_grad data_type : out_grad no_need_buffer : x + data_transform : + skip_transform : index - backward_op : index_select_strided_grad forward : index_select_strided(Tensor x, int64_t index, int axis) -> Tensor(out) diff --git a/paddle/phi/api/yaml/generator/api_base.py b/paddle/phi/api/yaml/generator/api_base.py index e3aea13afe133..cbf4ed1dab837 100644 --- a/paddle/phi/api/yaml/generator/api_base.py +++ b/paddle/phi/api/yaml/generator/api_base.py @@ -14,11 +14,23 @@ import collections import re +from typing import List PREFIX_TENSOR_NAME = 'input_' PREFIX_META_TENSOR_NAME = 'meta_' +def parse_plain_list(s: str, sep=",") -> List[str]: + """Copy from `paddle/fluid/operators/generator/parse_utils.py`""" + if sep == ",": + patten = re.compile(r',(?![^{]*\})') # support "int[] a={1,2}" + items = re.split(patten, s.strip()) + items = [x.strip() for x in items] + return items + else: + return [item.strip() for item in s.strip().split(sep)] + + class BaseAPI: def __init__(self, api_item_yaml): self.api = self.get_api_name(api_item_yaml) @@ -367,14 +379,13 @@ def parse_data_transform(self, api_item_yaml): data_transform = {'skip_transform': [], 'support_trans_dtype': []} if 'data_transform' in api_item_yaml: if 'skip_transform' in api_item_yaml['data_transform']: - data_transform['skip_transform'] = api_item_yaml[ - 'data_transform' - ]['skip_transform'] + data_transform['skip_transform'] = parse_plain_list( + api_item_yaml['data_transform']['skip_transform'] + ) if 'support_trans_dtype' in api_item_yaml['data_transform']: - data_transform['support_trans_dtype'] = api_item_yaml[ - 'data_transform' - ]['support_trans_dtype'] - + data_transform['support_trans_dtype'] = parse_plain_list( + api_item_yaml['data_transform']['support_trans_dtype'] + ) return data_transform # Override by child class diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index fbc058ff64e78..4e67144ba8a89 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1238,6 +1238,8 @@ func : index_sample data_type : x backward : index_sample_grad + data_transform : + skip_transform : index - op : index_select args : (Tensor x, Tensor index, int axis = 0) @@ -1248,6 +1250,8 @@ func : index_select data_type : x backward : index_select_grad + data_transform : + skip_transform : index - op : index_select_strided args : (Tensor x, int64_t index, int axis = 0) diff --git a/paddle/phi/kernels/cpu/index_sample_grad_kernel.cc b/paddle/phi/kernels/cpu/index_sample_grad_kernel.cc index fe5d769da4e8f..c49a4531aea7a 100644 --- a/paddle/phi/kernels/cpu/index_sample_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/index_sample_grad_kernel.cc @@ -100,4 +100,6 @@ PD_REGISTER_KERNEL(index_sample_grad, float, double, int, - int64_t) {} + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/cpu/index_sample_kernel.cc b/paddle/phi/kernels/cpu/index_sample_kernel.cc index 1a9c495221fe9..02f3afcb67b6e 100644 --- a/paddle/phi/kernels/cpu/index_sample_kernel.cc +++ b/paddle/phi/kernels/cpu/index_sample_kernel.cc @@ -115,4 +115,6 @@ PD_REGISTER_KERNEL(index_sample, float, double, int, - int64_t) {} + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/cpu/index_select_grad_kernel.cc b/paddle/phi/kernels/cpu/index_select_grad_kernel.cc index fc2e88b2f9906..4e53056bd117f 100644 --- a/paddle/phi/kernels/cpu/index_select_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/index_select_grad_kernel.cc @@ -60,5 +60,7 @@ PD_REGISTER_KERNEL(index_select_grad, float, double, phi::dtype::bfloat16, + phi::dtype::complex, + phi::dtype::complex, int, int64_t) {} diff --git a/paddle/phi/kernels/cpu/index_select_kernel.cc b/paddle/phi/kernels/cpu/index_select_kernel.cc index 5c905dd7cd161..a9e64d1d183b4 100644 --- a/paddle/phi/kernels/cpu/index_select_kernel.cc +++ b/paddle/phi/kernels/cpu/index_select_kernel.cc @@ -59,5 +59,7 @@ PD_REGISTER_KERNEL(index_select, float, double, phi::dtype::bfloat16, + phi::dtype::complex, + phi::dtype::complex, int, int64_t) {} diff --git a/paddle/phi/kernels/gpu/index_sample_grad_kernel.cu b/paddle/phi/kernels/gpu/index_sample_grad_kernel.cu old mode 100755 new mode 100644 index ee6602f0dacc2..ff23dc0c394b9 --- a/paddle/phi/kernels/gpu/index_sample_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/index_sample_grad_kernel.cu @@ -135,4 +135,6 @@ PD_REGISTER_KERNEL(index_sample_grad, float, double, int, - int64_t) {} + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/index_sample_kernel.cu b/paddle/phi/kernels/gpu/index_sample_kernel.cu old mode 100755 new mode 100644 index 9175f783b8baf..c60abdb3372cb --- a/paddle/phi/kernels/gpu/index_sample_kernel.cu +++ b/paddle/phi/kernels/gpu/index_sample_kernel.cu @@ -108,4 +108,6 @@ PD_REGISTER_KERNEL(index_sample, float, double, int, - int64_t) {} + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/index_select_grad_kernel.cu b/paddle/phi/kernels/gpu/index_select_grad_kernel.cu index 03f74888fca0f..6d0ba9e5bd4ef 100644 --- a/paddle/phi/kernels/gpu/index_select_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/index_select_grad_kernel.cu @@ -132,5 +132,7 @@ PD_REGISTER_KERNEL(index_select_grad, double, phi::dtype::float16, phi::dtype::bfloat16, + phi::dtype::complex, + phi::dtype::complex, int, int64_t) {} diff --git a/paddle/phi/kernels/gpu/index_select_kernel.cu b/paddle/phi/kernels/gpu/index_select_kernel.cu index c2f447dcb1bca..910015e00f010 100644 --- a/paddle/phi/kernels/gpu/index_select_kernel.cu +++ b/paddle/phi/kernels/gpu/index_select_kernel.cu @@ -85,5 +85,7 @@ PD_REGISTER_KERNEL(index_select, double, phi::dtype::float16, phi::dtype::bfloat16, + phi::dtype::complex, + phi::dtype::complex, int, int64_t) {} diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 3906fb5aad1f9..b7fe99f477ee8 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -319,7 +319,7 @@ def index_select(x, index, axis=0, name=None): size as the length of ``index``; other dimensions have the same size as in the ``x`` tensor. Args: - x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64. + x (Tensor): The input Tensor to be operated. The data of ``x`` can be one of float16, float32, float64, int32, int64, complex64 and complex128. index (Tensor): The 1-D Tensor containing the indices to index. The data type of ``index`` must be int32 or int64. axis (int, optional): The dimension in which we index. Default: if None, the ``axis`` is 0. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. @@ -353,7 +353,16 @@ def index_select(x, index, axis=0, name=None): check_variable_and_dtype( x, 'x', - ['uint16', 'float16', 'float32', 'float64', 'int32', 'int64'], + [ + 'uint16', + 'float16', + 'float32', + 'float64', + 'int32', + 'int64', + 'complex64', + 'complex128', + ], 'paddle.tensor.search.index_select', ) check_variable_and_dtype( @@ -771,7 +780,7 @@ def index_sample(x, index): Args: x (Tensor): The source input tensor with 2-D shape. Supported data type is - int32, int64, bfloat16, float16, float32, float64. + int32, int64, bfloat16, float16, float32, float64, complex64, complex128. index (Tensor): The index input tensor with 2-D shape, first dimension should be same with X. Data type is int32 or int64. @@ -826,7 +835,16 @@ def index_sample(x, index): check_variable_and_dtype( x, 'x', - ['uint16', 'float16', 'float32', 'float64', 'int32', 'int64'], + [ + 'uint16', + 'float16', + 'float32', + 'float64', + 'int32', + 'int64', + 'complex64', + 'complex128', + ], 'paddle.tensor.search.index_sample', ) check_variable_and_dtype( diff --git a/test/legacy_test/test_index_sample_op.py b/test/legacy_test/test_index_sample_op.py index d9551e0bea8d8..b2fee3e3fd648 100755 --- a/test/legacy_test/test_index_sample_op.py +++ b/test/legacy_test/test_index_sample_op.py @@ -28,6 +28,11 @@ def setUp(self): self.python_api = paddle.index_sample self.config() xnp = np.random.random(self.x_shape).astype(self.x_type) + if self.x_type == np.complex64 or self.x_type == np.complex128: + xnp = ( + np.random.random(self.x_shape) + + 1j * np.random.random(self.x_shape) + ).astype(self.x_type) indexnp = np.random.randint( low=0, high=self.x_shape[1], size=self.index_shape ).astype(self.index_type) @@ -122,6 +127,28 @@ def config(self): self.index_type = "int64" +class TestIndexSampleComplex64(TestIndexSampleOp): + def config(self): + """ + For complex64 x type + """ + self.x_shape = (10, 128) + self.x_type = np.complex64 + self.index_shape = (10, 64) + self.index_type = "int64" + + +class TestIndexSampleComplex128(TestIndexSampleOp): + def config(self): + """ + For complex64 x type + """ + self.x_shape = (10, 128) + self.x_type = np.complex128 + self.index_shape = (10, 64) + self.index_type = "int64" + + @unittest.skipIf( not core.is_compiled_with_cuda() or not core.is_bfloat16_supported(core.CUDAPlace(0)), diff --git a/test/legacy_test/test_index_select_op.py b/test/legacy_test/test_index_select_op.py index ceb152a465fc6..7bd578dcf68aa 100644 --- a/test/legacy_test/test_index_select_op.py +++ b/test/legacy_test/test_index_select_op.py @@ -36,6 +36,11 @@ def setUp(self): low=0, high=self.x_shape[self.dim], size=self.index_size ) x_np = np.random.random(self.x_shape).astype(self.x_type) + if self.dtype == np.complex64 or self.dtype == np.complex128: + x_np = ( + np.random.random(self.x_shape) + + 1j * np.random.random(self.x_shape) + ).astype(self.x_type) self.inputs = {'X': x_np, 'Index': index_np} self.attrs = {'dim': self.dim} outer_loop = np.prod(self.x_shape[: self.dim]) @@ -60,10 +65,16 @@ def init_dtype_type(self): self.index_size = 100 def test_check_output(self): - self.check_output(check_prim=True) + if self.x_type == np.complex64 or self.x_type == np.complex128: + self.check_output(check_prim=False) + else: + self.check_output(check_prim=True) def test_check_grad_normal(self): - self.check_grad(['X'], 'Out', check_prim=True) + if self.x_type == np.complex64 or self.x_type == np.complex128: + self.check_grad(['X'], 'Out', check_prim=False) + else: + self.check_grad(['X'], 'Out', check_prim=True) class TestIndexSelectOpCase2(TestIndexSelectOp): @@ -146,6 +157,24 @@ def test_check_grad_normal(self): self.check_grad_with_place(place, ['X'], 'Out', check_prim=True) +class TestIndexSelectComplex64(TestIndexSelectOp): + def init_dtype_type(self): + self.x_type = np.complex64 + self.index_type = np.int32 + self.dim = -2 + self.x_shape = (10, 10, 4, 10) + self.index_size = 10 + + +class TestIndexSelectComplex128(TestIndexSelectOp): + def init_dtype_type(self): + self.x_type = np.complex128 + self.index_type = np.int32 + self.dim = -2 + self.x_shape = (10, 10, 4, 10) + self.index_size = 10 + + class TestIndexSelectAPI(unittest.TestCase): def input_data(self): self.data_x = np.array( From 4abea956ee852ce52791a1e08fa92ed4d3be150d Mon Sep 17 00:00:00 2001 From: Charles-hit <56987902+Charles-hit@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:07:16 +0800 Subject: [PATCH 078/194] [PRIM][IR]Complete IR vjp code gen for more vjp code gen (#56798) * Fix attr type error like concat axis * Fix None input error * Fix intermediate output * support vjp code gen --------- Co-authored-by: 0x45f --- .../fluid/ir/dialect/op_generator/api_gen.py | 31 +++- .../ir/dialect/op_generator/op_build_gen.py | 1 + .../fluid/ir/dialect/op_generator/op_gen.py | 13 ++ .../paddle_dialect/ir/pd_manual_api.cc | 10 ++ .../dialect/paddle_dialect/ir/pd_manual_api.h | 1 + .../paddle_dialect/ir/pd_manual_op_vjp.cc | 7 +- .../fluid/operators/generator/tests_utils.py | 18 +++ .../primitive/backend/manual/manual_backend.h | 8 -- .../backend/manual/manual_static_backend.cc | 48 ------- paddle/fluid/primitive/codegen/CMakeLists.txt | 3 +- paddle/fluid/primitive/codegen/gen.py | 133 +++++++++++++++++- .../backend/generated/generated_backend.h.j2 | 6 +- .../generated/generated_eager_backend.cc.j2 | 9 +- .../generated/generated_static_backend.cc.j2 | 80 ++++++++--- .../primitive/codegen/templates/common.j2 | 16 ++- .../templates/primitive/primitive.h.j2 | 4 +- .../rule/vjp/generated/generated_vjp.cc.j2 | 46 +++++- .../rule/vjp/generated/generated_vjp.h.j2 | 2 +- .../fluid/primitive/rule/vjp/CMakeLists.txt | 2 +- paddle/fluid/primitive/rule/vjp/details.h | 12 +- .../primitive/rule/vjp/manual/manual_vjp.cc | 46 +----- .../primitive/rule/vjp/manual/manual_vjp.h | 12 -- paddle/fluid/pybind/eager_utils.cc | 2 + test/prim/new_ir_prim/test_vjp_prim.py | 14 +- 24 files changed, 337 insertions(+), 187 deletions(-) diff --git a/paddle/fluid/ir/dialect/op_generator/api_gen.py b/paddle/fluid/ir/dialect/op_generator/api_gen.py index 667174e0fbcb3..cae035c657b69 100644 --- a/paddle/fluid/ir/dialect/op_generator/api_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/api_gen.py @@ -156,13 +156,26 @@ def _gen_api_args(self, op_info, with_default_attr, is_mutable_attr): def _gen_ret_type(self, op_info): type_list = op_info.output_type_list - if len(type_list) > 1: + intermediate_list = op_info.output_intermediate_list + assert len(type_list) == len(intermediate_list) + + output_num = len(type_list) - intermediate_list.count('true') + if output_num > 1: return 'std::tuple<{}>'.format( - ', '.join([self._type_map[type] for type in type_list]) + ', '.join( + [ + self._type_map[type] + for type, intermediate in zip( + type_list, intermediate_list + ) + if intermediate == 'false' + ] + ) ) - elif len(type_list) == 1: - return self._type_map[type_list[0]] - elif len(type_list) == 0: + elif output_num == 1: + index = intermediate_list.index('false') + return self._type_map[type_list[index]] + elif output_num == 0: return 'void' def _gen_one_declare(self, op_info, op_name, is_mutable_attr): @@ -252,10 +265,16 @@ def _gen_compute_op( def _gen_out_split_and_ret_list(self, op_info, op_inst_name): name_list = op_info.output_name_list type_list = op_info.output_type_list + intermediate_list = op_info.output_intermediate_list + assert len(name_list) == len(type_list) == len(intermediate_list) split_op_str = '' ret_list = [] - for i, (name, type) in enumerate(zip(name_list, type_list)): + for i, (name, type, intermediate) in enumerate( + zip(name_list, type_list, intermediate_list) + ): + if intermediate == 'true': + continue if VECTOR_TYPE in type: split_op_name = f'{name}_split_op' split_op_str += SPLIT_OP_TEMPLATE.format( diff --git a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py index a59948bcb3c6d..d9747b47e8747 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py @@ -129,6 +129,7 @@ def GenBuildInputArgsStr( 'float': 'phi::DataType::FLOAT32', 'std::vector': 'phi::DataType::INT64', 'const std::vector&': 'phi::DataType::INT64', + 'bool': 'phi::DataType::BOOL', } diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index fe2edb2b00ea5..fcbbf39c211bf 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -598,6 +598,19 @@ def parse_attribute_build_arg_type_list(self): if 'Scalar' in temp_type: if 'data_type' in attribute_info: temp_type = attribute_info['data_type'] + op_name = self.op_yaml_item['name'] + attr_name = attribute_info['name'] + if ( + op_name not in ["isclose", "allclose"] + and self.op_compat_item is not None + and 'scalar' in self.op_compat_item.keys() + and attr_name in self.op_compat_item['scalar'].keys() + and 'data_type' + in self.op_compat_item['scalar'][attr_name].keys() + ): + temp_type = self.op_compat_item['scalar'][attr_name][ + 'data_type' + ] if 'IntArray' in temp_type: if 'data_type' in attribute_info: temp_type = "const " + attribute_info['data_type'] + "&" diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.cc index 070c2e49ac6d5..b95d78a74f470 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.cc @@ -29,5 +29,15 @@ ir::OpResult split_grad(std::vector out_grads, return split_grad_op.x_grad(); } + +ir::OpResult split_grad(std::vector out_grads, int axis) { + auto combine_op = + APIBuilder::Instance().GetBuilder()->Build(out_grads); + paddle::dialect::SplitGradOp split_grad_op = + APIBuilder::Instance().GetBuilder()->Build( + combine_op.out(), axis); + + return split_grad_op.x_grad(); +} } // namespace dialect } // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.h index 1d16bc0793788..5eba73e5182bd 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_api.h @@ -25,5 +25,6 @@ namespace dialect { ir::OpResult split_grad(std::vector out_grads, ir::OpResult axis); +ir::OpResult split_grad(std::vector out_grads, int axis); } // namespace dialect } // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc index a69a1af650b98..c7bac02e3347e 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op_vjp.cc @@ -35,11 +35,8 @@ std::vector> SumOp::Vjp( Tensor x(std::make_shared(op_obj.x())); Tensor out_grad(std::make_shared(out_grads[0][0])); - IntArray axis = op_obj.axis() - .GetDefiningOp() - ->attribute("value") - .dyn_cast() - .data(); + Tensor axis(std::make_shared(op_obj.axis())); + bool keepdim = op->attribute("keepdim").dyn_cast().data(); bool reduce_all = false; std::vector> tensor_res = primitive::sum_vjp( diff --git a/paddle/fluid/operators/generator/tests_utils.py b/paddle/fluid/operators/generator/tests_utils.py index 2c99de85031b6..5f2e06eb89dab 100644 --- a/paddle/fluid/operators/generator/tests_utils.py +++ b/paddle/fluid/operators/generator/tests_utils.py @@ -84,3 +84,21 @@ def supports_no_need_buffer(op): def is_tensor_list(s): return s == 'Tensor[]' + + +def exist_mutable_attribute(attrs): + for attr in attrs: + if ( + attr['typename'] in ['Scalar', 'IntArray'] + and attr['support_tensor'] is True + ): + return True + else: + return False + + +def is_mutable_attribute(attr): + return ( + attr['typename'] in ['Scalar', 'IntArray'] + and attr['support_tensor'] is True + ) diff --git a/paddle/fluid/primitive/backend/manual/manual_backend.h b/paddle/fluid/primitive/backend/manual/manual_backend.h index fe009bd8fecbe..438f06034b010 100644 --- a/paddle/fluid/primitive/backend/manual/manual_backend.h +++ b/paddle/fluid/primitive/backend/manual/manual_backend.h @@ -28,14 +28,6 @@ using Scalar = paddle::experimental::Scalar; using IntArray = paddle::experimental::IntArray; using DataType = phi::DataType; -template -std::vector concat_grad(const std::vector& x, - const Tensor& out_grad, - const Tensor& axis); - -template -Tensor split_grad(const std::vector& out_grads, const Tensor& axis); - } // namespace backend } // namespace primitive } // namespace paddle diff --git a/paddle/fluid/primitive/backend/manual/manual_static_backend.cc b/paddle/fluid/primitive/backend/manual/manual_static_backend.cc index 53a231d4cf9e3..de39a58473337 100644 --- a/paddle/fluid/primitive/backend/manual/manual_static_backend.cc +++ b/paddle/fluid/primitive/backend/manual/manual_static_backend.cc @@ -23,54 +23,6 @@ namespace backend { using LazyTensor = paddle::primitive::LazyTensor; -template <> -std::vector concat_grad(const std::vector& x, - const Tensor& out_grad, - const Tensor& axis) { - std::vector x_res; - for (uint64_t idx = 0; idx < x.size(); idx++) { - x_res.emplace_back(std::static_pointer_cast(x[idx].impl()) - ->getValue() - .dyn_cast()); - } - - ir::OpResult out_grad_res = - std::static_pointer_cast(out_grad.impl()) - ->getValue() - .dyn_cast(); - - ir::OpResult axis_res = std::static_pointer_cast(axis.impl()) - ->getValue() - .dyn_cast(); - - std::vector op_res = - paddle::dialect::concat_grad(x_res, out_grad_res, axis_res); - - std::vector op_result; - for (uint64_t idx = 0; idx < op_res.size(); idx++) { - op_result.emplace_back( - std::make_shared(op_res[idx])); - } - return op_result; -} - -template <> -Tensor split_grad(const std::vector& out_grads, - const Tensor& axis) { - std::vector out_grads_res; - for (uint64_t idx = 0; idx < out_grads.size(); idx++) { - out_grads_res.emplace_back( - std::static_pointer_cast(out_grads[idx].impl()) - ->getValue() - .dyn_cast()); - } - ir::OpResult axis_res = std::static_pointer_cast(axis.impl()) - ->getValue() - .dyn_cast(); - ir::OpResult op_res = paddle::dialect::split_grad(out_grads_res, axis_res); - return Tensor(std::make_shared(op_res)); -} - } // namespace backend } // namespace primitive } // namespace paddle diff --git a/paddle/fluid/primitive/codegen/CMakeLists.txt b/paddle/fluid/primitive/codegen/CMakeLists.txt index d582616896253..d01d21829ca1e 100644 --- a/paddle/fluid/primitive/codegen/CMakeLists.txt +++ b/paddle/fluid/primitive/codegen/CMakeLists.txt @@ -7,6 +7,7 @@ set(rev_legacy_path ${parsed_yaml_path}/legacy_backward_ops.parsed.yaml) set(prim_path "${PADDLE_SOURCE_DIR}/paddle/fluid/primitive/primitive.yaml") set(templates_dir "${PADDLE_SOURCE_DIR}/paddle/fluid/primitive/codegen/templates/") +set(compat_path "${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml") set(destination_dir "${PADDLE_SOURCE_DIR}/paddle/fluid/primitive/") set(scripts "${PADDLE_SOURCE_DIR}/paddle/fluid/primitive/codegen/gen.py") @@ -17,7 +18,7 @@ execute_process( ${PYTHON_EXECUTABLE} ${scripts} --fwd_path ${fwd_path} --fwd_legacy_path ${fwd_legacy_path} --rev_path ${rev_path} --rev_legacy_path ${rev_legacy_path} --prim_path ${prim_path} --templates_dir ${templates_dir} - --destination_dir ${destination_dir} + --compat_path ${compat_path} --destination_dir ${destination_dir} RESULT_VARIABLE _result) if(${_result}) message( diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 09ba61fbb7fd6..7b3072675ec9a 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -16,6 +16,7 @@ import hashlib import pathlib import sys +from typing import Dict, List import jinja2 import yaml @@ -36,7 +37,15 @@ # fmt: on -VJPS = ['tanh_grad', 'mean_grad', 'add_grad', 'divide_grad', 'sum_grad'] +VJPS = [ + 'tanh_grad', + 'mean_grad', + 'add_grad', + 'divide_grad', + 'sum_grad', + 'concat_grad', + 'split_grad', +] VJP_COMPS = ['divide_grad', 'sum_grad'] BACKENDS = [ 'add_n', @@ -57,6 +66,8 @@ 'add_grad', 'divide_grad', 'sum_grad', + 'concat_grad', + 'split_grad', ] @@ -99,6 +110,7 @@ def render(src_dir: pathlib.Path, dst_dir: pathlib.Path, *args, **kwargs): 'to_paddle_attr_type': op_gen_filters.to_paddle_attr_type, 'to_paddle_input_type': op_gen_filters.to_paddle_input_type, 'to_paddle_output_type': op_gen_filters.to_paddle_output_type, + 'trip_intermediate': op_gen_filters.filter_intermediate, } ) env.tests.update( @@ -106,6 +118,8 @@ def render(src_dir: pathlib.Path, dst_dir: pathlib.Path, *args, **kwargs): 'scalar': op_gen_tests.is_scalar, 'intarray': op_gen_tests.is_intarray, 'datatype': op_gen_tests.is_datatype, + 'exist_mutable_attribute': op_gen_tests.exist_mutable_attribute, + 'mutable_attribute': op_gen_tests.is_mutable_attribute, } ) for tpl in env.list_templates( @@ -143,12 +157,114 @@ def save(content: str, path: pathlib.Path): print(f"Generate source file {path}") +def filter_compat_info(items): + for item in items: + item['op'] = item['op'].split('(')[0].strip() + if 'backward' in item: + item_backwards = item['backward'].split(',') + for idx, item_backward in enumerate(item_backwards): + item_backward = item_backward.split('(')[0].strip() + item_backwards[idx] = item_backward + item['backward'] = ( + ','.join(item_backwards) + if len(item_backwards) > 0 + else item_backwards[0] + ) + + +def to_compat_dict(items: List[Dict]) -> Dict[str, Dict]: + compat_dict = {} + for item in items: + name = item["op"] + compat_dict[name] = item + return compat_dict + + +def to_apis_dict(apis): + apis_dict = {} + for api in apis: + apis_dict[api['name']] = api + return apis_dict + + +def get_inplace_api(apis): + inplace_apis = [] + for api in apis: + if ( + 'inplace' in api + and api['inplace'] is not None + and not api['name'].endswith('_') + ): + inplace_api = api.copy() + inplace_api['name'] = api['name'] + '_' + inplace_apis.append(inplace_api) + return inplace_apis + + +def extend_compat_info(apis, compats): + for api in apis: + attrs = api["attrs"] + for attr in attrs: + if attr['typename'] in ["Scalar", "IntArray"]: + attr["support_tensor"] = False + apis_dict = to_apis_dict(apis) + for compat_item in compats: + fwd_op_name = compat_item["op"] + if fwd_op_name not in apis_dict: + continue + fwd_api = apis_dict[fwd_op_name] + backward_op_names = [] + while fwd_op_name is not None and fwd_op_name in apis_dict: + backward_op_names.append(apis_dict[fwd_op_name]['backward']) + fwd_op_name = apis_dict[fwd_op_name]['backward'] + backward_apis = [] + for backward_op_name in backward_op_names: + if backward_op_name in apis_dict: + backward_apis.append(apis_dict[backward_op_name]) + support_tensor_attrs_names = [] + compat_attrs_data_type = {} + if 'scalar' in compat_item: + for attr_name, attr_info in compat_item['scalar'].items(): + if ( + 'support_tensor' in attr_info + and attr_info['support_tensor'] is True + or 'tensor_name' in attr_info + ): + support_tensor_attrs_names.append(attr_name) + if 'data_type' in attr_info: + compat_attrs_data_type.update( + {attr_name: attr_info['data_type']} + ) + if 'int_array' in compat_item: + for attr_name, attr_info in compat_item['int_array'].items(): + if ( + 'support_tensor' in attr_info + and attr_info['support_tensor'] is True + or 'tensor_name' in attr_info + or 'tensors_name' in attr_info + ): + support_tensor_attrs_names.append(attr_name) + if len(support_tensor_attrs_names) > 0: + for api in [fwd_api] + backward_apis: + attrs = api["attrs"] + for attr in attrs: + if attr['name'] in support_tensor_attrs_names: + attr['support_tensor'] = True + for api in [fwd_api] + backward_apis: + attrs = api["attrs"] + for attr in attrs: + if attr['name'] in compat_attrs_data_type: + attr['data_type'] = compat_attrs_data_type[attr['name']] + return apis + + def gen( prim_path: pathlib.Path, fwd_path: pathlib.Path, fwd_legacy_path: pathlib.Path, rev_path: pathlib.Path, rev_legacy_path: pathlib.Path, + compat_path: pathlib.Path, templates_dir: pathlib.Path, destination_dir: pathlib.Path, ): @@ -163,20 +279,22 @@ def gen( rev_path (pathlib.Path): The YAML file path of the backward API. rev_legacy_path (pathlib.Path): The YAML file path of the legacy backward API. + compat_path: (pathlib.Path): The YAML file path of the ops compat. templates_dir (pathlib.Path): The directory of the templates. destination_dir (pathlib.Path): The Directory of the generated file. Returns: None """ - prims, fwds, legacy_fwds, revs, legacy_revs = ( + prims, fwds, legacy_fwds, revs, legacy_revs, compats = ( load(prim_path), load(fwd_path), load(fwd_legacy_path), load(rev_path), load(rev_legacy_path), + load(compat_path), ) - + filter_compat_info(compats) apis = [{**api, **{'is_fwd': True}} for api in fwds + legacy_fwds] apis = apis + [{**api, **{'is_fwd': False}} for api in revs + legacy_revs] apis = [ @@ -185,7 +303,8 @@ def gen( else {**api, **{'is_prim': False}} for api in apis ] - + apis = extend_compat_info(apis, compats) + apis = apis + get_inplace_api(apis) render( templates_dir, destination_dir, @@ -221,6 +340,11 @@ def gen( type=str, help='The parsed ops yaml file.', ) + parser.add_argument( + '--compat_path', + type=str, + help='The parsed ops compat yaml file.', + ) parser.add_argument( '--templates_dir', type=str, @@ -239,6 +363,7 @@ def gen( pathlib.Path(args.fwd_legacy_path), pathlib.Path(args.rev_path), pathlib.Path(args.rev_legacy_path), + pathlib.Path(args.compat_path), pathlib.Path(args.templates_dir), pathlib.Path(args.destination_dir), ) diff --git a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_backend.h.j2 b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_backend.h.j2 index fceb290b78bab..3bbd00d967b83 100644 --- a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_backend.h.j2 +++ b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_backend.h.j2 @@ -20,7 +20,11 @@ using DataType = phi::DataType; {% for api in apis %} {%- if api.name in backend_white_list -%} -{{common.sig(api.name, api.inputs, api.outputs, api.attrs, True)}}; + {% if api.attrs is exist_mutable_attribute %} +{{common.sig(api.name, api.inputs, api.outputs|trip_intermediate , api.attrs, True, True)}}; + + {% endif %} +{{common.sig(api.name, api.inputs, api.outputs|trip_intermediate , api.attrs, False, True)}}; {% endif %} {% endfor %} diff --git a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_eager_backend.cc.j2 b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_eager_backend.cc.j2 index d3639f98e4c88..4c2f02224e2f7 100644 --- a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_eager_backend.cc.j2 +++ b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_eager_backend.cc.j2 @@ -18,7 +18,7 @@ namespace backend { {%- macro sig(name, inputs, attrs, outputs) -%} template <> -{{common.ret(outputs)}} {{name}}({{common.params(inputs, attrs)}}) +{{common.ret(outputs)}} {{name}}({{common.params(inputs, attrs, False)}}) {%- endmacro -%} {% macro body(name, inputs, attrs, outputs) %} @@ -34,10 +34,9 @@ return ::{{name}}_ad_func({{common.args(input_names, attr_names)}}); {% for api in apis %} - {#- TODO(cxxly): codegen for reshape -#} - {%- if api.is_prim and api.name in backend_white_list and api.name != 'reshape' -%} -{{sig(api.name, api.inputs, api.attrs, api.outputs)}} { -{{body(api.name, api.inputs, api.attrs, api.outputs)}} + {%- if api.is_prim and api.name in backend_white_list -%} +{{sig(api.name, api.inputs, api.attrs, api.outputs | trip_intermediate)}} { +{{body(api.name, api.inputs, api.attrs, api.outputs | trip_intermediate)}} } {% endif %} diff --git a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_static_backend.cc.j2 b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_static_backend.cc.j2 index 0584f3953e7e7..8e004c22eeeb5 100644 --- a/paddle/fluid/primitive/codegen/templates/backend/generated/generated_static_backend.cc.j2 +++ b/paddle/fluid/primitive/codegen/templates/backend/generated/generated_static_backend.cc.j2 @@ -12,38 +12,73 @@ namespace backend { using LazyTensor = paddle::primitive::LazyTensor; -{%- macro sig(name, inputs, outputs, attrs) -%} +{%- macro sig(name, inputs, outputs, attrs, mutable_attribute_as_inputs=False) -%} template <> -{{common.ret(outputs)}} {{name}}({{common.params(inputs, attrs)}}) +{{common.ret(outputs)}} {{name}}({{common.params(inputs, attrs, mutable_attribute_as_inputs, False)}}) {%- endmacro -%} -{% macro body(name, inputs, outputs, attrs) %} +{% macro body(name, inputs, outputs, attrs, mutable_attribute_as_inputs=False) %} {%- set output_names = [] -%} {%- for o in outputs -%} {%- do output_names.append(o.name) -%} {%-endfor-%} - {%- for input in inputs -%} - {% if input.typename=='Tensor[]' %} + {%- for input in inputs -%} + {% if input.typename=='Tensor[]' %} std::vector {{input.name}}_res({{input.name}}.size()); std::transform({{input.name}}.begin(), {{input.name}}.end(), {{input.name}}_res.begin(), [](const Tensor& t) { return std::static_pointer_cast(t.impl())->getValue().dyn_cast(); }); - {% else %} + {% else %} ir::OpResult {{input.name}}_res = std::static_pointer_cast({{input.name}}.impl())->getValue().dyn_cast(); - {% endif %} - {% endfor %} + {% endif %} + {% endfor %} + {%- for attr in attrs -%} + {% if mutable_attribute_as_inputs and attr is mutable_attribute %} + ir::OpResult {{attr.name}}_res = std::static_pointer_cast({{attr.name~'_'}}.impl())->getValue().dyn_cast(); + {% endif %} + {% endfor %} {%- set input_names = [] -%} - {%- for i in inputs -%} {%- do input_names.append(i.name~'_res') -%} {%- endfor -%} + {%- for i in inputs -%} + {%- do input_names.append(i.name~'_res') -%} + {%- endfor -%} + {%- if mutable_attribute_as_inputs -%} + {%- for i in attrs -%} + {%- if i is mutable_attribute -%} + {%- do input_names.append(i.name~'_res') -%} + {%- endif -%} + {%- endfor -%} + {%- endif -%} {%- set attr_names = [] -%} - {%- for i in attrs -%} {%- do attr_names.append(common.phi2ir_attr(i)) -%} {% endfor %} + {%- for i in attrs -%} + {%- if not mutable_attribute_as_inputs or mutable_attribute_as_inputs and i is not mutable_attribute -%}{#- do nothing -#} + {%- do attr_names.append(common.phi2ir_attr(i)) -%} + {%- endif -%} + {% endfor %} auto op_res = paddle::dialect::{{name}}({{common.args(input_names, attr_names)}}); - {% if outputs|length > 1 %} - return std::make_tuple( + {% if outputs|length == 1 %} + {% if outputs[0].typename == 'Tensor' %} + Tensor {{outputs[0].name}}(std::make_shared(op_res)); + return {{outputs[0].name}}; + {% elif outputs[0].typename == 'Tensor[]' %} + std::vector {{outputs[0].name}}(op_res.size()); + std::transform(op_res.begin(), op_res.end(), {{outputs[0].name}}.begin(), [](const ir::OpResult& res) { + return Tensor(std::make_shared(res)); + }); + return {{outputs[0].name}}; + {% else %} {#- render nothing -#} + {% endif %} + {% elif outputs|length > 1 %} {% for i in range(outputs|length) %} - Tensor(std::make_shared(std::get<{{i}}>(op_res))){%- if i!=outputs|length - 1 -%}, {% endif %} - + auto op_res_{{i}} = std::get<{{i}}>(op_res); + {% if outputs[i].typename == 'Tensor' %} + Tensor {{outputs[i].name}}(std::make_shared(op_res_{{i}})); + {% elif outputs[i].typename == 'Tensor[]' %} + std::vector {{outputs[i].name}}(op_res_{{i}}.size()); + std::transform(op_res_{{i}}.begin(), op_res_{{i}}.end(), {{outputs[i].name}}.begin(), [](const ir::OpResult& res) { + return Tensor(std::make_shared(res)); + }); + {% else %} {#- render nothing -#} + {% endif %} {% endfor %} - ); - {% elif outputs|length == 1 %} - return Tensor(std::make_shared(op_res)); + return std::make_tuple({% for i in range(outputs|length) %}{{outputs[i].name}}{%- if i!=outputs|length - 1 -%}, {% endif %}{% endfor %}); {% else %} {#- render nothing -#} {% endif %} {% endmacro %} @@ -51,10 +86,17 @@ template <> {% for api in apis %} {% if api.name in backend_white_list %} -{{sig(api.name, api.inputs, api.outputs, api.attrs)}} { -{{body(api.name, api.inputs, api.outputs, api.attrs)}} + {% set api_outputs = api.outputs | trip_intermediate %} +{{sig(api.name, api.inputs, api_outputs, api.attrs)}} { +{{body(api.name, api.inputs, api_outputs, api.attrs)}} +} + + {% if api.attrs is exist_mutable_attribute %} +{{sig(api.name, api.inputs, api_outputs, api.attrs, True)}} { +{{body(api.name, api.inputs, api_outputs, api.attrs, True)}} } + {% endif %} {% endif %} {% endfor %} diff --git a/paddle/fluid/primitive/codegen/templates/common.j2 b/paddle/fluid/primitive/codegen/templates/common.j2 index 7984275ae2733..fd49c76d88db0 100644 --- a/paddle/fluid/primitive/codegen/templates/common.j2 +++ b/paddle/fluid/primitive/codegen/templates/common.j2 @@ -1,18 +1,22 @@ -{%- macro sig(name, inputs, outputs, attrs, default=False) -%} +{%- macro sig(name, inputs, outputs, attrs, mutable_attribute_as_inputs=False, default=False) -%} template -{{ret(outputs)}} {{name}}({{params(inputs, attrs, default)}}) +{{ret(outputs)}} {{name}}({{params(inputs, attrs, mutable_attribute_as_inputs, default)}}) {%- endmacro %} -{%- macro params(inputs, attrs, default=False) -%} +{%- macro params(inputs, attrs, mutable_attribute_as_inputs=False, default=False) -%} {%- set input_params = [] -%} {%- for i in inputs -%} {%- do input_params.append(i.typename|to_paddle_input_type(i.optional)~' '~i.name) -%} {%- endfor -%} {%- set attr_params = [] -%} {%- for i in attrs -%} - {%- if default -%} - {%- do attr_params.append(i.typename|to_paddle_attr_type~' '~i.name~default_value(i)) -%} + {%- if not mutable_attribute_as_inputs or i is not mutable_attribute -%} + {%- if default -%} + {%- do attr_params.append(i.typename|to_paddle_attr_type~' '~i.name~default_value(i)) -%} + {%- else -%} + {%- do attr_params.append(i.typename|to_paddle_attr_type~' '~i.name) -%} + {%- endif -%} {%- else -%} - {%- do attr_params.append(i.typename|to_paddle_attr_type~' '~i.name) -%} + {%- do input_params.append('const Tensor&'~' '~i.name~'_') -%} {%- endif -%} {%- endfor -%} {{sequence('', '', ', ', input_params)}} diff --git a/paddle/fluid/primitive/codegen/templates/primitive/primitive.h.j2 b/paddle/fluid/primitive/codegen/templates/primitive/primitive.h.j2 index 2340c32b08abb..34ee37c5898e6 100644 --- a/paddle/fluid/primitive/codegen/templates/primitive/primitive.h.j2 +++ b/paddle/fluid/primitive/codegen/templates/primitive/primitive.h.j2 @@ -13,12 +13,12 @@ using Tensor = paddle::Tensor; using IntArray = paddle::experimental::IntArray; {% for api in apis %} -{%- if api.is_prim and api.name in backend_white_list -%} +{%- if api.is_prim and api.name in backend_white_list and api.name[-1] != '_' -%} {%- set input_names = [] -%} {%- for i in api.inputs -%} {%- do input_names.append(i.name) -%} {%- endfor -%} {%- set attr_names = [] -%} {%- for i in api.attrs -%} {%- do attr_names.append(i.name) -%} {% endfor %} -{{common.sig(api.name, api.inputs, api.outputs, api.attrs, True)}} { +{{common.sig(api.name, api.inputs, api.outputs | trip_intermediate, api.attrs, False, True)}} { return backend::{{api.name}}({{common.args(input_names, attr_names)}}); } diff --git a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 index 13a3d397b834b..ab040254355f5 100644 --- a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 +++ b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 @@ -3,6 +3,7 @@ #include "paddle/fluid/primitive/rule/vjp/generated/generated_vjp.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_api.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h" #include "paddle/fluid/prim/utils/static/static_global_utils.h" #include "paddle/fluid/primitive/backend/backend.h" #include "paddle/fluid/primitive/rule/vjp/details.h" @@ -14,7 +15,7 @@ namespace paddle { namespace primitive { {% macro sig(fwd_name, name, inputs, attrs, outputs) -%} -std::vector> {{fwd_name}}_vjp({{common.params(inputs, attrs)}}, const std::vector>& stop_gradients) +std::vector> {{fwd_name}}_vjp({{common.params(inputs, attrs, attrs is exist_mutable_attribute)}}, const std::vector>& stop_gradients) {%- endmacro -%} {% macro body(api) %} @@ -34,16 +35,46 @@ if (paddle::prim::StaticCompositeContext::Instance().IsBwdPrimEnabled()) { return vjp_res; {% endmacro %} +{% macro get_mutable_attribute(attrs, api_name) %} + {% for i in attrs %} + {%- if i is mutable_attribute -%} +auto* {{i.name}}_define_op = std::static_pointer_cast({{i.name~'_'}}.impl())->getValue().dyn_cast().GetDefiningOp(); + {% if i.typename is scalar %} +if({{i.name}}_define_op->name() != "pd.full") { + PADDLE_THROW(platform::errors::Unimplemented( + "We don't support dynamic tensors attribute {{i.name}} for {{api_name}} composite " + "for now. ")); +} +auto {{i.name}} = {{i.name}}_define_op->attribute("value").dyn_cast().data(); + {% elif i.typename is intarray %} +if({{i.name}}_define_op->name() != "pd.full_int_array"){ + PADDLE_THROW(platform::errors::Unimplemented( + "We don't support dynamic tensors attribute {{i.name}} for {{api_name}} composite " + "for now. ")); +} +auto {{i.name}} = {{i.name}}_define_op->attribute("value").dyn_cast().data(); + {% endif %} + {% endif %} + {% endfor %} +{% endmacro %} + {% macro body_unprim(api) %} {%- set input_names=[] -%} {%- for i in api.inputs -%} {%- do input_names.append(i.name) -%} {%- endfor -%} {%- set attr_names=[] -%} - {%- for i in api.attrs -%} {%- do attr_names.append(i.name) -%} {%- endfor %} + {%- for i in api.attrs -%} + {%- if i is mutable_attribute -%} + {%- do input_names.append(i.name~'_') -%} + {%- else -%} + {%- do attr_names.append(i.name) -%} + {%- endif -%} + {%- endfor %} auto op_res = backend::{{api.name}}({{common.args(input_names, attr_names)}}); - {% if api.outputs|length > 1 %} - {% for i in range(api.outputs|length) %} + {% set outputs = api.outputs|trip_intermediate %} {#- ignore intermediate output -#} + {% if outputs|length > 1 %} + {% for i in range(outputs|length) %} auto out{{i}} = std::get<{{i}}>(op_res); - {% if api.outputs[i].typename=='Tensor' %} + {% if outputs[i].typename=='Tensor' %} vjp_res[{{i}}][0] = !stop_gradients[{{i}}][0] ? out{{i}} : vjp_res[{{i}}][0]; {% else %} for (size_t i=0; i< stop_gradients[{{i}}].size(); i++ ) { @@ -51,8 +82,8 @@ for (size_t i=0; i< stop_gradients[{{i}}].size(); i++ ) { } {% endif %} {% endfor %} - {% elif api.outputs|length == 1 %} - {% if api.outputs[0].typename=='Tensor' %} + {% elif outputs|length == 1 %} + {% if outputs[0].typename=='Tensor' %} vjp_res[0][0] = !stop_gradients[0][0] ? op_res : vjp_res[0][0]; {% else %} for (size_t i=0; i< stop_gradients[0].size(); i++ ) { @@ -74,6 +105,7 @@ for (size_t i=0; i< stop_gradients[{{i}}].size(); i++ ) { } {% endif %} {% endfor %} +{{get_mutable_attribute(api.attrs, api.name)}} details::{{api.composite.func_name}}({{api.composite.func_args}}); {% endmacro %} diff --git a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.h.j2 b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.h.j2 index 58aa5ed283560..b9e758aaa73ff 100644 --- a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.h.j2 +++ b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.h.j2 @@ -14,7 +14,7 @@ namespace primitive { using IntArray = paddle::experimental::IntArray; {% macro sig(fwd_name, name, inputs, attrs, outputs) %} -std::vector> {{fwd_name}}_vjp({{common.params(inputs, attrs)}}, const std::vector>& stop_gradients); +std::vector> {{fwd_name}}_vjp({{common.params(inputs, attrs, attrs is exist_mutable_attribute)}}, const std::vector>& stop_gradients); {% endmacro %} {%- set api_map = {} -%} diff --git a/paddle/fluid/primitive/rule/vjp/CMakeLists.txt b/paddle/fluid/primitive/rule/vjp/CMakeLists.txt index 3d1fc38a09050..3d6906bb33ca5 100644 --- a/paddle/fluid/primitive/rule/vjp/CMakeLists.txt +++ b/paddle/fluid/primitive/rule/vjp/CMakeLists.txt @@ -5,4 +5,4 @@ cc_library( primitive_vjp_experimental SRCS ${VJP_SRCS} DEPS primitive_backend_static_experimental static_global_utils - primitive_static_utils_experimental) + primitive_static_utils_experimental pd_dialect_core) diff --git a/paddle/fluid/primitive/rule/vjp/details.h b/paddle/fluid/primitive/rule/vjp/details.h index 6ee9c5880b6d6..e018cccdef7a0 100644 --- a/paddle/fluid/primitive/rule/vjp/details.h +++ b/paddle/fluid/primitive/rule/vjp/details.h @@ -51,8 +51,7 @@ void divide_grad(const Tensor& x, } else { auto dy_reduce_res = sum(dy_res, phi::vectorize(reduce_dim), y.dtype(), false); - auto reshape_res = reshape(dy_reduce_res, phi::vectorize(y.dims())); - auto dy_tmp = std::get<0>(reshape_res); + auto dy_tmp = reshape(dy_reduce_res, phi::vectorize(y.dims())); set_output(dy_tmp, dy); } } else { @@ -71,9 +70,7 @@ void divide_grad(const Tensor& x, } else { auto dx_reduce_res = sum(dx_res, phi::vectorize(reduce_dim), x.dtype(), false); - auto dx_reduce_reshape_res = - reshape(dx_reduce_res, phi::vectorize(x.dims())); - auto dx_tmp = std::get<0>(dx_reduce_reshape_res); + auto dx_tmp = reshape(dx_reduce_res, phi::vectorize(x.dims())); set_output(dx_tmp, dx); } @@ -121,9 +118,8 @@ void sum_grad(const Tensor& x, } } auto out_grad_shape = get_unsqueeze_dims(out_grad, axis_); - auto out_grad_reshape_res = reshape(out_grad, out_grad_shape); - auto out_grad_ = std::get<0>(out_grad_reshape_res); - x_grad_tmp = expand(out_grad, IntArray(x_dim)); + auto out_grad_ = reshape(out_grad, out_grad_shape); + x_grad_tmp = expand(out_grad_, IntArray(x_dim)); } else { x_grad_tmp = expand(out_grad, IntArray(x_dim)); } diff --git a/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.cc b/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.cc index 86d83dbee249d..c56ac5c5f79ab 100644 --- a/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.cc +++ b/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.cc @@ -24,49 +24,5 @@ #include "paddle/ir/core/operation.h" namespace paddle { -namespace primitive { - -std::vector> concat_vjp( - const std::vector& x, - const Tensor& out_grad, - const Tensor& axis, - const std::vector>& stop_gradients) { - std::vector> vjp_res(2, std::vector()); - // get concat_grad res. - std::vector op_res = - backend::concat_grad(x, out_grad, axis); - - // construct vjp result by op result and stop_gradients info - vjp_res[0].resize(op_res.size()); - for (uint64_t idx = 0; idx < op_res.size(); idx++) { - if (!stop_gradients[0][idx]) { - vjp_res[0][idx] = op_res[idx]; - } - } - // vjp_res[1] is axis's grad which is attribute (no grad). - vjp_res[1].resize(1); - return vjp_res; -} - -std::vector> split_vjp( - const std::vector& out_grads, - const Tensor& axis, - const std::vector>& stop_gradients) { - std::vector> vjp_res(3, std::vector(1)); - // get concat_grad res. - Tensor op_res = backend::split_grad(out_grads, axis); - - // construct vjp result by op result and stop_gradients info - if (!stop_gradients[0][0]) { - vjp_res[0][0] = op_res; - } - - // vjp_res[1] is sections's grad which is attribute (no grad). - // vjp_res[2] is axis's grad which is attribute (no grad). - vjp_res[1].resize(stop_gradients[1].size()); - vjp_res[2].resize(stop_gradients[2].size()); - return vjp_res; -} - -} // namespace primitive +namespace primitive {} // namespace primitive } // namespace paddle diff --git a/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.h b/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.h index 87e1f33bb9ebd..0fffd6ba31a4c 100644 --- a/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.h +++ b/paddle/fluid/primitive/rule/vjp/manual/manual_vjp.h @@ -23,17 +23,5 @@ namespace paddle { namespace primitive { using IntArray = paddle::experimental::IntArray; - -std::vector> concat_vjp( - const std::vector& x, - const Tensor& out_grad, - const Tensor& axis, - const std::vector>& stop_gradients); - -std::vector> split_vjp( - const std::vector& out_grads, - const Tensor& axis, - const std::vector>& stop_gradients); - } // namespace primitive } // namespace paddle diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index d953e953424b7..95d86f544c4bf 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -1490,6 +1490,8 @@ ir::OpResult CastPyArg2OpResult(PyObject* obj, size_t arg_pos) { if (PyObject_TypeCheck(obj, g_ir_opresult_pytype)) { return ::pybind11::handle(obj).cast(); + } else if (obj == nullptr || obj == Py_None) { + return ir::OpResult(); } else { PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument (position %d) must be " diff --git a/test/prim/new_ir_prim/test_vjp_prim.py b/test/prim/new_ir_prim/test_vjp_prim.py index 600a14129782f..46ff348734db2 100644 --- a/test/prim/new_ir_prim/test_vjp_prim.py +++ b/test/prim/new_ir_prim/test_vjp_prim.py @@ -32,11 +32,11 @@ def get_ir_program_0(): ) x.stop_gradient = False y = paddle.tensor.fill_constant(shape=[4], dtype='float32', value=1.0) - y.stop_gradiable = False + y.stop_gradient = False dout = paddle.tensor.fill_constant( shape=[1, 4], dtype='float32', value=1.0 ) - dout.stop_gradiable = False + dout.stop_gradient = False out = paddle.divide(x, y) newir_program = ir.translate_to_new_ir(main_program.desc) return newir_program @@ -52,10 +52,8 @@ def get_ir_program_1(): shape=[4, 5], dtype='float32', value=2.0 ) x.stop_gradient = False - dout = paddle.tensor.fill_constant( - shape=[1], dtype='float32', value=1.0 - ) - dout.stop_gradiable = False + dout = paddle.tensor.fill_constant(shape=[], dtype='float32', value=1.0) + dout.stop_gradient = False out = paddle.sum(x) newir_program = ir.translate_to_new_ir(main_program.desc) return newir_program @@ -124,7 +122,7 @@ def test_divide_grad_no_prim(self): def test_sum_grad_prim(self): newir_program = get_ir_program_1() paddle.fluid.core._set_prim_backward_enabled(True) - dout = newir_program.block().ops[-2].result(0) + dout = newir_program.block().ops[-3].result(0) out_grads = [[dout]] stop_gradients = [[False], [True]] sum_op = newir_program.block().ops[-1] @@ -162,7 +160,7 @@ def test_sum_grad_no_prim(self): grad_outs[0][0].get_defining_op().name(), "pd.sum_grad" ) self.assertEqual(grad_outs[1][0], None) - self.assertEqual(len(newir_program.block().ops), 6) + self.assertEqual(len(newir_program.block().ops), 5) if __name__ == "__main__": From 170033691399975913e7254cf5802ee19506d4b4 Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:19:25 +0800 Subject: [PATCH 079/194] export flags defined in phi on windows (#56848) * update * update --- paddle/phi/core/flags.h | 146 ++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 81 deletions(-) diff --git a/paddle/phi/core/flags.h b/paddle/phi/core/flags.h index 776e268b201c7..938f661853aa2 100644 --- a/paddle/phi/core/flags.h +++ b/paddle/phi/core/flags.h @@ -32,10 +32,9 @@ #define PHI_IMPORT_FLAG #endif // _WIN32 +// We redefine the flags macro for exporting flags defined in phi #ifdef PADDLE_WITH_GFLAGS -// We redefine the gflags' macro for exporting global variable -// ----------------------------DECLARE FLAGS---------------------------- // clang-format off #define PHI_DECLARE_VARIABLE(type, shorttype, name) \ namespace fL##shorttype { \ @@ -44,35 +43,10 @@ using fL##shorttype::FLAGS_##name // clang-format on -#define PHI_DECLARE_bool(name) PHI_DECLARE_VARIABLE(bool, B, name) - -#define PHI_DECLARE_int32(name) \ - PHI_DECLARE_VARIABLE(::GFLAGS_NAMESPACE::int32, I, name) - -#define PHI_DECLARE_uint32(name) \ - PHI_DECLARE_VARIABLE(::GFLAGS_NAMESPACE::uint32, U, name) - -#define PHI_DECLARE_int64(name) \ - PHI_DECLARE_VARIABLE(::GFLAGS_NAMESPACE::int64, I64, name) - -#define PHI_DECLARE_uint64(name) \ - PHI_DECLARE_VARIABLE(::GFLAGS_NAMESPACE::uint64, U64, name) - -#define PHI_DECLARE_double(name) PHI_DECLARE_VARIABLE(double, D, name) - -#define PHI_DECLARE_string(name) \ - /* We always want to import declared variables, dll or no */ \ - namespace fLS { \ - extern PHI_IMPORT_FLAG ::fLS::clstring& FLAGS_##name; \ - } \ - using fLS::FLAGS_##name - -// ----------------------------DEFINE FLAGS---------------------------- #define PHI_DEFINE_VARIABLE(type, shorttype, name, value, help) \ namespace fL##shorttype { \ - static const type FLAGS_nono##name = value; \ - PHI_EXPORT_FLAG type FLAGS_##name = FLAGS_nono##name; \ - static type FLAGS_no##name = FLAGS_nono##name; \ + PHI_EXPORT_FLAG type FLAGS_##name = value; \ + static type FLAGS_no##name = value; \ static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \ #name, \ MAYBE_STRIPPED_HELP(help), \ @@ -82,67 +56,77 @@ } /* NOLINT */ \ using fL##shorttype::FLAGS_##name -#define PHI_DEFINE_bool(name, val, txt) \ - namespace fLB { \ - typedef ::fLB::CompileAssert FLAG_##name##_value_is_not_a_bool /* NOLINT */ \ - [(sizeof(::fLB::IsBoolFlag(val)) != sizeof(double)) ? 1 : -1]; \ - } \ - PHI_DEFINE_VARIABLE(bool, B, name, val, txt) +#else // PADDLE_WITH_GFLAGS -#define PHI_DEFINE_int32(name, val, txt) \ - PHI_DEFINE_VARIABLE(GFLAGS_NAMESPACE::int32, I, name, val, txt) +#define PHI_DECLARE_VARIABLE(type, shorttype, name) \ + namespace paddle { \ + namespace flags { \ + extern PHI_IMPORT_FLAG type FLAGS_##name; \ + } \ + } \ + using paddle::flags::FLAGS_##name + +#define PHI_DEFINE_VARIABLE(type, shorttype, name, default_value, description) \ + namespace paddle { \ + namespace flags { \ + static const type FLAGS_##name##_default = default_value; \ + PHI_EXPORT_FLAG type FLAGS_##name = default_value; \ + /* Register FLAG */ \ + static ::paddle::flags::FlagRegisterer flag_##name##_registerer( \ + #name, description, __FILE__, &FLAGS_##name##_default, &FLAGS_##name); \ + } \ + } \ + using paddle::flags::FLAGS_##name -#define PHI_DEFINE_uint32(name, val, txt) \ - PHI_DEFINE_VARIABLE(GFLAGS_NAMESPACE::uint32, U, name, val, txt) +#endif -#define PHI_DEFINE_int64(name, val, txt) \ - PHI_DEFINE_VARIABLE(GFLAGS_NAMESPACE::int64, I64, name, val, txt) +// ----------------------------DECLARE FLAGS---------------------------- +#define PHI_DECLARE_bool(name) PHI_DECLARE_VARIABLE(bool, B, name) +#define PHI_DECLARE_int32(name) PHI_DECLARE_VARIABLE(int32_t, I, name) +#define PHI_DECLARE_uint32(name) PHI_DECLARE_VARIABLE(uint32_t, U, name) +#define PHI_DECLARE_int64(name) PHI_DECLARE_VARIABLE(int64_t, I64, name) +#define PHI_DECLARE_uint64(name) PHI_DECLARE_VARIABLE(uint64_t, U64, name) +#define PHI_DECLARE_double(name) PHI_DECLARE_VARIABLE(double, D, name) +// ----------------------------DEFINE FLAGS----------------------------- +#define PHI_DEFINE_bool(name, val, txt) \ + PHI_DEFINE_VARIABLE(bool, B, name, val, txt) +#define PHI_DEFINE_int32(name, val, txt) \ + PHI_DEFINE_VARIABLE(int32_t, I, name, val, txt) +#define PHI_DEFINE_uint32(name, val, txt) \ + PHI_DEFINE_VARIABLE(uint32_t, U, name, val, txt) +#define PHI_DEFINE_int64(name, val, txt) \ + PHI_DEFINE_VARIABLE(int64_t, I64, name, val, txt) #define PHI_DEFINE_uint64(name, val, txt) \ - PHI_DEFINE_VARIABLE(GFLAGS_NAMESPACE::uint64, U64, name, val, txt) - + PHI_DEFINE_VARIABLE(uint64_t, U64, name, val, txt) #define PHI_DEFINE_double(name, val, txt) \ PHI_DEFINE_VARIABLE(double, D, name, val, txt) -#define PHI_DEFINE_string(name, val, txt) \ - namespace fLS { \ - using ::fLS::clstring; \ - using ::fLS::StringFlagDestructor; \ - static union { /* NOLINT */ \ - void* align; \ - std::array s; \ - } s_##name[2]; /* NOLINT */ \ - clstring* const FLAGS_no##name = \ - ::fLS::dont_pass0toDEFINE_string(s_##name[0].s.data(), val); \ - static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \ - #name, \ - MAYBE_STRIPPED_HELP(txt), \ - __FILE__, \ - FLAGS_no##name, \ - new (s_##name[1].s.data()) clstring(*FLAGS_no##name)); \ - static StringFlagDestructor d_##name(s_##name[0].s.data(), \ - s_##name[1].s.data()); \ - extern PHI_EXPORT_FLAG clstring& FLAGS_##name; \ - using fLS::FLAGS_##name; \ - clstring& FLAGS_##name = *FLAGS_no##name; \ - } /* NOLINT */ \ +#ifdef PADDLE_WITH_GFLAGS +#define PHI_DECLARE_string(name) \ + namespace fLS { \ + extern PHI_IMPORT_FLAG ::fLS::clstring& FLAGS_##name; \ + } \ using fLS::FLAGS_##name -#else // PADDLE_WITH_GFLAGS -#define PHI_DEFINE_bool(name, val, txt) PD_DEFINE_bool(name, val, txt) -#define PHI_DEFINE_int32(name, val, txt) PD_DEFINE_int32(name, val, txt) -#define PHI_DEFINE_uint32(name, val, txt) PD_DEFINE_uint32(name, val, txt) -#define PHI_DEFINE_int64(name, val, txt) PD_DEFINE_int64(name, val, txt) -#define PHI_DEFINE_uint64(name, val, txt) PD_DEFINE_uint64(name, val, txt) -#define PHI_DEFINE_double(name, val, txt) PD_DEFINE_double(name, val, txt) -#define PHI_DEFINE_string(name, val, txt) PD_DEFINE_string(name, val, txt) - -#define PHI_DECLARE_bool(name) PD_DECLARE_bool(name) -#define PHI_DECLARE_int32(name) PD_DECLARE_int32(name) -#define PHI_DECLARE_uint32(name) PD_DECLARE_uint32(name) -#define PHI_DECLARE_int64(name) PD_DECLARE_int64(name) -#define PHI_DECLARE_uint64(name) PD_DECLARE_uint64(name) -#define PHI_DECLARE_double(name) PD_DECLARE_double(name) -#define PHI_DECLARE_string(name) PD_DECLARE_string(name) + +#define PHI_DEFINE_string(name, val, txt) \ + namespace fLS { \ + using ::fLS::clstring; \ + clstring FLAGS_##name##_default = val; \ + clstring FLAGS_##name##_current = val; \ + static GFLAGS_NAMESPACE::FlagRegisterer o_##name(#name, \ + MAYBE_STRIPPED_HELP(txt), \ + __FILE__, \ + &FLAGS_##name##_current, \ + &FLAGS_##name##_default); \ + PHI_EXPORT_FLAG clstring& FLAGS_##name = FLAGS_##name##_current; \ + } /* NOLINT */ \ + using ::fLS::FLAGS_##name +#else +#define PHI_DECLARE_string(name) PHI_DECLARE_VARIABLE(std::string, S, name) + +#define PHI_DEFINE_string(name, val, txt) \ + PHI_DEFINE_VARIABLE(std::string, S, name, val, txt) #endif namespace phi { From 394bb8c23fe79903e442c3374e8aa5907eccceb6 Mon Sep 17 00:00:00 2001 From: xuxinyi389 <104957571+xuxinyi389@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:46:47 +0800 Subject: [PATCH 080/194] Normalize_CI_scripts (#56855) --- paddle/scripts/paddle_build.bat | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/paddle/scripts/paddle_build.bat b/paddle/scripts/paddle_build.bat index 5098e025fea5e..0d676569b29de 100644 --- a/paddle/scripts/paddle_build.bat +++ b/paddle/scripts/paddle_build.bat @@ -24,7 +24,11 @@ rem -------clean up environment----------- set work_dir=%cd% if not defined cache_dir set cache_dir=%work_dir:Paddle=cache% if not exist %cache_dir%\tools ( - git clone https://github.com/zhouwei25/tools.git %cache_dir%\tools + cd /d cache_dir + python -m pip install wget + python -c "import wget;wget.download('https://paddle-ci.gz.bcebos.com/window_requirement/tools.zip')" + tar xf tools.zip + cd /d work_dir ) taskkill /f /im cmake.exe /t 2>NUL taskkill /f /im ninja.exe /t 2>NUL From c196d9aac81b555b659934bdeb046ee084c05e6d Mon Sep 17 00:00:00 2001 From: xuxinyi389 <104957571+xuxinyi389@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:50:45 +0800 Subject: [PATCH 081/194] One click configuration of CI environment (#56817) * One click configuration of CI environment * fix bugs --- tools/windows/build_compile_environment.bat | 187 +++++++++++++++----- 1 file changed, 145 insertions(+), 42 deletions(-) diff --git a/tools/windows/build_compile_environment.bat b/tools/windows/build_compile_environment.bat index fb173442a3319..884cea8ca4cd0 100644 --- a/tools/windows/build_compile_environment.bat +++ b/tools/windows/build_compile_environment.bat @@ -20,29 +20,36 @@ :: Install compile environment for xly CI. :: :: Include: -:: 1. CMake 3.17.0 +:: 1. CMake 3.18.0 :: 2. Git 2.28.0 -:: 3. Python 3.8.3 +:: 3. Python 3.8.3\3.9.7\3.10.0 :: 4. Visual Studio 2017 Community :: 5. CUDA 11.2 :: 6. java jre :: 7. sccache -:: 8. xly agent +:: 8. TensorRT +:: 9. xly agent :: Echo command is not required. @echo off cd /d %~dp0% +SET INSTALL_FLAG=%1 +:: 0. PR_CI_Windows_OPENBLAS +:: 1. PR_CI_Windows_Inference,will install VS2019, CUDA11.2 +:: 2. PR_CI_Windows,CUDA10.2 + + :: ===== start step 0: wget tool ===== :: Download wget for windows when there is not wget tool. -echo ">>>>>>>> step [0/8]: wget tool" +echo ">>>>>>>> step [0/9]: wget tool" wget --help > nul 2> nul || call:install_wget goto cmake :install_wget -echo There is not wget in this PC, will download wget 1.20. -echo Download package from https://eternallybored.org/misc/wget/1.20/64/wget.exe ... -certutil -urlcache -split -f https://eternallybored.org/misc/wget/1.20/64/wget.exe > nul 2> nul +echo There is not wget in this PC, will download wget 1.21.4. +echo Download package from https://eternallybored.org/misc/wget/1.21.4/64/wget.exe ... +certutil -urlcache -split -f https://eternallybored.org/misc/wget/1.21.4/64/wget.exe > nul 2> nul if %errorlevel% == 0 ( echo Download wget tool into %cd% success. ) else ( @@ -53,26 +60,26 @@ goto :eof :: ===== end step 0: wget tool ===== :: ===== start step 1: cmake ===== -:: Download CMake-3.17.0 and add in PATH when it not installed. -:: TODO: limit version >= 3.17.0 +:: Download CMake-3.18.0 and add in PATH when it not installed. +:: TODO: limit version >= 3.18.0 :cmake -echo ">>>>>>>> step [1/8]: CMake 3.17.0" +echo ">>>>>>>> step [1/9]: CMake 3.18.0" cmake --help > nul 2> nul || call :install_cmake goto git :install_cmake -echo There is not cmake in this PC, will install cmake-3.17.0. -echo Download package from https://cmake.org/files/v3.17/cmake-3.17.0-win64-x64.msi ... -wget -O cmake-3.17.0-win64-x64.msi https://cmake.org/files/v3.17/cmake-3.17.0-win64-x64.msi -echo Install cmake-3.17.0 ... +echo There is not cmake in this PC, will install cmake-3.18.0. +echo Download package from https://cmake.org/files/v3.18/cmake-3.18.0-win64-x64.msi ... +wget -O cmake-3.18.0-win64-x64.msi https://cmake.org/files/v3.18/cmake-3.18.0-win64-x64.msi +echo Install cmake-3.18.0 ... :: /passive [silent installation] :: /norestart [do not restart] :: ADD_CMAKE_TO_PATH = System [add CMake to the system PATH for all users] -start /wait cmake-3.17.0-win64-x64.msi /passive /norestart ADD_CMAKE_TO_PATH=System +start /wait cmake-3.18.0-win64-x64.msi /passive /norestart ADD_CMAKE_TO_PATH=System if %errorlevel% == 0 ( - echo Install CMake-3.17.0 success! + echo Install CMake-3.18.0 success! ) else ( - echo Error***** Install Cmake-3.17.0 failed, please re-install it manually. + echo Error***** Install Cmake-3.18.0 failed, please re-install it manually. ) goto :eof :: ===== end step 1: cmake ===== @@ -81,7 +88,7 @@ goto :eof :: Download Git-2.28.0 and add in PATH when it not installed. :: TODO: limit version >= 2.28.0 :git -echo ">>>>>>>> step [2/8]: Git 2.28.0" +echo ">>>>>>>> step [2/9]: Git 2.28.0" git --help > nul 2> nul || call :install_git goto python @@ -103,17 +110,19 @@ goto :eof :: ===== end step 2: Git ===== :: ===== start step 3: Python ===== -:: Download Python-3.8.3 and add in PATH when it not installed. +:: Download Python and add in PATH when it not installed. :: TODO: limit version >= 3.8.3 :python -echo ">>>>>>>> step [3/8]: Python 3.8.3" -python -V 2>&1 | findstr /C:"Python 3.8.3" > nul 2> nul || call :install_python +echo ">>>>>>>> step [3/9]: Python" +where python 2>&1 | findstr /C:"Python38" > nul 2> nul || call :install_python3.8.3 +where python 2>&1 | findstr /C:"Python39" > nul 2> nul || call :install_python3.9.7 +where python 2>&1 | findstr /C:"Python310" > nul 2> nul || call :install_python3.10.0 goto vs -:install_python +:install_python3.8.3 echo There is not Python in this PC, will install Python-3.8.3 -echo Download package from https://paddle-ci.gz.bcebos.com/window_requirement/python-3.8.3-amd64.exe ... -wget -O python-3.8.3-amd64.exe https://paddle-ci.gz.bcebos.com/window_requirement/python-3.8.3-amd64.exe +echo Download package from https://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exe ... +wget -O python-3.8.3-amd64.exe https://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exe echo Install Python-3.8.3 ... :: /passive [silent install] :: InstallAllUsers [add path for all users] @@ -126,16 +135,46 @@ if %errorlevel% == 0 ( echo Error***** Install python-3.8.3 failed, please re-install it manually. ) goto :eof + +:install_python3.9.7 +echo There is not Python in this PC, will install Python-3.9.7 +echo Download package from https://www.python.org/ftp/python/3.9.7/python-3.9.7-amd64.exe ... +wget -O python-3.9.7-amd64.exe https://www.python.org/ftp/python/3.9.7/python-3.9.7-amd64.exe +echo Install Python-3.9.7 ... +start /wait python-3.9.7-amd64.exe /passive InstallAllUsers=1 PrependPath=1 TargetDir=C:\Python39 +if %errorlevel% == 0 ( + echo Install python-3.9.7 success! +) else ( + echo Error***** Install python-3.9.7 failed, please re-install it manually. +) +goto :eof + +:install_python3.10.0 +echo There is not Python in this PC, will install Python-3.10.0 +echo Download package from https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe ... +wget -O python-3.10.0-amd64.exe https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe +echo Install Python-3.10.0 ... +start /wait python-3.10.0-amd64.exe /passive InstallAllUsers=1 PrependPath=1 TargetDir=C:\Python310 +if %errorlevel% == 0 ( + echo Install python-3.10.0 success! +) else ( + echo Error***** Install python-3.10.0 failed, please re-install it manually. +) +goto :eof :: ===== end step 3: Python ===== -:: ===== start step 4: Visual Studio 2017 Community ===== +:: ===== start step 4: Visual Studio Community ===== :: Download Visual Studio 2017 when it not installed. :vs -echo ">>>>>>>> step [4/8]: Visual Studio 2017 " -cmd /C "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" > nul 2> nul || call :install_visual_studio +echo ">>>>>>>> step [4/9]: Visual Studio" +if %INSTALL_FLAG% == 1 ( + cmd /C "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" > nul 2> nul || call :install_visual_studio2019 +) else ( + cmd /C "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" > nul 2> nul || call :install_visual_studio2017 +) goto :cuda -:install_visual_studio +:install_visual_studio2017 echo There is not Visual Studio in this PC, will install VS2017. echo Download package from "https://paddle-ci.gz.bcebos.com/window_requirement/VS2017/vs_Community.exe" wget -O vs_Community.exe "https://paddle-ci.gz.bcebos.com/window_requirement/VS2017/vs_Community.exe" @@ -151,15 +190,57 @@ if %errorlevel% == 0 ( echo Error***** Install Visual Studio 2017 failed, please re-install it manually. ) goto :eof -:: ===== end step 4: Visual Studio 2017 ===== -:: ===== start step 5: CUDA 11 ===== +:install_visual_studio2019 +echo There is not Visual Studio in this PC, will install VS2019. +echo Download package from "https://paddle-ci.gz.bcebos.com/window_requirement/vs_community_2019.exe" +wget -O vs_community_2019.exe "https://paddle-ci.gz.bcebos.com/window_requirement/vs_community_2019.exe" +echo Install Visual Studio 2019 ... +start /wait vs_community_2019.exe --passive --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Workload.Universal --includeRecommended +if %errorlevel% == 0 ( + echo Install Visual Studio 2019 success! +) else ( + echo Error***** Install Visual Studio 2019 failed, please re-install it manually. +) +goto :eof +:: ===== end step 4: Visual Studio Community ===== + +:: ===== start step 5: CUDA ===== :cuda -echo ">>>>>>>> step [5/8]: CUDA 11.2" -cmd /C nvcc --version 2> nul | findstr /C:"11.2" > nul 2> nul || call :install_cuda +echo ">>>>>>>> step [5/9]: CUDA " +if %INSTALL_FLAG% == 1 ( + cmd /C nvcc --version 2> nul | findstr /C:"11.2" > nul 2> nul || call :install_cuda112 +) +if %INSTALL_FLAG% == 2 ( + cmd /C nvcc --version 2> nul | findstr /C:"10.2" > nul 2> nul || call :install_cuda102 +) goto java-jre -:install_cuda +:install_cuda102 +echo There is not CUDA in this PC, will install CUDA-10.2. +echo Download package from "https://paddle-ci.gz.bcebos.com/window_requirement/cuda_10.2.89_441.22_win10.exe" +wget -O cuda_installer.exe "https://paddle-ci.gz.bcebos.com/window_requirement/cuda_10.2.89_441.22_win10.exe" +echo Install CUDA-10.2 ... +:: -s [silent install] +start /wait cuda_installer.exe -s +if %errorlevel% == 0 ( + echo Install CUDA-10.2 success! +) else ( + echo Error***** Install CUDA-10.2 failed, please re-install it manually. + goto :eof +) +del cuda_installer.exe + +echo Download cudnn from "https://paddle-ci.gz.bcebos.com/window_requirement/cudnn-10.2-windows10-x64-v7.6.5.32.zip" +wget -O cudnn-10.2-windows10-x64-v7.6.5.32.zip "https://paddle-ci.gz.bcebos.com/window_requirement/cudnn-10.2-windows10-x64-v7.6.5.32.zip" +tar xf cudnn-10.2-windows10-x64-v7.6.5.32.zip +xcopy /E /Y /R "cuda\bin\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin" +xcopy /E /Y /R "cuda\include\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include" +xcopy /E /Y /R "cuda\lib\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib" +rd /s /q cuda +goto :eof + +:install_cuda112 echo There is not CUDA in this PC, will install CUDA-11.2. echo Download package from "https://paddle-ci.gz.bcebos.com/window_requirement/cuda_11.2.0_460.89_win10.exe" wget -O cuda_installer.exe "https://paddle-ci.gz.bcebos.com/window_requirement/cuda_11.2.0_460.89_win10.exe" @@ -173,7 +254,6 @@ if %errorlevel% == 0 ( goto :eof ) del cuda_installer.exe - echo Download cudnn from "https://paddle-ci.gz.bcebos.com/window_requirement/cudnn-11.2-windows-x64-v8.1.0.77.zip" wget -O cudnn-11.2-windows-x64-v8.1.0.77.zip "https://paddle-ci.gz.bcebos.com/window_requirement/cudnn-11.2-windows-x64-v8.1.0.77.zip" tar xf cudnn-11.2-windows-x64-v8.1.0.77.zip @@ -182,11 +262,11 @@ xcopy /E /Y /R "cuda\include\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\C xcopy /E /Y /R "cuda\lib\*" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\lib" rd /s /q cuda goto :eof -:: ===== end step 5: CUDA 10 ===== +:: ===== end step 5: CUDA ===== :: ===== start step 6: java jre ===== :java-jre -echo ">>>>>>>> step [6/8]: java jre" +echo ">>>>>>>> step [6/9]: java jre" cmd /C java -version > nul 2> nul || call :install_java goto sccache @@ -208,21 +288,44 @@ goto :eof :: ===== start step 7: sccache on windowss ===== :sccache -echo ">>>>>>>> step [7/8]: sccache" +echo ">>>>>>>> step [7/9]: sccache" cmd /C sccache -V > nul 2> nul || call :download_sccache -goto xly-agent +goto tensorrt :download_sccache echo There is not sccache in this PC, will install sccache. echo Download package from https://paddle-ci.gz.bcebos.com/window_requirement/sccache.exe wget -O sccache.exe "https://paddle-ci.gz.bcebos.com/window_requirement/sccache.exe" copy sccache.exe C:\Python38 /Y +goto :eof :: ===== end step 7: sccache on windows ===== -:: ===== start step 8: xly agent ===== +:: ===== start step 8: TensorRT ===== +:tensorrt +echo ">>>>>>>> step [8/9]: TensorRT" +if %INSTALL_FLAG% == 2 ( + call :download_TensorRT_7_0_0_11 +) +if %INSTALL_FLAG% == 1 ( + call :download_TensorRT_8_0_1_6 +) +goto xly-agent + +:download_TensorRT_7_0_0_11 +if not exist TensorRT-7.0.0.11.Windows10.x86_64.cuda-10.2.cudnn7.6.zip wget -O TensorRT-7.0.0.11.Windows10.x86_64.cuda-10.2.cudnn7.6.zip ^ +"https://paddle-ci.gz.bcebos.com/window_requirement/TensorRT-7.0.0.11.Windows10.x86_64.cuda-10.2.cudnn7.6.zip" +goto :eof + +:download_TensorRT_8_0_1_6 +if not exist TTensorRT-8.0.1.6.Windows10.x86_64.cuda-11.3.cudnn8.2.zip wget -O TensorRT-8.0.1.6.Windows10.x86_64.cuda-11.3.cudnn8.2.zip ^ +"https://paddle-ci.gz.bcebos.com/window_requirement/TensorRT-8.0.1.6.Windows10.x86_64.cuda-11.3.cudnn8.2.zip" +goto :eof +:: ===== end step 8: TensorRT ===== + +:: ===== start step 9: xly agent ===== :xly-agent -echo ">>>>>>>> step [8/8]: xly agent" -wget -O agent.jar "https://xly.bce.baidu.com/sa_server/agent/v1/download?version=1.2.8" -:: ===== end step 8: xly agent ===== +echo ">>>>>>>> step [9/9]: xly agent" +if not exist agent.jar wget -O agent.jar "https://xly.bce.baidu.com/sa_server/agent/v1/download?version=1.2.8" +:: ===== end step 9: xly agent ===== pause From e110cbb4d4264b7d2d98a24f52afe34b11be0d2a Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Sat, 2 Sep 2023 08:14:28 +0800 Subject: [PATCH 082/194] delete pd_op.yaml (#56862) --- .../ir/dialect/paddle_dialect/ir/pd_op.yaml | 485 ------------------ 1 file changed, 485 deletions(-) delete mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml deleted file mode 100644 index 942c3b8703c7e..0000000000000 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml +++ /dev/null @@ -1,485 +0,0 @@ -- name: assign_value - inputs: [] - attrs: - - {typename: 'int[]', name: shape} - - {typename: DataType, name: dtype} - - {typename: 'Scalar[]', name: values, data_type: 'std::vector'} - - {typename: Place, name: place, default_value: '{}'} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: AssignValueInferMeta - param: [shape, dtype] - kernel: - func: [assign_value] - param: [shape, dtype, values] - backend: - ordered: true - candidates: [place] - layout: null - data_type: - ordered: false - candidates: [dtype] - to_complex_flag: [false] - dispatch: {assign_value: null} - force_backend: null - inplace: null - view: null - backward: null - -- name: feed - inputs: [] - attrs: - - {typename: str, name: name} - - {typename: int, name: col} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - inplace: null - backward: null - -- name: fetch - inputs: - - typename: Tensor - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: str, name: name} - - {typename: int, name: col} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: UnchangedInferMeta - param: [x] - kernel: - func: [fetch] - param: [x] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - inplace: null - backward: null - -- name: save_combine - inputs: - - typename: Tensor[] - name: X - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: str, name: file_path} - - {typename: bool, name: overwrite} - - {typename: bool, name: save_as_fp16} - - {typename: bool, name: save_to_memory} - outputs: - - {typename: Tensor, name: out, optional: true, intermediate: false} - no_need_buffer: null - data_transform: null - kernel: - func: [save_combine_tensor] - param: [X, file_path, overwrite, save_as_fp16, save_to_memory] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - inplace: null - backward: null - -- name: load_combine - inputs: [] - attrs: - - {typename: str, name: file_path} - - {typename: bool, name: load_as_fp16} - - {typename: bool, name: model_from_memory} - outputs: - - {typename: 'Tensor[]', name: Out, optional: true, intermediate: false} - no_need_buffer: null - data_transform: null - kernel: - func: [load_combine] - param: [file_path, load_as_fp16, model_from_memory] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - inplace: null - backward: null - -- name: share_buffer_ - inputs: - - typename: Tensor[] - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: 'bool[]', name: share_dims_and_dtype, default_value: '{}'} - outputs: - - {typename: 'Tensor[]', name: out, size: x.size(), optional: false, intermediate: false} - - {typename: 'Tensor[]', name: xout, size: x.size(), optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - inplace: null - backward: null - -- name: assert - inputs: - - typename: Tensor - name: cond - optional: false - no_need_buffer: false - data_transform: {} - - typename: Tensor[] - name: data - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: 'int64_t', name: summarize, default_value: '-1'} - outputs: [] - no_need_buffer: null - data_transform: null - kernel: - func: [assert] - param: [cond, data, summarize] - backend: null - layout: null - data_type: - ordered: false - candidates: [cond] - to_complex_flag: [false] - dispatch: {assert: null} - force_backend: null - inplace: null - backward: null - -- name: print - inputs: - - typename: Tensor - name: in - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: 'int', name: first_n} - - {typename: 'str', name: message} - - {typename: 'int', name: summarize} - - {typename: 'bool', name: print_tensor_name, default_value: 'true'} - - {typename: 'bool', name: print_tensor_type, default_value: 'true'} - - {typename: 'bool', name: print_tensor_shape, default_value: 'true'} - - {typename: 'bool', name: print_tensor_layout, default_value: 'true'} - - {typename: 'bool', name: print_tensor_lod, default_value: 'true'} - - {typename: 'str', name: print_phase, default_value: '"BOTH"'} - - {typename: 'bool', name: is_forward, default_value: 'true'} - outputs: - - typename: Tensor - name: out - optional: false - no_need_buffer: false - data_transform: {} - infer_meta: - func: UnchangedInferMeta - param: [in] - kernel: - func: [print_kernel] - param: [in, first_n, message, summarize, print_tensor_name, print_tensor_type, print_tensor_shape, print_tensor_layout, print_tensor_lod, print_phase, is_forward ] - backend: null - layout: null - data_type: null - dispatch: {print: null} - force_backend: null - no_need_buffer: null - data_transform: null - inplace: null - backward: null - -- name: add_n_ - inputs: - - typename: Tensor[] - name: inputs - optional: false - no_need_buffer: false - data_transform: {} - attrs: [] - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: AddNInferMeta - param: [inputs] - kernel: - func: [add_n] - param: [inputs] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - backward: add_n_grad - -- name: add_n_with_kernel - inputs: - - typename: Tensor[] - name: inputs - optional: false - no_need_buffer: false - data_transform: {} - attrs: [] - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: AddNInferMeta - param: [inputs] - kernel: - func: [add_n] - param: [inputs] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - backward: add_n_grad - -- name: write_to_array - inputs: - - typename: Tensor - name: i - optional: false - no_need_buffer: false - data_transform: {} - - typename: Tensor - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: [] - outputs: - - {typename: 'Tensor[]', name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - backward: write_to_array_grad - -- name: lod_array_length - inputs: - - typename: Tensor[] - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: [] - outputs: - - {typename: 'Tensor', name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - -- name: embedding_grad_sparse - inputs: - - typename: Tensor - name: x - optional: false - no_need_buffer: false - data_transform: {} - - typename: Tensor - name: weight - optional: false - no_need_buffer: false - data_transform: {} - - typename: Tensor - name: out_grad - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: int64_t, name: padding_idx, default_value: '-1'} - - {typename: bool, name: sparse, default_value: 'false'} - outputs: - - {typename: SelectedRows, name: weight_grad, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: EmbeddingGradSparseInferMeta - param: [weight] - kernel: - func: [embedding_sparse_grad] - param: [x, weight, out_grad, padding_idx, sparse] - backend: null - layout: null - data_type: - ordered: false - candidates: [weight] - to_complex_flag: [false] - dispatch: {embedding_sparse_grad: null} - force_backend: null - inplace: null - view: null - backward: null - -- name: shadow_feed - inputs: - - typename: Tensor - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: [] - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: UnchangedInferMeta - param: [x] - kernel: - func: [shadow_feed] - param: [x] - backend: null - layout: null - data_type: null - dispatch: {fetch: null} - force_backend: null - inplace: null - backward: null - -- name: send_v2 - inputs: - - typename: Tensor - name: x - optional: false - no_need_buffer: false - data_transform: {} - attrs: - - {typename: int, name: ring_id, default_value: '0'} - - {typename: int, name: peer, default_value: '0'} - - {typename: bool, name: use_calc_stream, default_value: 'false'} - - {typename: bool, name: dynamic_shape, default_value: 'false'} - outputs: [] - no_need_buffer: null - data_transform: null - infer_meta: - func: SendV2InferMeta - param: [peer, ring_id] - kernel: - func: [send_v2] - param: [x, ring_id, dynamic_shape, peer, use_calc_stream] - backend: null - layout: null - data_type: null - dispatch: {send_v2: null} - force_backend: null - inplace: null - view: null - backward: null - -- name: recv_v2 - inputs: [] - attrs: - - {typename: 'int[]', name: out_shape, default_value: '{}'} - - {typename: DataType, name: dtype, default_value: 'DataType::FLOAT32'} - - {typename: int, name: peer, default_value: '0'} - - {typename: int, name: ring_id, default_value: '0'} - - {typename: bool, name: use_calc_stream, default_value: 'false'} - - {typename: bool, name: dynamic_shape, default_value: 'false'} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - no_need_buffer: null - data_transform: null - infer_meta: - func: RecvV2InferMeta - param: [peer, dtype, out_shape] - kernel: - func: [recv_v2] - param: [ring_id, dynamic_shape, peer, out_shape, dtype, use_calc_stream] - backend: null - layout: null - data_type: - ordered: false - candidates: [dtype] - to_complex_flag: [false] - dispatch: {recv_v2: null} - force_backend: null - inplace: null - view: null - -- name : set_value - inputs: - - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } - attrs: - - {typename: 'int64_t[]', name: starts} - - {typename: 'int64_t[]', name: ends} - - {typename: 'int64_t[]', name: steps} - - {typename: 'int64_t[]', name: axes} - - {typename: 'int64_t[]', name: decrease_axes} - - {typename: 'int64_t[]', name: none_axes} - - {typename: 'int64_t[]', name: shape} - - {typename: 'Scalar[]', name: values} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - infer_meta: - func: SetValueInferMeta - param: [x] - kernel: - func: [set_value] - param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values] - inplace: {out: x} - backward: set_value_grad - -- name : set_value_with_tensor - inputs: - - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: values, optional: false, no_need_buffer: false, data_transform: {} } - attrs: - - {typename: 'int64_t[]', name: starts} - - {typename: 'int64_t[]', name: ends} - - {typename: 'int64_t[]', name: steps} - - {typename: 'int64_t[]', name: axes} - - {typename: 'int64_t[]', name: decrease_axes} - - {typename: 'int64_t[]', name: none_axes} - outputs: - - {typename: Tensor, name: out, optional: false, intermediate: false} - infer_meta: - func: SetValueInferMeta - param: [x] - kernel: - func: [set_value_with_tensor] - param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] - inplace: {out: x} - backward: set_value_grad - - -- name : set_value_grad - inputs: - - {typename: Tensor, name: out_grad, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: values, optional: false, no_need_buffer: false, data_transform: {} } - attrs: - - {typename: 'int64_t[]', name: starts} - - {typename: 'int64_t[]', name: ends} - - {typename: 'int64_t[]', name: steps} - - {typename: 'int64_t[]', name: axes} - - {typename: 'int64_t[]', name: decrease_axes} - - {typename: 'int64_t[]', name: none_axes} - outputs: - - {typename: Tensor, name: x_grad, optional: false, intermediate: false} - - {typename: Tensor, name: values_grad, optional: false, intermediate: false} - infer_meta: - func: SetValueGradInferMeta - param: [out_grad, values] - kernel: - func: [set_value_grad] - param: [out_grad, starts, ends, steps, axes, decrease_axes, none_axes] - inplace: null - backward: null From fa75ebeb87960be6d501b54ad4283a4e260e6dfa Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Sat, 2 Sep 2023 10:03:18 +0800 Subject: [PATCH 083/194] link C++ tests to libpaddle.so (#56829) * link C++ tests to libpaddle.so except windows * fix compile kill-9 bug * fix compile kill-9 bug * fix compile kill-9 bug * fix compile kill-9 bug --- .../fleet_executor/test/CMakeLists.txt | 152 ++++++++++++++---- test/cpp/fluid/mkldnn/CMakeLists.txt | 32 +--- 2 files changed, 127 insertions(+), 57 deletions(-) diff --git a/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt b/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt index 5a08ecb857169..e3eb700e91bf9 100644 --- a/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt +++ b/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt @@ -1,62 +1,154 @@ +get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME) set_source_files_properties( interceptor_ping_pong_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old(interceptor_ping_pong_test SRCS interceptor_ping_pong_test.cc DEPS - fleet_executor ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old(interceptor_ping_pong_test SRCS interceptor_ping_pong_test.cc + DEPS fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + interceptor_ping_pong_test + SRCS + interceptor_ping_pong_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( compute_interceptor_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old(compute_interceptor_test SRCS compute_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) + +if(WIN32 AND WITH_TESTING) + cc_test_old(compute_interceptor_test SRCS compute_interceptor_test.cc DEPS + fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + compute_interceptor_test + SRCS + compute_interceptor_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( source_interceptor_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old(source_interceptor_test SRCS source_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old(source_interceptor_test SRCS source_interceptor_test.cc DEPS + fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + source_interceptor_test + SRCS + source_interceptor_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( sink_interceptor_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old(sink_interceptor_test SRCS sink_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old(sink_interceptor_test SRCS sink_interceptor_test.cc DEPS + fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + sink_interceptor_test + SRCS + sink_interceptor_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( interceptor_pipeline_short_path_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old( - interceptor_pipeline_short_path_test SRCS - interceptor_pipeline_short_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old( + interceptor_pipeline_short_path_test SRCS + interceptor_pipeline_short_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + interceptor_pipeline_short_path_test + SRCS + interceptor_pipeline_short_path_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( interceptor_pipeline_long_path_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old( - interceptor_pipeline_long_path_test SRCS - interceptor_pipeline_long_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old( + interceptor_pipeline_long_path_test SRCS + interceptor_pipeline_long_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +else() + cc_test_old( + interceptor_pipeline_long_path_test + SRCS + interceptor_pipeline_long_path_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) +endif() set_source_files_properties( compute_interceptor_run_op_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -cc_test_old( - compute_interceptor_run_op_test - SRCS - compute_interceptor_run_op_test.cc - DEPS - fleet_executor - naive_executor - fill_constant_op - op_registry - elementwise_add_op - scope - device_context - ${BRPC_DEPS}) +if(WIN32 AND WITH_TESTING) + cc_test_old( + compute_interceptor_run_op_test + SRCS + compute_interceptor_run_op_test.cc + DEPS + fleet_executor + naive_executor + fill_constant_op + op_registry + elementwise_add_op + scope + device_context + ${BRPC_DEPS}) +else() + cc_test_old( + compute_interceptor_run_op_test + SRCS + compute_interceptor_run_op_test.cc + DEPS + ${paddle_lib} + python + fleet_executor + fill_constant_op + elementwise_add_op) +endif() if(WITH_DISTRIBUTE AND NOT WITH_PSLIB) set_source_files_properties( interceptor_ping_pong_with_brpc_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) - cc_test_old( - interceptor_ping_pong_with_brpc_test SRCS - interceptor_ping_pong_with_brpc_test.cc DEPS fleet_executor ${BRPC_DEPS}) + if(WIN32 AND WITH_TESTING) + cc_test_old( + interceptor_ping_pong_with_brpc_test SRCS + interceptor_ping_pong_with_brpc_test.cc DEPS fleet_executor ${BRPC_DEPS}) + else() + cc_test_old( + interceptor_ping_pong_with_brpc_test + SRCS + interceptor_ping_pong_with_brpc_test.cc + DEPS + ${paddle_lib} + python + fleet_executor) + endif() endif() diff --git a/test/cpp/fluid/mkldnn/CMakeLists.txt b/test/cpp/fluid/mkldnn/CMakeLists.txt index 9144b500a3924..8a04637e5d705 100644 --- a/test/cpp/fluid/mkldnn/CMakeLists.txt +++ b/test/cpp/fluid/mkldnn/CMakeLists.txt @@ -1,3 +1,4 @@ +get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME) cc_test( test_mkldnn_op_inplace SRCS test_mkldnn_op_inplace.cc @@ -41,33 +42,7 @@ cc_test( test_mkldnn_caching SRCS test_mkldnn_caching.cc DEPS ${TEST_MKLDNN_CACHING_DEPS}) -if(WITH_CINN) - cc_test_old( - test_mkldnn_op_nhwc - SRCS - test_mkldnn_op_nhwc.cc - DEPS - fleet_executor - conditional_block_op - standalone_executor - executor - recurrent_op_helper - cinn_compiler - recurrent_op - op_registry - generated_static_op - crop_op - activation_op - generated_op - generated_static_op - phi - transpose_op - fused_transpose_op - scope - device_context - enforce - python) -else() +if(WIN32 AND WITH_TESTING) cc_test_old( test_mkldnn_op_nhwc SRCS @@ -91,4 +66,7 @@ else() scope device_context enforce) +else() + cc_test_old(test_mkldnn_op_nhwc SRCS test_mkldnn_op_nhwc.cc DEPS + ${paddle_lib} python) endif() From 061bb9d50c5a8654d91289fd87059b4c0c3102de Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Sat, 2 Sep 2023 10:41:52 +0800 Subject: [PATCH 084/194] [NewIR]Refine and Split CINN Dilact directory (#56805) * [NewIR]Refine CINN Dilact directory * fix conflict * fix deps * fix unittest deps --- paddle/cinn/hlir/dialect/CMakeLists.txt | 69 +------------------ .../hlir/dialect/cinn_dialect/CMakeLists.txt | 1 + .../dialect/cinn_dialect/ir/CMakeLists.txt | 61 ++++++++++++++++ .../{ => cinn_dialect/ir}/cinn_dialect.cc | 6 +- .../{ => cinn_dialect/ir}/cinn_dialect.h | 0 .../{ => cinn_dialect/ir}/cinn_ops.yaml | 0 .../dialect/generated/cinn_ops.parsed.yaml | 31 +++++++++ .../dialect/runtime_dialect/CMakeLists.txt | 1 + .../dialect/runtime_dialect/ir/CMakeLists.txt | 4 ++ .../{ => runtime_dialect/ir}/jit_kernel_op.cc | 2 +- .../{ => runtime_dialect/ir}/jit_kernel_op.h | 0 .../ir}/runtime_dialect.cc | 4 +- .../ir}/runtime_dialect.h | 0 .../cinn/hlir/framework/convert_to_dialect.cc | 4 +- .../new_executor/instruction/CMakeLists.txt | 2 +- .../instruction/cinn_jit_instruction.cc | 4 +- test/cpp/ir/cinn/CMakeLists.txt | 1 + test/cpp/ir/cinn/jit_instruction_test.cc | 4 +- test/cpp/ir/cinn/new_ir_compiler_test.cc | 4 +- 19 files changed, 116 insertions(+), 82 deletions(-) create mode 100644 paddle/cinn/hlir/dialect/cinn_dialect/CMakeLists.txt create mode 100644 paddle/cinn/hlir/dialect/cinn_dialect/ir/CMakeLists.txt rename paddle/cinn/hlir/dialect/{ => cinn_dialect/ir}/cinn_dialect.cc (86%) rename paddle/cinn/hlir/dialect/{ => cinn_dialect/ir}/cinn_dialect.h (100%) rename paddle/cinn/hlir/dialect/{ => cinn_dialect/ir}/cinn_ops.yaml (100%) create mode 100644 paddle/cinn/hlir/dialect/generated/cinn_ops.parsed.yaml create mode 100644 paddle/cinn/hlir/dialect/runtime_dialect/CMakeLists.txt create mode 100644 paddle/cinn/hlir/dialect/runtime_dialect/ir/CMakeLists.txt rename paddle/cinn/hlir/dialect/{ => runtime_dialect/ir}/jit_kernel_op.cc (95%) rename paddle/cinn/hlir/dialect/{ => runtime_dialect/ir}/jit_kernel_op.h (100%) rename paddle/cinn/hlir/dialect/{ => runtime_dialect/ir}/runtime_dialect.cc (87%) rename paddle/cinn/hlir/dialect/{ => runtime_dialect/ir}/runtime_dialect.h (100%) diff --git a/paddle/cinn/hlir/dialect/CMakeLists.txt b/paddle/cinn/hlir/dialect/CMakeLists.txt index 68798ede2e552..5d30ab6d34504 100755 --- a/paddle/cinn/hlir/dialect/CMakeLists.txt +++ b/paddle/cinn/hlir/dialect/CMakeLists.txt @@ -1,67 +1,2 @@ -# TODO(Aurelius84): new_ir_compiler depends on pd_dialect and could -# not found under CINN_ONLY mode -if(NOT CINN_ONLY) - set(CINN_DIALECT_SOURCE_DIR "${PADDLE_SOURCE_DIR}/paddle/cinn/hlir/dialect") - set(CINN_DIALECT_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/cinn/hlir/dialect") - - # Generate cinn_dialect files defining op using op_gen_file - set(cinn_op_gen_parsed_yaml_file - ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parse_op.py) - - set(cinn_op_gen_file - ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/op_generator/op_gen.py) - - set(cinn_op_compat_yaml_file - ${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml) - - set(cinn_op_yaml_file - ${PADDLE_SOURCE_DIR}/paddle/cinn/hlir/dialect/cinn_ops.yaml) - - set(parsed_op_dir ${PADDLE_SOURCE_DIR}/paddle/cinn/hlir/dialect/generated) - - set(cinn_op_parsed_yaml_file ${parsed_op_dir}/cinn_ops.parsed.yaml) - - set(cinn_op_parsed_yaml_files ${cinn_op_parsed_yaml_file}) - - set(cinn_op_namespace cinn,dialect) - set(cinn_dialect_name cinn) - set(cinn_op_header_file ${CINN_DIALECT_BINARY_DIR}/cinn_op.h) - set(cinn_op_source_file ${CINN_DIALECT_BINARY_DIR}/cinn_op.cc) - set(cinn_op_header_file_tmp ${cinn_op_header_file}.tmp) - set(cinn_op_source_file_tmp ${cinn_op_source_file}.tmp) - - add_custom_command( - OUTPUT ${cinn_op_parsed_yaml_file} - COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_op_dir} - COMMAND ${PYTHON_EXECUTABLE} ${cinn_op_gen_parsed_yaml_file} --op_yaml_path - ${cinn_op_yaml_file} --output_path ${cinn_op_parsed_yaml_file} - VERBATIM) - - add_custom_command( - OUTPUT ${cinn_op_header_file} ${cinn_op_source_file} - COMMAND - ${PYTHON_EXECUTABLE} ${cinn_op_gen_file} --op_yaml_files - ${cinn_op_parsed_yaml_files} --op_compat_yaml_file - ${cinn_op_compat_yaml_file} --namespaces ${cinn_op_namespace} - --dialect_name ${cinn_dialect_name} --op_def_h_file - ${cinn_op_header_file_tmp} --op_def_cc_file ${cinn_op_source_file_tmp} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cinn_op_header_file_tmp} - ${cinn_op_header_file} - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cinn_op_source_file_tmp} - ${cinn_op_source_file} - DEPENDS ${cinn_op_gen_file} ${cinn_op_parsed_yaml_file} - ${cinn_op_compat_yaml_file} - VERBATIM) - - cinn_cc_library( - cinn_dialect - SRCS - runtime_dialect.cc - jit_kernel_op.cc - cinn_dialect.cc - cinn_op.cc - DEPS - pd_dialect) - - target_include_directories(cinn_dialect PRIVATE ${CINN_DIALECT_BINARY_DIR}) -endif() +add_subdirectory(cinn_dialect) +add_subdirectory(runtime_dialect) diff --git a/paddle/cinn/hlir/dialect/cinn_dialect/CMakeLists.txt b/paddle/cinn/hlir/dialect/cinn_dialect/CMakeLists.txt new file mode 100644 index 0000000000000..dd1b708ce9fe4 --- /dev/null +++ b/paddle/cinn/hlir/dialect/cinn_dialect/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(ir) diff --git a/paddle/cinn/hlir/dialect/cinn_dialect/ir/CMakeLists.txt b/paddle/cinn/hlir/dialect/cinn_dialect/ir/CMakeLists.txt new file mode 100644 index 0000000000000..5fa53f74cc4a9 --- /dev/null +++ b/paddle/cinn/hlir/dialect/cinn_dialect/ir/CMakeLists.txt @@ -0,0 +1,61 @@ +# TODO(Aurelius84): new_ir_compiler depends on pd_dialect and could +# not found under CINN_ONLY mode +if(NOT CINN_ONLY) + set(CINN_DIALECT_BINARY_DIR + "${PADDLE_BINARY_DIR}/paddle/cinn/hlir/dialect/cinn_dialect/ir") + + # Generate cinn_dialect files defining op using op_gen_file + set(cinn_op_gen_parsed_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/generator/parse_op.py) + + set(cinn_op_gen_file + ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/op_generator/op_gen.py) + + set(cinn_op_compat_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/phi/api/yaml/op_compat.yaml) + + set(cinn_op_yaml_file + ${PADDLE_SOURCE_DIR}/paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_ops.yaml + ) + + set(parsed_op_dir ${PADDLE_SOURCE_DIR}/paddle/cinn/hlir/dialect/generated) + + set(cinn_op_parsed_yaml_file ${parsed_op_dir}/cinn_ops.parsed.yaml) + + set(cinn_op_parsed_yaml_files ${cinn_op_parsed_yaml_file}) + + set(cinn_op_namespace cinn,dialect) + set(cinn_dialect_name cinn) + set(cinn_op_header_file ${CINN_DIALECT_BINARY_DIR}/cinn_op.h) + set(cinn_op_source_file ${CINN_DIALECT_BINARY_DIR}/cinn_op.cc) + set(cinn_op_header_file_tmp ${cinn_op_header_file}.tmp) + set(cinn_op_source_file_tmp ${cinn_op_source_file}.tmp) + + add_custom_command( + OUTPUT ${cinn_op_parsed_yaml_file} + COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_op_dir} + COMMAND ${PYTHON_EXECUTABLE} ${cinn_op_gen_parsed_yaml_file} --op_yaml_path + ${cinn_op_yaml_file} --output_path ${cinn_op_parsed_yaml_file} + VERBATIM) + + add_custom_command( + OUTPUT ${cinn_op_header_file} ${cinn_op_source_file} + COMMAND + ${PYTHON_EXECUTABLE} ${cinn_op_gen_file} --op_yaml_files + ${cinn_op_parsed_yaml_files} --op_compat_yaml_file + ${cinn_op_compat_yaml_file} --namespaces ${cinn_op_namespace} + --dialect_name ${cinn_dialect_name} --op_def_h_file + ${cinn_op_header_file_tmp} --op_def_cc_file ${cinn_op_source_file_tmp} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cinn_op_header_file_tmp} + ${cinn_op_header_file} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${cinn_op_source_file_tmp} + ${cinn_op_source_file} + DEPENDS ${cinn_op_gen_file} ${cinn_op_parsed_yaml_file} + ${cinn_op_compat_yaml_file} + VERBATIM) + + cinn_cc_library(cinn_dialect SRCS cinn_dialect.cc ${cinn_op_source_file} DEPS + pd_dialect) + + target_include_directories(cinn_dialect PRIVATE ${CINN_DIALECT_BINARY_DIR}) +endif() diff --git a/paddle/cinn/hlir/dialect/cinn_dialect.cc b/paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.cc similarity index 86% rename from paddle/cinn/hlir/dialect/cinn_dialect.cc rename to paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.cc index 9bc6c6ff8c9c6..9e8ccfb6492e4 100644 --- a/paddle/cinn/hlir/dialect/cinn_dialect.cc +++ b/paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.cc @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/cinn/hlir/dialect/cinn_dialect.h" +#include "paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.h" // NOTE(chenxi67): File cinn_op.h is generated by op_gen.py, see details in // paddle/cinn/hlir/dialect/CMakeLists.txt. -#include "paddle/cinn/hlir/dialect/cinn_op.h" +#include "paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_op.h" namespace cinn { namespace dialect { @@ -32,7 +32,7 @@ void CinnDialect::initialize() { // paddle/cinn/hlir/dialect/CMakeLists.txt. RegisterOps< #define GET_OP_LIST -#include "paddle/cinn/hlir/dialect/cinn_op.h" // NOLINT +#include "paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_op.h" // NOLINT >(); } diff --git a/paddle/cinn/hlir/dialect/cinn_dialect.h b/paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.h similarity index 100% rename from paddle/cinn/hlir/dialect/cinn_dialect.h rename to paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_dialect.h diff --git a/paddle/cinn/hlir/dialect/cinn_ops.yaml b/paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_ops.yaml similarity index 100% rename from paddle/cinn/hlir/dialect/cinn_ops.yaml rename to paddle/cinn/hlir/dialect/cinn_dialect/ir/cinn_ops.yaml diff --git a/paddle/cinn/hlir/dialect/generated/cinn_ops.parsed.yaml b/paddle/cinn/hlir/dialect/generated/cinn_ops.parsed.yaml new file mode 100644 index 0000000000000..b345bb699084e --- /dev/null +++ b/paddle/cinn/hlir/dialect/generated/cinn_ops.parsed.yaml @@ -0,0 +1,31 @@ +- name: add + inputs: + - typename: Tensor + name: x + optional: false + no_need_buffer: false + data_transform: {} + - typename: Tensor + name: y + optional: false + no_need_buffer: false + data_transform: {} + attrs: [] + outputs: + - {typename: Tensor, name: out, optional: false, intermediate: false} + no_need_buffer: null + data_transform: null + infer_meta: + func: ElementwiseInferMeta + param: [x, y] + kernel: + func: [add] + param: [x, y] + backend: null + layout: null + data_type: null + dispatch: {add: null} + force_backend: null + inplace: {out: x} + view: null + backward: null diff --git a/paddle/cinn/hlir/dialect/runtime_dialect/CMakeLists.txt b/paddle/cinn/hlir/dialect/runtime_dialect/CMakeLists.txt new file mode 100644 index 0000000000000..dd1b708ce9fe4 --- /dev/null +++ b/paddle/cinn/hlir/dialect/runtime_dialect/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(ir) diff --git a/paddle/cinn/hlir/dialect/runtime_dialect/ir/CMakeLists.txt b/paddle/cinn/hlir/dialect/runtime_dialect/ir/CMakeLists.txt new file mode 100644 index 0000000000000..1df80a5bb3f75 --- /dev/null +++ b/paddle/cinn/hlir/dialect/runtime_dialect/ir/CMakeLists.txt @@ -0,0 +1,4 @@ +if(NOT CINN_ONLY) + cinn_cc_library(runtime_dialect SRCS runtime_dialect.cc jit_kernel_op.cc DEPS + ir_core) +endif() diff --git a/paddle/cinn/hlir/dialect/jit_kernel_op.cc b/paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.cc similarity index 95% rename from paddle/cinn/hlir/dialect/jit_kernel_op.cc rename to paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.cc index dc25665e09325..49e3685a8475a 100644 --- a/paddle/cinn/hlir/dialect/jit_kernel_op.cc +++ b/paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/enforce.h" diff --git a/paddle/cinn/hlir/dialect/jit_kernel_op.h b/paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h similarity index 100% rename from paddle/cinn/hlir/dialect/jit_kernel_op.h rename to paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h diff --git a/paddle/cinn/hlir/dialect/runtime_dialect.cc b/paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.cc similarity index 87% rename from paddle/cinn/hlir/dialect/runtime_dialect.cc rename to paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.cc index c3c90be703ffa..c21d21f11213e 100644 --- a/paddle/cinn/hlir/dialect/runtime_dialect.cc +++ b/paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.cc @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/cinn/hlir/dialect/runtime_dialect.h" -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" namespace cinn { namespace dialect { diff --git a/paddle/cinn/hlir/dialect/runtime_dialect.h b/paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h similarity index 100% rename from paddle/cinn/hlir/dialect/runtime_dialect.h rename to paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h diff --git a/paddle/cinn/hlir/framework/convert_to_dialect.cc b/paddle/cinn/hlir/framework/convert_to_dialect.cc index 63a1dd247a62e..306e27dc1fea5 100644 --- a/paddle/cinn/hlir/framework/convert_to_dialect.cc +++ b/paddle/cinn/hlir/framework/convert_to_dialect.cc @@ -17,8 +17,8 @@ #include #include -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" -#include "paddle/cinn/hlir/dialect/runtime_dialect.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h" #include "paddle/cinn/hlir/framework/program.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/program.h" diff --git a/paddle/fluid/framework/new_executor/instruction/CMakeLists.txt b/paddle/fluid/framework/new_executor/instruction/CMakeLists.txt index 17c8f8cd7626d..8a9247859b85f 100644 --- a/paddle/fluid/framework/new_executor/instruction/CMakeLists.txt +++ b/paddle/fluid/framework/new_executor/instruction/CMakeLists.txt @@ -8,5 +8,5 @@ if(WITH_CINN AND NOT CINN_ONLY) cc_library( cinn_jit_instruction NOT_FOR_INFER SRCS cinn_jit_instruction.cc - DEPS phi cinnapi cinn_dialect) + DEPS phi cinnapi cinn_dialect runtime_dialect) endif() diff --git a/paddle/fluid/framework/new_executor/instruction/cinn_jit_instruction.cc b/paddle/fluid/framework/new_executor/instruction/cinn_jit_instruction.cc index f0dec72fc2987..d56ccc7b7ba6b 100644 --- a/paddle/fluid/framework/new_executor/instruction/cinn_jit_instruction.cc +++ b/paddle/fluid/framework/new_executor/instruction/cinn_jit_instruction.cc @@ -14,8 +14,8 @@ #include "paddle/fluid/framework/new_executor/instruction/cinn_jit_instruction.h" -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" -#include "paddle/cinn/hlir/dialect/runtime_dialect.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h" #include "paddle/cinn/hlir/framework/instruction.h" #include "paddle/fluid/framework/paddle2cinn/transform_type.h" diff --git a/test/cpp/ir/cinn/CMakeLists.txt b/test/cpp/ir/cinn/CMakeLists.txt index 20a3c18b100d3..360b6fc29ac81 100644 --- a/test/cpp/ir/cinn/CMakeLists.txt +++ b/test/cpp/ir/cinn/CMakeLists.txt @@ -6,6 +6,7 @@ if(WITH_TESTING AND WITH_CINN) DEPS new_ir_compiler convert_to_dialect + runtime_dialect ir phi gtest diff --git a/test/cpp/ir/cinn/jit_instruction_test.cc b/test/cpp/ir/cinn/jit_instruction_test.cc index b7088ef448e7f..9b6f5b2c20800 100644 --- a/test/cpp/ir/cinn/jit_instruction_test.cc +++ b/test/cpp/ir/cinn/jit_instruction_test.cc @@ -27,8 +27,8 @@ #include "paddle/ir/core/ir_context.h" #include "paddle/ir/core/program.h" -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" -#include "paddle/cinn/hlir/dialect/runtime_dialect.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h" #include "paddle/cinn/hlir/framework/convert_to_dialect.h" #include "paddle/cinn/hlir/framework/new_ir_compiler.h" #include "paddle/cinn/utils/data_util.h" diff --git a/test/cpp/ir/cinn/new_ir_compiler_test.cc b/test/cpp/ir/cinn/new_ir_compiler_test.cc index 4b55a71f8e9ac..1ea7a455e1217 100644 --- a/test/cpp/ir/cinn/new_ir_compiler_test.cc +++ b/test/cpp/ir/cinn/new_ir_compiler_test.cc @@ -26,8 +26,8 @@ #include "paddle/cinn/utils/data_util.h" -#include "paddle/cinn/hlir/dialect/jit_kernel_op.h" -#include "paddle/cinn/hlir/dialect/runtime_dialect.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/jit_kernel_op.h" +#include "paddle/cinn/hlir/dialect/runtime_dialect/ir/runtime_dialect.h" #include "paddle/cinn/hlir/framework/convert_to_dialect.h" #include "paddle/cinn/hlir/framework/new_ir_compiler.h" From d74bfefe3cd5fbc606351361b05bfe2de975421e Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 2 Sep 2023 19:54:42 +0800 Subject: [PATCH 085/194] polish code of pass and executor (#56886) * polish code of pass and executor * update ut --- .../new_executor/program_interpreter.cc | 74 ++++++++++--------- paddle/fluid/pybind/ir.cc | 2 +- .../transforms/dead_code_elimination_pass.cc | 2 +- test/ir/new_ir/test_pass_manager.py | 3 +- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/paddle/fluid/framework/new_executor/program_interpreter.cc b/paddle/fluid/framework/new_executor/program_interpreter.cc index c94a326f69842..43e5301476d9b 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.cc +++ b/paddle/fluid/framework/new_executor/program_interpreter.cc @@ -123,9 +123,8 @@ void ProgramInterpreter::RunImpl() { #endif } -FetchList ProgramInterpreter::Run( - const std::vector& feed_names, - const std::vector& feed_tensors) { +FetchList ProgramInterpreter::Run(const std::vector& feed_names, + bool need_fetch) { SetDeviceId(place_); CheckCUDAGraphBeforeRun(feed_names); @@ -133,10 +132,32 @@ FetchList ProgramInterpreter::Run( platform::AttachPointerHashToMKLDNNKey(this, place_); #endif - bool is_build = is_build_; - Prepare(feed_names, feed_tensors, is_build); + if (!is_build_) { + LOG_FIRST_N(INFO, 1) << "New Executor is Running."; + paddle::framework::interpreter::BuildVariableScope( + block_, execution_config_, &var_scope_); - if (is_build) { + std::vector op_func_nodes; + paddle::framework::interpreter::BuildOpFuncList( + place_, + block_, + execution_config_.skip_gc_vars, + &op_func_nodes, + &var_scope_, + execution_config_, + HasLocalScope(), + static_build_); + SetFeedVarsInplaceSkip(feed_names); + // convert vec func_list to graph + Convert(&op_func_nodes); + UpdateSyncOpNum(); + if (static_build_) { + VLOG(4) << "RUN impl"; + RunImpl(); + } + is_build_ = true; + is_shared_results_build_ = true; + } else { RunImpl(); } @@ -145,8 +166,10 @@ FetchList ProgramInterpreter::Run( } // return Fetch Tensors - auto* fetch_var = local_scope_->FindVar(interpreter::kFetchVarName); - if (fetch_var) { + Scope* inner_scope = + HasLocalScope() ? local_scope_ : var_scope_.GetMutableScope(); + auto* fetch_var = inner_scope->FindVar(interpreter::kFetchVarName); + if (fetch_var && need_fetch) { auto fetch_list = std::move(*fetch_var->GetMutable()); #ifdef PADDLE_WITH_CUDA if (platform::IsCUDAGraphCapturing()) { @@ -162,8 +185,9 @@ FetchList ProgramInterpreter::Run( } } -FetchList ProgramInterpreter::Run(const std::vector& feed_names, - bool need_fetch) { +FetchList ProgramInterpreter::Run( + const std::vector& feed_names, + const std::vector& feed_tensors) { SetDeviceId(place_); CheckCUDAGraphBeforeRun(feed_names); @@ -171,32 +195,10 @@ FetchList ProgramInterpreter::Run(const std::vector& feed_names, platform::AttachPointerHashToMKLDNNKey(this, place_); #endif - if (!is_build_) { - LOG_FIRST_N(INFO, 1) << "New Executor is Running."; - paddle::framework::interpreter::BuildVariableScope( - block_, execution_config_, &var_scope_); + bool is_build = is_build_; + Prepare(feed_names, feed_tensors, is_build); - std::vector op_func_nodes; - paddle::framework::interpreter::BuildOpFuncList( - place_, - block_, - execution_config_.skip_gc_vars, - &op_func_nodes, - &var_scope_, - execution_config_, - HasLocalScope(), - static_build_); - SetFeedVarsInplaceSkip(feed_names); - // convert vec func_list to graph - Convert(&op_func_nodes); - UpdateSyncOpNum(); - if (static_build_) { - VLOG(4) << "RUN impl"; - RunImpl(); - } - is_build_ = true; - is_shared_results_build_ = true; - } else { + if (is_build) { RunImpl(); } @@ -208,7 +210,7 @@ FetchList ProgramInterpreter::Run(const std::vector& feed_names, Scope* inner_scope = HasLocalScope() ? local_scope_ : var_scope_.GetMutableScope(); auto* fetch_var = inner_scope->FindVar(interpreter::kFetchVarName); - if (fetch_var && need_fetch) { + if (fetch_var) { auto fetch_list = std::move(*fetch_var->GetMutable()); #ifdef PADDLE_WITH_CUDA if (platform::IsCUDAGraphCapturing()) { diff --git a/paddle/fluid/pybind/ir.cc b/paddle/fluid/pybind/ir.cc index 675e6f2acd25d..67da2ba77dbfb 100644 --- a/paddle/fluid/pybind/ir.cc +++ b/paddle/fluid/pybind/ir.cc @@ -507,7 +507,7 @@ void BindPassManager(pybind11::module *m) { }, py::arg("opt_level") = 2) .def("add_pass", - [](PassManager &self, std::string pass_name) { + [](PassManager &self, const std::string &pass_name) { self.AddPass( std::move(ir::PassRegistry::Instance().Get(pass_name))); }) diff --git a/paddle/ir/transforms/dead_code_elimination_pass.cc b/paddle/ir/transforms/dead_code_elimination_pass.cc index d56b83b844680..461ab9c6708a2 100644 --- a/paddle/ir/transforms/dead_code_elimination_pass.cc +++ b/paddle/ir/transforms/dead_code_elimination_pass.cc @@ -26,7 +26,7 @@ namespace { // Now just a naive implementation. class DeadCodeEliminationPass : public ir::Pass { public: - DeadCodeEliminationPass() : ir::Pass("DeadCodeEliminationPass", 0) {} + DeadCodeEliminationPass() : ir::Pass("dead_code_elimination", 0) {} void Run(ir::Operation *op) override { auto module_op = op->dyn_cast(); diff --git a/test/ir/new_ir/test_pass_manager.py b/test/ir/new_ir/test_pass_manager.py index 2f31e945f31f4..761baaea13be8 100644 --- a/test/ir/new_ir/test_pass_manager.py +++ b/test/ir/new_ir/test_pass_manager.py @@ -56,8 +56,7 @@ def test_op(self): pm.run(new_program) op_names = [op.name() for op in new_program.block().ops] # print(op_names) - # TODO(zhiqiu): unify the name of pass - self.assertEqual(pm.passes(), ['DeadCodeEliminationPass']) + self.assertEqual(pm.passes(), ['dead_code_elimination']) self.assertFalse(pm.empty()) self.assertTrue( 'pd.uniform' not in op_names From e2af9d569af56ae3b450b633fc5fedade3c4299e Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Sun, 3 Sep 2023 12:55:59 +0800 Subject: [PATCH 086/194] =?UTF-8?q?=E3=80=90New=20IR=E3=80=91New=20ir=20op?= =?UTF-8?q?=20test=20v1.1(sum=20passed)=20(#56756)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add reference of lbfgs * add reference of lbfgs * new ir op test v1.0 * fix new ir optest bug1.0 * modify two testcase bug * add new ir white list & pass test_mean_op.py * rename white list * add new_ir_guard * new ir sum op test all pass * rename backward.grad as ir_backward.grad * check place for new ir * fix test_build_model env bug * fix test_prim_program backward bug * change backward to ir_backward in check_appr * add check_new_ir flag for mkldnn * clean --------- Co-authored-by: wangruting --- python/paddle/tensor/math.py | 7 +++++-- test/legacy_test/eager_op_test.py | 8 ++++++++ test/legacy_test/test_reduce_op.py | 6 +++++- test/mkldnn/test_reduce_bf16_mkldnn_op.py | 3 ++- test/mkldnn/test_reduce_mkldnn_op.py | 4 ++-- test/white_list/new_ir_python_api_grad_white_list.py | 1 + 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index d620b57124207..02ac8b4669e7e 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -1510,8 +1510,11 @@ def sum(x, axis=None, dtype=None, keepdim=False, name=None): dtype_flag = False if dtype is not None: - dtype_flag = True - dtype = convert_np_dtype_to_dtype_(dtype) + if paddle.ir.core._use_new_ir_api(): + dtype = paddle.ir.core.convert_np_dtype_to_dtype_(dtype) + else: + dtype_flag = True + dtype = convert_np_dtype_to_dtype_(dtype) if in_dynamic_mode(): return _C_ops.sum(x, axis, dtype, keepdim) diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index 3c50f3d6f5a6f..95715c2dc84e1 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -1927,6 +1927,7 @@ def check_output_with_place( only_check_prim=False, inplace_atol=None, check_cinn=False, + check_new_ir=True, ): core._set_prim_all_enabled(False) core.set_prim_eager_enabled(False) @@ -2455,6 +2456,7 @@ def _is_skip_name(self, name): if ( self.op_type in new_ir_python_api_grad_white_list.new_ir_python_api_grad_white_list + and check_new_ir ): if ( type(place) is paddle.fluid.libpaddle.CPUPlace @@ -2576,6 +2578,7 @@ def check_output( inplace_atol=None, check_cinn=False, only_check_prim=False, + check_new_ir=True, ): self.__class__.op_type = self.op_type if self.is_mkldnn_op(): @@ -2600,6 +2603,7 @@ def check_output( only_check_prim=only_check_prim, inplace_atol=inplace_atol, check_cinn=check_cinn, + check_new_ir=check_new_ir, ) if not res and only_check_prim: continue @@ -2766,6 +2770,7 @@ def check_grad( only_check_prim=False, atol=1e-5, check_cinn=False, + check_new_ir=True, ): if hasattr(self, "use_custom_device") and self.use_custom_device: check_dygraph = False @@ -2788,6 +2793,7 @@ def check_grad( only_check_prim=only_check_prim, atol=atol, check_cinn=check_cinn, + check_new_ir=check_new_ir, ) def check_grad_with_place( @@ -2807,6 +2813,7 @@ def check_grad_with_place( numeric_place=None, atol=1e-5, check_cinn=False, + check_new_ir=True, ): if hasattr(self, "use_custom_device") and self.use_custom_device: check_dygraph = False @@ -3007,6 +3014,7 @@ def check_grad_with_place( if ( self.op_type in new_ir_python_api_grad_white_list.new_ir_python_api_grad_white_list + and check_new_ir ): if ( type(place) is paddle.fluid.libpaddle.CPUPlace diff --git a/test/legacy_test/test_reduce_op.py b/test/legacy_test/test_reduce_op.py index 9617f39537b4b..4043cb8ccba26 100644 --- a/test/legacy_test/test_reduce_op.py +++ b/test/legacy_test/test_reduce_op.py @@ -1180,7 +1180,11 @@ def setUp(self): def reduce_sum_wrapper2(x, axis=[0], dtype=None, keepdim=False): - return paddle._C_ops.sum(x, axis, dtype, keepdim) + if paddle.in_dynamic_mode(): + return paddle._C_ops.sum(x, axis, dtype, keepdim) + else: + if paddle.ir.core._use_new_ir_api(): + return paddle._ir_ops.sum(x, axis, dtype, keepdim) class Test8DReduce0(Test1DReduce): diff --git a/test/mkldnn/test_reduce_bf16_mkldnn_op.py b/test/mkldnn/test_reduce_bf16_mkldnn_op.py index 7e2581d4c5933..5001a03372e75 100644 --- a/test/mkldnn/test_reduce_bf16_mkldnn_op.py +++ b/test/mkldnn/test_reduce_bf16_mkldnn_op.py @@ -40,7 +40,7 @@ def setUp(self): self.attrs = {'use_mkldnn': self.use_mkldnn} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_dygraph=False, check_new_ir=False) def calculate_grads(self): tmp_tensor = np.zeros(self.x_fp32.shape).astype("float32") @@ -84,6 +84,7 @@ def test_check_grad(self): check_dygraph=False, user_defined_grads=[self.grad_X], user_defined_grad_outputs=[convert_float_to_uint16(self.grad_Out)], + check_new_ir=False, ) diff --git a/test/mkldnn/test_reduce_mkldnn_op.py b/test/mkldnn/test_reduce_mkldnn_op.py index 015271dfc4bbb..8e0ae6274d92a 100644 --- a/test/mkldnn/test_reduce_mkldnn_op.py +++ b/test/mkldnn/test_reduce_mkldnn_op.py @@ -29,12 +29,12 @@ def setUp(self): self.attrs = {'use_mkldnn': self.use_mkldnn} def test_check_output(self): - self.check_output(check_dygraph=False) + self.check_output(check_dygraph=False, check_new_ir=False) class TestReduceDefaultWithGradOneDNNOp(TestReduceSumDefaultOneDNNOp): def test_check_grad(self): - self.check_grad(['X'], 'Out', check_dygraph=False) + self.check_grad(['X'], 'Out', check_dygraph=False, check_new_ir=False) class TestReduceSum4DOneDNNOp(TestReduceDefaultWithGradOneDNNOp): diff --git a/test/white_list/new_ir_python_api_grad_white_list.py b/test/white_list/new_ir_python_api_grad_white_list.py index 81ab325a12aeb..b96ba7c4ff939 100644 --- a/test/white_list/new_ir_python_api_grad_white_list.py +++ b/test/white_list/new_ir_python_api_grad_white_list.py @@ -14,4 +14,5 @@ new_ir_python_api_grad_white_list = [ "mean", + "reduce_sum", ] From 83b942f347d7ca2b9dd1c138f041ec8963d811ae Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 4 Sep 2023 10:26:51 +0800 Subject: [PATCH 087/194] fix set value inplace strided bug (#56892) --- paddle/fluid/imperative/tracer.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index bd3bbf2108efc..785e5f2f75785 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -413,6 +413,7 @@ void Tracer::TraceOp(const std::string& type, std::map> need_backup_inputs2holder; std::map need_backup_inputs2strides; + std::map need_backup_inputs2offset; if (FLAGS_use_stride_kernel) { for (auto& iter : inplace_map) { auto inputs_iter = ins.find(iter.first); @@ -431,6 +432,7 @@ void Tracer::TraceOp(const std::string& type, ->GetMutable(); need_backup_inputs2holder[dense_tensor] = dense_tensor->Holder(); need_backup_inputs2strides[dense_tensor] = dense_tensor->strides(); + need_backup_inputs2offset[dense_tensor] = dense_tensor->offset(); } } } @@ -449,9 +451,11 @@ void Tracer::TraceOp(const std::string& type, for (auto& iter : need_backup_inputs2outputs) { iter.first->ResetHolder(need_backup_inputs2holder[iter.first]); iter.first->set_strides(need_backup_inputs2strides[iter.first]); + iter.first->set_offset(need_backup_inputs2offset[iter.first]); paddle::experimental::TransStrideLegacy(dev_ctx, iter.second, iter.first); iter.second->ResetHolder(need_backup_inputs2holder[iter.first]); iter.second->set_strides(need_backup_inputs2strides[iter.first]); + iter.second->set_offset(need_backup_inputs2offset[iter.first]); } } else { TraceOpImpl(type, From d7fc3781ec66c6cfbed1ee2fad5c5ed9367f3148 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 4 Sep 2023 10:27:56 +0800 Subject: [PATCH 088/194] fix contiguous (#56863) * fix contiguous --- paddle/fluid/pybind/eager_method.cc | 7 ++++--- test/legacy_test/test_stride.py | 29 ----------------------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 7d3037a076d57..2a4699f9454fa 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -2819,9 +2819,10 @@ static PyObject* tensor_contiguous(TensorObject* self, return reinterpret_cast(self); } else { eager_gil_scoped_release guard; - return ToPyObject( - paddle::Tensor(std::make_shared(std::move( - paddle::experimental::Trans2Contiguous(*(dense_tensor.get())))))); + self->tensor.set_impl(std::make_shared(std::move( + paddle::experimental::Trans2Contiguous(*(dense_tensor.get()))))); + Py_INCREF(self); + return reinterpret_cast(self); } } else { diff --git a/test/legacy_test/test_stride.py b/test/legacy_test/test_stride.py index ec78392bbc64b..2886e46febd6f 100644 --- a/test/legacy_test/test_stride.py +++ b/test/legacy_test/test_stride.py @@ -42,13 +42,11 @@ def call_transpose(self): x_c = x_transposed1.contiguous() self.assertTrue(np.allclose(x_c.numpy(), x_np_transposed1)) - self.assertFalse(x_c._is_shared_buffer_with(x_transposed1)) x_transposed2 = paddle.transpose(x_transposed1, perm=[2, 0, 1]) x_np_transposed2 = x_np_transposed1.transpose(2, 0, 1) self.assertTrue(np.allclose(x_transposed2.numpy(), x_np_transposed2)) self.assertFalse(x_transposed2.is_contiguous()) - self.assertTrue(x._is_shared_buffer_with(x_transposed2)) y = x_transposed2 + 2 y_np = x_np_transposed2 + 2 @@ -96,11 +94,6 @@ def call_diagonal(self): self.assertTrue(np.allclose(out3_c.numpy(), np_out3)) self.assertTrue(np.allclose(out4_c.numpy(), np_out4)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - self.assertFalse(out3_c._is_shared_buffer_with(out3)) - self.assertFalse(out4_c._is_shared_buffer_with(out4)) - def call_slice(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -141,8 +134,6 @@ def call_strided_slice(self): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_index_select(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -161,8 +152,6 @@ def call_index_select(self): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_reshape(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -201,8 +190,6 @@ def call_real(self): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_imag(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128') x = paddle.to_tensor(x_np) @@ -221,8 +208,6 @@ def call_imag(self): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_as_real(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128') x = paddle.to_tensor(x_np) @@ -351,10 +336,6 @@ def call_split(self): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_split2(self): x_np = np.random.random(size=[3, 9, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -386,10 +367,6 @@ def call_split2(self): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_split3(self): x_np = np.random.random(size=[9, 3, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -485,10 +462,6 @@ def call_chunk(self): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_unbind(self): x_np = np.random.random(size=[3, 9, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -622,8 +595,6 @@ def call_unfold(self): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_stride(self): self.call_transpose() self.call_diagonal() From 413ca9896ec1f4cbd9edd802b4fdd200af21e005 Mon Sep 17 00:00:00 2001 From: engineer1109 <1292846099@qq.com> Date: Mon, 4 Sep 2023 10:32:58 +0800 Subject: [PATCH 089/194] fix symbol redefined (#56766) --- paddle/fluid/distributed/collective/process_group_custom.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/distributed/collective/process_group_custom.cc b/paddle/fluid/distributed/collective/process_group_custom.cc index a905fcc095599..f6d0c761ec797 100644 --- a/paddle/fluid/distributed/collective/process_group_custom.cc +++ b/paddle/fluid/distributed/collective/process_group_custom.cc @@ -815,7 +815,7 @@ std::shared_ptr ProcessGroupCustom::Broadcast( CommType::BROADCAST); } -void CheckTensorsInDifferentDevices( +inline void CheckTensorsInDifferentDevices( const std::vector& tensors, const size_t num_devices) { PADDLE_ENFORCE_EQ( tensors.empty(), From a28e6f63a31c1ecee86818eb7778974660159cfa Mon Sep 17 00:00:00 2001 From: LiYuRio <63526175+LiYuRio@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:34:43 +0800 Subject: [PATCH 090/194] reshard r to p (#56833) --- paddle/fluid/pybind/auto_parallel_py.cc | 9 +++ paddle/fluid/pybind/eager_method.cc | 71 +++++++++++++--- .../distributed/auto_parallel/CMakeLists.txt | 3 +- .../distributed/auto_parallel/dist_attr.cc | 2 +- .../auto_parallel/r_to_p_reshard_function.cc | 80 +++++++++++++++++++ .../auto_parallel/r_to_p_reshard_function.h | 34 ++++++++ .../auto_parallel/s_to_r_reshard_function.cc | 1 - paddle/phi/kernels/assign_kernel.h | 8 ++ test/auto_parallel/CMakeLists.txt | 3 + test/auto_parallel/reshard_r_to_p.py | 73 +++++++++++++++++ test/auto_parallel/reshard_r_to_s.py | 1 + test/auto_parallel/test_reshard_r_to_p.py | 45 +++++++++++ test/auto_parallel/test_reshard_r_to_s.py | 1 - test/cpp/auto_parallel/spmd_rule_test.cc | 10 +-- 14 files changed, 319 insertions(+), 22 deletions(-) create mode 100644 paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.cc create mode 100644 paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h create mode 100644 test/auto_parallel/reshard_r_to_p.py create mode 100644 test/auto_parallel/test_reshard_r_to_p.py diff --git a/paddle/fluid/pybind/auto_parallel_py.cc b/paddle/fluid/pybind/auto_parallel_py.cc index 6f639f145dcea..bdd467fbefa8b 100644 --- a/paddle/fluid/pybind/auto_parallel_py.cc +++ b/paddle/fluid/pybind/auto_parallel_py.cc @@ -32,6 +32,7 @@ #include "paddle/fluid/distributed/auto_parallel/spmd_rules/common.h" #include "paddle/fluid/distributed/auto_parallel/spmd_rules/dist_tensor_spec.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h" #include "paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.h" #include "paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.h" @@ -157,6 +158,10 @@ void BindAutoParallel(py::module *m) { *m, "SToRReshardFunction", ReshardFunction) .def(py::init<>()); + py::class_( + *m, "RToPReshardFunction", ReshardFunction) + .def(py::init<>()); + py::class_(*m, "ProcessMesh") .def(py::init<>()) .def(py::init &, @@ -338,6 +343,10 @@ void BindAutoParallel(py::module *m) { .def("_is_partial", &TensorDistAttr::is_partial) .def("_partial_dims", &TensorDistAttr::partial_dims) .def("_clean_partial_dims", &TensorDistAttr::clean_partial_dims) + .def("_set_partial_dims", + [](TensorDistAttr &self, const std::vector &dims) { + self.set_partial_status(dims); + }) .def("_clean_partial_status", &TensorDistAttr::clean_partial_status); py::class_(*m, "SPMDRuleBase") diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 2a4699f9454fa..dd99770e05fae 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -61,6 +61,8 @@ typedef SSIZE_T ssize_t; #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/core/ddim.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/reshard_function.h" +#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" #include "paddle/phi/core/flags.h" #include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" @@ -99,6 +101,30 @@ Py_ssize_t GetSliceIndexFromPyObject(PyObject* obj) { } } +namespace { +#ifdef PADDLE_WITH_DISTRIBUTE +phi::DenseTensor ReshardXToReplicated( + phi::distributed::DistTensor* dist_tensor) { + if (!phi::distributed::IsDimsMappingReplicated( + dist_tensor->dist_attr().dims_mapping())) { + phi::distributed::TensorDistAttr dist_attr(dist_tensor->dist_attr()); + std::vector dims_mapping(dist_tensor->dims().size(), -1); + dist_attr.set_dims_mapping(dims_mapping); + + // reshard to replicate dist tensor + auto* func = + phi::distributed::ChooseProperReshardFunction(*dist_tensor, dist_attr); + auto* dev_ctx = + phi::DeviceContextPool::Instance().Get(dist_tensor->place()); + auto out_tensor = func->Eval(dev_ctx, *dist_tensor, dist_attr); + return out_tensor->value(); + } else { + return dist_tensor->value(); + } +} +#endif +} // namespace + PyDoc_STRVAR(tensor_method_numpy__doc__, // NOLINT R"DOC(numpy($self, /) -- @@ -145,15 +171,6 @@ static PyObject* tensor_method_numpy(TensorObject* self, return array; } auto tensor_dims = self->tensor.shape(); -#ifdef PADDLE_WITH_DISTRIBUTE - // Now the DistTensor's numpy() return the local tensor value - if (self->tensor.is_dist_tensor()) { - tensor_dims = phi::vectorize( - static_cast(self->tensor.impl().get()) - ->value() - .dims()); - } -#endif auto numpy_dtype = TensorDtype2NumpyDtype(self->tensor.type()); auto sizeof_dtype = phi::SizeOf(self->tensor.type()); Py_intptr_t py_dims[paddle::framework::DDim::kMaxRank]; // NOLINT @@ -258,12 +275,11 @@ static PyObject* tensor_method_numpy(TensorObject* self, dense_tensor->Holder()->size()); } else if (self->tensor.is_dist_tensor()) { #ifdef PADDLE_WITH_DISTRIBUTE - // TODO(chenweihang): deal with DistTensor as local DenseTensor now, - // if the local DenseTensor is shard or partial, do gather or reduce? VLOG(6) << "Getting DistTensor's numpy value"; auto* dist_tensor = static_cast(self->tensor.impl().get()); - auto& dense_tensor = dist_tensor->value(); + auto dense_tensor = ReshardXToReplicated(dist_tensor); + cpu_tensor.set_meta(dense_tensor.meta()); // deep copy auto tmp_allocation_ptr = @@ -330,7 +346,8 @@ static PyObject* tensor_method_numpy(TensorObject* self, VLOG(6) << "Getting DistTensor's numpy value"; auto* dist_tensor = static_cast(self->tensor.impl().get()); - auto& dense_tensor = dist_tensor->value(); + auto dense_tensor = ReshardXToReplicated(dist_tensor); + cpu_tensor.set_meta(dense_tensor.meta()); auto tmp_allocation_ptr = memory::Alloc(cpu_place, dense_tensor.Holder()->size()); @@ -2680,6 +2697,30 @@ static PyObject* tensor__grad_value(TensorObject* self, EAGER_CATCH_AND_THROW_RETURN_NULL } +static PyObject* tensor__local_value(TensorObject* self, + PyObject* args, + PyObject* kwargs) { + EAGER_TRY + if (self->tensor.is_dist_tensor()) { +#ifdef PADDLE_WITH_DISTRIBUTE + phi::distributed::DistTensor* dist_tensor = + static_cast(self->tensor.impl().get()); + paddle::Tensor result( + std::make_shared(dist_tensor->value())); + return ToPyObject(result); +#else + PADDLE_THROW(platform::errors::Unavailable( + "The `_local_value` method of (Dist)Tensor is not supported " + "in the current PaddlePaddle, please recompile and install " + "PaddlePaddle " + "with the option of `WITH_DISTRIBUTE=ON`.")); +#endif + } else { + RETURN_PY_NONE + } + EAGER_CATCH_AND_THROW_RETURN_NULL +} + static PyObject* tensor__unset_fake_empty(TensorObject* self, PyObject* args, PyObject* kwargs) { @@ -3131,6 +3172,10 @@ PyMethodDef variable_methods[] = { // NOLINT (PyCFunction)(void (*)())tensor__grad_value, METH_VARARGS | METH_KEYWORDS, nullptr}, + {"_local_value", + (PyCFunction)(void (*)())tensor__local_value, + METH_VARARGS | METH_KEYWORDS, + nullptr}, {"_unset_fake_empty", (PyCFunction)(void (*)())tensor__unset_fake_empty, METH_VARARGS | METH_KEYWORDS, diff --git a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt index d9d1c27ed23f3..3068881c82a17 100644 --- a/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt +++ b/paddle/phi/core/distributed/auto_parallel/CMakeLists.txt @@ -13,4 +13,5 @@ collect_srcs( inferspmd_utils.cc reshard_function.cc r_to_s_reshard_function.cc - s_to_r_reshard_function.cc) + s_to_r_reshard_function.cc + r_to_p_reshard_function.cc) diff --git a/paddle/phi/core/distributed/auto_parallel/dist_attr.cc b/paddle/phi/core/distributed/auto_parallel/dist_attr.cc index 0e091e98b27ab..ae58402acb08d 100644 --- a/paddle/phi/core/distributed/auto_parallel/dist_attr.cc +++ b/paddle/phi/core/distributed/auto_parallel/dist_attr.cc @@ -227,7 +227,7 @@ bool TensorDistAttr::verify_partial_status() const { if (itr.first < 0 || itr.first >= process_mesh_.ndim()) { return false; } - if (itr.second < ReduceType::kRedSum || itr.second <= ReduceType::kRedAll) { + if (itr.second < ReduceType::kRedSum || itr.second > ReduceType::kRedAll) { return false; } } diff --git a/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.cc new file mode 100644 index 0000000000000..e941e82c98b77 --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h" + +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" +#include "paddle/phi/kernels/assign_kernel.h" +#include "paddle/phi/kernels/full_kernel.h" + +namespace phi { +namespace distributed { + +bool RToPReshardFunction::IsSuitable(const DistTensor& in, + const TensorDistAttr& out_dist_attr) { + bool flag = true; + const auto& in_dist_attr = in.dist_attr(); + + const auto& in_dims_mapping = in_dist_attr.dims_mapping(); + + flag &= IsDimsMappingReplicated(in_dims_mapping); + flag &= out_dist_attr.is_partial(); + + const auto& in_process_mesh = in_dist_attr.process_mesh(); + const auto& out_process_mesh = out_dist_attr.process_mesh(); + + flag &= (in_process_mesh.ndim() == 1); + flag &= (out_process_mesh.ndim() == 1); + flag &= (in_process_mesh == out_process_mesh); + + return flag; +} + +void RToPReshardFunction::Eval(phi::DeviceContext* dev_ctx, + const DistTensor& in, + const TensorDistAttr& out_dist_attr, + DistTensor* out) { + const auto& out_process_mesh = out_dist_attr.process_mesh(); + int64_t local_rank = GetCurRankCoordInMesh(out_process_mesh)[0]; + IntArray shape(in.dims().Get(), in.dims().size()); + + if (local_rank != 0) { + // reset the physical tensor to zero + RESHARD_FUNCTOR(dev_ctx, Full, in.dtype(), shape, 0, GetMutableTensor(out)); + } else { + // assign the input value to output + if (phi::CPUContext::classof(dev_ctx)) { + Assign(static_cast(*dev_ctx), + in.value(), + GetMutableTensor(out)); +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) + } else if (phi::GPUContext::classof(dev_ctx)) { + Assign(static_cast(*dev_ctx), + in.value(), + GetMutableTensor(out)); +#endif + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "The assign in reshard only supported on CPU and GPU for now.")); + } + } + SetDistProps(out, in.dims(), out_dist_attr); +} + +REGISTER_RESHARD_FUNC(RToPReshardFunction); + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h b/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h new file mode 100644 index 0000000000000..af3bdb41d78c1 --- /dev/null +++ b/paddle/phi/core/distributed/auto_parallel/r_to_p_reshard_function.h @@ -0,0 +1,34 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/phi/core/distributed/auto_parallel/reshard_function.h" + +namespace phi { +namespace distributed { + +class RToPReshardFunction final : public ReshardFunction { + public: + bool IsSuitable(const DistTensor& in, + const TensorDistAttr& out_dist_attr) override; + + void Eval(DeviceContext* dev_ctx, + const DistTensor& in, + const TensorDistAttr& out_dist_attr, + DistTensor* out) override; +}; + +} // namespace distributed +} // namespace phi diff --git a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc index 61f57e1d6695c..8e4cb877c95db 100644 --- a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc @@ -76,7 +76,6 @@ void SToRReshardFunction::Eval(DeviceContext* dev_ctx, in.value(), in_process_ids.size(), GetMutableTensor(out)); - std::map split_axis_to_mesh_axis = GetSplitAxisWithDimsMapping(in_dims_mapping); int64_t split_axis = split_axis_to_mesh_axis.begin()->first; diff --git a/paddle/phi/kernels/assign_kernel.h b/paddle/phi/kernels/assign_kernel.h index 7fa0350ad0ed6..fa331f76addb6 100644 --- a/paddle/phi/kernels/assign_kernel.h +++ b/paddle/phi/kernels/assign_kernel.h @@ -38,6 +38,14 @@ DenseTensor Assign(const Context& dev_ctx, const DenseTensor& x) { return out; } +template +void Assign(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out) { + MetaTensor meta_out(out); + MetaTensor meta_x(x); + UnchangedInferMeta(meta_x, &meta_out); + AssignKernel(dev_ctx, x, out); +} + // In order to be compatible with the `AsDispensable` input in the original // assign op maker, the input parameter here needs to be dispensable, but // this looks weird diff --git a/test/auto_parallel/CMakeLists.txt b/test/auto_parallel/CMakeLists.txt index eef02ecb28cc9..458c273951e05 100644 --- a/test/auto_parallel/CMakeLists.txt +++ b/test/auto_parallel/CMakeLists.txt @@ -85,6 +85,9 @@ if(WITH_DISTRIBUTE AND WITH_GPU) py_test_modules(test_reshard_r_to_s MODULES test_reshard_r_to_s) set_tests_properties(test_reshard_r_to_s PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + py_test_modules(test_reshard_r_to_p MODULES test_reshard_r_to_p) + set_tests_properties(test_reshard_r_to_p + PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) # End of unittests WITH multi cards and timeout # NOTE(zyl): unittests WITH multi cards and WITHOUT timeout diff --git a/test/auto_parallel/reshard_r_to_p.py b/test/auto_parallel/reshard_r_to_p.py new file mode 100644 index 0000000000000..13e899876e15b --- /dev/null +++ b/test/auto_parallel/reshard_r_to_p.py @@ -0,0 +1,73 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import os + +import numpy as np + +import paddle +import paddle.distributed as dist +from paddle.framework import core + + +class TestReshardRToP: + def __init__(self): + self._shape = eval(os.getenv("shape")) + self._dtype = os.getenv("dtype") + self._seeds = eval(os.getenv("seeds")) + self._backend = os.getenv("backend") + self._mesh = dist.ProcessMesh([0, 1], dim_names=["x"]) + + def run_test_case(self): + if self._backend == "cpu": + paddle.set_device("cpu") + place = paddle.CPUPlace() + elif self._backend == "gpu": + place = paddle.CUDAPlace(dist.get_rank()) + + dev_ctx = core.DeviceContext.create(place) + a = paddle.ones(self._shape) + + in_shard_specs = [None for i in range(len(self._shape))] + out_shard_specs = [None for i in range(len(self._shape))] + + dist_attr = dist.DistAttr( + mesh=self._mesh, sharding_specs=in_shard_specs + ) + out_dist_attr = dist.DistAttr( + mesh=self._mesh, sharding_specs=out_shard_specs + ) + out_dist_attr._set_partial_dims([0]) + + input_tensor = dist.shard_tensor(a, dist_attr=dist_attr) + + reshard_func = core.RToPReshardFunction() + assert reshard_func.is_suitable(input_tensor, out_dist_attr) + + out = reshard_func.eval(dev_ctx, input_tensor, out_dist_attr) + + if dist.get_rank() == 0: + np.testing.assert_equal( + out._local_value().numpy(), input_tensor.numpy() + ) + else: + zeros = paddle.zeros(self._shape) + np.testing.assert_equal(out._local_value().numpy(), zeros.numpy()) + + assert np.equal(out.shape, input_tensor.shape).all() + assert np.equal(out._local_shape, input_tensor._local_shape).all() + + +if __name__ == '__main__': + TestReshardRToP().run_test_case() diff --git a/test/auto_parallel/reshard_r_to_s.py b/test/auto_parallel/reshard_r_to_s.py index 814b0ef0dd77f..690c42fa49283 100644 --- a/test/auto_parallel/reshard_r_to_s.py +++ b/test/auto_parallel/reshard_r_to_s.py @@ -61,6 +61,7 @@ def run_test_case(self): if out_shape[self._shard] % 2 == 0: out_shape[self._shard] = out_shape[self._shard] // 2 + np.testing.assert_equal(out.numpy(), input_tensor.numpy()) else: out_shape[self._shard] = ( out_shape[self._shard] // 2 diff --git a/test/auto_parallel/test_reshard_r_to_p.py b/test/auto_parallel/test_reshard_r_to_p.py new file mode 100644 index 0000000000000..a8619722e4050 --- /dev/null +++ b/test/auto_parallel/test_reshard_r_to_p.py @@ -0,0 +1,45 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import collective.test_communication_api_base as test_base + + +class TestReshardRToP(test_base.CommunicationTestDistBase): + def setUp(self): + super().setUp(num_of_devices=2, timeout=120) + self._default_envs = { + "shape": "(10, 20)", + "dtype": "float32", + "seeds": str(self._seeds), + } + self._changeable_envs = { + "shape": ["(10, 20)"], + "backend": ["cpu", "gpu"], + } + + def test_reshard_r_to_p(self): + envs_list = test_base.gen_product_envs_list( + self._default_envs, self._changeable_envs + ) + for envs in envs_list: + self.run_test_case( + "reshard_r_to_p.py", + user_defined_envs=envs, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/auto_parallel/test_reshard_r_to_s.py b/test/auto_parallel/test_reshard_r_to_s.py index 187fa40918dcd..68699885094de 100644 --- a/test/auto_parallel/test_reshard_r_to_s.py +++ b/test/auto_parallel/test_reshard_r_to_s.py @@ -21,7 +21,6 @@ class TestReshardRToS(test_base.CommunicationTestDistBase): def setUp(self): super().setUp(num_of_devices=2, timeout=120) self._default_envs = { - "shape": "(10, 20)", "dtype": "float32", "seeds": str(self._seeds), } diff --git a/test/cpp/auto_parallel/spmd_rule_test.cc b/test/cpp/auto_parallel/spmd_rule_test.cc index 30907b707aa9e..079f2d84ea044 100644 --- a/test/cpp/auto_parallel/spmd_rule_test.cc +++ b/test/cpp/auto_parallel/spmd_rule_test.cc @@ -209,8 +209,8 @@ TEST(MatmulSPMDRule, Ctor) { EXPECT_EQ(infered_dist_attrs.second[0].is_partial(), false); VLOG(4) << "test8 done." << std::endl << std::endl << std::endl; - // abcmk[-1, -1, -1, -1], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = - // abcmn[-1, -1, -1, 1] partial[0]: done + // abcmk[-1, -1, 0, 1]+trans_x=true, kn[1, 0]+trans_y=true --> abcmk[-1, -1, + // 0, -1],kn[-1, 0] = abcmn[-1, -1, 1, -1] partial[0]: done x_dist_attr.set_dims_mapping({-1, -1, 0, 1}); y_dist_attr.set_dims_mapping({1, 0}); x = phi::distributed::DistMetaTensor(phi::make_ddim(x_shape), x_dist_attr); @@ -221,7 +221,8 @@ TEST(MatmulSPMDRule, Ctor) { EXPECT_EQ(infered_dist_attrs.first[0].dims_mapping(), std::vector({-1, -1, 0, 1})); EXPECT_EQ(infered_dist_attrs.first[1].dims_mapping(), - std::vector({-1, 0})); + std::vector( + {-1, 0})); // confilct and should be changed to [-1, 0] EXPECT_EQ(infered_dist_attrs.second[0].dims_mapping(), std::vector({-1, -1, 1, -1})); EXPECT_EQ(infered_dist_attrs.second[0].partial_dims(), @@ -229,8 +230,7 @@ TEST(MatmulSPMDRule, Ctor) { VLOG(4) << infered_dist_attrs.second[0].to_string(); infered_dist_attrs.second[0].clean_partial_status(); EXPECT_EQ(infered_dist_attrs.second[0].is_partial(), false); - infered_dist_attrs.second[0].set_partial_status(std::vector({1})); - EXPECT_EQ(infered_dist_attrs.second[0].verify_partial_status(), false); + // EXPECT_ANY_THROW(infered_dist_attrs.second[0].set_partial_status(std::vector({1}))); VLOG(4) << "test9 done." << std::endl << std::endl << std::endl; // abcmk[-1, -1, 1, 0], kn[1, 0] --> abcmk[-1, -1, -1, 0],kn[1, 0] = From d38cd6cee5372aa46fb77d833434183cdf5c1e30 Mon Sep 17 00:00:00 2001 From: BiynXu <62832681+BiynXu@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:35:15 +0800 Subject: [PATCH 091/194] [CINN][Fix] change Flatten to Fuse (#56719) Change FlattenLoops in the elementwise schedule to Fuse --- paddle/cinn/hlir/pe/ir_schedule_pe.cc | 16 ++++++++-------- test/legacy_test/CMakeLists.txt | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/paddle/cinn/hlir/pe/ir_schedule_pe.cc b/paddle/cinn/hlir/pe/ir_schedule_pe.cc index a97b248c1223b..3677025aaedaa 100644 --- a/paddle/cinn/hlir/pe/ir_schedule_pe.cc +++ b/paddle/cinn/hlir/pe/ir_schedule_pe.cc @@ -46,15 +46,15 @@ void IRElementwiseSchedule(ir::IRSchedule &ir_sch, // NOLINT << ir_sch.GetModule().GetExprs().at(0); if (target == common::DefaultNVGPUTarget()) { auto blocks = ir_sch.GetAllBlocks(); - ir_sch.FlattenLoops(ir_sch.GetLoops(blocks[0]), true); + std::vector loops = ir_sch.GetLoops(blocks[0]); + ir::Expr loop = ir_sch.Fuse(loops); - auto loops = ir_sch.GetLoops(blocks[0]); auto size = std::accumulate( output_shape.begin(), output_shape.end(), 1, std::multiplies()); if (size <= target.max_num_threads()) { - ir_sch.Bind(loops[0], "threadIdx.x"); + ir_sch.Bind(loop, "threadIdx.x"); } else { - auto splited = ir_sch.Split(loops[0], {-1, target.max_num_threads()}); + auto splited = ir_sch.Split(loop, {-1, target.max_num_threads()}); ir_sch.Bind(splited[0], "blockIdx.x"); ir_sch.Bind(splited[1], "threadIdx.x"); } @@ -74,15 +74,15 @@ void IRInjectiveSchedule(ir::IRSchedule &ir_sch, // NOLINT << ir_sch.GetModule().GetExprs().at(0); if (target == common::DefaultNVGPUTarget()) { auto blocks = ir_sch.GetAllBlocks(); - ir_sch.FlattenLoops(ir_sch.GetLoops(blocks[0]), false); + std::vector loops = ir_sch.GetLoops(blocks[0]); + ir::Expr loop = ir_sch.Fuse(loops); - auto loops = ir_sch.GetLoops(blocks[0]); auto size = std::accumulate( output_shape.begin(), output_shape.end(), 1, std::multiplies()); if (size <= target.max_num_threads()) { - ir_sch.Bind(loops[0], "threadIdx.x"); + ir_sch.Bind(loop, "threadIdx.x"); } else { - auto splited = ir_sch.Split(loops[0], {-1, target.max_num_threads()}); + auto splited = ir_sch.Split(loop, {-1, target.max_num_threads()}); ir_sch.Bind(splited[0], "blockIdx.x"); ir_sch.Bind(splited[1], "threadIdx.x"); } diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 86443907bd9aa..8e0b9ad91cba5 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -1061,7 +1061,7 @@ set_tests_properties( PROPERTIES TIMEOUT 120) set_tests_properties(test_conv_nn_grad PROPERTIES TIMEOUT 120) set_tests_properties(test_program_prune_backward PROPERTIES TIMEOUT 120) -set_tests_properties(test_group_norm_op PROPERTIES TIMEOUT 300) +set_tests_properties(test_group_norm_op PROPERTIES TIMEOUT 1000) set_tests_properties(test_imperative_optimizer PROPERTIES TIMEOUT 250) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 250) set_tests_properties(test_pool2d_op PROPERTIES TIMEOUT 120) From 25a0b46dc6155b058f7a1ef04550bd2dcf65dbd1 Mon Sep 17 00:00:00 2001 From: duanyanhui <45005871+YanhuiDua@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:59:18 +0800 Subject: [PATCH 092/194] optimize softmax_mask_fuse (#56877) --- paddle/fluid/eager/amp_auto_cast.h | 4 + .../fusion/gpu/fused_softmax_mask_kernel.cu | 131 ++++++++++++------ test/legacy_test/test_softmax_mask_fuse_op.py | 21 +++ 3 files changed, 112 insertions(+), 44 deletions(-) diff --git a/paddle/fluid/eager/amp_auto_cast.h b/paddle/fluid/eager/amp_auto_cast.h index c9cf3e2ee2823..66080ecef6a6b 100644 --- a/paddle/fluid/eager/amp_auto_cast.h +++ b/paddle/fluid/eager/amp_auto_cast.h @@ -75,6 +75,10 @@ inline paddle::Tensor AmpAutoCast(const std::string& input_name, input_name != "X") { return input; } + if (op_name == "fused_softmax_mask" && input_name == "Mask" && + input.dtype() == phi::DataType::FLOAT32) { + return input; + } if (dst_dtype == phi::DataType::FLOAT16) { if (op_name == "run_program") { return input; diff --git a/paddle/phi/kernels/fusion/gpu/fused_softmax_mask_kernel.cu b/paddle/phi/kernels/fusion/gpu/fused_softmax_mask_kernel.cu index 0902b9448eca6..1adadb6c3f627 100644 --- a/paddle/phi/kernels/fusion/gpu/fused_softmax_mask_kernel.cu +++ b/paddle/phi/kernels/fusion/gpu/fused_softmax_mask_kernel.cu @@ -22,9 +22,9 @@ namespace phi { namespace fusion { // T == fp16 -template +template __global__ void SoftmaxMaskFuseGPUKernel(const T* x_data, - const T* mask_data, + const MT* mask_data, T* y_data, int batch_count, int key_seq_len) { @@ -62,7 +62,7 @@ __global__ void SoftmaxMaskFuseGPUKernel(const T* x_data, // using float for all inter compute float data[kLocalBatchSize][kLocalIterations]; T temp_data[kOneLoadingCounts]; - T temp_mask[kOneLoadingCounts]; + MT temp_mask[kOneLoadingCounts]; #pragma unroll for (int i = 0; i < kLocalBatchSize; ++i) { @@ -151,7 +151,6 @@ void FusedSoftmaxMaskKernel(const Context& dev_ctx, const DenseTensor& mask, DenseTensor* out) { auto* x_data = x.data(); - auto* mask_data = mask.data(); auto* y_data = dev_ctx.template Alloc(out); auto x_dim = x.dims(); @@ -226,46 +225,90 @@ void FusedSoftmaxMaskKernel(const Context& dev_ctx, dim3 blocks(query_seq_len / batches_per_block, attn_heads, batches); dim3 threads(warp_size, warps_per_block, 1); - // launch the kernel based on the pow2_index - switch (pow2_index) { - case 5: // 32 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 6: // 64 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 7: // 128 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 8: // 256 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 9: // 512 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 10: // 1024 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 11: // 2048 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 12: // 4096 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - case 13: // 8192 - SoftmaxMaskFuseGPUKernel<<>>( - x_data, mask_data, y_data, batch_count, key_seq_len); - break; - default: - break; + if (mask.dtype() == x.dtype()) { + auto* mask_data = mask.data(); + switch (pow2_index) { + case 5: // 32 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 6: // 64 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 7: // 128 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 8: // 256 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 9: // 512 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 10: // 1024 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 11: // 2048 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 12: // 4096 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 13: // 8192 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + default: + break; + } + } else if (mask.dtype() == phi::DataType::FLOAT32) { + auto* mask_data = mask.data(); + switch (pow2_index) { + case 5: // 32 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 6: // 64 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 7: // 128 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 8: // 256 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 9: // 512 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 10: // 1024 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 11: // 2048 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 12: // 4096 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + case 13: // 8192 + SoftmaxMaskFuseGPUKernel<<>>( + x_data, mask_data, y_data, batch_count, key_seq_len); + break; + default: + break; + } } } diff --git a/test/legacy_test/test_softmax_mask_fuse_op.py b/test/legacy_test/test_softmax_mask_fuse_op.py index 79c6ad8c93525..56a4ba24a6862 100644 --- a/test/legacy_test/test_softmax_mask_fuse_op.py +++ b/test/legacy_test/test_softmax_mask_fuse_op.py @@ -78,6 +78,27 @@ def test_check_grad(self): self.check_grad_with_place(core.CUDAPlace(0), ["X"], "Out") +@unittest.skipIf( + not core.is_compiled_with_cuda(), "core is not compiled with CUDA" +) +class TestSoftmaxMaskFuseOp01(OpTest): + def setUp(self): + self.op_type = "fused_softmax_mask" + self.python_api = paddle.incubate.softmax_mask_fuse + x = np.random.random((1, 1, 8, 32)).astype("float16") + mask = np.random.randint(0, 2, (1, 1, 8, 32)).astype("float32") + mask_input = np.where(mask == 1, -10000.0, mask) + self.inputs = {'X': x, 'Mask': mask_input} + rst = _get_softmax(x, mask_input) + self.outputs = {'Out': rst} + + def test_check_output(self): + self.check_output_with_place(core.CUDAPlace(0)) + + def test_check_grad(self): + self.check_grad_with_place(core.CUDAPlace(0), ["X"], "Out") + + @unittest.skipIf( not core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) From 7d8402a87fa88a689005aef2b23ff85fdb8d496b Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Mon, 4 Sep 2023 11:19:49 +0800 Subject: [PATCH 093/194] fix paddle namespace conflict when using paddle_flags (#56913) * update * update * update --- .../distributed/ps/service/heter_client.cc | 24 +++++++++---------- paddle/phi/core/flags.h | 12 ++++------ paddle/utils/flags_native.h | 12 ++++------ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/paddle/fluid/distributed/ps/service/heter_client.cc b/paddle/fluid/distributed/ps/service/heter_client.cc index d7a1f5cf7c4f1..e0744f96f91fa 100644 --- a/paddle/fluid/distributed/ps/service/heter_client.cc +++ b/paddle/fluid/distributed/ps/service/heter_client.cc @@ -85,7 +85,7 @@ void HeterClient::CreateClient2XpuConnection() { xpu_channels_[i].reset(new brpc::Channel()); if (xpu_channels_[i]->Init(xpu_list_[i].c_str(), "", &options) != 0) { VLOG(0) << "HeterClient channel init fail. Try Again"; - auto ip_port = ::paddle::string::Split(xpu_list_[i], ':'); + auto ip_port = paddle::string::Split(xpu_list_[i], ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -100,7 +100,7 @@ void HeterClient::CreateClient2XpuConnection() { if (previous_xpu_channels_[i]->Init( previous_xpu_list_[i].c_str(), "", &options) != 0) { VLOG(0) << "HeterClient channel init fail. Try Again"; - auto ip_port = ::paddle::string::Split(previous_xpu_list_[i], ':'); + auto ip_port = paddle::string::Split(previous_xpu_list_[i], ':'); std::string ip = ip_port[0]; int port = std::stoi(ip_port[1]); std::string int_ip_port = GetIntTypeEndpoint(ip, port); @@ -167,13 +167,13 @@ void HeterClient::SendAndRecvAsync( // int idx = 1; // for test // LOG(INFO) << "xpu_channels_ size: " << xpu_channels_.size(); // channel = xpu_channels_[idx].get(); // 为了适配 send_and_recv op - // ::paddle::distributed::PsService_Stub stub(channel); + // paddle::distributed::PsService_Stub stub(channel); // stub.SendToSwitch(&closure->cntl, &request, &closure->response, // closure); fut.wait(); VLOG(4) << "calling switch service done"; return; } - ::paddle::distributed::PsService_Stub stub(channel); + paddle::distributed::PsService_Stub stub(channel); stub.SendAndRecvVariable( &closure->cntl, &request, &closure->response, closure); } @@ -181,11 +181,11 @@ void HeterClient::SendAndRecvAsync( std::future HeterClient::SendCmd( uint32_t table_id, int cmd_id, const std::vector& params) { size_t request_call_num = xpu_channels_.size(); - ::paddle::distributed::DownpourBrpcClosure* closure = - new ::paddle::distributed::DownpourBrpcClosure( + paddle::distributed::DownpourBrpcClosure* closure = + new paddle::distributed::DownpourBrpcClosure( request_call_num, [request_call_num, cmd_id](void* done) { int ret = 0; - auto* closure = (::paddle::distributed::DownpourBrpcClosure*)done; + auto* closure = (paddle::distributed::DownpourBrpcClosure*)done; for (size_t i = 0; i < request_call_num; ++i) { if (closure->check_response(i, cmd_id) != 0) { ret = -1; @@ -204,7 +204,7 @@ std::future HeterClient::SendCmd( for (const auto& param : params) { closure->request(i)->add_params(param); } - ::paddle::distributed::PsService_Stub rpc_stub(xpu_channels_[i].get()); + paddle::distributed::PsService_Stub rpc_stub(xpu_channels_[i].get()); closure->cntl(i)->set_timeout_ms( FLAGS_pserver_timeout_ms); // cmd msg don't limit timeout for save/load rpc_stub.service( @@ -270,7 +270,7 @@ int HeterClient::Send(const platform::DeviceContext& ctx, } brpc::Channel* channel = send_switch_channels_[0].get(); // brpc::Channel* channel = xpu_channels_[0].get(); - ::paddle::distributed::PsService_Stub stub(channel); + paddle::distributed::PsService_Stub stub(channel); stub.SendToSwitch(&closure->cntl, &request, &closure->ps_response, closure); VLOG(4) << "waiting SendToSwitch response result......"; @@ -317,7 +317,7 @@ int HeterClient::Send(int group_id, send_switch_channels_.push_back(xpu_channels_[0]); } brpc::Channel* channel = send_switch_channels_[0].get(); - ::paddle::distributed::PsService_Stub stub(channel); + paddle::distributed::PsService_Stub stub(channel); stub.SendToSwitch(&closure->cntl, &request, &closure->ps_response, closure); fut.wait(); delete closure; @@ -362,7 +362,7 @@ int HeterClient::Recv(const platform::DeviceContext& ctx, recv_switch_channels_.push_back(xpu_channels_[1]); } brpc::Channel* channel = recv_switch_channels_[0].get(); - ::paddle::distributed::PsService_Stub stub(channel); + paddle::distributed::PsService_Stub stub(channel); stub.RecvFromSwitch(&closure->cntl, &request, &closure->response, closure); fut.wait(); VLOG(4) << "RecvFromSwitch done"; @@ -412,7 +412,7 @@ int HeterClient::Recv(int group_id, recv_switch_channels_.push_back(xpu_channels_[0]); } brpc::Channel* channel = recv_switch_channels_[0].get(); - ::paddle::distributed::PsService_Stub stub(channel); + paddle::distributed::PsService_Stub stub(channel); stub.RecvFromSwitch(&closure->cntl, &request, &closure->response, closure); fut.wait(); VLOG(4) << "RecvFromSwitch done"; diff --git a/paddle/phi/core/flags.h b/paddle/phi/core/flags.h index 938f661853aa2..f6e35f52eeefb 100644 --- a/paddle/phi/core/flags.h +++ b/paddle/phi/core/flags.h @@ -59,24 +59,20 @@ #else // PADDLE_WITH_GFLAGS #define PHI_DECLARE_VARIABLE(type, shorttype, name) \ - namespace paddle { \ - namespace flags { \ + namespace paddle_flags { \ extern PHI_IMPORT_FLAG type FLAGS_##name; \ } \ - } \ - using paddle::flags::FLAGS_##name + using paddle_flags::FLAGS_##name #define PHI_DEFINE_VARIABLE(type, shorttype, name, default_value, description) \ - namespace paddle { \ - namespace flags { \ + namespace paddle_flags { \ static const type FLAGS_##name##_default = default_value; \ PHI_EXPORT_FLAG type FLAGS_##name = default_value; \ /* Register FLAG */ \ static ::paddle::flags::FlagRegisterer flag_##name##_registerer( \ #name, description, __FILE__, &FLAGS_##name##_default, &FLAGS_##name); \ } \ - } \ - using paddle::flags::FLAGS_##name + using paddle_flags::FLAGS_##name #endif diff --git a/paddle/utils/flags_native.h b/paddle/utils/flags_native.h index 04814a4f67950..abff8a486ecac 100644 --- a/paddle/utils/flags_native.h +++ b/paddle/utils/flags_native.h @@ -74,12 +74,10 @@ void PrintAllFlagHelp(bool to_file = false, // ----------------------------DECLARE FLAGS---------------------------- #define PD_DECLARE_VARIABLE(type, name) \ - namespace paddle { \ - namespace flags { \ + namespace paddle_flags { \ extern type FLAGS_##name; \ } \ - } \ - using paddle::flags::FLAGS_##name + using paddle_flags::FLAGS_##name #define PD_DECLARE_bool(name) PD_DECLARE_VARIABLE(bool, name) #define PD_DECLARE_int32(name) PD_DECLARE_VARIABLE(int32_t, name) @@ -105,16 +103,14 @@ class FlagRegisterer { // ----------------------------DEFINE FLAGS---------------------------- #define PD_DEFINE_VARIABLE(type, name, default_value, description) \ - namespace paddle { \ - namespace flags { \ + namespace paddle_flags { \ static const type FLAGS_##name##_default = default_value; \ type FLAGS_##name = default_value; \ /* Register FLAG */ \ static ::paddle::flags::FlagRegisterer flag_##name##_registerer( \ #name, description, __FILE__, &FLAGS_##name##_default, &FLAGS_##name); \ } \ - } \ - using paddle::flags::FLAGS_##name + using paddle_flags::FLAGS_##name #define PD_DEFINE_bool(name, val, txt) PD_DEFINE_VARIABLE(bool, name, val, txt) #define PD_DEFINE_int32(name, val, txt) \ From 8aa1772cf33170e4dc8fc6566bf60b18598b4bb2 Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Mon, 4 Sep 2023 11:41:44 +0800 Subject: [PATCH 094/194] fix compile errors when using shared phi on windows (#56915) * update * fix bug * fix bug * fix bug * fix bug * rerun ci * turn off shared_phi --- .../fluid/framework/new_executor/interpreter_base_impl.h | 2 +- paddle/fluid/framework/op_kernel_type.h | 2 +- paddle/fluid/imperative/tracer.cc | 2 +- paddle/fluid/imperative/tracer.h | 2 +- paddle/fluid/pybind/eager_method.cc | 2 +- paddle/phi/api/lib/api_gen_utils.cc | 4 ++-- paddle/phi/api/lib/data_transform.cc | 4 ++-- paddle/phi/core/kernel_factory.cc | 9 ++++----- 8 files changed, 13 insertions(+), 14 deletions(-) diff --git a/paddle/fluid/framework/new_executor/interpreter_base_impl.h b/paddle/fluid/framework/new_executor/interpreter_base_impl.h index 88ac481d16c5b..2c030ef1dc264 100644 --- a/paddle/fluid/framework/new_executor/interpreter_base_impl.h +++ b/paddle/fluid/framework/new_executor/interpreter_base_impl.h @@ -45,7 +45,7 @@ PD_DECLARE_bool(new_executor_use_local_scope); PHI_DECLARE_bool(check_nan_inf); PD_DECLARE_bool(benchmark); -PD_DECLARE_uint64(executor_log_deps_every_microseconds); +PHI_DECLARE_uint64(executor_log_deps_every_microseconds); PHI_DECLARE_bool(new_executor_use_cuda_graph); PHI_DECLARE_bool(enable_new_ir_in_executor); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) diff --git a/paddle/fluid/framework/op_kernel_type.h b/paddle/fluid/framework/op_kernel_type.h index 558ae8f9b9078..3bfe90dd6b921 100644 --- a/paddle/fluid/framework/op_kernel_type.h +++ b/paddle/fluid/framework/op_kernel_type.h @@ -26,7 +26,7 @@ limitations under the License. */ #include "paddle/phi/core/kernel_factory.h" #include "paddle/utils/flags.h" -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); namespace paddle { namespace framework { diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index 785e5f2f75785..cc00af31e61aa 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -38,7 +38,7 @@ PHI_DECLARE_bool(use_mkldnn); PHI_DECLARE_string(tracer_mkldnn_ops_on); PHI_DECLARE_string(tracer_mkldnn_ops_off); -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); namespace paddle { namespace imperative { diff --git a/paddle/fluid/imperative/tracer.h b/paddle/fluid/imperative/tracer.h index 49b94c743fdb8..3eced7804c320 100644 --- a/paddle/fluid/imperative/tracer.h +++ b/paddle/fluid/imperative/tracer.h @@ -32,7 +32,7 @@ #include "paddle/fluid/platform/macros.h" #include "paddle/phi/core/compat/arg_map_context.h" -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); namespace paddle { namespace imperative { diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index dd99770e05fae..5200db761d049 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -69,7 +69,7 @@ typedef SSIZE_T ssize_t; #include "paddle/utils/pybind.h" PHI_DECLARE_bool(set_to_1d); -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); namespace paddle { namespace pybind { diff --git a/paddle/phi/api/lib/api_gen_utils.cc b/paddle/phi/api/lib/api_gen_utils.cc index 80e169fae10c8..51875ed9175ec 100644 --- a/paddle/phi/api/lib/api_gen_utils.cc +++ b/paddle/phi/api/lib/api_gen_utils.cc @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/api/lib/api_gen_utils.h" +#include "paddle/phi/core/flags.h" #include "paddle/phi/core/visit_type.h" #include "paddle/phi/kernels/strided_copy_kernel.h" -#include "paddle/utils/flags.h" -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); #include "glog/logging.h" diff --git a/paddle/phi/api/lib/data_transform.cc b/paddle/phi/api/lib/data_transform.cc index a8eb379e359fc..b1559908ebc3f 100644 --- a/paddle/phi/api/lib/data_transform.cc +++ b/paddle/phi/api/lib/data_transform.cc @@ -21,15 +21,15 @@ limitations under the License. */ #include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/backends/context_pool.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" +#include "paddle/phi/core/flags.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/visit_type.h" #include "paddle/phi/kernels/cast_kernel.h" #include "paddle/phi/kernels/contiguous_kernel.h" #include "paddle/phi/kernels/transfer_layout_kernel.h" -#include "paddle/utils/flags.h" -PD_DECLARE_bool(use_stride_kernel); +PHI_DECLARE_bool(use_stride_kernel); namespace paddle { namespace experimental { diff --git a/paddle/phi/core/kernel_factory.cc b/paddle/phi/core/kernel_factory.cc index 3ead251e33220..d58decadfadca 100644 --- a/paddle/phi/core/kernel_factory.cc +++ b/paddle/phi/core/kernel_factory.cc @@ -25,14 +25,13 @@ #if defined(PADDLE_WITH_CUSTOM_DEVICE) #include "paddle/phi/backends/custom/custom_device_op_list.h" #endif -#include "paddle/fluid/platform/flags.h" #include "paddle/phi/core/compat/op_utils.h" +#include "paddle/phi/core/flags.h" #include "paddle/utils/string/string_helper.h" -PADDLE_DEFINE_EXPORTED_bool( - use_stride_kernel, - true, - "Whether to use strdie kernel if op support stride."); +PHI_DEFINE_EXPORTED_bool(use_stride_kernel, + true, + "Whether to use strdie kernel if op support stride."); PD_DECLARE_int32(low_precision_op_list); PD_DECLARE_bool(enable_api_kernel_fallback); From 179d4264cae53014c256cde6e1eeb8f29a10c2f3 Mon Sep 17 00:00:00 2001 From: ming1753 <61511741+ming1753@users.noreply.github.com> Date: Mon, 4 Sep 2023 13:09:47 +0800 Subject: [PATCH 095/194] Modify MarkTrtEngineOutputs API (#56858) * Modify MarkTrtEngineOutputs API --- paddle/fluid/inference/analysis/argument.h | 1 + .../inference/analysis/ir_pass_manager.cc | 2 ++ .../ir_passes/tensorrt_subgraph_pass.cc | 25 ++++++++++--------- paddle/fluid/inference/api/analysis_config.cc | 5 +++- .../fluid/inference/api/analysis_predictor.cc | 1 + .../inference/api/paddle_analysis_config.h | 4 ++- paddle/fluid/pybind/inference_api.cc | 3 ++- .../api/trt_mark_trt_engine_outputs_test.cc | 8 +++--- 8 files changed, 31 insertions(+), 18 deletions(-) diff --git a/paddle/fluid/inference/analysis/argument.h b/paddle/fluid/inference/analysis/argument.h index b3757886e2fb3..73bd1cb5e6c76 100644 --- a/paddle/fluid/inference/analysis/argument.h +++ b/paddle/fluid/inference/analysis/argument.h @@ -241,6 +241,7 @@ struct Argument { DECL_ARGUMENT_FIELD(tensorrt_workspace_size, TensorRtWorkspaceSize, int64_t); DECL_ARGUMENT_FIELD(tensorrt_min_subgraph_size, TensorRtMinSubgraphSize, int); DECL_ARGUMENT_FIELD(trt_mark_output, TRTMarkOutput, bool); + DECL_ARGUMENT_FIELD(trt_mark_output_with_id, TRTMarkOutputWithId, bool); DECL_ARGUMENT_FIELD(trt_output_tensor_names, TRTOutputTensorNames, std::vector); diff --git a/paddle/fluid/inference/analysis/ir_pass_manager.cc b/paddle/fluid/inference/analysis/ir_pass_manager.cc index 47091b347c6c7..ab3f1de01bd86 100644 --- a/paddle/fluid/inference/analysis/ir_pass_manager.cc +++ b/paddle/fluid/inference/analysis/ir_pass_manager.cc @@ -163,6 +163,8 @@ void IRPassManager::CreatePasses(Argument *argument, pass->Set("min_subgraph_size", new int(argument->tensorrt_min_subgraph_size())); pass->Set("mark_output", new bool(argument->trt_mark_output())); + pass->Set("mark_output_with_id", + new bool(argument->trt_mark_output_with_id())); pass->Set( "output_tensor_names", new std::vector(argument->trt_output_tensor_names())); diff --git a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc index e65aff1118090..1fc24afe4942a 100644 --- a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc +++ b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc @@ -376,29 +376,30 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp( std::vector origin_outputs_dtype; std::map map_origin_outputs_dtype; - // Whether to mark Outpus + // Mark TensorRT output nodes as trt outputs auto mark_output = Get("mark_output"); auto output_tensor_name = Get>("output_tensor_names"); - VLOG(1) << "mark Output: " << mark_output; + auto mark_output_with_id = Get("mark_output_with_id"); - if (mark_output == 1) { + if (mark_output) { VLOG(1) << "begin to mark output ..."; for (auto node : subgraph) { if (node->NodeType() == Node::Type::kOperation) { - if (node->Op()->Outputs().count("Xshape")) continue; for (auto *x : node->outputs) { if (std::count(parameters.begin(), parameters.end(), x->Name()) > 0) continue; - if (!output_tensor_name.empty() && - std::count(output_tensor_name.begin(), - output_tensor_name.end(), - x->Name())) { - VLOG(1) << "output " << x->Name() << " has been marked"; - std::string output_name_withid = - x->Name() + std::to_string(x->id()); + std::string name_with_id = x->Name() + std::to_string(x->id()); + if (((!mark_output_with_id && std::count(output_tensor_name.begin(), + output_tensor_name.end(), + x->Name()) > 0) || + (mark_output_with_id && std::count(output_tensor_name.begin(), + output_tensor_name.end(), + name_with_id) > 0)) && + !x->outputs.empty()) { + VLOG(3) << "output " << x->Name() << " has been marked"; output_names.insert(x->Name()); - output_names_with_id.insert(output_name_withid); + output_names_with_id.insert(name_with_id); origin_name_output_rank[x->Name()] = x->Var()->GetShape().size(); trt_outputs.insert(x); map_origin_outputs_dtype[x->Name()] = diff --git a/paddle/fluid/inference/api/analysis_config.cc b/paddle/fluid/inference/api/analysis_config.cc index 3f9ca0a58ed63..efd450f2bf5ca 100644 --- a/paddle/fluid/inference/api/analysis_config.cc +++ b/paddle/fluid/inference/api/analysis_config.cc @@ -461,6 +461,7 @@ AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) { CP_MEMBER(tensorrt_min_subgraph_size_); CP_MEMBER(tensorrt_precision_mode_); CP_MEMBER(trt_mark_output_); + CP_MEMBER(trt_mark_output_with_id_); CP_MEMBER(trt_output_tensor_names_); CP_MEMBER(trt_disabled_ops_); CP_MEMBER(trt_use_dla_); @@ -762,8 +763,10 @@ void AnalysisConfig::EnableTensorRtEngine(int64_t workspace_size, } void AnalysisConfig::MarkTrtEngineOutputs( - const std::vector &output_tensor_names) { + const std::vector &output_tensor_names, + const bool mark_output_with_id) { trt_mark_output_ = true; + trt_mark_output_with_id_ = mark_output_with_id; trt_output_tensor_names_ = output_tensor_names; } diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 1fb7e2c157134..83f75c1ae0703 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -1392,6 +1392,7 @@ void AnalysisPredictor::PrepareArgument() { argument_->SetTensorRtMaxBatchSize(config_.tensorrt_max_batchsize_); argument_->SetTensorRtMinSubgraphSize(config_.tensorrt_min_subgraph_size_); argument_->SetTRTMarkOutput(config_.trt_mark_output_); + argument_->SetTRTMarkOutputWithId(config_.trt_mark_output_with_id_); argument_->SetTRTOutputTensorNames(config_.trt_output_tensor_names_); argument_->SetTensorRtDisabledOPs(config_.trt_disabled_ops_); argument_->SetTensorRtUseDLA(config_.trt_use_dla_); diff --git a/paddle/fluid/inference/api/paddle_analysis_config.h b/paddle/fluid/inference/api/paddle_analysis_config.h index f1d193d0640d9..7348418d6e562 100644 --- a/paddle/fluid/inference/api/paddle_analysis_config.h +++ b/paddle/fluid/inference/api/paddle_analysis_config.h @@ -695,7 +695,8 @@ struct PD_INFER_DECL AnalysisConfig { /// \param output_tensor_names The name of the Tensor that needs to be marked /// void MarkTrtEngineOutputs( - const std::vector& output_tensor_names = {}); + const std::vector& output_tensor_names = {}, + const bool trt_mark_output_with_id = false); /// /// \brief Turn on the TensorRT memory optimization. /// @@ -1237,6 +1238,7 @@ struct PD_INFER_DECL AnalysisConfig { bool trt_use_varseqlen_{false}; bool trt_with_interleaved_{false}; bool trt_mark_output_{false}; + bool trt_mark_output_with_id_{false}; std::vector trt_output_tensor_names_{}; std::string tensorrt_transformer_posid_{""}; std::string tensorrt_transformer_maskid_{""}; diff --git a/paddle/fluid/pybind/inference_api.cc b/paddle/fluid/pybind/inference_api.cc index 16131ad12eaeb..1690d738a2c60 100644 --- a/paddle/fluid/pybind/inference_api.cc +++ b/paddle/fluid/pybind/inference_api.cc @@ -896,7 +896,8 @@ void BindAnalysisConfig(py::module *m) { &AnalysisConfig::tensorrt_dynamic_shape_enabled) .def("mark_trt_engine_outputs", &AnalysisConfig::MarkTrtEngineOutputs, - py::arg("output_tensor_names") = std::vector({})) + py::arg("output_tensor_names") = std::vector({}), + py::arg("mark_output_with_id") = false) .def("enable_tensorrt_varseqlen", &AnalysisConfig::EnableVarseqlen) .def("tensorrt_varseqlen_enabled", &AnalysisConfig::tensorrt_varseqlen_enabled) diff --git a/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc b/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc index d34d640cfaf40..7157f442ae66d 100644 --- a/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc +++ b/test/cpp/inference/api/trt_mark_trt_engine_outputs_test.cc @@ -24,9 +24,11 @@ TEST(TensorRT, mark_trt_engine_outputs) { config.EnableUseGpu(100, 0); config.EnableTensorRtEngine( 1 << 30, 1, 5, AnalysisConfig::Precision::kFloat32, false, false); - // The name of the tensor that needs to be marked, the default is empty (all - // marks) - std::vector markOutput = {"fc_0.tmp_0", "fc_0.tmp_1"}; + // The name of the tensor that needs to be marked + std::vector markOutput = {"pool2d_0.tmp_0", + "elementwise_add_0.tmp_0", + "conv2d_5.tmp_0", + "batch_norm_6.tmp_2"}; config.MarkTrtEngineOutputs(markOutput); std::vector> inputs_all; From 11a526ab2d38cc8810d1cf5a6b9a348be0317816 Mon Sep 17 00:00:00 2001 From: WangZhen <23097963+0x45f@users.noreply.github.com> Date: Mon, 4 Sep 2023 14:01:31 +0800 Subject: [PATCH 096/194] [NewIR]Call _C_ops.xx in both dygraph and static mode (#56809) --- .gitignore | 2 +- paddle/fluid/eager/api/utils/global_utils.cc | 2 + paddle/fluid/eager/api/utils/global_utils.h | 3 +- .../generator/CMakeLists.txt | 17 +++-- .../generator/python_c_gen.py | 67 +++++++++++++++++-- paddle/fluid/imperative/tracer.cc | 2 +- .../ir/dialect/op_generator/ops_api_gen.py | 48 ++++++++++++- paddle/fluid/pybind/.gitignore | 2 +- python/paddle/_C_ops.py | 8 +++ python/paddle/__init__.py | 1 + python/paddle/fluid/framework.py | 62 +++++++++++++++-- python/paddle/framework/__init__.py | 2 + .../paddle/jit/dy2static/partial_program.py | 5 +- python/paddle/tensor/stat.py | 8 +-- test/legacy_test/test_inference_api.py | 2 + 15 files changed, 203 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index cc68215a8d2dc..6abc4d02a4392 100644 --- a/.gitignore +++ b/.gitignore @@ -72,7 +72,7 @@ tools/nvcc_lazy # This file is automatically generated. # TODO(zhiqiang) Move this file to build directory. -paddle/fluid/pybind/eager_op_function.cc +paddle/fluid/pybind/eager_op_function.* tools/nvcc_lazy paddle/phi/kernels/sparse/gpu/cutlass_generator/all_gemm_operations.h paddle/phi/kernels/sparse/gpu/cutlass_generator/configurations.h diff --git a/paddle/fluid/eager/api/utils/global_utils.cc b/paddle/fluid/eager/api/utils/global_utils.cc index 3a6a05eb1bfc8..9797bfa2c3750 100644 --- a/paddle/fluid/eager/api/utils/global_utils.cc +++ b/paddle/fluid/eager/api/utils/global_utils.cc @@ -18,5 +18,7 @@ namespace egr { Controller* Controller::controller_ = new Controller(); +thread_local std::shared_ptr Controller::tracer_ = + std::make_shared(); } // namespace egr diff --git a/paddle/fluid/eager/api/utils/global_utils.h b/paddle/fluid/eager/api/utils/global_utils.h index 75eb28a6af91d..c65b2fe5bc116 100644 --- a/paddle/fluid/eager/api/utils/global_utils.h +++ b/paddle/fluid/eager/api/utils/global_utils.h @@ -145,8 +145,7 @@ class Controller { private: Controller() = default; static Controller* controller_; - std::shared_ptr tracer_{ - new paddle::imperative::Tracer()}; + static thread_local std::shared_ptr tracer_; std::unordered_map> op_meta_info_map_; /* op_type : {{{grad_outputs}, {grad_inputs}, {input}, {output}, {attrs}}, diff --git a/paddle/fluid/eager/auto_code_generator/generator/CMakeLists.txt b/paddle/fluid/eager/auto_code_generator/generator/CMakeLists.txt index 08b96903ea019..3c5685f580b5b 100644 --- a/paddle/fluid/eager/auto_code_generator/generator/CMakeLists.txt +++ b/paddle/fluid/eager/auto_code_generator/generator/CMakeLists.txt @@ -53,10 +53,14 @@ add_custom_target( ${nodes_h_path} VERBATIM) -set(tmp_python_c_output_path +set(tmp_python_c_source_path "${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/eager_op_function.cc.tmp") -set(python_c_output_path +set(python_c_source_path "${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/eager_op_function.cc") +set(tmp_python_c_header_path + "${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/eager_op_function.h.tmp") +set(python_c_header_path + "${PADDLE_SOURCE_DIR}/paddle/fluid/pybind/eager_op_function.h") add_custom_target( eager_python_c_codegen @@ -64,7 +68,10 @@ add_custom_target( "${PYTHON_EXECUTABLE}" "${PADDLE_SOURCE_DIR}/paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py" "--api_yaml_path=${api_yaml_path},${fwd_api_yaml_path}" - "--output_path=${tmp_python_c_output_path}" - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_python_c_output_path} - ${python_c_output_path} + "--source_path=${tmp_python_c_source_path}" + "--header_path=${tmp_python_c_header_path}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_python_c_source_path} + ${python_c_source_path} + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_python_c_header_path} + ${python_c_header_path} VERBATIM) diff --git a/paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py b/paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py index 7fe53febc5a9b..62f1a2a654cb9 100644 --- a/paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py +++ b/paddle/fluid/eager/auto_code_generator/generator/python_c_gen.py @@ -87,7 +87,7 @@ def FindParsingFunctionFromAttributeType(atype): PYTHON_C_FUNCTION_TEMPLATE = """ -static PyObject * eager_api_{}(PyObject *self, PyObject *args, PyObject *kwargs) {{ +PyObject * eager_api_{}(PyObject *self, PyObject *args, PyObject *kwargs) {{ {} PyThreadState *tstate = nullptr; try {{ @@ -173,6 +173,7 @@ def FindParsingFunctionFromAttributeType(atype): #include "paddle/fluid/pybind/eager.h" #include "paddle/fluid/eager/amp_utils.h" #include "paddle/fluid/eager/eager_amp_auto_cast.h" +#include "paddle/fluid/pybind/eager_op_function.h" namespace paddle {{ namespace pybind {{ @@ -253,6 +254,29 @@ def FindParsingFunctionFromAttributeType(atype): }} """ +PYTHON_C_H_TEMPLATE = """ +#pragma once + +#include + +// Avoid a problem with copysign defined in pyconfig.h on Windows. +#ifdef copysign +#undef copysign +#endif + +namespace paddle {{ +namespace pybind {{ + +{body} + +}} // namespace pybind +}} // namespace paddle +""" + +PYTHON_C_FUNCTION_DECLARE_TEMPLATE = """ +PyObject *eager_api_{name}(PyObject *self, PyObject *args, PyObject *kwargs); +""" + ##################### # Generator Classes # @@ -279,6 +303,7 @@ def __init__(self, forward_api_contents, namespace): # Generated Results self.python_c_function_str = "" self.python_c_function_reg_str = "" + self.python_c_funcion_declare_str = "" def CollectIsForwardOnly(self): forward_api_contents = self.forward_api_contents @@ -428,6 +453,9 @@ def GeneratePythonCFunction(self): noamp_dygraph_function_str, return_str, ) + self.python_c_funcion_declare_str = ( + PYTHON_C_FUNCTION_DECLARE_TEMPLATE.format(name=forward_api_name) + ) # Set prefix of forward_api_name to avoid conflicts prefix = self.namespace.strip("::") @@ -483,6 +511,12 @@ def GeneratePythonCFunction(self): return_str, ) + python_c_funcion_declare_str = ( + PYTHON_C_FUNCTION_DECLARE_TEMPLATE.format( + name=inplaced_forward_api_name + ) + ) + python_c_inplace_func_reg_str = ( PYTHON_C_FUNCTION_REG_TEMPLATE.format( forward_api_name_prefix, @@ -496,10 +530,14 @@ def GeneratePythonCFunction(self): # self.forward_api_name ending with '_' means it only has inplace api if self.forward_api_name[-1] == '_': self.python_c_function_str = python_c_inplace_func_str + self.python_c_funcion_declare_str = python_c_funcion_declare_str # Generate Python-C Function Registration self.python_c_function_reg_str = python_c_inplace_func_reg_str else: self.python_c_function_str += python_c_inplace_func_str + self.python_c_funcion_declare_str += ( + python_c_funcion_declare_str + ) # Generate Python-C Function Registration self.python_c_function_reg_str += python_c_inplace_func_reg_str @@ -541,6 +579,7 @@ def __init__(self, path): # Generated Result self.python_c_functions_str = "" self.python_c_functions_reg_str = "" + self.python_c_funcion_declare_str = "" def GeneratePythonCFunctions(self): namespace = self.namespace @@ -559,6 +598,9 @@ def GeneratePythonCFunctions(self): self.python_c_functions_reg_str += ( f_generator.python_c_function_reg_str ) + self.python_c_funcion_declare_str += ( + f_generator.python_c_funcion_declare_str + ) def AttachNamespace(self): namespace = self.namespace @@ -570,6 +612,11 @@ def AttachNamespace(self): self.python_c_functions_str = NAMESPACE_WRAPPER_TEMPLATE.format( namespace, python_c_functions_str ) + self.python_c_funcion_declare_str = ( + NAMESPACE_WRAPPER_TEMPLATE.format( + namespace, self.python_c_funcion_declare_str + ) + ) def run(self): # Infer namespace from yaml_path @@ -593,7 +640,8 @@ def ParseArguments(): description='Eager Code Generator Args Parser' ) parser.add_argument('--api_yaml_path', type=str) - parser.add_argument('--output_path', type=str) + parser.add_argument('--source_path', type=str) + parser.add_argument('--header_path', type=str) args = parser.parse_args() return args @@ -631,6 +679,7 @@ def GeneratePythonCFile(filepath, python_c_str): generated_python_c_functions = "" generated_python_c_registration = "" + generated_python_c_functions_header = "" for i in range(len(api_yaml_paths)): api_yaml_path = api_yaml_paths[i] @@ -643,14 +692,22 @@ def GeneratePythonCFile(filepath, python_c_str): generated_python_c_registration += ( py_c_generator.python_c_functions_reg_str ) + generated_python_c_functions_header += ( + py_c_generator.python_c_funcion_declare_str + ) python_c_str = GeneratePythonCWrappers( generated_python_c_functions, generated_python_c_registration ) - output_path = args.output_path - for path in [output_path]: + soucre_path = args.source_path + header_path = args.header_path + for path in [soucre_path, header_path]: if os.path.exists(path): os.remove(path) - GeneratePythonCFile(output_path, python_c_str) + GeneratePythonCFile(soucre_path, python_c_str) + GeneratePythonCFile( + header_path, + PYTHON_C_H_TEMPLATE.format(body=generated_python_c_functions_header), + ) diff --git a/paddle/fluid/imperative/tracer.cc b/paddle/fluid/imperative/tracer.cc index cc00af31e61aa..ffaf95d543d50 100644 --- a/paddle/fluid/imperative/tracer.cc +++ b/paddle/fluid/imperative/tracer.cc @@ -56,7 +56,7 @@ thread_local AmpLevel Tracer::amp_level_ = AmpLevel::O0; thread_local phi::DataType Tracer::amp_dtype_ = phi::DataType::FLOAT32; -static std::shared_ptr g_current_tracer(nullptr); +static thread_local std::shared_ptr g_current_tracer(nullptr); const std::shared_ptr& GetCurrentTracer() { return g_current_tracer; } diff --git a/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py b/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py index 135d75ecf9fd8..529dd1ea3f60c 100644 --- a/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/ops_api_gen.py @@ -21,7 +21,9 @@ #include #include "paddle/fluid/pybind/static_op_function.h" +#include "paddle/fluid/pybind/eager_op_function.h" #include "paddle/phi/core/enforce.h" +#include "paddle/fluid/eager/api/utils/global_utils.h" {body} @@ -44,19 +46,63 @@ FUNCTION_IMPL_TEMPLATE = """ static PyObject *{name}(PyObject *self, PyObject *args, PyObject *kwargs) {{ + if (egr::Controller::Instance().GetCurrentTracer() == nullptr) {{ + VLOG(6) << "Call static_api_{name}"; + return static_api_{name}(self, args, kwargs); + }} else {{ + VLOG(6) << "Call eager_api_{name}"; + return eager_api_{name}(self, args, kwargs); + }} +}}""" + +NO_DY_FUNCTION_IMPL_TEMPLATE = """ +static PyObject *{name}(PyObject *self, PyObject *args, PyObject *kwargs) {{ + VLOG(6) << "Call static_api_{name}"; return static_api_{name}(self, args, kwargs); }}""" OPS_API_TEMPLATE = """ {{"{name}", (PyCFunction)(void (*)(void)){name}, METH_VARARGS | METH_KEYWORDS, "C++ interface function for {name}."}},""" +SPECIAL_STATIC_ONLY_APIS = [ + 'fetch', + 'set_value_with_tensor', + 'set_value_with_tensor_', + 'fused_bn_add_activation_', + 'fused_batch_norm_act_', + 'add_n_', + 'set_value', + 'assign_value', + 'set_value_', + 'embedding_grad_sparse', + 'add_n_with_kernel', + 'print', + 'send_v2', + 'shadow_feed', + 'recv_v2', + 'rnn_', + 'fused_scale_bias_relu_conv_bnstats', + 'batch_norm_', + 'c_allreduce_sum', + 'c_embedding', + 'c_identity', +] + class OpsAPIGen(CodeGen): def __init__(self) -> None: super().__init__() def _gen_one_function_impl(self, name): - return FUNCTION_IMPL_TEMPLATE.format(name=name) + if ( + name.endswith('grad') + or name.endswith('grad_') + or name.endswith('xpu') + or name in SPECIAL_STATIC_ONLY_APIS + ): + return NO_DY_FUNCTION_IMPL_TEMPLATE.format(name=name) + else: + return FUNCTION_IMPL_TEMPLATE.format(name=name) def _gen_one_ops_api(self, name): return OPS_API_TEMPLATE.format(name=name) diff --git a/paddle/fluid/pybind/.gitignore b/paddle/fluid/pybind/.gitignore index fd4c878935d41..75563f45cc281 100644 --- a/paddle/fluid/pybind/.gitignore +++ b/paddle/fluid/pybind/.gitignore @@ -1,3 +1,3 @@ pybind.h -eager_op_function.cc +eager_op_function.* eager_legacy_op_function.cc diff --git a/python/paddle/_C_ops.py b/python/paddle/_C_ops.py index 423d1b12536bd..ccb9009c12358 100644 --- a/python/paddle/_C_ops.py +++ b/python/paddle/_C_ops.py @@ -16,6 +16,14 @@ __all__ = [] +UNIFIED_APIS = ['mean'] + for name in dir(core.eager.ops): globals()[name] = getattr(core.eager.ops, name) __all__.append(name) + +for name in dir(core.ir.ops): + if name in UNIFIED_APIS: + globals()[name] = getattr(core.ir.ops, name) + if name not in __all__: + __all__.append(name) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index b59aadf5cc0df..666935934235f 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -459,6 +459,7 @@ from . import linalg # noqa: F401 from . import fft # noqa: F401 from . import signal # noqa: F401 +from . import _ir_ops # noqa: F401 import paddle.text # noqa: F401 import paddle.vision # noqa: F401 diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 39d589cb2a900..b4442bcedf923 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -55,6 +55,8 @@ 'xpu_places', 'cuda_pinned_places', 'in_dygraph_mode', + 'in_new_ir_mode', + 'in_dynamic_or_new_ir_mode', 'is_compiled_with_cinn', 'is_compiled_with_cuda', 'is_compiled_with_rocm', @@ -102,6 +104,7 @@ def __setattr__(self, name, val): if name == '_dygraph_tracer_': global _dygraph_tracer_ _dygraph_tracer_ = val + core._switch_tracer(val) self.__dict__[name] = val @@ -209,6 +212,59 @@ def in_dygraph_mode(): return global_var._dygraph_tracer_ is not None +def in_new_ir_mode(): + """ + + This API checks whether paddle runs in static graph mode and use new ir api. + + Returns: + bool: Whether paddle runs in static graph mode and use new ir api. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> print(paddle.framework.in_new_ir_mode()) + False + + >>> paddle.enable_static() + >>> paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) + >>> print(paddle.framework.in_new_ir_mode()) + True + + """ + return ir.core._use_new_ir_api() and not in_dygraph_mode() + + +def in_dynamic_or_new_ir_mode(): + """ + + This API checks whether paddle runs in dynamic graph or new ir mode. + + Returns: + bool: Whether paddle runs in static graph mode and use new ir api. + + Examples: + .. code-block:: python + + >>> import paddle + + >>> print(paddle.framework.in_dynamic_or_new_ir_mode()) + True + + >>> paddle.enable_static() + >>> print(paddle.framework.in_dynamic_or_new_ir_mode()) + False + + >>> paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) + >>> print(paddle.framework.in_dynamic_or_new_ir_mode()) + True + + """ + return in_dygraph_mode() or in_new_ir_mode() + + global_ipu_index = -1 global_ipu_stage = -1 ipu_index_attr_name = 'ipu_index' @@ -7604,14 +7660,10 @@ def dygraph_guard_if_declarative(): def _dygraph_guard(tracer): tmp_tracer = global_var._dygraph_tracer_ global_var._dygraph_tracer_ = tracer - if tracer is not None: - core._switch_tracer(tracer) try: yield finally: - if tmp_tracer is not None: - core._switch_tracer(tmp_tracer) global_var._dygraph_tracer_ = tmp_tracer @@ -7622,8 +7674,6 @@ def _static_guard(): try: yield finally: - if tmp_tracer is not None: - core._switch_tracer(tmp_tracer) global_var._dygraph_tracer_ = tmp_tracer diff --git a/python/paddle/framework/__init__.py b/python/paddle/framework/__init__.py index 778e2b55cd793..302acc6b4c4db 100755 --- a/python/paddle/framework/__init__.py +++ b/python/paddle/framework/__init__.py @@ -56,6 +56,8 @@ from ..fluid.dygraph.base import enable_dygraph as disable_static # noqa: F401 from ..fluid.dygraph.base import disable_dygraph as enable_static # noqa: F401 from ..fluid.framework import in_dygraph_mode as in_dynamic_mode # noqa: F401 +from ..fluid.framework import in_new_ir_mode # noqa: F401 +from ..fluid.framework import in_dynamic_or_new_ir_mode # noqa: F401 from ..fluid.framework import ( _current_expected_place, _get_paddle_place, diff --git a/python/paddle/jit/dy2static/partial_program.py b/python/paddle/jit/dy2static/partial_program.py index 0264d645a0676..2c44346054cd6 100644 --- a/python/paddle/jit/dy2static/partial_program.py +++ b/python/paddle/jit/dy2static/partial_program.py @@ -190,7 +190,10 @@ def __init__( assert isinstance(self._build_strategy, BuildStrategy) self._origin_main_program = self._verify_program(main_program) - self._cuda_graph_vec = self._create_cuda_graph_vec() + with paddle.fluid.framework._dygraph_guard( + paddle.fluid.dygraph.Tracer() + ): + self._cuda_graph_vec = self._create_cuda_graph_vec() self._cuda_graph_capture_mode = "" self._cuda_graph_pool_id = 0 # Set default mode to train diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 30d1ee80279cf..04fe17d85546b 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -15,8 +15,8 @@ # TODO: define statistical functions of a tensor import paddle -from paddle import _C_ops, _ir_ops, ir -from paddle.framework import in_dynamic_mode +from paddle import _C_ops +from paddle.framework import in_dynamic_mode, in_dynamic_or_new_ir_mode from ..common_ops_import import Variable from ..fluid.data_feeder import check_type, check_variable_and_dtype @@ -83,11 +83,9 @@ def mean(x, axis=None, keepdim=False, name=None): >>> print(out4.numpy()) [ 8.5 12.5 16.5] """ - if in_dynamic_mode(): + if in_dynamic_or_new_ir_mode(): return _C_ops.mean(x, axis, keepdim) else: - if ir.core._use_new_ir_api(): - return _ir_ops.mean(x, axis, keepdim) reduce_all, axis = _get_reduce_axis_with_tensor(axis, x) check_variable_and_dtype( x, diff --git a/test/legacy_test/test_inference_api.py b/test/legacy_test/test_inference_api.py index 1fb190618e714..4f8cb527f1c6b 100644 --- a/test/legacy_test/test_inference_api.py +++ b/test/legacy_test/test_inference_api.py @@ -190,6 +190,7 @@ def test_lod_tensor(): predictor.run() def test_paddle_tensor(): + paddle.disable_static() config = self.get_config(program, params) predictor = create_predictor(config) in_names = predictor.get_input_names() @@ -197,6 +198,7 @@ def test_paddle_tensor(): in_data = paddle.Tensor(np.ones((1, 6, 32, 32)).astype(np.float32)) in_handle.share_external_data(in_data) predictor.run() + paddle.enable_static() test_lod_tensor() test_paddle_tensor() From 67c84c4555954811016a65bd9323f7ba700211bb Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Mon, 4 Sep 2023 14:45:56 +0800 Subject: [PATCH 097/194] [IR] Support inplace pass (#56672) * add code * add code * refine code * add code * fix bug * fix bug * fix bug * add code * add ut * polish code * fix bug * refine code * fix bug * refine code * fix bug * refine code * fix bug * refine code * fix bug * refine code * add code * fix bug * fix bug * fix bug * fix bug * fix bug * refine code --- .../framework/new_executor/CMakeLists.txt | 1 + .../new_executor/standalone_executor.cc | 15 + .../fluid/ir/dialect/op_generator/op_gen.py | 2 +- .../utils/op_yaml_info_parser.cc | 17 +- .../utils/op_yaml_info_parser.h | 16 +- paddle/fluid/ir/transforms/CMakeLists.txt | 5 + paddle/fluid/ir/transforms/inplace_pass.cc | 331 ++++++++++++++++++ paddle/fluid/ir/transforms/inplace_pass.h | 26 ++ paddle/fluid/pybind/CMakeLists.txt | 1 + paddle/fluid/pybind/ir.cc | 2 + python/paddle/nn/functional/activation.py | 4 + test/ir/new_ir/CMakeLists.txt | 4 +- test/ir/new_ir/test_pd_inplace_pass.py | 48 +++ test/legacy_test/eager_op_test.py | 28 +- 14 files changed, 484 insertions(+), 16 deletions(-) create mode 100644 paddle/fluid/ir/transforms/inplace_pass.cc create mode 100644 paddle/fluid/ir/transforms/inplace_pass.h create mode 100644 test/ir/new_ir/test_pd_inplace_pass.py diff --git a/paddle/fluid/framework/new_executor/CMakeLists.txt b/paddle/fluid/framework/new_executor/CMakeLists.txt index 8d905371991c2..16b18c2d7d6bd 100644 --- a/paddle/fluid/framework/new_executor/CMakeLists.txt +++ b/paddle/fluid/framework/new_executor/CMakeLists.txt @@ -16,6 +16,7 @@ set(STANDALONE_EXECUTOR_DEPS phi_kernel_adaptor program_translator instruction_base + pd_inplace_pass ir) cc_library( diff --git a/paddle/fluid/framework/new_executor/standalone_executor.cc b/paddle/fluid/framework/new_executor/standalone_executor.cc index e37867857a02d..7d65fe33818a1 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor.cc @@ -16,16 +16,24 @@ #include "paddle/fluid/framework/new_executor/feed_fetch_utils.h" #include "paddle/fluid/framework/new_executor/interpreter/interpreter_util.h" #include "paddle/fluid/framework/new_executor/program_interpreter.h" +#include "paddle/fluid/platform/flags.h" #include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h" +#include "paddle/fluid/ir/transforms/inplace_pass.h" #include "paddle/fluid/ir_adaptor/translator/translate.h" #include "paddle/ir/core/program.h" +#include "paddle/ir/pass/pass.h" +#include "paddle/ir/pass/pass_manager.h" PHI_DECLARE_bool(enable_new_ir_in_executor); PHI_DECLARE_bool(enable_new_ir_api); +PADDLE_DEFINE_EXPORTED_bool(new_ir_apply_inplace_pass, + true, + "new ir kernel program apply inplace pass."); + namespace paddle { namespace framework { StandaloneExecutor::StandaloneExecutor(const platform::Place& place, @@ -101,6 +109,13 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, } auto kernel_program = paddle::dialect::PdOpLowerToKernelPass(base_program.get(), place); + + if (FLAGS_new_ir_apply_inplace_pass) { + ir::PassManager pm(ir::IrContext::Instance(), 3); + pm.AddPass(ir::CreateInplacePass()); + pm.Run(kernel_program.get()); + } + interpretercores_.emplace_back( std::make_shared(place_, fetch_var_names_, diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index fcbbf39c211bf..5d51a731c546a 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -856,7 +856,7 @@ def OpGenerator( op_infer_meta_map, muta_attr_is_input=False, ) - if len(op_attribute_name_list) > 1: + if len(op_attribute_name_list) > 0: ( build_args_with_attr_is_map_for_declare, build_func_with_attr_is_map, diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.cc b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.cc index ebed61bf25484..8b5be8ff00cfd 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.cc @@ -84,15 +84,15 @@ const OpRunTimeInfo& OpYamlInfoParser::OpRuntimeInfo() const { return std::get<3>(op_info_tuple_); } -const std::map& OpYamlInfoParser::InputName2Id() const { +const std::map& OpYamlInfoParser::InputName2Id() const { return input_name2id_; } -const std::map& OpYamlInfoParser::OutputName2Id() const { +const std::map& OpYamlInfoParser::OutputName2Id() const { return output_name2id_; } -const std::vector& OpYamlInfoParser::NoNeedBufferIds() const { +const std::vector& OpYamlInfoParser::NoNeedBufferIds() const { return no_need_buffer_ids_; } @@ -118,6 +118,17 @@ const std::string& OpYamlInfoParser::InplaceName( "Can not find inplace input of [%s].", out_name)); } +std::unordered_map OpYamlInfoParser::GetInplaceIdMap() + const { + std::unordered_map inplace_id_map; + auto& inplace_info = std::get<3>(op_info_tuple_).inplace; + for (const auto& info : inplace_info) { + inplace_id_map[OutputName2Id().at(info.first)] = + InputName2Id().at(info.second); + } + return inplace_id_map; +} + bool OpYamlInfoParser::HasView(const std::string& out_name) const { auto& view_info = std::get<3>(op_info_tuple_).view; for (size_t i = 0; i < view_info.size(); i++) { diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h index 29d10ecd8cd66..acbc1b8e19649 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h @@ -34,10 +34,10 @@ class OpYamlInfoParser { const std::vector& TensorParams(bool is_kernel = false) const; const std::vector& AttrParams(bool is_kernel = false) const; const OpRunTimeInfo& OpRuntimeInfo() const; - const std::map& InputName2Id() const; - const std::map& OutputName2Id() const; + const std::map& InputName2Id() const; + const std::map& OutputName2Id() const; - const std::vector& NoNeedBufferIds() const; + const std::vector& NoNeedBufferIds() const; const std::vector& InputNames() const { return input_name_list_; @@ -53,6 +53,8 @@ class OpYamlInfoParser { const std::string& InplaceName(const std::string& out_name) const; + std::unordered_map GetInplaceIdMap() const; + bool HasView(const std::string& out_name) const; const std::string& ViewName(const std::string& out_name) const; @@ -68,20 +70,20 @@ class OpYamlInfoParser { OpInfoTuple op_info_tuple_; // input info - std::map input_name2id_; + std::map input_name2id_; std::vector input_name_list_; std::map input_info_; - int input_tensor_number_{0}; + uint32_t input_tensor_number_{0}; // no_need_buffer_ids - std::vector no_need_buffer_ids_; + std::vector no_need_buffer_ids_; // attribute info std::vector attribute_name_list_; std::map attr_info_; // output info - std::map output_name2id_; + std::map output_name2id_; std::vector output_name_list_; std::map output_info_; diff --git a/paddle/fluid/ir/transforms/CMakeLists.txt b/paddle/fluid/ir/transforms/CMakeLists.txt index 80e14cd5ba867..36e06410d338a 100644 --- a/paddle/fluid/ir/transforms/CMakeLists.txt +++ b/paddle/fluid/ir/transforms/CMakeLists.txt @@ -12,3 +12,8 @@ cc_library( _constant_folding_pass SRCS constant_folding_pass.cc DEPS standalone_executor pd_op_to_kernel_pass transform_general_functions) + +cc_library( + pd_inplace_pass + SRCS inplace_pass.cc + DEPS pd_dialect_core op_yaml_info_parser) diff --git a/paddle/fluid/ir/transforms/inplace_pass.cc b/paddle/fluid/ir/transforms/inplace_pass.cc new file mode 100644 index 0000000000000..2ce0f663ecd92 --- /dev/null +++ b/paddle/fluid/ir/transforms/inplace_pass.cc @@ -0,0 +1,331 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/fluid/ir/transforms/inplace_pass.h" + +#include "paddle/fluid/ir/dialect/paddle_dialect/interface/op_yaml_info.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/trait/inplace.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" +#include "paddle/ir/core/builtin_op.h" +#include "paddle/ir/core/operation.h" +#include "paddle/ir/pass/pass.h" +#include "paddle/ir/pass/pass_registry.h" + +// NOTE(zhangbo): Which kind of value can be deleted? +// (1) Value's type needs to be AllocatedDenseTensorType or +// AllocatedSelectedRowsType; (2) Value's is not persisable. +bool CanBeDeleted(ir::Value value) { + if (!value.type()) { + return false; + } + if (!value.type().isa() && + !value.type().isa()) { + return false; + } + if (value.GetDefiningOp()->HasAttribute(kAttrIsPersisable)) { + return !(value.GetDefiningOp() + ->attribute(kAttrIsPersisable) + .dyn_cast<::ir::ArrayAttribute>() + .AsVector()[value.dyn_cast<::ir::OpResult>().GetResultIndex()] + .dyn_cast<::ir::BoolAttribute>() + .data()); + } + return true; +} + +bool CanDoInplace(const std::unordered_set& eager_dels, + ir::Value input, + ir::Value output) { + if (input.type() != output.type()) { + VLOG(9) << " -- input's type != output's type, can't do inplace"; + return false; + } + if (eager_dels.count(input) == 0) { + VLOG(9) << " -- input not in eager_deletion_valus, can't do inplace"; + return false; + } + return true; +} + +bool IsNoNeedBuffer(ir::Operation* op, ir::Value value) { + if (op->dialect()->name().compare( + paddle::dialect::PaddleKernelDialect::name()) != 0) { + VLOG(8) << op->name() + << "is not a kernel_dialect op, no need buffer is false"; + return false; + } + auto op_name = + op->attributes().at("op_name").dyn_cast<::ir::StrAttribute>().AsString(); + ir::OpInfo op_info = ir::IrContext::Instance()->GetRegisteredOpInfo(op_name); + if (op_info) { + auto info_interface = + op_info.GetInterfaceImpl(); + if (info_interface) { + paddle::dialect::OpYamlInfoParser info_parser( + info_interface->get_op_info_()); + auto& no_need_buffer_ids = info_parser.NoNeedBufferIds(); + for (size_t id = 0; id < no_need_buffer_ids.size(); id++) { + if (value == op->operand_source(no_need_buffer_ids[id])) { + return true; + } + } + } + } + return false; +} + +// NOTE(zhangbo): pd.feed's output and pd.fetch's input can not be eager +// deleted. +std::unordered_set GetSkipDeletionValues(ir::Block* block) { + std::unordered_set skip_dels; + for (auto& op : *block) { + if (op->dialect()->name().compare( + paddle::dialect::PaddleKernelDialect::name()) != 0) { + continue; + } + IR_ENFORCE(op->attributes().count("op_name") > 0, + "kernel_dialect op should own an 'op_name' attribute."); + auto upper_op_name = op->attributes() + .at("op_name") + .dyn_cast<::ir::StrAttribute>() + .AsString(); + + if (upper_op_name == "pd.feed" || upper_op_name == "pd.data") { + skip_dels.insert(op->result(0)); + continue; + } + if (upper_op_name == "pd.fetch" || upper_op_name == "pd.shadow_output") { + skip_dels.insert(op->operand_source(0)); + continue; + } + } + return skip_dels; +} + +// NOTE(zhangbo): For inplace Pass, currently only the kernel_dialect operator +// is supported. Therefore, this function only returns the values in the +// kernel_dialect operator that can be eager deleted. +std::unordered_map> +GetEagerDeletionValues(ir::Block* block) { + std::unordered_set skip_dels = GetSkipDeletionValues(block); + + std::unordered_map del_value_2_op; + for (auto& op : *block) { + std::string upper_op_name = op->name(); + if (op->dialect()->name().compare( + paddle::dialect::PaddleKernelDialect::name()) == 0) { + IR_ENFORCE(op->attributes().count("op_name") > 0, + "kernel_dialect op should own an 'op_name' attribute."); + upper_op_name = op->attributes() + .at("op_name") + .dyn_cast<::ir::StrAttribute>() + .AsString(); + } + + for (size_t i = 0; i < op->num_operands(); ++i) { + auto input = op->operand_source(i); + if (skip_dels.count(input) > 0 || !input || !CanBeDeleted(input) || + IsNoNeedBuffer(op, input)) { + VLOG(6) << "The " << i << "-th input value of the Operation(" + << upper_op_name << ") can not be deleted."; + VLOG(8) << " -- skip dels: " << skip_dels.count(input); + VLOG(8) << " -- value is null: " << !input; + VLOG(8) << " -- can be deleted: " << !CanBeDeleted(input); + VLOG(8) << " -- is no_need_buffer: " << IsNoNeedBuffer(op, input); + continue; + } + del_value_2_op[input] = op; + } + + for (size_t i = 0; i < op->num_results(); ++i) { + ir::Value output = op->result(i); + if (output && CanBeDeleted(output)) { + del_value_2_op[output] = op; + } + } + } + + std::unordered_map> eager_dels; + for (auto& kv : del_value_2_op) { + eager_dels[kv.second].insert(kv.first); + } + + return eager_dels; +} + +std::unordered_map GetInplaceOps( + ir::Block* block) { + const auto eager_dels = GetEagerDeletionValues(block); + + std::unordered_map inplace_ops; + + std::unordered_set visited_values; + std::unordered_set reused_input_values; + std::unordered_set reused_output_values; + + for (auto& op : *block) { + for (size_t i = 0; i < op->num_operands(); ++i) { + visited_values.insert(op->operand_source(i)); + } + + if (op->dialect()->name().compare( + paddle::dialect::PaddleKernelDialect::name()) != 0) { + VLOG(6) << op->name() + << "is not a kernel_dialect op, inplace only support " + "kernel_dialect operators"; + for (size_t i = 0; i < op->num_results(); ++i) { + visited_values.insert(op->result(i)); + } + continue; + } + + auto upper_op_attrs = op->attributes(); + auto upper_op_name = + upper_op_attrs.at("op_name").dyn_cast<::ir::StrAttribute>().AsString(); + VLOG(6) << "analyse op: " << upper_op_name; + + if (upper_op_attrs.count("is_inplace") != 0 && + upper_op_attrs.at("is_inplace").dyn_cast().data()) { + VLOG(6) << upper_op_name << " is already an inplace op."; + for (size_t i = 0; i < op->num_operands(); ++i) { + reused_input_values.insert(op->operand_source(i)); + } + for (size_t i = 0; i < op->num_results(); ++i) { + reused_output_values.insert(op->result(i)); + visited_values.insert(op->result(i)); + } + continue; + } + + ir::OpInfo upper_inplace_op_info = + ir::IrContext::Instance()->GetRegisteredOpInfo(upper_op_name + "_"); + + if (eager_dels.count(op) == 0 || (!upper_inplace_op_info)) { + VLOG(6) << upper_op_name + << "'s value can't delete or doesn't have inplace op, so that " + "can't do inplace."; + for (size_t i = 0; i < op->num_results(); ++i) { + visited_values.insert(op->result(i)); + } + continue; + } + + auto upper_inplace_op_interface = + upper_inplace_op_info + .GetInterfaceImpl(); + PADDLE_ENFORCE_NOT_NULL( + upper_inplace_op_interface, + phi::errors::PreconditionNotMet( + "can not find OpYamlInfoInterface from [%s]", upper_op_name + "_")); + paddle::dialect::OpYamlInfoParser upper_inplace_op_info_parser( + upper_inplace_op_interface->get_op_info_()); + std::unordered_map inplace_out_2_in = + upper_inplace_op_info_parser.GetInplaceIdMap(); + + bool can_do_inplace = true; + for (auto& kv : inplace_out_2_in) { + uint32_t out_slot = kv.first; + uint32_t in_slot = kv.second; + if ((in_slot >= op->num_operands()) || (out_slot >= op->num_results()) || + (!CanDoInplace(eager_dels.at(op), + op->operand_source(in_slot), + op->result(out_slot))) || + (visited_values.count(op->result(out_slot)) > 0) || + (!CanBeDeleted(op->result(out_slot))) || + (reused_input_values.count(op->operand_source(in_slot)) > 0) || + (reused_output_values.count(op->result(out_slot)) > 0)) { + can_do_inplace = false; + VLOG(6) << upper_op_name + << "'s value has been visited or reused by other inplace op, " + "so that can't do inplace."; + VLOG(8) << " -- operand " << in_slot << " and result " << out_slot + << " can do inplace: " + << CanDoInplace(eager_dels.at(op), + op->operand_source(in_slot), + op->result(out_slot)); + VLOG(8) << " -- result " << out_slot << " visited: " + << (visited_values.count(op->result(out_slot)) > 0); + VLOG(8) << " -- operand " << in_slot << " has been reused: " + << (reused_input_values.count(op->operand_source(in_slot)) > 0); + VLOG(8) << " -- result " << out_slot << " has been reused: " + << (reused_output_values.count(op->result(out_slot)) > 0); + break; + } + } + if (can_do_inplace) { + inplace_ops[op] = upper_op_name + "_"; + for (auto& kv : inplace_out_2_in) { + reused_input_values.insert(op->operand_source(kv.second)); + reused_output_values.insert(op->result(kv.first)); + } + VLOG(6) << upper_op_name + << " will change to inplace version op: " << upper_op_name + "_"; + } + + for (size_t i = 0; i < op->num_results(); ++i) { + visited_values.insert(op->result(i)); + } + } + return inplace_ops; +} + +class InplacePass : public ir::Pass { + public: + InplacePass() : ir::Pass("InplacePass", 3) {} + + void Run(ir::Operation* op) override { + auto module_op = op->dyn_cast(); + IR_ENFORCE(module_op, "DcePass should run on module op."); + auto* block = module_op.block(); + + auto inplace_ops = GetInplaceOps(block); + + for (auto kv : inplace_ops) { + VLOG(6) << "Do inplace for: " + << kv.first->attributes() + .at("op_name") + .dyn_cast<::ir::StrAttribute>() + .AsString(); + ir::Block::iterator insert_pos = + std::find(block->begin(), block->end(), kv.first); + IR_ENFORCE(insert_pos != block->end(), + "Operator %s not found in block.", + kv.first->name()); + + kv.first->set_attribute( + "op_name", + ir::StrAttribute::get(ir::IrContext::Instance(), kv.second)); + kv.first->set_attribute( + "is_inplace", + ir::BoolAttribute::get(ir::IrContext::Instance(), true)); + } + } + + bool CanApplyOn(ir::Operation* op) const override { + return op->name() == "builtin.module" && op->num_regions() > 0; + } +}; + +namespace ir { + +std::unique_ptr CreateInplacePass() { + return std::make_unique(); +} + +} // namespace ir + +REGISTER_PASS(inplace, InplacePass); diff --git a/paddle/fluid/ir/transforms/inplace_pass.h b/paddle/fluid/ir/transforms/inplace_pass.h new file mode 100644 index 0000000000000..028d6a9eb94e8 --- /dev/null +++ b/paddle/fluid/ir/transforms/inplace_pass.h @@ -0,0 +1,26 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include "paddle/ir/core/dll_decl.h" + +namespace ir { + +class Pass; + +std::unique_ptr CreateInplacePass(); + +} // namespace ir diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 478efa7a5af95..30cb90a5d2042 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -41,6 +41,7 @@ set(PYBIND_DEPS phi_kernel_adaptor pd_dialect program_translator + pd_inplace_pass ir new_profiler jit_layer diff --git a/paddle/fluid/pybind/ir.cc b/paddle/fluid/pybind/ir.cc index 67da2ba77dbfb..2838bfa2fb2b9 100644 --- a/paddle/fluid/pybind/ir.cc +++ b/paddle/fluid/pybind/ir.cc @@ -30,6 +30,7 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" +#include "paddle/fluid/ir/transforms/inplace_pass.h" #include "paddle/fluid/ir_adaptor/translator/translate.h" #include "paddle/fluid/ir_adaptor/translator/utils.h" #include "paddle/ir/core/block.h" @@ -59,6 +60,7 @@ using paddle::dialect::DenseTensorType; using pybind11::return_value_policy; USE_PASS(dead_code_elimination); +USE_PASS(inplace); namespace paddle { namespace pybind { diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index dbbed03b3aac4..bc8a49d3e8c3e 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -759,6 +759,10 @@ def relu(x, name=None): if in_dynamic_mode(): return _C_ops.relu(x) else: + if paddle.ir.core._use_new_ir_api(): + # Below code will be removed after we can generate IR api automatically + return paddle._ir_ops.relu(x) + check_variable_and_dtype( x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'relu' ) diff --git a/test/ir/new_ir/CMakeLists.txt b/test/ir/new_ir/CMakeLists.txt index 9ed0319d88012..943f7b42ba15a 100644 --- a/test/ir/new_ir/CMakeLists.txt +++ b/test/ir/new_ir/CMakeLists.txt @@ -4,7 +4,7 @@ file( "test_*.py") string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") -set(TEST_IR_SYSTEM_CASES test_build_model) +set(TEST_IR_SYSTEM_CASES test_build_model test_pd_inplace_pass) list(REMOVE_ITEM TEST_INTERP_CASES ${TEST_IR_SYSTEM_CASES}) foreach(target ${TEST_INTERP_CASES}) @@ -16,3 +16,5 @@ foreach(target ${TEST_IR_SYSTEM_CASES}) py_test_modules(${target} MODULES ${target} ENVS GLOG_v=1 FLAGS_enable_new_ir_api=true) endforeach() + +set_tests_properties(test_pd_inplace_pass PROPERTIES TIMEOUT 60) diff --git a/test/ir/new_ir/test_pd_inplace_pass.py b/test/ir/new_ir/test_pd_inplace_pass.py new file mode 100644 index 0000000000000..e00f34389d3b4 --- /dev/null +++ b/test/ir/new_ir/test_pd_inplace_pass.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle.fluid import core + +paddle.enable_static() + + +class TestPdInplacePass(unittest.TestCase): + def test_pd_inplace_pass(self): + place = core.Place() + place.set_place(paddle.CPUPlace()) + new_scope = paddle.static.Scope() + main_program = paddle.static.Program() + with paddle.static.scope_guard(new_scope): + with paddle.static.program_guard(main_program): + x = paddle.static.data('x', [2, 2], dtype='float32') + y = paddle.ones([2, 2], dtype='float32') + z = paddle.divide(x, y) + out = paddle.nn.functional.relu(z) + + exe = paddle.static.Executor() + x_feed = np.ones([2, 2], dtype=np.float32) * 10 + (sum_value,) = exe.run(feed={'x': x_feed}, fetch_list=[out]) + self.assertEqual( + (sum_value == np.ones([2, 2], dtype="float32") * 10).all(), + True, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index 95715c2dc84e1..817d39b7d879d 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -1370,9 +1370,19 @@ def _check_ir_output(self, place, program, feed_map, fetch_list, outs): return if self._check_cinn: return - stored_flag = get_flags('FLAGS_enable_new_ir_in_executor') + stored_flag = get_flags( + [ + 'FLAGS_enable_new_ir_in_executor', + "FLAGS_new_ir_apply_inplace_pass", + ] + ) try: - set_flags({"FLAGS_enable_new_ir_in_executor": True}) + set_flags( + { + "FLAGS_enable_new_ir_in_executor": True, + "FLAGS_new_ir_apply_inplace_pass": 0, + } + ) new_scope = paddle.static.Scope() executor = Executor(place) new_program = None @@ -3215,9 +3225,19 @@ def _check_ir_grad_output( if self._check_cinn: return - stored_flag = get_flags('FLAGS_enable_new_ir_in_executor') + stored_flag = get_flags( + [ + 'FLAGS_enable_new_ir_in_executor', + "FLAGS_new_ir_apply_inplace_pass", + ] + ) try: - set_flags({"FLAGS_enable_new_ir_in_executor": True}) + set_flags( + { + "FLAGS_enable_new_ir_in_executor": True, + "FLAGS_new_ir_apply_inplace_pass": 0, + } + ) executor = Executor(place) new_gradients = list( map( From 10d60b7304829c4745e6ae7fb4d43436feeab369 Mon Sep 17 00:00:00 2001 From: tianshuo78520a <707759223@qq.com> Date: Mon, 4 Sep 2023 15:20:47 +0800 Subject: [PATCH 098/194] Fix eigen build (#56774) * Fix eigen build * Fix * Fix --- patches/eigen/Complex.h.patch | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/patches/eigen/Complex.h.patch b/patches/eigen/Complex.h.patch index 140e4ba840940..d005ea7f5d6f2 100644 --- a/patches/eigen/Complex.h.patch +++ b/patches/eigen/Complex.h.patch @@ -1,7 +1,7 @@ -diff --git a/Eigen/src/Core/arch/SSE/Complex.h b/Eigen/src/Core/arch/SSE/Complex.h -index f6f1b8c..2f4e7d1 100644 ---- a/Eigen/src/Core/arch/SSE/Complex.h -+++ b/Eigen/src/Core/arch/SSE/Complex.h +diff --git a/old/Complex.h b/new/Complex.h +index f6f1b8c..7558e8c 100644 +--- a/old/Complex.h ++++ b/new/Complex.h @@ -17,7 +17,7 @@ namespace internal { //---------- float ---------- struct Packet2cf @@ -11,3 +11,28 @@ index f6f1b8c..2f4e7d1 100644 EIGEN_STRONG_INLINE explicit Packet2cf(const __m128& a) : v(a) {} __m128 v; }; +@@ -113,19 +113,13 @@ template<> EIGEN_STRONG_INLINE Packet2cf ploadu(const std::complex EIGEN_STRONG_INLINE Packet2cf pset1(const std::complex& from) + { + Packet2cf res; +-#if EIGEN_GNUC_AT_MOST(4,2) +- // Workaround annoying "may be used uninitialized in this function" warning with gcc 4.2 +- res.v = _mm_loadl_pi(_mm_set1_ps(0.0f), reinterpret_cast(&from)); +-#elif EIGEN_GNUC_AT_LEAST(4,6) +- // Suppress annoying "may be used uninitialized in this function" warning with gcc >= 4.6 +- #pragma GCC diagnostic push +- #pragma GCC diagnostic ignored "-Wuninitialized" +- res.v = _mm_loadl_pi(res.v, (const __m64*)&from); +- #pragma GCC diagnostic pop ++#ifdef EIGEN_VECTORIZE_SSE3 ++ res.v = _mm_castpd_ps(_mm_loaddup_pd(reinterpret_cast(&from))); + #else +- res.v = _mm_loadl_pi(res.v, (const __m64*)&from); ++ res.v = _mm_castpd_ps(_mm_load_sd(reinterpret_cast(&from))); ++ res.v = _mm_movelh_ps(res.v, res.v); + #endif +- return Packet2cf(_mm_movelh_ps(res.v,res.v)); ++ return res; + } + + template<> EIGEN_STRONG_INLINE Packet2cf ploaddup(const std::complex* from) { return pset1(*from); } From 0e74bf363c303821f1b52975dfb87219bab587e5 Mon Sep 17 00:00:00 2001 From: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com> Date: Mon, 4 Sep 2023 15:40:55 +0800 Subject: [PATCH 099/194] [NewIR]support c_allreduce_sum/c_identity/c_embedding/c_embedding_grad (#56836) * [NewIR]add c_allreduce_sum/c_identity/c_reduce_sum/c_embedding/c_embedding_grad * rm VLOG * rm c_identity from LegacyOpList * rm VLOG * rm c_reduce_sum --- .../ir/dialect/paddle_dialect/utils/utils.cc | 4 +- paddle/phi/api/yaml/legacy_backward.yaml | 11 ++++++ paddle/phi/api/yaml/legacy_ops.yaml | 31 ++++++++++++++++ paddle/phi/api/yaml/op_compat.yaml | 19 ++++++++++ paddle/phi/infermeta/binary.cc | 37 +++++++++++++++++++ paddle/phi/infermeta/binary.h | 5 +++ 6 files changed, 106 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc index 1216589aeb584..e0ec875ca00d6 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.cc @@ -27,7 +27,9 @@ const std::unordered_set LegacyOpList = { "pd.c_sync_calc_stream_", "pd.c_sync_comm_stream_", "pd.send_v2", - "pd.recv_v2"}; + "pd.recv_v2", + "pd.c_allreduce_sum", + "pd.c_allreduce_sum_"}; enum class AttrType { UNDEFINED = 0, diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 56a96c05420cb..ddeb3b3d4f81c 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -102,6 +102,17 @@ composite: batch_norm_grad(x, scale, bias, mean_out, variance_out, saved_mean, saved_variance, reserve_space, out_grad, momentum, epsilon, data_layout, is_test, use_global_stats, trainable_statistics) backward : batch_norm_double_grad +- backward_op : c_embedding_grad + forward : c_embedding (Tensor weight, Tensor x, int64_t start_index=0) -> Tensor(out) + args : (Tensor weight, Tensor x, Tensor out_grad, int64_t start_index=0) + output : Tensor(weight_grad) + infer_meta : + func : EmbeddingGradInferMeta + param : [x, weight] + kernel : + func : c_embedding_grad + no_need_buffer : weight + - backward_op : cast_grad forward : cast (Tensor x, DataType dtype) -> Tensor(out) args : (Tensor x, Tensor out_grad) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index f760c6f96aac3..fcf4b2f28bb1e 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -123,6 +123,16 @@ backward : batch_norm_grad optional : reserve_space +- op : c_allreduce_sum + args : (Tensor x, int ring_id, bool use_calc_stream, bool use_model_parallel) + output : Tensor(out) + infer_meta : + func : AllReduceInferMeta + param : [x] + kernel : + func : c_allreduce_sum + inplace : (x -> out) + - op : c_broadcast args : (Tensor x, int ring_id=0, int root=0, bool use_calc_stream=false) output : Tensor(out) @@ -142,6 +152,27 @@ kernel : func : c_concat +- op : c_embedding + args : (Tensor weight, Tensor x, int64_t start_index=0) + output : Tensor(out) + infer_meta : + func : CEmbeddingInferMeta + param : [weight, x, start_index] + kernel : + func : c_embedding + param : [weight, x, start_index] + data_type : weight + backward : c_embedding_grad + +- op : c_identity + args : (Tensor x, int ring_id, bool use_calc_stream, bool use_model_parallel) + output : Tensor(out) + infer_meta : + func : CIdentityInferMeta + kernel : + func : c_identity + inplace : (x -> out) + - op : c_sync_calc_stream args : (Tensor x) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 29c5983176d1d..77b023e84c68c 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -435,6 +435,13 @@ outputs : out : Out +- op : c_embedding + backward : c_embedding_grad + inputs : + {weight : W, x : Ids} + outputs : + out : Out + - op : cast inputs : x : X @@ -3032,12 +3039,24 @@ yolo_loss : GetYoloLossExpectedKernelType yolo_loss_grad : GetYoloLossExpectedKernelType +- op: c_allreduce_sum + inputs : + x : X + outputs : + out: Out + - op: c_broadcast inputs : x : X outputs : out : Out +- op: c_identity + inputs : + x : X + outputs : + out: Out + - op: c_sync_calc_stream inputs : x : X diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index a9b14d2df3d17..a7bd8572a4e4a 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -1274,6 +1274,43 @@ void EmbeddingInferMeta(const MetaTensor& x, out->share_lod(x); } +void CEmbeddingInferMeta(const MetaTensor& weight, + const MetaTensor& x, + int64_t start_index, + MetaTensor* out) { + const auto& table_dims = weight.dims(); + const auto& ids_dims = x.dims(); + int ids_rank = ids_dims.size(); + + VLOG(5) << "ids rank is " << ids_rank << std::endl; + PADDLE_ENFORCE_EQ( + table_dims.size(), + 2, + phi::errors::InvalidArgument( + "ShapeError: The dimensions of the 'c_embedding' must be 2. " + "But received c_embedding's dimensions = %d, " + "c_embedding's shape = [%s].", + table_dims.size(), + table_dims)); + + auto output_dims = phi::vectorize(ids_dims); + output_dims.push_back(table_dims[1]); + out->set_dims(phi::make_ddim(output_dims)); + out->set_dtype(weight.dtype()); + out->share_lod(x); + + const auto height = table_dims[0]; + const auto width = table_dims[1]; + PADDLE_ENFORCE_EQ( + (height > 0 && width > 0 && start_index >= 0), + true, + phi::errors::InvalidArgument( + "height:%ld width:%ld start_index:%ld must not have negative values", + height, + width, + start_index)); +} + void ExpandAsInferMeta(const MetaTensor& x, const MetaTensor& y, const std::vector& target_shape, diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 9060d2abc6564..887da467e07b1 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -211,6 +211,11 @@ void EmbeddingInferMeta(const MetaTensor& x, int64_t padding_idx, MetaTensor* out); +void CEmbeddingInferMeta(const MetaTensor& weight, + const MetaTensor& x, + int64_t start_index, + MetaTensor* out); + void ExpandAsInferMeta(const MetaTensor& x, const MetaTensor& y, const std::vector& target_shape, From eddf6d05857f5b549b5ee4018af13f037dc8e94b Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 4 Sep 2023 16:19:01 +0800 Subject: [PATCH 100/194] disable strided split (#56882) * disable strided split --- paddle/phi/kernels/stride/split_kernel.cc | 4 ++-- test/legacy_test/test_stride.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/paddle/phi/kernels/stride/split_kernel.cc b/paddle/phi/kernels/stride/split_kernel.cc index b264e21c72b79..b468f77fbd841 100644 --- a/paddle/phi/kernels/stride/split_kernel.cc +++ b/paddle/phi/kernels/stride/split_kernel.cc @@ -65,7 +65,7 @@ void SplitWithNumStridedKernel(const Context& dev_ctx, } // namespace phi PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE_EXCEPT_CUSTOM( - split, STRIDED, phi::SplitStridedKernel) {} + split_strided, STRIDED, phi::SplitStridedKernel) {} PD_REGISTER_KERNEL_FOR_ALL_BACKEND_DTYPE_EXCEPT_CUSTOM( - split_with_num, STRIDED, phi::SplitWithNumStridedKernel) {} + split_with_num_strided, STRIDED, phi::SplitWithNumStridedKernel) {} diff --git a/test/legacy_test/test_stride.py b/test/legacy_test/test_stride.py index 2886e46febd6f..531575a287230 100644 --- a/test/legacy_test/test_stride.py +++ b/test/legacy_test/test_stride.py @@ -609,11 +609,11 @@ def call_stride(self): self.call_flatten() self.call_squeeze() self.call_unsqueeze() - self.call_split() - self.call_split2() - self.call_split3() - self.call_split4() - self.call_chunk() + # self.call_split() + # self.call_split2() + # self.call_split3() + # self.call_split4() + # self.call_chunk() self.call_unbind() self.call_as_strided() self.call_view() From 7fd6ffb84117477893e32a2c0f9fd392a1343b24 Mon Sep 17 00:00:00 2001 From: niuliling123 <51102941+niuliling123@users.noreply.github.com> Date: Mon, 4 Sep 2023 16:21:05 +0800 Subject: [PATCH 101/194] add num_splist to support deterministic for flash_attn_bwd and FlashAttnUnpaddedGradKernel (#56363) * add num_splist for flash_attn_bwd and FlashAttnUnpaddedGradKernel * Add assertTrue * Update submodule to a specific commit --- .../phi/kernels/gpu/flash_attn_grad_kernel.cu | 15 +- .../test_flash_attention_deterministic.py | 208 ++++++++++++++++++ third_party/flashattn | 2 +- 3 files changed, 219 insertions(+), 6 deletions(-) create mode 100644 test/legacy_test/test_flash_attention_deterministic.py diff --git a/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu b/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu index 7b76a5f458ddd..fae308008b460 100644 --- a/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/flash_attn_grad_kernel.cu @@ -28,6 +28,11 @@ PD_DECLARE_bool(cudnn_deterministic); namespace phi { +int get_num_split() { + // 0 for an internal heuristic, which is optimal + return FLAGS_cudnn_deterministic ? 1 : 0; +} + template void FlashAttnUnpaddedGradImpl(const Context& ctx, const DenseTensor& q, @@ -236,11 +241,7 @@ void FlashAttnUnpaddedGradKernel(const Context& ctx, const int64_t total_k = k.dims()[0]; const int64_t num_heads_k = k.dims()[1]; - // TODO(umiswing): add deterministic in fa2. - // int num_splits = 0; // 0 for an internal heuristic, which is optimal - // if (FLAGS_cudnn_deterministic) { - // num_splits = 1; - // } + int num_splits = get_num_split(); // TODO(umiswing): add shape check PADDLE_ENFORCE_EQ( @@ -294,6 +295,7 @@ void FlashAttnUnpaddedGradKernel(const Context& ctx, params.scale, params.causal, params.is_bf16, + num_splits, stream, params.seed, params.offset); @@ -401,6 +403,8 @@ void FlashAttnGradKernel(const Context& ctx, VLOG(10) << "FlashAttn bwd seed: " << params.seed << ", offset: " << params.offset; + int num_splits = get_num_split(); + bool succ = phi::dynload::flash_attn_bwd(dout.data(), q.data(), k.data(), @@ -426,6 +430,7 @@ void FlashAttnGradKernel(const Context& ctx, params.scale, params.causal, params.is_bf16, + num_splits, stream, params.seed, params.offset); diff --git a/test/legacy_test/test_flash_attention_deterministic.py b/test/legacy_test/test_flash_attention_deterministic.py new file mode 100644 index 0000000000000..1d6b37cbf30bc --- /dev/null +++ b/test/legacy_test/test_flash_attention_deterministic.py @@ -0,0 +1,208 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import os +import re +import unittest + +import numpy as np + +import paddle +import paddle.nn.functional as F +from paddle.device import core +from paddle.nn.functional.flash_attention import ( + flash_attention, + scaled_dot_product_attention, +) + + +def get_cuda_version(): + result = os.popen("nvcc --version").read() + regex = r'release (\S+),' + match = re.search(regex, result) + if match: + num = str(match.group(1)) + integer, decimal = num.split('.') + return int(integer) * 1000 + int(float(decimal) * 10) + else: + return -1 + + +def attention_naive(q, k, v, causal=False): + qt = paddle.transpose(q, [0, 2, 1, 3]) + kt = paddle.transpose(k, [0, 2, 1, 3]) + vt = paddle.transpose(v, [0, 2, 1, 3]) + scale = 1.0 / np.sqrt(q.shape[-1]) + s = paddle.matmul(qt, paddle.transpose(kt, [0, 1, 3, 2])) + s = paddle.scale(s, scale) + p = ( + paddle.incubate.softmax_mask_fuse_upper_triangle(s) + if causal + else F.softmax(s) + ) + o = paddle.matmul(p, vt) + return paddle.transpose(o, [0, 2, 1, 3]) + + +is_sm8x = ( + core.is_compiled_with_cuda() + and paddle.device.cuda.get_device_capability()[0] == 8 + and paddle.device.cuda.get_device_capability()[1] >= 0 +) + +is_sm90 = ( + core.is_compiled_with_cuda() + and paddle.device.cuda.get_device_capability()[0] == 9 + and paddle.device.cuda.get_device_capability()[1] == 0 +) + +is_sm_supported = is_sm8x or is_sm90 + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or get_cuda_version() < 11040 + or not is_sm_supported, + "core is not compiled with CUDA and cuda version need larger than or equal to 11.4" + "and device's compute capability must be 8.x or 90", +) +class TestFlashAttentionAPIFlag(unittest.TestCase): + def setUp(self): + self.place = paddle.CUDAPlace(0) + self.shape = (2, 128, 8, 16) + self.dtype = 'float16' + self.dropout = 0.0 + self.causal = False + self.return_softmax = False + self.use_sdp_kernel = False + self.use_sdp_api = False + + def flash_attn_compute(self, query, key, value): + # test dynamic + paddle.disable_static() + + q = paddle.to_tensor( + query, place=self.place, dtype=self.dtype, stop_gradient=False + ) + k = paddle.to_tensor( + key, place=self.place, dtype=self.dtype, stop_gradient=False + ) + v = paddle.to_tensor( + value, place=self.place, dtype=self.dtype, stop_gradient=False + ) + + q_ = paddle.to_tensor( + query, place=self.place, dtype=self.dtype, stop_gradient=False + ) + k_ = paddle.to_tensor( + key, place=self.place, dtype=self.dtype, stop_gradient=False + ) + v_ = paddle.to_tensor( + value, place=self.place, dtype=self.dtype, stop_gradient=False + ) + + if self.use_sdp_kernel: + with paddle.nn.functional.sdp_kernel( + enable_math=self.enable_math, + enable_flash=self.enable_flash, + enable_mem_efficient=self.enable_mem_efficient, + ): + if self.use_sdp_api: + out = scaled_dot_product_attention( + q, k, v, None, self.dropout, self.causal + ) + else: + out, _ = flash_attention( + q, k, v, self.dropout, self.causal, self.return_softmax + ) + + else: + out, _ = flash_attention( + q, k, v, self.dropout, self.causal, self.return_softmax + ) + out_ = attention_naive(q_, k_, v_, self.causal) + + out.backward() + out_.backward() + + self.assertEqual(q.grad.shape, q.shape) + self.assertEqual(q_.grad.shape, q.shape) + + np.testing.assert_allclose( + q.grad.numpy(), q_.grad.numpy(), rtol=5e-03, atol=1e-03 + ) + + return out, out_, q.grad.numpy(), k.grad.numpy(), v.grad.numpy() + + def test_all_flag(self): + paddle.set_flags({'FLAGS_cudnn_deterministic': 1}) + query = np.random.random(self.shape) + key = np.random.random(self.shape) + value = np.random.random(self.shape) + + out1, out1_, q_grad1, k_grad1, v_grad1 = self.flash_attn_compute( + query, key, value + ) + + np.testing.assert_allclose(out1.numpy(), out1_, rtol=5e-03, atol=1e-03) + + out2, out2_, q_grad2, k_grad2, v_grad2 = self.flash_attn_compute( + query, key, value + ) + self.assertTrue(np.equal(out1.numpy(), out2.numpy()).all()) + self.assertTrue(np.equal(q_grad1, q_grad2).all()) + self.assertTrue(np.equal(k_grad1, k_grad2).all()) + self.assertTrue(np.equal(v_grad1, v_grad2).all()) + paddle.set_flags({'FLAGS_cudnn_deterministic': 0}) + + +class TestFlashAttentionAPIFlagTest1(TestFlashAttentionAPIFlag): + def setUp(self): + self.place = paddle.CUDAPlace(0) + self.shape = (2, 128, 8, 16) + self.dtype = paddle.float16 + self.dropout = 0.0 + self.causal = False + self.return_softmax = False + self.use_sdp_kernel = False + + +class TestFlashAttentionAPIFlagTest2(TestFlashAttentionAPIFlag): + def setUp(self): + self.place = paddle.CUDAPlace(0) + self.shape = (8, 1024, 16, 256) + self.dtype = paddle.float16 + self.dropout = 0.0 + self.causal = False + self.return_softmax = False + self.use_sdp_kernel = False + + +class TestSDPAttentionAPIFlagTest(TestFlashAttentionAPIFlag): + def setUp(self): + self.place = paddle.CUDAPlace(0) + self.shape = (8, 1024, 16, 128) + self.dtype = paddle.float16 + self.dropout = 0.0 + self.causal = False + self.return_softmax = False + self.use_sdp_kernel = True + self.use_sdp_api = True + self.enable_math = True + self.enable_flash = False + self.enable_mem_efficient = False + + +if __name__ == '__main__': + unittest.main() diff --git a/third_party/flashattn b/third_party/flashattn index b5bdb79d5e1f2..e6b9d0d48c29f 160000 --- a/third_party/flashattn +++ b/third_party/flashattn @@ -1 +1 @@ -Subproject commit b5bdb79d5e1f2f88b1ef62e86899a14f82fa079a +Subproject commit e6b9d0d48c29f8205b440dede6a48ceb8394383f From 79bfb184dd6b529f2cb19867cd77628d37fad028 Mon Sep 17 00:00:00 2001 From: Yuanle Liu Date: Mon, 4 Sep 2023 16:33:38 +0800 Subject: [PATCH 102/194] multihead_matmul op support codegen and kernel remove to phi (#56846) --- .../multihead_matmul_roformer_plugin.cu | 6 +- .../tensorrt/plugin/qkv_to_context_plugin.cu | 6 +- paddle/fluid/operators/fused/CMakeLists.txt | 3 - .../operators/fused/multihead_matmul_op.cc | 116 --- .../operators/math/bert_encoder_functor.cu | 698 ---------------- .../operators/math/bert_encoder_functor.h | 29 - paddle/phi/api/yaml/fused_ops.yaml | 10 + paddle/phi/api/yaml/op_compat.yaml | 8 + paddle/phi/infermeta/multiary.cc | 33 + paddle/phi/infermeta/multiary.h | 11 + .../kernels/funcs/multihead_matmul_functor.cu | 745 ++++++++++++++++++ .../kernels/funcs/multihead_matmul_functor.h | 51 ++ .../fusion/gpu/multihead_matmul_kernel.cu} | 345 ++++---- 13 files changed, 1035 insertions(+), 1026 deletions(-) delete mode 100644 paddle/fluid/operators/fused/multihead_matmul_op.cc create mode 100644 paddle/phi/kernels/funcs/multihead_matmul_functor.cu create mode 100644 paddle/phi/kernels/funcs/multihead_matmul_functor.h rename paddle/{fluid/operators/fused/multihead_matmul_op.cu => phi/kernels/fusion/gpu/multihead_matmul_kernel.cu} (54%) diff --git a/paddle/fluid/inference/tensorrt/plugin/multihead_matmul_roformer_plugin.cu b/paddle/fluid/inference/tensorrt/plugin/multihead_matmul_roformer_plugin.cu index 539178d3fc5e8..f6b1f97e3b3a2 100644 --- a/paddle/fluid/inference/tensorrt/plugin/multihead_matmul_roformer_plugin.cu +++ b/paddle/fluid/inference/tensorrt/plugin/multihead_matmul_roformer_plugin.cu @@ -22,9 +22,9 @@ #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/inference/tensorrt/plugin/common/common.cuh" #include "paddle/fluid/inference/tensorrt/plugin/trt_plugin_utils.h" -#include "paddle/fluid/operators/math/bert_encoder_functor.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/kernels/funcs/blas/blas.h" +#include "paddle/phi/kernels/funcs/multihead_matmul_functor.h" namespace paddle { namespace inference { @@ -254,7 +254,7 @@ int MultiheadMatmulRoformerPlugin::enqueue( platform::CUDAPlace(device_id))); const phi::GPUContext &dev_ctx = *device_ctx; - operators::math::MultiHeadGPUComputeFunctor multihead_compute_func; + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; multihead_compute_func(dev_ctx, batch, seq_len, @@ -341,7 +341,7 @@ int MultiheadMatmulRoformerPlugin::enqueue( tptr, static_cast(scale_), n_q); const phi::GPUContext &dev_ctx = *device_ctx; - operators::math::MultiHeadGPUComputeFunctor multihead_compute_func; + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; multihead_compute_func(dev_ctx, batch, seq_len, diff --git a/paddle/fluid/inference/tensorrt/plugin/qkv_to_context_plugin.cu b/paddle/fluid/inference/tensorrt/plugin/qkv_to_context_plugin.cu index 2500f624967c6..298f54de48e8f 100644 --- a/paddle/fluid/inference/tensorrt/plugin/qkv_to_context_plugin.cu +++ b/paddle/fluid/inference/tensorrt/plugin/qkv_to_context_plugin.cu @@ -24,9 +24,9 @@ #include "paddle/fluid/inference/tensorrt/plugin/common/common.cuh" #include "paddle/fluid/inference/tensorrt/plugin/qkv_to_context_plugin.h" #include "paddle/fluid/inference/tensorrt/plugin/trt_plugin_utils.h" -#include "paddle/fluid/operators/math/bert_encoder_functor.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/kernels/funcs/blas/blas.h" +#include "paddle/phi/kernels/funcs/multihead_matmul_functor.h" namespace paddle { namespace inference { @@ -396,7 +396,7 @@ int QkvToContextPluginDynamic::enqueue( platform::CUDAPlace(device_id))); const phi::GPUContext &dev_ctx = *device_ctx; - operators::math::MultiHeadGPUComputeFunctor multihead_compute_func; + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; multihead_compute_func(dev_ctx, batch, seq_len, @@ -506,7 +506,7 @@ int QkvToContextPluginDynamic::enqueue( tptr, static_cast(scale_), n_q); const phi::GPUContext &dev_ctx = *device_ctx; - operators::math::MultiHeadGPUComputeFunctor multihead_compute_func; + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; multihead_compute_func(dev_ctx, batch, seq_len, diff --git a/paddle/fluid/operators/fused/CMakeLists.txt b/paddle/fluid/operators/fused/CMakeLists.txt index 9f75b311d18d2..89ea5def6fa6b 100755 --- a/paddle/fluid/operators/fused/CMakeLists.txt +++ b/paddle/fluid/operators/fused/CMakeLists.txt @@ -10,7 +10,6 @@ register_operators( fusion_transpose_flatten_concat_op fusion_conv_inception_op fused_fc_elementwise_layernorm_op - multihead_matmul_op self_dp_attention_op skip_layernorm_op yolo_box_head_op @@ -74,8 +73,6 @@ if(WITH_GPU OR WITH_ROCM) endif() # fused_fc_elementwise_layernorm_op op_library(fused_fc_elementwise_layernorm_op) - # multihead_matmul_op - op_library(multihead_matmul_op) op_library(skip_layernorm_op) op_library(yolo_box_head_op) op_library(yolo_box_post_op) diff --git a/paddle/fluid/operators/fused/multihead_matmul_op.cc b/paddle/fluid/operators/fused/multihead_matmul_op.cc deleted file mode 100644 index d263e885263a7..0000000000000 --- a/paddle/fluid/operators/fused/multihead_matmul_op.cc +++ /dev/null @@ -1,116 +0,0 @@ -/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -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. */ - -#include - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/platform/errors.h" - -namespace paddle { -namespace operators { - -class MultiHeadMatMulV2Op : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - void InferShape(framework::InferShapeContext *context) const override { - PADDLE_ENFORCE_EQ( - context->HasInput("Input"), - true, - platform::errors::InvalidArgument( - "Input(Input) of MultiHeadMatMul should not be null.")); - PADDLE_ENFORCE_EQ(context->HasInput("W"), - true, - platform::errors::InvalidArgument( - "Input(W) of MultiHeadMatMul should not be null.")); - PADDLE_ENFORCE_EQ( - context->HasInput("Bias"), - true, - platform::errors::InvalidArgument( - "Input(Bias) of MultiHeadMatMul should not be null.")); - PADDLE_ENFORCE_EQ( - context->HasOutput("Out"), - true, - platform::errors::InvalidArgument( - "Output(Out) of MultiHeadMatMul should not be null.")); - - auto dim_w = context->GetInputDim("W"); - PADDLE_ENFORCE_GT( - dim_w.size(), - 2, - platform::errors::InvalidArgument( - "Multihead input is expected at least a 3-D tensor, but " - "it's %d-D tensor now.", - dim_w.size())); - - auto dim_bias_q = context->GetInputDim("Bias"); - PADDLE_ENFORCE_GT( - dim_bias_q.size(), - 1, - platform::errors::InvalidArgument( - "Multihead input should be at least 2-D tensor, but it's " - "%d-D tensor now.", - dim_bias_q.size())); - - auto dim_input = context->GetInputDim("Input"); - context->SetOutputDim("Out", dim_input); - context->ShareLoD("Input", /*->*/ "Out"); - } -}; - -class MultiHeadMatMulV2OpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Input", "The input of MultiHeadMatMul op"); - AddInput("W", "The weight input of MultiHeadMatMul op"); - AddInput("Bias", "The bias input of MultiHeadMatMul op"); - AddInput("BiasQK", "The QK bias input of MultiHeadMatMul op") - .AsDispensable(); - AddOutput("Out", "The output of MultiHeadMatMul op"); - AddAttr("transpose_Q", - R"DOC(If true, use the transpose of `Q`. - )DOC") - .SetDefault(false); - AddAttr("transpose_K", - R"DOC(If true, use the transpose of `K`. - )DOC") - .SetDefault(true); - AddAttr("transpose_V", - R"DOC(If true, use the transpose of `V`. - )DOC") - .SetDefault(false); - AddAttr("alpha", "The scale of Out").SetDefault(1.0f); - AddAttr("head_number", "The number of heads of the matrix") - .SetDefault(1); - AddComment(R"DOC( -MultiHeadMatMul Operator. - -This op is used for optimize multi head calculation in ernie model. -Not suggest to use in other case except has same structure as ernie. - -Example of matrix multiplication with head_number of B -- X: [B, M, K], Y: [B, K, N] => Out: [B, M, N] - -)DOC"); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OP_WITHOUT_GRADIENT(multihead_matmul, - ops::MultiHeadMatMulV2Op, - ops::MultiHeadMatMulV2OpMaker); diff --git a/paddle/fluid/operators/math/bert_encoder_functor.cu b/paddle/fluid/operators/math/bert_encoder_functor.cu index c6bb45a094391..657b0b976ef62 100644 --- a/paddle/fluid/operators/math/bert_encoder_functor.cu +++ b/paddle/fluid/operators/math/bert_encoder_functor.cu @@ -255,704 +255,6 @@ template class EmbEltwiseLayerNormFunctor; template class EmbEltwiseLayerNormFunctor; #endif -template -__global__ void SoftmaxKernelWithEltadd(T *qk_buf_, - const T *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - float tmp = threadIdx.x < seq_len - ? static_cast(qk_buf_[threadIdx.x + qk_offset] + - bias_qk_[threadIdx.x + qk_offset]) - : -1e20f; - float max_val = phi::funcs::BlockReduceMax(tmp, mask); - - float qk_tmp = threadIdx.x < seq_len ? __expf(tmp - max_val) : 0.0f; - float sum_val = phi::funcs::BlockReduceSum(qk_tmp, mask); - - if (threadIdx.x < seq_len) - qk_buf_[threadIdx.x + qk_offset] = (T)(qk_tmp / sum_val); -} - -// HIP defined __HIP_NO_HALF_CONVERSIONS__ -#ifndef __HIPCC__ // @{ Half kernel: SoftmaxKernelWithEltadd -template <> -__global__ void SoftmaxKernelWithEltadd( - half *qk_buf_, - const half *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { -#if CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - float tmp = threadIdx.x < seq_len - ? static_cast(qk_buf_[threadIdx.x + qk_offset] + - bias_qk_[threadIdx.x + qk_offset]) - : -1e20f; - float max_val = phi::funcs::BlockReduceMax(tmp, mask); - - float qk_tmp = threadIdx.x < seq_len ? __expf(tmp - max_val) : 0.0f; - float sum_val = phi::funcs::BlockReduceSum(qk_tmp, mask); - - if (threadIdx.x < seq_len) - qk_buf_[threadIdx.x + qk_offset] = (half)(qk_tmp / sum_val); -#endif -} -#endif // @} End Half kernel: SoftmaxKernelWithEltadd - -template -__global__ void SoftmaxKernelWithEltadd2(T *qk_buf_, - const T *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { - int qk_offset = blockIdx.x * seq_len; - int idx = threadIdx.x; - assert(blockDim.x % WARP_SIZE == 0); - - float2 tmp = idx < seq_len - ? phi::funcs::ToFloat2(qk_buf_[idx + qk_offset] + - bias_qk_[idx + qk_offset]) - : make_float2(-1e20f, -1e20f); - float max_val = phi::funcs::BlockReduceMax(max(tmp.x, tmp.y), mask); - float2 qk_tmp = idx < seq_len ? make_float2(__expf(tmp.x - max_val), - __expf(tmp.y - max_val)) - : make_float2(0.f, 0.f); - float sum_val = - phi::funcs::BlockReduceSum(qk_tmp.x + qk_tmp.y, mask) + 1e-6f; - - if (idx < seq_len) { - qk_buf_[idx + qk_offset] = - phi::funcs::FloatsToPair(qk_tmp.x / sum_val, qk_tmp.y / sum_val); - } -} - -template <> -__global__ void SoftmaxKernelWithEltadd2( - half2 *qk_buf_, - const half2 *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { -// operator "+" of half only suppotted after cuda version 10.0 -// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake -#if defined(PADDLE_WITH_CUDA) && \ - (CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) && CUDA_VERSION >= 10000) - int qk_offset = blockIdx.x * seq_len; - int idx = threadIdx.x; - assert(blockDim.x % WARP_SIZE == 0); - - float2 tmp = idx < seq_len - ? phi::funcs::ToFloat2(qk_buf_[idx + qk_offset] + - bias_qk_[idx + qk_offset]) - : make_float2(-1e20f, -1e20f); - float max_val = phi::funcs::BlockReduceMax(max(tmp.x, tmp.y), mask); - float2 qk_tmp = idx < seq_len ? make_float2(__expf(tmp.x - max_val), - __expf(tmp.y - max_val)) - : make_float2(0.f, 0.f); - float sum_val = - phi::funcs::BlockReduceSum(qk_tmp.x + qk_tmp.y, mask) + 1e-6f; - - if (idx < seq_len) { - qk_buf_[idx + qk_offset] = - phi::funcs::FloatsToPair(qk_tmp.x / sum_val, qk_tmp.y / sum_val); - } -#endif -} - -template -__global__ void SoftmaxKernelWithEltaddForLarge( - T *qk_buf, - const T *bias_qk, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - T stride_max = -1e20f; - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - stride_max = qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset] > - stride_max - ? qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset] - : stride_max; - } - T max_val = phi::funcs::BlockReduceMax(stride_max, mask); - - T stride_sum = 0.f; - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - stride_sum += __expf(qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset] - max_val); - } - T sum_val = phi::funcs::BlockReduceSum(stride_sum, mask); - - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - qk_buf[threadIdx.x + i + qk_offset] = - (T)(__expf(qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset] - max_val) / - sum_val); - } -} - -// HIP defined __HIP_NO_HALF_CONVERSIONS__ -#ifndef __HIPCC__ // @{ Half kernel: SoftmaxKernelWithEltadd -template <> -__global__ void SoftmaxKernelWithEltaddForLarge( - half *qk_buf, - const half *bias_qk, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { -#if CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - float stride_max = -1e20f; - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float tmp = static_cast(qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset]); - stride_max = tmp > stride_max ? tmp : stride_max; - } - float max_val = phi::funcs::BlockReduceMax(stride_max, mask); - - float stride_sum = 0.f; - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float tmp = static_cast(qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset]); - stride_sum += __expf(tmp - max_val); - } - float sum_val = phi::funcs::BlockReduceSum(stride_sum, mask); - - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float tmp = - __expf(static_cast(qk_buf[threadIdx.x + i + qk_offset] + - bias_qk[threadIdx.x + i + qk_offset]) - - max_val); - qk_buf[threadIdx.x + i + qk_offset] = (half)(tmp / sum_val); - } -#endif -} -#endif // @} End Half kernel: SoftmaxKernelWithEltadd - -template -__global__ void SoftmaxKernelWithEltaddForLarge2( - T *qk_buf_, - const T *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - float2 stride_max = make_float2(-1e20f, -1e20f); - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - stride_max.x = max(stride_max.x, cur.x); - stride_max.y = max(stride_max.y, cur.y); - } - float max_val = - phi::funcs::BlockReduceMax(max(stride_max.x, stride_max.y), mask); - - float2 stride_sum = make_float2(0.f, 0.f); - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - stride_sum.x += __expf(cur.x - max_val); - stride_sum.y += __expf(cur.y - max_val); - } - - float sum_val = - phi::funcs::BlockReduceSum(stride_sum.x + stride_sum.y, mask) + - 1e-6f; - - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - qk_buf_[threadIdx.x + i + qk_offset] = phi::funcs::FloatsToPair( - __expf(cur.x - max_val) / sum_val, __expf(cur.y - max_val) / sum_val); - } -} - -template <> -__global__ void SoftmaxKernelWithEltaddForLarge2( - half2 *qk_buf_, - const half2 *bias_qk_, - const int batch_size, - const int head_num, - const int seq_len, - const phi::funcs::warp_mask_t mask) { -// operator "+" of half only suppotted after cuda version 10.0 -// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake -#if defined(PADDLE_WITH_CUDA) && \ - (CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) && CUDA_VERSION >= 10000) - - int qk_offset = blockIdx.x * seq_len; - assert(blockDim.x % WARP_SIZE == 0); - - float2 stride_max = make_float2(-1e20f, -1e20f); - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = - phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - stride_max.x = max(stride_max.x, cur.x); - stride_max.y = max(stride_max.y, cur.y); - } - float max_val = - phi::funcs::BlockReduceMax(max(stride_max.x, stride_max.y), mask); - - float2 stride_sum = make_float2(0.f, 0.f); - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = - phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - stride_sum.x += __expf(cur.x - max_val); - stride_sum.y += __expf(cur.y - max_val); - } - - float sum_val = - phi::funcs::BlockReduceSum(stride_sum.x + stride_sum.y, mask) + - 1e-6f; - - for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { - float2 cur = - phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + - bias_qk_[threadIdx.x + i + qk_offset]); - qk_buf_[threadIdx.x + i + qk_offset] = phi::funcs::FloatsToPair( - __expf(cur.x - max_val) / sum_val, __expf(cur.y - max_val) / sum_val); - } -#endif -} - -template -inline __device__ T ldg(const T *val) { - return __ldg(val); -} - -template -inline __device__ T hexp2(T a) { - return h2exp(a); -} - -template -inline __device__ T_OUT type2type2(T_IN a); - -template <> -inline __device__ half2 type2type2(half a) { - return __half2half2(a); -} - -template -inline __device__ T float2type2(float a); - -template <> -inline __device__ half2 float2type2(float a) { - return __float2half2_rn(a); -} - -template -inline __device__ T hmul2(T a, T b) { - return __hmul2(a, b); -} - -template -inline __device__ T hsub2(T a, T b) { - return __hsub2(a, b); -} - -template -inline __device__ T hadd2(T a, T b) { - return __hadd2(a, b); -} - -template -__global__ void softmax_kernel_with_mask(T *qk_buf_, - const T *attr_mask, - const int batch_size, - const int head_num, - const int seq_len) { - using T2 = half2; - T2 *qk_buf_half2 = reinterpret_cast(qk_buf_); - const T2 *attr_mask_half2 = (const T2 *)attr_mask; - - for (int seq_id = blockIdx.x; seq_id < seq_len; seq_id += gridDim.x * NUM) { - T2 data[NUM][ITEMS_PER_THREAD]; - - int qk_offset[NUM]; - - __shared__ float s_sum[NUM], s_max[NUM]; - float local_max[NUM]; -#pragma unroll - for (int j = 0; j < NUM; j++) { - local_max[j] = -1e20f; - } - - for (int i = 0; - blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; - i++) { - int mask_offset[NUM]; -#pragma unroll - for (int j = 0; j < NUM; j++) { - qk_offset[j] = ((blockIdx.y * head_num + blockIdx.z) * seq_len + - seq_id + j * gridDim.x) * - (seq_len / 2) + - blockDim.x * i + threadIdx.x; - mask_offset[j] = - (blockIdx.y * seq_len + seq_id + j * gridDim.x) * (seq_len / 2) + - blockDim.x * i + threadIdx.x; - } - - T2 mask_val[NUM]; -#pragma unroll - for (int j = 0; j < NUM; j++) { - mask_val[j] = ldg(&attr_mask_half2[mask_offset[j]]); - } - - T2 qk[NUM]; -#pragma unroll - for (int j = 0; j < NUM; j++) { - qk[j] = qk_buf_half2[qk_offset[j]]; - } - -#pragma unroll - for (int j = 0; j < NUM; j++) { - mask_val[j] = hmul2(hsub2(float2type2(1.0f), mask_val[j]), - float2type2(-10000.0f)); - } - -#pragma unroll - for (int j = 0; j < NUM; j++) { - data[j][i] = hadd2(qk[j], mask_val[j]); - local_max[j] = fmax(local_max[j], - fmax(static_cast(data[j][i].x), - static_cast(data[j][i].y))); - } - } - - if (blockDim.x <= WARP_SIZE) { - phi::funcs::WarpReduceMaxV2(local_max); - } else { - phi::funcs::BlockReduceMaxV2(local_max); - } - - if (threadIdx.x == 0) { -#pragma unroll - for (int j = 0; j < NUM; j++) { - s_max[j] = local_max[j]; - } - } - __syncthreads(); - - float local_sum[NUM]; -#pragma unroll - for (int j = 0; j < NUM; j++) { - local_sum[j] = {0.f}; - } - - for (int i = 0; - blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; - i++) { -#pragma unroll - for (int j = 0; j < NUM; j++) { - data[j][i] = - hexp2(hsub2(data[j][i], float2type2(s_max[j]))); - } - -#pragma unroll - for (int j = 0; j < NUM; j++) { - local_sum[j] += static_cast(data[j][i].x + data[j][i].y); - } - } - - if (blockDim.x <= WARP_SIZE) { - phi::funcs::WarpReduceSumV2(local_sum); - } else { - phi::funcs::BlockReduceSumV2(local_sum); - } - - if (threadIdx.x == 0) { -#pragma unroll - for (int j = 0; j < NUM; j++) { - s_sum[j] = __fdividef(1.0f, local_sum[j] + 1e-6f); - } - } - __syncthreads(); - - for (int i = 0; - blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; - i++) { -#pragma unroll - for (int j = 0; j < NUM; j++) { - qk_offset[j] = ((blockIdx.y * head_num + blockIdx.z) * seq_len + - seq_id + j * gridDim.x) * - (seq_len / 2) + - blockDim.x * i + threadIdx.x; - } - -#pragma unroll - for (int j = 0; j < NUM; j++) { - qk_buf_half2[qk_offset[j]] = - hmul2(data[j][i], float2type2(s_sum[j])); - } - } - } -} - -#define SOFTMAX_KERNEL_WITH_MASK(REPEAT_THREAD) \ - do { \ - block.x /= REPEAT_THREAD; \ - grid.x /= 4; \ - constexpr int NUM = 4; \ - softmax_kernel_with_mask \ - <<>>(reinterpret_cast(qk_buf_), \ - (const half *)bias_qk, \ - batch_size, \ - head_num, \ - seq_len); \ - } while (0) - -template -inline void MatMulWithHeadQK(const phi::GPUContext &context, - int head_num, - int seq_len, - int size_per_head, - int batch_size, - bool q_trans, - bool k_trans, - T *q_buf_, - T *k_buf_, - T *qk_buf_, - const T *bias_qk, - bool bias_is_mask, - T alpha, - T beta) { - CBLAS_TRANSPOSE transA = !q_trans ? CblasNoTrans : CblasTrans; - CBLAS_TRANSPOSE transB = !k_trans ? CblasNoTrans : CblasTrans; - - typedef typename CUDATypeTraits::TYPE run_type; - auto blas = phi::funcs::GetBlas(context); - auto stream = context.stream(); - - blas.BatchedGEMM(transA, - transB, - seq_len, - seq_len, - size_per_head, - static_cast(alpha), - reinterpret_cast(q_buf_), - reinterpret_cast(k_buf_), - static_cast(beta), - reinterpret_cast(qk_buf_), - batch_size * head_num, - seq_len * size_per_head, - seq_len * size_per_head); - - if (seq_len <= 1024) { - int grid = batch_size * head_num * seq_len; - int block = seq_len; - - // Align block to 32, also limit seq_len to max block size. - if (seq_len % 2 == 0) { - block = - (seq_len <= (2 * WARP_SIZE)) - ? WARP_SIZE - : ((seq_len + (2 * WARP_SIZE - 1)) / (2 * WARP_SIZE)) * WARP_SIZE; - if (std::is_same::value) { - SoftmaxKernelWithEltadd2<<>>( - reinterpret_cast(qk_buf_), - reinterpret_cast(bias_qk), - batch_size, - head_num, - seq_len / 2, - FINAL_MASK); - } else { - if (bias_is_mask) { -#if defined(__HIPCC__) || (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700) - PADDLE_ENFORCE_EQ(bias_is_mask, - false, - platform::errors::InvalidArgument( - "QK_bias is mask can't be supported on rocm or " - "cuda_arch<700")); -#else - dim3 grid(seq_len, batch_size, head_num); - dim3 block((seq_len / 2 + WARP_SIZE - 1) / WARP_SIZE * WARP_SIZE); - SOFTMAX_KERNEL_WITH_MASK(1); -#endif - } else { - SoftmaxKernelWithEltadd2<__half2><<>>( - reinterpret_cast<__half2 *>(qk_buf_), - reinterpret_cast(bias_qk), - batch_size, - head_num, - seq_len / 2, - FINAL_MASK); - } - } - } else { - block = (seq_len <= WARP_SIZE) - ? WARP_SIZE - : ((seq_len + WARP_SIZE - 1) / WARP_SIZE) * WARP_SIZE; - SoftmaxKernelWithEltadd<<>>( - qk_buf_, bias_qk, batch_size, head_num, seq_len, FINAL_MASK); - } - } else { - int grid = batch_size * head_num * seq_len; - int block = 512; - if (seq_len % 2 == 0) { - if (std::is_same::value) { - SoftmaxKernelWithEltaddForLarge2<<>>( - reinterpret_cast(qk_buf_), - reinterpret_cast(bias_qk), - batch_size, - head_num, - seq_len / 2, - FINAL_MASK); - } else { - if (bias_is_mask) { -#if defined(__HIPCC__) || (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700) - PADDLE_ENFORCE_EQ(bias_is_mask, - false, - platform::errors::InvalidArgument( - "QK_bias is mask can't be supported on rocm or " - "cuda_arch<700")); -#else - dim3 grid(seq_len, batch_size, head_num); - dim3 block((seq_len / 2 + WARP_SIZE - 1) / WARP_SIZE * WARP_SIZE); - if (block.x > 0 && block.x <= 1024) { - SOFTMAX_KERNEL_WITH_MASK(1); - } else if (block.x <= 2048) { - SOFTMAX_KERNEL_WITH_MASK(2); - } else if (block.x <= 4096) { - SOFTMAX_KERNEL_WITH_MASK(4); - } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "Cannot support the length of attention > 8192.")); - } -#endif - } else { - SoftmaxKernelWithEltaddForLarge2<__half2><<>>( - reinterpret_cast<__half2 *>(qk_buf_), - reinterpret_cast(bias_qk), - batch_size, - head_num, - seq_len / 2, - FINAL_MASK); - } - } - } else { - SoftmaxKernelWithEltaddForLarge<<>>( - qk_buf_, bias_qk, batch_size, head_num, seq_len, FINAL_MASK); - } - } -} - -template -inline void MatMulWithHeadQKV(const phi::GPUContext &context, - int head_num, - int seq_len, - int size_per_head, - int batch_size, - bool qk_trans, - bool v_trans, - T *v_buf_, - const T *qk_buf_, - T *dst, - T alpha, - T beta) { - int m = batch_size * seq_len; - int k = head_num * size_per_head; - - typedef typename CUDATypeTraits::TYPE run_type; - auto blas = phi::funcs::GetBlas(context); - auto stream = context.stream(); - CBLAS_TRANSPOSE transA = !qk_trans ? CblasNoTrans : CblasTrans; - CBLAS_TRANSPOSE transB = !v_trans ? CblasNoTrans : CblasTrans; - - blas.BatchedGEMM(transA, - transB, - seq_len, - size_per_head, - seq_len, - static_cast(alpha), - reinterpret_cast(qk_buf_), - reinterpret_cast(v_buf_), - static_cast(beta), - reinterpret_cast(dst), - batch_size * head_num, - seq_len * seq_len, - seq_len * size_per_head); -} - -template -void MultiHeadGPUComputeFunctor::operator()(const phi::GPUContext &dev_ctx, - int batch, - int seq_len, - int head_num, - int head_size, - T *qkptr, - const T *bias_qk_ptr, - bool bias_is_mask, - T *tptr, - T alpha, - T beta) { - auto stream = dev_ctx.stream(); - const int tsize = batch * head_num * seq_len * head_size; - - T *qptr = tptr; - T *kptr = qptr + tsize; - T *vptr = kptr + tsize; - // batch gemm stride, softmaxwithscale. - MatMulWithHeadQK(dev_ctx, - head_num, - seq_len, - head_size, - batch, - false, - true, - qptr, - kptr, - qkptr, - bias_qk_ptr, - bias_is_mask, - alpha, - beta); - // batch gemm stride, transpose. - MatMulWithHeadQKV(dev_ctx, - head_num, - seq_len, - head_size, - batch, - false, - false, - vptr, - qkptr, - tptr, - T(1.0), - beta); -} - -template class MultiHeadGPUComputeFunctor; - -// device function 'operator()' is not supportted until cuda 10.0 -// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake -#if defined(PADDLE_WITH_CUDA) && CUDA_VERSION >= 10000 -template class MultiHeadGPUComputeFunctor; -#endif - template __global__ void SkipLayerNormSmallKernel(int num, int hidden, diff --git a/paddle/fluid/operators/math/bert_encoder_functor.h b/paddle/fluid/operators/math/bert_encoder_functor.h index 9a0b5a1ae3ab7..6d31098686608 100644 --- a/paddle/fluid/operators/math/bert_encoder_functor.h +++ b/paddle/fluid/operators/math/bert_encoder_functor.h @@ -77,35 +77,6 @@ class EmbEltwiseLayerNormFunctor { gpuStream_t stream); }; -// This functor involves a fusion calculation in Ernie or Bert. -// The fusion mode is as follows: -// -// | | -// matmul -// | -// eltwise_add -// | -// softmax / -// \ / -// matmul -// | - -template -class MultiHeadGPUComputeFunctor { - public: - void operator()(const phi::GPUContext &dev_ctx, - int batch, - int seq_len, - int head_num, - int head_size, - T *qkptr, - const T *bias_qk_ptr, - bool bias_is_mask, - T *tptr, - T alpha, - T beta); -}; - // This functor involves a fusion calculation in Ernie or Bert. // The fusion mode is as follows: // diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index 09ccd2fe7d87d..90593ed4d43ea 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -189,6 +189,16 @@ data_type : x optional : mask, seq_lod, max_seq_len, x_fp16, out_fp16 +- op : multihead_matmul + args : (Tensor input, Tensor w, Tensor bias, Tensor bias_qk, bool transpose_q = false, bool transpose_k = true, bool transpose_v = false, float alpha = 1.0f, int head_number = 1) + output : Tensor(out) + infer_meta : + func : MultiheadMatmulInferMeta + kernel : + func : multihead_matmul + data_type : input + optional : bias_qk + - op : yolo_box_xpu args : (Tensor x, Tensor x_max, Tensor grid, Tensor stride, Tensor anchor_grid, float offset) output : Tensor(out), Tensor(out_max) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 77b023e84c68c..495ba53cd7613 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1935,6 +1935,14 @@ outputs : {out : Out, index : Index, nms_rois_num : NmsRoisNum} +- op : multihead_matmul + inputs : + {input : Input, w : W, bias : Bias, bias_qk : BiasQK} + outputs : + out : Out + attrs : + {transpose_q : transpose_Q, transpose_k : transpose_K, transpose_v : transpose_V} + - op : multinomial inputs : {x : X} diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 7c2e941bac2e0..e54c6ab3ba7dc 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -4126,6 +4126,39 @@ void WeightedSampleNeighborsInferMeta(const MetaTensor& row, out_count->set_dtype(DataType::INT32); } +void MultiheadMatmulInferMeta(const MetaTensor& input, + const MetaTensor& w, + const MetaTensor& bias, + const MetaTensor& bias_qk, + const bool transpose_q, + const bool transpose_k, + const bool transpose_v, + const float alpha, + const int head_number, + MetaTensor* out) { + auto w_dims = w.dims(); + PADDLE_ENFORCE_GT( + w_dims.size(), + 2, + errors::InvalidArgument( + "MultiheadMatmul's w is expected at least a 3-D tensor, but " + "it's %d-D tensor now.", + w_dims.size())); + + auto bias_dims = bias.dims(); + PADDLE_ENFORCE_GT( + bias_dims.size(), + 1, + errors::InvalidArgument( + "MultiheadMatmul's bias should be at least 2-D tensor, but it's " + "%d-D tensor now.", + bias_dims.size())); + + out->set_dims(input.dims()); + out->set_dtype(input.dtype()); + out->share_lod(input); +} + void MaskedMultiheadAttentionInferMeta(const MetaTensor& x, const MetaTensor& cache_kv, const MetaTensor& bias, diff --git a/paddle/phi/infermeta/multiary.h b/paddle/phi/infermeta/multiary.h index 24cc6f64022c4..8a782676fe917 100644 --- a/paddle/phi/infermeta/multiary.h +++ b/paddle/phi/infermeta/multiary.h @@ -811,6 +811,17 @@ void FusedRopeInferMeta(const MetaTensor& q, MetaTensor* out_k, MetaTensor* out_v); +void MultiheadMatmulInferMeta(const MetaTensor& input, + const MetaTensor& w, + const MetaTensor& bias, + const MetaTensor& bias_qk, + const bool transpose_q, + const bool transpose_k, + const bool transpose_v, + const float alpha, + const int head_number, + MetaTensor* out); + void MaskedMultiheadAttentionInferMeta(const MetaTensor& x, const MetaTensor& cache_kv, const MetaTensor& bias, diff --git a/paddle/phi/kernels/funcs/multihead_matmul_functor.cu b/paddle/phi/kernels/funcs/multihead_matmul_functor.cu new file mode 100644 index 0000000000000..50d7c39b198a5 --- /dev/null +++ b/paddle/phi/kernels/funcs/multihead_matmul_functor.cu @@ -0,0 +1,745 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#ifdef PADDLE_WITH_CUDA +#include +#include + +#include // NOLINT +#endif +#ifdef PADDLE_WITH_HIP +#include + +#include +namespace cub = hipcub; +#endif + +#include "paddle/phi/kernels/funcs/multihead_matmul_functor.h" + +#include "paddle/phi/common/float16.h" +#include "paddle/phi/kernels/funcs/blas/blas.h" +#include "paddle/phi/kernels/funcs/math_cuda_utils.h" + +namespace phi { +namespace funcs { + +template +struct CUDATypeTraits; + +template <> +struct CUDATypeTraits { + typedef phi::dtype::float16 TYPE; +}; + +template <> +struct CUDATypeTraits { + typedef float TYPE; +}; + +using phi::funcs::operator+; + +template +__global__ void SoftmaxKernelWithEltadd(T *qk_buf_, + const T *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + float tmp = threadIdx.x < seq_len + ? static_cast(qk_buf_[threadIdx.x + qk_offset] + + bias_qk_[threadIdx.x + qk_offset]) + : -1e20f; + float max_val = phi::funcs::BlockReduceMax(tmp, mask); + + float qk_tmp = threadIdx.x < seq_len ? __expf(tmp - max_val) : 0.0f; + float sum_val = phi::funcs::BlockReduceSum(qk_tmp, mask); + + if (threadIdx.x < seq_len) + qk_buf_[threadIdx.x + qk_offset] = (T)(qk_tmp / sum_val); +} + +template <> +__global__ void SoftmaxKernelWithEltadd( + half *qk_buf_, + const half *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { +#if defined(PADDLE_WITH_CUDA) && CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + float tmp = threadIdx.x < seq_len + ? static_cast(qk_buf_[threadIdx.x + qk_offset] + + bias_qk_[threadIdx.x + qk_offset]) + : -1e20f; + float max_val = phi::funcs::BlockReduceMax(tmp, mask); + + float qk_tmp = threadIdx.x < seq_len ? __expf(tmp - max_val) : 0.0f; + float sum_val = phi::funcs::BlockReduceSum(qk_tmp, mask); + + if (threadIdx.x < seq_len) + qk_buf_[threadIdx.x + qk_offset] = (half)(qk_tmp / sum_val); +#endif +} + +template +__global__ void SoftmaxKernelWithEltadd2(T *qk_buf_, + const T *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { + int qk_offset = blockIdx.x * seq_len; + int idx = threadIdx.x; + assert(blockDim.x % WARP_SIZE == 0); + + float2 tmp = idx < seq_len + ? phi::funcs::ToFloat2(qk_buf_[idx + qk_offset] + + bias_qk_[idx + qk_offset]) + : make_float2(-1e20f, -1e20f); + float max_val = phi::funcs::BlockReduceMax(max(tmp.x, tmp.y), mask); + float2 qk_tmp = idx < seq_len ? make_float2(__expf(tmp.x - max_val), + __expf(tmp.y - max_val)) + : make_float2(0.f, 0.f); + float sum_val = + phi::funcs::BlockReduceSum(qk_tmp.x + qk_tmp.y, mask) + 1e-6f; + + if (idx < seq_len) { + qk_buf_[idx + qk_offset] = + phi::funcs::FloatsToPair(qk_tmp.x / sum_val, qk_tmp.y / sum_val); + } +} + +template <> +__global__ void SoftmaxKernelWithEltadd2( + half2 *qk_buf_, + const half2 *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { +// operator "+" of half only suppotted after cuda version 10.0 +// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake +#if defined(PADDLE_WITH_CUDA) && CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) + int qk_offset = blockIdx.x * seq_len; + int idx = threadIdx.x; + assert(blockDim.x % WARP_SIZE == 0); + + float2 tmp = idx < seq_len + ? phi::funcs::ToFloat2(qk_buf_[idx + qk_offset] + + bias_qk_[idx + qk_offset]) + : make_float2(-1e20f, -1e20f); + float max_val = phi::funcs::BlockReduceMax(max(tmp.x, tmp.y), mask); + float2 qk_tmp = idx < seq_len ? make_float2(__expf(tmp.x - max_val), + __expf(tmp.y - max_val)) + : make_float2(0.f, 0.f); + float sum_val = + phi::funcs::BlockReduceSum(qk_tmp.x + qk_tmp.y, mask) + 1e-6f; + + if (idx < seq_len) { + qk_buf_[idx + qk_offset] = + phi::funcs::FloatsToPair(qk_tmp.x / sum_val, qk_tmp.y / sum_val); + } +#endif +} + +template +__global__ void SoftmaxKernelWithEltaddForLarge( + T *qk_buf, + const T *bias_qk, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + T stride_max = -1e20f; + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + stride_max = qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset] > + stride_max + ? qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset] + : stride_max; + } + T max_val = phi::funcs::BlockReduceMax(stride_max, mask); + + T stride_sum = 0.f; + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + stride_sum += __expf(qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset] - max_val); + } + T sum_val = phi::funcs::BlockReduceSum(stride_sum, mask); + + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + qk_buf[threadIdx.x + i + qk_offset] = + (T)(__expf(qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset] - max_val) / + sum_val); + } +} + +template <> +__global__ void SoftmaxKernelWithEltaddForLarge( + half *qk_buf, + const half *bias_qk, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { +#if defined(PADDLE_WITH_CUDA) && \ + (CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) && CUDA_VERSION >= 10000) + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + float stride_max = -1e20f; + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float tmp = static_cast(qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset]); + stride_max = tmp > stride_max ? tmp : stride_max; + } + float max_val = phi::funcs::BlockReduceMax(stride_max, mask); + + float stride_sum = 0.f; + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float tmp = static_cast(qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset]); + stride_sum += __expf(tmp - max_val); + } + float sum_val = phi::funcs::BlockReduceSum(stride_sum, mask); + + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float tmp = + __expf(static_cast(qk_buf[threadIdx.x + i + qk_offset] + + bias_qk[threadIdx.x + i + qk_offset]) - + max_val); + qk_buf[threadIdx.x + i + qk_offset] = (half)(tmp / sum_val); + } +#endif +} + +template +__global__ void SoftmaxKernelWithEltaddForLarge2( + T *qk_buf_, + const T *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + float2 stride_max = make_float2(-1e20f, -1e20f); + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + stride_max.x = max(stride_max.x, cur.x); + stride_max.y = max(stride_max.y, cur.y); + } + float max_val = + phi::funcs::BlockReduceMax(max(stride_max.x, stride_max.y), mask); + + float2 stride_sum = make_float2(0.f, 0.f); + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + stride_sum.x += __expf(cur.x - max_val); + stride_sum.y += __expf(cur.y - max_val); + } + + float sum_val = + phi::funcs::BlockReduceSum(stride_sum.x + stride_sum.y, mask) + + 1e-6f; + + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + qk_buf_[threadIdx.x + i + qk_offset] = phi::funcs::FloatsToPair( + __expf(cur.x - max_val) / sum_val, __expf(cur.y - max_val) / sum_val); + } +} + +template <> +__global__ void SoftmaxKernelWithEltaddForLarge2( + half2 *qk_buf_, + const half2 *bias_qk_, + const int batch_size, + const int head_num, + const int seq_len, + const phi::funcs::warp_mask_t mask) { +// operator "+" of half only suppotted after cuda version 10.0 +// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake +#if defined(PADDLE_WITH_CUDA) && \ + (CUDA_ARCH_FP16_SUPPORTED(__CUDA_ARCH__) && CUDA_VERSION >= 10000) + + int qk_offset = blockIdx.x * seq_len; + assert(blockDim.x % WARP_SIZE == 0); + + float2 stride_max = make_float2(-1e20f, -1e20f); + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = + phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + stride_max.x = max(stride_max.x, cur.x); + stride_max.y = max(stride_max.y, cur.y); + } + float max_val = + phi::funcs::BlockReduceMax(max(stride_max.x, stride_max.y), mask); + + float2 stride_sum = make_float2(0.f, 0.f); + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = + phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + stride_sum.x += __expf(cur.x - max_val); + stride_sum.y += __expf(cur.y - max_val); + } + + float sum_val = + phi::funcs::BlockReduceSum(stride_sum.x + stride_sum.y, mask) + + 1e-6f; + + for (int i = 0; threadIdx.x + i < seq_len; i += blockDim.x) { + float2 cur = + phi::funcs::ToFloat2(qk_buf_[threadIdx.x + i + qk_offset] + + bias_qk_[threadIdx.x + i + qk_offset]); + qk_buf_[threadIdx.x + i + qk_offset] = phi::funcs::FloatsToPair( + __expf(cur.x - max_val) / sum_val, __expf(cur.y - max_val) / sum_val); + } +#endif +} + +template +inline __device__ T ldg(const T *val) { + return __ldg(val); +} + +template +inline __device__ T hexp2(T a) { + return h2exp(a); +} + +template +inline __device__ T_OUT type2type2(T_IN a); + +template <> +inline __device__ half2 type2type2(half a) { + return __half2half2(a); +} + +template +inline __device__ T float2type2(float a); + +template <> +inline __device__ half2 float2type2(float a) { + return __float2half2_rn(a); +} + +template +inline __device__ T hmul2(T a, T b) { + return __hmul2(a, b); +} + +template +inline __device__ T hsub2(T a, T b) { + return __hsub2(a, b); +} + +template +inline __device__ T hadd2(T a, T b) { + return __hadd2(a, b); +} + +template +__global__ void softmax_kernel_with_mask(T *qk_buf_, + const T *attr_mask, + const int batch_size, + const int head_num, + const int seq_len) { + using T2 = half2; + T2 *qk_buf_half2 = reinterpret_cast(qk_buf_); + const T2 *attr_mask_half2 = (const T2 *)attr_mask; + + for (int seq_id = blockIdx.x; seq_id < seq_len; seq_id += gridDim.x * NUM) { + T2 data[NUM][ITEMS_PER_THREAD]; + + int qk_offset[NUM]; + + __shared__ float s_sum[NUM], s_max[NUM]; + float local_max[NUM]; +#pragma unroll + for (int j = 0; j < NUM; j++) { + local_max[j] = -1e20f; + } + + for (int i = 0; + blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; + i++) { + int mask_offset[NUM]; +#pragma unroll + for (int j = 0; j < NUM; j++) { + qk_offset[j] = ((blockIdx.y * head_num + blockIdx.z) * seq_len + + seq_id + j * gridDim.x) * + (seq_len / 2) + + blockDim.x * i + threadIdx.x; + mask_offset[j] = + (blockIdx.y * seq_len + seq_id + j * gridDim.x) * (seq_len / 2) + + blockDim.x * i + threadIdx.x; + } + + T2 mask_val[NUM]; +#pragma unroll + for (int j = 0; j < NUM; j++) { + mask_val[j] = ldg(&attr_mask_half2[mask_offset[j]]); + } + + T2 qk[NUM]; +#pragma unroll + for (int j = 0; j < NUM; j++) { + qk[j] = qk_buf_half2[qk_offset[j]]; + } + +#pragma unroll + for (int j = 0; j < NUM; j++) { + mask_val[j] = hmul2(hsub2(float2type2(1.0f), mask_val[j]), + float2type2(-10000.0f)); + } + +#pragma unroll + for (int j = 0; j < NUM; j++) { + data[j][i] = hadd2(qk[j], mask_val[j]); + local_max[j] = fmax(local_max[j], + fmax(static_cast(data[j][i].x), + static_cast(data[j][i].y))); + } + } + + if (blockDim.x <= WARP_SIZE) { + phi::funcs::WarpReduceMaxV2(local_max); + } else { + phi::funcs::BlockReduceMaxV2(local_max); + } + + if (threadIdx.x == 0) { +#pragma unroll + for (int j = 0; j < NUM; j++) { + s_max[j] = local_max[j]; + } + } + __syncthreads(); + + float local_sum[NUM]; +#pragma unroll + for (int j = 0; j < NUM; j++) { + local_sum[j] = {0.f}; + } + + for (int i = 0; + blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; + i++) { +#pragma unroll + for (int j = 0; j < NUM; j++) { + data[j][i] = + hexp2(hsub2(data[j][i], float2type2(s_max[j]))); + } + +#pragma unroll + for (int j = 0; j < NUM; j++) { + local_sum[j] += static_cast(data[j][i].x + data[j][i].y); + } + } + + if (blockDim.x <= WARP_SIZE) { + phi::funcs::WarpReduceSumV2(local_sum); + } else { + phi::funcs::BlockReduceSumV2(local_sum); + } + + if (threadIdx.x == 0) { +#pragma unroll + for (int j = 0; j < NUM; j++) { + s_sum[j] = __fdividef(1.0f, local_sum[j] + 1e-6f); + } + } + __syncthreads(); + + for (int i = 0; + blockDim.x * i + threadIdx.x < (seq_len / 2) && i < ITEMS_PER_THREAD; + i++) { +#pragma unroll + for (int j = 0; j < NUM; j++) { + qk_offset[j] = ((blockIdx.y * head_num + blockIdx.z) * seq_len + + seq_id + j * gridDim.x) * + (seq_len / 2) + + blockDim.x * i + threadIdx.x; + } + +#pragma unroll + for (int j = 0; j < NUM; j++) { + qk_buf_half2[qk_offset[j]] = + hmul2(data[j][i], float2type2(s_sum[j])); + } + } + } +} + +#define SOFTMAX_KERNEL_WITH_MASK(REPEAT_THREAD) \ + do { \ + block.x /= REPEAT_THREAD; \ + grid.x /= 4; \ + constexpr int NUM = 4; \ + softmax_kernel_with_mask \ + <<>>(reinterpret_cast(qk_buf_), \ + (const half *)bias_qk, \ + batch_size, \ + head_num, \ + seq_len); \ + } while (0) + +template +inline void MatmulWithHeadQK(const phi::GPUContext &context, + int head_num, + int seq_len, + int size_per_head, + int batch_size, + bool q_trans, + bool k_trans, + T *q_buf_, + T *k_buf_, + T *qk_buf_, + const T *bias_qk, + bool bias_is_mask, + T alpha, + T beta) { + CBLAS_TRANSPOSE transA = !q_trans ? CblasNoTrans : CblasTrans; + CBLAS_TRANSPOSE transB = !k_trans ? CblasNoTrans : CblasTrans; + + typedef typename CUDATypeTraits::TYPE run_type; + auto blas = phi::funcs::GetBlas(context); + auto stream = context.stream(); + + blas.BatchedGEMM(transA, + transB, + seq_len, + seq_len, + size_per_head, + static_cast(alpha), + reinterpret_cast(q_buf_), + reinterpret_cast(k_buf_), + static_cast(beta), + reinterpret_cast(qk_buf_), + batch_size * head_num, + seq_len * size_per_head, + seq_len * size_per_head); + + if (seq_len <= 1024) { + int grid = batch_size * head_num * seq_len; + int block = seq_len; + + // Align block to 32, also limit seq_len to max block size. + if (seq_len % 2 == 0) { + block = + (seq_len <= (2 * WARP_SIZE)) + ? WARP_SIZE + : ((seq_len + (2 * WARP_SIZE - 1)) / (2 * WARP_SIZE)) * WARP_SIZE; + if (std::is_same::value) { + SoftmaxKernelWithEltadd2<<>>( + reinterpret_cast(qk_buf_), + reinterpret_cast(bias_qk), + batch_size, + head_num, + seq_len / 2, + FINAL_MASK); + } else { + if (bias_is_mask) { +#if defined(__HIPCC__) || (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700) + PADDLE_ENFORCE_EQ(bias_is_mask, + false, + phi::errors::InvalidArgument( + "QK_bias is mask can't be supported on rocm or " + "cuda_arch<700")); +#else + dim3 grid(seq_len, batch_size, head_num); + dim3 block((seq_len / 2 + WARP_SIZE - 1) / WARP_SIZE * WARP_SIZE); + SOFTMAX_KERNEL_WITH_MASK(1); +#endif + } else { + SoftmaxKernelWithEltadd2<__half2><<>>( + reinterpret_cast<__half2 *>(qk_buf_), + reinterpret_cast(bias_qk), + batch_size, + head_num, + seq_len / 2, + FINAL_MASK); + } + } + } else { + block = (seq_len <= WARP_SIZE) + ? WARP_SIZE + : ((seq_len + WARP_SIZE - 1) / WARP_SIZE) * WARP_SIZE; + SoftmaxKernelWithEltadd<<>>( + qk_buf_, bias_qk, batch_size, head_num, seq_len, FINAL_MASK); + } + } else { + int grid = batch_size * head_num * seq_len; + int block = 512; + if (seq_len % 2 == 0) { + if (std::is_same::value) { + SoftmaxKernelWithEltaddForLarge2<<>>( + reinterpret_cast(qk_buf_), + reinterpret_cast(bias_qk), + batch_size, + head_num, + seq_len / 2, + FINAL_MASK); + } else { + if (bias_is_mask) { +#if defined(__HIPCC__) || (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 700) + PADDLE_ENFORCE_EQ(bias_is_mask, + false, + phi::errors::InvalidArgument( + "QK_bias is mask can't be supported on rocm or " + "cuda_arch<700")); +#else + dim3 grid(seq_len, batch_size, head_num); + dim3 block((seq_len / 2 + WARP_SIZE - 1) / WARP_SIZE * WARP_SIZE); + if (block.x > 0 && block.x <= 1024) { + SOFTMAX_KERNEL_WITH_MASK(1); + } else if (block.x <= 2048) { + SOFTMAX_KERNEL_WITH_MASK(2); + } else if (block.x <= 4096) { + SOFTMAX_KERNEL_WITH_MASK(4); + } else { + PADDLE_THROW(phi::errors::InvalidArgument( + "Cannot support the length of attention > 8192.")); + } +#endif + } else { + SoftmaxKernelWithEltaddForLarge2<__half2><<>>( + reinterpret_cast<__half2 *>(qk_buf_), + reinterpret_cast(bias_qk), + batch_size, + head_num, + seq_len / 2, + FINAL_MASK); + } + } + } else { + SoftmaxKernelWithEltaddForLarge<<>>( + qk_buf_, bias_qk, batch_size, head_num, seq_len, FINAL_MASK); + } + } +} + +template +inline void MatmulWithHeadQKV(const phi::GPUContext &context, + int head_num, + int seq_len, + int size_per_head, + int batch_size, + bool qk_trans, + bool v_trans, + T *v_buf_, + const T *qk_buf_, + T *dst, + T alpha, + T beta) { + int m = batch_size * seq_len; + int k = head_num * size_per_head; + + typedef typename CUDATypeTraits::TYPE run_type; + auto blas = phi::funcs::GetBlas(context); + auto stream = context.stream(); + CBLAS_TRANSPOSE transA = !qk_trans ? CblasNoTrans : CblasTrans; + CBLAS_TRANSPOSE transB = !v_trans ? CblasNoTrans : CblasTrans; + + blas.BatchedGEMM(transA, + transB, + seq_len, + size_per_head, + seq_len, + static_cast(alpha), + reinterpret_cast(qk_buf_), + reinterpret_cast(v_buf_), + static_cast(beta), + reinterpret_cast(dst), + batch_size * head_num, + seq_len * seq_len, + seq_len * size_per_head); +} + +template +void MultiheadGPUComputeFunctor::operator()(const phi::GPUContext &dev_ctx, + int batch, + int seq_len, + int head_num, + int head_size, + T *qkptr, + const T *bias_qk_ptr, + bool bias_is_mask, + T *tptr, + T alpha, + T beta) { + auto stream = dev_ctx.stream(); + const int tsize = batch * head_num * seq_len * head_size; + + T *qptr = tptr; + T *kptr = qptr + tsize; + T *vptr = kptr + tsize; + // batch gemm stride, softmaxwithscale. + MatmulWithHeadQK(dev_ctx, + head_num, + seq_len, + head_size, + batch, + false, + true, + qptr, + kptr, + qkptr, + bias_qk_ptr, + bias_is_mask, + alpha, + beta); + // batch gemm stride, transpose. + MatmulWithHeadQKV(dev_ctx, + head_num, + seq_len, + head_size, + batch, + false, + false, + vptr, + qkptr, + tptr, + T(1.0), + beta); +} + +template class MultiheadGPUComputeFunctor; + +// device function 'operator()' is not supportted until cuda 10.0 +// HIP defined __HIP_NO_HALF_CONVERSIONS__ in hip.cmake +#if defined(PADDLE_WITH_CUDA) && CUDA_VERSION >= 10000 +template class MultiheadGPUComputeFunctor; +#endif + +} // namespace funcs +} // namespace phi diff --git a/paddle/phi/kernels/funcs/multihead_matmul_functor.h b/paddle/phi/kernels/funcs/multihead_matmul_functor.h new file mode 100644 index 0000000000000..00f331861bf09 --- /dev/null +++ b/paddle/phi/kernels/funcs/multihead_matmul_functor.h @@ -0,0 +1,51 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/phi/backends/gpu/gpu_context.h" + +namespace phi { +namespace funcs { + +// This functor involves a fusion calculation in Ernie or Bert. +// The fusion mode is as follows: +// +// | | +// matmul +// | +// eltwise_add +// | +// softmax / +// \ / +// matmul +// | +template +class MultiheadGPUComputeFunctor { + public: + void operator()(const phi::GPUContext &dev_ctx, + int batch, + int seq_len, + int head_num, + int head_size, + T *qkptr, + const T *bias_qk_ptr, + bool bias_is_mask, + T *tptr, + T alpha, + T beta); +}; + +} // namespace funcs +} // namespace phi diff --git a/paddle/fluid/operators/fused/multihead_matmul_op.cu b/paddle/phi/kernels/fusion/gpu/multihead_matmul_kernel.cu similarity index 54% rename from paddle/fluid/operators/fused/multihead_matmul_op.cu rename to paddle/phi/kernels/fusion/gpu/multihead_matmul_kernel.cu index 8402bc78ef64c..06f28d387b3b3 100644 --- a/paddle/fluid/operators/fused/multihead_matmul_op.cu +++ b/paddle/phi/kernels/fusion/gpu/multihead_matmul_kernel.cu @@ -1,4 +1,4 @@ -// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,20 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/memory/malloc.h" -#include "paddle/fluid/operators/math/bert_encoder_functor.h" -#include "paddle/fluid/platform/float16.h" +#include "paddle/phi/common/float16.h" +#include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/errors.h" +#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/blas/blas.h" +#include "paddle/phi/kernels/funcs/multihead_matmul_functor.h" -namespace paddle { -namespace operators { +namespace phi { +namespace fusion { template __global__ void transpose(T *src, @@ -149,7 +148,7 @@ void TransQKVWithBias(const int batch, // limit h * head_num to max block size(1024). PADDLE_ENFORCE_LE(h * head_num, 1024, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "head_num (%d) * head_size (%d) should <= %d", head_num, head_size, @@ -165,7 +164,7 @@ void TransQKVWithBias(const int batch, // limit h * head_num to max block size(1024). PADDLE_ENFORCE_LE(h * head_num, 1024, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "head_num (%d) * head_size (%d) should <= %d", head_num, head_size, @@ -177,7 +176,7 @@ void TransQKVWithBias(const int batch, // limit head_size * head_num to max block size(1024). PADDLE_ENFORCE_LE(head_size * head_num, 1024, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "head_num (%d) * head_size (%d) should <= %d", head_num, head_size, @@ -193,9 +192,9 @@ void TransQKVWithBias(const int batch, const int seq_len, const int head_size, const int head_num, - const platform::float16 *input, - const platform::float16 *bias, - platform::float16 *output, + const phi::dtype::float16 *input, + const phi::dtype::float16 *bias, + phi::dtype::float16 *output, gpuStream_t stream) { // BxSx3xNxH + 3xNxH -> 3xBxNxSxH int scratch_size = batch * head_num * seq_len * seq_len; @@ -209,7 +208,7 @@ void TransQKVWithBias(const int batch, // limit h * head_num to max block size(1024). PADDLE_ENFORCE_LE(h * head_num, 1024, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "head_num (%d) * head_size (%d) should <= %d", head_num, head_size, @@ -225,7 +224,7 @@ void TransQKVWithBias(const int batch, // limit head_size * head_num to max block size(1024). PADDLE_ENFORCE_LE(head_size * head_num, 1024, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "head_num (%d) * head_size (%d) should <= %d", head_num, head_size, @@ -240,7 +239,7 @@ inline int round_up(int seq_len, int multiple = 32) { PADDLE_ENFORCE_GT( multiple, 0, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "multiple should be a positive number, but it's (%d)", multiple)); return ((seq_len + multiple - 1) / multiple) * multiple; } @@ -270,168 +269,166 @@ __global__ void broadcast_batch_head_number(const T *src, } } -template -class MultiHeadMatMulV2Kernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext &context) const override { - auto *input = context.Input("Input"); - auto *w = context.Input("W"); - auto *bias = context.Input("Bias"); - auto *bias_qk = context.Input("BiasQK"); - - auto *input_d = input->data(); - auto *w_d = w->data(); - auto *bias_d = bias->data(); - auto *bias_qk_d = bias_qk ? bias_qk->data() : nullptr; - T scale = static_cast(context.Attr("alpha")); - - int head_number = context.Attr("head_number"); - // compute q*k with eltadd - auto &device_ctx = context.template device_context(); - auto stream = device_ctx.stream(); - // should be (B * S * hidden) - auto input_dims = input->dims(); - // shouble be (hidden * 3 * all_head_size) - auto w_dims = w->dims(); - int batch = input_dims[0]; - int seq_len = input_dims[1]; - int hidden = input_dims[2]; - phi::DenseTensor temp_bias_tensor; - // if bias_qk is[batch, 1, 1, seq_len], the bias_qk_d need to be broadcasted - if (bias_qk && bias_qk->numel() == (batch * seq_len)) { - VLOG(4) << "Do broadcasted bias_qk from [batch, 1, 1, seq_len]"; - temp_bias_tensor.Resize({batch * head_number * seq_len * seq_len}); - auto *temp_qk_bias = device_ctx.template Alloc( - &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); - int grid = batch * head_number * seq_len; - int block = round_up(seq_len); - broadcast<<>>( - bias_qk_d, temp_qk_bias, seq_len, head_number); - bias_qk_d = static_cast(temp_qk_bias); - } - // if bias_qk is[1, 1, seq_len, seq_len], the bias_qk_d need to be - // broadcasted - if (bias_qk && bias_qk->numel() == (1 * seq_len * seq_len)) { - VLOG(4) << "do broadcasted bias_qk from [1, 1, seq_len, seq_len]"; - temp_bias_tensor.Resize({batch * head_number * seq_len * seq_len}); - auto *temp_qk_bias = device_ctx.template Alloc( - &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); - int grid = batch * head_number * seq_len; - int block = round_up(seq_len); - broadcast_batch_head_number<<>>( - bias_qk_d, temp_qk_bias, batch, seq_len, head_number); - bias_qk_d = static_cast(temp_qk_bias); - } - if (!bias_qk) { - int size = batch * head_number * seq_len * seq_len; - temp_bias_tensor.Resize({size}); - auto *temp_qk_bias = device_ctx.template Alloc( - &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); +template +void MultiheadMatmulKernel(const Context &dev_ctx, + const DenseTensor &input, + const DenseTensor &w, + const DenseTensor &bias, + const paddle::optional &bias_qk, + const bool transpose_q, + const bool transpose_k, + const bool transpose_v, + const float alpha, + const int head_number, + DenseTensor *out) { + auto *input_d = input.data(); + auto *w_d = w.data(); + auto *bias_d = bias.data(); + auto *bias_qk_d = bias_qk ? bias_qk->data() : nullptr; + T scale = static_cast(alpha); + + // compute q*k with eltadd + auto stream = dev_ctx.stream(); + // should be (B * S * hidden) + auto input_dims = input.dims(); + // shouble be (hidden * 3 * all_head_size) + auto w_dims = w.dims(); + int batch = input_dims[0]; + int seq_len = input_dims[1]; + int hidden = input_dims[2]; + phi::DenseTensor temp_bias_tensor; + // if bias_qk is[batch, 1, 1, seq_len], the bias_qk_d need to be broadcasted + if (bias_qk && bias_qk->numel() == (batch * seq_len)) { + VLOG(4) << "Do broadcasted bias_qk from [batch, 1, 1, seq_len]"; + temp_bias_tensor.Resize({batch * head_number * seq_len * seq_len}); + auto *temp_qk_bias = dev_ctx.template Alloc( + &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); + int grid = batch * head_number * seq_len; + int block = round_up(seq_len); + broadcast<<>>( + bias_qk_d, temp_qk_bias, seq_len, head_number); + bias_qk_d = static_cast(temp_qk_bias); + } + // if bias_qk is[1, 1, seq_len, seq_len], the bias_qk_d need to be + // broadcasted + if (bias_qk && bias_qk->numel() == (1 * seq_len * seq_len)) { + VLOG(4) << "do broadcasted bias_qk from [1, 1, seq_len, seq_len]"; + temp_bias_tensor.Resize({batch * head_number * seq_len * seq_len}); + auto *temp_qk_bias = dev_ctx.template Alloc( + &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); + int grid = batch * head_number * seq_len; + int block = round_up(seq_len); + broadcast_batch_head_number<<>>( + bias_qk_d, temp_qk_bias, batch, seq_len, head_number); + bias_qk_d = static_cast(temp_qk_bias); + } + if (!bias_qk) { + int size = batch * head_number * seq_len * seq_len; + temp_bias_tensor.Resize({size}); + auto *temp_qk_bias = dev_ctx.template Alloc( + &temp_bias_tensor, temp_bias_tensor.numel() * sizeof(T)); #ifdef PADDLE_WITH_HIP - hipMemset(temp_qk_bias, 0, sizeof(float) * size); + hipMemset(temp_qk_bias, 0, sizeof(float) * size); #else - cudaMemset(temp_qk_bias, 0, sizeof(float) * size); + cudaMemset(temp_qk_bias, 0, sizeof(float) * size); #endif - bias_qk_d = static_cast(temp_qk_bias); - } - int all_head_size = w_dims[2]; - int head_size = all_head_size / head_number; - - auto *out = context.Output("Out"); - out->Resize({batch, seq_len, all_head_size}); - auto *output_d = - device_ctx.template Alloc(out, out->numel() * sizeof(T)); - - // (B*S, hidden) - const phi::DenseTensor input_matrix = - phi::ReshapeToMatrix(*input, 2 /*x_num_col_dims */); - // (hidden, 3 * all_head_size) - const phi::DenseTensor w_matrix = - phi::ReshapeToMatrix(*w, 1 /*y_num_col_dims*/); - - phi::DenseTensor temp_out_tensor; - auto temp_out_dims = - phi::make_ddim({batch, seq_len, 3, head_number, head_size}); - temp_out_tensor.Resize( - {batch * seq_len, phi::product(temp_out_dims) / (batch * seq_len)}); - auto *temp_out_data = device_ctx.template Alloc( - &temp_out_tensor, temp_out_tensor.numel() * sizeof(T)); - - // (B * S, hidden) * (hidden, 3 * N * H) -> (B * S * 3 * N * H) - auto blas = phi::funcs::GetBlas(device_ctx); - blas.MatMul(input_matrix, w_matrix, &temp_out_tensor); - VLOG(2) << "(B * S, hidden) * (hidden, 3 * N * H) -> (B * S * 3 * N * H)"; - VLOG(2) << temp_out_tensor; - // temp_out_tensor.Resize(temp_out_dims); - - phi::DenseTensor multihead_temp_tensor; - // B * head_number * S * S * 1 + B * S * 3 * N * H - int scratch_size = batch * head_number * seq_len * seq_len * 1; - multihead_temp_tensor.Resize({scratch_size + temp_out_tensor.numel()}); - auto *multihead_temp_data = device_ctx.template Alloc( - &multihead_temp_tensor, multihead_temp_tensor.numel() * sizeof(T)); - - auto *qkptr = multihead_temp_data; - auto *tptr = multihead_temp_data + scratch_size; - - // Do the transpose with bias. - // BxSx3xNxH => tptr: 3xBxNxSxH. - TransQKVWithBias(batch, - seq_len, - head_size, - head_number, - temp_out_data, - bias_d, - tptr, - stream); - if (std::is_same::value) { - math::MultiHeadGPUComputeFunctor multihead_compute_func; - multihead_compute_func(device_ctx, - batch, - seq_len, - head_number, - head_size, - reinterpret_cast(qkptr), - reinterpret_cast(bias_qk_d), - false, - reinterpret_cast(tptr), - __float2half(static_cast(scale)), - __float2half(0.0)); - } else { - math::MultiHeadGPUComputeFunctor multihead_compute_func; - multihead_compute_func(device_ctx, - batch, - seq_len, - head_number, - head_size, - qkptr, - bias_qk_d, - false, - tptr, - scale, - T(0.0)); - } - - int grid = batch * head_number * seq_len; - int block = head_size; - transpose<<>>( - tptr, output_d, batch, seq_len, head_number, head_size); + bias_qk_d = static_cast(temp_qk_bias); + } + int all_head_size = w_dims[2]; + int head_size = all_head_size / head_number; + + out->Resize({batch, seq_len, all_head_size}); + auto *output_d = dev_ctx.template Alloc(out, out->numel() * sizeof(T)); + + // (B*S, hidden) + const phi::DenseTensor input_matrix = + phi::ReshapeToMatrix(input, 2 /*x_num_col_dims */); + // (hidden, 3 * all_head_size) + const phi::DenseTensor w_matrix = + phi::ReshapeToMatrix(w, 1 /*y_num_col_dims*/); + + phi::DenseTensor temp_out_tensor; + auto temp_out_dims = + phi::make_ddim({batch, seq_len, 3, head_number, head_size}); + temp_out_tensor.Resize( + {batch * seq_len, phi::product(temp_out_dims) / (batch * seq_len)}); + auto *temp_out_data = dev_ctx.template Alloc( + &temp_out_tensor, temp_out_tensor.numel() * sizeof(T)); + + // (B * S, hidden) * (hidden, 3 * N * H) -> (B * S * 3 * N * H) + auto blas = phi::funcs::GetBlas(dev_ctx); + blas.MatMul(input_matrix, w_matrix, &temp_out_tensor); + VLOG(2) << "(B * S, hidden) * (hidden, 3 * N * H) -> (B * S * 3 * N * H)"; + // temp_out_tensor.Resize(temp_out_dims); + + phi::DenseTensor multihead_temp_tensor; + // B * head_number * S * S * 1 + B * S * 3 * N * H + int scratch_size = batch * head_number * seq_len * seq_len * 1; + multihead_temp_tensor.Resize({scratch_size + temp_out_tensor.numel()}); + auto *multihead_temp_data = dev_ctx.template Alloc( + &multihead_temp_tensor, multihead_temp_tensor.numel() * sizeof(T)); + + auto *qkptr = multihead_temp_data; + auto *tptr = multihead_temp_data + scratch_size; + + // Do the transpose with bias. + // BxSx3xNxH => tptr: 3xBxNxSxH. + TransQKVWithBias(batch, + seq_len, + head_size, + head_number, + temp_out_data, + bias_d, + tptr, + stream); + if (std::is_same::value) { + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; + multihead_compute_func(dev_ctx, + batch, + seq_len, + head_number, + head_size, + reinterpret_cast(qkptr), + reinterpret_cast(bias_qk_d), + false, + reinterpret_cast(tptr), + __float2half(static_cast(scale)), + __float2half(0.0)); + } else { + phi::funcs::MultiheadGPUComputeFunctor multihead_compute_func; + multihead_compute_func(dev_ctx, + batch, + seq_len, + head_number, + head_size, + qkptr, + bias_qk_d, + false, + tptr, + scale, + T(0.0)); } -}; -} // namespace operators -} // namespace paddle + int grid = batch * head_number * seq_len; + int block = head_size; + transpose<<>>( + tptr, output_d, batch, seq_len, head_number, head_size); +} + +} // namespace fusion +} // namespace phi -namespace ops = paddle::operators; -namespace plat = paddle::platform; #if defined(PADDLE_WITH_CUDA) && CUDA_VERSION >= 10000 -PD_REGISTER_STRUCT_KERNEL(multihead_matmul, - GPU, - ALL_LAYOUT, - ops::MultiHeadMatMulV2Kernel, - float, - plat::float16) {} +PD_REGISTER_KERNEL(multihead_matmul, + GPU, + ALL_LAYOUT, + phi::fusion::MultiheadMatmulKernel, + float, + phi::dtype::float16) {} #else -PD_REGISTER_STRUCT_KERNEL( - multihead_matmul, GPU, ALL_LAYOUT, ops::MultiHeadMatMulV2Kernel, float) {} +PD_REGISTER_KERNEL(multihead_matmul, + GPU, + ALL_LAYOUT, + phi::fusion::MultiheadMatmulKernel, + float) {} #endif From 201480d5295be18d312b6d543602b979659db801 Mon Sep 17 00:00:00 2001 From: ShenLiang <1422485404@qq.com> Date: Mon, 4 Sep 2023 16:47:16 +0800 Subject: [PATCH 103/194] fix bug in c_split (#56917) --- .../distributed/fleet/layers/mpu/mp_ops.py | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/python/paddle/distributed/fleet/layers/mpu/mp_ops.py b/python/paddle/distributed/fleet/layers/mpu/mp_ops.py index 82a072fe05640..ccfba7acafffd 100644 --- a/python/paddle/distributed/fleet/layers/mpu/mp_ops.py +++ b/python/paddle/distributed/fleet/layers/mpu/mp_ops.py @@ -48,6 +48,38 @@ def backward(ctx, dy): return dy +class c_split_eager(PyLayer): + @staticmethod + def forward(ctx, tensor, group, rank, nranks): + ctx.group = group + ctx.nranks = nranks + return _legacy_C_ops.c_split( + tensor, + 'use_calc_stream', + True, + 'ring_id', + group.id, + 'rank', + rank, + 'nranks', + nranks, + 'use_model_parallel', + True, + ) + + @staticmethod + def backward(ctx, dy): + group = ctx.group + out_shape = dy.shape + out_shape[0] = out_shape[0] * ctx.nranks + out = paddle.empty(out_shape, dtype=dy.dtype) + group.process_group.all_gather_into_tensor_on_calc_stream( + out, + dy, + ) + return out + + def _c_identity(tensor, group=None, skip_c_identity_dynamic=False): """ Return a copy of the tensor, mainly used with model parallel. @@ -179,19 +211,7 @@ def _c_split(tensor, group=None): ) if in_dynamic_mode(): - return _legacy_C_ops.c_split( - tensor, - 'use_calc_stream', - True, - 'ring_id', - ring_id, - 'rank', - rank, - 'nranks', - nranks, - 'use_model_parallel', - True, - ) + return c_split_eager.apply(tensor, group, rank, nranks) else: op_type = 'c_split' helper = LayerHelper(op_type, **locals()) From be9cb946e507832f9c0fe5fc1d0f5343f1da08b9 Mon Sep 17 00:00:00 2001 From: Sonder <55493212+AndSonder@users.noreply.github.com> Date: Mon, 4 Sep 2023 17:26:49 +0800 Subject: [PATCH 104/194] Remove has_structed_kerenl and has_fluid_kernel (#56779) * remove has_structed_kerenl and has_fluid_kernel * add test_fused_layernorm_op to STATIC_BUILD_TESTS list * open static_build flag * remove distributed_fused_lamb_init from StaticBuildBlackList * use initialized replacing IsInitialized * recover codes * delete useless codes * close the flag --- .../framework/new_executor/interpreter/static_build.cc | 9 +-------- test/legacy_test/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/paddle/fluid/framework/new_executor/interpreter/static_build.cc b/paddle/fluid/framework/new_executor/interpreter/static_build.cc index a3c9802a7d35e..bf9b257c01c83 100644 --- a/paddle/fluid/framework/new_executor/interpreter/static_build.cc +++ b/paddle/fluid/framework/new_executor/interpreter/static_build.cc @@ -47,8 +47,6 @@ std::set StaticBuildBlackList = { "cinn_launch" /*: to handle subgraph infermeta*/, "run_program" /*: to handle scope output*/, "sparse_sparse_coo_tensor" /*: to handle sparse output*/, - "shuffle_batch", - "shuffle_batch_grad", "distributed_fused_lamb_init"}; namespace paddle { @@ -60,8 +58,6 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { // is_operator_base = (kernelCode >> 6) & 1 // is_custom_op = (kernelCode >> 5) & 1 // use_mkldnn = (kernelCode >> 4) & 1 - // has_fluid_kernel = (kernelCode >> 3) & 1 - // has_structed_kernel = (kernelCode >> 2) & 1 using KernelCode = int8_t; std::set> invalid_ops; for (auto& op : block.AllOps()) { @@ -81,13 +77,11 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { use_mkldnn = attr.index() == 1 ? PADDLE_GET_CONST(int, attr) : PADDLE_GET_CONST(bool, attr); } - bool has_fluid_kernel = OperatorWithKernel::AllOpKernels().count(op_type); bool has_structured_kernel = phi::KernelFactory::Instance().HasStructuredKernel(op_type); KernelCode kernel_code = (in_black_list << 7) + (is_operator_base << 6) + (is_custom_op << 5) + (use_mkldnn << 4) + - (has_fluid_kernel << 3) + (has_structured_kernel << 2); if (!OpsCanSkipedFakeAllocInStaticBuild.count(op_type)) { if (in_black_list || @@ -107,8 +101,7 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { << ", is_operator_base = " << (item.second >> 6 & 1) << ", is_custom_op = " << (item.second >> 5 & 1) << ", use_mkldnn = " << (item.second >> 4 & 1) - << ", has_fluid_kernel = " << (item.second >> 3 & 1) - << ", has_structed_kerenl = " << (item.second >> 2 & 1) << "]\n"; + << (item.second >> 2 & 1) << "]\n"; } VLOG(1) << ss.str(); } diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 8e0b9ad91cba5..05f30dca257f1 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -1290,6 +1290,7 @@ set(STATIC_BUILD_TESTS test_fuse_bn_act_pass test_fused_feedforward_op test_fused_feedforward_pass + test_fused_layernorm_op test_imperative_optimizer test_lamb_op test_layer_norm_op From c089a2af5469f7a8fa5219be2d4586f4669485e4 Mon Sep 17 00:00:00 2001 From: tianhaodongbd <137985359+tianhaodongbd@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:52:29 +0800 Subject: [PATCH 105/194] Add rotate_half implementation for fused_rope (#56401) * add rotate_half in fused_rope * add position_ids in fused_rope * modified examples about fused_rope * add set_device in examples --- paddle/phi/api/yaml/fused_backward.yaml | 6 +- paddle/phi/api/yaml/fused_ops.yaml | 4 +- paddle/phi/infermeta/backward.cc | 2 + paddle/phi/infermeta/backward.h | 2 + paddle/phi/infermeta/multiary.cc | 2 + paddle/phi/infermeta/multiary.h | 2 + .../fusion/gpu/fused_rope_grad_kernel.cu | 48 +++-- .../kernels/fusion/gpu/fused_rope_kernel.cu | 99 ++++++++-- .../phi/kernels/fusion/gpu/fused_rope_utils.h | 183 ++++++++++++++---- .../fused_rotary_position_embedding.py | 76 ++++++-- .../test_fused_rotary_position_embedding.py | 149 ++++++++++++-- 11 files changed, 459 insertions(+), 114 deletions(-) diff --git a/paddle/phi/api/yaml/fused_backward.yaml b/paddle/phi/api/yaml/fused_backward.yaml index 5f49e790e2550..8dfa117d44c97 100644 --- a/paddle/phi/api/yaml/fused_backward.yaml +++ b/paddle/phi/api/yaml/fused_backward.yaml @@ -17,10 +17,10 @@ support_dygraph_mode : true - backward_op : fused_rotary_position_embedding_grad - forward: fused_rotary_position_embedding (Tensor q, Tensor k, Tensor v, Tensor sin, Tensor cos) -> Tensor(out_q), Tensor(out_k), Tensor(out_v) - args : (Tensor sin, Tensor cos, Tensor out_q_grad, Tensor out_k_grad,Tensor out_v_grad) + forward: fused_rotary_position_embedding (Tensor q, Tensor k, Tensor v, Tensor sin, Tensor cos, Tensor position_ids, bool use_neox_rotary_style) -> Tensor(out_q), Tensor(out_k), Tensor(out_v) + args : (Tensor sin, Tensor cos, Tensor position_ids, Tensor out_q_grad, Tensor out_k_grad,Tensor out_v_grad, bool use_neox_rotary_style) output : Tensor(q_grad), Tensor(k_grad), Tensor(v_grad) - optional : sin, cos, out_k_grad, out_v_grad, k_grad, v_grad + optional : sin, cos, position_ids, out_k_grad, out_v_grad, k_grad, v_grad infer_meta : func : FusedRopeGradInferMeta kernel : diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index 90593ed4d43ea..9820fd6140557 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -149,11 +149,11 @@ optional : cache_kv, pre_caches, rotary_pos_emb, time_step, seq_lengths, src_mask, gather_index - op : fused_rotary_position_embedding - args : (Tensor q, Tensor k, Tensor v, Tensor sin, Tensor cos) + args : (Tensor q, Tensor k, Tensor v, Tensor sin, Tensor cos, Tensor position_ids, bool use_neox_rotary_style = true) output : Tensor(out_q), Tensor(out_k), Tensor(out_v) infer_meta : func : FusedRopeInferMeta - optional : k,v,sin,cos, out_k, out_v + optional : k, v, sin, cos, position_ids, out_k, out_v kernel : func : fused_rotary_position_embedding data_type : q diff --git a/paddle/phi/infermeta/backward.cc b/paddle/phi/infermeta/backward.cc index 31da8f079bb43..5dedf81e937ac 100644 --- a/paddle/phi/infermeta/backward.cc +++ b/paddle/phi/infermeta/backward.cc @@ -1219,9 +1219,11 @@ void IndexPutGradInferMeta(const MetaTensor& x, void FusedRopeGradInferMeta(const MetaTensor& sin, const MetaTensor& cos, + const MetaTensor& position_ids, const MetaTensor& dout_q, const MetaTensor& dout_k, const MetaTensor& dout_v, + bool use_neox_rotary_style, MetaTensor* dq, MetaTensor* dk, MetaTensor* dv) { diff --git a/paddle/phi/infermeta/backward.h b/paddle/phi/infermeta/backward.h index b8a3aee03bde4..a00bc2cde450f 100644 --- a/paddle/phi/infermeta/backward.h +++ b/paddle/phi/infermeta/backward.h @@ -186,9 +186,11 @@ void FusedDropoutAddGradInferMeta(const MetaTensor& seed_offset, void FusedRopeGradInferMeta(const MetaTensor& sin, const MetaTensor& cos, + const MetaTensor& position_ids, const MetaTensor& dout_q, const MetaTensor& dout_k, const MetaTensor& dout_v, + bool use_neox_rotary_style, MetaTensor* dq, MetaTensor* dk, MetaTensor* dv); diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index e54c6ab3ba7dc..63c421dcdcdb9 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -4041,6 +4041,8 @@ void FusedRopeInferMeta(const MetaTensor& q, const MetaTensor& v, const MetaTensor& sin, const MetaTensor& cos, + const MetaTensor& position_ids, + bool use_neox_rotary_style, MetaTensor* out_q, MetaTensor* out_k, MetaTensor* out_v) { diff --git a/paddle/phi/infermeta/multiary.h b/paddle/phi/infermeta/multiary.h index 8a782676fe917..9beb9d213899d 100644 --- a/paddle/phi/infermeta/multiary.h +++ b/paddle/phi/infermeta/multiary.h @@ -807,6 +807,8 @@ void FusedRopeInferMeta(const MetaTensor& q, const MetaTensor& v, const MetaTensor& sin, const MetaTensor& cos, + const MetaTensor& position_ids, + bool use_neox_rotary_style, MetaTensor* out_q, MetaTensor* out_k, MetaTensor* out_v); diff --git a/paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu b/paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu index 442317eb53d98..70ea70912f639 100644 --- a/paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu +++ b/paddle/phi/kernels/fusion/gpu/fused_rope_grad_kernel.cu @@ -27,9 +27,11 @@ template void FusedRopeGradKernel(const Context& dev_ctx, const paddle::optional& sin, const paddle::optional& cos, + const paddle::optional& position_ids, const DenseTensor& dout_q, const paddle::optional& dout_k, const paddle::optional& dout_v, + bool use_neox_rotary_style, DenseTensor* dq, DenseTensor* dk, DenseTensor* dv) { @@ -58,6 +60,7 @@ void FusedRopeGradKernel(const Context& dev_ctx, phi::Array outs_data; phi::Array ins_data; phi::Array sin_cos_data; + const int64_t* position_ids_data = NULL; ins_data[0] = dout_q.data(); outs_data[0] = dq->data(); @@ -86,21 +89,42 @@ void FusedRopeGradKernel(const Context& dev_ctx, sin_cos_data[1] = cos->data(); flag_sin_cos = true; + + if (position_ids.get_ptr()) { + position_ids_data = position_ids->data(); + } } int sign = -1; - VectorizedFusedRopeKernel - <<>>(ins_data, - sin_cos_data, - flag_sin_cos, - sign, - batch_size, - seq_len, - num_heads, - head_dim, - outs_data, - num_inputs, - div_c); + if (use_neox_rotary_style) { + VectorizedFusedRopeWithRotateEveryTwoKernel + <<>>(ins_data, + sin_cos_data, + position_ids_data, + flag_sin_cos, + sign, + batch_size, + seq_len, + num_heads, + head_dim, + outs_data, + num_inputs, + div_c); + } else { + VectorizedFusedRopeWithRotateHalfKernel + <<>>(ins_data, + sin_cos_data, + position_ids_data, + flag_sin_cos, + sign, + batch_size, + seq_len, + num_heads, + head_dim, + outs_data, + num_inputs, + div_c); + } } } // namespace fusion diff --git a/paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu b/paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu index f6dcbc2a9038f..6e032211cc6a0 100644 --- a/paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu +++ b/paddle/phi/kernels/fusion/gpu/fused_rope_kernel.cu @@ -30,6 +30,8 @@ void FusedRopeKernel(const Context& dev_ctx, const paddle::optional& v, const paddle::optional& sin, const paddle::optional& cos, + const paddle::optional& position_ids, + bool use_neox_rotary_style, DenseTensor* out_q, DenseTensor* out_k, DenseTensor* out_v) { @@ -59,6 +61,7 @@ void FusedRopeKernel(const Context& dev_ctx, phi::Array outs_data; phi::Array ins_data; phi::Array sin_cos_data; + const int64_t* position_ids_data = NULL; ins_data[0] = q.data(); outs_data[0] = out_q->data(); @@ -109,15 +112,52 @@ void FusedRopeKernel(const Context& dev_ctx, "The batch_size and num_heads of sin and cos must be 1.")); } int sin_seq_len_dim = (dims_size) == 4 ? 1 : 0; - PADDLE_ENFORCE_EQ((sin_dims[dims_size - 1] == head_dim && - sin_dims[sin_seq_len_dim] == seq_len), - true, - phi::errors::InvalidArgument( - "The seq_len and head_dim of sin and cos " - "must be the same as those of q. But recieved sin's " - "shape is {%s}, q's shape is {%s}.", - sin_dims, - q.dims())); + + if (position_ids.get_ptr()) { + PADDLE_ENFORCE_EQ( + (sin_dims[dims_size - 1] == head_dim && + sin_dims[sin_seq_len_dim] >= seq_len), + true, + phi::errors::InvalidArgument( + "The seq_len of sin and cos must be greater than or equal to " + "this of q. The head_dim of sin and cos must be the same as this " + "of q. But recieved sin's " + "shape is {%s}, q's shape is {%s}.", + sin_dims, + q.dims())); + + auto position_ids_dims = position_ids.get_ptr()->dims(); + PADDLE_ENFORCE_EQ(position_ids_dims.size(), + 2, + phi::errors::InvalidArgument( + "The dims of position_ids is expected to " + "be 2, but recieved %d.", + position_ids_dims.size())); + + PADDLE_ENFORCE_EQ( + (position_ids_dims[0] == batch_size && + position_ids_dims[1] == seq_len), + true, + phi::errors::InvalidArgument( + "The batch_size and seq_len of position_ids must be the same as " + "those of q. But recieved position_ids's " + "shape is {%s}, q's shape is {%s}.", + position_ids_dims, + q.dims())); + + position_ids_data = position_ids->data(); + } else { + PADDLE_ENFORCE_EQ( + (sin_dims[dims_size - 1] == head_dim && + sin_dims[sin_seq_len_dim] == seq_len), + true, + phi::errors::InvalidArgument( + "The seq_len and head_dim of sin and cos " + "must be the same as those of q. But recieved sin's " + "shape is {%s}, q's shape is {%s}.", + sin_dims, + q.dims())); + } sin_cos_data[0] = sin->data(); sin_cos_data[1] = cos->data(); @@ -126,18 +166,35 @@ void FusedRopeKernel(const Context& dev_ctx, } int sign = 1; - VectorizedFusedRopeKernel - <<>>(ins_data, - sin_cos_data, - flag_sin_cos, - sign, - batch_size, - seq_len, - num_heads, - head_dim, - outs_data, - num_inputs, - div_c); + if (use_neox_rotary_style) { + VectorizedFusedRopeWithRotateEveryTwoKernel + <<>>(ins_data, + sin_cos_data, + position_ids_data, + flag_sin_cos, + sign, + batch_size, + seq_len, + num_heads, + head_dim, + outs_data, + num_inputs, + div_c); + } else { + VectorizedFusedRopeWithRotateHalfKernel + <<>>(ins_data, + sin_cos_data, + position_ids_data, + flag_sin_cos, + sign, + batch_size, + seq_len, + num_heads, + head_dim, + outs_data, + num_inputs, + div_c); + } } } // namespace fusion } // namespace phi diff --git a/paddle/phi/kernels/fusion/gpu/fused_rope_utils.h b/paddle/phi/kernels/fusion/gpu/fused_rope_utils.h index 54ffba19e60c0..972f5ee633bbb 100644 --- a/paddle/phi/kernels/fusion/gpu/fused_rope_utils.h +++ b/paddle/phi/kernels/fusion/gpu/fused_rope_utils.h @@ -20,17 +20,71 @@ namespace phi { namespace fusion { template -__global__ void VectorizedFusedRopeKernel(phi::Array ins_data, - phi::Array sin_cos_data, - bool flag_sin_cos, - int sign, - int64_t batch_size, - int64_t seq_len, - int64_t num_heads, - int64_t head_dim, - phi::Array outs_data, - int num_inputs, - MPType div_c) { +__device__ void VectorizedGetSinCos(phi::Array sin_cos_data, + const int64_t* position_ids_data, + bool flag_sin_cos, + int64_t index, + int64_t seq_len, + int64_t num_heads, + int64_t head_dim, + MPType* out_sin, + MPType* out_cos, + MPType div_c) { + MPType* sin_value = out_sin; + MPType* cos_value = out_cos; + + if (flag_sin_cos) { +#pragma unroll + for (int64_t nx = 0; nx < VecSize; ++nx) { + int64_t index_wc = (index + nx) % (seq_len * num_heads * head_dim); + int64_t pos_seq_ori = index_wc / (num_heads * head_dim); + int64_t pos_seq; + if (position_ids_data) { + int64_t pos_bs = (index + nx) / (seq_len * num_heads * head_dim); + int64_t index_ids = pos_bs * seq_len + pos_seq_ori; + pos_seq = position_ids_data[index_ids]; + } else { + pos_seq = pos_seq_ori; + } + int64_t pos_head = index_wc % head_dim; + int64_t index_sc = pos_seq * head_dim + pos_head; + const T* sin_input = sin_cos_data[0] + index_sc; + const T* cos_input = sin_cos_data[1] + index_sc; + + sin_value[nx] = static_cast(sin_input[0]); + cos_value[nx] = static_cast(cos_input[0]); + } + } else { +#pragma unroll + for (int nx = 0; nx < VecSize; ++nx) { + // get sin_index and cos_index + int64_t index_wc = (index + nx) % (seq_len * num_heads * head_dim); + int64_t pos_seq = index_wc / (num_heads * head_dim); + MPType idx = static_cast((index_wc % head_dim) / 2 * 2.0); + MPType indicses = + static_cast(1) / + pow(static_cast(10000), idx * static_cast(div_c)); + MPType value = pos_seq * indicses; + sin_value[nx] = sin(value); + cos_value[nx] = cos(value); + } + } +} + +template +__global__ void VectorizedFusedRopeWithRotateEveryTwoKernel( + phi::Array ins_data, + phi::Array sin_cos_data, + const int64_t* position_ids_data, + bool flag_sin_cos, + int sign, + int64_t batch_size, + int64_t seq_len, + int64_t num_heads, + int64_t head_dim, + phi::Array outs_data, + int num_inputs, + MPType div_c) { int64_t index = (static_cast(blockIdx.x) * static_cast(blockDim.x) + threadIdx.x) * @@ -46,34 +100,16 @@ __global__ void VectorizedFusedRopeKernel(phi::Array ins_data, constexpr int kVectorsPerThread = VecSize / 2; for (; index < size; index += stride) { - if (flag_sin_cos) { -#pragma unroll - for (int64_t nx = 0; nx < VecSize; ++nx) { - int64_t index_wc = (index + nx) % (seq_len * num_heads * head_dim); - int64_t pos_seq = index_wc / (num_heads * head_dim); - int64_t pos_head = index_wc % head_dim; - int64_t index_sc = pos_seq * head_dim + pos_head; - const T* sin_input = sin_cos_data[0] + index_sc; - const T* cos_input = sin_cos_data[1] + index_sc; - - sin_value[nx] = static_cast(sin_input[0]); - cos_value[nx] = static_cast(cos_input[0]); - } - } else { -#pragma unroll - for (int nx = 0; nx < VecSize; ++nx) { - // get sin_index and cos_index - int64_t index_wc = (index + nx) % (seq_len * num_heads * head_dim); - int64_t pos_seq = index_wc / (num_heads * head_dim); - MPType idx = static_cast((index_wc % head_dim) / 2 * 2.0); - MPType indicses = - static_cast(1) / - pow(static_cast(10000), idx * static_cast(div_c)); - MPType value = pos_seq * indicses; - sin_value[nx] = sin(value); - cos_value[nx] = cos(value); - } - } + VectorizedGetSinCos(sin_cos_data, + position_ids_data, + flag_sin_cos, + index, + seq_len, + num_heads, + head_dim, + sin_value, + cos_value, + div_c); #pragma unroll for (int iter = 0; iter < 3; iter++) { @@ -102,5 +138,74 @@ __global__ void VectorizedFusedRopeKernel(phi::Array ins_data, } } +template +__global__ void VectorizedFusedRopeWithRotateHalfKernel( + phi::Array ins_data, + phi::Array sin_cos_data, + const int64_t* position_ids_data, + bool flag_sin_cos, + int sign, + int64_t batch_size, + int64_t seq_len, + int64_t num_heads, + int64_t head_dim, + phi::Array outs_data, + int num_inputs, + MPType div_c) { + int64_t index = + (static_cast(blockIdx.x) * static_cast(blockDim.x) + + threadIdx.x) * + VecSize; + int64_t stride = static_cast(gridDim.x) * + static_cast(blockDim.x) * VecSize; + int64_t size = batch_size * seq_len * num_heads * head_dim; + MPType sin_value[VecSize]; + MPType cos_value[VecSize]; + MPType result[VecSize]; + T store[VecSize]; + using VecType = phi::AlignedVector; + constexpr int kVectorsPerThread = VecSize / 2; + + for (; index < size; index += stride) { + VectorizedGetSinCos(sin_cos_data, + position_ids_data, + flag_sin_cos, + index, + seq_len, + num_heads, + head_dim, + sin_value, + cos_value, + div_c); + + // use rotate_half mode + int stride_r = head_dim / 2; +#pragma unroll + for (int iter = 0; iter < 3; iter++) { + if (iter > num_inputs) break; + // get value_index and rotate_half_index + int index_v = index; + int index_r = (index % head_dim) < stride_r ? (index + stride_r) + : (index - stride_r); + MPType sign_r = (index % head_dim) < stride_r ? static_cast(-1) + : static_cast(1); + const T* input_v = ins_data[iter] + index_v; + const T* input_r = ins_data[iter] + index_r; + VecType* out = reinterpret_cast(outs_data[iter] + index); + +#pragma unroll + for (int nx = 0; nx < VecSize; ++nx) { + MPType p0 = static_cast(input_v[nx]); + MPType p1 = static_cast(input_r[nx]); + + result[nx] = cos_value[nx] * p0 + sign * sign_r * sin_value[nx] * p1; + + store[nx] = static_cast(result[nx]); + } + out[0] = *(reinterpret_cast(store)); + } + } +} + } // namespace fusion } // namespace phi diff --git a/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py b/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py index e05ae63f07807..f68dfb1dcd53f 100644 --- a/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py +++ b/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py @@ -17,16 +17,26 @@ from paddle.framework import in_dynamic_mode -def fused_rotary_position_embedding(q, k=None, v=None, sin=None, cos=None): +def fused_rotary_position_embedding( + q, + k=None, + v=None, + sin=None, + cos=None, + position_ids=None, + use_neox_rotary_style=True, +): r""" Fused rotary position embedding. Args: - q (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape if q must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. - k (optional|Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape if k must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. - v (optional|Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape if v must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. - sin (optional|Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape if sin must be [seq_len, head_dim] or [1, 1, seq_len, head_dim] and head_dim must be a multiple of 2. - cos (optional|Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape if cos must be [seq_len, head_dim] or [1, 1, seq_len, head_dim] and head_dim must be a multiple of 2. + q (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape of q must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. + k (Tensor, optional): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape of k must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. + v (Tensor, optional): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape of v must be [batch_size, seq_len, num_heads, head_dim] and head_dim must be a multiple of 2. + sin (Tensor, optional): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape of sin must be [seq_len, head_dim] or [1, seq_len, 1, head_dim] and head_dim must be a multiple of 2. + cos (Tensor, optional): The input tensor. The data type is bfloat16, float16, float32 or float64. The shape of cos must be [seq_len, head_dim] or [1, seq_len, 1, head_dim] and head_dim must be a multiple of 2. + position_ids (Tensor, optional): The input tensor. The data type is int64. The shape of position_ids must be [batch_size, seq_len]. + use_neox_rotary_style(optional|bool): When the use_neox_rotary_style is True, every two adjacent numbers are calculated. When the use_neox_rotary_style is False, the numbers corresponding to the positions of the front half and back half segments are calculated. Default True. Returns: out_q/out_k/out_v Tensor representing the fused rotary position embedding, has same shape and data type as `q` . @@ -36,23 +46,51 @@ def fused_rotary_position_embedding(q, k=None, v=None, sin=None, cos=None): .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_rotary_position_embedding + >>> # required: gpu + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_rotary_position_embedding - q = paddle.randn([1, 1, 4, 10], dtype='float16') - k = paddle.randn([1, 1, 4, 10], dtype='float16') - v = paddle.randn([1, 1, 4, 10], dtype='float16') - out_q, out_k, out_v = fused_rotary_position_embedding(q, k, v) + >>> paddle.device.set_device('gpu') - x = paddle.randn([1, 1, 1, 10], dtype='float16') - y = paddle.randn([1, 1, 1, 10], dtype='float16') - sin = paddle.sin(x) - cos = paddle.cos(y) - out_q, out_k, out_v = fused_rotary_position_embedding(q, k, v, sin=sin, cos=cos) + >>> # batch_size = 2 + >>> # seq_len = 2 + >>> # num_heads = 2 + >>> # head_dim = 2 + + >>> paddle.seed(1204) + + >>> # q, k, v: [batch_size, seq_len, num_heads, head_dim] + >>> q = paddle.randn([2, 2, 2, 2], dtype='float16') + >>> k = paddle.randn([2, 2, 2, 2], dtype='float16') + >>> v = paddle.randn([2, 2, 2, 2], dtype='float16') + + >>> # sin, cos: [1, seq_len, 1, head_dim] + >>> x = paddle.randn([1, 2, 1, 2], dtype='float16') + >>> y = paddle.randn([1, 2, 1, 2], dtype='float16') + >>> sin = paddle.sin(x) + >>> cos = paddle.cos(y) + + >>> # position_ids: [batch_size, seq_len] + >>> position_ids = paddle.randint(high=2, shape=[2, 2], dtype='int64') + + >>> # out_q, out_k, out_v: [batch_size, seq_len, num_heads, head_dim] + >>> out_q, out_k, out_v = fused_rotary_position_embedding(q, k, v, sin=sin, cos=cos, position_ids=position_ids, use_neox_rotary_style=False) + >>> print(out_q) + Tensor(shape=[2, 2, 2, 2], dtype=float16, place=Place(gpu:0), stop_gradient=True, + [[[[-0.54931641, 0.64990234], + [-1.08691406, 1.18261719]], + [[ 0.57812500, 0.11749268], + [-0.63281250, 0.15551758]]], + [[[-0.77050781, 0.07733154], + [-0.73730469, -0.16735840]], + [[ 0.07116699, -0.90966797], + [-0.03628540, -0.20202637]]]]) """ if in_dynamic_mode(): - return _C_ops.fused_rotary_position_embedding(q, k, v, sin, cos) + return _C_ops.fused_rotary_position_embedding( + q, k, v, sin, cos, position_ids, use_neox_rotary_style + ) raise RuntimeError( "This feature is currently supported only in dynamic mode and with CUDAPlace." diff --git a/test/legacy_test/test_fused_rotary_position_embedding.py b/test/legacy_test/test_fused_rotary_position_embedding.py index 9842fbf1f4ee8..de6355d56a5ee 100644 --- a/test/legacy_test/test_fused_rotary_position_embedding.py +++ b/test/legacy_test/test_fused_rotary_position_embedding.py @@ -41,6 +41,24 @@ def mult_qkv(value, cos_tensor, sin_tensor): return query +def mult_qkv_rotate_half(value, cos_tensor, sin_tensor): + rotate_half_q = paddle.reshape( + paddle.concat( + [ + -value[..., value.shape[-1] // 2 :], + value[..., : value.shape[-1] // 2], + ], + axis=-1, + ), + paddle.shape(value), + ) + query = paddle.add( + paddle.multiply(value, cos_tensor), + paddle.multiply(rotate_half_q, sin_tensor), + ) + return query + + def get_sin_cos_tensor(seq_len, head_dim, sign): pos_seq = paddle.arange(0, seq_len, 1, dtype="float32") indices = paddle.arange(0, head_dim, 2, dtype="float32") @@ -74,22 +92,38 @@ def get_sin_cos_tensor(seq_len, head_dim, sign): return tensor_sin, tensor_cos -def paddle_fused_rotary_position_embedding(init_q, init_k, init_v): +def paddle_fused_rotary_position_embedding( + init_q, init_k, init_v, position_ids=None, use_neox_rotary_style=True +): # permute q, k, v from [batch_size, seq_len, num_heads, head_dim] # to [batch_size, num_heads, seq_len, head_dim] q, k, v = deal_qkv(init_q, init_k, init_v) - sin_tensor, cos_tensor = get_sin_cos_tensor(q.shape[2], q.shape[3], -1) + sign = -1 if use_neox_rotary_style else 1 + sin_tensor, cos_tensor = get_sin_cos_tensor(q.shape[2], q.shape[3], sign) + + if position_ids is not None: + sin_tensor = sin_tensor.squeeze(axis=[0, 2]) # [seq_len, dim] + cos_tensor = cos_tensor.squeeze(axis=[0, 2]) # [seq_len, dim] + sin_tensor = sin_tensor[position_ids].unsqueeze( + 2 + ) # [bs, seq_len, 1, dim] + cos_tensor = cos_tensor[position_ids].unsqueeze( + 2 + ) # [bs, seq_len, 1, dim] - # permute sin, cos from [1, seq_len, 1, head_dim] - # to [1, 1, seq_len, head_dim] perm = [0, 2, 1, 3] sin_tensor = paddle.transpose(x=sin_tensor, perm=perm) cos_tensor = paddle.transpose(x=cos_tensor, perm=perm) - query = mult_qkv(q, cos_tensor, sin_tensor) - value = mult_qkv(v, cos_tensor, sin_tensor) - key = mult_qkv(k, cos_tensor, sin_tensor) + if use_neox_rotary_style: + query = mult_qkv(q, cos_tensor, sin_tensor) + value = mult_qkv(v, cos_tensor, sin_tensor) + key = mult_qkv(k, cos_tensor, sin_tensor) + else: + query = mult_qkv_rotate_half(q, cos_tensor, sin_tensor) + value = mult_qkv_rotate_half(v, cos_tensor, sin_tensor) + key = mult_qkv_rotate_half(k, cos_tensor, sin_tensor) # permute the result back to [batch_size, seq_len, num_heads, head_dim] r_query, r_key, r_value = deal_qkv(query, key, value) @@ -102,7 +136,7 @@ def paddle_fused_rotary_position_embedding(init_q, init_k, init_v): ) class TestFusedRotaryPositionEmbedding(unittest.TestCase): def setUp(self): - self.shape = [1, 8, 2, 16] + self.shape = [2, 8, 2, 16] self.dtype = 'float32' self.training = True self.seed = 1203 @@ -112,7 +146,14 @@ def get_paddle_tensor(self): tmp.stop_gradient = False return tmp - def get_forward_backward(self, rope_function, seed, flag=0): + def get_forward_backward( + self, + rope_function, + seed, + flag=False, + use_neox_rotary_style=True, + position_ids=None, + ): paddle.disable_static() paddle.seed(seed) fw = [] @@ -120,15 +161,45 @@ def get_forward_backward(self, rope_function, seed, flag=0): tensor_q = self.get_paddle_tensor() tensor_k = self.get_paddle_tensor() tensor_v = self.get_paddle_tensor() - if flag: - tensor_sin, tensor_cos = get_sin_cos_tensor( - tensor_q.shape[1], tensor_q.shape[3], 1 - ) - out_q, out_k, out_v = rope_function( - tensor_q, tensor_k, tensor_v, tensor_sin, tensor_cos - ) + if use_neox_rotary_style: + if flag: + tensor_sin, tensor_cos = get_sin_cos_tensor( + tensor_q.shape[1], tensor_q.shape[3], 1 + ) + out_q, out_k, out_v = rope_function( + tensor_q, + tensor_k, + tensor_v, + tensor_sin, + tensor_cos, + position_ids=position_ids, + ) + else: + out_q, out_k, out_v = rope_function( + tensor_q, tensor_k, tensor_v, position_ids=position_ids + ) else: - out_q, out_k, out_v = rope_function(tensor_q, tensor_k, tensor_v) + if flag: + tensor_sin, tensor_cos = get_sin_cos_tensor( + tensor_q.shape[1], tensor_q.shape[3], 1 + ) + out_q, out_k, out_v = rope_function( + tensor_q, + tensor_k, + tensor_v, + tensor_sin, + tensor_cos, + position_ids=position_ids, + use_neox_rotary_style=False, + ) + else: + out_q, out_k, out_v = rope_function( + tensor_q, + tensor_k, + tensor_v, + position_ids=position_ids, + use_neox_rotary_style=False, + ) fw.append(out_q) fw.append(out_k) @@ -166,7 +237,49 @@ def test_fused_rope_with_sin_cos(self): paddle_fused_rotary_position_embedding, seed=self.seed ) f_fw, f_bw = self.get_forward_backward( - fused_rotary_position_embedding, seed=self.seed, flag=1 + fused_rotary_position_embedding, seed=self.seed, flag=True + ) + for i in range(len(p_fw)): + np.testing.assert_allclose( + p_fw[i].numpy(), f_fw[i].numpy(), rtol=1e-05 + ) + np.testing.assert_allclose( + p_bw[i].numpy(), f_bw[i].numpy(), rtol=1e-05 + ) + + def test_fused_rope_rotate_half(self): + p_fw, p_bw = self.get_forward_backward( + paddle_fused_rotary_position_embedding, + seed=self.seed, + use_neox_rotary_style=False, + ) + f_fw, f_bw = self.get_forward_backward( + fused_rotary_position_embedding, + seed=self.seed, + use_neox_rotary_style=False, + ) + for i in range(len(p_fw)): + np.testing.assert_allclose( + p_fw[i].numpy(), f_fw[i].numpy(), rtol=1e-05 + ) + np.testing.assert_allclose( + p_bw[i].numpy(), f_bw[i].numpy(), rtol=1e-05 + ) + + def test_fused_rope_position_ids(self): + position_ids = paddle.to_tensor( + [[7, 5, 4, 6, 3, 1, 2, 0], [3, 1, 4, 0, 7, 6, 5, 2]] + ) + p_fw, p_bw = self.get_forward_backward( + paddle_fused_rotary_position_embedding, + seed=self.seed, + position_ids=position_ids, + ) + f_fw, f_bw = self.get_forward_backward( + fused_rotary_position_embedding, + seed=self.seed, + flag=True, + position_ids=position_ids, ) for i in range(len(p_fw)): np.testing.assert_allclose( From abf7af224903f75cfa6613e5efd9221789460333 Mon Sep 17 00:00:00 2001 From: WangZhen <23097963+0x45f@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:03:28 +0800 Subject: [PATCH 106/194] Fix ci build error (#56936) --- .../dialect/paddle_dialect/ir/CMakeLists.txt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index 83d38a676e5bd..cc8d1357bf070 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -88,6 +88,9 @@ add_custom_command( ${op_yaml_file4} VERBATIM) +set(api_gen_yaml_files + ${op_forward_yaml_file1},${op_forward_yaml_file2},${op_backward_yaml_file1},${op_backward_yaml_file2},${op_yaml_file3},${op_yaml_file4} +) set(api_gen_file ${PADDLE_SOURCE_DIR}/paddle/fluid/ir/dialect/op_generator/api_gen.py) set(api_header_file ${PD_DIALECT_BINARY_DIR}/pd_api.h) @@ -98,7 +101,7 @@ set(api_source_file_tmp ${api_source_file}.tmp) add_custom_command( OUTPUT ${api_header_file} ${api_source_file} COMMAND - ${PYTHON_EXECUTABLE} ${api_gen_file} --op_yaml_files ${op_yaml_files} + ${PYTHON_EXECUTABLE} ${api_gen_file} --op_yaml_files ${api_gen_yaml_files} --op_compat_yaml_file ${op_compat_yaml_file} --namespaces ${op_namespace} --api_def_h_file ${api_header_file_tmp} --api_def_cc_file ${api_source_file_tmp} @@ -129,9 +132,10 @@ set(python_c_source_file_tmp ${python_c_source_file}.tmp) add_custom_command( OUTPUT ${python_c_header_file} ${python_c_source_file} COMMAND - ${PYTHON_EXECUTABLE} ${python_c_gen_file} --op_yaml_files ${op_yaml_files} - --op_compat_yaml_file ${op_compat_yaml_file} --namespaces "paddle,pybind" - --python_c_def_h_file ${python_c_header_file_tmp} --python_c_def_cc_file + ${PYTHON_EXECUTABLE} ${python_c_gen_file} --op_yaml_files + ${api_gen_yaml_files} --op_compat_yaml_file ${op_compat_yaml_file} + --namespaces "paddle,pybind" --python_c_def_h_file + ${python_c_header_file_tmp} --python_c_def_cc_file ${python_c_source_file_tmp} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${python_c_header_file_tmp} ${python_c_header_file} @@ -159,9 +163,9 @@ set(ops_api_source_file_tmp ${ops_api_source_file}.tmp) add_custom_command( OUTPUT ${ops_api_source_file} COMMAND - ${PYTHON_EXECUTABLE} ${ops_api_gen_file} --op_yaml_files ${op_yaml_files} - --op_compat_yaml_file ${op_compat_yaml_file} --namespaces "paddle,pybind" - --ops_api_file ${ops_api_source_file_tmp} + ${PYTHON_EXECUTABLE} ${ops_api_gen_file} --op_yaml_files + ${api_gen_yaml_files} --op_compat_yaml_file ${op_compat_yaml_file} + --namespaces "paddle,pybind" --ops_api_file ${ops_api_source_file_tmp} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ops_api_source_file_tmp} ${ops_api_source_file} COMMENT "copy_if_different ${ops_api_source_file}" From 3130582075dabebf0b986ee375911858145efad5 Mon Sep 17 00:00:00 2001 From: xiaoye <50870160+xiaoyewww@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:15:06 +0800 Subject: [PATCH 107/194] [clang-tidy] No. 59,60 bugprone-unused-return-value, bugprone-suspicious-missing-comma (#56676) --- .clang-tidy | 4 ++-- paddle/fluid/distributed/fleet_executor/fleet_executor.cc | 2 +- .../ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc | 2 +- paddle/fluid/framework/operator.cc | 2 +- paddle/fluid/pybind/eager_legacy_op_function_generator.cc | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c0b30cf39be72..5a4baae37c421 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -32,7 +32,7 @@ bugprone-misplaced-widening-cast, -bugprone-string-literal-with-embedded-nul, -bugprone-suspicious-enum-usage, -bugprone-suspicious-memset-usage, --bugprone-suspicious-missing-comma, +bugprone-suspicious-missing-comma, -bugprone-suspicious-semicolon, -bugprone-suspicious-string-compare, -bugprone-terminating-continue, @@ -42,7 +42,7 @@ bugprone-misplaced-widening-cast, -bugprone-undelegated-constructor, bugprone-unhandled-self-assignment, bugprone-unused-raii, --bugprone-unused-return-value, +bugprone-unused-return-value, bugprone-use-after-move, -bugprone-virtual-near-miss, -clang-analyzer-apiModeling.StdCLibraryFunctions, diff --git a/paddle/fluid/distributed/fleet_executor/fleet_executor.cc b/paddle/fluid/distributed/fleet_executor/fleet_executor.cc index f2a9f9cc6bfd8..8daf0636ce890 100644 --- a/paddle/fluid/distributed/fleet_executor/fleet_executor.cc +++ b/paddle/fluid/distributed/fleet_executor/fleet_executor.cc @@ -190,7 +190,7 @@ void FleetExecutor::Init( framework::GetUnusedVars(program_desc.Block(0), ops, {}); for (auto& unique_op : ops) { - unique_op.release(); + [[maybe_unused]] auto released_op = unique_op.release(); } // NOTE: For inference, the vars in inference_root_scope_vars diff --git a/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc b/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc index b5d2255a4b908..745e0ffa9cff8 100644 --- a/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc @@ -492,7 +492,7 @@ void ComputePropagateScalesMkldnnPass::ApplyImpl(ir::Graph* graph) const { FusePassBase::Init(pattern_name, graph); const std::unordered_set scale_immutable_ops = { - "fused_transpose" + "fused_transpose", "transpose2", "reshape2", "pool2d", diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 0c03486fdd750..c095ba849a4f2 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2022,7 +2022,7 @@ void OperatorWithKernel::RunImpl(const Scope& scope, ExecutionContext(*this, exec_scope, *dev_ctx, *runtime_ctx)); } if (fallback_to_cpu) { - phi_kernel_.release(); + [[maybe_unused]] auto released_kernel = phi_kernel_.release(); } } diff --git a/paddle/fluid/pybind/eager_legacy_op_function_generator.cc b/paddle/fluid/pybind/eager_legacy_op_function_generator.cc index b1504ba8f88dd..e7c9c62e01661 100644 --- a/paddle/fluid/pybind/eager_legacy_op_function_generator.cc +++ b/paddle/fluid/pybind/eager_legacy_op_function_generator.cc @@ -488,11 +488,11 @@ int main(int argc, char* argv[]) { // NOLINT paddle::operators::RegisterCustomDeviceCommonKernel("fake_device"); #endif + const std::string str = "\"paddle/fluid/eager/api/generated/fluid_generated/"; std::vector headers{ "", "\"paddle/fluid/platform/enforce.h\"", - "\"paddle/fluid/eager/api/generated/fluid_generated/" - "dygraph_forward_api.h\"", + str + "dygraph_forward_api.h\"", "\"paddle/fluid/pybind/eager_utils.h\"", "\"paddle/fluid/platform/profiler/event_tracing.h\"", "\"paddle/fluid/pybind/exception.h\"", From 6dd9a024f710d957dd70f1f49e28b421ed08827a Mon Sep 17 00:00:00 2001 From: xiaoye <50870160+xiaoyewww@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:16:20 +0800 Subject: [PATCH 108/194] [clang-tidy] No. 57,58 cppcoreguidelines-explicit-virtual-functions clang-analyzer-core.NonNullParamChecker (#56649) --- .clang-tidy | 4 ++-- paddle/fluid/memory/allocation/naive_best_fit_allocator.cc | 3 +-- paddle/phi/kernels/cpu/rnn_functor.h | 7 +++++++ paddle/phi/kernels/cpu/rnn_kernel.cc | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5a4baae37c421..9721281764494 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -53,7 +53,7 @@ bugprone-use-after-move, -clang-analyzer-core.CallAndMessage, -clang-analyzer-core.DivideZero, -clang-analyzer-core.DynamicTypePropagation, --clang-analyzer-core.NonNullParamChecker, +clang-analyzer-core.NonNullParamChecker, -clang-analyzer-core.NonnilStringConstants, -clang-analyzer-core.NullDereference, -clang-analyzer-core.StackAddrEscapeBase, @@ -154,7 +154,7 @@ clang-analyzer-unix.Vfork, cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-avoid-goto, cppcoreguidelines-c-copy-assignment-signature, --cppcoreguidelines-explicit-virtual-functions, +cppcoreguidelines-explicit-virtual-functions, -cppcoreguidelines-init-variables, -cppcoreguidelines-narrowing-conversions, -cppcoreguidelines-no-malloc, diff --git a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc index 1493913f5b2a8..d39cb285517f2 100644 --- a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc @@ -386,8 +386,7 @@ void *Alloc(const platform::CUDAPinnedPlace &place, if (ptr == nullptr) { LOG(WARNING) << "cudaHostAlloc Cannot allocate " << size << " bytes in CUDAPinnedPlace"; - } - if (FLAGS_init_allocated_mem) { + } else if (FLAGS_init_allocated_mem) { memset(ptr, 0xEF, size); } return ptr; diff --git a/paddle/phi/kernels/cpu/rnn_functor.h b/paddle/phi/kernels/cpu/rnn_functor.h index d711998184d93..87b533799ba90 100644 --- a/paddle/phi/kernels/cpu/rnn_functor.h +++ b/paddle/phi/kernels/cpu/rnn_functor.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/generator.h" #include "paddle/phi/core/tensor_utils.h" @@ -344,6 +345,12 @@ void RnnFunc(const Context& dev_ctx, auto last_h_unbind = Unbind(*last_h); std::vector init_c_unbind, last_c_unbind; if (is_lstm(cell_type)) { + PADDLE_ENFORCE_NOT_NULL( + init_c, + paddle::platform::errors::InvalidArgument("init_c contains no data.")); + PADDLE_ENFORCE_NOT_NULL( + last_c, + paddle::platform::errors::InvalidArgument("last_c contains no data.")); init_c_unbind = Unbind(*init_c); last_c_unbind = Unbind(*last_c); } diff --git a/paddle/phi/kernels/cpu/rnn_kernel.cc b/paddle/phi/kernels/cpu/rnn_kernel.cc index b2e7dd19fafd8..a861f6696473e 100644 --- a/paddle/phi/kernels/cpu/rnn_kernel.cc +++ b/paddle/phi/kernels/cpu/rnn_kernel.cc @@ -706,7 +706,7 @@ struct SingleLayer : public Layer { DenseTensor* cell_value, DenseTensor* cell_act_value, const std::string& mode, - bool is_test) { + bool is_test) override { this->RunIter(dev_ctx, input, vec, @@ -745,7 +745,7 @@ struct BidirLayer : public Layer { DenseTensor* cell_value, DenseTensor* cell_act_value, const std::string& mode, - bool is_test) { + bool is_test) override { std::vector output_vec(2); DenseTensor forward_input_w, forward_cell_value, forward_cell_act_value; DenseTensor backward_input_w, backward_cell_value, backward_cell_act_value; From 5efaaaa3a041b46b5066c2df0e3997281ae32f51 Mon Sep 17 00:00:00 2001 From: jiangfan06 <117341294+MuShangCC@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:34:53 +0800 Subject: [PATCH 109/194] [XPU] Add element_mul_add_fuse_pass and elementwise_madd_xpu kernel (#56629) --- paddle/fluid/framework/ir/CMakeLists.txt | 2 + .../ir/xpu/elementwise_mul_add_fuse_pass.cc | 333 ++++++++++++++++++ .../inference/api/paddle_pass_builder.cc | 1 + paddle/phi/api/yaml/fused_ops.yaml | 9 + paddle/phi/backends/xpu/xpu2_op_list.cc | 4 + paddle/phi/infermeta/fusion.cc | 9 + paddle/phi/infermeta/fusion.h | 5 + .../kernels/fusion/xpu/addcmul_xpu_kernel.cc | 61 ++++ paddle/phi/kernels/xpu/concat_kernel.cc | 4 +- .../kernels/xpu/plugin/include/xpu/plugin.h | 3 + .../src/kernel/kunlun2cpp/fast_addcmul.xpu | 77 ++++ .../xpu/plugin/src/wrapper/fast_addcmul.cpp | 76 ++++ .../test_xpu_elementwise_mul_add_fuse_pass.py | 74 ++++ 13 files changed, 657 insertions(+), 1 deletion(-) create mode 100644 paddle/fluid/framework/ir/xpu/elementwise_mul_add_fuse_pass.cc create mode 100644 paddle/phi/kernels/fusion/xpu/addcmul_xpu_kernel.cc create mode 100644 paddle/phi/kernels/xpu/plugin/src/kernel/kunlun2cpp/fast_addcmul.xpu create mode 100644 paddle/phi/kernels/xpu/plugin/src/wrapper/fast_addcmul.cpp create mode 100644 test/ir/inference/test_xpu_elementwise_mul_add_fuse_pass.py diff --git a/paddle/fluid/framework/ir/CMakeLists.txt b/paddle/fluid/framework/ir/CMakeLists.txt index b1dafb0d3934d..f856eca2aa526 100755 --- a/paddle/fluid/framework/ir/CMakeLists.txt +++ b/paddle/fluid/framework/ir/CMakeLists.txt @@ -290,6 +290,8 @@ if(WITH_XPU) pass_library(fast_where_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS}) pass_library(fast_layernorm_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS}) + pass_library(elementwise_mul_add_fuse_pass inference DIR xpu DEPS + ${XPU_PASS_DEPS}) endif() cc_library( diff --git a/paddle/fluid/framework/ir/xpu/elementwise_mul_add_fuse_pass.cc b/paddle/fluid/framework/ir/xpu/elementwise_mul_add_fuse_pass.cc new file mode 100644 index 0000000000000..6057f200ac0c1 --- /dev/null +++ b/paddle/fluid/framework/ir/xpu/elementwise_mul_add_fuse_pass.cc @@ -0,0 +1,333 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include + +#include "glog/logging.h" + +#include "paddle/fluid/framework/ir/fuse_pass_base.h" +#include "paddle/fluid/framework/ir/graph_pattern_detector.h" +#include "paddle/fluid/framework/ir/pass.h" +#include "paddle/fluid/framework/ir/xpu/pass_utils.h" +#include "paddle/fluid/framework/ir/xpu/quant_utils.h" +#include "paddle/fluid/framework/op_version_registry.h" +#include "paddle/fluid/platform/enforce.h" + +namespace phi { +class DenseTensor; +} // namespace phi + +namespace paddle { +namespace framework { +class Scope; +} // namespace framework +} // namespace paddle + +namespace paddle { +namespace framework { +namespace ir { +namespace patterns { + +/* +fuse elementwise_mul + elementwise_add op to addcmul_xpu op +For example: +graph: + x y + \ / + \ / + elementwise_mul w + \ / + \ / + elementwise_add + | + | + output +------------------------------------------------------ +After the pass is applied: + x y w + \ | / + \ | / + addcmul_xpu + | + | + output +*/ +struct ElementwiseMulAddFusePass : public PatternBase { + ElementwiseMulAddFusePass(PDPattern* pattern, const std::string& name_scope); + // declare operator node's name + PATTERN_DECL_NODE(elementwise_mul); + PATTERN_DECL_NODE(elementwise_add); + // declare variable node's name + PATTERN_DECL_NODE(mul_x); + PATTERN_DECL_NODE(mul_y); + PATTERN_DECL_NODE(mul_out); + PATTERN_DECL_NODE(add_w); + PATTERN_DECL_NODE(add_out); +}; + +ElementwiseMulAddFusePass::ElementwiseMulAddFusePass( + PDPattern* pattern, const std::string& name_scope) + : PatternBase(pattern, name_scope, name_scope) { + auto elementwise_mul = + pattern->NewNode(elementwise_mul_repr())->assert_is_op("elementwise_mul"); + auto elementwise_add = + pattern->NewNode(elementwise_add_repr())->assert_is_op("elementwise_add"); + auto mul_x = pattern->NewNode(mul_x_repr()) + ->AsInput() + ->assert_is_op_input("elementwise_mul", "X"); + auto mul_y = pattern->NewNode(mul_y_repr()) + ->AsInput() + ->assert_is_op_input("elementwise_mul", "Y"); + auto mul_out = pattern->NewNode(mul_out_repr()) + ->AsOutput() + ->assert_is_op_output("elementwise_mul", "Out") + ->assert_is_op_input("elementwise_add", "X") + ->assert_has_n_outputs(1); + elementwise_mul->LinksFrom({mul_x, mul_y}).LinksTo({mul_out}); + auto add_w = pattern->NewNode(add_w_repr()) + ->AsInput() + ->assert_is_op_input("elementwise_add", "Y"); + auto add_out = pattern->NewNode(add_out_repr()) + ->AsOutput() + ->assert_is_op_output("elementwise_add", "Out"); + elementwise_add->LinksFrom({mul_out, add_w}).LinksTo({add_out}); +} + +/* +special case for addcmul_xpu op: +graph: + x y + \ / + \ / + elementwise_mul x + \ / + \ / + elementwise_add + | + | + output +------------------------------------------------------ +After the pass is applied: + x y + \ / + \ / + addcmul_xpu + | + | + output +*/ +struct ElementwiseMulAddFuseXYPattern : public PatternBase { + ElementwiseMulAddFuseXYPattern(PDPattern* pattern, + const std::string& name_scope); + // declare operator node's name + PATTERN_DECL_NODE(elementwise_mul); + PATTERN_DECL_NODE(elementwise_add); + // declare variable node's name + PATTERN_DECL_NODE(mul_x); + PATTERN_DECL_NODE(mul_y); + PATTERN_DECL_NODE(mul_out); + PATTERN_DECL_NODE(add_out); +}; + +ElementwiseMulAddFuseXYPattern::ElementwiseMulAddFuseXYPattern( + PDPattern* pattern, const std::string& name_scope) + : PatternBase(pattern, name_scope, name_scope) { + auto elementwise_mul = + pattern->NewNode(elementwise_mul_repr())->assert_is_op("elementwise_mul"); + auto elementwise_add = + pattern->NewNode(elementwise_add_repr())->assert_is_op("elementwise_add"); + auto mul_x = pattern->NewNode(mul_x_repr()) + ->AsInput() + ->assert_is_op_input("elementwise_mul", "X") + ->assert_is_op_input("elementwise_add", "Y"); + auto mul_y = pattern->NewNode(mul_y_repr()) + ->AsInput() + ->assert_is_op_input("elementwise_mul", "Y"); + auto mul_out = pattern->NewNode(mul_out_repr()) + ->AsOutput() + ->assert_is_op_output("elementwise_mul", "Out") + ->assert_is_op_input("elementwise_add", "X"); + elementwise_mul->LinksFrom({mul_x, mul_y}).LinksTo({mul_out}); + auto add_out = pattern->NewNode(add_out_repr()) + ->AsOutput() + ->assert_is_op_output("elementwise_add", "Out"); + elementwise_add->LinksFrom({mul_out, mul_x}).LinksTo({add_out}); +} +} // namespace patterns + +class ElementwiseMulAddFusePass : public FusePassBase { + protected: + void ApplyImpl(ir::Graph* graph) const override; + + private: + void FuseElementwiseMulAdd(ir::Graph* graph) const; + void FuseElementwiseMulAddWithOnlyXY(ir::Graph* graph) const; + + const std::string name_scope_{"elementwise_mul_add_fuse_pass"}; +}; + +void ElementwiseMulAddFusePass::ApplyImpl(ir::Graph* graph) const { + PADDLE_ENFORCE_NOT_NULL( + graph, platform::errors::PreconditionNotMet("graph should not be null.")); + Init(name_scope_, graph); + + FuseElementwiseMulAdd(graph); + FuseElementwiseMulAddWithOnlyXY(graph); +} + +void ElementwiseMulAddFusePass::FuseElementwiseMulAdd(ir::Graph* graph) const { + GraphPatternDetector gpd; + patterns::ElementwiseMulAddFusePass pattern(gpd.mutable_pattern(), + name_scope_); + + int found_subgraph_count = 0; + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, + Graph* graph) { + VLOG(4) << "handle ElementwiseMulAddFusePass"; + // declare operator node's name + GET_IR_NODE(elementwise_mul); + GET_IR_NODE(elementwise_add); + // declare variable node's name + GET_IR_NODE(mul_x); + GET_IR_NODE(mul_y); + GET_IR_NODE(mul_out); + GET_IR_NODE(add_w); + GET_IR_NODE(add_out); + + bool flag = true; + auto var_type = mul_x->Var()->GetDataType(); + if (var_type != proto::VarType::FP16 && var_type != proto::VarType::FP32) { + flag = false; + } + + auto x_shape = mul_x->Var()->GetShape(); + auto y_shape = mul_y->Var()->GetShape(); + auto w_shape = add_w->Var()->GetShape(); + if (x_shape.size() == y_shape.size() && x_shape.size() == w_shape.size()) { + for (size_t i = 0; i < x_shape.size(); ++i) { + if (x_shape[i] != y_shape[i] || x_shape[i] != w_shape[i] || + x_shape[i] == -1) { + flag = false; + } + } + } else { + flag = false; + } + + if (flag) { + auto* block = elementwise_mul->Op()->Block(); + + // delete useless node + std::unordered_set delete_nodes; + + // Generate addcmul_xpu op + framework::OpDesc fused_op_desc(block); + fused_op_desc.SetType("addcmul_xpu"); + fused_op_desc.SetInput("x", {mul_x->Name()}); + fused_op_desc.SetInput("y", {mul_y->Name()}); + fused_op_desc.SetInput("w", {add_w->Name()}); + fused_op_desc.SetOutput("out", {add_out->Name()}); + auto* fused_op = graph->CreateOpNode(&fused_op_desc); + IR_NODE_LINK_TO(mul_x, fused_op); + IR_NODE_LINK_TO(mul_y, fused_op); + IR_NODE_LINK_TO(add_w, fused_op); + IR_NODE_LINK_TO(fused_op, add_out); + delete_nodes.insert({elementwise_mul, elementwise_add, mul_out}); + GraphSafeRemoveNodes(graph, delete_nodes); + found_subgraph_count++; + } + }; + + gpd(graph, handler); + AddStatis(found_subgraph_count); +} + +void ElementwiseMulAddFusePass::FuseElementwiseMulAddWithOnlyXY( + ir::Graph* graph) const { + GraphPatternDetector gpd; + patterns::ElementwiseMulAddFuseXYPattern pattern(gpd.mutable_pattern(), + name_scope_); + + int found_subgraph_count = 0; + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, + Graph* graph) { + VLOG(4) << "handle ElementwiseMulAddFusePass"; + // declare operator node's name + GET_IR_NODE(elementwise_mul); + GET_IR_NODE(elementwise_add); + // declare variable node's name + GET_IR_NODE(mul_x); + GET_IR_NODE(mul_y); + GET_IR_NODE(mul_out); + GET_IR_NODE(add_out); + + bool flag = true; + auto var_type = mul_x->Var()->GetDataType(); + if (var_type != proto::VarType::FP16 && var_type != proto::VarType::FP32) { + flag = false; + } + + auto x_shape = mul_x->Var()->GetShape(); + auto y_shape = mul_y->Var()->GetShape(); + if (x_shape.size() == y_shape.size()) { + for (size_t i = 0; i < x_shape.size(); ++i) { + if (x_shape[i] != y_shape[i] || x_shape[i] == -1) { + flag = false; + } + } + } else { + flag = false; + } + + if (flag) { + auto* block = elementwise_mul->Op()->Block(); + + // delete useless node + std::unordered_set delete_nodes; + + // Generate addcmul_xpu op + framework::OpDesc fused_op_desc(block); + fused_op_desc.SetType("addcmul_xpu"); + fused_op_desc.SetInput("x", {mul_x->Name()}); + fused_op_desc.SetInput("y", {mul_y->Name()}); + fused_op_desc.SetInput("w", {mul_x->Name()}); + fused_op_desc.SetOutput("out", {add_out->Name()}); + auto* fused_op = graph->CreateOpNode(&fused_op_desc); + IR_NODE_LINK_TO(mul_x, fused_op); + IR_NODE_LINK_TO(mul_y, fused_op); + IR_NODE_LINK_TO(fused_op, add_out); + delete_nodes.insert({elementwise_mul, elementwise_add, mul_out}); + GraphSafeRemoveNodes(graph, delete_nodes); + found_subgraph_count++; + } + }; + + gpd(graph, handler); + AddStatis(found_subgraph_count); +} + +} // namespace ir +} // namespace framework +} // namespace paddle + +REGISTER_PASS(elementwise_mul_add_fuse_pass, + paddle::framework::ir::ElementwiseMulAddFusePass); + +REGISTER_PASS_CAPABILITY(elementwise_mul_add_fuse_pass) + .AddCombination( + paddle::framework::compatible::OpVersionComparatorCombination() + .GE("elementwise_add", 0) + .LE("elementwise_add", 1) + .GE("elementwise_mul", 0) + .LE("elementwise_mul", 1)); diff --git a/paddle/fluid/inference/api/paddle_pass_builder.cc b/paddle/fluid/inference/api/paddle_pass_builder.cc index 31d044f8c0b48..0c5423fe4d915 100644 --- a/paddle/fluid/inference/api/paddle_pass_builder.cc +++ b/paddle/fluid/inference/api/paddle_pass_builder.cc @@ -552,6 +552,7 @@ XpuPassStrategy::XpuPassStrategy() : PassStrategy({}) { "fast_layernorm_xpu_fuse_pass", "yolo_box_xpu_fuse_pass", "fast_where_xpu_fuse_pass", + "elementwise_mul_add_fuse_pass", "link_xpu_op_max_pass", "delete_isolated_node_pass", // "auto_mixed_precision_pass", diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index 9820fd6140557..53da3a659758c 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -23,6 +23,15 @@ func : add_layernorm_xpu data_type : x +- op : addcmul_xpu + args : (Tensor x, Tensor y, Tensor w) + output : Tensor(out) + infer_meta : + func : AddCMulXPUInferMeta + kernel : + func : addcmul_xpu + data_type : x + - op : conv1d_xpu args : (Tensor x, Tensor x_max, Tensor filter, Tensor filter_max, Tensor bias, Tensor branch, Tensor branch_max, int[] paddings, str padding_algorithm, int dilations, int strides, int groups, int act_type, float act_param) output : Tensor(out), Tensor(out_max) diff --git a/paddle/phi/backends/xpu/xpu2_op_list.cc b/paddle/phi/backends/xpu/xpu2_op_list.cc index 9154d1aa09246..d52769723e34f 100644 --- a/paddle/phi/backends/xpu/xpu2_op_list.cc +++ b/paddle/phi/backends/xpu/xpu2_op_list.cc @@ -36,6 +36,8 @@ XPUOpMap& get_kl2_ops() { {"adam_dense_param_sparse_grad", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, {"adagrad", XPUKernelSet({phi::DataType::FLOAT32})}, + {"addcmul_xpu", + XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})}, {"arg_max", XPUKernelSet({phi::DataType::INT32, phi::DataType::FLOAT32, @@ -161,6 +163,8 @@ XPUOpMap& get_kl2_ops() { XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16, phi::DataType::FLOAT64, + phi::DataType::BOOL, + phi::DataType::INT8, phi::DataType::INT64, phi::DataType::INT32})}, {"conv2d_grad", diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 993fb5d5887b8..1baf780f67610 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -821,6 +821,15 @@ void FastLayernormXPUInferMeta(const MetaTensor& x, out->set_layout(x.layout()); } +void AddCMulXPUInferMeta(const MetaTensor& x, + const MetaTensor& y, + const MetaTensor& w, + MetaTensor* out) { + out->set_dims(x.dims()); + out->set_dtype(x.dtype()); + out->set_layout(x.layout()); +} + void FusedScaleBiasReluConvBnstatsInferMeta( const MetaTensor& x, const MetaTensor& w, diff --git a/paddle/phi/infermeta/fusion.h b/paddle/phi/infermeta/fusion.h index 3d7ba19c4ec3f..ee41d55ca5524 100644 --- a/paddle/phi/infermeta/fusion.h +++ b/paddle/phi/infermeta/fusion.h @@ -201,6 +201,11 @@ void FastLayernormXPUInferMeta(const MetaTensor& x, float epsilon, MetaTensor* out); +void AddCMulXPUInferMeta(const MetaTensor& x, + const MetaTensor& y, + const MetaTensor& w, + MetaTensor* out); + void FusedScaleBiasReluConvBnstatsInferMeta( const MetaTensor& x, const MetaTensor& w, diff --git a/paddle/phi/kernels/fusion/xpu/addcmul_xpu_kernel.cc b/paddle/phi/kernels/fusion/xpu/addcmul_xpu_kernel.cc new file mode 100644 index 0000000000000..57c71bcd4bd7d --- /dev/null +++ b/paddle/phi/kernels/fusion/xpu/addcmul_xpu_kernel.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/backends/xpu/enforce_xpu.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { +namespace fusion { + +template +void AddCMulXPUKernel(const Context& ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& w, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + const auto* x_data = x.data(); + const auto* y_data = y.data(); + const auto* w_data = w.data(); + + auto* out_data = ctx.template Alloc(out); + +#ifdef PADDLE_WITH_XPU_PLUGIN + int r = xpu::plugin::fast_addcmul(ctx.x_context(), + reinterpret_cast(w_data), + reinterpret_cast(x_data), + reinterpret_cast(y_data), + reinterpret_cast(out_data), + x.numel()); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "fast_addcmul"); +#else + int r = xpu::addcmul(ctx.x_context(), + reinterpret_cast(w_data), + reinterpret_cast(x_data), + reinterpret_cast(y_data), + reinterpret_cast(out_data), + 1.0f, + x.numel()); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "addcmul"); +#endif +} +} // namespace fusion +} // namespace phi + +PD_REGISTER_KERNEL(addcmul_xpu, + XPU, + ALL_LAYOUT, + phi::fusion::AddCMulXPUKernel, + float, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/xpu/concat_kernel.cc b/paddle/phi/kernels/xpu/concat_kernel.cc index f1fac997061c5..5afdf2612981e 100644 --- a/paddle/phi/kernels/xpu/concat_kernel.cc +++ b/paddle/phi/kernels/xpu/concat_kernel.cc @@ -119,4 +119,6 @@ PD_REGISTER_KERNEL(concat, double, phi::dtype::float16, int64_t, - int) {} + int, + int8_t, + bool) {} diff --git a/paddle/phi/kernels/xpu/plugin/include/xpu/plugin.h b/paddle/phi/kernels/xpu/plugin/include/xpu/plugin.h index 1357ca43001c8..2038fef802393 100644 --- a/paddle/phi/kernels/xpu/plugin/include/xpu/plugin.h +++ b/paddle/phi/kernels/xpu/plugin/include/xpu/plugin.h @@ -114,6 +114,9 @@ DLL_EXPORT int fast_embedding(Context* ctx, int64_t ym, int64_t padding_idx, TID start_index = 0); +template +DLL_EXPORT int fast_addcmul( + Context* ctx, const T* w, const T* x, const T* y, T* z, int64_t len); } // namespace plugin } // namespace api diff --git a/paddle/phi/kernels/xpu/plugin/src/kernel/kunlun2cpp/fast_addcmul.xpu b/paddle/phi/kernels/xpu/plugin/src/kernel/kunlun2cpp/fast_addcmul.xpu new file mode 100644 index 0000000000000..dd2f2f6488a5b --- /dev/null +++ b/paddle/phi/kernels/xpu/plugin/src/kernel/kunlun2cpp/fast_addcmul.xpu @@ -0,0 +1,77 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +/* + * copyright (C) 2022 KUNLUNXIN, Inc + */ + +#include "xpu/kernel/cluster.h" +#include "xpu/kernel/cluster_partition.h" +#include "xpu/kernel/cluster_primitive.h" + +namespace xpu2 { +namespace plugin { + +template +static inline __device__ void primitive_addcmul(T* x, const T* y, int len) { + float32x16_t vx0; + float32x16_t vy0; + float32x16_t vx1; + float32x16_t vy1; + for (int i = 0; i < len; i += 32) { + vload2_lm(x + i, vx0, vx1); + vload2_lm(y + i, vy0, vy1); + vx0 = vvmac_float32x16(vx0, vy0, vx0); + vx1 = vvmac_float32x16(vx1, vy1, vx1); + vstore2_lm(x + i, vx0, vx1); + } + mfence_lm(); +} + +template +__global__ void fast_addcmul(const T* x, const T* y, T* z, int64_t len) { + int cid = core_id(); + const int ncores = core_num(); + int tid = cid * cluster_num() + cluster_id(); + int nthreads = cluster_num() * ncores; + const int buf_len = 512 / sizeof(T); + __simd__ float local_x_after_cast[buf_len]; + __simd__ float local_y_after_cast[buf_len]; + T* local_x = (T*)(local_x_after_cast); + T* local_y = (T*)(local_y_after_cast); + + int loop = 0; + for (int64_t i = tid * buf_len; i < len; i += nthreads * buf_len) { + int read_len = min(static_cast(buf_len), len - i); + GM2LM_ASYNC(x + i, local_x, read_len * sizeof(T)); + GM2LM(y + i, local_y, read_len * sizeof(T)); + primitive_addcmul(local_x, local_y, read_len); + LM2GM_ASYNC(local_x, z + i, read_len * sizeof(T)); + mfence_lm(); +#ifndef __XPU3__ + loop++; + if ((loop & 0xF) == 0) { + sync_all(); + } +#endif + } +} + +#define _XPU_DEF__FAST_ADDCMUL_(DTYPE) \ + template __global__ void fast_addcmul( \ + const DTYPE* x, const DTYPE* y, DTYPE* z, int64_t len); +_XPU_DEF__FAST_ADDCMUL_(float); +_XPU_DEF__FAST_ADDCMUL_(float16); + +} // namespace plugin +} // namespace xpu2 diff --git a/paddle/phi/kernels/xpu/plugin/src/wrapper/fast_addcmul.cpp b/paddle/phi/kernels/xpu/plugin/src/wrapper/fast_addcmul.cpp new file mode 100644 index 0000000000000..a333cbd7a43a2 --- /dev/null +++ b/paddle/phi/kernels/xpu/plugin/src/wrapper/fast_addcmul.cpp @@ -0,0 +1,76 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +/* + * copyright (C) 2022 KUNLUNXIN, Inc + */ + +#include "xpu/plugin.h" +#include "xpu/refactor/impl_public/wrapper_check.h" +#include "xpu/refactor/util/vector_util.h" + +namespace xpu2 { +namespace plugin { +template +__attribute__((global)) void fast_addcmul(const T* x, + const T* y, + T* z, + int64_t len); +} // namespace plugin +} // namespace xpu2 + +namespace baidu { +namespace xpu { +namespace api { +namespace plugin { + +template +static int xpu2_wrapper( + Context* ctx, const T* w, const T* x, const T* y, T* z, int64_t len) { + if (x == w) { + xpu2::plugin::fast_addcmul + <<ncluster(), 64, ctx->xpu_stream>>>(x, y, z, len); + } else { + return addcmul(ctx, w, x, y, z, 1.0f, len); + } + return SUCCESS; +} + +template +int fast_addcmul( + Context* ctx, const T* w, const T* x, const T* y, T* z, int64_t len) { + WRAPPER_CHECK_CTX(ctx); + WRAPPER_DUMP_FUNCTION_T1(ctx, "fast_mul_add", T); + WRAPPER_DUMP_PARAM4(ctx, w, x, y, z); + WRAPPER_DUMP_PARAM2(ctx, len, ctx->_l3_mgr.get_size()); + WRAPPER_DUMP(ctx); + WRAPPER_CHECK_4PTRS(ctx, T, len, w, x, y, z); + if (ctx->dev().type() == api::kXPU2) { + return xpu2_wrapper(ctx, w, x, y, z, len); + } + WRAPPER_UNIMPLEMENTED(ctx); +} + +template int fast_addcmul( + Context*, const float*, const float*, const float*, float*, int64_t); +template int fast_addcmul(Context*, + const float16*, + const float16*, + const float16*, + float16*, + int64_t); + +} // namespace plugin +} // namespace api +} // namespace xpu +} // namespace baidu diff --git a/test/ir/inference/test_xpu_elementwise_mul_add_fuse_pass.py b/test/ir/inference/test_xpu_elementwise_mul_add_fuse_pass.py new file mode 100644 index 0000000000000..48603acf90de9 --- /dev/null +++ b/test/ir/inference/test_xpu_elementwise_mul_add_fuse_pass.py @@ -0,0 +1,74 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest +from functools import partial + +import hypothesis.strategies as st +import numpy as np +from auto_scan_test import PassAutoScanTest +from program_config import OpConfig, ProgramConfig, TensorConfig + + +class TestGatherAddTransposePass(PassAutoScanTest): + def sample_predictor_configs(self, program_config): + config = self.create_inference_config(use_xpu=True) + yield config, ["addcmul_xpu"], (1e-3, 1e-3) + + def sample_program_config(self, draw): + x_shape = draw( + st.lists( + st.integers(min_value=1, max_value=4), min_size=3, max_size=4 + ) + ) + + def generate_data(shape): + return np.random.random(shape).astype(np.float32) + + mul_op = OpConfig( + "elementwise_mul", + inputs={"X": ["mul_x"], "Y": ["mul_y"]}, + outputs={"Out": ["mul_out"]}, + ) + + add_op = OpConfig( + "elementwise_add", + inputs={"X": ["mul_out"], "Y": ["add_w"]}, + outputs={"Out": ["add_out"]}, + ) + + ops = [mul_op, add_op] + + program_config = ProgramConfig( + ops=ops, + inputs={ + "mul_x": TensorConfig(data_gen=partial(generate_data, x_shape)), + "mul_y": TensorConfig(data_gen=partial(generate_data, x_shape)), + "add_w": TensorConfig(data_gen=partial(generate_data, x_shape)), + }, + weights={}, + outputs=["add_out"], + ) + return program_config + + def test(self): + self.run_and_statis( + quant=False, + max_examples=25, + passes=["elementwise_mul_add_fuse_pass"], + ) + + +if __name__ == "__main__": + unittest.main() From 41acf19b58669be116b67902ac3345b128511262 Mon Sep 17 00:00:00 2001 From: jiangfan06 <117341294+MuShangCC@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:35:27 +0800 Subject: [PATCH 110/194] Fix identity_op_clean_pass (#56753) --- paddle/fluid/framework/ir/identity_op_clean_pass.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/ir/identity_op_clean_pass.cc b/paddle/fluid/framework/ir/identity_op_clean_pass.cc index 255d1b06ea0f6..152ac0860366b 100644 --- a/paddle/fluid/framework/ir/identity_op_clean_pass.cc +++ b/paddle/fluid/framework/ir/identity_op_clean_pass.cc @@ -124,7 +124,14 @@ FindTwoCastOpPattern::FindTwoCastOpPattern(PDPattern* pattern, }); auto* cast_op_1 = pattern->NewNode(cast_op_1_repr())->assert_is_op("cast"); - auto* cast_op_1_out = pattern->NewNode(cast_op_1_out_repr())->assert_is_var(); + auto* cast_op_1_out = pattern->NewNode(cast_op_1_out_repr()) + ->assert_is_var() + ->assert_is_op_output("cast", "Out") + ->assert_more([](Node* x) { + const auto& var_type = x->Var()->GetDataType(); + return var_type != proto::VarType::INT32 && + var_type != proto::VarType::INT64; + }); auto* cast_op_2 = pattern->NewNode(cast_op_2_repr())->assert_is_op("cast"); auto* cast_op_2_out = pattern->NewNode(cast_op_2_out_repr())->assert_is_var(); From 42869ab66cba6dd87a783022669703b1297375b4 Mon Sep 17 00:00:00 2001 From: Charles-hit <56987902+Charles-hit@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:38:24 +0800 Subject: [PATCH 111/194] support more vjp code gen (#56890) --- .../fluid/operators/generator/tests_utils.py | 21 ++--- paddle/fluid/primitive/codegen/gen.py | 93 +++++++++++++++---- test/prim/new_ir_prim/test_vjp_prim.py | 22 +++-- 3 files changed, 99 insertions(+), 37 deletions(-) diff --git a/paddle/fluid/operators/generator/tests_utils.py b/paddle/fluid/operators/generator/tests_utils.py index 5f2e06eb89dab..cfcf674381605 100644 --- a/paddle/fluid/operators/generator/tests_utils.py +++ b/paddle/fluid/operators/generator/tests_utils.py @@ -86,19 +86,18 @@ def is_tensor_list(s): return s == 'Tensor[]' -def exist_mutable_attribute(attrs): - for attr in attrs: +def exist_mutable_attribute(attributes): + for attribute in attributes: if ( - attr['typename'] in ['Scalar', 'IntArray'] - and attr['support_tensor'] is True - ): + is_scalar(attribute['typename']) + or is_intarray(attribute['typename']) + ) and attribute.get('support_tensor', False): return True - else: - return False + else: + return False -def is_mutable_attribute(attr): +def is_mutable_attribute(attribute): return ( - attr['typename'] in ['Scalar', 'IntArray'] - and attr['support_tensor'] is True - ) + is_scalar(attribute['typename']) or is_intarray(attribute['typename']) + ) and attribute.get('support_tensor', False) diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 7b3072675ec9a..89ba4fe53cdf0 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -45,6 +45,22 @@ 'sum_grad', 'concat_grad', 'split_grad', + 'gelu_grad', + 'softmax_grad', + 'silu_grad', + 'multiply_grad', + 'subtract_grad', + 'erf_grad', + 'expand_grad', + 'exp_grad', + 'elementwise_pow_grad', + 'fused_softmax_mask_upper_triangle_grad', + 'matmul_grad', + 'pow_grad', + 'reshape_grad', + 'rsqrt_grad', + 'slice_grad', + 'transpose_grad', ] VJP_COMPS = ['divide_grad', 'sum_grad'] BACKENDS = [ @@ -68,6 +84,49 @@ 'sum_grad', 'concat_grad', 'split_grad', + 'gelu_grad', + 'softmax_grad', + 'silu_grad', + 'multiply_grad', + 'subtract_grad', + 'erf_grad', + 'expand_grad', + 'exp_grad', + 'multiply', + 'exp', + 'erf', + 'cast', + 'elementwise_pow_grad', + 'fused_softmax_mask_upper_triangle_grad', + 'matmul_grad', + 'pow_grad', + 'reshape_grad', + 'rsqrt_grad', + 'slice_grad', + 'transpose_grad', + 'subtract', + 'assign', + 'equal', + 'greater_equal', + 'greater_than', + 'less_equal', + 'less_than', + 'matmul', + 'max', + 'maximum', + 'minimum', + 'not_equal', + 'abs', + 'bitwise_and', + 'bitwise_not', + 'bitwise_or', + 'bitwise_xor', + 'floor', + 'gather_nd', + 'log', + 'roll', + 'scatter', + 'scatter_nd_add', ] @@ -157,21 +216,6 @@ def save(content: str, path: pathlib.Path): print(f"Generate source file {path}") -def filter_compat_info(items): - for item in items: - item['op'] = item['op'].split('(')[0].strip() - if 'backward' in item: - item_backwards = item['backward'].split(',') - for idx, item_backward in enumerate(item_backwards): - item_backward = item_backward.split('(')[0].strip() - item_backwards[idx] = item_backward - item['backward'] = ( - ','.join(item_backwards) - if len(item_backwards) > 0 - else item_backwards[0] - ) - - def to_compat_dict(items: List[Dict]) -> Dict[str, Dict]: compat_dict = {} for item in items: @@ -201,11 +245,28 @@ def get_inplace_api(apis): return inplace_apis +def filter_compat_info(items): + for item in items: + item['op'] = item['op'].split('(')[0].strip() + if 'backward' in item: + item_backwards = item['backward'].split(',') + for idx, item_backward in enumerate(item_backwards): + item_backward = item_backward.split('(')[0].strip() + item_backwards[idx] = item_backward + item['backward'] = ( + ','.join(item_backwards) + if len(item_backwards) > 0 + else item_backwards[0] + ) + + def extend_compat_info(apis, compats): for api in apis: attrs = api["attrs"] for attr in attrs: - if attr['typename'] in ["Scalar", "IntArray"]: + if op_gen_tests.is_scalar( + attr['typename'] + ) or op_gen_tests.is_intarray(attr['typename']): attr["support_tensor"] = False apis_dict = to_apis_dict(apis) for compat_item in compats: diff --git a/test/prim/new_ir_prim/test_vjp_prim.py b/test/prim/new_ir_prim/test_vjp_prim.py index 46ff348734db2..2a29ae9f69fc2 100644 --- a/test/prim/new_ir_prim/test_vjp_prim.py +++ b/test/prim/new_ir_prim/test_vjp_prim.py @@ -21,7 +21,7 @@ paddle.enable_static() -def get_ir_program_0(): +def get_ir_divide_program(): main_program, start_program = ( paddle.static.Program(), paddle.static.Program(), @@ -42,7 +42,7 @@ def get_ir_program_0(): return newir_program -def get_ir_program_1(): +def get_ir_sum_program(): main_program, start_program = ( paddle.static.Program(), paddle.static.Program(), @@ -61,8 +61,8 @@ def get_ir_program_1(): class TestVjpPrim(unittest.TestCase): def test_divide_grad_prim_case1(self): - newir_program = get_ir_program_0() - paddle.fluid.core._set_prim_backward_enabled(True) + newir_program = get_ir_divide_program() + paddle.framework.core._set_prim_backward_enabled(True) dout = newir_program.block().ops[-2].result(0) out_grads = [[dout]] stop_gradients = [[False], [False]] @@ -100,10 +100,11 @@ def test_divide_grad_prim_case1(self): ] for idx, op in enumerate(newir_program.block().ops): self.assertEqual(op.name(), all_op_names[idx]) + paddle.framework.core._set_prim_backward_enabled(False) def test_divide_grad_no_prim(self): - newir_program = get_ir_program_0() - paddle.fluid.core._set_prim_backward_enabled(False) + newir_program = get_ir_divide_program() + paddle.framework.core._set_prim_backward_enabled(False) dout = newir_program.block().ops[-2].result(0) out_grads = [[dout]] stop_gradients = [[False], [False]] @@ -120,8 +121,8 @@ def test_divide_grad_no_prim(self): self.assertEqual(len(newir_program.block().ops), 5) def test_sum_grad_prim(self): - newir_program = get_ir_program_1() - paddle.fluid.core._set_prim_backward_enabled(True) + newir_program = get_ir_sum_program() + paddle.framework.core._set_prim_backward_enabled(True) dout = newir_program.block().ops[-3].result(0) out_grads = [[dout]] stop_gradients = [[False], [True]] @@ -145,10 +146,11 @@ def test_sum_grad_prim(self): ] for idx, op in enumerate(newir_program.block().ops): self.assertEqual(op.name(), all_op_names[idx]) + paddle.framework.core._set_prim_backward_enabled(False) def test_sum_grad_no_prim(self): - newir_program = get_ir_program_1() - paddle.fluid.core._set_prim_backward_enabled(False) + newir_program = get_ir_sum_program() + paddle.framework.core._set_prim_backward_enabled(False) dout = newir_program.block().ops[-2].result(0) out_grads = [[dout]] stop_gradients = [[False], [True]] From 53d0869ffa0f47b600c525cbac97854e4c189bfa Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Tue, 5 Sep 2023 10:58:03 +0800 Subject: [PATCH 112/194] [xdoctest][task 224-225] reformat example code with google style in `python/paddle/distributed/fleet` (#56815) * [Doctest]fix No.224-225, test=docs_preview * fix the AttributeError --- python/paddle/distributed/fleet/model.py | 72 +++++++++----------- python/paddle/distributed/fleet/optimizer.py | 15 ++-- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/python/paddle/distributed/fleet/model.py b/python/paddle/distributed/fleet/model.py index e7cd0b33e9d16..c849a94dcea5d 100755 --- a/python/paddle/distributed/fleet/model.py +++ b/python/paddle/distributed/fleet/model.py @@ -41,44 +41,40 @@ def distributed_model(model): .. code-block:: python - import paddle - import paddle.nn as nn - from paddle.distributed import fleet - - class LinearNet(nn.Layer): - def __init__(self): - super().__init__() - self._linear1 = nn.Linear(10, 10) - self._linear2 = nn.Linear(10, 1) - - def forward(self, x): - return self._linear2(self._linear1(x)) - - # 1. initialize fleet environment - fleet.init(is_collective=True) - - # 2. create layer & optimizer - layer = LinearNet() - loss_fn = nn.MSELoss() - adam = paddle.optimizer.Adam( - learning_rate=0.001, parameters=layer.parameters()) - - # 3. get data_parallel model using fleet - adam = fleet.distributed_optimizer(adam) - dp_layer = fleet.distributed_model(layer) - - # 4. run layer - inputs = paddle.randn([10, 10], 'float32') - outputs = dp_layer(inputs) - labels = paddle.randn([10, 1], 'float32') - loss = loss_fn(outputs, labels) - - print("loss:", loss.numpy()) - - loss.backward() - - adam.step() - adam.clear_grad() + >>> import paddle + >>> import paddle.nn as nn + >>> from paddle.distributed import fleet + + >>> class LinearNet(nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self._linear1 = nn.Linear(10, 10) + ... self._linear2 = nn.Linear(10, 1) + ... def forward(self, x): + ... return self._linear2(self._linear1(x)) + + >>> # 1. initialize fleet environment + >>> fleet.init(is_collective=True) + + >>> # 2. create layer & optimizer + >>> layer = LinearNet() + >>> loss_fn = nn.MSELoss() + >>> adam = paddle.optimizer.Adam( + ... learning_rate=0.001, parameters=layer.parameters()) + + >>> # 3. get data_parallel model using fleet + >>> adam = fleet.distributed_optimizer(adam) + >>> dp_layer = fleet.distributed_model(layer) + + >>> # 4. run layer + >>> inputs = paddle.randn([10, 10], 'float32') + >>> outputs = dp_layer(inputs) + >>> labels = paddle.randn([10, 1], 'float32') + >>> loss = loss_fn(outputs, labels) + >>> print("loss:", loss.numpy()) + >>> loss.backward() + >>> adam.step() + >>> adam.clear_grad() """ diff --git a/python/paddle/distributed/fleet/optimizer.py b/python/paddle/distributed/fleet/optimizer.py index 9e693e670f3ff..85c27fd138ef3 100755 --- a/python/paddle/distributed/fleet/optimizer.py +++ b/python/paddle/distributed/fleet/optimizer.py @@ -37,12 +37,15 @@ def _dygraph_distributed_optimizer(optimizer, strategy=None): Fleet: instance of fleet. Examples: .. code-block:: python - import paddle - import paddle.distributed.fleet as fleet - fleet.init(is_collective=True) - strategy = fleet.DistributedStrategy() - optimizer = paddle.optimizer.SGD(learning_rate=0.001) - optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) + + >>> import paddle + >>> import paddle.distributed.fleet as fleet + >>> fleet.init(is_collective=True) + >>> strategy = fleet.DistributedStrategy() + >>> linear = paddle.nn.Linear(10, 10) + >>> optimizer = paddle.optimizer.SGD(learning_rate=0.001, parameters=linear.parameters()) + >>> optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) + """ fleet_env = fleet.fleet fleet_env.user_defined_optimizer = optimizer From bb078c1d4e48c1039c24a23bfe34245ecc8f5606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:00:14 +0800 Subject: [PATCH 113/194] [xdoctest] reformat example code with google style in No. 301 (#56599) * Update private_helper_function.py * xdoc * Apply suggestions from code review * format and skip a to_static case * add a missing space --------- Co-authored-by: SigureMo --- .../paddle/incubate/optimizer/modelaverage.py | 342 +++++++++--------- 1 file changed, 177 insertions(+), 165 deletions(-) diff --git a/python/paddle/incubate/optimizer/modelaverage.py b/python/paddle/incubate/optimizer/modelaverage.py index d0c73e306e78b..8e182ab9d8284 100644 --- a/python/paddle/incubate/optimizer/modelaverage.py +++ b/python/paddle/incubate/optimizer/modelaverage.py @@ -69,96 +69,100 @@ class ModelAverage(Optimizer): Examples: - .. code-block:: python - - import numpy as np - import paddle - import paddle.nn as nn - import paddle.optimizer as opt - - BATCH_SIZE = 16 - BATCH_NUM = 4 - EPOCH_NUM = 4 - - IMAGE_SIZE = 784 - CLASS_NUM = 10 - - # define a random dataset - class RandomDataset(paddle.io.Dataset): - def __init__(self, num_samples): - self.num_samples = num_samples - - def __getitem__(self, idx): - image = np.random.random([IMAGE_SIZE]).astype('float32') - label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') - return image, label - - def __len__(self): - return self.num_samples - - class LinearNet(nn.Layer): - def __init__(self): - super().__init__() - self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) - self.bias = self._linear.bias - - @paddle.jit.to_static - def forward(self, x): - return self._linear(x) - - def train(layer, loader, loss_fn, opt, model_average): - for epoch_id in range(EPOCH_NUM): - for batch_id, (image, label) in enumerate(loader()): - out = layer(image) - loss = loss_fn(out, label) - loss.backward() - opt.step() - model_average.step() - opt.clear_grad() - model_average.clear_grad() - print("Train Epoch {} batch {}: loss = {}, bias = {}".format( - epoch_id, batch_id, np.mean(loss.numpy()), layer.bias.numpy())) - def evaluate(layer, loader, loss_fn): - for batch_id, (image, label) in enumerate(loader()): - out = layer(image) - loss = loss_fn(out, label) - loss.backward() - print("Evaluate batch {}: loss = {}, bias = {}".format( - batch_id, np.mean(loss.numpy()), layer.bias.numpy())) - - # create network - layer = LinearNet() - loss_fn = nn.CrossEntropyLoss() - optimizer = opt.Momentum(learning_rate=0.2, momentum=0.1, parameters=layer.parameters()) - model_average = paddle.incubate.ModelAverage(0.15, - parameters=layer.parameters(), - min_average_window=2, - max_average_window=10) - - # create data loader - dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) - loader = paddle.io.DataLoader(dataset, - batch_size=BATCH_SIZE, - shuffle=True, - drop_last=True, - num_workers=2) - # create data loader - eval_loader = paddle.io.DataLoader(dataset, - batch_size=BATCH_SIZE, - shuffle=True, - drop_last=True, - num_workers=1) - - # train - train(layer, loader, loss_fn, optimizer, model_average) - - print("\nEvaluate With ModelAverage") - with model_average.apply(need_restore=False): - evaluate(layer, eval_loader, loss_fn) - - print("\nEvaluate With Restored Paramters") - model_average.restore() - evaluate(layer, eval_loader, loss_fn) + .. code-block:: python + + >>> # doctest: +SKIP("Cannot get source code by to_static in REPL") + >>> import numpy as np + >>> import paddle + >>> import paddle.nn as nn + >>> import paddle.optimizer as opt + + >>> BATCH_SIZE = 16 + >>> BATCH_NUM = 4 + >>> EPOCH_NUM = 4 + + >>> IMAGE_SIZE = 784 + >>> CLASS_NUM = 10 + + >>> # define a random dataset + >>> class RandomDataset(paddle.io.Dataset): + ... def __init__(self, num_samples): + ... self.num_samples = num_samples + ... def __getitem__(self, idx): + ... image = np.random.random([IMAGE_SIZE]).astype('float32') + ... label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64') + ... return image, label + ... def __len__(self): + ... return self.num_samples + ... + >>> class LinearNet(nn.Layer): + ... def __init__(self): + ... super().__init__() + ... self._linear = nn.Linear(IMAGE_SIZE, CLASS_NUM) + ... self.bias = self._linear.bias + ... + ... @paddle.jit.to_static + ... def forward(self, x): + ... return self._linear(x) + ... + >>> def train(layer, loader, loss_fn, opt, model_average): + ... for epoch_id in range(EPOCH_NUM): + ... for batch_id, (image, label) in enumerate(loader()): + ... out = layer(image) + ... loss = loss_fn(out, label) + ... loss.backward() + ... opt.step() + ... model_average.step() + ... opt.clear_grad() + ... model_average.clear_grad() + ... print("Train Epoch {} batch {}: loss = {}, bias = {}".format( + ... epoch_id, batch_id, np.mean(loss.numpy()), layer.bias.numpy())) + ... + >>> def evaluate(layer, loader, loss_fn): + ... for batch_id, (image, label) in enumerate(loader()): + ... out = layer(image) + ... loss = loss_fn(out, label) + ... loss.backward() + ... print("Evaluate batch {}: loss = {}, bias = {}".format( + ... batch_id, np.mean(loss.numpy()), layer.bias.numpy())) + ... + >>> # create network + >>> layer = LinearNet() + >>> loss_fn = nn.CrossEntropyLoss() + >>> optimizer = opt.Momentum(learning_rate=0.2, momentum=0.1, parameters=layer.parameters()) + >>> model_average = paddle.incubate.ModelAverage( + ... 0.15, + ... parameters=layer.parameters(), + ... min_average_window=2, + ... max_average_window=10 + ... ) + ... + >>> # create data loader + >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE) + >>> loader = paddle.io.DataLoader(dataset, + ... batch_size=BATCH_SIZE, + ... shuffle=True, + ... drop_last=True, + ... num_workers=2) + ... + >>> # create data loader + >>> eval_loader = paddle.io.DataLoader(dataset, + ... batch_size=BATCH_SIZE, + ... shuffle=True, + ... drop_last=True, + ... num_workers=1 + ... ) + ... + >>> # train + >>> train(layer, loader, loss_fn, optimizer, model_average) + + >>> print("\nEvaluate With ModelAverage") + >>> with model_average.apply(need_restore=False): + ... evaluate(layer, eval_loader, loss_fn) + + >>> print("\nEvaluate With Restored Paramters") + >>> model_average.restore() + >>> evaluate(layer, eval_loader, loss_fn) """ @@ -317,23 +321,25 @@ def minimize( .. code-block:: python - import paddle - inp = paddle.rand([1, 10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - loss.backward() - - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - sgd.minimize(loss) - - modelaverage = paddle.incubate.ModelAverage(0.15, - parameters=linear.parameters(), - min_average_window=2, - max_average_window=4) - modelaverage.minimize(loss) - sgd.clear_grad() - modelaverage.clear_grad() + >>> import paddle + >>> inp = paddle.rand([1, 10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> loss.backward() + + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + >>> sgd.minimize(loss) + + >>> modelaverage = paddle.incubate.ModelAverage( + ... 0.15, + ... parameters=linear.parameters(), + ... min_average_window=2, + ... max_average_window=4 + ... ) + >>> modelaverage.minimize(loss) + >>> sgd.clear_grad() + >>> modelaverage.clear_grad() """ if in_dynamic_mode(): @@ -352,21 +358,23 @@ def step(self): .. code-block:: python - import paddle - inp = paddle.rand([1, 10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - modelaverage = paddle.incubate.ModelAverage(0.15, - parameters=linear.parameters(), - min_average_window=2, - max_average_window=4) - loss.backward() - sgd.step() - modelaverage.step() - sgd.clear_grad() - modelaverage.clear_grad() + >>> import paddle + >>> inp = paddle.rand([1, 10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + >>> modelaverage = paddle.incubate.ModelAverage( + ... 0.15, + ... parameters=linear.parameters(), + ... min_average_window=2, + ... max_average_window=4 + ... ) + >>> loss.backward() + >>> sgd.step() + >>> modelaverage.step() + >>> sgd.clear_grad() + >>> modelaverage.clear_grad() """ params_grads = [] @@ -398,28 +406,30 @@ def apply(self, executor=None, need_restore=True): .. code-block:: python - import paddle - inp = paddle.rand([1, 10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - loss.backward() - - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - - modelaverage = paddle.incubate.ModelAverage(0.15, - parameters=linear.parameters(), - min_average_window=2, - max_average_window=4) - sgd.step() - modelaverage.step() - - with modelaverage.apply(): - for param in linear.parameters(): - print(param) - - for param in linear.parameters(): - print(param) + >>> import paddle + >>> inp = paddle.rand([1, 10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> loss.backward() + + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + + >>> modelaverage = paddle.incubate.ModelAverage( + ... 0.15, + ... parameters=linear.parameters(), + ... min_average_window=2, + ... max_average_window=4 + ... ) + >>> sgd.step() + >>> modelaverage.step() + + >>> with modelaverage.apply(): + ... for param in linear.parameters(): + ... print(param) + + >>> for param in linear.parameters(): + ... print(param) """ if in_dynamic_mode(): for param in self._parameter_list: @@ -472,33 +482,35 @@ def restore(self, executor=None): .. code-block:: python - import paddle - inp = paddle.rand([1, 10], dtype="float32") - linear = paddle.nn.Linear(10, 1) - out = linear(inp) - loss = paddle.mean(out) - loss.backward() + >>> import paddle + >>> inp = paddle.rand([1, 10], dtype="float32") + >>> linear = paddle.nn.Linear(10, 1) + >>> out = linear(inp) + >>> loss = paddle.mean(out) + >>> loss.backward() - sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) + >>> sgd = paddle.optimizer.SGD(learning_rate=0.1,parameters=linear.parameters()) - modelaverage = paddle.incubate.ModelAverage(0.15, - parameters=linear.parameters(), - min_average_window=2, - max_average_window=4) - sgd.step() - modelaverage.step() + >>> modelaverage = paddle.incubate.ModelAverage( + ... 0.15, + ... parameters=linear.parameters(), + ... min_average_window=2, + ... max_average_window=4 + ... ) + >>> sgd.step() + >>> modelaverage.step() - with modelaverage.apply(need_restore=False): - for param in linear.parameters(): - print(param) + >>> with modelaverage.apply(need_restore=False): + ... for param in linear.parameters(): + ... print(param) - for param in linear.parameters(): - print(param) + >>> for param in linear.parameters(): + ... print(param) - modelaverage.restore() + >>> modelaverage.restore() - for param in linear.parameters(): - print(param) + >>> for param in linear.parameters(): + ... print(param) """ if in_dynamic_mode(): for param in self._parameter_list: From 3e36271e62ae2251d536d48bab7c028590aeb2bd Mon Sep 17 00:00:00 2001 From: Ryan <44900829+DrRyanHuang@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:01:05 +0800 Subject: [PATCH 114/194] [xdoctest][task 125-137] reformat example code with google style in static/* (#56677) * add docx * fix conversation * fix * fix code style * fix code style * Thank SigureMo * finished? * add ... and fix line * add skip doctest --- python/paddle/static/nn/common.py | 720 ++++++++++++------------ python/paddle/static/nn/control_flow.py | 468 ++++++++------- 2 files changed, 613 insertions(+), 575 deletions(-) diff --git a/python/paddle/static/nn/common.py b/python/paddle/static/nn/common.py index 41ec17f0567a6..5994742bffbfe 100644 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -152,34 +152,32 @@ def fc( Examples: .. code-block:: python - import paddle - paddle.enable_static() - - # When input is a single tensor - x = paddle.static.data(name="x", shape=[1, 2, 2], dtype="float32") - # x: [[[0.1 0.2] - # [0.3 0.4]]] - out = paddle.static.nn.fc( - x=x, - size=1, - num_flatten_dims=2, - weight_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=0.5)), - bias_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=1.0))) - # out: [[[1.15] - # [1.35]]] - - # When input is multiple tensors - x0 = paddle.static.data(name="x0", shape=[1, 2, 2], dtype="float32") - # x0: [[[0.1 0.2] - # [0.3 0.4]]] - x1 = paddle.static.data(name="x1", shape=[1, 1, 3], dtype="float32") - # x1: [[[0.1 0.2 0.3]]] - out = paddle.static.nn.fc( - x=[x0, x1], - size=2, - weight_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=0.5)), - bias_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=1.0))) - # out: [[1.8 1.8]] + >>> import paddle + >>> paddle.enable_static() + + >>> # When input is a single tensor + >>> x = paddle.static.data(name="x", shape=[1, 2, 2], dtype="float32") + >>> out = paddle.static.nn.fc( + ... x=x, + ... size=1, + ... num_flatten_dims=2, + ... weight_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=0.5)), + ... bias_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=1.0))) + >>> print(out) + var fc_0.tmp_1 : LOD_TENSOR.shape(1, 2, 1).dtype(float32).stop_gradient(False) + + >>> # When input is multiple tensors + >>> x0 = paddle.static.data(name="x0", shape=[1, 2, 2], dtype="float32") + >>> x1 = paddle.static.data(name="x1", shape=[1, 1, 3], dtype="float32") + + >>> out = paddle.static.nn.fc( + ... x=[x0, x1], + ... size=2, + ... weight_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=0.5)), + ... bias_attr=paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(value=1.0))) + >>> print(out) + var fc_1.tmp_3 : LOD_TENSOR.shape(1, 2).dtype(float32).stop_gradient(False) + """ def fc_fluid( @@ -308,11 +306,11 @@ def instance_norm( .. code-block:: python - import paddle - paddle.enable_static() - x = paddle.static.data(name='x', shape=[3, 7, 3, 7], dtype='float32') - hidden1 = paddle.static.nn.fc(x, size=200) - hidden2 = paddle.static.nn.instance_norm(hidden1) + >>> import paddle + >>> paddle.enable_static() + >>> x = paddle.static.data(name='x', shape=[3, 7, 3, 7], dtype='float32') + >>> hidden1 = paddle.static.nn.fc(x, size=200) + >>> hidden2 = paddle.static.nn.instance_norm(hidden1) """ check_variable_and_dtype( input, @@ -412,18 +410,18 @@ def continuous_value_model(input, cvm, use_cvm=True): A Tensor with same type as input. Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle - input = paddle.static.data(name="input", shape=[64, 1], dtype="int64") - label = paddle.static.data(name="label", shape=[64, 1], dtype="int64") - w0 = paddle.full(shape=(100, 1), fill_value=2).astype(paddle.float32) - embed = paddle.nn.functional.embedding( - input, - w0) - ones = paddle.full_like(label, 1, dtype="int64") - show_clk = paddle.cast(paddle.concat([ones, label], axis=1), dtype='float32') - show_clk.stop_gradient = True - input_with_cvm = paddle.static.nn.continuous_value_model(embed, show_clk, True) + + >>> import paddle + + >>> paddle.enable_static() + >>> input = paddle.static.data(name="input", shape=[64, 1], dtype="int64") + >>> label = paddle.static.data(name="label", shape=[64, 1], dtype="int64") + >>> w0 = paddle.full(shape=(100, 1), fill_value=2).astype(paddle.float32) + >>> embed = paddle.nn.functional.embedding(input, w0) + >>> ones = paddle.full_like(label, 1, dtype="int64") + >>> show_clk = paddle.cast(paddle.concat([ones, label], axis=1), dtype='float32') + >>> show_clk.stop_gradient = True + >>> input_with_cvm = paddle.static.nn.continuous_value_model(embed[:, 0], show_clk, True) """ helper = LayerHelper('cvm', **locals()) out = helper.create_variable(dtype=input.dtype) @@ -515,11 +513,11 @@ def data_norm( .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.randn(shape=[32,100]) - hidden2 = paddle.static.nn.data_norm(input=x) + >>> x = paddle.randn(shape=[32, 100]) + >>> hidden2 = paddle.static.nn.data_norm(input=x) """ helper = LayerHelper('data_norm', **locals()) dtype = helper.input_dtype() @@ -694,12 +692,13 @@ def group_norm( Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - data = paddle.static.data(name='data', shape=[2, 8, 32, 32], dtype='float32') - x = paddle.static.nn.group_norm(input=data, groups=4) - print(x.shape) # [2, 8, 32, 32] + >>> data = paddle.static.data(name='data', shape=[2, 8, 32, 32], dtype='float32') + >>> x = paddle.static.nn.group_norm(input=data, groups=4) + >>> print(x.shape) + (2, 8, 32, 32) """ helper = LayerHelper('group_norm', **locals()) dtype = helper.input_dtype() @@ -888,12 +887,13 @@ def conv2d( Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - data = paddle.static.data(name='data', shape=[None, 3, 32, 32], dtype='float32') - conv2d = paddle.static.nn.conv2d(input=data, num_filters=2, filter_size=3, act="relu") - print(conv2d.shape) # [-1, 2, 30, 30] + >>> data = paddle.static.data(name='data', shape=[None, 3, 32, 32], dtype='float32') + >>> conv2d = paddle.static.nn.conv2d(input=data, num_filters=2, filter_size=3, act="relu") + >>> print(conv2d.shape) + (-1, 2, 30, 30) """ check_variable_and_dtype( @@ -1193,19 +1193,22 @@ def conv3d( Examples: .. code-block:: python - import paddle - import numpy as np - - paddle.enable_static() - data = paddle.static.data(name='data', shape=[None, 3, 12, 32, 32], dtype='float32') - param_attr = paddle.framework.ParamAttr(name='conv3d.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001) - res = paddle.static.nn.conv3d(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr) - place = paddle.CPUPlace() - exe = paddle.static.Executor(place) - exe.run(paddle.static.default_startup_program()) - x = np.random.rand(1, 3, 12, 32, 32).astype("float32") - output = exe.run(feed={"data": x}, fetch_list=[res]) - print(output) + >>> import paddle + >>> import numpy as np + + >>> np.random.seed(1107) + >>> paddle.seed(1107) + >>> paddle.enable_static() + >>> data = paddle.static.data(name='data', shape=[None, 3, 12, 32, 32], dtype='float32') + >>> param_attr = paddle.framework.ParamAttr(name='conv3d.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001) + >>> res = paddle.static.nn.conv3d(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr) + >>> place = paddle.CPUPlace() + >>> exe = paddle.static.Executor(place) + >>> exe.run(paddle.static.default_startup_program()) + >>> x = np.random.rand(1, 3, 12, 32, 32).astype("float32") + >>> output, = exe.run(feed={"data": x}, fetch_list=[res]) + >>> print(output.shape) + (1, 2, 10, 30, 30) """ l_type = 'conv3d' @@ -1536,14 +1539,15 @@ def conv2d_transpose( ShapeError: If the size of `output_size` is not equal to that of `stride`. Examples: - .. code-block:: python + .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - data = paddle.static.data(name='data', shape=[None, 3, 32, 32], dtype='float32') - conv2d_transpose = paddle.static.nn.conv2d_transpose(input=data, num_filters=2, filter_size=3) - print(conv2d_transpose.shape) # [-1, 2, 34, 34] + >>> data = paddle.static.data(name='data', shape=[None, 3, 32, 32], dtype='float32') + >>> conv2d_transpose = paddle.static.nn.conv2d_transpose(input=data, num_filters=2, filter_size=3) + >>> print(conv2d_transpose.shape) + (-1, 2, 34, 34) """ assert ( param_attr is not False @@ -1907,21 +1911,24 @@ def conv3d_transpose( variable storing transposed convolution and non-linearity activation result. Examples: - .. code-block:: python + .. code-block:: python - import paddle - import numpy as np - - paddle.enable_static() - data = paddle.static.data(name='data', shape=[None, 3, 12, 32, 32], dtype='float32') - param_attr = paddle.framework.ParamAttr(name='conv3d.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001) - res = paddle.static.nn.conv3d_transpose(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr) - place = paddle.CPUPlace() - exe = paddle.static.Executor(place) - exe.run(paddle.static.default_startup_program()) - x = np.random.rand(1, 3, 12, 32, 32).astype("float32") - output = exe.run(feed={"data": x}, fetch_list=[res]) - print(output) + >>> import paddle + >>> import numpy as np + + >>> paddle.seed(1107) + >>> np.random.seed(1107) + >>> paddle.enable_static() + >>> data = paddle.static.data(name='data', shape=[None, 3, 12, 32, 32], dtype='float32') + >>> param_attr = paddle.framework.ParamAttr(name='conv3d.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001) + >>> res = paddle.static.nn.conv3d_transpose(input=data, num_filters=2, filter_size=3, act="relu", param_attr=param_attr) + >>> place = paddle.CPUPlace() + >>> exe = paddle.static.Executor(place) + >>> exe.run(paddle.static.default_startup_program()) + >>> x = np.random.rand(1, 3, 12, 32, 32).astype("float32") + >>> output = exe.run(feed={"data": x}, fetch_list=[res.mean()]) + >>> print(output) + [array(0.5148856, dtype=float32)] """ assert ( param_attr is not False @@ -2226,28 +2233,26 @@ def deformable_conv( Examples: .. code-block:: python - #deformable conv v2: - - import paddle - paddle.enable_static() - - C_in, H_in, W_in = 3, 32, 32 - filter_size, deformable_groups = 3, 1 - data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') - offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - mask = paddle.static.data(name='mask', shape=[None, deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - out = paddle.static.layers.common.deformable_conv(input=data, offset=offset, mask=mask, - num_filters=2, filter_size=filter_size, padding=1, modulated=True) - - #deformable conv v1: - - import paddle - C_in, H_in, W_in = 3, 32, 32 - filter_size, deformable_groups = 3, 1 - data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') - offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - out = paddle.static.layers.common.deformable_conv(input=data, offset=offset, mask=None, - num_filters=2, filter_size=filter_size, padding=1, modulated=False) + >>> # deformable conv v2: + >>> import paddle + >>> paddle.enable_static() + + >>> C_in, H_in, W_in = 3, 32, 32 + >>> filter_size, deformable_groups = 3, 1 + >>> data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') + >>> offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> mask = paddle.static.data(name='mask', shape=[None, deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> out = paddle.static.nn.common.deformable_conv(input=data, offset=offset, mask=mask, + ... num_filters=2, filter_size=filter_size, padding=1, modulated=True) + + >>> # deformable conv v1: + >>> import paddle + >>> C_in, H_in, W_in = 3, 32, 32 + >>> filter_size, deformable_groups = 3, 1 + >>> data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') + >>> offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> out = paddle.static.nn.common.deformable_conv(input=data, offset=offset, mask=None, + ... num_filters=2, filter_size=filter_size, padding=1, modulated=False) """ check_variable_and_dtype( @@ -2471,30 +2476,28 @@ def deform_conv2d( Examples: .. code-block:: python - #deformable conv v2: - - import paddle - paddle.enable_static() - - C_in, H_in, W_in = 3, 32, 32 - filter_size, deformable_groups = 3, 1 - data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') - offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - mask = paddle.static.data(name='mask', shape=[None, deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - out = paddle.static.nn.deform_conv2d(x=data, offset=offset, mask=mask, - num_filters=2, filter_size=filter_size, padding=1) - - #deformable conv v1: - - import paddle - paddle.enable_static() - - C_in, H_in, W_in = 3, 32, 32 - filter_size, deformable_groups = 3, 1 - data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') - offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') - out = paddle.static.nn.deform_conv2d(x=data, offset=offset, mask=None, - num_filters=2, filter_size=filter_size, padding=1) + >>> # deformable conv v2: + >>> import paddle + >>> paddle.enable_static() + + >>> C_in, H_in, W_in = 3, 32, 32 + >>> filter_size, deformable_groups = 3, 1 + >>> data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') + >>> offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> mask = paddle.static.data(name='mask', shape=[None, deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> out = paddle.static.nn.deform_conv2d(x=data, offset=offset, mask=mask, + ... num_filters=2, filter_size=filter_size, padding=1) + + >>> # deformable conv v1: + >>> import paddle + >>> paddle.enable_static() + + >>> C_in, H_in, W_in = 3, 32, 32 + >>> filter_size, deformable_groups = 3, 1 + >>> data = paddle.static.data(name='data', shape=[None, C_in, H_in, W_in], dtype='float32') + >>> offset = paddle.static.data(name='offset', shape=[None, 2*deformable_groups*filter_size**2, H_in, W_in], dtype='float32') + >>> out = paddle.static.nn.deform_conv2d(x=data, offset=offset, mask=None, + ... num_filters=2, filter_size=filter_size, padding=1) """ if mask is None: @@ -2574,12 +2577,12 @@ def bilinear_tensor_product( Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data("t1", shape=[-1, 5], dtype="float32") - y = paddle.static.data("t2", shape=[-1, 4], dtype="float32") - tensor = paddle.static.nn.bilinear_tensor_product(x, y, size=1000) + >>> x = paddle.static.data("t1", shape=[-1, 5], dtype="float32") + >>> y = paddle.static.data("t2", shape=[-1, 4], dtype="float32") + >>> tensor = paddle.static.nn.bilinear_tensor_product(x, y, size=1000) """ helper = LayerHelper('bilinear_tensor_product', **locals()) @@ -2731,16 +2734,16 @@ def batch_norm( .. code-block:: python - import paddle + >>> import paddle - paddle.enable_static() - x = paddle.static.data(name='x', shape=[3, 7, 3, 7], dtype='float32') - hidden1 = paddle.static.nn.fc(x=x, size=200) - print(hidden1.shape) - # [3, 200] - hidden2 = paddle.static.nn.batch_norm(input=hidden1) - print(hidden2.shape) - # [3, 200] + >>> paddle.enable_static() + >>> x = paddle.static.data(name='x', shape=[3, 7, 3, 7], dtype='float32') + >>> hidden1 = paddle.static.nn.fc(x=x, size=200) + >>> print(hidden1.shape) + (3, 200) + >>> hidden2 = paddle.static.nn.batch_norm(input=hidden1) + >>> print(hidden2.shape) + (3, 200) """ assert ( bias_attr is not False @@ -2978,13 +2981,13 @@ def prelu(x, mode, param_attr=None, data_format="NCHW", name=None): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name="x", shape=[None,5,10,10], dtype="float32") - mode = 'channel' - output = paddle.static.nn.prelu( - x,mode,param_attr=paddle.ParamAttr(name='alpha')) + >>> x = paddle.static.data(name="x", shape=[None, 5, 10, 10], dtype="float32") + >>> mode = 'channel' + >>> output = paddle.static.nn.prelu( + ... x,mode,param_attr=paddle.ParamAttr(name='alpha')) """ check_variable_and_dtype(x, 'x', ['float16', 'float32', 'float64'], 'prelu') @@ -3170,103 +3173,111 @@ def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): Tensor|tuple(Tensor)|list[Tensor], The output ``out`` of the forward function ``func``. Examples: - .. code-block:: python :name: code-example1 - # example 1: - import paddle - import numpy as np - paddle.enable_static() - # Creates a forward function, Tensor can be input directly without - # being converted into numpy array. - def tanh(x): - return np.tanh(x) - # Skip x in backward function and return the gradient of x - # Tensor must be actively converted to numpy array, otherwise, - # operations such as +/- can't be used. - def tanh_grad(y, dy): - return np.array(dy) * (1 - np.square(np.array(y))) - # Creates a forward function for debugging running networks(print value) - def debug_func(x): - print(x) - def create_tmp_var(name, dtype, shape): - return paddle.static.default_main_program().current_block().create_var( - name=name, dtype=dtype, shape=shape) - def simple_net(img, label): - hidden = img - for idx in range(4): - hidden = paddle.static.nn.fc(hidden, size=200) - new_hidden = create_tmp_var(name='hidden_{}'.format(idx), - dtype=hidden.dtype, shape=hidden.shape) - # User-defined forward and backward - hidden = paddle.static.py_func(func=tanh, x=hidden, - out=new_hidden, backward_func=tanh_grad, - skip_vars_in_backward_input=hidden) - # User-defined debug functions that print out the input Tensor - paddle.static.py_func(func=debug_func, x=hidden, out=None) - prediction = paddle.static.nn.fc(hidden, size=10, activation='softmax') - ce_loss = paddle.nn.loss.CrossEntropyLoss() - return ce_loss(prediction, label) - x = paddle.static.data(name='x', shape=[1,4], dtype='float32') - y = paddle.static.data(name='y', shape=[1], dtype='int64') - res = simple_net(x, y) - exe = paddle.static.Executor(paddle.CPUPlace()) - exe.run(paddle.static.default_startup_program()) - input1 = np.random.random(size=[1,4]).astype('float32') - input2 = np.random.randint(1, 10, size=[1], dtype='int64') - out = exe.run(paddle.static.default_main_program(), - feed={'x':input1, 'y':input2}, - fetch_list=[res.name]) - print(out) + >>> import paddle + >>> import numpy as np + + >>> np.random.seed(1107) + >>> paddle.seed(1107) + + >>> paddle.enable_static() + >>> # Creates a forward function, Tensor can be input directly without + >>> # being converted into numpy array. + >>> def tanh(x): + ... return np.tanh(x) + + >>> # Skip x in backward function and return the gradient of x + >>> # Tensor must be actively converted to numpy array, otherwise, + >>> # operations such as +/- can't be used. + >>> def tanh_grad(y, dy): + ... return np.array(dy) * (1 - np.square(np.array(y))) + + >>> # Creates a forward function for debugging running networks(print value) + >>> def debug_func(x): + ... # print(x) + ... pass + >>> def create_tmp_var(name, dtype, shape): + ... return paddle.static.default_main_program().current_block().create_var( + ... name=name, dtype=dtype, shape=shape) + >>> def simple_net(img, label): + ... hidden = img + ... for idx in range(4): + ... hidden = paddle.static.nn.fc(hidden, size=200) + ... new_hidden = create_tmp_var(name='hidden_{}'.format(idx), + ... dtype=hidden.dtype, shape=hidden.shape) + ... # User-defined forward and backward + ... hidden = paddle.static.py_func(func=tanh, x=hidden, + ... out=new_hidden, backward_func=tanh_grad, + ... skip_vars_in_backward_input=hidden) + ... # User-defined debug functions that print out the input Tensor + ... paddle.static.py_func(func=debug_func, x=hidden, out=None) + ... prediction = paddle.static.nn.fc(hidden, size=10, activation='softmax') + ... ce_loss = paddle.nn.loss.CrossEntropyLoss() + ... return ce_loss(prediction, label) + >>> x = paddle.static.data(name='x', shape=[1,4], dtype='float32') + >>> y = paddle.static.data(name='y', shape=[1], dtype='int64') + >>> res = simple_net(x, y) + >>> exe = paddle.static.Executor(paddle.CPUPlace()) + >>> exe.run(paddle.static.default_startup_program()) + >>> input1 = np.random.random(size=[1,4]).astype('float32') + >>> input2 = np.random.randint(1, 10, size=[1], dtype='int64') + >>> out = exe.run(paddle.static.default_main_program(), + ... feed={'x':input1, 'y':input2}, + ... fetch_list=[res.name]) + >>> print(out[0].shape) + () .. code-block:: python :name: code-example2 - # example 2: - # This example shows how to turn Tensor into numpy array and - # use numpy API to register an Python OP - import paddle - import numpy as np - paddle.enable_static() - def element_wise_add(x, y): - # Tensor must be actively converted to numpy array, otherwise, - # numpy.shape can't be used. - x = np.array(x) - y = np.array(y) - if x.shape != y.shape: - raise AssertionError("the shape of inputs must be the same!") - result = np.zeros(x.shape, dtype='int32') - for i in range(len(x)): - for j in range(len(x[0])): - result[i][j] = x[i][j] + y[i][j] - return result - def create_tmp_var(name, dtype, shape): - return paddle.static.default_main_program().current_block().create_var( - name=name, dtype=dtype, shape=shape) - def py_func_demo(): - start_program = paddle.static.default_startup_program() - main_program = paddle.static.default_main_program() - # Input of the forward function - x = paddle.static.data(name='x', shape=[2,3], dtype='int32') - y = paddle.static.data(name='y', shape=[2,3], dtype='int32') - # Output of the forward function, name/dtype/shape must be specified - output = create_tmp_var('output','int32', [3,1]) - # Multiple Tensor should be passed in the form of tuple(Tensor) or list[Tensor] - paddle.static.py_func(func=element_wise_add, x=[x,y], out=output) - exe=paddle.static.Executor(paddle.CPUPlace()) - exe.run(start_program) - # Feed numpy array to main_program - input1 = np.random.randint(1, 10, size=[2,3], dtype='int32') - input2 = np.random.randint(1, 10, size=[2,3], dtype='int32') - out = exe.run(main_program, - feed={'x':input1, 'y':input2}, - fetch_list=[output.name]) - print("{0} + {1} = {2}".format(input1, input2, out)) - py_func_demo() - # Reference output: - # [[5, 9, 9] + [[7, 8, 4] = [array([[12, 17, 13] - # [7, 5, 2]] [1, 3, 3]] [8, 8, 5]], dtype=int32)] + >>> # This example shows how to turn Tensor into numpy array and + >>> # use numpy API to register an Python OP + >>> import paddle + >>> import numpy as np + + >>> np.random.seed(1107) + >>> paddle.seed(1107) + + >>> paddle.enable_static() + >>> def element_wise_add(x, y): + ... # Tensor must be actively converted to numpy array, otherwise, + ... # numpy.shape can't be used. + ... x = np.array(x) + ... y = np.array(y) + ... if x.shape != y.shape: + ... raise AssertionError("the shape of inputs must be the same!") + ... result = np.zeros(x.shape, dtype='int32') + ... for i in range(len(x)): + ... for j in range(len(x[0])): + ... result[i][j] = x[i][j] + y[i][j] + ... return result + >>> def create_tmp_var(name, dtype, shape): + ... return paddle.static.default_main_program().current_block().create_var( + ... name=name, dtype=dtype, shape=shape) + >>> def py_func_demo(): + ... start_program = paddle.static.default_startup_program() + ... main_program = paddle.static.default_main_program() + ... # Input of the forward function + ... x = paddle.static.data(name='x', shape=[2, 3], dtype='int32') + ... y = paddle.static.data(name='y', shape=[2, 3], dtype='int32') + ... # Output of the forward function, name/dtype/shape must be specified + ... output = create_tmp_var('output','int32', [3, 1]) + ... # Multiple Tensor should be passed in the form of tuple(Tensor) or list[Tensor] + ... paddle.static.py_func(func=element_wise_add, x=[x, y], out=output) + ... exe=paddle.static.Executor(paddle.CPUPlace()) + ... exe.run(start_program) + ... # Feed numpy array to main_program + ... input1 = np.random.randint(1, 10, size=[2, 3], dtype='int32') + ... input2 = np.random.randint(1, 10, size=[2, 3], dtype='int32') + ... out = exe.run(main_program, + ... feed={'x':input1, 'y':input2}, + ... fetch_list=[output.name]) + ... print("{0} + {1} = {2}".format(input1, input2, out)) + >>> py_func_demo() + >>> # [[1 5 4] + [[3 7 7] = [array([[ 4, 12, 11] + >>> # [9 4 8]] [2 3 9]] [11, 7, 17]], dtype=int32)] """ helper = LayerHelper('py_func', **locals()) check_type(x, 'X', (list, tuple, Variable, type(None)), 'py_func') @@ -3352,17 +3363,18 @@ def row_conv(input, future_context_size, param_attr=None, act=None): Examples: - .. code-block:: python - - # for LodTensor inputs - import paddle - paddle.enable_static() - x = paddle.static.data(name='x', shape=[9, 16], - dtype='float32', lod_level=1) - out_x = paddle.static.nn.row_conv(input=x, future_context_size=2) - # for Tensor inputs - y = paddle.static.data(name='y', shape=[9, 4, 16], dtype='float32') - out_y = paddle.static.nn.row_conv(input=y, future_context_size=2) + .. code-block:: python + + >>> # for LodTensor inputs + >>> import paddle + >>> paddle.enable_static() + >>> x = paddle.static.data(name='x', shape=[9, 16], + ... dtype='float32', lod_level=1) + >>> out_x = paddle.static.nn.row_conv(input=x, future_context_size=2) + + >>> # for Tensor inputs + >>> y = paddle.static.data(name='y', shape=[9, 4, 16], dtype='float32') + >>> out_y = paddle.static.nn.row_conv(input=y, future_context_size=2) """ helper = LayerHelper('row_conv', **locals()) check_variable_and_dtype(input, 'input', ['float32'], 'row_conv') @@ -3443,12 +3455,13 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - paddle.enable_static() - weight = paddle.static.data(name='weight', shape=[2, 8, 32, 32], dtype='float32') - x = paddle.static.nn.spectral_norm(weight=weight, dim=1, power_iters=2) - print(x.shape) # [2, 8, 32, 32] + >>> paddle.enable_static() + >>> weight = paddle.static.data(name='weight', shape=[2, 8, 32, 32], dtype='float32') + >>> x = paddle.static.nn.spectral_norm(weight=weight, dim=1, power_iters=2) + >>> print(x.shape) + (2, 8, 32, 32) """ helper = LayerHelper('spectral_norm', **locals()) check_variable_and_dtype( @@ -3584,11 +3597,12 @@ def layer_norm( .. code-block:: python - import paddle - paddle.enable_static() - x = paddle.static.data(name='x', shape=[8, 32, 32], dtype='float32') - output = paddle.static.nn.layer_norm(input=x, begin_norm_axis=1) - print(output.shape) # [8, 32, 32] + >>> import paddle + >>> paddle.enable_static() + >>> x = paddle.static.data(name='x', shape=[8, 32, 32], dtype='float32') + >>> output = paddle.static.nn.layer_norm(input=x, begin_norm_axis=1) + >>> print(output.shape) + (8, 32, 32) """ assert ( in_dygraph_mode() is not True @@ -3744,37 +3758,29 @@ def embedding( Static Examples: .. code-block:: python - import paddle - import numpy as np - paddle.enable_static() - - x = paddle.static.data(name="x", shape = [2, 4], dtype=np.int64) - output = paddle.static.nn.embedding(x, (10, 3), - param_attr=paddle.nn.initializer.Constant(value=1.0)) - m_output=paddle.mean(output) - place = paddle.CPUPlace() - exe = paddle.static.Executor(place) - exe.run(paddle.static.default_startup_program()) - - x = np.array([[7, 2, 4, 5],[4, 3, 2, 9]], dtype=np.int64) - - # x is a Numpy. - # x.data = [[7, 2, 4, 5], [4, 3, 2, 9]] - # x.shape = [2, 4] - - out, = exe.run(paddle.static.default_main_program(), feed={'x':x}, fetch_list=[output]) - - # out is a Numpy. - # out.data = [[1., 1., 1.], - # [1., 1., 1.], - # [1., 1., 1.], - # [1., 1., 1.]], - # - # [[1., 1., 1.], - # [1., 1., 1.], - # [1., 1., 1.], - # [0., 0., 0.]]] - # out.shape = [2, 4, 3] + >>> import paddle + >>> import numpy as np + >>> paddle.enable_static() + + >>> x = paddle.static.data(name="x", shape = [2, 4], dtype=np.int64) + >>> output = paddle.static.nn.embedding(x, (10, 3), + ... param_attr=paddle.nn.initializer.Constant(value=1.0)) + >>> m_output=paddle.mean(output) + >>> place = paddle.CPUPlace() + >>> exe = paddle.static.Executor(place) + >>> exe.run(paddle.static.default_startup_program()) + + >>> x = np.array([[7, 2, 4, 5],[4, 3, 2, 9]], dtype=np.int64) + >>> out, = exe.run(paddle.static.default_main_program(), feed={'x':x}, fetch_list=[output]) + >>> print(out) + [[[1. 1. 1.] + [1. 1. 1.] + [1. 1. 1.] + [1. 1. 1.]] + [[1. 1. 1.] + [1. 1. 1.] + [1. 1. 1.] + [1. 1. 1.]]] """ helper = LayerHelper('embedding', **locals()) @@ -3912,24 +3918,24 @@ def sparse_embedding( Examples: .. code-block:: python - import paddle + >>> import paddle - paddle.enable_static() - sparse_feature_dim = 1024 - embedding_size = 64 + >>> paddle.enable_static() + >>> sparse_feature_dim = 1024 + >>> embedding_size = 64 - # Only when the feature appear more than 10 times or more will be participated in the training. - entry = paddle.distributed.CountFilterEntry(10) + >>> # Only when the feature appear more than 10 times or more will be participated in the training. + >>> entry = paddle.distributed.CountFilterEntry(10) - input = paddle.static.data(name='ins', shape=[1], dtype='int64') + >>> input = paddle.static.data(name='ins', shape=[1], dtype='int64') - emb = paddle.static.nn.sparse_embedding( - input=input, - size=[sparse_feature_dim, embedding_size], - is_test=False, - entry=entry, - param_attr=paddle.ParamAttr(name="SparseFeatFactors", - initializer=paddle.nn.initializer.Uniform())) + >>> emb = paddle.static.nn.sparse_embedding( + ... input=input, + ... size=[sparse_feature_dim, embedding_size], + ... is_test=False, + ... entry=entry, + ... param_attr=paddle.ParamAttr(name="SparseFeatFactors", + ... initializer=paddle.nn.initializer.Uniform())) """ @@ -4060,49 +4066,49 @@ class ExponentialMovingAverage: .. code-block:: python - import numpy - import paddle - import paddle.static as static - from paddle.static import ExponentialMovingAverage - - paddle.enable_static() - - data = static.data(name='x', shape=[-1, 5], dtype='float32') - hidden = static.nn.fc(x=data, size=10) - cost = paddle.mean(hidden) - - test_program = static.default_main_program().clone(for_test=True) - optimizer = paddle.optimizer.Adam(learning_rate=0.001) - optimizer.minimize(cost) - - ema = ExponentialMovingAverage(0.999) - ema.update() - - place = paddle.CPUPlace() - exe = static.Executor(place) - exe.run(static.default_startup_program()) - - for pass_id in range(3): - for batch_id in range(6): - data = numpy.random.random(size=(10, 5)).astype('float32') - exe.run(program=static.default_main_program(), - feed={'x': data}, - fetch_list=[cost.name]) - - # usage 1 - with ema.apply(exe): - data = numpy.random.random(size=(10, 5)).astype('float32') - exe.run(program=test_program, - feed={'x': data}, - fetch_list=[hidden.name]) - - # usage 2 - with ema.apply(exe, need_restore=False): - data = numpy.random.random(size=(10, 5)).astype('float32') - exe.run(program=test_program, - feed={'x': data}, - fetch_list=[hidden.name]) - ema.restore(exe) + >>> import numpy + >>> import paddle + >>> import paddle.static as static + >>> from paddle.static import ExponentialMovingAverage + + >>> paddle.enable_static() + + >>> data = static.data(name='x', shape=[-1, 5], dtype='float32') + >>> hidden = static.nn.fc(x=data, size=10) + >>> cost = paddle.mean(hidden) + + >>> test_program = static.default_main_program().clone(for_test=True) + >>> optimizer = paddle.optimizer.Adam(learning_rate=0.001) + >>> optimizer.minimize(cost) + + >>> ema = ExponentialMovingAverage(0.999) + >>> ema.update() + + >>> place = paddle.CPUPlace() + >>> exe = static.Executor(place) + >>> exe.run(static.default_startup_program()) + + >>> for pass_id in range(3): + ... for batch_id in range(6): + ... data = numpy.random.random(size=(10, 5)).astype('float32') + ... exe.run(program=static.default_main_program(), + ... feed={'x': data}, + ... fetch_list=[cost.name]) + + ... # usage 1 + ... with ema.apply(exe): + ... data = numpy.random.random(size=(10, 5)).astype('float32') + ... exe.run(program=test_program, + ... feed={'x': data}, + ... fetch_list=[hidden.name]) + + ... # usage 2 + ... with ema.apply(exe, need_restore=False): + ... data = numpy.random.random(size=(10, 5)).astype('float32') + ... exe.run(program=test_program, + ... feed={'x': data}, + ... fetch_list=[hidden.name]) + ... ema.restore(exe) """ diff --git a/python/paddle/static/nn/control_flow.py b/python/paddle/static/nn/control_flow.py index 08b5d962abff7..39a95f42a7018 100644 --- a/python/paddle/static/nn/control_flow.py +++ b/python/paddle/static/nn/control_flow.py @@ -62,29 +62,29 @@ def Assert(cond, data=None, summarize=20, name=None): Examples: .. code-block:: python - import paddle - from paddle.static.nn.control_flow import Assert - - paddle.enable_static() - x = paddle.full([2, 3], 2.0, 'float32') - condition = paddle.max(x) < 1.0 # False - Assert(condition, [x], 10, "example_assert_layer") - - exe = paddle.static.Executor() - try: - exe.run(paddle.static.default_main_program()) - # Print x and throws ValueError - # Example printed message for x: - # - # Variable: fill_constant_0.tmp_0 - # - lod: {} - # - place: CPUPlace() - # - shape: [2, 3] - # - layout: NCHW - # - dtype: float - # - data: [2 2 2 2 2 2] - except ValueError as e: - print("Assert Exception Example") + >>> import paddle + >>> from paddle.static.nn.control_flow import Assert + + >>> paddle.enable_static() + >>> x = paddle.full([2, 3], 2.0, 'float32') + >>> condition = paddle.max(x) < 1.0 # False + >>> Assert(condition, [x], 10, "example_assert_layer") + + >>> exe = paddle.static.Executor() + >>> try: + ... exe.run(paddle.static.default_main_program()) + ... # Print x and throws ValueError + ... # Example printed message for x: + ... # + ... # Variable: fill_constant_0.tmp_0 + ... # - lod: {} + ... # - place: CPUPlace() + ... # - shape: [2, 3] + ... # - layout: NCHW + ... # - dtype: float + ... # - data: [2 2 2 2 2 2] + ... except ValueError as e: + ... print("Assert Exception Example") ''' check_variable_and_dtype( @@ -165,17 +165,21 @@ class ConditionalBlock: Examples: .. code-block:: python - import paddle - import paddle.fluid as fluid - cond = paddle.less_than(x=label, y=limit) - true_image, false_image = layers.split_lod_tensor( - input=image, mask=cond) - true_cond = layers.ConditionalBlock([true_image]) - - with true_cond.block(): - ... - with false_cond.block(): - ... + >>> import paddle + >>> from paddle.static.nn.control_flow import ConditionalBlock + + >>> label = paddle.rand([1]) + >>> limit = paddle.ones([1]) * 0.5 + >>> cond = paddle.less_than(x=label, y=limit) + >>> image = paddle.ones([1]) + + >>> true_image = image[cond] + >>> true_cond = ConditionalBlock([true_image]) + + >>> with true_cond.block(): + ... pass + >>> with false_cond.block(): + ... pass ''' def __init__(self, inputs, is_scalar_condition=False, name=None): @@ -425,59 +429,61 @@ class While: is_test(bool, optional): A flag indicating whether execution is in test phase. Default value is False. name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` . - Examples 1: - .. code-block:: python - - import paddle - import numpy as np - - paddle.enable_static() - - i = paddle.full(shape=[1], dtype='int64', fill_value=0) # loop counter - - loop_len = paddle.full(shape=[1],dtype='int64', fill_value=10) # loop length - - cond = paddle.less_than(x=i, y=loop_len) - while_op = paddle.static.nn.control_flow.While(cond=cond) - with while_op.block(): - i = paddle.increment(x=i, value=1) - paddle.assign(paddle.less_than(x=i, y=loop_len), output=cond) - - exe = paddle.static.Executor(paddle.CPUPlace()) - exe.run(paddle.static.default_startup_program()) + Examples: + .. code-block:: python + :name: example-1 - res = exe.run(paddle.static.default_main_program(), feed={}, fetch_list=[i]) - print(res) # [array([10])] + >>> import paddle + >>> import numpy as np + >>> paddle.enable_static() - Examples 2: - .. code-block:: python + >>> i = paddle.full(shape=[1], dtype='int64', fill_value=0) # loop counter - import paddle - import numpy as np + >>> loop_len = paddle.full(shape=[1],dtype='int64', fill_value=10) # loop length - paddle.enable_static() + >>> cond = paddle.less_than(x=i, y=loop_len) + >>> while_op = paddle.static.nn.control_flow.While(cond=cond) + >>> with while_op.block(): + ... i = paddle.increment(x=i, value=1) + ... paddle.assign(paddle.less_than(x=i, y=loop_len), output=cond) - i = paddle.full(shape=[1], dtype='int64', fill_value=0) - loop_len = paddle.full(shape=[1], dtype='int64', fill_value=10) - one = paddle.full(shape=[1], dtype='float32', fill_value=1) - data = paddle.static.data(name='data', shape=[1], dtype='float32') - sums = paddle.full(shape=[1], dtype='float32', fill_value=0) # Define the variable to be obtained ouside of While, which name should be different from the variable inside the While to be obtained + >>> exe = paddle.static.Executor(paddle.CPUPlace()) + >>> exe.run(paddle.static.default_startup_program()) - cond = paddle.less_than(x=i, y=loop_len) - while_op = paddle.static.nn.control_flow.While(cond=cond) - with while_op.block(): - sums_tensor = paddle.add(x=data, y=data) - paddle.assign(sums_tensor, sums) # Update the value of sums_tensor defined in While to the sums which defined outside of While through layers.assign - i = paddle.increment(x=i, value=1) - data = paddle.add(x=data, y=one) - paddle.assign(paddle.less_than(x=i, y=loop_len), output=cond) + >>> res = exe.run(paddle.static.default_main_program(), feed={}, fetch_list=[i]) + >>> print(res) + [array([10], dtype=int64)] - feed_data = np.ones(1).astype('float32') - exe = paddle.static.Executor(paddle.CPUPlace()) - exe.run(paddle.static.default_startup_program()) - res = exe.run(paddle.static.default_main_program(), feed={'data': feed_data}, fetch_list=sums) - print(res[0]) # [2.] # Because the data in While does not update the value outside the While, the value of sums is [2.] after the loop + .. code-block:: python + :name: example-2 + + >>> import paddle + >>> import numpy as np + + >>> paddle.enable_static() + + >>> i = paddle.full(shape=[1], dtype='int64', fill_value=0) + >>> loop_len = paddle.full(shape=[1], dtype='int64', fill_value=10) + >>> one = paddle.full(shape=[1], dtype='float32', fill_value=1) + >>> data = paddle.static.data(name='data', shape=[1], dtype='float32') + >>> sums = paddle.full(shape=[1], dtype='float32', fill_value=0) # Define the variable to be obtained >>> ouside of While, which name should be different from the variable inside the While to be obtained + + >>> cond = paddle.less_than(x=i, y=loop_len) + >>> while_op = paddle.static.nn.control_flow.While(cond=cond) + >>> with while_op.block(): + ... sums_tensor = paddle.add(x=data, y=data) + ... paddle.assign(sums_tensor, sums) # Update the value of sums_tensor defined in While to the sums which defined outside of While through layers.assign + ... i = paddle.increment(x=i, value=1) + ... data = paddle.add(x=data, y=one) + ... paddle.assign(paddle.less_than(x=i, y=loop_len), output=cond) + + >>> feed_data = np.ones(1).astype('float32') + >>> exe = paddle.static.Executor(paddle.CPUPlace()) + >>> exe.run(paddle.static.default_startup_program()) + >>> res = exe.run(paddle.static.default_main_program(), feed={'data': feed_data}, fetch_list=sums) + >>> print(res[0]) # Because the data in While does not update the value outside the While, the value of sums is [2.] after the loop + [2.] """ BEFORE_WHILE_BLOCK = 0 @@ -619,26 +625,27 @@ def while_loop(cond, body, loop_vars, is_test=False, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - def cond(i, ten): - return i < ten + >>> def cond(i, ten): + ... return i < ten - def body(i, ten): - i = i + 1 - return [i, ten] + >>> def body(i, ten): + ... i = i + 1 + ... return [i, ten] - main_program = paddle.static.default_main_program() - startup_program = paddle.static.default_startup_program() - with paddle.static.program_guard(main_program, startup_program): - i = paddle.full(shape=[1], fill_value=0, dtype='int64') # loop counter - ten = paddle.full(shape=[1], fill_value=10, dtype='int64') # loop length - i, ten = paddle.static.nn.while_loop(cond, body, [i, ten]) + >>> main_program = paddle.static.default_main_program() + >>> startup_program = paddle.static.default_startup_program() + >>> with paddle.static.program_guard(main_program, startup_program): + ... i = paddle.full(shape=[1], fill_value=0, dtype='int64') # loop counter + ... ten = paddle.full(shape=[1], fill_value=10, dtype='int64') # loop length + ... i, ten = paddle.static.nn.while_loop(cond, body, [i, ten]) - exe = paddle.static.Executor(paddle.CPUPlace()) - res = exe.run(main_program, feed={}, fetch_list=[i]) - print(res) # [array([10])] + ... exe = paddle.static.Executor(paddle.CPUPlace()) + ... res = exe.run(main_program, feed={}, fetch_list=[i]) + ... print(res) + [array([10], dtype=int64)] """ helper = LayerHelper('while_loop', **locals()) @@ -783,43 +790,42 @@ def case(pred_fn_pairs, default=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.enable_static() - paddle.enable_static() + >>> def fn_1(): + ... return paddle.full(shape=[1, 2], dtype='float32', fill_value=1) - def fn_1(): - return paddle.full(shape=[1, 2], dtype='float32', fill_value=1) + >>> def fn_2(): + ... return paddle.full(shape=[2, 2], dtype='int32', fill_value=2) - def fn_2(): - return paddle.full(shape=[2, 2], dtype='int32', fill_value=2) + >>> def fn_3(): + ... return paddle.full(shape=[3], dtype='int32', fill_value=3) - def fn_3(): - return paddle.full(shape=[3], dtype='int32', fill_value=3) + >>> main_program = paddle.static.default_startup_program() + >>> startup_program = paddle.static.default_main_program() - main_program = paddle.static.default_startup_program() - startup_program = paddle.static.default_main_program() + >>> with paddle.static.program_guard(main_program, startup_program): + ... x = paddle.full(shape=[1], dtype='float32', fill_value=0.3) + ... y = paddle.full(shape=[1], dtype='float32', fill_value=0.1) + ... z = paddle.full(shape=[1], dtype='float32', fill_value=0.2) - with paddle.static.program_guard(main_program, startup_program): - x = paddle.full(shape=[1], dtype='float32', fill_value=0.3) - y = paddle.full(shape=[1], dtype='float32', fill_value=0.1) - z = paddle.full(shape=[1], dtype='float32', fill_value=0.2) + ... pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 + ... pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 + ... pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 - pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3 - pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1 - pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1 + ... # Call fn_1 because pred_1 is True + ... out_1 = paddle.static.nn.case( + ... pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3) - # Call fn_1 because pred_1 is True - out_1 = paddle.static.nn.case( - pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3) + ... # Argument default is None and no pred in pred_fn_pairs is True. fn_3 will be called. + ... # because fn_3 is the last callable in pred_fn_pairs. + ... out_2 = paddle.static.nn.case(pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)]) - # Argument default is None and no pred in pred_fn_pairs is True. fn_3 will be called. - # because fn_3 is the last callable in pred_fn_pairs. - out_2 = paddle.static.nn.case(pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)]) - - exe = paddle.static.Executor(paddle.CPUPlace()) - res_1, res_2 = exe.run(main_program, fetch_list=[out_1, out_2]) - print(res_1) # [[1. 1.]] - print(res_2) # [3 3 3] + ... exe = paddle.static.Executor(paddle.CPUPlace()) + ... res_1, res_2 = exe.run(main_program, fetch_list=[out_1, out_2]) + ... print(res_1, res_2) + [[1. 1.]] [3 3 3] ''' helper = LayerHelper('case', **locals()) @@ -919,45 +925,59 @@ def switch_case(branch_index, branch_fns, default=None, name=None): Examples: .. code-block:: python - import paddle - - paddle.enable_static() - - def fn_1(): - return paddle.full(shape=[1, 2], dtype='float32', fill_value=1) - - def fn_2(): - return paddle.full(shape=[2, 2], dtype='int32', fill_value=2) - - def fn_3(): - return paddle.full(shape=[3], dtype='int32', fill_value=3) - - main_program = paddle.static.default_startup_program() - startup_program = paddle.static.default_main_program() - with paddle.static.program_guard(main_program, startup_program): - index_1 = paddle.full(shape=[1], dtype='int32', fill_value=1) - index_2 = paddle.full(shape=[1], dtype='int32', fill_value=2) - - out_1 = paddle.static.nn.switch_case( - branch_index=index_1, - branch_fns={1: fn_1, 2: fn_2}, - default=fn_3) - - out_2 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(1, fn_1), (2, fn_2)], - default=fn_3) - - # Argument default is None and no index matches. fn;,,_3 will be called because of the max index 7. - out_3 = paddle.static.nn.switch_case( - branch_index=index_2, - branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)]) - - exe = paddle.static.Executor(paddle.CPUPlace()) - res_1, res_2, res_3 = exe.run(main_program, fetch_list=[out_1, out_2, out_3]) - print(res_1) # [[1. 1.]] - print(res_2) # [[2 2] [2 2]] - print(res_3) # [3 3 3] + >>> import paddle + >>> paddle.enable_static() + + >>> def fn_1(): + ... return paddle.full(shape=[1, 2], dtype='float32', fill_value=1) + + >>> def fn_2(): + ... return paddle.full(shape=[2, 2], dtype='int32', fill_value=2) + + >>> def fn_3(): + ... return paddle.full(shape=[3], dtype='int32', fill_value=3) + + >>> startup_program = paddle.static.default_startup_program() + >>> main_program = paddle.static.default_main_program() + >>> with paddle.static.program_guard(main_program, startup_program): + ... index_1 = paddle.full(shape=[1], dtype='int32', fill_value=1) + ... index_2 = paddle.full(shape=[1], dtype='int32', fill_value=2) + ... + ... out_1 = paddle.static.nn.switch_case( + ... branch_index=index_1, + ... branch_fns={1: fn_1, 2: fn_2}, + ... default=fn_3) + ... + ... out_2 = paddle.static.nn.switch_case( + ... branch_index=index_2, + ... branch_fns=[(1, fn_1), (2, fn_2)], + ... default=fn_3) + ... + ... # Argument default is None and no index matches. fn_3 will be called because of the max index 7. + ... out_3 = paddle.static.nn.switch_case( + ... branch_index=index_2, + ... branch_fns=[(0, fn_1), (4, fn_2), (7, fn_3)]) + ... + ... exe = paddle.static.Executor(paddle.CPUPlace()) + ... res_1, res_2, res_3 = exe.run(main_program, fetch_list=[out_1, out_2, out_3]) + ... # Variable: fill_constant_1.tmp_0 + ... # - message: The content of input layer: + ... # - lod: {} + ... # - place: Place(cpu) + ... # - shape: [2, 3] + ... # - layout: NCHW + ... # - dtype: int64 + ... # - data: [3 3 3 3 3 3] + + >>> print(res_1) + [[1. 1.]] + + >>> print(res_2) + [[2 2] + [2 2]] + + >>> print(res_3) + [3 3 3] ''' helper = LayerHelper('switch_case', **locals()) @@ -1093,12 +1113,12 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None): .. code-block:: python :name: code-example-1 - import paddle + >>> import paddle - a = paddle.zeros((1, 1)) - b = paddle.zeros((1, 1)) - c = a * b - out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b) + >>> a = paddle.zeros((1, 1)) + >>> b = paddle.zeros((1, 1)) + >>> c = a * b + >>> out = paddle.static.nn.cond(a < b, lambda: a + c, lambda: b * b) No matter whether ``a < b`` , ``c = a * b`` will be in net building and run. ``a + c`` and ``b * b`` will be in net building, but only one @@ -1128,39 +1148,46 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None): .. code-block:: python :name: code-example-2 - import paddle - - # - # pseudocode: - # if 0.1 < 0.23: - # return 1, True - # else: - # return 3, 2 - # - - def true_func(): - return paddle.full(shape=[1, 2], dtype='int32', - fill_value=1), paddle.full(shape=[2, 3], - dtype='bool', - fill_value=True) - - - def false_func(): - return paddle.full(shape=[3, 4], dtype='float32', - fill_value=3), paddle.full(shape=[4, 5], - dtype='int64', - fill_value=2) - - - x = paddle.full(shape=[1], dtype='float32', fill_value=0.1) - y = paddle.full(shape=[1], dtype='float32', fill_value=0.23) - pred = paddle.less_than(x=x, y=y, name=None) - ret = paddle.static.nn.cond(pred, true_func, false_func) - # ret is a tuple containing 2 tensors - # ret[0] = [[1 1]] - # ret[1] = [[ True True True] - # [ True True True]] - + >>> import paddle + + >>> # pseudocode: + >>> # if 0.1 < 0.23: + >>> # return 1, True + >>> # else: + >>> # return 3, 2 + + >>> def true_func(): + ... return paddle.full(shape=[1, 2], + ... dtype='int32', + ... fill_value=1 + ... ), paddle.full(shape=[2, 3], + ... dtype='bool', + ... fill_value=True + ... ) + + + >>> def false_func(): + ... return paddle.full(shape=[3, 4], + ... dtype='float32', + ... fill_value=3 + ... ), paddle.full(shape=[4, 5], + ... dtype='int64', + ... fill_value=2 + ... ) + + + >>> x = paddle.full(shape=[1], dtype='float32', fill_value=0.1) + >>> y = paddle.full(shape=[1], dtype='float32', fill_value=0.23) + >>> pred = paddle.less_than(x=x, y=y, name=None) + >>> a, b = paddle.static.nn.cond(pred, true_func, false_func) + + >>> print(a) + Tensor(shape=[1, 2], dtype=int32, place=Place(cpu), stop_gradient=True, + [[1, 1]]) + >>> print(b) + Tensor(shape=[2, 3], dtype=bool, place=Place(cpu), stop_gradient=True, + [[True, True, True], + [True, True, True]]) """ if in_dygraph_mode(): assert isinstance(pred, Variable), "The pred in cond must be Variable" @@ -1665,24 +1692,29 @@ def Print( Examples: .. code-block:: python - import paddle - - paddle.enable_static() - - x = paddle.full(shape=[2, 3], fill_value=3, dtype='int64') - out = paddle.static.Print(x, message="The content of input layer:") - - main_program = paddle.static.default_main_program() - exe = paddle.static.Executor(place=paddle.CPUPlace()) - res = exe.run(main_program, fetch_list=[out]) - # Variable: fill_constant_1.tmp_0 - # - message: The content of input layer: - # - lod: {} - # - place: CPUPlace - # - shape: [2, 3] - # - layout: NCHW - # - dtype: long - # - data: [3 3 3 3 3 3] + >>> import paddle + + >>> paddle.enable_static() + + >>> x = paddle.full(shape=[2, 3], fill_value=3, dtype='int64') + >>> out = paddle.static.Print(x, message="The content of input layer:") + + >>> main_program = paddle.static.default_main_program() + >>> exe = paddle.static.Executor(place=paddle.CPUPlace()) + >>> res = exe.run(main_program, fetch_list=[out]) + >>> # doctest: +SKIP('Unable to get output') + Variable: fill_constant_1.tmp_0 + - message: The content of input layer: + - lod: {} + - place: Place(cpu) + - shape: [2, 3] + - layout: NCHW + - dtype: int64 + - data: [3 3 3 3 3 3] + >>> # doctest: -SKIP + >>> res + [array([[3, 3, 3], + [3, 3, 3]], dtype=int64)] ''' check_variable_and_dtype( input, From 8746e230f4776f7cc28a89ca8e2cb0901d8ad07a Mon Sep 17 00:00:00 2001 From: KongAKun <143328179+KongAKun@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:07:42 +0800 Subject: [PATCH 115/194] [xdoctest] reformat example code with google style in No. 264-265 (#56907) * Fix styles of code * update the GPU option * add the GPU setup * remove the note * update the code --- .../incubate/nn/layer/fused_dropout_add.py | 15 ++++---- .../incubate/nn/layer/fused_dropout_nd.py | 34 +++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/python/paddle/incubate/nn/layer/fused_dropout_add.py b/python/paddle/incubate/nn/layer/fused_dropout_add.py index 77874d2944764..51cfc18d443b4 100644 --- a/python/paddle/incubate/nn/layer/fused_dropout_add.py +++ b/python/paddle/incubate/nn/layer/fused_dropout_add.py @@ -44,16 +44,17 @@ class FusedDropoutAdd(Layer): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.layer.fused_dropout_add import FusedDropoutAdd + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> from paddle.incubate.nn.layer.fused_dropout_add import FusedDropoutAdd - x = paddle.to_tensor([[1,2,3], [4,5,6]], dtype="float32") - y = paddle.to_tensor([[1,2,3], [4,5,6]], dtype="float32") + >>> x = paddle.to_tensor([[1,2,3], [4,5,6]], dtype="float32") + >>> y = paddle.to_tensor([[1,2,3], [4,5,6]], dtype="float32") - m = FusedDropoutAdd(p=0.5) + >>> m = FusedDropoutAdd(p=0.5) - out = m(x, y) + >>> out = m(x, y) """ def __init__(self, p=0.5, mode="upscale_in_train", name=None): diff --git a/python/paddle/incubate/nn/layer/fused_dropout_nd.py b/python/paddle/incubate/nn/layer/fused_dropout_nd.py index 156880f73281b..a820654fa9efc 100644 --- a/python/paddle/incubate/nn/layer/fused_dropout_nd.py +++ b/python/paddle/incubate/nn/layer/fused_dropout_nd.py @@ -53,23 +53,23 @@ class FusedDropout(paddle.nn.Layer): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32") - m = paddle.incubate.nn.FusedDropout(p=0.5) - - y_train = m(x) - print(y_train) - # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[2., 0., 6.], - # [0., 0., 0.]]) - - m.eval() # switch the model to test phase - y_test = m(x) - print(y_test) - # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[1., 2., 3.], - # [4., 5., 6.]]) + >>> import paddle + + >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32") + >>> m = paddle.incubate.nn.FusedDropout(p=0.5) + + >>> y_train = m(x) + >>> print(y_train) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[2., 0., 6.], + [0., 0., 0.]]) + + >>> m.eval() # switch the model to test phase + >>> y_test = m(x) + >>> print(y_test) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1., 2., 3.], + [4., 5., 6.]]) """ def __init__(self, p=0.5, axis=None, mode="upscale_in_train", name=None): From 1a15a351e97c087edd3c1f3b8083f1c31dce67b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A3=9E=E7=8C=AA?= <106524776+ooooo-create@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:34:33 +0800 Subject: [PATCH 116/194] [xdoctest][task 184-185] reformat example code with google style in `distributed/auto_parallel/static/*` (#56666) * [Doctest]fix No.184,185, test=docs_preview * add env skip * fix @staticmethod * fix * add xdoctest for v2 * fix --- .../auto_parallel/static/cluster_v2.py | 13 +- .../auto_parallel/static/converter.py | 138 ++++++++++-------- 2 files changed, 82 insertions(+), 69 deletions(-) diff --git a/python/paddle/distributed/auto_parallel/static/cluster_v2.py b/python/paddle/distributed/auto_parallel/static/cluster_v2.py index 3f08d240efb05..479dbdfb57493 100644 --- a/python/paddle/distributed/auto_parallel/static/cluster_v2.py +++ b/python/paddle/distributed/auto_parallel/static/cluster_v2.py @@ -58,14 +58,15 @@ class DeviceMesh(core.DeviceMesh): Examples: .. code-block:: python - import paddle - import paddle.distributed as dist + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import paddle + >>> import paddle.distributed as dist - paddle.enable_static() + >>> paddle.enable_static() - mesh = dist.DeviceMesh([[2, 4, 5], [0, 1, 3]]) - assert mesh.shape == [2, 3] - assert mesh.device_ids == [2, 4, 5, 0, 1, 3] + >>> mesh = dist.DeviceMesh([[2, 4, 5], [0, 1, 3]]) + >>> assert mesh.shape == [2, 3] + >>> assert mesh.device_ids == [2, 4, 5, 0, 1, 3] """ diff --git a/python/paddle/distributed/auto_parallel/static/converter.py b/python/paddle/distributed/auto_parallel/static/converter.py index 68f571857d7cf..710dfb43e877b 100644 --- a/python/paddle/distributed/auto_parallel/static/converter.py +++ b/python/paddle/distributed/auto_parallel/static/converter.py @@ -101,28 +101,30 @@ def convert(self, strict=True): Examples: .. code-block:: python - import numpy as np - complete_tensors = np.arange(4).reshape([2, 2]) - partitial_tensors = np.split(complete_tensors, 2, axis=0) - name = "tmp_0" - tensors_dict = {name: partitial_tensors} - strategy_1 = { - name: { - "process_shape": [2], - "process_group": [0, 1], - "dims_mapping": [0, -1] - } - } - strategy_2 = { - name: { - "process_shape": [2], - "process_group": [0, 1], - "dims_mapping": [-1, -1] - } - } - converter = Converter(tensors_dict, strategy_1, strategy_2) - result = converter.convert() - # the result's value is equal to `complete_tensors` + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import numpy as np + >>> from paddle.distributed.auto_parallel.static.converter import Converter + >>> complete_tensors = np.arange(4).reshape([2, 2]) + >>> partitial_tensors = np.split(complete_tensors, 2, axis=0) + >>> name = "tmp_0" + >>> tensors_dict = {name: partitial_tensors} + >>> strategy_1 = { + ... name: { + ... "process_shape": [2], + ... "process_group": [0, 1], + ... "dims_mapping": [0, -1] + ... } + ... } + >>> strategy_2 = { + ... name: { + ... "process_shape": [2], + ... "process_group": [0, 1], + ... "dims_mapping": [-1, -1] + ... } + ... } + >>> converter = Converter(tensors_dict, strategy_1, strategy_2) + >>> result = converter.convert() + >>> # the result's value is equal to `complete_tensors` """ tensors_dict = {} # the name which is in cur_process but not in pre_process @@ -352,13 +354,18 @@ def merge(partition_tensor_list, tensor, partition_index, complete_shape): Examples: .. code-block:: python - import numpy as np - partition_tensor_list = [(np.array([[[1.11, 1.12]]]), [[0,1],[0,1],[0,2]])] - tensor = np.array([[[1.13, 1.14]]]) - partition_index = [[0,1],[0,1],[2,4]] - - _merge_tensor(partition_tensor_list, tensor, partition_index) - # partition_tensor_list: [(np.array([[[1.11, 1.12, 1.13, 1.14]]]), [[0,1],[0,1],[0,4]])] + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import numpy as np + >>> import paddle + >>> from paddle.distributed.auto_parallel.static.converter import Converter + >>> partition_tensor_list = [(np.array([[[1.11, 1.12]]]), [[0,1],[0,1],[0,2]])] + >>> tensor = np.array([[[1.13, 1.14]]]) + >>> partition_index = [[0,1],[0,1],[2,4]] + >>> complete_shape = [3, 2] + + >>> Converter.merge(partition_tensor_list, tensor, partition_index, complete_shape) + >>> print(partition_tensor_list) + [(array([[[1.11, 1.12, 1.13, 1.14]]]), [[0, 1], [0, 1], [0, 4]])] """ from .reshard import Resharder @@ -416,16 +423,19 @@ def split(complete_tensor, partition_index_list, length): Examples: .. code-block:: python - import numpy as np - complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) - rank = 2 - complete_shape = [1, 1, 6] - dims_mapping = [-1, -1, 0] - process_shape = [3] - process_group = [0, 1, 2] - - sliced_tensor_list = split(complete_tensor, [[], [], [2, 4]], 3) - # [array([[[1.11, 1.12]]]), array([[[1.13, 1.14]]]), array([[[1.15, 1.16]]])] + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import numpy as np + >>> from paddle.distributed.auto_parallel.static.converter import Converter + >>> complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) + >>> rank = 2 + >>> complete_shape = [1, 1, 6] + >>> dims_mapping = [-1, -1, 0] + >>> process_shape = [3] + >>> process_group = [0, 1, 2] + + >>> sliced_tensor_list = Converter.split(complete_tensor, [[], [], [2, 4]], 3) + >>> print(sliced_tensor_list) + [array([[[1.11, 1.12]]]), array([[[1.13, 1.14]]]), array([[[1.15, 1.16]]])] """ sliced_tensor_list = [] axis = len(complete_tensor.shape) - length @@ -453,15 +463,18 @@ def _get_split_indices( Examples: .. code-block:: python - import numpy as np - complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) - complete_shape = [1, 1, 6] - dims_mapping = [-1, -1, 0] - process_shape = [3] - process_group = [0, 1, 2] - - index = _get_split_indices(complete_shape, dims_mapping, process_shape, process_group) - # index: [[], [], [2, 4]] + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import numpy as np + >>> from paddle.distributed.auto_parallel.static.utils import _get_split_indices + >>> complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) + >>> complete_shape = [1, 1, 6] + >>> dims_mapping = [-1, -1, 0] + >>> process_shape = [3] + >>> process_group = [0, 1, 2] + + >>> index = _get_split_indices(complete_shape, dims_mapping, process_shape, process_group) + >>> print(index) + [[], [], [2, 4]] """ from .reshard import Resharder @@ -502,21 +515,20 @@ def _get_sliced_index( Examples: .. code-block:: python - import numpy as np - complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) - rank = 2 - complete_shape = [1, 1, 6] - dims_mapping = [-1, -1, 0] - process_shape = [3] - process_group = [0, 1, 2] - - slice_tensor = _slice_tensor(complete_tensor, [[], [], [2, 4]], 3) - # slice_tensor: - # [array([[[1.11, 1.12]]]), array([[[1.13, 1.14]]]), array([[[1.15, 1.16]]])] - - index = _get_sliced_index(rank, complete_shape, dims_mapping - process_shape, process_group) - # index: 2 + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> import numpy as np + >>> from paddle.distributed.auto_parallel.static.converter import Converter + >>> complete_tensor = np.array([[[1.11, 1.12, 1.13, 1.14, 1.15, 1.16]]]) + >>> rank = 2 + >>> complete_shape = [1, 1, 6] + >>> dims_mapping = [-1, -1, 0] + >>> process_shape = [3] + >>> process_group = [0, 1, 2] + + >>> index = Converter._get_sliced_index(rank, complete_shape, dims_mapping, + ... process_shape, process_group) + >>> print(index) + 2 """ from .reshard import Resharder From 52d29f0daa152f0b3d5952b95fa356263697a251 Mon Sep 17 00:00:00 2001 From: ronnywang Date: Tue, 5 Sep 2023 12:44:54 +0800 Subject: [PATCH 117/194] [CustomDevice] add mp_allreduce_sum op for all custom devices (#56927) --- .../custom_device_common_op_registry.cc | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/paddle/fluid/operators/custom_device_common_op_registry.cc b/paddle/fluid/operators/custom_device_common_op_registry.cc index df4d65af1d03f..a1106b1386757 100644 --- a/paddle/fluid/operators/custom_device_common_op_registry.cc +++ b/paddle/fluid/operators/custom_device_common_op_registry.cc @@ -535,6 +535,23 @@ template class CAllReduceOpCustomDeviceKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { + if (ctx.HasInput("Cond")) { + auto cond = ctx.Input("Cond"); + auto place = cond->place(); + PADDLE_ENFORCE_EQ(platform::is_cpu_place(place), + true, + platform::errors::PreconditionNotMet( + "The input `cond` tensor should be on cpu place")); + PADDLE_ENFORCE_EQ(cond->numel(), + 1, + platform::errors::PreconditionNotMet( + "The input `cond` should be shape [1]")); + if (!cond->data()[0]) { + VLOG(4) << "Skip all reduce Op since cond is 0"; + return; + } + } + auto in = ctx.Input("X"); auto out = ctx.Output("Out"); int rid = ctx.Attr("ring_id"); @@ -1441,6 +1458,29 @@ void RegisterCustomDeviceCommonKernel(const std::string& dev_type) { paddle::platform::CustomDeviceContext, int64_t, phi::ccl::CCLReduceOp::SUM>) {} + REGISTER_OP_CUSTOM_DEVICE_KERNEL( + mp_allreduce_sum, + device_type, + paddle::operators::CAllReduceOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + float, + phi::ccl::CCLReduceOp::SUM>, + paddle::operators::CAllReduceOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + double, + phi::ccl::CCLReduceOp::SUM>, + paddle::operators::CAllReduceOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + paddle::platform::float16, + phi::ccl::CCLReduceOp::SUM>, + paddle::operators::CAllReduceOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + int32_t, + phi::ccl::CCLReduceOp::SUM>, + paddle::operators::CAllReduceOpCustomDeviceKernel< + paddle::platform::CustomDeviceContext, + int64_t, + phi::ccl::CCLReduceOp::SUM>) {} REGISTER_OP_CUSTOM_DEVICE_KERNEL( c_allreduce_min, device_type, From 724adf49fc6a0f22221683d614d8247aeb3e7083 Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Tue, 5 Sep 2023 12:53:55 +0800 Subject: [PATCH 118/194] update cinn flags usage (#56896) * update * update * fix empty arg * update test * update * fix bug * update --- CMakeLists.txt | 3 + cmake/cinn.cmake | 18 +- paddle/cinn/CMakeLists.txt | 2 +- paddle/cinn/auto_schedule/auto_tuner_test.cc | 2 +- .../search_space/search_space.cc | 2 +- .../search_strategy/evolutionary_search.cc | 2 +- .../cinn/auto_schedule/task/task_optimizer.cc | 2 +- .../cinn/auto_schedule/task/task_registry.h | 3 +- .../auto_schedule/task/task_registry_test.cc | 2 +- .../tests/performance_comparison_test.cc | 14 +- paddle/cinn/backends/codegen_c.cc | 2 +- paddle/cinn/backends/codegen_c.h | 3 +- paddle/cinn/backends/compiler.cc | 10 +- paddle/cinn/backends/extern_func_emitter.cc | 2 +- paddle/cinn/backends/function_prototype.cc | 2 +- .../backends/llvm/runtime_symbol_registry.cc | 4 +- paddle/cinn/backends/nvrtc/nvrtc_util.cc | 4 +- paddle/cinn/common/context.cc | 6 +- paddle/cinn/common/context.h | 4 +- paddle/cinn/frontend/computation_test.cc | 2 +- paddle/cinn/frontend/interpreter.cc | 2 +- paddle/cinn/frontend/interpreter_test.cc | 2 +- paddle/cinn/frontend/optimize.cc | 18 +- .../cinn/frontend/paddle/model_parser_test.cc | 4 +- .../cinn/frontend/paddle_model_convertor.cc | 2 +- .../frontend/paddle_model_convertor_test.cc | 2 +- .../cinn/frontend/paddle_model_to_program.cc | 2 +- paddle/cinn/frontend/pass/pass_test_helper.h | 4 +- paddle/cinn/frontend/syntax_test.cc | 2 +- paddle/cinn/gtest_main.cc | 4 +- .../cinn/hlir/framework/accuracy_checker.cc | 2 +- .../hlir/framework/accuracy_checker_test.cc | 2 +- paddle/cinn/hlir/framework/graph.cc | 2 +- paddle/cinn/hlir/framework/graph_test.cc | 2 +- paddle/cinn/hlir/framework/instruction.cc | 4 +- .../hlir/framework/new_ir/op_lowering_impl.cc | 2 +- .../cinn/hlir/framework/op_lowering_impl.cc | 2 +- .../cinn/hlir/framework/parallel_compiler.cc | 2 +- .../cinn/hlir/framework/visualize_helper.cc | 4 +- .../cinn/hlir/op/contrib/bitcast_convert.cc | 2 - paddle/cinn/hlir/op/contrib/cholesky.cc | 2 - paddle/cinn/hlir/op/contrib/gather_nd.cc | 2 - .../cinn/hlir/op/contrib/gaussian_random.cc | 2 - .../hlir/op/contrib/logical_right_shift.cc | 2 +- paddle/cinn/hlir/op/contrib/lookup_table.cc | 2 +- paddle/cinn/hlir/op/contrib/one_hot.cc | 2 - paddle/cinn/hlir/op/contrib/randint.cc | 2 - paddle/cinn/hlir/op/contrib/reciprocal.cc | 2 +- paddle/cinn/hlir/op/contrib/repeat.cc | 2 - paddle/cinn/hlir/op/contrib/resize.cc | 2 - paddle/cinn/hlir/op/contrib/sort.cc | 2 - paddle/cinn/hlir/op/contrib/uniform_random.cc | 2 - paddle/cinn/hlir/pass/alterlayout_test.cc | 2 +- .../common_subexpression_elimination_test.cc | 2 +- paddle/cinn/hlir/pass/const_propagate_test.cc | 2 +- paddle/cinn/hlir/pass/custom_call_pass.cc | 2 +- paddle/cinn/hlir/pass/fusion_merge_pass.cc | 2 +- .../hlir/pass/general_fusion_merge_pass.cc | 2 +- paddle/cinn/hlir/pass/opfusion_test.cc | 2 +- paddle/cinn/hlir/pass/test_primitive_ops.cc | 2 +- paddle/cinn/hlir/pe/schedule.cc | 2 +- paddle/cinn/ir/schedule/ir_schedule.cc | 2 +- paddle/cinn/runtime/custom_function.cc | 2 +- paddle/cinn/runtime/flags.cc | 282 +++++++++--------- paddle/cinn/utils/profiler.cc | 4 +- paddle/utils/CMakeLists.txt | 19 +- paddle/utils/flags.h | 26 ++ paddle/utils/flags_native.cc | 41 +++ paddle/utils/flags_native.h | 6 + paddle/utils/flags_native_test.cc | 6 + 70 files changed, 329 insertions(+), 256 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f20a52522acdb..9e9e8b3d74a13 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -610,6 +610,9 @@ if(WITH_CINN) add_subdirectory(python) endif() add_subdirectory(test) + if(NOT WITH_GFLAGS) + add_subdirectory(paddle/utils) + endif() return() endif() endif() diff --git a/cmake/cinn.cmake b/cmake/cinn.cmake index ed7735e3c7d91..a8ebe6a9a46ae 100644 --- a/cmake/cinn.cmake +++ b/cmake/cinn.cmake @@ -114,10 +114,6 @@ include(cmake/cinn/core.cmake) include(cmake/cinn/nvrtc.cmake) include(cmake/cinn/nvtx.cmake) -if(CINN_ONLY) - link_libraries(gflags) -endif() - set(LINK_FLAGS "-Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cinn/export.map" CACHE INTERNAL "") @@ -183,9 +179,9 @@ if(WITH_MKL) endif() endif() -if(NOT WITH_GFLAGS) - target_link_libraries(cinnapi gflags) - add_dependencies(cinnapi gflags) +if(CINN_ONLY) + target_link_libraries(cinnapi ${flags_dep}) + add_dependencies(cinnapi ${flags_dep}) endif() if(WITH_GPU) @@ -242,9 +238,9 @@ function(gen_cinncore LINKTYPE) endif() endif() - if(NOT WITH_GFLAGS) - target_link_libraries(${CINNCORE_TARGET} gflags) - add_dependencies(${CINNCORE_TARGET} gflags) + if(CINN_ONLY) + target_link_libraries(${CINNCORE_TARGET} ${flags_dep}) + add_dependencies(${CINNCORE_TARGET} ${flags_dep}) endif() if(WITH_GPU) @@ -272,6 +268,8 @@ set(PUBLISH_LIBS ON) if(PUBLISH_LIBS) set(core_includes "${core_includes};paddle/cinn/runtime/cuda/cinn_cuda_runtime_source.cuh") + set(core_includes + "${core_includes};paddle/utils/flags.h;paddle/utils/flags_native.h") foreach(header ${core_includes}) get_filename_component(prefix ${header} DIRECTORY) file(COPY ${header} diff --git a/paddle/cinn/CMakeLists.txt b/paddle/cinn/CMakeLists.txt index 4166e6453b420..4645ff2c06636 100644 --- a/paddle/cinn/CMakeLists.txt +++ b/paddle/cinn/CMakeLists.txt @@ -1,5 +1,5 @@ if(WITH_TESTING) - cinn_cc_library(cinn_gtest_main SRCS gtest_main.cc DEPS gtest gflags) + cinn_cc_library(cinn_gtest_main SRCS gtest_main.cc DEPS gtest ${flags_dep}) endif() add_subdirectory(api) diff --git a/paddle/cinn/auto_schedule/auto_tuner_test.cc b/paddle/cinn/auto_schedule/auto_tuner_test.cc index 81ef14efee1e9..36fd51016c989 100644 --- a/paddle/cinn/auto_schedule/auto_tuner_test.cc +++ b/paddle/cinn/auto_schedule/auto_tuner_test.cc @@ -32,7 +32,7 @@ #include "paddle/cinn/ir/ir_base.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_bool(auto_schedule_use_cost_model); +PD_DECLARE_bool(auto_schedule_use_cost_model); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/auto_schedule/search_space/search_space.cc b/paddle/cinn/auto_schedule/search_space/search_space.cc index 9ceab068c76d4..a96663386ff7d 100644 --- a/paddle/cinn/auto_schedule/search_space/search_space.cc +++ b/paddle/cinn/auto_schedule/search_space/search_space.cc @@ -34,7 +34,7 @@ #include "paddle/cinn/ir/utils/ir_copy.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_bool(auto_schedule_use_cost_model); +PD_DECLARE_bool(auto_schedule_use_cost_model); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/auto_schedule/search_strategy/evolutionary_search.cc b/paddle/cinn/auto_schedule/search_strategy/evolutionary_search.cc index c2668e1da72d3..1881697237e82 100644 --- a/paddle/cinn/auto_schedule/search_strategy/evolutionary_search.cc +++ b/paddle/cinn/auto_schedule/search_strategy/evolutionary_search.cc @@ -36,7 +36,7 @@ #include "paddle/cinn/utils/sized_multi_set.h" #include "paddle/cinn/utils/string.h" -DECLARE_bool(auto_schedule_use_cost_model); +PD_DECLARE_bool(auto_schedule_use_cost_model); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/auto_schedule/task/task_optimizer.cc b/paddle/cinn/auto_schedule/task/task_optimizer.cc index c4a03bde62464..ea1b18c764533 100644 --- a/paddle/cinn/auto_schedule/task/task_optimizer.cc +++ b/paddle/cinn/auto_schedule/task/task_optimizer.cc @@ -40,7 +40,7 @@ #include "paddle/cinn/backends/cuda_util.h" #endif -DECLARE_bool(auto_schedule_use_cost_model); +PD_DECLARE_bool(auto_schedule_use_cost_model); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/auto_schedule/task/task_registry.h b/paddle/cinn/auto_schedule/task/task_registry.h index c506a7ea332ef..7cff52c220461 100644 --- a/paddle/cinn/auto_schedule/task/task_registry.h +++ b/paddle/cinn/auto_schedule/task/task_registry.h @@ -14,14 +14,13 @@ #pragma once -#include - #include #include #include "paddle/cinn/ir/schedule/ir_schedule.h" #include "paddle/cinn/ir/utils/ir_copy.h" #include "paddle/cinn/utils/registry.h" +#include "paddle/utils/flags.h" namespace cinn { diff --git a/paddle/cinn/auto_schedule/task/task_registry_test.cc b/paddle/cinn/auto_schedule/task/task_registry_test.cc index 26e790b25bd08..23f31028be9e9 100644 --- a/paddle/cinn/auto_schedule/task/task_registry_test.cc +++ b/paddle/cinn/auto_schedule/task/task_registry_test.cc @@ -28,7 +28,7 @@ #include "paddle/cinn/utils/string.h" #include "paddle/cinn/utils/type_defs.h" -DECLARE_bool(auto_schedule_use_cost_model); +PD_DECLARE_bool(auto_schedule_use_cost_model); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc b/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc index bfa152ce558b5..ce7a9369f847e 100644 --- a/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc +++ b/paddle/cinn/auto_schedule/tests/performance_comparison_test.cc @@ -44,9 +44,9 @@ * parameters for more detail. */ -DEFINE_string(resnet50_model_dir, - "./ResNet50", - "the path to paddle model resnet50."); +PD_DEFINE_string(resnet50_model_dir, + "./ResNet50", + "the path to paddle model resnet50."); // Flags that control which schedule tests will be run. // Bit with index 0 controls no schedule test, means options = 1 = "001" will // run no schedule test. Bit with index 1 controls manual schedule test, means @@ -54,10 +54,10 @@ DEFINE_string(resnet50_model_dir, // auto schedule test, means options = 4 = "100" will run auto schedule test. // The default value is -1, which means that this flag is disabled to set the // options -DEFINE_int32(evaluate_knobs, - -1, - "the options to control which schedule tests will be run."); -DECLARE_double(cinn_infer_model_version); +PD_DEFINE_int32(evaluate_knobs, + -1, + "the options to control which schedule tests will be run."); +PD_DECLARE_double(cinn_infer_model_version); namespace cinn { namespace auto_schedule { diff --git a/paddle/cinn/backends/codegen_c.cc b/paddle/cinn/backends/codegen_c.cc index 9d3ffa40f125d..cffebdc1a6736 100644 --- a/paddle/cinn/backends/codegen_c.cc +++ b/paddle/cinn/backends/codegen_c.cc @@ -29,7 +29,7 @@ #include "paddle/cinn/utils/string.h" //! Root of the builtin code. -DECLARE_string(cinn_x86_builtin_code_root); +PD_DECLARE_string(cinn_x86_builtin_code_root); namespace cinn { namespace backends { diff --git a/paddle/cinn/backends/codegen_c.h b/paddle/cinn/backends/codegen_c.h index 72398d9d6b085..74be310e8f520 100644 --- a/paddle/cinn/backends/codegen_c.h +++ b/paddle/cinn/backends/codegen_c.h @@ -14,8 +14,6 @@ #pragma once -#include - #include #include @@ -27,6 +25,7 @@ #include "paddle/cinn/ir/utils/ir_printer.h" #include "paddle/cinn/lang/packed_func.h" #include "paddle/cinn/runtime/cinn_runtime.h" +#include "paddle/utils/flags.h" namespace cinn { diff --git a/paddle/cinn/backends/compiler.cc b/paddle/cinn/backends/compiler.cc index 1e806db8b92f4..f501fe62d0746 100644 --- a/paddle/cinn/backends/compiler.cc +++ b/paddle/cinn/backends/compiler.cc @@ -30,11 +30,11 @@ #include "paddle/cinn/runtime/flags.h" #endif -DECLARE_string(cinn_source_code_save_path); -DECLARE_string(cinn_dump_group_lowered_func); -DECLARE_string(cinn_dump_group_source_code); -DECLARE_string(cinn_dump_group_ptx); -DECLARE_string(cinn_dump_group_instruction); +PD_DECLARE_string(cinn_source_code_save_path); +PD_DECLARE_string(cinn_dump_group_lowered_func); +PD_DECLARE_string(cinn_dump_group_source_code); +PD_DECLARE_string(cinn_dump_group_ptx); +PD_DECLARE_string(cinn_dump_group_instruction); namespace cinn { namespace backends { diff --git a/paddle/cinn/backends/extern_func_emitter.cc b/paddle/cinn/backends/extern_func_emitter.cc index 9c1c7fa347dbc..06a9e37e6d732 100644 --- a/paddle/cinn/backends/extern_func_emitter.cc +++ b/paddle/cinn/backends/extern_func_emitter.cc @@ -27,7 +27,7 @@ #include "paddle/cinn/runtime/flags.h" #include "paddle/cinn/utils/string.h" -DECLARE_bool(verbose_function_register); +PD_DECLARE_bool(verbose_function_register); namespace cinn { namespace backends { diff --git a/paddle/cinn/backends/function_prototype.cc b/paddle/cinn/backends/function_prototype.cc index 9f360d58f9a0c..e413521246b8f 100644 --- a/paddle/cinn/backends/function_prototype.cc +++ b/paddle/cinn/backends/function_prototype.cc @@ -21,7 +21,7 @@ #include "paddle/cinn/ir/tensor.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_bool(verbose_function_register); +PD_DECLARE_bool(verbose_function_register); namespace cinn { namespace backends { diff --git a/paddle/cinn/backends/llvm/runtime_symbol_registry.cc b/paddle/cinn/backends/llvm/runtime_symbol_registry.cc index 0265b72d50a6a..c2c1f53d06f1b 100644 --- a/paddle/cinn/backends/llvm/runtime_symbol_registry.cc +++ b/paddle/cinn/backends/llvm/runtime_symbol_registry.cc @@ -19,10 +19,10 @@ #include -#include "gflags/gflags_declare.h" #include "paddle/cinn/runtime/flags.h" +#include "paddle/utils/flags.h" -DECLARE_bool(verbose_function_register); +PD_DECLARE_bool(verbose_function_register); namespace cinn { namespace backends { diff --git a/paddle/cinn/backends/nvrtc/nvrtc_util.cc b/paddle/cinn/backends/nvrtc/nvrtc_util.cc index 90674e65f9c8d..092520ef5bb85 100644 --- a/paddle/cinn/backends/nvrtc/nvrtc_util.cc +++ b/paddle/cinn/backends/nvrtc/nvrtc_util.cc @@ -30,8 +30,8 @@ #include "paddle/cinn/runtime/flags.h" #include "paddle/cinn/utils/string.h" -DECLARE_string(cinn_nvcc_cmd_path); -DECLARE_bool(nvrtc_compile_to_cubin); +PD_DECLARE_string(cinn_nvcc_cmd_path); +PD_DECLARE_bool(nvrtc_compile_to_cubin); namespace cinn { namespace backends { diff --git a/paddle/cinn/common/context.cc b/paddle/cinn/common/context.cc index d243fef9e263d..e7621fa3528f7 100644 --- a/paddle/cinn/common/context.cc +++ b/paddle/cinn/common/context.cc @@ -78,7 +78,7 @@ std::string NameGenerator::New(const std::string& name_hint) { } // namespace common -DEFINE_bool(cinn_runtime_display_debug_info, - false, - "Whether to display debug information in runtime"); +PD_DEFINE_bool(cinn_runtime_display_debug_info, + false, + "Whether to display debug information in runtime"); } // namespace cinn diff --git a/paddle/cinn/common/context.h b/paddle/cinn/common/context.h index 1c7202ff6dc97..0e1566cca9932 100644 --- a/paddle/cinn/common/context.h +++ b/paddle/cinn/common/context.h @@ -14,7 +14,6 @@ #pragma once #include -#include #include #include @@ -25,10 +24,11 @@ #include "paddle/cinn/common/debug_manager.h" #include "paddle/cinn/common/info_registry.h" #include "paddle/cinn/common/target.h" +#include "paddle/utils/flags.h" namespace cinn { -DECLARE_bool(cinn_runtime_display_debug_info); +PD_DECLARE_bool(cinn_runtime_display_debug_info); namespace ir { class Expr; diff --git a/paddle/cinn/frontend/computation_test.cc b/paddle/cinn/frontend/computation_test.cc index d0d5514180b5c..b3b01af03aba9 100644 --- a/paddle/cinn/frontend/computation_test.cc +++ b/paddle/cinn/frontend/computation_test.cc @@ -23,7 +23,7 @@ #include "paddle/cinn/frontend/pass/use_program_pass.h" #include "paddle/cinn/frontend/program_pass.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/interpreter.cc b/paddle/cinn/frontend/interpreter.cc index 4aaad76c9ce0d..2a5685572a045 100644 --- a/paddle/cinn/frontend/interpreter.cc +++ b/paddle/cinn/frontend/interpreter.cc @@ -25,7 +25,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_bool(enable_auto_tuner); +PD_DECLARE_bool(enable_auto_tuner); namespace cinn::frontend { diff --git a/paddle/cinn/frontend/interpreter_test.cc b/paddle/cinn/frontend/interpreter_test.cc index 6ce674caa4d17..ab3f211120b94 100755 --- a/paddle/cinn/frontend/interpreter_test.cc +++ b/paddle/cinn/frontend/interpreter_test.cc @@ -18,7 +18,7 @@ #include "paddle/cinn/runtime/use_extern_funcs.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn::frontend { diff --git a/paddle/cinn/frontend/optimize.cc b/paddle/cinn/frontend/optimize.cc index 02ea652d50412..2e8d824ee1104 100644 --- a/paddle/cinn/frontend/optimize.cc +++ b/paddle/cinn/frontend/optimize.cc @@ -29,15 +29,15 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_bool(cinn_use_fill_constant_folding); -DECLARE_bool(cinn_use_op_fusion); -DECLARE_bool(cinn_use_common_subexpression_elimination); -DECLARE_string(cinn_check_fusion_accuracy_pass); -DECLARE_bool(cinn_use_custom_call); -DECLARE_bool(use_reduce_split_pass); -DECLARE_bool(cinn_use_dense_merge_pass); -DECLARE_string(cinn_custom_call_deny_ops); -DECLARE_bool(general_fusion_merge_pass); +PD_DECLARE_bool(cinn_use_fill_constant_folding); +PD_DECLARE_bool(cinn_use_op_fusion); +PD_DECLARE_bool(cinn_use_common_subexpression_elimination); +PD_DECLARE_string(cinn_check_fusion_accuracy_pass); +PD_DECLARE_bool(cinn_use_custom_call); +PD_DECLARE_bool(use_reduce_split_pass); +PD_DECLARE_bool(cinn_use_dense_merge_pass); +PD_DECLARE_string(cinn_custom_call_deny_ops); +PD_DECLARE_bool(general_fusion_merge_pass); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/paddle/model_parser_test.cc b/paddle/cinn/frontend/paddle/model_parser_test.cc index 23cb3bdbf6f1a..289b41f9ff12e 100644 --- a/paddle/cinn/frontend/paddle/model_parser_test.cc +++ b/paddle/cinn/frontend/paddle/model_parser_test.cc @@ -14,10 +14,10 @@ #include "paddle/cinn/frontend/paddle/model_parser.h" -#include #include +#include "paddle/utils/flags.h" -DEFINE_string(model_dir, "", "model directory path"); +PD_DEFINE_string(model_dir, "", "model directory path"); namespace cinn::frontend::paddle { diff --git a/paddle/cinn/frontend/paddle_model_convertor.cc b/paddle/cinn/frontend/paddle_model_convertor.cc index f89fa0fcae017..1e4aa1eb46b22 100644 --- a/paddle/cinn/frontend/paddle_model_convertor.cc +++ b/paddle/cinn/frontend/paddle_model_convertor.cc @@ -27,7 +27,7 @@ #include "paddle/cinn/frontend/var_type_utils.h" #include "paddle/cinn/hlir/op/use_ops.h" -DECLARE_double(cinn_infer_model_version); +PD_DECLARE_double(cinn_infer_model_version); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/paddle_model_convertor_test.cc b/paddle/cinn/frontend/paddle_model_convertor_test.cc index 888a3a0c7143c..953a391960132 100644 --- a/paddle/cinn/frontend/paddle_model_convertor_test.cc +++ b/paddle/cinn/frontend/paddle_model_convertor_test.cc @@ -20,7 +20,7 @@ #include "paddle/cinn/frontend/decomposer/test_helper.h" #include "paddle/cinn/runtime/use_extern_funcs.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/paddle_model_to_program.cc b/paddle/cinn/frontend/paddle_model_to_program.cc index 9cdd67afd332b..e17ca8863b5f5 100644 --- a/paddle/cinn/frontend/paddle_model_to_program.cc +++ b/paddle/cinn/frontend/paddle_model_to_program.cc @@ -21,7 +21,7 @@ #include "paddle/cinn/frontend/paddle/pb/program_desc.h" #include "paddle/cinn/hlir/framework/node.h" -DECLARE_double(cinn_infer_model_version); +PD_DECLARE_double(cinn_infer_model_version); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/pass/pass_test_helper.h b/paddle/cinn/frontend/pass/pass_test_helper.h index a683220bb88a3..0aeebef4022fe 100644 --- a/paddle/cinn/frontend/pass/pass_test_helper.h +++ b/paddle/cinn/frontend/pass/pass_test_helper.h @@ -14,7 +14,6 @@ #pragma once -#include #include #include @@ -42,8 +41,9 @@ #include "paddle/cinn/hlir/op/use_ops.h" #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" +#include "paddle/utils/flags.h" -DECLARE_bool(cinn_use_op_fusion); +PD_DECLARE_bool(cinn_use_op_fusion); namespace cinn { namespace frontend { diff --git a/paddle/cinn/frontend/syntax_test.cc b/paddle/cinn/frontend/syntax_test.cc index f5681ae54d8cc..1cc76ef295061 100644 --- a/paddle/cinn/frontend/syntax_test.cc +++ b/paddle/cinn/frontend/syntax_test.cc @@ -30,7 +30,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/gtest_main.cc b/paddle/cinn/gtest_main.cc index 0de75bd71aa6f..7c937870b13b8 100644 --- a/paddle/cinn/gtest_main.cc +++ b/paddle/cinn/gtest_main.cc @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include +#include "paddle/utils/flags.h" int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); - GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, false); + paddle::flags::ParseCommandLineFlags(&argc, &argv); return RUN_ALL_TESTS(); } diff --git a/paddle/cinn/hlir/framework/accuracy_checker.cc b/paddle/cinn/hlir/framework/accuracy_checker.cc index be58a9443f427..95b5b4102414f 100644 --- a/paddle/cinn/hlir/framework/accuracy_checker.cc +++ b/paddle/cinn/hlir/framework/accuracy_checker.cc @@ -21,7 +21,7 @@ #include #endif -DECLARE_int64(cinn_self_check_accuracy_num); +PD_DECLARE_int64(cinn_self_check_accuracy_num); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/accuracy_checker_test.cc b/paddle/cinn/hlir/framework/accuracy_checker_test.cc index b712752953709..f5070101f8eb7 100644 --- a/paddle/cinn/hlir/framework/accuracy_checker_test.cc +++ b/paddle/cinn/hlir/framework/accuracy_checker_test.cc @@ -25,7 +25,7 @@ #include "paddle/cinn/hlir/framework/instruction.h" #include "paddle/cinn/hlir/framework/op_strategy.h" -DECLARE_string(cinn_self_check_accuracy); +PD_DECLARE_string(cinn_self_check_accuracy); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/graph.cc b/paddle/cinn/hlir/framework/graph.cc index 010f8bf9da5db..3f81b8b91906d 100644 --- a/paddle/cinn/hlir/framework/graph.cc +++ b/paddle/cinn/hlir/framework/graph.cc @@ -21,7 +21,7 @@ #include "paddle/cinn/runtime/flags.h" #include "paddle/cinn/utils/string.h" -DECLARE_string(cinn_fusion_groups_graphviz_dir); +PD_DECLARE_string(cinn_fusion_groups_graphviz_dir); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/graph_test.cc b/paddle/cinn/hlir/framework/graph_test.cc index 4dd944ca64b7a..42aec09eca3c2 100644 --- a/paddle/cinn/hlir/framework/graph_test.cc +++ b/paddle/cinn/hlir/framework/graph_test.cc @@ -20,7 +20,7 @@ #include "paddle/cinn/hlir/framework/pass.h" #include "paddle/cinn/hlir/pass/use_pass.h" -DECLARE_string(cinn_fusion_groups_graphviz_dir); +PD_DECLARE_string(cinn_fusion_groups_graphviz_dir); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/instruction.cc b/paddle/cinn/hlir/framework/instruction.cc index 09d644e8533ab..d9b624e518e22 100644 --- a/paddle/cinn/hlir/framework/instruction.cc +++ b/paddle/cinn/hlir/framework/instruction.cc @@ -22,8 +22,8 @@ #include "paddle/cinn/runtime/flags.h" #include "paddle/cinn/utils/profiler.h" -DECLARE_bool(cinn_sync_run); -DECLARE_string(cinn_self_check_accuracy); +PD_DECLARE_bool(cinn_sync_run); +PD_DECLARE_string(cinn_self_check_accuracy); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc index 882d6409c3678..040ea9cf87c98 100644 --- a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc +++ b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc @@ -26,7 +26,7 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/phi/core/ddim.h" -DECLARE_bool(cinn_use_cuda_vectorize); +PD_DECLARE_bool(cinn_use_cuda_vectorize); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/op_lowering_impl.cc b/paddle/cinn/hlir/framework/op_lowering_impl.cc index 9bb8f4e07d779..fb8302f921221 100644 --- a/paddle/cinn/hlir/framework/op_lowering_impl.cc +++ b/paddle/cinn/hlir/framework/op_lowering_impl.cc @@ -19,7 +19,7 @@ #include "paddle/cinn/ir/schedule/ir_schedule.h" #include "paddle/cinn/optim/transform_gpu_forloop.h" -DECLARE_bool(cinn_use_cuda_vectorize); +PD_DECLARE_bool(cinn_use_cuda_vectorize); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/parallel_compiler.cc b/paddle/cinn/hlir/framework/parallel_compiler.cc index 154d9f2a98dde..9bc44c0bc3a84 100644 --- a/paddle/cinn/hlir/framework/parallel_compiler.cc +++ b/paddle/cinn/hlir/framework/parallel_compiler.cc @@ -32,7 +32,7 @@ #include "paddle/cinn/ir/module.h" #include "paddle/cinn/runtime/flags.h" -DECLARE_int32(cinn_parallel_compile_thread); +PD_DECLARE_int32(cinn_parallel_compile_thread); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/framework/visualize_helper.cc b/paddle/cinn/hlir/framework/visualize_helper.cc index e370737b67e3f..bc8c5e4124186 100644 --- a/paddle/cinn/hlir/framework/visualize_helper.cc +++ b/paddle/cinn/hlir/framework/visualize_helper.cc @@ -30,8 +30,8 @@ #include "paddle/cinn/utils/dot_lang.h" #include "paddle/cinn/utils/string.h" -DECLARE_string(cinn_pass_visualize_dir); -DECLARE_string(cinn_check_fusion_accuracy_pass); +PD_DECLARE_string(cinn_pass_visualize_dir); +PD_DECLARE_string(cinn_check_fusion_accuracy_pass); namespace cinn { namespace hlir { namespace framework { diff --git a/paddle/cinn/hlir/op/contrib/bitcast_convert.cc b/paddle/cinn/hlir/op/contrib/bitcast_convert.cc index 21b3eb2a4bd87..cfa957ba5f06f 100644 --- a/paddle/cinn/hlir/op/contrib/bitcast_convert.cc +++ b/paddle/cinn/hlir/op/contrib/bitcast_convert.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/cholesky.cc b/paddle/cinn/hlir/op/contrib/cholesky.cc index 1c6eb90374734..a1d43859c71bd 100644 --- a/paddle/cinn/hlir/op/contrib/cholesky.cc +++ b/paddle/cinn/hlir/op/contrib/cholesky.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/gather_nd.cc b/paddle/cinn/hlir/op/contrib/gather_nd.cc index a79bbeeb2ebb0..1a3f06330f6c7 100644 --- a/paddle/cinn/hlir/op/contrib/gather_nd.cc +++ b/paddle/cinn/hlir/op/contrib/gather_nd.cc @@ -14,8 +14,6 @@ #include "paddle/cinn/hlir/op/contrib/gather_nd.h" -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/gaussian_random.cc b/paddle/cinn/hlir/op/contrib/gaussian_random.cc index d20e7d26f0e44..1d70e4098e721 100644 --- a/paddle/cinn/hlir/op/contrib/gaussian_random.cc +++ b/paddle/cinn/hlir/op/contrib/gaussian_random.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/logical_right_shift.cc b/paddle/cinn/hlir/op/contrib/logical_right_shift.cc index 10626ae915f86..f4223a2d79412 100644 --- a/paddle/cinn/hlir/op/contrib/logical_right_shift.cc +++ b/paddle/cinn/hlir/op/contrib/logical_right_shift.cc @@ -17,7 +17,6 @@ #include #include -#include "gflags/gflags.h" #include "paddle/cinn/common/cas.h" #include "paddle/cinn/common/common.h" #include "paddle/cinn/common/context.h" @@ -37,6 +36,7 @@ #include "paddle/cinn/ir/tensor.h" #include "paddle/cinn/lang/builtin.h" #include "paddle/cinn/lang/compute.h" +#include "paddle/utils/flags.h" namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/op/contrib/lookup_table.cc b/paddle/cinn/hlir/op/contrib/lookup_table.cc index 4fdd854ec382d..2796d45e016b9 100644 --- a/paddle/cinn/hlir/op/contrib/lookup_table.cc +++ b/paddle/cinn/hlir/op/contrib/lookup_table.cc @@ -19,7 +19,6 @@ #include #include -#include "gflags/gflags.h" #include "paddle/cinn/common/cas.h" #include "paddle/cinn/common/common.h" #include "paddle/cinn/common/context.h" @@ -38,6 +37,7 @@ #include "paddle/cinn/ir/tensor.h" #include "paddle/cinn/lang/builtin.h" #include "paddle/cinn/lang/compute.h" +#include "paddle/utils/flags.h" namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/op/contrib/one_hot.cc b/paddle/cinn/hlir/op/contrib/one_hot.cc index a1bff0a5fa4e5..c7d0c3564da94 100644 --- a/paddle/cinn/hlir/op/contrib/one_hot.cc +++ b/paddle/cinn/hlir/op/contrib/one_hot.cc @@ -14,8 +14,6 @@ #include "paddle/cinn/hlir/op/contrib/one_hot.h" -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/randint.cc b/paddle/cinn/hlir/op/contrib/randint.cc index 260397ea6388c..335155fd5afad 100644 --- a/paddle/cinn/hlir/op/contrib/randint.cc +++ b/paddle/cinn/hlir/op/contrib/randint.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/reciprocal.cc b/paddle/cinn/hlir/op/contrib/reciprocal.cc index 8e73c5e8759ca..f9a71f7172b5b 100644 --- a/paddle/cinn/hlir/op/contrib/reciprocal.cc +++ b/paddle/cinn/hlir/op/contrib/reciprocal.cc @@ -17,7 +17,6 @@ #include #include -#include "gflags/gflags.h" #include "paddle/cinn/common/cas.h" #include "paddle/cinn/common/common.h" #include "paddle/cinn/common/context.h" @@ -37,6 +36,7 @@ #include "paddle/cinn/ir/tensor.h" #include "paddle/cinn/lang/builtin.h" #include "paddle/cinn/lang/compute.h" +#include "paddle/utils/flags.h" namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/op/contrib/repeat.cc b/paddle/cinn/hlir/op/contrib/repeat.cc index 266af46324e70..6f35a0f9b058c 100644 --- a/paddle/cinn/hlir/op/contrib/repeat.cc +++ b/paddle/cinn/hlir/op/contrib/repeat.cc @@ -14,8 +14,6 @@ #include "paddle/cinn/hlir/op/contrib/repeat.h" -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/resize.cc b/paddle/cinn/hlir/op/contrib/resize.cc index 2d113f562288c..786365d21dcd9 100644 --- a/paddle/cinn/hlir/op/contrib/resize.cc +++ b/paddle/cinn/hlir/op/contrib/resize.cc @@ -14,8 +14,6 @@ #include "paddle/cinn/hlir/op/contrib/resize.h" -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/sort.cc b/paddle/cinn/hlir/op/contrib/sort.cc index 778a966ed5aef..0941d2690b604 100644 --- a/paddle/cinn/hlir/op/contrib/sort.cc +++ b/paddle/cinn/hlir/op/contrib/sort.cc @@ -14,8 +14,6 @@ #include "paddle/cinn/hlir/op/contrib/sort.h" -#include - #include #include #include diff --git a/paddle/cinn/hlir/op/contrib/uniform_random.cc b/paddle/cinn/hlir/op/contrib/uniform_random.cc index 1b6a5b829cc21..cd034560f8feb 100644 --- a/paddle/cinn/hlir/op/contrib/uniform_random.cc +++ b/paddle/cinn/hlir/op/contrib/uniform_random.cc @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include diff --git a/paddle/cinn/hlir/pass/alterlayout_test.cc b/paddle/cinn/hlir/pass/alterlayout_test.cc index bcf0f9416e120..293c0c07ebdde 100644 --- a/paddle/cinn/hlir/pass/alterlayout_test.cc +++ b/paddle/cinn/hlir/pass/alterlayout_test.cc @@ -25,7 +25,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/hlir/pass/common_subexpression_elimination_test.cc b/paddle/cinn/hlir/pass/common_subexpression_elimination_test.cc index 3188a63289ec6..3e7417a9c29aa 100644 --- a/paddle/cinn/hlir/pass/common_subexpression_elimination_test.cc +++ b/paddle/cinn/hlir/pass/common_subexpression_elimination_test.cc @@ -37,7 +37,7 @@ #include "paddle/cinn/hlir/framework/pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/hlir/pass/const_propagate_test.cc b/paddle/cinn/hlir/pass/const_propagate_test.cc index 0ba2b5b8857b8..c1600c81aa5f9 100644 --- a/paddle/cinn/hlir/pass/const_propagate_test.cc +++ b/paddle/cinn/hlir/pass/const_propagate_test.cc @@ -25,7 +25,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/hlir/pass/custom_call_pass.cc b/paddle/cinn/hlir/pass/custom_call_pass.cc index 9990683788165..a1d8d9dd9b86f 100644 --- a/paddle/cinn/hlir/pass/custom_call_pass.cc +++ b/paddle/cinn/hlir/pass/custom_call_pass.cc @@ -17,7 +17,7 @@ #include "paddle/cinn/hlir/op/external_api_registry.h" #include "paddle/cinn/utils/string.h" -DECLARE_string(cinn_custom_call_deny_ops); +PD_DECLARE_string(cinn_custom_call_deny_ops); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/pass/fusion_merge_pass.cc b/paddle/cinn/hlir/pass/fusion_merge_pass.cc index da2cfe86a4c21..8d3233e23c706 100644 --- a/paddle/cinn/hlir/pass/fusion_merge_pass.cc +++ b/paddle/cinn/hlir/pass/fusion_merge_pass.cc @@ -14,7 +14,7 @@ #include "paddle/cinn/hlir/pass/fusion_merge_pass_util.h" -DECLARE_bool(enhance_vertical_fusion_with_recompute); +PD_DECLARE_bool(enhance_vertical_fusion_with_recompute); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/pass/general_fusion_merge_pass.cc b/paddle/cinn/hlir/pass/general_fusion_merge_pass.cc index 8274b1705e09b..6ea908ed31f31 100644 --- a/paddle/cinn/hlir/pass/general_fusion_merge_pass.cc +++ b/paddle/cinn/hlir/pass/general_fusion_merge_pass.cc @@ -26,7 +26,7 @@ #include "paddle/cinn/hlir/pass/general_fusion_merge_pass/lightware_fuse_pass_ctx.h" #include "paddle/cinn/hlir/pass/general_fusion_merge_pass_utils.h" -DECLARE_bool(enhance_vertical_fusion_with_recompute); +PD_DECLARE_bool(enhance_vertical_fusion_with_recompute); namespace cinn { namespace hlir { diff --git a/paddle/cinn/hlir/pass/opfusion_test.cc b/paddle/cinn/hlir/pass/opfusion_test.cc index 50846c80c3454..0dc87573cceb3 100644 --- a/paddle/cinn/hlir/pass/opfusion_test.cc +++ b/paddle/cinn/hlir/pass/opfusion_test.cc @@ -25,7 +25,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/hlir/pass/test_primitive_ops.cc b/paddle/cinn/hlir/pass/test_primitive_ops.cc index bd3bd19b0c71f..2b39b5c57f27b 100644 --- a/paddle/cinn/hlir/pass/test_primitive_ops.cc +++ b/paddle/cinn/hlir/pass/test_primitive_ops.cc @@ -26,7 +26,7 @@ #include "paddle/cinn/hlir/pass/use_pass.h" #include "paddle/cinn/utils/data_util.h" -DEFINE_string(model_dir, "", ""); +PD_DEFINE_string(model_dir, "", ""); namespace cinn { namespace frontend { diff --git a/paddle/cinn/hlir/pe/schedule.cc b/paddle/cinn/hlir/pe/schedule.cc index 45022f740d7eb..6e9cfe6d88756 100644 --- a/paddle/cinn/hlir/pe/schedule.cc +++ b/paddle/cinn/hlir/pe/schedule.cc @@ -31,7 +31,7 @@ #include "paddle/cinn/poly/isl_utils.h" #include "paddle/cinn/utils/string.h" -DECLARE_bool(cinn_use_cuda_vectorize); +PD_DECLARE_bool(cinn_use_cuda_vectorize); namespace cinn { namespace hlir { namespace pe { diff --git a/paddle/cinn/ir/schedule/ir_schedule.cc b/paddle/cinn/ir/schedule/ir_schedule.cc index 0c3632ffd22eb..78ce98564dbdc 100644 --- a/paddle/cinn/ir/schedule/ir_schedule.cc +++ b/paddle/cinn/ir/schedule/ir_schedule.cc @@ -41,7 +41,7 @@ #include "paddle/cinn/optim/replace_var_with_expr.h" #include "paddle/cinn/utils/string.h" -DECLARE_int32(cinn_error_message_level); +PD_DECLARE_int32(cinn_error_message_level); namespace cinn { namespace ir { diff --git a/paddle/cinn/runtime/custom_function.cc b/paddle/cinn/runtime/custom_function.cc index d04d00c2d3d72..dda7430b9e336 100644 --- a/paddle/cinn/runtime/custom_function.cc +++ b/paddle/cinn/runtime/custom_function.cc @@ -22,7 +22,7 @@ #include "paddle/cinn/runtime/flags.h" #include "paddle/cinn/utils/string.h" -DECLARE_string(cinn_check_fusion_accuracy_pass); +PD_DECLARE_string(cinn_check_fusion_accuracy_pass); namespace cinn { namespace runtime { diff --git a/paddle/cinn/runtime/flags.cc b/paddle/cinn/runtime/flags.cc index 91bf9d1f47cf7..3d3801fa675fb 100644 --- a/paddle/cinn/runtime/flags.cc +++ b/paddle/cinn/runtime/flags.cc @@ -14,7 +14,6 @@ #include "paddle/cinn/runtime/flags.h" -#include #include #include #include @@ -23,169 +22,182 @@ #include #include "paddle/cinn/common/target.h" +#include "paddle/utils/flags.h" #ifdef CINN_WITH_CUDNN -DEFINE_bool(cinn_cudnn_deterministic, - false, - "Whether allow using an autotuning algorithm for convolution " - "operator. The autotuning algorithm may be non-deterministic. If " - "true, the algorithm is deterministic."); +PD_DEFINE_bool( + cinn_cudnn_deterministic, + false, + "Whether allow using an autotuning algorithm for convolution " + "operator. The autotuning algorithm may be non-deterministic. If " + "true, the algorithm is deterministic."); #endif -using ::GFLAGS_NAMESPACE::BoolFromEnv; -using ::GFLAGS_NAMESPACE::DoubleFromEnv; -using ::GFLAGS_NAMESPACE::Int32FromEnv; -using ::GFLAGS_NAMESPACE::Int64FromEnv; -using ::GFLAGS_NAMESPACE::StringFromEnv; - -DEFINE_string(cinn_x86_builtin_code_root, - StringFromEnv("FLAGS_cinn_x86_builtin_code_root", ""), - ""); -DEFINE_string(cinn_nvcc_cmd_path, - StringFromEnv("FLAGS_cinn_nvcc_cmd_path", "/usr/local/cuda/bin"), - "Setting nvcc default path!"); - -DEFINE_int32(cinn_parallel_compile_thread, - Int32FromEnv("FLAGS_cinn_parallel_compile_thread", - (std::thread::hardware_concurrency() >> 1)), - "How much thread the parallel compile used."); - -DEFINE_bool(cinn_use_op_fusion, - BoolFromEnv("FLAGS_cinn_use_op_fusion", true), - "Whether to use op fusion pass."); - -DEFINE_bool(general_fusion_merge_pass, - BoolFromEnv("FLAGS_general_fusion_merge_pass", true), - "Whether to use general fusion_merge pass."); - -DEFINE_bool(cinn_use_common_subexpression_elimination, - BoolFromEnv("FLAGS_cinn_use_common_subexpression_elimination", - false), - "Whether to use common subexpression elimination pass."); - -DEFINE_string( +using ::paddle::flags::BoolFromEnv; +using ::paddle::flags::DoubleFromEnv; +using ::paddle::flags::Int32FromEnv; +using ::paddle::flags::Int64FromEnv; +using ::paddle::flags::StringFromEnv; + +PD_DEFINE_string(cinn_x86_builtin_code_root, + StringFromEnv("FLAGS_cinn_x86_builtin_code_root", ""), + ""); + +PD_DEFINE_string(cinn_nvcc_cmd_path, + StringFromEnv("FLAGS_cinn_nvcc_cmd_path", + "/usr/local/cuda/bin"), + "Setting nvcc default path!"); + +PD_DEFINE_int32(cinn_parallel_compile_thread, + Int32FromEnv("FLAGS_cinn_parallel_compile_thread", + (std::thread::hardware_concurrency() >> 1)), + "How much thread the parallel compile used."); + +PD_DEFINE_bool(cinn_use_op_fusion, + BoolFromEnv("FLAGS_cinn_use_op_fusion", true), + "Whether to use op fusion pass."); + +PD_DEFINE_bool(general_fusion_merge_pass, + BoolFromEnv("FLAGS_general_fusion_merge_pass", true), + "Whether to use general fusion_merge pass."); + +PD_DEFINE_bool(cinn_use_common_subexpression_elimination, + BoolFromEnv("FLAGS_cinn_use_common_subexpression_elimination", + false), + "Whether to use common subexpression elimination pass."); + +PD_DEFINE_string( cinn_custom_call_deny_ops, StringFromEnv("FLAGS_cinn_custom_call_deny_ops", ""), "a blacklist of op are denied by MarkCustomCallOps pass, separated by ;"); -DEFINE_bool(cinn_use_custom_call, - BoolFromEnv("FLAGS_cinn_use_custom_call", true), - "Whether to use custom_call for ops with external_api registered"); +PD_DEFINE_bool( + cinn_use_custom_call, + BoolFromEnv("FLAGS_cinn_use_custom_call", true), + "Whether to use custom_call for ops with external_api registered"); -DEFINE_bool(cinn_use_fill_constant_folding, - BoolFromEnv("FLAGS_cinn_use_fill_constant_folding", false), - "Whether use the FillConstantFolding pass."); +PD_DEFINE_bool(cinn_use_fill_constant_folding, + BoolFromEnv("FLAGS_cinn_use_fill_constant_folding", false), + "Whether use the FillConstantFolding pass."); -DEFINE_string(cinn_check_fusion_accuracy_pass, - StringFromEnv("FLAGS_cinn_check_fusion_accuracy_pass", ""), - "Check the correct of fusion kernels, if the results not " - "satisfied 'allclose(rtol=1e-05f, atol=1e-08f)', " - "report error and exited."); +PD_DEFINE_string(cinn_check_fusion_accuracy_pass, + StringFromEnv("FLAGS_cinn_check_fusion_accuracy_pass", ""), + "Check the correct of fusion kernels, if the results not " + "satisfied 'allclose(rtol=1e-05f, atol=1e-08f)', " + "report error and exited."); -DEFINE_bool(cinn_use_cuda_vectorize, - BoolFromEnv("FLAGS_cinn_use_cuda_vectorize", false), - "Whether use cuda vectroize on schedule config"); +PD_DEFINE_bool(cinn_use_cuda_vectorize, + BoolFromEnv("FLAGS_cinn_use_cuda_vectorize", false), + "Whether use cuda vectroize on schedule config"); -DEFINE_bool(use_reduce_split_pass, - BoolFromEnv("FLAGS_use_reduce_split_pass", false), - "Whether use reduce split pass."); +PD_DEFINE_bool(use_reduce_split_pass, + BoolFromEnv("FLAGS_use_reduce_split_pass", false), + "Whether use reduce split pass."); -DEFINE_bool(cinn_use_dense_merge_pass, - BoolFromEnv("FLAGS_cinn_use_dense_merge_pass", false), - "Whether use dense merge pass."); +PD_DEFINE_bool(cinn_use_dense_merge_pass, + BoolFromEnv("FLAGS_cinn_use_dense_merge_pass", false), + "Whether use dense merge pass."); -DEFINE_bool(nvrtc_compile_to_cubin, - BoolFromEnv("FLAGS_nvrtc_compile_to_cubin", false), - "Whether nvrtc compile cuda source into cubin instead of ptx (only " - "works after cuda-11.1)."); +PD_DEFINE_bool( + nvrtc_compile_to_cubin, + BoolFromEnv("FLAGS_nvrtc_compile_to_cubin", false), + "Whether nvrtc compile cuda source into cubin instead of ptx (only " + "works after cuda-11.1)."); -DEFINE_bool(cinn_compile_with_nvrtc, - BoolFromEnv("FLAGS_cinn_compile_with_nvrtc", true), - "Whether nvrtc compile cuda source with nvrtc(default nvcc)."); +PD_DEFINE_bool(cinn_compile_with_nvrtc, + BoolFromEnv("FLAGS_cinn_compile_with_nvrtc", true), + "Whether nvrtc compile cuda source with nvrtc(default nvcc)."); // FLAGS for performance analysis and accuracy debug -DEFINE_bool(cinn_sync_run, - BoolFromEnv("FLAGS_cinn_sync_run", false), - "Whether sync all devices after each instruction run, which is " - "used for debug."); - -DEFINE_string(cinn_self_check_accuracy, - StringFromEnv("FLAGS_cinn_self_check_accuracy", ""), - "Whether self-check accuracy after each instruction run, which " - "is used for debug."); - -DEFINE_int64(cinn_self_check_accuracy_num, - Int64FromEnv("FLAGS_cinn_self_check_accuracy_num", 0L), - "Set self-check accuracy print numel, which is used for debug."); - -DEFINE_string(cinn_fusion_groups_graphviz_dir, - StringFromEnv("FLAGS_cinn_fusion_groups_graphviz_dir", ""), - "Specify the directory path of dot file of graph, which is used " - "for debug."); - -DEFINE_string(cinn_source_code_save_path, - StringFromEnv("FLAGS_cinn_source_code_save_path", ""), - "Specify the directory path of generated source code, which is " - "used for debug."); - -DEFINE_string(cinn_dump_group_lowered_func, - StringFromEnv("FLAGS_cinn_dump_group_lowered_func", ""), - "Specify the path for dump lowered functions by group, which is " - "used for debug."); - -DEFINE_string( +PD_DEFINE_bool(cinn_sync_run, + BoolFromEnv("FLAGS_cinn_sync_run", false), + "Whether sync all devices after each instruction run, which is " + "used for debug."); + +PD_DEFINE_string( + cinn_self_check_accuracy, + StringFromEnv("FLAGS_cinn_self_check_accuracy", ""), + "Whether self-check accuracy after each instruction run, which " + "is used for debug."); + +PD_DEFINE_int64( + cinn_self_check_accuracy_num, + Int64FromEnv("FLAGS_cinn_self_check_accuracy_num", 0L), + "Set self-check accuracy print numel, which is used for debug."); + +PD_DEFINE_string( + cinn_fusion_groups_graphviz_dir, + StringFromEnv("FLAGS_cinn_fusion_groups_graphviz_dir", ""), + "Specify the directory path of dot file of graph, which is used " + "for debug."); + +PD_DEFINE_string( + cinn_source_code_save_path, + StringFromEnv("FLAGS_cinn_source_code_save_path", ""), + "Specify the directory path of generated source code, which is " + "used for debug."); + +PD_DEFINE_string( + cinn_dump_group_lowered_func, + StringFromEnv("FLAGS_cinn_dump_group_lowered_func", ""), + "Specify the path for dump lowered functions by group, which is " + "used for debug."); + +PD_DEFINE_string( cinn_dump_group_source_code, StringFromEnv("FLAGS_cinn_dump_group_source_code", ""), "Specify the path for dump source code by group, which is used for debug."); -DEFINE_string( +PD_DEFINE_string( cinn_dump_group_ptx, StringFromEnv("FLAGS_cinn_dump_group_ptx", ""), "Specify the path for dump ptx by group, which is used for debug."); -DEFINE_string( +PD_DEFINE_string( cinn_dump_group_instruction, StringFromEnv("FLAGS_cinn_dump_group_instruction", ""), "Specify the path for dump instruction by group, which is used for debug."); -DEFINE_string(cinn_pass_visualize_dir, - StringFromEnv("FLAGS_cinn_pass_visualize_dir", ""), - "Specify the directory path of pass visualize file of graph, " - "which is used for debug."); - -DEFINE_bool(enable_auto_tuner, - BoolFromEnv("FLAGS_enable_auto_tuner", false), - "Whether enable auto tuner."); - -DEFINE_bool(auto_schedule_use_cost_model, - BoolFromEnv("FLAGS_auto_schedule_use_cost_model", true), - "Whether to use cost model in auto schedule, this is an " - "on-developing flag and it will be removed when " - "cost model is stable."); - -DEFINE_bool(enhance_vertical_fusion_with_recompute, - BoolFromEnv("FLAGS_enhance_vertical_fusion_with_recompute", true), - "Whether to enhance check logic on vertical fusion with recompute"); - -DEFINE_bool(verbose_function_register, - BoolFromEnv("FLAGS_verbose_function_register", false), - "Whether to verbose function regist log. This will only work if " - "CINN build with flag -DWITH_DEBUG=ON."); - -DEFINE_int32(cinn_profiler_state, - Int32FromEnv("FLAGS_cinn_profiler_state", -1), - "Specify the ProfilerState by Int in CINN, 0 for kDisabled, 1 for " - "kCPU, 2 for kCUDA, 3 for kAll, default 0."); - -DEFINE_int32(cinn_error_message_level, - Int32FromEnv("FLAGS_cinn_error_message_level", 0), - "Specify the level of printing error message in the schedule." - "0 means short, 1 means detailed."); - -DEFINE_double(cinn_infer_model_version, - DoubleFromEnv("FLAGS_cinn_infer_model_version", 2.0), - "Paddle has different model format in inference model. We use " - "a flag to load different versions."); +PD_DEFINE_string(cinn_pass_visualize_dir, + StringFromEnv("FLAGS_cinn_pass_visualize_dir", ""), + "Specify the directory path of pass visualize file of graph, " + "which is used for debug."); + +PD_DEFINE_bool(enable_auto_tuner, + BoolFromEnv("FLAGS_enable_auto_tuner", false), + "Whether enable auto tuner."); + +PD_DEFINE_bool(auto_schedule_use_cost_model, + BoolFromEnv("FLAGS_auto_schedule_use_cost_model", true), + "Whether to use cost model in auto schedule, this is an " + "on-developing flag and it will be removed when " + "cost model is stable."); + +PD_DEFINE_bool( + enhance_vertical_fusion_with_recompute, + BoolFromEnv("FLAGS_enhance_vertical_fusion_with_recompute", true), + "Whether to enhance check logic on vertical fusion with recompute"); + +PD_DEFINE_bool(verbose_function_register, + BoolFromEnv("FLAGS_verbose_function_register", false), + "Whether to verbose function regist log. This will only work if " + "CINN build with flag -DWITH_DEBUG=ON."); + +PD_DEFINE_int32( + cinn_profiler_state, + Int32FromEnv("FLAGS_cinn_profiler_state", -1), + "Specify the ProfilerState by Int in CINN, 0 for kDisabled, 1 for " + "kCPU, 2 for kCUDA, 3 for kAll, default 0."); + +PD_DEFINE_int32(cinn_error_message_level, + Int32FromEnv("FLAGS_cinn_error_message_level", 0), + "Specify the level of printing error message in the schedule." + "0 means short, 1 means detailed."); + +PD_DEFINE_double(cinn_infer_model_version, + DoubleFromEnv("FLAGS_cinn_infer_model_version", 2.0), + "Paddle has different model format in inference model. We use " + "a flag to load different versions."); namespace cinn { namespace runtime { diff --git a/paddle/cinn/utils/profiler.cc b/paddle/cinn/utils/profiler.cc index 07ca00467aaa5..7ed75fd7f73fd 100644 --- a/paddle/cinn/utils/profiler.cc +++ b/paddle/cinn/utils/profiler.cc @@ -14,7 +14,7 @@ #include "paddle/cinn/utils/profiler.h" -#include +#include "paddle/utils/flags.h" #ifdef CINN_WITH_NVTX #include @@ -27,7 +27,7 @@ #endif #include -DECLARE_int32(cinn_profiler_state); +PD_DECLARE_int32(cinn_profiler_state); namespace cinn { namespace utils { diff --git a/paddle/utils/CMakeLists.txt b/paddle/utils/CMakeLists.txt index bb177e7578bb4..0bc36dd5578ea 100644 --- a/paddle/utils/CMakeLists.txt +++ b/paddle/utils/CMakeLists.txt @@ -1,3 +1,14 @@ +if(NOT WITH_GFLAGS) + cc_library(paddle_flags SRCS flags_native.cc) + if(CINN_ONLY) + return() + endif() + cc_test( + flags_native_test + SRCS flags_native_test.cc + DEPS paddle_flags) +endif() + add_subdirectory(string) cc_test( @@ -23,11 +34,3 @@ if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) SRCS pybind.cc DEPS phi) endif() - -if(NOT WITH_GFLAGS) - cc_library(paddle_flags SRCS flags_native.cc) - cc_test( - flags_native_test - SRCS flags_native_test.cc - DEPS paddle_flags) -endif() diff --git a/paddle/utils/flags.h b/paddle/utils/flags.h index 5a019f1439b43..3f68ba2f5dc1f 100644 --- a/paddle/utils/flags.h +++ b/paddle/utils/flags.h @@ -73,5 +73,31 @@ inline void AllowUndefinedFlags() { gflags::AllowCommandLineReparsing(); } using paddle::flags::AllowUndefinedFlags; #endif +#ifdef PADDLE_WITH_GFLAGS +using gflags::BoolFromEnv; +using gflags::DoubleFromEnv; +using gflags::Int32FromEnv; +using gflags::Int64FromEnv; +using gflags::StringFromEnv; +using gflags::UInt32FromEnv; +using gflags::UInt64FromEnv; +#else +#define DEFINE_FROM_ENV_FUNC(type, name) \ + inline type name##FromEnv(const std::string& env_var_name, \ + type default_val) { \ + return GetFromEnv(env_var_name, default_val); \ + } + +DEFINE_FROM_ENV_FUNC(bool, Bool); +DEFINE_FROM_ENV_FUNC(int32_t, Int32); +DEFINE_FROM_ENV_FUNC(uint32_t, UInt32); +DEFINE_FROM_ENV_FUNC(int64_t, Int64); +DEFINE_FROM_ENV_FUNC(uint64_t, UInt64); +DEFINE_FROM_ENV_FUNC(double, Double); +DEFINE_FROM_ENV_FUNC(std::string, String); + +#undef DEFINE_FROM_ENV_FUNC +#endif + } // namespace flags } // namespace paddle diff --git a/paddle/utils/flags_native.cc b/paddle/utils/flags_native.cc index 05d90d8adf21c..ea7445f7144dc 100644 --- a/paddle/utils/flags_native.cc +++ b/paddle/utils/flags_native.cc @@ -393,6 +393,10 @@ void ParseCommandLineFlags(int* pargc, char*** pargv) { for (size_t i = 0; i < argv_num; i++) { const std::string& argv = argvs[i]; + if (argv.empty()) { + continue; + } + if (argv.size() < 2 || argv[0] != '-') { LOG_FLAG_FATAL_ERROR("invalid commandline argument: \"" + argv + "\", " + arg_format_help); @@ -480,5 +484,42 @@ void ParseCommandLineFlags(int* pargc, char*** pargv) { } } +template +T GetFromEnv(const std::string& name, const T& default_val) { + std::string value_str; + if (GetValueFromEnv(name, &value_str)) { + T value; + FlagType type = FlagTypeTraits::Type; + Flag flag("tmp_" + name, "", "", type, &value, &value); + flag.SetValueFromString(value_str); + if (!ErrorStream().str().empty()) { + ErrorStream().str(""); + LOG_FLAG_FATAL_ERROR("value \"" + value_str + + "\" of environment" + "variable \"" + + name + + "\" is invalid when " + "using GetFromEnv with " + + FlagType2String(type) + " type."); + } + return value; + } else { + return default_val; + } +} + +#define INSTANTIATE_GET_FROM_ENV(type) \ + template type GetFromEnv(const std::string& name, const type& default_val) + +INSTANTIATE_GET_FROM_ENV(bool); +INSTANTIATE_GET_FROM_ENV(int32_t); +INSTANTIATE_GET_FROM_ENV(uint32_t); +INSTANTIATE_GET_FROM_ENV(int64_t); +INSTANTIATE_GET_FROM_ENV(uint64_t); +INSTANTIATE_GET_FROM_ENV(double); +INSTANTIATE_GET_FROM_ENV(std::string); + +#undef INSTANTIATE_GET_FROM_ENV + } // namespace flags } // namespace paddle diff --git a/paddle/utils/flags_native.h b/paddle/utils/flags_native.h index abff8a486ecac..1f403e56e7faa 100644 --- a/paddle/utils/flags_native.h +++ b/paddle/utils/flags_native.h @@ -69,6 +69,12 @@ bool FindFlag(const std::string& name); */ void PrintAllFlagHelp(bool to_file = false, const std::string& file_name = "all_flags.txt"); + +/** + * @brief Get environment variable. If not found, return default value. + */ +template +T GetFromEnv(const std::string& name, const T& default_val); } // namespace flags } // namespace paddle diff --git a/paddle/utils/flags_native_test.cc b/paddle/utils/flags_native_test.cc index 2b26a8cd1e9db..1c684f168fba7 100644 --- a/paddle/utils/flags_native_test.cc +++ b/paddle/utils/flags_native_test.cc @@ -34,6 +34,7 @@ void SplitCommandlineArg(const std::string& commandline, end_pos = commandline.find(' ', start_pos); args.push_back(commandline.substr(start_pos, end_pos - start_pos)); } + args.push_back(""); // test empty argument *argc = args.size(); *argv = new char*[*argc]; for (size_t i = 0; i < args.size(); i++) { @@ -88,6 +89,11 @@ TEST(flags_native_test, SetFlagsFromEnv) { ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_bool", "true")); ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_double", "2.71")); + // test GetFromEnv + ASSERT_EQ(GetFromEnv("FLAGS_paddle_test_env_bool", false), true); + ASSERT_EQ(GetFromEnv("FLAGS_int32_not_defined", 34), 34); + + // test SetFlagsFromEnv std::string commandline = "test --fromenv=paddle_test_env_bool,paddle_test_env_double"; int argc; From 323ef25247c915e3cd4eb803e548e53d481551c3 Mon Sep 17 00:00:00 2001 From: tianshuo78520a <707759223@qq.com> Date: Tue, 5 Sep 2023 14:10:07 +0800 Subject: [PATCH 119/194] Fix openblas (#56746) * Fix openblas * fix bug * Fix * Fix log * Fix log * Fix * Fix --- cmake/external/openblas.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake index 6f9f6a11ab2e4..267f8d733cbd4 100644 --- a/cmake/external/openblas.cmake +++ b/cmake/external/openblas.cmake @@ -18,7 +18,6 @@ set(CBLAS_PREFIX_DIR ${THIRD_PARTY_PATH}/openblas) set(CBLAS_INSTALL_DIR ${THIRD_PARTY_PATH}/install/openblas) set(CBLAS_SOURCE_DIR ${PADDLE_SOURCE_DIR}/third_party/openblas) set(CBLAS_TAG v0.3.7) -set(CMAKE_VERBOSE_MAKEFILE 1) # OpenBLAS support Raptor Lake from v0.3.22 if(UNIX @@ -90,7 +89,7 @@ if(NOT WIN32) PREFIX ${CBLAS_PREFIX_DIR} INSTALL_DIR ${CBLAS_INSTALL_DIR} BUILD_IN_SOURCE 1 - BUILD_COMMAND make ${ARM_ARGS} -j${NPROC} ${COMMON_ARGS} ${OPTIONAL_ARGS} + BUILD_COMMAND make ${ARM_ARGS} -s -j${NPROC} ${COMMON_ARGS} ${OPTIONAL_ARGS} INSTALL_COMMAND make install NO_SHARED=1 NO_LAPACK=1 PREFIX= UPDATE_COMMAND "" CONFIGURE_COMMAND "" @@ -118,6 +117,7 @@ else() -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE} -DBUILD_SHARED_LIBS=ON + -DCMAKE_VERBOSE_MAKEFILE=OFF -DMSVC_STATIC_CRT=${MSVC_STATIC_CRT} ${EXTERNAL_OPTIONAL_ARGS} CMAKE_CACHE_ARGS From b196e076e96a78a394d030016b84f02df6664ae9 Mon Sep 17 00:00:00 2001 From: tianshuo78520a <707759223@qq.com> Date: Tue, 5 Sep 2023 14:11:29 +0800 Subject: [PATCH 120/194] Fix build python310;test=document_fix (#56949) --- paddle/scripts/paddle_build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index 286909b73e263..3dc17f2979994 100644 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -1025,7 +1025,7 @@ function generate_upstream_develop_api_spec() { git log --pretty=oneline -10 dev_commit=`git log -1|head -1|awk '{print $2}'` - dev_url="https://xly-devops.bj.bcebos.com/PR/build_whl/0/${dev_commit}/paddlepaddle_gpu-0.0.0-cp37-cp37m-linux_x86_64.whl" + dev_url="https://xly-devops.bj.bcebos.com/PR/build_whl/0/${dev_commit}/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl" url_return=`curl -s -m 5 -IL ${dev_url} |awk 'NR==1{print $2}'` if [ "$url_return" == '200' ];then echo "wget develop whl from bos! " From 54b247b132dd897ab3033aa9268562e436ad8c52 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:27:55 +0800 Subject: [PATCH 121/194] [Fluid] move lars_momentum_xpu to phi (#56751) * [Fluid] move lars_momentum_xpu to phi * Empty-Commit;test=kunlun; --- .../optimizers/lars_momentum_op_xpu.cc | 125 ------------------ .../phi/kernels/xpu/lars_momentum_kernel.cc | 113 ++++++++++++++++ 2 files changed, 113 insertions(+), 125 deletions(-) delete mode 100644 paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc create mode 100644 paddle/phi/kernels/xpu/lars_momentum_kernel.cc diff --git a/paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc b/paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc deleted file mode 100644 index 266ce2e57ca79..0000000000000 --- a/paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc +++ /dev/null @@ -1,125 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -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. */ - -#ifdef PADDLE_WITH_XPU -#include "paddle/fluid/framework/eigen.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/backends/xpu/enforce_xpu.h" - -namespace paddle { -namespace operators { - -template -class LarsMomentumOpXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - bool multi_precision = ctx.Attr("multi_precision"); - auto param_out = ctx.MultiOutput("ParamOut"); - auto velocity_out = ctx.MultiOutput("VelocityOut"); - auto param = ctx.MultiInput("Param"); - auto velocity = ctx.MultiInput("Velocity"); - auto learning_rate = ctx.MultiInput("LearningRate"); - auto grad = ctx.MultiInput("Grad"); - auto weight_decay_arr = ctx.Attr>("lars_weight_decay"); - auto master_param = ctx.MultiInput("MasterParam"); - auto master_param_out = ctx.MultiOutput("MasterParamOut"); - float mu = static_cast(ctx.Attr("mu")); - float lars_coeff = ctx.Attr("lars_coeff"); - float epsilon = ctx.Attr("epsilon"); - float rescale_grad = ctx.Attr("rescale_grad"); - - std::vector param_list; - std::vector grad_list; - std::vector param_out_list; - std::vector velocity_list; - std::vector velocity_out_list; - std::vector lrs; - std::vector param_sizes; - - std::vector master_param_list; - std::vector master_param_out_list; - int op_num = param.size(); - for (int i = 0; i < op_num; ++i) { - param_list.push_back( - reinterpret_cast(const_cast((param[i]->data())))); - grad_list.push_back( - reinterpret_cast(const_cast(grad[i]->data()))); - param_out_list.push_back(reinterpret_cast( - param_out[i]->mutable_data(ctx.GetPlace()))); - velocity_list.push_back(const_cast(velocity[i]->data())); - velocity_out_list.push_back( - velocity_out[i]->mutable_data(ctx.GetPlace())); - lrs.push_back(const_cast(learning_rate[i]->data())); - param_sizes.push_back(param[i]->numel()); - - PADDLE_ENFORCE_EQ( - param_list[i], - param_out_list[i], - platform::errors::InvalidArgument( - "Input(Param) and Output(ParamOut) must be the same Tensors.")); - PADDLE_ENFORCE_EQ(velocity_list[i], - velocity_out_list[i], - platform::errors::InvalidArgument( - "Input(Velocity) and Output(VelocityOut) must be " - "the same Tensors.")); - if (multi_precision) { - master_param_list.push_back( - const_cast(master_param[i]->data())); - master_param_out_list.push_back( - master_param_out[i]->mutable_data(ctx.GetPlace())); - PADDLE_ENFORCE_EQ(master_param_list[i], - master_param_out_list[i], - platform::errors::InvalidArgument( - "Input(MasterParam) and Output(MasterParamOut) " - "must be the same Tensors.")); - } else { - master_param_list.push_back(nullptr); - master_param_out_list.push_back(nullptr); - } - } - - auto& dev_ctx = ctx.template device_context(); - int r = lars_momentum(dev_ctx.x_context(), - param_list, - grad_list, - velocity_list, - lrs, - master_param_list, - param_out_list, - velocity_out_list, - master_param_out_list, - weight_decay_arr, - param_sizes, - mu, - lars_coeff, - epsilon, - rescale_grad); - PADDLE_ENFORCE_XDNN_SUCCESS(r, "lars_momentum"); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; -PD_REGISTER_STRUCT_KERNEL(lars_momentum, - XPU, - ALL_LAYOUT, - ops::LarsMomentumOpXPUKernel, - float, - plat::float16) {} -#endif diff --git a/paddle/phi/kernels/xpu/lars_momentum_kernel.cc b/paddle/phi/kernels/xpu/lars_momentum_kernel.cc new file mode 100644 index 0000000000000..8821a27f6137f --- /dev/null +++ b/paddle/phi/kernels/xpu/lars_momentum_kernel.cc @@ -0,0 +1,113 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/phi/kernels/lars_momentum_kernel.h" +#include "paddle/phi/backends/xpu/enforce_xpu.h" +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void LarsMomentumKernel( + const Context& dev_ctx, + const std::vector& param, + const std::vector& velocity, + const std::vector& learning_rate, + const std::vector& grad, + const paddle::optional>& master_param, + const std::vector& weight_decay_arr, + float mu, + float lars_coeff, + float epsilon, + bool multi_precision, + float rescale_grad, + std::vector param_out, + std::vector velocity_out, + std::vector master_param_out) { + using XPUType = typename XPUTypeTrait::Type; + std::vector param_list; + std::vector grad_list; + std::vector param_out_list; + std::vector velocity_list; + std::vector velocity_out_list; + std::vector lrs; + std::vector param_sizes; + + std::vector master_param_list; + std::vector master_param_out_list; + int op_num = param.size(); + for (int i = 0; i < op_num; ++i) { + param_list.push_back( + reinterpret_cast(const_cast((param[i]->data())))); + grad_list.push_back( + reinterpret_cast(const_cast(grad[i]->data()))); + param_out_list.push_back( + reinterpret_cast(dev_ctx.template Alloc(param_out[i]))); + velocity_list.push_back(const_cast(velocity[i]->data())); + velocity_out_list.push_back(dev_ctx.template Alloc(velocity_out[i])); + lrs.push_back(const_cast(learning_rate[i]->data())); + param_sizes.push_back(param[i]->numel()); + + PADDLE_ENFORCE_EQ( + param_list[i], + param_out_list[i], + phi::errors::InvalidArgument( + "Input(Param) and Output(ParamOut) must be the same Tensors.")); + PADDLE_ENFORCE_EQ(velocity_list[i], + velocity_out_list[i], + phi::errors::InvalidArgument( + "Input(Velocity) and Output(VelocityOut) must be " + "the same Tensors.")); + if (multi_precision) { + master_param_list.push_back( + const_cast(master_param.get()[i]->data())); + master_param_out_list.push_back( + dev_ctx.template Alloc(master_param_out[i])); + PADDLE_ENFORCE_EQ(master_param_list[i], + master_param_out_list[i], + phi::errors::InvalidArgument( + "Input(MasterParam) and Output(MasterParamOut) " + "must be the same Tensors.")); + } else { + master_param_list.push_back(nullptr); + master_param_out_list.push_back(nullptr); + } + } + + int r = lars_momentum(dev_ctx.x_context(), + param_list, + grad_list, + velocity_list, + lrs, + master_param_list, + param_out_list, + velocity_out_list, + master_param_out_list, + weight_decay_arr, + param_sizes, + mu, + lars_coeff, + epsilon, + rescale_grad); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "lars_momentum"); +} +} // namespace phi + +PD_REGISTER_KERNEL(lars_momentum, + XPU, + ALL_LAYOUT, + phi::LarsMomentumKernel, + float, + phi::dtype::float16) {} From e9e07a19b6bc0fcfc16ef3e892a558ebf78e05c5 Mon Sep 17 00:00:00 2001 From: Wennie396 <44974020+Wennie396@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:30:58 +0800 Subject: [PATCH 122/194] fix some bugs for amp and test case test_tuning_recompute_with_amp.py (#56864) * replace amp.use_pure_fp16 with amp.dtype and amp.level * old api still use use_pure_fp16 * test_fuse_adamw_pass still use use_pure_fp16 * add test case tuning recompute with amp(float16,o2) * reset new test case properties TIMEOUT 60 * set smaller value of batch_size and batch_num * deepcopy dist_context fix _rename_input problem * fix loss name after cast * set tuning.enable=True and use engine._tune() * restore some changes in _rename_input()/_rename_output() * add self.amp_dtype for _cast_loss() in auto_parallel_amp.py * fix insert op index in _cast_loss() --- .../static/tuner/optimization_tuner.py | 2 +- .../distributed/passes/auto_parallel_amp.py | 26 ++-- .../distributed/passes/auto_parallel_fp16.py | 7 +- test/auto_parallel/CMakeLists.txt | 3 + test/auto_parallel/test_fp16_assign.py | 3 +- .../test_tuning_recompute_with_amp.py | 113 ++++++++++++++++++ 6 files changed, 141 insertions(+), 13 deletions(-) create mode 100644 test/auto_parallel/test_tuning_recompute_with_amp.py diff --git a/python/paddle/distributed/auto_parallel/static/tuner/optimization_tuner.py b/python/paddle/distributed/auto_parallel/static/tuner/optimization_tuner.py index 890e5fd2be57c..64eaca28c06ea 100644 --- a/python/paddle/distributed/auto_parallel/static/tuner/optimization_tuner.py +++ b/python/paddle/distributed/auto_parallel/static/tuner/optimization_tuner.py @@ -308,7 +308,7 @@ def _apply_optimization(self, trial): self._baseline_dist_context.serial_feed_vars["inputs"] + self._baseline_dist_context.serial_feed_vars["labels"] ) - if config["use_pure_fp16"]: + if config["dtype"] == "float16" and config["level"] == "o2": config["base_opt"] = dist_context.serial_optimizer auto_parallel_fp16_pass = new_pass("auto_parallel_fp16", config) auto_parallel_fp16_pass.apply( diff --git a/python/paddle/distributed/passes/auto_parallel_amp.py b/python/paddle/distributed/passes/auto_parallel_amp.py index cb875f63e3d25..34ab1c29534a9 100644 --- a/python/paddle/distributed/passes/auto_parallel_amp.py +++ b/python/paddle/distributed/passes/auto_parallel_amp.py @@ -728,7 +728,7 @@ def _apply_single_impl(self, main_program, startup_program, context): if is_train: self._update_backward_cast_ops() - self._cast_loss() + self._cast_loss(self.amp_dtype) if is_train and self.amp_dtype == "float16": self._init_amp_var() @@ -913,7 +913,7 @@ def _init_amp_var(self): world_process_group.ranks, ) - def _cast_loss(self): + def _cast_loss(self, target_dtype): main_block = paddle.static.default_main_program().global_block() main_block._sync_with_cpp() @@ -957,11 +957,18 @@ def _cast_loss(self): ) # backward - first_backward_op = main_block.ops[loss_op_idx + 2] - assert ( - first_backward_op.type == "fill_constant" - and int(first_backward_op.all_attrs()[OP_ROLE_KEY]) == 257 - ) + first_backward_op = None + insert_op_offset = 3 + for idx, op in enumerate(main_block.ops[loss_op_idx:]): + if op.type == "fill_constant" and is_loss_grad_op(op): + first_backward_op = op + insert_op_offset = idx + 1 + break + if is_backward_op(op): + break + + assert first_backward_op is not None, "There is not loss_grad op." + cast_loss_grad = main_block.create_var( name=unique_name.generate(tmp_name + "@GRAD"), shape=loss.shape, @@ -984,13 +991,13 @@ def _cast_loss(self): self.dist_context, ) cast_grad_op = main_block._insert_op( - loss_op_idx + 3, + loss_op_idx + insert_op_offset, type='cast', inputs={'X': [cast_loss_grad]}, outputs={'Out': [pre_grad_name]}, attrs={ "in_dtype": core.VarDesc.VarType.FP32, - "out_dtype": _str_to_dtype(self.amp_dtype), + "out_dtype": _str_to_dtype(target_dtype), "op_role": OpRole.Backward, }, ) @@ -1002,6 +1009,7 @@ def _cast_loss(self): ) loss_op = cast_op loss = cast_loss + self.set_attr("loss", loss) self._loss = loss main_block._sync_with_cpp() diff --git a/python/paddle/distributed/passes/auto_parallel_fp16.py b/python/paddle/distributed/passes/auto_parallel_fp16.py index fc1c5bd9807a8..c9a0f772db5ce 100644 --- a/python/paddle/distributed/passes/auto_parallel_fp16.py +++ b/python/paddle/distributed/passes/auto_parallel_fp16.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import copy from collections import defaultdict import paddle @@ -408,8 +409,8 @@ def _insert_forward_cast_ops( (cast_name, in_var.name, dst_dtype, src_dtype, in_name) ] - in_var_dist_attr = consume_op_attr.get_input_dist_attr( - in_var.name + in_var_dist_attr = copy.deepcopy( + consume_op_attr.get_input_dist_attr(in_var.name) ) assert in_var_dist_attr is not None # truly insert cast op @@ -800,6 +801,8 @@ def _apply_single_impl(self, main_program, startup_program, context): is_train = fp16_state._build_state() cast_startup_program() + if is_train: + self._cast_loss(self.target_dtype) if is_train: if self.target_dtype == "float16": diff --git a/test/auto_parallel/CMakeLists.txt b/test/auto_parallel/CMakeLists.txt index 458c273951e05..aeb00a0fc7296 100644 --- a/test/auto_parallel/CMakeLists.txt +++ b/test/auto_parallel/CMakeLists.txt @@ -105,6 +105,9 @@ if(WITH_DISTRIBUTE AND WITH_GPU) set_tests_properties(test_selective_recompute PROPERTIES TIMEOUT 50) py_test_modules(test_tuning_recompute MODULES test_tuning_recompute) set_tests_properties(test_tuning_recompute PROPERTIES TIMEOUT 300) + py_test_modules(test_tuning_recompute_with_amp MODULES + test_tuning_recompute_with_amp) + set_tests_properties(test_tuning_recompute_with_amp PROPERTIES TIMEOUT 60) py_test_modules(test_fused_linear_pass MODULES test_fused_linear_pass) set_tests_properties(test_fused_linear_pass PROPERTIES TIMEOUT 40) py_test_modules(test_align_tool MODULES test_align_tool) diff --git a/test/auto_parallel/test_fp16_assign.py b/test/auto_parallel/test_fp16_assign.py index 5eabd0501ac56..a7257cb025470 100644 --- a/test/auto_parallel/test_fp16_assign.py +++ b/test/auto_parallel/test_fp16_assign.py @@ -80,7 +80,8 @@ def parallelizer(program_func, rank): strategy = auto.Strategy() amp = strategy.amp amp.enable = True - amp.use_pure_fp16 = True + amp.dtype = "float16" + amp.level = "o2" amp.init_loss_scaling = 32768 amp.use_fp16_guard = False amp.custom_black_list = ['where'] diff --git a/test/auto_parallel/test_tuning_recompute_with_amp.py b/test/auto_parallel/test_tuning_recompute_with_amp.py new file mode 100644 index 0000000000000..a7f093ab0545c --- /dev/null +++ b/test/auto_parallel/test_tuning_recompute_with_amp.py @@ -0,0 +1,113 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import sys +import unittest + +from get_gpt_model import FakeDataset + +import paddle +from paddle.distributed.fleet import auto + +sys.path.append("../legacy_test") +import auto_parallel_gpt_model as modeling +from auto_parallel_gpt_model import ( + GPTForPretraining, + GPTModel, + GPTPretrainingCriterion, +) + +paddle.enable_static() + + +def generate_model(): + modeling.init_global() + modeling._global_parallel_strategy = "serial" + ranks = list(range(paddle.distributed.get_world_size())) + modeling._global_process_mesh = auto.ProcessMesh( + mesh=ranks, dim_names=["x"] + ) + + gpt = GPTModel( + vocab_size=50304, + hidden_size=1024, + num_hidden_layers=8, + num_attention_heads=16, + intermediate_size=1024 * 4, + hidden_act="gelu", + hidden_dropout_prob=0.1, + attention_probs_dropout_prob=0.1, + max_position_embeddings=1024, + type_vocab_size=1, + initializer_range=0.02, + pad_token_id=0, + eos_token_id=7, + bos_token_id=0, + eol_token_id=3, + use_new_recompute=True, + recompute_granularity="full", + ) + model = GPTForPretraining( + gpt, vocab_size=50304, hidden_size=1024, initializer_range=0.02 + ) + criterion = GPTPretrainingCriterion() + return model, criterion + + +def apply_pass(): + strategy = auto.Strategy() + strategy.auto_mode = "semi" + + recompute = strategy.recompute + recompute.enable = True + recompute.enable_tuning = True + + tuning = strategy.tuning + tuning.enable = True + tuning.profile_start_step = 1 + tuning.profile_end_step = 2 + tuning.run_after_tuning = True + tuning.verbose = True + + amp = strategy.amp + amp.enable = True + amp.dtype = "float16" + amp.level = "o2" + + return strategy + + +class TestRecomputeWithAMPPassTuning(unittest.TestCase): + def setUp(self): + self.batch_size = 2 + self.batch_num = 10 + self.dataset = FakeDataset( + self.batch_size * self.batch_num, + vocab_size=50304, + sequence_len=1024, + ) + + def test_recompute_with_amp_pass(self): + strategy = apply_pass() + clip = paddle.nn.ClipGradByGlobalNorm(0.2) + opt = paddle.optimizer.AdamW(learning_rate=0.00001, grad_clip=clip) + model, loss = generate_model() + + engine = auto.Engine(model, loss, opt, strategy=strategy) + # engine.fit(self.dataset, 3, batch_size=self.batch_size) + engine._tune(self.dataset, 3, batch_size=self.batch_size) + + +if __name__ == "__main__": + unittest.main() From c2f0e9c4e965a5b28d624e0c357afb5aaca37dd2 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:31:41 +0800 Subject: [PATCH 123/194] [clang-tidy] NO.8 enable `cppcoreguidelines-narrowing-conversions`. step:2 (#56895) * [clang-tidy] replenish cppcoreguidelines-narrowing-conversions * fix * fix --- .../distributed/auto_parallel/dist_attr.cc | 4 +- .../auto_parallel/spmd_rules/common.cc | 2 +- .../cross_entropy_with_softmax_spmd_rule.cc | 2 +- .../auto_parallel/spmd_rules/dim_trans.cc | 27 ++-- .../spmd_rules/elementwise_spmd_rule.cc | 6 +- .../spmd_rules/embedding_spmd_rule.cc | 8 +- .../spmd_rules/layer_norm_spmd_rule.cc | 6 +- .../spmd_rules/reduction_spmd_rule.cc | 4 +- .../spmd_rules/reshape_spmd_rule.cc | 14 +- .../spmd_rules/softmax_spmd_rule.cc | 2 +- .../spmd_rules/split_spmd_rule.cc | 6 +- .../spmd_rules/transpose_spmd_rule.cc | 6 +- .../fluid/distributed/collective/reducer.cc | 9 +- .../distributed/fleet_executor/carrier.cc | 5 +- .../distributed/fleet_executor/dist_model.cc | 13 +- .../distributed/fleet_executor/dist_model.h | 2 +- paddle/fluid/framework/version.cc | 9 +- .../auto_growth_best_fit_allocator.cc | 11 +- .../fluid/memory/allocation/mmap_allocator.cc | 3 +- paddle/fluid/platform/init.cc | 2 +- paddle/fluid/platform/profiler.cc | 3 +- .../platform/profiler/chrometracing_logger.cc | 3 +- .../profiler/dump/deserialization_reader.cc | 2 +- paddle/fluid/platform/timer.cc | 4 +- .../auto_parallel/r_to_s_reshard_function.cc | 2 +- .../auto_parallel/s_to_r_reshard_function.cc | 9 +- paddle/phi/infermeta/backward.cc | 12 +- paddle/phi/infermeta/binary.cc | 89 ++++++------ paddle/phi/infermeta/fusion.cc | 38 ++--- paddle/phi/infermeta/multiary.cc | 93 ++++++------ paddle/phi/infermeta/sparse/binary.cc | 8 +- paddle/phi/infermeta/ternary.cc | 12 +- paddle/phi/infermeta/unary.cc | 136 +++++++++--------- .../phi/kernels/cpu/lars_momentum_kernel.cc | 2 +- paddle/phi/kernels/cpu/prelu_kernel.cc | 10 +- paddle/phi/kernels/cpu/prior_box_kernel.cc | 7 +- .../phi/kernels/cpu/psroi_pool_grad_kernel.cc | 14 +- paddle/phi/kernels/cpu/psroi_pool_kernel.cc | 30 ++-- paddle/phi/kernels/cpu/qr_kernel.cc | 6 +- paddle/phi/kernels/cpu/rnn_grad_kernel.cc | 20 +-- paddle/phi/kernels/cpu/rnn_kernel.cc | 20 +-- .../phi/kernels/cpu/roi_align_grad_kernel.cc | 8 +- paddle/phi/kernels/cpu/roi_align_kernel.cc | 16 +-- .../phi/kernels/cpu/roi_pool_grad_kernel.cc | 8 +- paddle/phi/kernels/cpu/roi_pool_kernel.cc | 16 +-- paddle/phi/kernels/cpu/rrelu_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/rrelu_kernel.cc | 2 +- .../kernels/cpu/send_u_recv_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/send_u_recv_kernel.cc | 2 +- .../kernels/cpu/send_ue_recv_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/send_ue_recv_kernel.cc | 2 +- paddle/phi/kernels/cpu/send_uv_kernel.cc | 2 +- paddle/phi/kernels/cpu/sgd_kernel.cc | 5 +- .../kernels/cpu/shuffle_batch_grad_kernel.cc | 4 +- .../phi/kernels/cpu/shuffle_batch_kernel.cc | 11 +- ...d_cross_entropy_with_logits_grad_kernel.cc | 2 +- ...igmoid_cross_entropy_with_logits_kernel.cc | 2 +- paddle/phi/kernels/cpu/stack_grad_kernel.cc | 6 +- paddle/phi/kernels/cpu/stack_kernel.cc | 4 +- paddle/phi/kernels/cpu/svd_kernel.cc | 6 +- .../kernels/cpu/temporal_shift_grad_kernel.cc | 21 +-- .../phi/kernels/cpu/temporal_shift_kernel.cc | 18 +-- paddle/phi/kernels/cpu/top_k_grad_kernel.cc | 4 +- paddle/phi/kernels/cpu/top_k_kernel.cc | 6 +- paddle/phi/kernels/cpu/transpose_kernel.cc | 4 +- .../kernels/cpu/triangular_solve_kernel.cc | 6 +- paddle/phi/kernels/cpu/trunc_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/unpool_grad_kernel.cc | 28 ++-- paddle/phi/kernels/cpu/unpool_kernel.cc | 28 ++-- .../cpu/weighted_sample_neighbors_kernel.cc | 2 +- paddle/phi/kernels/cpu/yolo_box_kernel.cc | 12 +- .../phi/kernels/cpu/yolo_loss_grad_kernel.cc | 12 +- paddle/phi/kernels/cpu/yolo_loss_kernel.cc | 16 +-- paddle/phi/kernels/funcs/jit/benchmark.cc | 4 +- .../kernels/selected_rows/cpu/adam_kernel.cc | 6 +- .../hsigmoid_loss_grad_kernel.cc | 2 +- .../phi/kernels/sparse/cpu/coalesce_kernel.cc | 2 +- .../kernels/sparse/cpu/conv_grad_kernel.cc | 10 +- paddle/phi/kernels/sparse/cpu/conv_kernel.cc | 15 +- .../sparse/cpu/elementwise_grad_kernel.cc | 4 +- paddle/phi/kernels/sparse/cpu/mask_kernel.cc | 2 +- .../kernels/sparse/cpu/pool_grad_kernel.cc | 4 +- paddle/phi/kernels/sparse/cpu/pool_kernel.cc | 6 +- .../phi/kernels/sparse/cpu/reshape_kernel.cc | 2 +- .../kernels/sparse/cpu/softmax_grad_kernel.cc | 4 +- .../phi/kernels/sparse/cpu/softmax_kernel.cc | 4 +- .../kernels/sparse/cpu/sparse_utils_kernel.cc | 30 ++-- .../sparse/cpu/transpose_grad_kernel.cc | 2 +- .../kernels/sparse/cpu/transpose_kernel.cc | 2 +- .../phi/kernels/stride/as_strided_kernel.cc | 4 +- paddle/phi/kernels/stride/diagonal_kernel.cc | 16 ++- .../phi/kernels/stride/index_select_kernel.cc | 9 +- paddle/phi/kernels/stride/slice_kernel.cc | 14 +- paddle/phi/kernels/stride/split_kernel.cc | 2 +- paddle/phi/kernels/stride/squeeze_kernel.cc | 13 +- .../kernels/stride/strided_slice_kernel.cc | 10 +- .../kernels/stride/tensor_unfold_kernel.cc | 10 +- .../kernels/stride/transpose_grad_kernel.cc | 4 +- paddle/phi/kernels/stride/transpose_kernel.cc | 4 +- paddle/phi/kernels/stride/unbind_kernel.cc | 2 +- paddle/phi/kernels/stride/unsqueeze_kernel.cc | 13 +- paddle/phi/kernels/stride/view_kernel.cc | 10 +- paddle/phi/kernels/strings/unicode.cc | 8 +- 103 files changed, 603 insertions(+), 537 deletions(-) diff --git a/paddle/fluid/distributed/auto_parallel/dist_attr.cc b/paddle/fluid/distributed/auto_parallel/dist_attr.cc index 0b6751de6ab8c..5ba9a700e3e29 100644 --- a/paddle/fluid/distributed/auto_parallel/dist_attr.cc +++ b/paddle/fluid/distributed/auto_parallel/dist_attr.cc @@ -378,13 +378,13 @@ std::string OperatorDistAttr::to_string() const { } void OperatorDistAttr::from_proto(const OperatorDistAttrProto& proto) { - for (int64_t i = 0; i < proto.input_dist_attrs_size(); ++i) { + for (int i = 0; i < proto.input_dist_attrs_size(); ++i) { TensorDistAttr dist_attr; std::string name = proto.input_dist_attrs(i).name(); dist_attr.from_proto(proto.input_dist_attrs(i).tensor_dist_attr()); input_dist_attrs_[name] = dist_attr; } - for (int64_t i = 0; i < proto.output_dist_attrs_size(); ++i) { + for (int i = 0; i < proto.output_dist_attrs_size(); ++i) { TensorDistAttr dist_attr; std::string name = proto.output_dist_attrs(i).name(); dist_attr.from_proto(proto.output_dist_attrs(i).tensor_dist_attr()); diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/common.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/common.cc index 9bef518850bdc..e8ef88c03032b 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/common.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/common.cc @@ -223,7 +223,7 @@ std::vector GetDimsMappingForAxes( const std::unordered_map& axis_to_dim_map, const bool unsharded_miss_axis) { std::vector dims_mapping; - for (int64_t i = 0, n = axes.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(axes.size()); i < n; i++) { std::string axis = axes.substr(i, 1); if (axis == "1") { dims_mapping.emplace_back(-1); diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/cross_entropy_with_softmax_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/cross_entropy_with_softmax_spmd_rule.cc index 4e55a6d7920b3..922b525abd57a 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/cross_entropy_with_softmax_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/cross_entropy_with_softmax_spmd_rule.cc @@ -34,7 +34,7 @@ CrossEntropyWithSoftmaxSPMDRule::InferForward( input_specs_size)); auto x_shape = input_specs[0].shape(); - int x_ndim = x_shape.size(); + int x_ndim = static_cast(x_shape.size()); auto x_dist_attr_src = input_specs[0].dist_attr(); std::vector x_dims_mapping_src = x_dist_attr_src.dims_mapping(); diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/dim_trans.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/dim_trans.cc index ad3d67c2990ad..56aab1ec6093f 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/dim_trans.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/dim_trans.cc @@ -85,7 +85,7 @@ void Flatten::set_inputs(const std::vector& dims) { std::string Flatten::to_string() { std::string ret_str("Flatten("); - for (int64_t i = 0, n = input_dims_.size(); i < n; ++i) { + for (int i = 0, n = static_cast(input_dims_.size()); i < n; ++i) { ret_str += input_dims_[i]->to_string(); if (i < n - 1) { ret_str += ","; @@ -125,7 +125,7 @@ int64_t Split::local_splitted_shape_value() { std::string Split::to_string() { std::string ret_str("Split("); ret_str += input_dim_trans_->to_string() + ", ("; - for (int64_t i = 0, n = splitted_shape_.size(); i < n; ++i) { + for (int i = 0, n = static_cast(splitted_shape_.size()); i < n; ++i) { ret_str += std::to_string(splitted_shape_[i]); if (i < n - 1) { ret_str += ","; @@ -161,9 +161,9 @@ DimTrans* make_split(DimTrans* dim, std::vector new_shape; // map between from idx in shape to new_shape std::vector idx_map(shape.size(), -1); - for (int64_t i = 0, n = shape.size(); i < n; ++i) { + for (int i = 0, n = static_cast(shape.size()); i < n; ++i) { if (shape[id] != 1) { - idx_map[i] = new_shape.size(); + idx_map[i] = static_cast(new_shape.size()); new_shape.emplace_back(shape[i]); } } @@ -173,7 +173,8 @@ DimTrans* make_split(DimTrans* dim, } void CleanUp() { - for (int64_t i = 0, n = all_dim_trans.size(); i < n; i++) { + int n = static_cast(all_dim_trans.size()); + for (int i = 0; i < n; i++) { if (all_dim_trans[i]) { delete all_dim_trans[i]; all_dim_trans[i] = nullptr; @@ -210,8 +211,8 @@ DimTrans* GetDimTrans(DimTrans* dim_trans, } else if (type == DimTrans::Type::FLATTEN) { Flatten* flatten = dynamic_cast(dim_trans); const std::vector& inputs = flatten->inputs(); - int64_t nmesh = (*shardable)[0].size(); - for (int64_t i = 1, n = inputs.size(); i < n; i++) { + int64_t nmesh = (*shardable)[0].size(); // NOLINT + for (int i = 1, n = static_cast(inputs.size()); i < n; i++) { DimTrans* input = inputs[i]; if (input->type() == DimTrans::Type::INPUTDIM) { InputDim* inputdim = dynamic_cast(input); @@ -252,7 +253,7 @@ DimTrans* GetDimTrans(DimTrans* dim_trans, phi::errors::InvalidArgument( "The returned dim_trans must be INPUTDIM.")); InputDim* inputdim = dynamic_cast(dim); - int64_t nmesh = mesh_shape.size(); + int64_t nmesh = static_cast(mesh_shape.size()); int64_t input_axis = inputdim->input_dim(); // Check whether the sharded dim can be sharded on @@ -295,13 +296,15 @@ std::vector> InferFromDimTrans( const std::vector& mesh_shape = mesh.shape(); std::set sharded_input_dims; - for (int64_t i = 0, n = input_dims_mapping.size(); i < n; ++i) { + for (int64_t i = 0, n = static_cast(input_dims_mapping.size()); + i < n; + ++i) { if (input_dims_mapping[i] > -1) { sharded_input_dims.insert(i); } } - int64_t ndim = input_shape.size(); - int64_t nmesh = mesh_shape.size(); + int64_t ndim = static_cast(input_shape.size()); + int64_t nmesh = static_cast(mesh_shape.size()); std::vector> shardable(ndim, std::vector(nmesh, true)); @@ -319,7 +322,7 @@ std::vector> InferFromDimTrans( // get the map from sharded input dimensions to output dimensions. std::vector dim_map_src2tgt(ndim, -1); - for (int64_t i = 0, n = dim_trans.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(dim_trans.size()); i < n; i++) { DimTrans* dim = GetDimTrans(dim_trans[i], &shardable, &seen_input_dims, diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/elementwise_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/elementwise_spmd_rule.cc index 6a19aed1f7fbc..2ab6636840e0e 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/elementwise_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/elementwise_spmd_rule.cc @@ -25,7 +25,7 @@ ElementwiseSPMDRule::InferForward( const std::vector& input_specs, const paddle::framework::AttributeMap& attrs) { // step0: Verify Input Args Based on Elementwise Logic - int64_t ninputs = input_specs.size(); + int64_t ninputs = static_cast(input_specs.size()); PADDLE_ENFORCE_GT( ninputs, 0, @@ -39,7 +39,7 @@ ElementwiseSPMDRule::InferForward( std::vector input_axes_vec; int64_t max_ndim = 0; for (int64_t i = 0; i < ninputs; ++i) { - int64_t ndim = input_specs[i].shape().size(); + int64_t ndim = static_cast(input_specs[i].shape().size()); if (ndim > max_ndim) { max_ndim = ndim; } @@ -49,7 +49,7 @@ ElementwiseSPMDRule::InferForward( std::vector broadcast_axis_count(max_ndim, 0); for (int64_t i = 0; i < ninputs; ++i) { std::vector shape = input_specs[i].shape(); - int64_t ndim = shape.size(); + int64_t ndim = static_cast(shape.size()); int64_t start_dim = max_ndim - ndim; std::string axes_notation = GetBroadcastAxes(ndim, max_ndim, alphabet); if (ninputs > 1) { diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/embedding_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/embedding_spmd_rule.cc index b64afd9230075..77a60abe090d5 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/embedding_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/embedding_spmd_rule.cc @@ -33,8 +33,8 @@ EmbeddingSPMDRule::InferForward(const std::vector& input_specs, input_specs_size)); auto x_shape = input_specs[0].shape(); auto weight_shape = input_specs[1].shape(); - int x_ndim = x_shape.size(); - int weight_ndim = weight_shape.size(); + int x_ndim = static_cast(x_shape.size()); + int weight_ndim = static_cast(weight_shape.size()); auto x_dist_attr_src = input_specs[0].dist_attr(); auto weight_dist_attr_src = input_specs[1].dist_attr(); std::vector x_dims_mapping = x_dist_attr_src.dims_mapping(); @@ -170,9 +170,9 @@ EmbeddingSPMDRule::InferBackward( output_specs_size)); auto x_shape = input_specs[0].shape(); - int x_ndim = x_shape.size(); + int x_ndim = static_cast(x_shape.size()); auto out_shape = output_specs[0].shape(); - int out_ndim = out_shape.size(); + int out_ndim = static_cast(out_shape.size()); PADDLE_ENFORCE_EQ(x_ndim, out_ndim - 1, diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/layer_norm_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/layer_norm_spmd_rule.cc index 48fd03e8a4f3b..61b2c6bb91c46 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/layer_norm_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/layer_norm_spmd_rule.cc @@ -34,9 +34,9 @@ LayerNormSPMDRule::InferForward(const std::vector& input_specs, auto x_shape = input_specs[0].shape(); auto scale_shape = input_specs[1].shape(); auto bias_shape = input_specs[2].shape(); - int x_ndim = x_shape.size(); - int scale_ndim = scale_shape.size(); - int bias_ndim = bias_shape.size(); + int x_ndim = static_cast(x_shape.size()); + int scale_ndim = static_cast(scale_shape.size()); + int bias_ndim = static_cast(bias_shape.size()); PADDLE_ENFORCE_EQ( scale_ndim, diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/reduction_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/reduction_spmd_rule.cc index 7b97be299aa07..e2c3045d12faa 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/reduction_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/reduction_spmd_rule.cc @@ -26,7 +26,7 @@ std::pair, std::vector> ReductionSPMDRule::InferForward(const std::vector& input_specs, const paddle::framework::AttributeMap& attrs) { // step0: Verify Input Args Based on Elementwise Logic - int64_t ninputs = input_specs.size(); + int64_t ninputs = static_cast(input_specs.size()); PADDLE_ENFORCE_EQ( ninputs, 1, @@ -42,7 +42,7 @@ ReductionSPMDRule::InferForward(const std::vector& input_specs, std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; // get einsum notation for input - int64_t ndim = input_specs[0].shape().size(); + int64_t ndim = static_cast(input_specs[0].shape().size()); std::vector input_axes_vec; std::string input_axes = alphabet.substr(0, ndim); input_axes_vec.emplace_back(input_axes); diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/reshape_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/reshape_spmd_rule.cc index cb1989144d662..01cb5cffe65ab 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/reshape_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/reshape_spmd_rule.cc @@ -28,7 +28,7 @@ using phi::distributed::auto_parallel::str_join; std::vector InferTargetShape(const std::vector& shape, int64_t len) { int64_t infer_idx = -1; - for (int64_t i = 0, n = shape.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(shape.size()); i < n; i++) { if (shape[i] == -1) { PADDLE_ENFORCE_EQ( infer_idx, @@ -74,8 +74,8 @@ std::vector MakeReshapeDimTrans( int64_t src_idx = 0, tgt_idx = 0; int64_t s, t; int64_t src_len, tgt_len; - src_len = src_shape.size(); - tgt_len = inferred_tgt_shape.size(); + src_len = static_cast(src_shape.size()); + tgt_len = static_cast(inferred_tgt_shape.size()); while (src_idx < src_len || tgt_idx < tgt_len) { std::vector src_dims, tgt_splitted_shape; if (src_idx >= src_len) { @@ -125,7 +125,9 @@ std::vector MakeReshapeDimTrans( } DimTrans* flatten = make_flatten(input_dims); - for (int64_t i = 0, n = tgt_splitted_shape.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(tgt_splitted_shape.size()); + i < n; + i++) { ret.emplace_back(make_split(flatten, tgt_splitted_shape, i)); } } @@ -155,7 +157,7 @@ paddle::distributed::auto_parallel::ReshapeSPMDRule::InferForward( // handle the '0' values in target shape, '0' indicates // that the target shape is equal to the source shape - for (int64_t i = 0, n = tgt_shape.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(tgt_shape.size()); i < n; i++) { if (tgt_shape[i] == 0) { tgt_shape[i] = src_shape[i]; } @@ -178,7 +180,7 @@ paddle::distributed::auto_parallel::ReshapeSPMDRule::InferForward( VLOG(4) << "Reshape: input_shape: [" << str_join(src_shape) << "] output_shape: [" << str_join(tgt_shape) << "]"; VLOG(4) << "Transformation from input to output:"; - for (int64_t i = 0, n = trans.size(); i < n; i++) { + for (int64_t i = 0, n = static_cast(trans.size()); i < n; i++) { DimTrans* t = trans[i]; VLOG(4) << "\tOutput axis " << i << ": " << t->to_string(); } diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/softmax_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/softmax_spmd_rule.cc index e2521bbaba34e..c3468c9d1a2cd 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/softmax_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/softmax_spmd_rule.cc @@ -33,7 +33,7 @@ SoftmaxSPMDRule::InferForward(const std::vector& input_specs, input_specs_size)); auto x_shape = input_specs[0].shape(); - int x_ndim = x_shape.size(); + int x_ndim = static_cast(x_shape.size()); auto x_dist_attr_src = input_specs[0].dist_attr(); std::vector x_dims_mapping = x_dist_attr_src.dims_mapping(); diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/split_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/split_spmd_rule.cc index 59c962dab8982..6f50c17fc5c2b 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/split_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/split_spmd_rule.cc @@ -27,7 +27,7 @@ std::pair, std::vector> SplitSPMDRule::InferForward(const std::vector& input_specs, const paddle::framework::AttributeMap& attrs) { // step0: Verify Input Args Based on Elementwise Logic - int64_t ninputs = input_specs.size(); + int64_t ninputs = static_cast(input_specs.size()); PADDLE_ENFORCE_EQ( ninputs, 1, @@ -37,7 +37,7 @@ SplitSPMDRule::InferForward(const std::vector& input_specs, VerifySpecs(input_specs, "split"); // step1: Build Einsum Notation - int64_t ndim = input_specs[0].shape().size(); + int64_t ndim = static_cast(input_specs[0].shape().size()); int64_t noutput = 0; // split api uses num or sections as attribute if (attrs.find("num") != attrs.end()) { @@ -45,7 +45,7 @@ SplitSPMDRule::InferForward(const std::vector& input_specs, } else if (attrs.find("sections") != attrs.end()) { std::vector sections = ExtractAttr>("sections", attrs); - noutput = sections.size(); + noutput = static_cast(sections.size()); } int64_t axis = ExtractAttr("axis", attrs); if (axis < 0) { diff --git a/paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc b/paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc index fe567e70fa019..a74fb3f6ebdcc 100644 --- a/paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc +++ b/paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc @@ -23,7 +23,7 @@ std::pair, std::vector> TransposeSPMDRule::InferForward(const std::vector& input_specs, const paddle::framework::AttributeMap& attrs) { // step0: Verify Input Args Based on Transpose Logic - int64_t ninputs = input_specs.size(); + int64_t ninputs = static_cast(input_specs.size()); PADDLE_ENFORCE_EQ( ninputs, 1, @@ -38,13 +38,13 @@ TransposeSPMDRule::InferForward(const std::vector& input_specs, std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; // get einsum notation for input - int64_t ndim = input_specs[0].shape().size(); + int64_t ndim = static_cast(input_specs[0].shape().size()); std::vector input_axes_vec; std::string input_axes = alphabet.substr(0, ndim); input_axes_vec.emplace_back(input_axes); // get einsum notation for output - for (int64_t i = 0, n = perm_dims.size(); i < n; ++i) { + for (int64_t i = 0, n = static_cast(perm_dims.size()); i < n; ++i) { // convert the negative dim value to normal dim value if (perm_dims[i] < 0) { perm_dims[i] = ndim + perm_dims[i]; diff --git a/paddle/fluid/distributed/collective/reducer.cc b/paddle/fluid/distributed/collective/reducer.cc index 378668499c90b..fd8805d088597 100644 --- a/paddle/fluid/distributed/collective/reducer.cc +++ b/paddle/fluid/distributed/collective/reducer.cc @@ -953,9 +953,9 @@ void EagerReducer::MarkGroupReady(size_t group_index) { ++next_group_) { UNUSED auto &group = groups_[next_group_]; if (group.is_sparse_) { - AllReduceSparse(&group, next_group_); + AllReduceSparse(&group, static_cast(next_group_)); } else { - FusedAllReduceSchedule(&group, next_group_); + FusedAllReduceSchedule(&group, static_cast(next_group_)); } } } @@ -1078,7 +1078,7 @@ void EagerReducer::FusedAllReduceSchedule(EagerGroup *group, // div nranks paddle::experimental::scale_( - group->dense_contents_, 1.0 / nranks_, 0.0, false); + group->dense_contents_, 1.0 / nranks_, 0.0, false); // NOLINT // all_reduce std::vector reduce_tensors = {group->dense_contents_}; @@ -1104,7 +1104,8 @@ void EagerReducer::AllReduceSparse(EagerGroup *group, const int curr_group_index) { // div nranks Tensor sparse_tensor(group->sparse_contents_); - paddle::experimental::scale_(sparse_tensor, 1.0 / nranks_, 0.0, false); + paddle::experimental::scale_( + sparse_tensor, 1.0 / nranks_, 0.0, false); // NOLINT VLOG(3) << "sparse_group [" << curr_group_index << "] start allreduce."; diff --git a/paddle/fluid/distributed/fleet_executor/carrier.cc b/paddle/fluid/distributed/fleet_executor/carrier.cc index bb128c1287c9c..70ab3b94de3c5 100644 --- a/paddle/fluid/distributed/fleet_executor/carrier.cc +++ b/paddle/fluid/distributed/fleet_executor/carrier.cc @@ -260,7 +260,8 @@ Interceptor* Carrier::SetInterceptor(int64_t interceptor_id, interceptor->RegisterCarrier(this); // TODO(fleet_exe dev): get loop - auto* loop = thread_pool_.GetLoop(interceptor_id % thread_num_); + auto* loop = + thread_pool_.GetLoop(static_cast(interceptor_id % thread_num_)); PADDLE_ENFORCE_NOT_NULL( loop, platform::errors::Fatal("thread task loop must not null")); interceptor->RegisterTaskLoop(loop); @@ -296,7 +297,7 @@ void Carrier::CreateInterceptors( auto gc = GetGC(place_); // create source and sink task node - auto max_run_times = microbatch_scopes_.size(); + int64_t max_run_times = static_cast(microbatch_scopes_.size()); TaskNode* source = new TaskNode( rank_, SOURCE_ID, max_run_times); // rank, task_id, max_run_times TaskNode* sink = new TaskNode(rank_, SINK_ID, max_run_times); diff --git a/paddle/fluid/distributed/fleet_executor/dist_model.cc b/paddle/fluid/distributed/fleet_executor/dist_model.cc index 9924b2674911a..add575cd0aaa2 100644 --- a/paddle/fluid/distributed/fleet_executor/dist_model.cc +++ b/paddle/fluid/distributed/fleet_executor/dist_model.cc @@ -242,7 +242,8 @@ bool DistModel::CommInit() { std::string var_name_base = "comm_init_"; for (int64_t ring_id : ring_ids) { VLOG(3) << "Init comm for ring id: " << ring_id; - int64_t ranks_in_group = config_.ring_id_to_ranks_[ring_id].size(); + int64_t ranks_in_group = + static_cast(config_.ring_id_to_ranks_[ring_id].size()); int64_t rank_in_group = 0; std::vector &ranks = config_.ring_id_to_ranks_[ring_id]; for (int64_t rank : ranks) { @@ -259,11 +260,11 @@ bool DistModel::CommInit() { peer_endpoints.emplace_back(config_.trainer_endpoints[rank]); } InsertCommOp(var_name_base + std::to_string(order), - ranks_in_group, - rank_in_group, + static_cast(ranks_in_group), + static_cast(rank_in_group), peer_endpoints, comm_init_block, - ring_id); + static_cast(ring_id)); order += 1; } framework::NaiveExecutor e(place_); @@ -408,7 +409,7 @@ bool DistModel::LoadProgram() { fin.seekg(0, std::ios::end); pb_content.resize(fin.tellg()); fin.seekg(0, std::ios::beg); - fin.read(&(pb_content.at(0)), pb_content.size()); + fin.read(&(pb_content.at(0)), pb_content.size()); // NOLINT fin.close(); program_proto.ParseFromString(pb_content); VLOG(5) << pb_content; @@ -582,7 +583,7 @@ bool DistModel::FeedData(const std::vector &input_data, << DistModelDTypeToString(input_data[i].dtype) << "."; return false; } - int feed_idx = feed_names_[target_name]; + int feed_idx = static_cast(feed_names_[target_name]); framework::SetFeedVariable(scope, *input_tensor, "feed", feed_idx); } return true; diff --git a/paddle/fluid/distributed/fleet_executor/dist_model.h b/paddle/fluid/distributed/fleet_executor/dist_model.h index e69ce6ee24a16..8b0160d0f1d46 100644 --- a/paddle/fluid/distributed/fleet_executor/dist_model.h +++ b/paddle/fluid/distributed/fleet_executor/dist_model.h @@ -43,7 +43,7 @@ struct DistModelConfig { framework::ProgramDesc* program_desc{nullptr}; framework::Scope* scope{nullptr}; std::string place{}; - int64_t device_id{0}; + int device_id{0}; std::string device_type{}; std::vector trainer_endpoints{}; std::string current_endpoint{}; diff --git a/paddle/fluid/framework/version.cc b/paddle/fluid/framework/version.cc index 9f07f0f5587b1..834b843fdf45d 100644 --- a/paddle/fluid/framework/version.cc +++ b/paddle/fluid/framework/version.cc @@ -39,10 +39,11 @@ bool IsTensorVersionSupported(uint32_t version) { std::string DumpVersion(const int64_t version) { std::stringstream buffer; - const int major = version / MAJOR_COEFF; - const int minor = (version - major * MAJOR_COEFF) / MINOR_COEFF; - const int patch = - (version - major * MAJOR_COEFF - minor * MINOR_COEFF) / PATCH_COEFF; + const int major = static_cast(version / MAJOR_COEFF); + const int minor = + static_cast((version - major * MAJOR_COEFF) / MINOR_COEFF); + const int patch = static_cast( + (version - major * MAJOR_COEFF - minor * MINOR_COEFF) / PATCH_COEFF); buffer << major << "." << minor << "." << patch; return buffer.str(); } diff --git a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc index ccbaca8ed7188..1d439a5bf5c65 100644 --- a/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc @@ -199,12 +199,15 @@ void AutoGrowthBestFitAllocator::Trace() const { cur_idle_bytes += it->second->size_; } - VLOG(1) << "alloc:" << total_alloc_size_ / static_cast(1024 * 1024) - << "m free:" << total_free_size_ / static_cast(1024 * 1024) + VLOG(1) << "alloc:" + << total_alloc_size_ / static_cast(1024 * 1024) // NOLINT + << "m free:" + << total_free_size_ / static_cast(1024 * 1024) // NOLINT << "m busy:" - << (total_alloc_size_ - total_free_size_) / + << (total_alloc_size_ - total_free_size_) / // NOLINT static_cast(1024 * 1024) - << "m idle:" << cur_idle_bytes / static_cast(1024 * 1024) + << "m idle:" + << cur_idle_bytes / static_cast(1024 * 1024) // NOLINT << "m alloc_times:" << total_alloc_times_ << " free_times:" << total_free_times_ << " free_blocks_num:" << free_blocks_.size() diff --git a/paddle/fluid/memory/allocation/mmap_allocator.cc b/paddle/fluid/memory/allocation/mmap_allocator.cc index 271bbacc19957..e3acb1e8579ac 100644 --- a/paddle/fluid/memory/allocation/mmap_allocator.cc +++ b/paddle/fluid/memory/allocation/mmap_allocator.cc @@ -345,7 +345,8 @@ int MemoryMapAllocationPool::FindFromCache(const int &flag, const std::string &file_name, bool check_refcount) { std::lock_guard guard(mtx_); - for (size_t idx = 0; idx < memory_map_allocations_.size(); idx++) { + for (int idx = 0; idx < static_cast(memory_map_allocations_.size()); + idx++) { if (memory_map_allocations_.at(idx).flags_ == flag && memory_map_allocations_.at(idx).data_size_ == data_size) { if (file_name.empty() || diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 4e0803c59ce59..9343c8ddf7781 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -92,7 +92,7 @@ bool InitGflags(std::vector args) { args.insert(args.begin(), "dummy"); std::vector argv; std::string line; - int argc = args.size(); + int argc = static_cast(args.size()); for (auto &arg : args) { argv.push_back(const_cast(arg.data())); line += arg; diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index e44c713315b94..c3279d445025d 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -859,7 +859,8 @@ std::string PrintHostEvents() { oss << thr_evt_sec.thread_id << std::endl; for (const auto &evt : thr_evt_sec.events) { oss << "{ " << evt.name << " | " << evt.start_ns << "ns | " << evt.end_ns - << "ns | " << (evt.end_ns - evt.start_ns) / 1000.000 << "us }" + << "ns | " << (evt.end_ns - evt.start_ns) / 1000.000 + << "us }" // NOLINT << std::endl; } } diff --git a/paddle/fluid/platform/profiler/chrometracing_logger.cc b/paddle/fluid/platform/profiler/chrometracing_logger.cc index e6c0d5404323b..de8fd01a1e59d 100644 --- a/paddle/fluid/platform/profiler/chrometracing_logger.cc +++ b/paddle/fluid/platform/profiler/chrometracing_logger.cc @@ -450,7 +450,8 @@ void ChromeTracingLogger::HandleTypeMemcpy( MemcpyEventInfo memcpy_info = device_node.MemcpyInfo(); float memory_bandwidth = 0; if (device_node.Duration() > 0) { - memory_bandwidth = memcpy_info.num_bytes * 1.0 / device_node.Duration(); + memory_bandwidth = + memcpy_info.num_bytes * 1.0 / device_node.Duration(); // NOLINT } float dur = nsToMsFloat(device_node.Duration()); std::string dur_display; diff --git a/paddle/fluid/platform/profiler/dump/deserialization_reader.cc b/paddle/fluid/platform/profiler/dump/deserialization_reader.cc index 42f5517ead783..1fce7edc3e329 100644 --- a/paddle/fluid/platform/profiler/dump/deserialization_reader.cc +++ b/paddle/fluid/platform/profiler/dump/deserialization_reader.cc @@ -287,7 +287,7 @@ DeserializationReader::RestoreOperatorSupplementEventNode( auto shape_vector_proto = shape_vectors_proto.shapes(j); std::vector shape; for (int k = 0; k < shape_vector_proto.size_size(); k++) { - shape.push_back(shape_vector_proto.size(k)); + shape.push_back(shape_vector_proto.size(k)); // NOLINT } input_shape_vec.push_back(shape); } diff --git a/paddle/fluid/platform/timer.cc b/paddle/fluid/platform/timer.cc index 7ddb5aeedb6fb..855a3d47e38bb 100644 --- a/paddle/fluid/platform/timer.cc +++ b/paddle/fluid/platform/timer.cc @@ -49,9 +49,9 @@ int Timer::Count() { return _count; } double Timer::ElapsedUS() { return static_cast(_elapsed); } -double Timer::ElapsedMS() { return _elapsed / 1000.0; } +double Timer::ElapsedMS() { return _elapsed / 1000.0; } // NOLINT -double Timer::ElapsedSec() { return _elapsed / 1000000.0; } +double Timer::ElapsedSec() { return _elapsed / 1000000.0; } // NOLINT int64_t Timer::Tickus() { gettimeofday(&_now, nullptr); diff --git a/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc index c0c59e5d8c395..31529eaa1b282 100644 --- a/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/r_to_s_reshard_function.cc @@ -68,7 +68,7 @@ void RToSReshardFunction::Eval(phi::DeviceContext* dev_ctx, << " process participate in."; std::vector split_num_vec = - BalancedSplit(in.dims()[split_axis], num_of_process); + BalancedSplit(in.dims()[static_cast(split_axis)], num_of_process); IntArray sections(split_num_vec); std::vector split_out_vec; diff --git a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc index 8e4cb877c95db..8c8540a02ee1a 100644 --- a/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc +++ b/paddle/phi/core/distributed/auto_parallel/s_to_r_reshard_function.cc @@ -50,8 +50,8 @@ bool SToRReshardFunction::IsSuitable(const DistTensor& in, GetSplitAxisWithDimsMapping(in_dims_mapping); int64_t split_axis = split_axis_to_mesh_axis.begin()->first; int64_t num_of_process = in_process_mesh.size(); - flag &= - (in.local_dims()[split_axis] * num_of_process == in.dims()[split_axis]); + flag &= (in.local_dims()[static_cast(split_axis)] * num_of_process == + in.dims()[static_cast(split_axis)]); return flag; } @@ -89,10 +89,11 @@ void SToRReshardFunction::Eval(DeviceContext* dev_ctx, // first we need to split the result on axis 0, // then we need to concat the split result on input split axis. int64_t default_split_axis = 0; - int64_t num_of_process = in_process_ids.size(); + int64_t num_of_process = static_cast(in_process_ids.size()); IntArray sections(std::vector( - num_of_process, in.value().dims()[default_split_axis])); + num_of_process, + in.value().dims()[static_cast(default_split_axis)])); std::vector split_out_vec; RESHARD_FUNCTOR(dev_ctx, Split, diff --git a/paddle/phi/infermeta/backward.cc b/paddle/phi/infermeta/backward.cc index 5dedf81e937ac..e66e55ca97153 100644 --- a/paddle/phi/infermeta/backward.cc +++ b/paddle/phi/infermeta/backward.cc @@ -376,7 +376,7 @@ void FFTC2RGradInferMeta(const MetaTensor& x, out->set_dtype(ToComplexType(x.dtype())); phi::DDim out_dim = x.dims(); - const int64_t last_fft_axis = axes.back(); + const int last_fft_axis = static_cast(axes.back()); if (last_dim_size > 0) { out_dim.at(last_fft_axis) = last_dim_size / 2 + 1; } else if (config.is_runtime) { @@ -534,7 +534,7 @@ void InstanceNormGradInferMeta(const MetaTensor& x, phi::errors::InvalidArgument( "The X@GRAD in InstanceNormGradInferMeta can't be nullptr.")); const auto x_dims = x.dims(); - const int C = x_dims[1]; + const int C = static_cast(x_dims[1]); x_grad->set_dims(x_dims); x_grad->set_dtype(x.dtype()); x_grad->set_layout(x.layout()); @@ -563,7 +563,7 @@ void InstanceNormDoubleGradInferMeta(const MetaTensor& x, phi::errors::InvalidArgument( "The DX in InstanceNormDoubleGradInferMeta can't be nullptr.")); const auto x_dims = x.dims(); - const int C = x_dims[1]; + const int C = static_cast(x_dims[1]); dx->set_dims(x_dims); dx->set_dtype(x.dtype()); dx->set_layout(x.layout()); @@ -1076,12 +1076,12 @@ void TransposeGradInferMeta(const MetaTensor& x, std::vector formated_axis = axis; for (size_t i = 0; i < axis.size(); i++) { if (axis[i] < 0) { - formated_axis[i] = axis[i] + x_rank; + formated_axis[i] = static_cast(axis[i] + x_rank); } } std::vector reversed_axis(axis); - for (size_t i = 0; i < formated_axis.size(); i++) { + for (int i = 0; i < static_cast(formated_axis.size()); i++) { reversed_axis[formated_axis[i]] = i; } @@ -1151,7 +1151,7 @@ void UnStackGradInferMeta(const std::vector& out_grad, if (axis < 0) axis += (rank + 1); auto vec = phi::vectorize(input_dims[0]); - vec.insert(vec.begin() + axis, input_dims.size()); + vec.insert(vec.begin() + axis, static_cast(input_dims.size())); x_grad->set_dims(phi::make_ddim(vec)); x_grad->set_dtype(out_grad[0]->dtype()); } diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index a7bd8572a4e4a..a66790d0ce6cd 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -450,7 +450,7 @@ void ConvInferMeta(const MetaTensor& input, std::vector dilations = dilations_t; auto in_dims = input.dims(); auto filter_dims = filter.dims(); - int dilation_size = dilations.size(); + int dilation_size = static_cast(dilations.size()); for (int i = 0; i < dilation_size; ++i) { PADDLE_ENFORCE_GT( dilations[i], @@ -492,7 +492,7 @@ void ConvInferMeta(const MetaTensor& input, filter_dims, filter_dims.size())); - int stride_size = strides.size(); + int stride_size = static_cast(strides.size()); for (int i = 0; i < stride_size; ++i) { PADDLE_ENFORCE_GT( strides[i], @@ -579,11 +579,12 @@ void ConvInferMeta(const MetaTensor& input, (in_data_dims[i] <= 0 || filter_dims[i + 2] <= 0)) { output_shape.push_back(-1); } else { - const int dkernel = dilations[i] * (filter_data_dims[i] - 1) + 1; - int output_size = + const int dkernel = + static_cast(dilations[i] * (filter_data_dims[i] - 1) + 1); + int output_size = static_cast( (in_data_dims[i] + paddings[2 * i] + paddings[2 * i + 1] - dkernel) / strides[i] + - 1; + 1); output_shape.push_back(output_size); } } @@ -660,7 +661,7 @@ void ConvTransposeInferMeta(const MetaTensor& x, filter_dims, filter_dims.size())); - int stride_size = strides.size(); + int stride_size = static_cast(strides.size()); for (int i = 0; i < stride_size; ++i) { PADDLE_ENFORCE_GT( strides[i], @@ -734,7 +735,7 @@ void ConvTransposeInferMeta(const MetaTensor& x, output_shape.push_back(filter_dims[1] * groups); } const int offset = (data_layout != DataLayout::kNHWC ? 2 : 1); - for (size_t i = 0; i < strides.size(); ++i) { + for (int i = 0; i < static_cast(strides.size()); ++i) { auto filter_extent = dilations_[i] * (filter_dims[i + 2] - 1) + 1; auto infer_shape = (config.is_runtime || x_dims[i + offset] > 0) ? (x_dims[i + offset] - 1) * strides[i] - @@ -1115,7 +1116,7 @@ void DropoutNdInferMeta(const MetaTensor& x, std::for_each( axis.begin(), axis.end(), [&mask_dims, &x_dims](const int64_t& t) { - mask_dims[t] = x_dims[t]; + mask_dims[t] = x_dims[static_cast(t)]; }); mask->set_dims(make_ddim(mask_dims)); @@ -1125,7 +1126,7 @@ void DropoutNdInferMeta(const MetaTensor& x, void DotInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { auto x_dims = x.dims(); - auto x_rank = static_cast(x_dims.size()); + int x_rank = static_cast(x_dims.size()); PADDLE_ENFORCE_EQ(true, 1 == x_rank || 2 == x_rank, phi::errors::PreconditionNotMet( @@ -1136,14 +1137,14 @@ void DotInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { auto y_dims = y.dims(); PADDLE_ENFORCE_EQ( true, - x_rank == static_cast(y_dims.size()), + x_rank == static_cast(y_dims.size()), phi::errors::PreconditionNotMet( "ShapeError: The shape of input tensor Y: %s should match with " "input tenosr X: %s", y_dims.to_str(), x_dims.to_str())); bool shape_match = true; - for (size_t i = 0; i < x_rank; ++i) { + for (int i = 0; i < x_rank; ++i) { if (x_dims[i] != y_dims[i]) { shape_match = false; break; @@ -1458,10 +1459,10 @@ void FusedMatmulInferMeta(const MetaTensor& x, } } if (!x_broadcasted) { - new_dims.push_back(M); + new_dims.push_back(M); // NOLINT } if (!y_broadcasted) { - new_dims.push_back(N); + new_dims.push_back(N); // NOLINT } if (x_broadcasted && y_broadcasted) { new_dims.push_back(1); @@ -1558,10 +1559,10 @@ void GatherInferMeta(const MetaTensor& x, } else { std::vector out_dim_vec; for (int i = 0; i < axis_v; i++) { - out_dim_vec.push_back(input_dim[i]); + out_dim_vec.push_back(input_dim[i]); // NOLINT } for (int i = axis_v + 1; i < input_dim.size(); i++) { - out_dim_vec.push_back(input_dim[i]); + out_dim_vec.push_back(input_dim[i]); // NOLINT } auto output_dims = phi::make_ddim(out_dim_vec); out->set_dims(output_dims); @@ -1572,21 +1573,21 @@ void GatherInferMeta(const MetaTensor& x, } else { if (axis.FromTensor() || axis_v == 0) { // if axis.FromTensor(), we can not obtain correct shape of output - int batch_size = index_dims[0]; + int batch_size = static_cast(index_dims[0]); phi::DDim output_dims(input_dim); output_dims[0] = batch_size; out->set_dims(output_dims); out->set_dtype(x.dtype()); out->share_lod(x); } else { - int index_size = index_dims[0]; + int index_size = static_cast(index_dims[0]); std::vector out_dim_vec; for (int i = 0; i < axis_v; i++) { - out_dim_vec.push_back(input_dim[i]); + out_dim_vec.push_back(input_dim[i]); // NOLINT } out_dim_vec.push_back(index_size); for (int i = axis_v + 1; i < input_dim.size(); i++) { - out_dim_vec.push_back(input_dim[i]); + out_dim_vec.push_back(input_dim[i]); // NOLINT } auto output_dims = phi::make_ddim(out_dim_vec); out->set_dims(output_dims); @@ -1602,7 +1603,7 @@ void GatherNdInferMeta(const MetaTensor& x, auto x_dims = x.dims(); auto x_dims_size = x_dims.size(); auto index_dims = index.dims(); - auto index_dims_size = index_dims.size(); + int index_dims_size = index_dims.size(); PADDLE_ENFORCE_LE( index_dims[index_dims_size - 1], @@ -1620,7 +1621,9 @@ void GatherNdInferMeta(const MetaTensor& x, for (int i = 0; i < index_dims_size - 1; ++i) { result_dims.emplace_back(index_dims[i]); } - for (int i = index_dims[index_dims_size - 1]; i < x_dims_size; ++i) { + for (int i = static_cast(index_dims[index_dims_size - 1]); + i < x_dims_size; + ++i) { result_dims.emplace_back(x_dims[i]); } @@ -2010,8 +2013,8 @@ void LUUnpackInferMeta(const MetaTensor& x, 2, phi::errors::InvalidArgument("The rank of input must greater than 2.")); - int m = x_dims[x_rank - 1]; - int n = x_dims[x_rank - 2]; + int m = static_cast(x_dims[x_rank - 1]); + int n = static_cast(x_dims[x_rank - 2]); int min_mn = std::min(m, n); if (unpack_ludata) { auto ldims = x_dims; @@ -2157,10 +2160,10 @@ void MatmulInferMeta(const MetaTensor& x, } } if (!x_broadcasted) { - new_dims.push_back(M); + new_dims.push_back(M); // NOLINT } if (!y_broadcasted) { - new_dims.push_back(N); + new_dims.push_back(N); // NOLINT } auto ddim_out = phi::make_ddim(new_dims); @@ -2227,10 +2230,10 @@ void MatmulInt8InferMeta(const MetaTensor& x, } } if (!x_broadcasted) { - new_dims.push_back(M); + new_dims.push_back(M); // NOLINT } if (!y_broadcasted) { - new_dims.push_back(N); + new_dims.push_back(N); // NOLINT } auto ddim_out = phi::make_ddim(new_dims); @@ -2397,8 +2400,8 @@ void MatrixRankTolInferMeta(const MetaTensor& x, phi::errors::InvalidArgument("The dims of input must be greater than 2")); if (hermitian) { - int rows = dim_x[dim_x.size() - 2]; - int cols = dim_x[dim_x.size() - 1]; + int rows = static_cast(dim_x[dim_x.size() - 2]); + int cols = static_cast(dim_x[dim_x.size() - 1]); PADDLE_ENFORCE_EQ(rows, cols, phi::errors::InvalidArgument( @@ -2508,7 +2511,7 @@ void PReluInferMeta(const MetaTensor& x, } else if (mode == "element") { auto alpha_dim = alpha.dims(); auto alpha_rank = alpha_dim.size(); - auto x_rank = x_dim.size(); + int x_rank = x_dim.size(); PADDLE_ENFORCE_GE(x_rank, 1, phi::errors::InvalidArgument( @@ -2527,7 +2530,7 @@ void PReluInferMeta(const MetaTensor& x, x_rank)); size_t x_product = 1; size_t alpha_product = 1; - for (int64_t i = x_rank - 1; i > 0; i--) { + for (int i = x_rank - 1; i > 0; i--) { x_product *= x_dim[i]; alpha_product *= alpha_dim[i]; } @@ -2647,7 +2650,7 @@ void PriorBoxInferMeta(const MetaTensor& input, std::vector dim_vec(4); dim_vec[0] = input_dims[2]; dim_vec[1] = input_dims[3]; - dim_vec[2] = num_priors; + dim_vec[2] = static_cast(num_priors); dim_vec[3] = 4; out->set_dtype(input.dtype()); @@ -2720,8 +2723,8 @@ void SearchsortedInferMeta(const MetaTensor& sorted_sequence, if (sequences_dims.size() != values_dims.size()) { flag = false; } - const auto& sequences_dims_size = sequences_dims.size(); - for (int64_t dim = 0; dim < sequences_dims_size - 1; ++dim) { + const int& sequences_dims_size = sequences_dims.size(); + for (int dim = 0; dim < sequences_dims_size - 1; ++dim) { if (sequences_dims[dim] != values_dims[dim]) { flag = false; break; @@ -2918,9 +2921,9 @@ void LstsqInferMeta(const MetaTensor& x, int x_rank = x_dims.size(); int y_rank = y_dims.size(); - int m = x_dims[x_rank - 2]; - int n = x_dims[x_rank - 1]; - int nrhs = y_dims[x_rank - 1]; + int m = static_cast(x_dims[x_rank - 2]); + int n = static_cast(x_dims[x_rank - 1]); + int nrhs = static_cast(y_dims[x_rank - 1]); PADDLE_ENFORCE_GE( x_rank, @@ -3004,7 +3007,7 @@ void YoloBoxInferMeta(const MetaTensor& x, MetaConfig config) { auto dim_x = x.dims(); auto dim_imgsize = img_size.dims(); - int anchor_num = anchors.size() / 2; + int anchor_num = static_cast(anchors.size() / 2); PADDLE_ENFORCE_EQ( dim_x.size(), @@ -3089,7 +3092,7 @@ void YoloBoxInferMeta(const MetaTensor& x, int box_num; if ((dim_x[2] > 0 && dim_x[3] > 0) || config.is_runtime) { - box_num = dim_x[2] * dim_x[3] * anchor_num; + box_num = static_cast(dim_x[2] * dim_x[3] * anchor_num); } else { box_num = -1; } @@ -3181,10 +3184,10 @@ void SolveInferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) { new_dims.assign(y_dims_vec.begin(), y_dims_vec.end() - 2); } if (!x_broadcasted) { - new_dims.push_back(M); + new_dims.push_back(M); // NOLINT } if (!y_broadcasted) { - new_dims.push_back(N); + new_dims.push_back(N); // NOLINT } if (x_broadcasted && y_broadcasted) { new_dims.push_back(1); @@ -3232,7 +3235,7 @@ void UnpoolInferMeta(const MetaTensor& x, if (config.is_runtime || !output_size.FromTensor()) { output_size_val = output_size.GetData(); } - for (size_t i = 0; i < ksize.size(); ++i) { + for (int i = 0; i < static_cast(ksize.size()); ++i) { if (!config.is_runtime && in_x_dims[i + 2] <= 0) { output_shape.push_back(-1); } else { @@ -3273,7 +3276,7 @@ void Unpool3dInferMeta(const MetaTensor& x, in_y_dims)); std::vector output_shape({in_x_dims[0], in_x_dims[1]}); - for (size_t i = 0; i < ksize.size(); ++i) { + for (int i = 0; i < static_cast(ksize.size()); ++i) { if (!config.is_runtime && in_x_dims[i + 2] <= 0) { output_shape.push_back(-1); } else { diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 1baf780f67610..7a10fd752acf3 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -207,14 +207,14 @@ void Conv1dXPUInferMeta(const MetaTensor& x, groups)); std::vector out_shape({in_dims[0], filter_dims[0]}); - out_shape.push_back(ConvOutSize(in_dims[2], - filter_dims[2], + out_shape.push_back(ConvOutSize(static_cast(in_dims[2]), + static_cast(filter_dims[2]), dilations, paddings[0], paddings[1], strides)); // set output and output max dims - out->set_dims(DDim(out_shape.data(), out_shape.size())); + out->set_dims(DDim(out_shape.data(), static_cast(out_shape.size()))); out->set_dtype(x.dtype()); out->set_layout(x.layout()); out_max->set_dims(phi::make_ddim({6})); @@ -264,9 +264,9 @@ void Conv2dXPUInferMeta(const MetaTensor& x, filter_dims.size())); const auto input_channels = in_dims[1]; - int stride_size = strides.size(); + int stride_size = static_cast(strides.size()); int in_sub_stride_size = in_dims.size() - stride_size; - int dilation_size = dilations.size(); + int dilation_size = static_cast(dilations.size()); PADDLE_ENFORCE_EQ( in_dims.size(), strides.size() + 2U, @@ -333,16 +333,16 @@ void Conv2dXPUInferMeta(const MetaTensor& x, ksize); std::vector out_shape({in_dims[0], filter_dims[0]}); - for (size_t i = 0; i < strides.size(); ++i) { - out_shape.push_back(ConvOutSize(in_dims[i + 2], - filter_dims[i + 2], + for (int i = 0; i < static_cast(strides.size()); ++i) { + out_shape.push_back(ConvOutSize(static_cast(in_dims[i + 2]), + static_cast(filter_dims[i + 2]), dilations[i], paddings_vec[i * 2], paddings_vec[i * 2 + 1], strides[i])); } // set output and output max dims - out->set_dims(DDim(out_shape.data(), out_shape.size())); + out->set_dims(DDim(out_shape.data(), static_cast(out_shape.size()))); out_max->set_dims(phi::make_ddim({6})); out->set_dtype(out_dtype); } @@ -388,10 +388,10 @@ void FcXPUInferMeta(const MetaTensor& x, MetaTensor* out_max) { std::vector out_shape(in_num_col_dims + 1); for (int i = 0; i < in_num_col_dims; i++) { - out_shape[i] = x.dims()[i]; + out_shape[i] = static_cast(x.dims()[i]); } - out_shape[in_num_col_dims] = w.dims()[0]; - out->set_dims(DDim(out_shape.data(), out_shape.size())); + out_shape[in_num_col_dims] = static_cast(w.dims()[0]); + out->set_dims(DDim(out_shape.data(), static_cast(out_shape.size()))); out->set_dtype(out_dtype); out->set_layout(x.layout()); out_max->set_dims(phi::make_ddim({6})); @@ -671,7 +671,7 @@ void ConvTransposeXPUInferMeta(const MetaTensor& x, filter_dims, filter_dims.size())); - int stride_size = strides.size(); + int stride_size = static_cast(strides.size()); for (int i = 0; i < stride_size; ++i) { PADDLE_ENFORCE_GT( strides[i], @@ -744,7 +744,7 @@ void ConvTransposeXPUInferMeta(const MetaTensor& x, output_shape.push_back(filter_dims[1] * groups); } const int offset = (data_format != "NHWC" ? 2 : 1); - for (size_t i = 0; i < strides.size(); ++i) { + for (int i = 0; i < static_cast(strides.size()); ++i) { auto filter_extent = dilations_[i] * (filter_dims[i + 2] - 1) + 1; auto infer_shape = (x_dims[i + offset] > 0) ? (x_dims[i + offset] - 1) * strides[i] - @@ -901,7 +901,7 @@ void FusedScaleBiasReluConvBnstatsInferMeta( phi::errors::InvalidArgument("Expect group to be 1, got %d.", groups)); const auto input_channels = in_dims[in_dims.size() - 1]; - int dilation_size = dilations.size(); + int dilation_size = static_cast(dilations.size()); for (int i = 0; i < dilation_size; ++i) { PADDLE_ENFORCE_GT( dilations[i], @@ -943,9 +943,9 @@ void FusedScaleBiasReluConvBnstatsInferMeta( ksize); std::vector out_shape({in_dims[0]}); - for (size_t i = 0; i < strides.size(); ++i) { - out_shape.push_back(ConvOutSize(in_dims[i + 1], - filter_dims[i + 2], + for (int i = 0; i < static_cast(strides.size()); ++i) { + out_shape.push_back(ConvOutSize(static_cast(in_dims[i + 1]), + static_cast(filter_dims[i + 2]), dilations[i], paddings_vec[i * 2], paddings_vec[i * 2 + 1], @@ -955,7 +955,7 @@ void FusedScaleBiasReluConvBnstatsInferMeta( // make shape for other outputs auto c_dims = phi::make_ddim({filter_dims[0]}); // set output and output max dims - out->set_dims(DDim(out_shape.data(), out_shape.size())); + out->set_dims(DDim(out_shape.data(), static_cast(out_shape.size()))); out_running_mean->set_dims(c_dims); out_running_var->set_dims(c_dims); saved_mean->set_dims(c_dims); diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 63c421dcdcdb9..958bfa76fe9c5 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -795,7 +795,7 @@ void BroadcastTensorsInferMeta(const std::vector& x, int axis = static_cast(input_ddim.size()) - index - 1; int dim_size = 1; if (axis >= 0) { - dim_size = input_ddim[axis]; + dim_size = static_cast(input_ddim[axis]); } if (target_dim_size != 1 && dim_size != 1 && @@ -857,7 +857,7 @@ void CoalesceTensorInferMeta(const std::vector& input, MetaTensor* fused_output, MetaConfig config) { if (size_of_dtype == -1) { - size_of_dtype = phi::SizeOf(dtype); + size_of_dtype = static_cast(phi::SizeOf(dtype)); } if (config.is_runtime) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -902,7 +902,7 @@ void CoalesceTensorInferMeta(const std::vector& input, align_size) / size_of_dtype : static_cast(size); - numel += len; + numel += static_cast(len); } if (fused_output) { fused_output->set_dims(phi::make_ddim({numel})); @@ -925,7 +925,7 @@ void CheckMemoryContinueInferMeta(const std::vector& input, const auto& dim = item->dims(); auto size = phi::product(dim); auto len = size * phi::SizeOf(item->dtype()); - numel += len; + numel += static_cast(len); } output->set_dims(phi::make_ddim({numel})); output->set_dtype(phi::DataType::INT8); @@ -1215,16 +1215,17 @@ void DeformableConvInferMeta(const MetaTensor& x, } std::vector output_shape({in_dims[0], filter_dims[0]}); - for (size_t i = 0; i < strides.size(); ++i) { + for (int i = 0; i < static_cast(strides.size()); ++i) { if (!config.is_runtime && (in_dims[i + 2] <= 0 || filter_dims[i + 2] <= 0)) { output_shape.push_back(-1); } else { - output_shape.push_back(ConvOutputSize(in_dims[i + 2], - filter_dims[i + 2], - dilations[i], - paddings[i], - strides[i])); + output_shape.push_back( + ConvOutputSize(static_cast(in_dims[i + 2]), + static_cast(filter_dims[i + 2]), + dilations[i], + paddings[i], + strides[i])); } } @@ -1628,7 +1629,7 @@ void FusedLayerNormInferMeta(const MetaTensor& x, int32_t rows = 1; for (int i = 0; i < begin_norm_axis; i++) { - rows *= x.dims()[i]; + rows *= static_cast(x.dims()[i]); } if (norm_weight) { @@ -2025,9 +2026,10 @@ static void Interpolate1DInferShapeCheck( scale_w)); if (scale_w > 0.) { // round down - out_w_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale_w) - : static_cast(dim_x[1] * scale_w)); + out_w_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[2]) * scale_w + : static_cast(dim_x[1]) * scale_w); // protect when input shape is -1 out_w_tmp = out_w_tmp > 0 ? out_w_tmp : -1; } @@ -2171,12 +2173,14 @@ static void Interpolate2DInferShapeCheck( scale_h)); if (scale_h > 0. && scale_w > 0.) { // round down - out_h_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale_h) - : static_cast(dim_x[1] * scale_h)); - out_w_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[3] * scale_w) - : static_cast(dim_x[2] * scale_w)); + out_h_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[2]) * scale_h + : static_cast(dim_x[1]) * scale_h); + out_w_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[3]) * scale_w + : static_cast(dim_x[2]) * scale_w); // protect when input shape is -1 out_h_tmp = out_h_tmp > 0 ? out_h_tmp : -1; out_w_tmp = out_w_tmp > 0 ? out_w_tmp : -1; @@ -2325,15 +2329,18 @@ static void Interpolate3DInferShapeCheck( scale_d)); if (scale_d > 0. && scale_h > 0. && scale_w > 0.) { // round down - out_d_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale_d) - : static_cast(dim_x[1] * scale_d)); - out_h_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[3] * scale_h) - : static_cast(dim_x[2] * scale_h)); - out_w_tmp = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[4] * scale_w) - : static_cast(dim_x[3] * scale_w)); + out_d_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[2]) * scale_d + : static_cast(dim_x[1]) * scale_d); + out_h_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[3]) * scale_h + : static_cast(dim_x[2]) * scale_h); + out_w_tmp = + static_cast(data_layout == DataLayout::kNCHW + ? static_cast(dim_x[4]) * scale_w + : static_cast(dim_x[3]) * scale_w); // protect when input shape is -1 out_d_tmp = out_d_tmp > 0 ? out_d_tmp : -1; out_h_tmp = out_h_tmp > 0 ? out_h_tmp : -1; @@ -2860,13 +2867,13 @@ void MeshgridInferMeta(const std::vector& inputs, std::vector outputs) { const size_t inputs_num = inputs.size(); - auto out_shape = std::vector(inputs_num); + std::vector out_shape = std::vector(inputs_num); for (size_t i = 0; i < inputs.size(); i++) { if (inputs[i]->dims().size() == 0) { out_shape[i] = 1; } else { - out_shape[i] = inputs[i]->dims()[0]; + out_shape[i] = static_cast(inputs[i]->dims()[0]); } } auto out_dims = phi::make_ddim(std::vector(out_shape)); @@ -3317,7 +3324,7 @@ void RnnInferMeta(const MetaTensor& x, out->set_dims(out_dims); out->set_dtype(x.dtype()); - int state_num = pre_state.size(); + int state_num = static_cast(pre_state.size()); for (int i = 0; i < state_num; ++i) { state[i]->set_dims(pre_state[i]->dims()); state[i]->set_dtype(x.dtype()); @@ -3482,7 +3489,7 @@ void SendUERecvInferMeta(const MetaTensor& x, std::vector x_dims2(x_dims1.begin() + 1, x_dims1.end()); std::vector y_dims2(y_dims1.begin() + 1, y_dims1.end()); - int max_dim = std::max(x_dims2.size(), y_dims2.size()); + int max_dim = static_cast(std::max(x_dims2.size(), y_dims2.size())); int axis = std::abs(static_cast(x_dims2.size() - y_dims2.size())); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); @@ -3552,7 +3559,7 @@ void SendUVInferMeta(const MetaTensor& x, auto y_dims1 = phi::vectorize(y_dims); std::vector x_dims2(x_dims1.begin() + 1, x_dims1.end()); std::vector y_dims2(y_dims1.begin() + 1, y_dims1.end()); - int max_dim = std::max(x_dims2.size(), y_dims2.size()); + int max_dim = static_cast(std::max(x_dims2.size(), y_dims2.size())); int axis = std::abs(static_cast(x_dims2.size() - y_dims2.size())); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); @@ -3565,7 +3572,7 @@ void SendUVInferMeta(const MetaTensor& x, out_dims_array.data(), max_dim, axis); - out_dims_array.insert(out_dims_array.begin(), src_index_dims[0]); + out_dims_array.insert(out_dims_array.begin(), src_index_dims[0]); // NOLINT out->set_dims(phi::make_ddim(out_dims_array)); } @@ -3603,7 +3610,7 @@ void StackInferMeta(const std::vector& x, axis)); if (axis < 0) axis += (rank + 1); auto vec = phi::vectorize(out_dim); - vec.insert(vec.begin() + axis, input_dims.size()); + vec.insert(vec.begin() + axis, input_dims.size()); // NOLINT out->set_dims(phi::make_ddim(vec)); out->set_dtype(x.at(0)->dtype()); out->share_lod(*x.at(0)); @@ -3692,7 +3699,7 @@ void WarpctcInferMeta(const MetaTensor& logits, int sequence_width = 0; if (logits_length) { - sequence_width = logits_dims[2]; + sequence_width = static_cast(logits_dims[2]); } else { sequence_width = static_cast(phi::product(logits_dims) / logits_dims[0]); @@ -3726,7 +3733,7 @@ void WarprnntInferMeta(const MetaTensor& input, MetaTensor* loss, MetaTensor* warpctcgrad) { auto acts_dims = input.dims(); - int D = acts_dims[3]; + int D = static_cast(acts_dims[3]); PADDLE_ENFORCE_GE( blank, @@ -3835,8 +3842,8 @@ void YoloLossInferMeta(const MetaTensor& x, auto dim_x = x.dims(); auto dim_gtbox = gt_box.dims(); auto dim_gtlabel = gt_label.dims(); - int anchor_num = anchors.size() / 2; - int mask_num = anchor_mask.size(); + int anchor_num = static_cast(anchors.size() / 2); + int mask_num = static_cast(anchor_mask.size()); PADDLE_ENFORCE_EQ(dim_x.size(), 4, @@ -4183,10 +4190,10 @@ void MaskedMultiheadAttentionInferMeta(const MetaTensor& x, MetaTensor* out, MetaTensor* cache_kv_out, MetaTensor* beam_cache_offset_out) { - int bsz = x.dims()[0]; + int bsz = static_cast(x.dims()[0]); auto cache_kv_dims = cache_kv.dims(); - int num_head = cache_kv.dims()[2]; - int dim_head = cache_kv.dims()[4]; + int num_head = static_cast(cache_kv.dims()[2]); + int dim_head = static_cast(cache_kv.dims()[4]); PADDLE_ENFORCE_EQ( cache_kv_dims.size(), diff --git a/paddle/phi/infermeta/sparse/binary.cc b/paddle/phi/infermeta/sparse/binary.cc index ede010808ac48..6e9a5775e2463 100644 --- a/paddle/phi/infermeta/sparse/binary.cc +++ b/paddle/phi/infermeta/sparse/binary.cc @@ -69,7 +69,7 @@ inline void ResetSubmKernelSizeAndStrides(const DDim& kernel_dims, std::vector* paddings, std::vector* strides) { for (uint64_t i = 0; i < paddings->size(); i++) { - (*paddings)[i] = kernel_dims[i] / 2; + (*paddings)[i] = kernel_dims[i] / 2; // NOLINT (*strides)[i] = 1; } } @@ -95,7 +95,7 @@ void Conv3dInferMeta(const MetaTensor& x, std::vector kernel_sizes(kernel_dims.size()); for (int i = 0; i < kernel_dims.size(); i++) { - kernel_sizes[i] = kernel_dims[i]; + kernel_sizes[i] = static_cast(kernel_dims[i]); } std::vector subm_paddings(paddings), subm_strides(strides); @@ -143,8 +143,8 @@ void Pool3dInferMeta(const MetaTensor& x, const auto& x_dims = x.dims(); DDim out_dims = {1, 1, 1, 1, 1}; - const std::vector& real_kernel_sizes = - PoolResetKernel(kernel_sizes, x_dims[4], x_dims[4]); + const std::vector& real_kernel_sizes = PoolResetKernel( + kernel_sizes, static_cast(x_dims[4]), static_cast(x_dims[4])); GetOutShape( x_dims, real_kernel_sizes, paddings, dilations, strides, &out_dims); out->set_dtype(x.dtype()); diff --git a/paddle/phi/infermeta/ternary.cc b/paddle/phi/infermeta/ternary.cc index bc41536fd9f18..b18ecf48363f0 100644 --- a/paddle/phi/infermeta/ternary.cc +++ b/paddle/phi/infermeta/ternary.cc @@ -1099,7 +1099,7 @@ void ScatterNdAddInferMeta(const MetaTensor& x, const auto& ref_dims = x.dims(); auto ref_dims_size = ref_dims.size(); const auto& index_dims = index.dims(); - auto index_dims_size = index_dims.size(); + int index_dims_size = static_cast(index_dims.size()); const auto& updates_dims = updates.dims(); auto updates_dims_size = updates_dims.size(); @@ -1135,10 +1135,12 @@ void ScatterNdAddInferMeta(const MetaTensor& x, // update.shape = index.shape[:-1] + output.shape[index.shape[-1]:] std::vector r_updates_dims; - for (int64_t i = 0; i < index_dims_size - 1; ++i) { + for (int i = 0; i < index_dims_size - 1; ++i) { r_updates_dims.emplace_back(index_dims[i]); } - for (int64_t i = index_dims[index_dims_size - 1]; i < ref_dims_size; ++i) { + for (int i = static_cast(index_dims[index_dims_size - 1]); + i < ref_dims_size; + ++i) { r_updates_dims.emplace_back(ref_dims[i]); } // check for non-0d updates @@ -1265,11 +1267,11 @@ void SpectralNormInferMeta(const MetaTensor& weight, "Attr(power_iters) should be greater equal then 0, but received %d", power_iters)); - int h = dim_weight[dim]; + int h = static_cast(dim_weight[dim]); int w = 1; for (int i = 0; i < rank_weight; i++) { if (i != dim) { - w *= dim_weight[i]; + w *= static_cast(dim_weight[i]); } } auto dim_u = u.dims(); diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 28b80b58155fa..9932f9612d70f 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -221,7 +221,7 @@ void ArgMinMaxInferMeta(const MetaTensor& x, if (flatten) { all_element_num = phi::product(x_dims); } else { - all_element_num = x_dims[int_axis]; + all_element_num = x_dims[static_cast(int_axis)]; } PADDLE_ENFORCE_LE( all_element_num, @@ -243,11 +243,13 @@ void ArgMinMaxInferMeta(const MetaTensor& x, vec = {}; } } else { - for (int64_t i = 0; i < int_axis; i++) vec.emplace_back(x_dims[i]); + for (int64_t i = 0; i < int_axis; i++) + vec.emplace_back(x_dims[static_cast(i)]); if (keepdims) { vec.emplace_back(static_cast(1)); } - for (int64_t i = int_axis + 1; i < x_rank; i++) vec.emplace_back(x_dims[i]); + for (int64_t i = int_axis + 1; i < x_rank; i++) + vec.emplace_back(x_dims[static_cast(i)]); } out->set_dims(phi::make_ddim(vec)); @@ -314,7 +316,7 @@ void AsComplexInferMeta(const MetaTensor& input, MetaTensor* output) { "Expected the rank of input(X) to be equal to or greater than 1." "But received rank of input(X) = %d", input_rank)); - const int last_dim_size = in_dims[input_rank - 1]; + const int last_dim_size = static_cast(in_dims[input_rank - 1]); PADDLE_ENFORCE_EQ( last_dim_size, 2, @@ -675,7 +677,7 @@ void CropInferMeta(const MetaTensor& x, } auto out_dims = std::vector(shape.size(), -1); - for (size_t i = 0; i < shape_dims.size(); ++i) { + for (int i = 0; i < static_cast(shape_dims.size()); ++i) { if (shape_dims[i] > 0) { out_dims[i] = static_cast(shape_dims[i]); } else { @@ -763,7 +765,7 @@ void DiagEmbedInferMeta( dim1, dim2)); - int new_dim_len = offset_ + x_dims[x_dims.size() - 1]; + int new_dim_len = static_cast(offset_ + x_dims[x_dims.size() - 1]); auto sizes = vectorize(x_dims); sizes.pop_back(); sizes.insert(sizes.begin() + std::min(dim1_, dim2_), new_dim_len); @@ -1171,11 +1173,11 @@ void ExpandInferMeta(const MetaTensor& x, auto out_rank = std::max(static_cast(x_dims.size()), expand_shape.size()); std::vector out_shape(out_rank); - for (size_t i = 0; i < expand_shape.size(); ++i) { + for (int i = 0; i < static_cast(expand_shape.size()); ++i) { if (x_dims[i] == -1) { out_shape[i] = -1; } else if (expand_shape[i] == -1) { - if (static_cast(x_dims.size()) > i) { + if (static_cast(x_dims.size()) > i) { out_shape[i] = x_dims[i]; } else { out_shape[i] = -1; @@ -1257,7 +1259,7 @@ void FFTC2RInferMeta(const MetaTensor& x, out, phi::errors::InvalidArgument("Output of fft_c2r should not be null.")); const phi::DDim x_dim = x.dims(); - const int64_t last_fft_axis = axes.back(); + const int last_fft_axis = static_cast(axes.back()); // only ensure that fft axes' size greater than zero at runtime // they might be -1 to indicate unknown size ar compile time @@ -1322,7 +1324,7 @@ void FFTR2CInferMeta(const MetaTensor& x, out->share_dims(x); } else { phi::DDim out_dim = x.dims(); - const int64_t last_fft_axis = axes.back(); + const int last_fft_axis = static_cast(axes.back()); const int64_t last_fft_dim_size = x_dim[last_fft_axis]; out_dim.at(last_fft_axis) = last_fft_dim_size / 2 + 1; out->set_dims(out_dim); @@ -1379,7 +1381,7 @@ void FlattenWithXShapeInferMeta(const MetaTensor& x, out_shape.reserve(in_dims_size - stop_axis + start_axis + 1); for (int i = 0; i < start_axis; ++i) { - out_shape.push_back(x_dims[i]); + out_shape.push_back(x_dims[i]); // NOLINT } for (int i = start_axis; i <= stop_axis; i++) { if (x_dims[i] == -1 || outer == -1) { @@ -1388,9 +1390,9 @@ void FlattenWithXShapeInferMeta(const MetaTensor& x, outer *= x_dims[i]; } } - out_shape.push_back(outer); + out_shape.push_back(outer); // NOLINT for (int i = stop_axis + 1; i < in_dims_size; i++) { - out_shape.push_back(x_dims[i]); + out_shape.push_back(x_dims[i]); // NOLINT } const auto& out_dims = phi::make_ddim(out_shape); out->set_dims(out_dims); @@ -1600,9 +1602,10 @@ void FoldInferMeta(const MetaTensor& x, std::vector out_dims; // batch_size - out_dims.push_back(in_dims[0]); + out_dims.push_back(in_dims[0]); // NOLINT // output_plane - int output_channels = in_dims[1] / (kernel_width * kernel_height); + int output_channels = + static_cast(in_dims[1] / (kernel_width * kernel_height)); out_dims.push_back(output_channels); int blocks_height = (output_sizes[0] + 2 * paddings[0] - @@ -1730,11 +1733,11 @@ void FrameInferMeta(const MetaTensor& x, int end_axis; if (axis == 0) { - seq_length = x_dims[0]; + seq_length = static_cast(x_dims[0]); start_axis = 1; end_axis = x_rank - 1; } else { - seq_length = x_dims[x_rank - 1]; + seq_length = static_cast(x_dims[x_rank - 1]); start_axis = 0; end_axis = x_rank - 2; } @@ -1859,11 +1862,11 @@ static phi::DDim ValidateShape(const std::vector shape, "be -1. But received shape = [%s], shape[%d] is also -1.", phi::make_ddim(shape), i)); - unk_dim_idx = i; + unk_dim_idx = static_cast(i); output_shape[i] = shape[i]; } else if (shape[i] == 0) { if (static_cast(i) < in_dims.size()) { - output_shape[i] = in_dims[i]; + output_shape[i] = in_dims[static_cast(i)]; } else { PADDLE_ENFORCE_EQ( in_size, @@ -1966,7 +1969,7 @@ void InferMetaFromVecValue(const MetaTensor& x, void InverseInferMeta(const MetaTensor& x, MetaTensor* out) { auto input_dims = x.dims(); - int64_t input_rank = input_dims.size(); + int input_rank = static_cast(input_dims.size()); PADDLE_ENFORCE_GE( input_rank, 2, @@ -1975,7 +1978,7 @@ void InverseInferMeta(const MetaTensor& x, MetaTensor* out) { "But received: Input(Input)'s dimension = %d, shape = [%s].", input_rank, input_dims)); - for (int64_t i = 0; i < input_rank; ++i) { + for (int i = 0; i < input_rank; ++i) { PADDLE_ENFORCE_EQ( (input_dims[i] == -1) || (input_dims[i] > 0), true, @@ -2065,13 +2068,13 @@ void KthvalueInferMeta(const MetaTensor& x, axis)); } std::vector dimvec; - for (int64_t i = 0; i < axis; i++) { + for (int i = 0; i < axis; i++) { dimvec.emplace_back(input_dims[i]); } if (keepdim && dim_size > 0) { dimvec.emplace_back(static_cast(1)); } - for (int64_t i = axis + 1; i < dim_size; i++) { + for (int i = axis + 1; i < dim_size; i++) { dimvec.emplace_back(input_dims[i]); } DDim dims = phi::make_ddim(dimvec); @@ -2148,8 +2151,8 @@ void LUInferMeta(const MetaTensor& x, phi::errors::InvalidArgument("The rank of input must greater than 2.")); out->set_dims(x_dims); out->set_dtype(x.dtype()); - int m = x_dims[x_rank - 1]; - int n = x_dims[x_rank - 2]; + int m = static_cast(x_dims[x_rank - 1]); + int n = static_cast(x_dims[x_rank - 2]); int min_mn = std::min(m, n); auto dims_vec = phi::vectorize(x_dims); PADDLE_ENFORCE_NOT_NULL( @@ -2187,8 +2190,8 @@ void MatrixRankInferMeta(const MetaTensor& x, "The dims of input must be greater than 2.")); if (hermitian) { - int rows = dim_x[dim_x.size() - 2]; - int cols = dim_x[dim_x.size() - 1]; + int rows = static_cast(dim_x[dim_x.size() - 2]); + int cols = static_cast(dim_x[dim_x.size() - 1]); PADDLE_ENFORCE_EQ(rows, cols, phi::errors::InvalidArgument( @@ -2268,7 +2271,7 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x, if (global_pooling) { kernel_size_.resize(static_cast(x_dims.size()) - 2); - for (size_t i = 0; i < kernel_size_.size(); ++i) { + for (int i = 0; i < static_cast(kernel_size_.size()); ++i) { paddings_[i] = 0; kernel_size_[i] = static_cast(x_dims[i + 2]); } @@ -2301,12 +2304,15 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x, output_shape.insert( output_shape.end(), kernel_size_.begin(), kernel_size_.end()); } else { - for (size_t i = 0; i < kernel_size_.size(); ++i) { + for (int i = 0; i < static_cast(kernel_size_.size()); ++i) { if ((!config.is_runtime) && (x_dims[i + 2] < 0)) { output_shape.push_back(x_dims[i + 2]); } else { - output_shape.push_back(funcs::MaxPoolOutputSize( - x_dims[i + 2], kernel_size_[i], paddings_[i], strides[i])); + output_shape.push_back( + funcs::MaxPoolOutputSize(static_cast(x_dims[i + 2]), + kernel_size_[i], + paddings_[i], + strides[i])); } } } @@ -2365,13 +2371,13 @@ void ModeInferMeta(const MetaTensor& x, } std::vector dimvec; - for (int64_t i = 0; i < axis; i++) { + for (int i = 0; i < axis; i++) { dimvec.emplace_back(input_dims[i]); } if (keepdim && dim_size > 0) { dimvec.emplace_back(static_cast(1)); } - for (int64_t i = axis + 1; i < dim_size; i++) { + for (int i = axis + 1; i < dim_size; i++) { dimvec.emplace_back(input_dims[i]); } DDim dims = phi::make_ddim(dimvec); @@ -2391,7 +2397,7 @@ void MultinomialInferMeta(const MetaTensor& x, MetaConfig config) { auto int_num_samples = num_samples.to(); auto x_dim = x.dims(); - int64_t x_rank = x_dim.size(); + int x_rank = static_cast(x_dim.size()); PADDLE_ENFORCE_GT(x_rank, 0, errors::InvalidArgument( @@ -2406,7 +2412,7 @@ void MultinomialInferMeta(const MetaTensor& x, x_rank)); std::vector out_dims(x_rank); - for (int64_t i = 0; i < x_rank - 1; i++) { + for (int i = 0; i < x_rank - 1; i++) { out_dims[i] = x_dim[i]; } @@ -2484,7 +2490,7 @@ void NanmedianInferMeta(const MetaTensor& x, for (int64_t i = 0; i < x_rank; i++) { if (std::find(formated_axis.begin(), formated_axis.end(), i) == formated_axis.end()) { - out_dim.push_back(x_dim[i]); + out_dim.push_back(x_dim[i]); // NOLINT } else if (keep_dim) { out_dim.push_back(1); } @@ -2611,13 +2617,13 @@ void OverlapAddInferMeta(const MetaTensor& x, int start_axis; int end_axis; if (axis == 0) { - n_frames = x_dims[0]; - frame_length = x_dims[1]; + n_frames = static_cast(x_dims[0]); + frame_length = static_cast(x_dims[1]); start_axis = 2; end_axis = x_rank - 1; } else { - n_frames = x_dims[x_rank - 1]; - frame_length = x_dims[x_rank - 2]; + n_frames = static_cast(x_dims[x_rank - 1]); + frame_length = static_cast(x_dims[x_rank - 2]); start_axis = 0; end_axis = x_rank - 3; } @@ -3127,12 +3133,13 @@ void PoolInferMeta(const MetaTensor& x, if ((!config.is_runtime) && (data_dims[i] < 0)) { output_shape.push_back(data_dims[i]); } else { - output_shape.push_back(funcs::PoolOutputSize(data_dims[i], - kernel_size_[i], - paddings_[2 * i], - paddings_[2 * i + 1], - strides[i], - ceil_mode)); + output_shape.push_back( + funcs::PoolOutputSize(static_cast(data_dims[i]), + kernel_size_[i], + paddings_[2 * i], + paddings_[2 * i + 1], + strides[i], + ceil_mode)); } } } @@ -3169,8 +3176,8 @@ void QrInferMeta(const MetaTensor& x, phi::errors::InvalidArgument("the rank of input must greater than 2")); bool compute_q; bool reduced_mode; - int m = x_dims[x_rank - 2]; - int n = x_dims[x_rank - 1]; + int m = static_cast(x_dims[x_rank - 2]); + int n = static_cast(x_dims[x_rank - 1]); int min_mn = std::min(m, n); std::tie(compute_q, reduced_mode) = phi::funcs::ParseQrMode(mode); @@ -3199,7 +3206,7 @@ DDim ReduceInferDim(const MetaTensor& x, const std::vector& axis, bool keep_dim, bool reduce_all) { - auto x_rank = x.dims().size(); + int x_rank = x.dims().size(); std::vector formated_axis = axis; for (size_t i = 0; i < axis.size(); ++i) { @@ -3246,7 +3253,7 @@ DDim ReduceInferDim(const MetaTensor& x, reduce_all = reduce_all || full_dim; std::vector out_dim_vector; - for (int64_t i = 0; i < x_rank; ++i) { + for (int i = 0; i < x_rank; ++i) { if (reduce_all || dims_set.find(i) != dims_set.end()) { if (keep_dim) { out_dim_vector.push_back(1); @@ -3452,7 +3459,7 @@ void ReverseInferMeta(const MetaTensor& x, true, phi::errors::InvalidArgument("'axis' can not be empty.")); const auto& x_dims = x.dims(); - for (int a : axis_data) { + for (int64_t a : axis_data) { PADDLE_ENFORCE_LT(a, x_dims.size(), phi::errors::OutOfRange( @@ -3827,14 +3834,14 @@ void SplitInferMeta(const MetaTensor& x, int num_of_unknow = 0; int sum_of_section = 0; - for (size_t i = 0; i < sections_data.size(); ++i) { + for (int i = 0; i < static_cast(sections_data.size()); ++i) { sections_vec.push_back(sections_data[i]); if (sections_data[i] == unknow_dim_val) { num_of_unknow++; unknow_dim_idx = i; } else { - sum_of_section += sections_data[i]; + sum_of_section += static_cast(sections_data[i]); } } @@ -3958,7 +3965,7 @@ void SqueezeInferMeta(const MetaTensor& x, if (!config.is_runtime && axes.FromTensor()) { // compile time infershape, set all elements to -1. - int output_size = x.dims().size() - axes.GetData().size(); + int output_size = static_cast(x.dims().size() - axes.GetData().size()); if (x.dims().size() == 0 && output_size == -1) { output_size = 0; } @@ -3970,7 +3977,7 @@ void SqueezeInferMeta(const MetaTensor& x, tmp.reserve(axes.GetData().size()); std::for_each(axes.GetData().begin(), axes.GetData().end(), - [&tmp](const int64_t& t) { tmp.push_back(t); }); + [&tmp](const int64_t& t) { tmp.push_back(t); }); // NOLINT auto out_dims = funcs::GetOutputSqueezeShape(tmp, x_dims, false); out->set_dims(out_dims); if (x_dims[0] == out_dims[0]) { @@ -4221,8 +4228,8 @@ void SvdInferMeta(const MetaTensor& x, in_dims.size(), 2, phi::errors::InvalidArgument("the rank of input must greater than 2")); - int m = in_dims[x_rank - 2]; - int n = in_dims[x_rank - 1]; + int m = static_cast(in_dims[x_rank - 2]); + int n = static_cast(in_dims[x_rank - 1]); int k = std::min(m, n); u->set_dims(!full_matrices ? UDDim(in_dims, k) : UDDim(in_dims, m)); vh->set_dims(!full_matrices ? VHDDim(in_dims, k) : VHDDim(in_dims, n)); @@ -4495,7 +4502,7 @@ void TransposeInferMeta(const MetaTensor& x, MetaTensor* out) { auto x_dims = x.dims(); int x_rank = x_dims.size(); - int axis_size = axis.size(); + int axis_size = static_cast(axis.size()); // Note: x_rank > axis_size when fuse squeeze2 + transpose2, else x_rank == // axis_size @@ -4572,7 +4579,7 @@ void UnbindInferMeta(const MetaTensor& x, axis = axis < 0 ? in_dims.size() + axis : axis; for (int i = 0; i < in_dims.size(); ++i) { - if (i != axis) out_dim.push_back(in_dims[i]); + if (i != axis) out_dim.push_back(in_dims[i]); // NOLINT } auto out_dims = phi::make_ddim(out_dim); @@ -4746,17 +4753,18 @@ void UnfoldInferMeta(const MetaTensor& x, dilations[1])); std::vector out_dims; - out_dims.push_back(in_dims[0]); - int output_channels = in_dims[1] * kernel_sizes[0] * kernel_sizes[1]; + out_dims.push_back(in_dims[0]); // NOLINT + int output_channels = + static_cast(in_dims[1] * kernel_sizes[0] * kernel_sizes[1]); out_dims.push_back(output_channels); - int output_height = phi::funcs::CalcOutputSize(in_dims[2], + int output_height = phi::funcs::CalcOutputSize(static_cast(in_dims[2]), kernel_sizes[0], dilations[0], paddings[0], paddings[2], strides[0]); - int output_width = phi::funcs::CalcOutputSize(in_dims[3], + int output_width = phi::funcs::CalcOutputSize(static_cast(in_dims[3]), kernel_sizes[1], dilations[1], paddings[1], @@ -5028,7 +5036,7 @@ void UnsqueezeInferMeta(const MetaTensor& x, "should be in the range of [1, 6] (Eigen limit)")); if (!config.is_runtime && axes.FromTensor()) { // compile time infershape. set all elements to -1. - int output_size = x.dims().size() + axes.GetData().size(); + int output_size = static_cast(x.dims().size() + axes.GetData().size()); std::vector vec_out_dims(output_size, -1); out->set_dtype(x.dtype()); out->set_dims(phi::make_ddim(vec_out_dims)); diff --git a/paddle/phi/kernels/cpu/lars_momentum_kernel.cc b/paddle/phi/kernels/cpu/lars_momentum_kernel.cc index c1f8a0a5fee43..39e59b8f67ff1 100644 --- a/paddle/phi/kernels/cpu/lars_momentum_kernel.cc +++ b/paddle/phi/kernels/cpu/lars_momentum_kernel.cc @@ -36,7 +36,7 @@ void LarsMomentumKernel( std::vector param_out, std::vector velocity_out, std::vector master_param_out) { - int op_num = param.size(); + int op_num = static_cast(param.size()); T mu_ = static_cast(mu); for (int i = 0; i < op_num; ++i) { auto* lr = learning_rate[i]->data(); diff --git a/paddle/phi/kernels/cpu/prelu_kernel.cc b/paddle/phi/kernels/cpu/prelu_kernel.cc index 636a3a4d750d1..f717be836e9bf 100644 --- a/paddle/phi/kernels/cpu/prelu_kernel.cc +++ b/paddle/phi/kernels/cpu/prelu_kernel.cc @@ -30,7 +30,7 @@ void PReluKernel(const Context& dev_ctx, const T* alpha_ptr = alpha.data(); T* o_ptr = dev_ctx.template Alloc(out); - int numel = x.numel(); + int numel = static_cast(x.numel()); auto dim = x.dims(); int index = 0; int i = 0; @@ -38,22 +38,22 @@ void PReluKernel(const Context& dev_ctx, if (data_format == "NCHW") { int temp = 1; for (int j = 2; j < dim.size(); j++) { - temp *= dim[j]; + temp *= static_cast(dim[j]); } for (i = 0; i < numel; i++) { - index = (i / temp) % dim[1]; + index = static_cast((i / temp) % dim[1]); o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[index] * x_ptr[i]; } } else { for (i = 0; i < numel; i++) { - index = i % dim[dim.size() - 1]; + index = static_cast(i % dim[dim.size() - 1]); o_ptr[i] = x_ptr[i] > 0 ? x_ptr[i] : alpha_ptr[index] * x_ptr[i]; } } } else if (mode == "element") { int temp = 1; for (int j = 1; j < dim.size(); j++) { - temp *= dim[j]; + temp *= static_cast(dim[j]); } for (i = 0; i < numel; i++) { index = i % temp; diff --git a/paddle/phi/kernels/cpu/prior_box_kernel.cc b/paddle/phi/kernels/cpu/prior_box_kernel.cc index 1d690e244762a..c289d11069992 100644 --- a/paddle/phi/kernels/cpu/prior_box_kernel.cc +++ b/paddle/phi/kernels/cpu/prior_box_kernel.cc @@ -57,9 +57,10 @@ void PriorBoxKernel(const Context& ctx, step_height = new_step_h; } - int num_priors = new_aspect_ratios.size() * min_sizes.size(); + int num_priors = + static_cast(new_aspect_ratios.size() * min_sizes.size()); if (!max_sizes.empty()) { - num_priors += max_sizes.size(); + num_priors += static_cast(max_sizes.size()); } ctx.template Alloc(out); @@ -148,7 +149,7 @@ void PriorBoxKernel(const Context& ctx, var_et(0, i) = variances[i]; } - int box_num = feature_height * feature_width * num_priors; + int box_num = static_cast(feature_height * feature_width * num_priors); auto var_dim = var->dims(); var->Resize({box_num, static_cast(variances.size())}); diff --git a/paddle/phi/kernels/cpu/psroi_pool_grad_kernel.cc b/paddle/phi/kernels/cpu/psroi_pool_grad_kernel.cc index e9f283bc63133..17539957a0d44 100644 --- a/paddle/phi/kernels/cpu/psroi_pool_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/psroi_pool_grad_kernel.cc @@ -34,10 +34,10 @@ void PsroiPoolGradKernel(const Context& ctx, DenseTensor* dx) { if (dx) { const auto& in_dims = x.dims(); - int input_channels = in_dims[1]; - int height = in_dims[2]; - int width = in_dims[3]; - int rois_num_t = rois.dims()[0]; + int input_channels = static_cast(in_dims[1]); + int height = static_cast(in_dims[2]); + int width = static_cast(in_dims[3]); + int rois_num_t = static_cast(rois.dims()[0]); // set roi batch id DenseTensor rois_batch_id_list; @@ -45,7 +45,7 @@ void PsroiPoolGradKernel(const Context& ctx, int* rois_batch_id_data = ctx.template Alloc(&rois_batch_id_list); int rois_batch_size; if (rois_num.get_ptr()) { - rois_batch_size = rois_num->numel(); + rois_batch_size = static_cast(rois_num->numel()); auto* rois_num_t_data = rois_num->data(); int start = 0; for (int n = 0; n < rois_batch_size; ++n) { @@ -56,7 +56,7 @@ void PsroiPoolGradKernel(const Context& ctx, } } else { auto rois_lod = rois.lod().back(); - rois_batch_size = rois_lod.size() - 1; + rois_batch_size = static_cast(rois_lod.size()) - 1; // calculate batch id index for each roi according to LoD for (int n = 0; n < rois_batch_size; ++n) { for (size_t i = rois_lod[n]; i < rois_lod[n + 1]; ++i) { @@ -73,7 +73,7 @@ void PsroiPoolGradKernel(const Context& ctx, set_zero(ctx, dx, static_cast(0)); // backpropagate gradient per output pixel - int dout_size = dout.numel(); + int dout_size = static_cast(dout.numel()); for (int i = 0; i < dout_size; ++i) { // The output is in order (n, c, ph, pw) int pw = i % pooled_width; diff --git a/paddle/phi/kernels/cpu/psroi_pool_kernel.cc b/paddle/phi/kernels/cpu/psroi_pool_kernel.cc index b921f07746e5d..fe48ee9e7e88e 100644 --- a/paddle/phi/kernels/cpu/psroi_pool_kernel.cc +++ b/paddle/phi/kernels/cpu/psroi_pool_kernel.cc @@ -31,11 +31,11 @@ void PsroiPoolKernel(const Context& ctx, float spatial_scale, DenseTensor* out) { auto in_dims = x.dims(); - int batch_size = in_dims[0]; - int input_channels = in_dims[1]; - int height = in_dims[2]; - int width = in_dims[3]; - int rois_num_t = rois.dims()[0]; + int batch_size = static_cast(in_dims[0]); + int input_channels = static_cast(in_dims[1]); + int height = static_cast(in_dims[2]); + int width = static_cast(in_dims[3]); + int rois_num_t = static_cast(rois.dims()[0]); PADDLE_ENFORCE_EQ(input_channels, output_channels * pooled_height * pooled_width, @@ -55,7 +55,7 @@ void PsroiPoolKernel(const Context& ctx, int rois_batch_size; if (rois_num.get_ptr()) { - rois_batch_size = rois_num->numel(); + rois_batch_size = static_cast(rois_num->numel()); auto* rois_num_data = rois_num->data(); PADDLE_ENFORCE_EQ( rois_batch_size, @@ -84,13 +84,13 @@ void PsroiPoolKernel(const Context& ctx, } } else { auto rois_lod = rois.lod().back(); - rois_batch_size = rois_lod.size() - 1; + rois_batch_size = static_cast(rois_lod.size()) - 1; PADDLE_ENFORCE_EQ( rois_batch_size, batch_size, errors::InvalidArgument("the rois_batch_size and input(X) " "batch_size should be the same.")); - int rois_num_with_lod = rois_lod[rois_batch_size]; + int rois_num_with_lod = static_cast(rois_lod[rois_batch_size]); PADDLE_ENFORCE_EQ(rois_num_with_lod, rois_num_t, errors::InvalidArgument( @@ -127,12 +127,14 @@ void PsroiPoolKernel(const Context& ctx, T bin_size_w = roi_width / static_cast(pooled_width); // calculate each pixel of the output feature map. - int out_roi_offset = n * out_stride[0]; + int out_roi_offset = static_cast(n * out_stride[0]); for (int c = 0; c < output_channels; ++c) { // per category - int out_plane_offset = out_roi_offset + c * out_stride[1]; + int out_plane_offset = + static_cast(out_roi_offset + c * out_stride[1]); for (int ph = 0; ph < pooled_height; ++ph) { - int out_row_offset = out_plane_offset + ph * out_stride[2]; + int out_row_offset = + static_cast(out_plane_offset + ph * out_stride[2]); for (int pw = 0; pw < pooled_width; ++pw) { // calculate w and h at input feature map int hstart = floor(static_cast(ph) * bin_size_h + roi_start_h); @@ -147,14 +149,14 @@ void PsroiPoolKernel(const Context& ctx, int output_index = out_row_offset + pw; int input_channel = (c * pooled_height + ph) * pooled_width + pw; - int input_plane_offset = - roi_batch_id * in_stride[0] + input_channel * in_stride[1]; + int input_plane_offset = static_cast( + roi_batch_id * in_stride[0] + input_channel * in_stride[1]); const T* offset_input_data = input_data + input_plane_offset; T out_sum = 0.; bool is_empty = (hend <= hstart) || (wend <= wstart); for (int ih = hstart; ih < hend; ++ih) { for (int iw = wstart; iw < wend; ++iw) { - int input_index = ih * in_stride[2] + iw; + int input_index = static_cast(ih * in_stride[2] + iw); out_sum += offset_input_data[input_index]; } } diff --git a/paddle/phi/kernels/cpu/qr_kernel.cc b/paddle/phi/kernels/cpu/qr_kernel.cc index 6a5551d95571b..ac61e8e172ae6 100644 --- a/paddle/phi/kernels/cpu/qr_kernel.cc +++ b/paddle/phi/kernels/cpu/qr_kernel.cc @@ -37,11 +37,11 @@ void QrKernel(const Context& ctx, numel, 0, errors::PreconditionNotMet("The input of QR is empty.")); auto x_dims = x.dims(); int x_rank = x_dims.size(); - int m = x_dims[x_rank - 2]; - int n = x_dims[x_rank - 1]; + int m = static_cast(x_dims[x_rank - 2]); + int n = static_cast(x_dims[x_rank - 1]); int min_mn = std::min(m, n); int k = reduced_mode ? min_mn : m; - int batch_size = numel / (m * n); + int batch_size = static_cast(numel / (m * n)); int x_stride = m * n; int q_stride = m * k; int r_stride = k * n; diff --git a/paddle/phi/kernels/cpu/rnn_grad_kernel.cc b/paddle/phi/kernels/cpu/rnn_grad_kernel.cc index 82415b88b662f..bccfa0a95211a 100644 --- a/paddle/phi/kernels/cpu/rnn_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/rnn_grad_kernel.cc @@ -83,8 +83,8 @@ struct GradCell { auto& place = *dev_ctx.eigen_device(); auto mask = EigenMatrix::From( mask_tensor, phi::make_ddim({mask_tensor.dims()[1], 1})); - auto mask_broadcast = - mask.broadcast(Eigen::DSizes(1, grad_pre_hidden->dims()[2])); + auto mask_broadcast = mask.broadcast(Eigen::DSizes( + 1, static_cast(grad_pre_hidden->dims()[2]))); auto pre_hidden_grad = EigenMatrix::Reshape( *grad_pre_hidden, grad_pre_hidden->dims().size() - 1); auto pre_hidden_bak_grad = EigenMatrix::Reshape( @@ -599,8 +599,8 @@ struct GradLayer { auto& place = *dev_ctx.eigen_device(); auto mask = EigenMatrix::From( mask_tensor, phi::make_ddim({mask_tensor.dims()[1], 1})); - auto mask_broadcast = - mask.broadcast(Eigen::DSizes(1, grad_output->dims()[2])); + auto mask_broadcast = mask.broadcast( + Eigen::DSizes(1, static_cast(grad_output->dims()[2]))); auto last_h_grad = EigenMatrix::Reshape(*grad_last_h, grad_last_h->dims().size() - 1); @@ -716,8 +716,8 @@ struct SingleGradLayer : GradLayer { phi::funcs::SetConstant zero; zero(dev_ctx, input_grad, static_cast(0.0)); - int time_step = input->dims()[0]; - int batch_size = input->dims()[1]; + int time_step = static_cast(input->dims()[0]); + int batch_size = static_cast(input->dims()[1]); int direction_num = is_bidirec ? 2 : 1; // in this section, create the gate_state_grad for the postprocess calculate @@ -825,8 +825,8 @@ struct BidirGradLayer : GradLayer { int hidden_size, const std::string& mode, int gate_num) { - int time_step = input->dims()[0]; - int batch_size = input->dims()[1]; + int time_step = static_cast(input->dims()[0]); + int batch_size = static_cast(input->dims()[1]); int direction_num = is_bidirec ? 2 : 1; // split the output two tensor to output_forward, output_backward phi::funcs::SetConstant zero; @@ -1009,8 +1009,8 @@ void RnnGradFunc(const CPUContext& dev_ctx, } // get the input_size, batch_size, time_step - const int time_step = x.dims()[0]; - const int batch_size = x.dims()[1]; + const int time_step = static_cast(x.dims()[0]); + const int batch_size = static_cast(x.dims()[1]); const int direction_num = is_bidirec ? 2 : 1; // allocate the memory and initization the x_grad diff --git a/paddle/phi/kernels/cpu/rnn_kernel.cc b/paddle/phi/kernels/cpu/rnn_kernel.cc index a861f6696473e..606a8f3ff400d 100644 --- a/paddle/phi/kernels/cpu/rnn_kernel.cc +++ b/paddle/phi/kernels/cpu/rnn_kernel.cc @@ -218,7 +218,7 @@ struct Layer { bool is_test, DenseTensor* cache_input) { // crate the temp input for the X * W_ih^T + Bias_ih - const int& hidden_size = weight.dims()[0]; + const int& hidden_size = weight.dims()[0]; // NOLINT cache_input->Resize( phi::make_ddim({input.dims()[0], input.dims()[1], hidden_size})); if (is_test) { @@ -242,8 +242,8 @@ struct Layer { EigenMatrix::Reshape(*cache_input, cache_input->dims().size() - 1); auto bias_ih_tmp = EigenMatrix::From(bias_ih, phi::make_ddim({1, bias_ih.dims()[0]})); - const int row_num = - phi::product(cache_input->dims()) / cache_input->dims()[2]; + const int row_num = static_cast(phi::product(cache_input->dims()) / + cache_input->dims()[2]); in = in + bias_ih_tmp.broadcast(Eigen::DSizes(row_num, 1)); if (is_gru(mode)) { // reset_gate update_gate cell_gate = [1, 1, 0] @@ -279,8 +279,8 @@ struct Layer { mask_tensor, phi::make_ddim({mask_tensor.dims()[1], 1})); auto pre_h = EigenMatrix::Reshape(*init_h, init_h->dims().size() - 1); auto curr_h = EigenMatrix::Reshape(*last_h, last_h->dims().size() - 1); - auto mask_broadcast = - mask.broadcast(Eigen::DSizes(1, output->dims()[2])); + auto mask_broadcast = mask.broadcast( + Eigen::DSizes(1, static_cast(output->dims()[2]))); curr_h.device(place) = out * mask_broadcast + pre_h * (1 - mask_broadcast); out.device(place) = out * mask_broadcast; @@ -332,7 +332,7 @@ struct Layer { is_reverse = true; } } - const int time_step = input->dims()[0]; + const int time_step = static_cast(input->dims()[0]); this->preprocess(dev_ctx, *input, vec[0 + offset * 4], @@ -532,7 +532,7 @@ struct Layer { is_reverse = true; } } - const int time_step = input->dims()[0]; + const int time_step = static_cast(input->dims()[0]); this->preprocess(dev_ctx, *input, vec[0 + offset * 4], @@ -749,9 +749,9 @@ struct BidirLayer : public Layer { std::vector output_vec(2); DenseTensor forward_input_w, forward_cell_value, forward_cell_act_value; DenseTensor backward_input_w, backward_cell_value, backward_cell_act_value; - int time_step = input->dims()[0]; - int batch_size = input->dims()[1]; - int hidden_size = output->dims()[2]; + int time_step = static_cast(input->dims()[0]); + int batch_size = static_cast(input->dims()[1]); + int hidden_size = static_cast(output->dims()[2]); for (int i = 0; i < 2; ++i) { output_vec[i].Resize({time_step, batch_size, hidden_size / 2}); dev_ctx.Alloc(&output_vec[i]); diff --git a/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc b/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc index aa1e06c60a4e7..81868afc46318 100644 --- a/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_align_grad_kernel.cc @@ -85,7 +85,7 @@ void RoiAlignGradKernel(const Context& dev_ctx, int channels = in_dims[1]; int height = in_dims[2]; int width = in_dims[3]; - int rois_num = boxes.dims()[0]; + int rois_num = static_cast(boxes.dims()[0]); if (!dx) { return; @@ -96,7 +96,7 @@ void RoiAlignGradKernel(const Context& dev_ctx, int boxes_batch_size; if (boxes_num) { - boxes_batch_size = boxes_num->numel(); + boxes_batch_size = static_cast(boxes_num->numel()); auto* boxes_num_data = boxes_num->data(); int start = 0; for (int n = 0; n < boxes_batch_size; ++n) { @@ -107,7 +107,7 @@ void RoiAlignGradKernel(const Context& dev_ctx, } } else { auto boxes_lod = boxes.lod().back(); - boxes_batch_size = boxes_lod.size() - 1; + boxes_batch_size = static_cast(boxes_lod.size() - 1); for (int n = 0; n < boxes_batch_size; ++n) { for (std::size_t i = boxes_lod[n]; i < boxes_lod[n + 1]; ++i) { box_batch_id_data[i] = n; @@ -119,7 +119,7 @@ void RoiAlignGradKernel(const Context& dev_ctx, phi::funcs::SetConstant set_zero; set_zero(dev_ctx, dx, static_cast(0)); - int output_grad_size = out_grad.numel(); + int output_grad_size = static_cast(out_grad.numel()); if ((!out_grad.IsInitialized()) || (output_grad_size <= 0)) { return; diff --git a/paddle/phi/kernels/cpu/roi_align_kernel.cc b/paddle/phi/kernels/cpu/roi_align_kernel.cc index d2c3f9a51b7c7..bf303b17fbc8b 100644 --- a/paddle/phi/kernels/cpu/roi_align_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_align_kernel.cc @@ -187,11 +187,11 @@ void RoiAlignKernel(const Context& dev_ctx, bool aligned, DenseTensor* out) { auto in_dims = x.dims(); - int batch_size = in_dims[0]; - int channels = in_dims[1]; - int height = in_dims[2]; - int width = in_dims[3]; - int rois_num = boxes.dims()[0]; + int batch_size = static_cast(in_dims[0]); + int channels = static_cast(in_dims[1]); + int height = static_cast(in_dims[2]); + int width = static_cast(in_dims[3]); + int rois_num = static_cast(boxes.dims()[0]); if (rois_num == 0) { dev_ctx.template Alloc(out); @@ -207,7 +207,7 @@ void RoiAlignKernel(const Context& dev_ctx, int* roi_batch_id_data = roi_batch_id_list.data(); int boxes_batch_size; if (boxes_num) { - boxes_batch_size = boxes_num->numel(); + boxes_batch_size = static_cast(boxes_num->numel()); PADDLE_ENFORCE_EQ( boxes_batch_size, batch_size, @@ -233,7 +233,7 @@ void RoiAlignKernel(const Context& dev_ctx, errors::InvalidArgument("Input(ROIs) Tensor of ROIAlignOp " "does not contain LoD information.")); auto boxes_lod = lod.back(); - int boxes_batch_size = boxes_lod.size() - 1; + int boxes_batch_size = static_cast(boxes_lod.size() - 1); PADDLE_ENFORCE_EQ( boxes_batch_size, batch_size, @@ -243,7 +243,7 @@ void RoiAlignKernel(const Context& dev_ctx, "batch_size = %d", boxes_batch_size, batch_size)); - int boxes_num_with_lod = boxes_lod[boxes_batch_size]; + int boxes_num_with_lod = static_cast(boxes_lod[boxes_batch_size]); PADDLE_ENFORCE_EQ( rois_num, boxes_num_with_lod, diff --git a/paddle/phi/kernels/cpu/roi_pool_grad_kernel.cc b/paddle/phi/kernels/cpu/roi_pool_grad_kernel.cc index f2fcfa5648d3f..704a2b4b610fc 100644 --- a/paddle/phi/kernels/cpu/roi_pool_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_pool_grad_kernel.cc @@ -33,13 +33,13 @@ void RoiPoolGradKernel(const Context& dev_ctx, float spatial_scale, DenseTensor* dx) { if (dx) { - int rois_num = boxes.dims()[0]; + int rois_num = static_cast(boxes.dims()[0]); DenseTensor box_batch_id_list = Empty(dev_ctx, {rois_num}); int* box_batch_id_data = box_batch_id_list.data(); int boxes_batch_size; if (boxes_num) { - boxes_batch_size = boxes_num->numel(); + boxes_batch_size = static_cast(boxes_num->numel()); auto* boxes_num_data = boxes_num->data(); int start = 0; for (int n = 0; n < boxes_batch_size; ++n) { @@ -50,7 +50,7 @@ void RoiPoolGradKernel(const Context& dev_ctx, } } else { auto boxes_lod = boxes.lod().back(); - boxes_batch_size = boxes_lod.size() - 1; + boxes_batch_size = static_cast(boxes_lod.size() - 1); for (int n = 0; n < boxes_batch_size; ++n) { for (size_t i = boxes_lod[n]; i < boxes_lod[n + 1]; ++i) { box_batch_id_data[i] = n; @@ -71,7 +71,7 @@ void RoiPoolGradKernel(const Context& dev_ctx, auto roi_stride = phi::stride(boxes.dims()); auto out_stride = phi::stride(out_grad.dims()); - int channels = x.dims()[1]; + int channels = static_cast(x.dims()[1]); for (int n = 0; n < rois_num; ++n) { int roi_batch_idx = box_batch_id_data[n]; diff --git a/paddle/phi/kernels/cpu/roi_pool_kernel.cc b/paddle/phi/kernels/cpu/roi_pool_kernel.cc index c817a9621bd8c..9208308ed12e2 100644 --- a/paddle/phi/kernels/cpu/roi_pool_kernel.cc +++ b/paddle/phi/kernels/cpu/roi_pool_kernel.cc @@ -31,11 +31,11 @@ void RoiPoolKernel(const Context& dev_ctx, DenseTensor* out, DenseTensor* arg_max) { auto x_dims = x.dims(); - int batch_size = x_dims[0]; - int channels = x_dims[1]; - int height = x_dims[2]; - int width = x_dims[3]; - int rois_num = boxes.dims()[0]; + int batch_size = static_cast(x_dims[0]); + int channels = static_cast(x_dims[1]); + int height = static_cast(x_dims[2]); + int width = static_cast(x_dims[3]); + int rois_num = static_cast(boxes.dims()[0]); if (rois_num == 0) { dev_ctx.template Alloc(out); @@ -54,7 +54,7 @@ void RoiPoolKernel(const Context& dev_ctx, int boxes_batch_size; if (boxes_num) { - boxes_batch_size = boxes_num->numel(); + boxes_batch_size = static_cast(boxes_num->numel()); PADDLE_ENFORCE_EQ( boxes_batch_size, batch_size, @@ -70,13 +70,13 @@ void RoiPoolKernel(const Context& dev_ctx, } } else { auto boxes_lod = boxes.lod().back(); - boxes_batch_size = boxes_lod.size() - 1; + boxes_batch_size = static_cast(boxes_lod.size() - 1); PADDLE_ENFORCE_EQ( boxes_batch_size, batch_size, phi::errors::InvalidArgument("The boxes_batch_size and imgs " "batch_size must be the same.")); - int rois_num_with_lod = boxes_lod[boxes_batch_size]; + int rois_num_with_lod = static_cast(boxes_lod[boxes_batch_size]); PADDLE_ENFORCE_EQ( rois_num, rois_num_with_lod, diff --git a/paddle/phi/kernels/cpu/rrelu_grad_kernel.cc b/paddle/phi/kernels/cpu/rrelu_grad_kernel.cc index 10b6c6b1a3ea8..1f6eed2804f23 100644 --- a/paddle/phi/kernels/cpu/rrelu_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/rrelu_grad_kernel.cc @@ -28,7 +28,7 @@ void RReluGradKernel(const Context& dev_ctx, const T* n_ptr = noise.data(); const T* x_ptr = x.data(); const T* out_grad_ptr = out_grad.data(); - int numel = x.numel(); + int numel = static_cast(x.numel()); if (!x_grad) return; int i = 0; diff --git a/paddle/phi/kernels/cpu/rrelu_kernel.cc b/paddle/phi/kernels/cpu/rrelu_kernel.cc index d5cf21b113112..0344cca0c8862 100644 --- a/paddle/phi/kernels/cpu/rrelu_kernel.cc +++ b/paddle/phi/kernels/cpu/rrelu_kernel.cc @@ -32,7 +32,7 @@ void RReluKernel(const Context& dev_ctx, T* o_ptr = dev_ctx.template Alloc(out); T* n_ptr = dev_ctx.template Alloc(noise); T zero = static_cast(0); - int numel = x.numel(); + int numel = static_cast(x.numel()); int i = 0; if (is_test) { diff --git a/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc b/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc index f5fcc82b9b9e2..5e48b19488ab9 100644 --- a/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc @@ -79,7 +79,7 @@ void GraphSendRecvGradOpKernelLaunchHelper( DenseTensor* x_grad, const DenseTensor* dst_count = nullptr, const DenseTensor* out = nullptr) { - const int& index_size = dst_index.dims()[0]; + const int& index_size = dst_index.dims()[0]; // NOLINT ctx.template Alloc(x_grad); T* p_output = x_grad->data(); diff --git a/paddle/phi/kernels/cpu/send_u_recv_kernel.cc b/paddle/phi/kernels/cpu/send_u_recv_kernel.cc index 73d4fc72af857..3db7844875f24 100644 --- a/paddle/phi/kernels/cpu/send_u_recv_kernel.cc +++ b/paddle/phi/kernels/cpu/send_u_recv_kernel.cc @@ -86,7 +86,7 @@ void GraphSendRecvOpKernelLaunchHelper(const Context& ctx, int64_t out_size, DenseTensor* out, DenseTensor* dst_count = nullptr) { - const int& index_size = src_index.dims()[0]; + const int& index_size = src_index.dims()[0]; // NOLINT const auto& src_dims = x.dims(); int64_t memset_size = 1; diff --git a/paddle/phi/kernels/cpu/send_ue_recv_grad_kernel.cc b/paddle/phi/kernels/cpu/send_ue_recv_grad_kernel.cc index 0d06245e068fc..0ca3be62a3971 100644 --- a/paddle/phi/kernels/cpu/send_ue_recv_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/send_ue_recv_grad_kernel.cc @@ -365,7 +365,7 @@ void GraphSendUERecvGradOpKernelLaunchHelper( DenseTensor* y_grad, const DenseTensor* dst_count = nullptr, const DenseTensor* out = nullptr) { - const int& index_size = dst_index.dims()[0]; + const int& index_size = dst_index.dims()[0]; // NOLINT ctx.template Alloc(x_grad); T* x_grad_data = x_grad->data(); diff --git a/paddle/phi/kernels/cpu/send_ue_recv_kernel.cc b/paddle/phi/kernels/cpu/send_ue_recv_kernel.cc index 4448079a1d700..0dd727811b3ed 100644 --- a/paddle/phi/kernels/cpu/send_ue_recv_kernel.cc +++ b/paddle/phi/kernels/cpu/send_ue_recv_kernel.cc @@ -115,7 +115,7 @@ void GraphSendUERecvOpKernelLaunchHelper(const Context& ctx, int64_t out_size, DenseTensor* out, DenseTensor* dst_count = nullptr) { - const int& index_size = src_index.dims()[0]; + const int& index_size = src_index.dims()[0]; // NOLINT auto out_dims = out->dims(); int64_t memset_size = 1; std::vector dims_ = phi::vectorize(out_dims); diff --git a/paddle/phi/kernels/cpu/send_uv_kernel.cc b/paddle/phi/kernels/cpu/send_uv_kernel.cc index 4d235c4dd7d12..59f334c2b67f4 100644 --- a/paddle/phi/kernels/cpu/send_uv_kernel.cc +++ b/paddle/phi/kernels/cpu/send_uv_kernel.cc @@ -57,7 +57,7 @@ void GraphSendUVOpKernelLaunchHelper(const Context& ctx, const DenseTensor& dst_index, const std::string& message_op, DenseTensor* out) { - const int& index_size = src_index.dims()[0]; + const int& index_size = src_index.dims()[0]; // NOLINT PADDLE_ENFORCE_GT( index_size, 0, diff --git a/paddle/phi/kernels/cpu/sgd_kernel.cc b/paddle/phi/kernels/cpu/sgd_kernel.cc index cad34aed06225..a8e19dd297599 100644 --- a/paddle/phi/kernels/cpu/sgd_kernel.cc +++ b/paddle/phi/kernels/cpu/sgd_kernel.cc @@ -70,9 +70,10 @@ void sgd_dense_param_sparse_grad_impl(const DenseTensor& param, phi::jit::sgd_attr_t attr; attr.param_height = param_out->dims()[0]; attr.param_width = param_out->numel() / attr.param_height; - attr.grad_height = grad_rows.size(); // note: it is not grad->height() + attr.grad_height = + static_cast(grad_rows.size()); // note: it is not grad->height() attr.grad_width = grad_value.numel() / attr.grad_height; - attr.selected_rows_size = grad_rows.size(); + attr.selected_rows_size = static_cast(grad_rows.size()); auto sgd = phi::jit::KernelFuncs, phi::CPUPlace>::Cache().At( diff --git a/paddle/phi/kernels/cpu/shuffle_batch_grad_kernel.cc b/paddle/phi/kernels/cpu/shuffle_batch_grad_kernel.cc index ccc3af4e2c10c..00074d8e87995 100644 --- a/paddle/phi/kernels/cpu/shuffle_batch_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/shuffle_batch_grad_kernel.cc @@ -29,11 +29,11 @@ void ShuffleBatchGradKernel(const Context& dev_ctx, auto embed_size = out_grad.dims()[out_grad.dims().size() - 1]; auto elem_size = 1; for (auto i = 0; i < out_grad.dims().size() - 1; i++) - elem_size *= out_grad.dims()[i]; + elem_size *= static_cast(out_grad.dims()[i]); std::vector idx_vec_grad(elem_size); auto* shuffleidx_data = shuffleidx.data(); - for (size_t i = 0; i < idx_vec_grad.size(); i++) { + for (int i = 0; i < static_cast(idx_vec_grad.size()); i++) { idx_vec_grad[shuffleidx_data[i]] = i; } diff --git a/paddle/phi/kernels/cpu/shuffle_batch_kernel.cc b/paddle/phi/kernels/cpu/shuffle_batch_kernel.cc index a509f407f418d..f7717d5aee4af 100644 --- a/paddle/phi/kernels/cpu/shuffle_batch_kernel.cc +++ b/paddle/phi/kernels/cpu/shuffle_batch_kernel.cc @@ -27,12 +27,13 @@ void ShuffleBatchKernel(const Context& dev_ctx, DenseTensor* shuffleidx, DenseTensor* seed_out) { auto x_embed_size = x.dims()[x.dims().size() - 1]; - auto elem_size = 1; - for (auto i = 0; i < x.dims().size() - 1; i++) elem_size *= x.dims()[i]; + int elem_size = 1; + for (auto i = 0; i < x.dims().size() - 1; i++) + elem_size *= static_cast(x.dims()[i]); std::vector idx_vec; // record shuffled order idx_vec.reserve(elem_size); - for (auto i = 0; i < elem_size; i++) { + for (int i = 0; i < elem_size; i++) { idx_vec.push_back(i); } int64_t seed_int = 0; @@ -48,7 +49,7 @@ void ShuffleBatchKernel(const Context& dev_ctx, std::random_device rnd; int64_t seed_tmp = rnd(); std::default_random_engine rng(seed_tmp); - const int n = idx_vec.size(); + const int n = static_cast(idx_vec.size()); std::vector v(n); std::iota(v.begin(), v.end(), 0); std::vector visit(n, false); @@ -73,7 +74,7 @@ void ShuffleBatchKernel(const Context& dev_ctx, std::shuffle(v.begin(), v.end(), rng); idx_vec[curr] = v.back(); v.pop_back(); - curr = idx_vec[curr]; + curr = static_cast(idx_vec[curr]); } } }; diff --git a/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_grad_kernel.cc b/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_grad_kernel.cc index b31c13e7f64b4..cb1a78bcedd26 100644 --- a/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_grad_kernel.cc @@ -31,7 +31,7 @@ void SigmoidCrossEntropyWithLogitsGradKernel( DenseTensor* in_grad) { auto dx_data = dev_ctx.template Alloc(in_grad); - int limit = in_grad->numel(); + int limit = static_cast(in_grad->numel()); auto x_data = x.data(); auto label_data = label.data(); auto dout_data = out_grad.data(); diff --git a/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_kernel.cc b/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_kernel.cc index 1fdc11d03b34b..6f9d5f9bab38e 100644 --- a/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_kernel.cc +++ b/paddle/phi/kernels/cpu/sigmoid_cross_entropy_with_logits_kernel.cc @@ -32,7 +32,7 @@ void SigmoidCrossEntropyWithLogitsKernel( int ignore_index, DenseTensor* out) { auto out_data = dev_ctx.template Alloc(out); - int limit = out->numel(); + int limit = static_cast(out->numel()); auto x_data = x.data(); auto label_data = label.data(); auto pos_weight_data = diff --git a/paddle/phi/kernels/cpu/stack_grad_kernel.cc b/paddle/phi/kernels/cpu/stack_grad_kernel.cc index e3190b2c74fbe..36057ce07711c 100644 --- a/paddle/phi/kernels/cpu/stack_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/stack_grad_kernel.cc @@ -26,7 +26,7 @@ void StackGradKernel(const Context& dev_ctx, int axis, std::vector x_grad) { if (axis < 0) axis += out.dims().size(); - int n = out.dims()[axis]; + int n = static_cast(out.dims()[axis]); std::vector dx_datas(n); // NOLINT for (int i = 0; i < n; i++) { @@ -38,8 +38,8 @@ void StackGradKernel(const Context& dev_ctx, } auto dy_data = out.data(); int pre = 1; - for (int i = 0; i < axis; ++i) pre *= out.dims()[i]; - int total_num = out.numel(); + for (int i = 0; i < axis; ++i) pre *= static_cast(out.dims()[i]); + int total_num = static_cast(out.numel()); int post = total_num / (n * pre); auto dx_data_arr = dx_datas.data(); phi::funcs::StackGradFunctorForRange( diff --git a/paddle/phi/kernels/cpu/stack_kernel.cc b/paddle/phi/kernels/cpu/stack_kernel.cc index cd43c32dfecc8..39a9dfba1b6a9 100644 --- a/paddle/phi/kernels/cpu/stack_kernel.cc +++ b/paddle/phi/kernels/cpu/stack_kernel.cc @@ -41,8 +41,8 @@ void StackKernel(const Context& dev_ctx, int pre = 1, post = 1; auto& dim = x[0]->dims(); - for (auto i = 0; i < axis; ++i) pre *= dim[i]; - for (auto i = axis; i < dim.size(); ++i) post *= dim[i]; + for (auto i = 0; i < axis; ++i) pre *= static_cast(dim[i]); + for (auto i = axis; i < dim.size(); ++i) post *= static_cast(dim[i]); auto x_data_arr = x_datas.data(); diff --git a/paddle/phi/kernels/cpu/svd_kernel.cc b/paddle/phi/kernels/cpu/svd_kernel.cc index c7a7471d15993..136835876249d 100644 --- a/paddle/phi/kernels/cpu/svd_kernel.cc +++ b/paddle/phi/kernels/cpu/svd_kernel.cc @@ -100,8 +100,8 @@ void SvdKernel(const Context& dev_ctx, DenseTensor trans_x = ::phi::TransposeLast2Dim(dev_ctx, X); auto* x_data = trans_x.data(); auto x_dims = X.dims(); - int rows = x_dims[x_dims.size() - 2]; - int cols = x_dims[x_dims.size() - 1]; + int rows = static_cast(x_dims[x_dims.size() - 2]); + int cols = static_cast(x_dims[x_dims.size() - 1]); // int k = std::min(rows, cols); // int col_u = full ? rows : k; // int col_v = full ? cols : k; @@ -113,7 +113,7 @@ void SvdKernel(const Context& dev_ctx, 0, cols, errors::InvalidArgument("The col of Input(X) should be greater than 0.")); - int batches = numel / (rows * cols); + int batches = static_cast(numel / (rows * cols)); auto* U_out = dev_ctx.template Alloc>(U); auto* VH_out = dev_ctx.template Alloc>(VH); auto* S_out = dev_ctx.template Alloc>(S); diff --git a/paddle/phi/kernels/cpu/temporal_shift_grad_kernel.cc b/paddle/phi/kernels/cpu/temporal_shift_grad_kernel.cc index 3dcd3c9eb49fb..42d0acd901c71 100644 --- a/paddle/phi/kernels/cpu/temporal_shift_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/temporal_shift_grad_kernel.cc @@ -94,21 +94,24 @@ void TemporalShiftGradKernel(const Context& dev_ctx, int t = seg_num; const DataLayout data_layout = phi::StringToDataLayout(data_format_str); - const int nt = output_grad->dims()[0]; - const int c = (data_layout == DataLayout::kNCHW ? output_grad->dims()[1] - : output_grad->dims()[3]); - const int h = (data_layout == DataLayout::kNCHW ? output_grad->dims()[2] - : output_grad->dims()[1]); - const int w = (data_layout == DataLayout::kNCHW ? output_grad->dims()[3] - : output_grad->dims()[2]); + const int nt = static_cast(output_grad->dims()[0]); + const int c = static_cast(data_layout == DataLayout::kNCHW + ? output_grad->dims()[1] + : output_grad->dims()[3]); + const int h = static_cast(data_layout == DataLayout::kNCHW + ? output_grad->dims()[2] + : output_grad->dims()[1]); + const int w = static_cast(data_layout == DataLayout::kNCHW + ? output_grad->dims()[3] + : output_grad->dims()[2]); const int hw = h * w; const int chw = c * hw; const int tchw = t * chw; const int ntchw = nt * chw; - const int c1 = static_cast(c * shift_ratio); - const int c2 = static_cast(c * 2 * shift_ratio); + const int c1 = static_cast(static_cast(c) * shift_ratio); + const int c2 = static_cast(static_cast(c) * 2.f * shift_ratio); DDim in_grad_dims = (data_layout == DataLayout::kNCHW ? phi::make_ddim({nt, c, h, w}) diff --git a/paddle/phi/kernels/cpu/temporal_shift_kernel.cc b/paddle/phi/kernels/cpu/temporal_shift_kernel.cc index 3edd3aa301f98..4e183bf81b499 100644 --- a/paddle/phi/kernels/cpu/temporal_shift_kernel.cc +++ b/paddle/phi/kernels/cpu/temporal_shift_kernel.cc @@ -94,21 +94,21 @@ void TemporalShiftKernel(const Context& dev_ctx, int t = seg_num; const DataLayout data_layout = phi::StringToDataLayout(data_format_str); - const int nt = input->dims()[0]; - const int c = - (data_layout == DataLayout::kNCHW ? input->dims()[1] : input->dims()[3]); - const int h = - (data_layout == DataLayout::kNCHW ? input->dims()[2] : input->dims()[1]); - const int w = - (data_layout == DataLayout::kNCHW ? input->dims()[3] : input->dims()[2]); + const int nt = static_cast(input->dims()[0]); + const int c = static_cast( + data_layout == DataLayout::kNCHW ? input->dims()[1] : input->dims()[3]); + const int h = static_cast( + data_layout == DataLayout::kNCHW ? input->dims()[2] : input->dims()[1]); + const int w = static_cast( + data_layout == DataLayout::kNCHW ? input->dims()[3] : input->dims()[2]); const int hw = h * w; const int chw = c * hw; const int tchw = t * chw; const int ntchw = nt * chw; - const int c1 = static_cast(c * shift_ratio); - const int c2 = static_cast(c * 2 * shift_ratio); + const int c1 = static_cast(static_cast(c) * shift_ratio); + const int c2 = static_cast(static_cast(c) * 2.f * shift_ratio); DDim out_dims = (data_layout == DataLayout::kNCHW ? phi::make_ddim({nt, c, h, w}) diff --git a/paddle/phi/kernels/cpu/top_k_grad_kernel.cc b/paddle/phi/kernels/cpu/top_k_grad_kernel.cc index 1bdcd4dd2547d..d98ca1702e3b5 100644 --- a/paddle/phi/kernels/cpu/top_k_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/top_k_grad_kernel.cc @@ -102,7 +102,7 @@ void TopkGradKernel(const Context& dev_ctx, trans.emplace_back(axis); phi::DDim trans_dims(out_dims); phi::DDim trans_in_dims(in_dims); - for (size_t i = 0; i < trans.size(); i++) { + for (int i = 0; i < static_cast(trans.size()); i++) { trans_dims[i] = out_dims[trans[i]]; trans_in_dims[i] = in_dims[trans[i]]; } @@ -113,7 +113,7 @@ void TopkGradKernel(const Context& dev_ctx, trans_ind.Resize(trans_dims); dev_ctx.template Alloc(&trans_dO); dev_ctx.template Alloc(&trans_ind); - int ndims = trans.size(); + int ndims = static_cast(trans.size()); // Do transpose funcs::TransCompute( diff --git a/paddle/phi/kernels/cpu/top_k_kernel.cc b/paddle/phi/kernels/cpu/top_k_kernel.cc index 8ba69f31adbe2..858be275734d4 100644 --- a/paddle/phi/kernels/cpu/top_k_kernel.cc +++ b/paddle/phi/kernels/cpu/top_k_kernel.cc @@ -198,17 +198,17 @@ void TopkKernel(const Context& dev_ctx, // get the trans input_dims, out_dims phi::DDim trans_dims(in_dims); phi::DDim trans_out_dims(out->dims()); - for (size_t i = 0; i < trans.size(); i++) { + for (int i = 0; i < static_cast(trans.size()); i++) { trans_dims[i] = in_dims[trans[i]]; } - for (size_t i = 0; i < trans.size(); i++) { + for (int i = 0; i < static_cast(trans.size()); i++) { trans_out_dims[i] = out_dims[trans[i]]; } DenseTensor trans_inp; trans_inp.Resize(trans_dims); dev_ctx.template Alloc(&trans_inp); - int ndims = trans.size(); + int ndims = static_cast(trans.size()); // transpose the input value funcs::TransCompute( diff --git a/paddle/phi/kernels/cpu/transpose_kernel.cc b/paddle/phi/kernels/cpu/transpose_kernel.cc index c43c7afc5db7a..73359479770fd 100644 --- a/paddle/phi/kernels/cpu/transpose_kernel.cc +++ b/paddle/phi/kernels/cpu/transpose_kernel.cc @@ -32,7 +32,7 @@ void TransposeKernel(const Context& ctx, std::vector formated_axis = axis; for (size_t i = 0; i < axis.size(); i++) { if (axis[i] < 0) { - formated_axis[i] = axis[i] + x_rank; + formated_axis[i] = static_cast(axis[i] + x_rank); } } @@ -40,7 +40,7 @@ void TransposeKernel(const Context& ctx, if (out->numel() == 0) { return; } - int rank = formated_axis.size(); + int rank = static_cast(formated_axis.size()); switch (rank) { case 0: phi::Copy(ctx, x, ctx.GetPlace(), false, out); diff --git a/paddle/phi/kernels/cpu/triangular_solve_kernel.cc b/paddle/phi/kernels/cpu/triangular_solve_kernel.cc index f2af19792615c..06c897b219984 100644 --- a/paddle/phi/kernels/cpu/triangular_solve_kernel.cc +++ b/paddle/phi/kernels/cpu/triangular_solve_kernel.cc @@ -37,8 +37,8 @@ void TriangularSolveKernel(const Context& dev_ctx, std::vector y_bst_dims_vec; std::tie(x_bst_dims_vec, y_bst_dims_vec) = funcs::MatrixGetBroadcastDims(x, y); - int x_bst_ndim = x_bst_dims_vec.size(); - int y_bst_ndim = y_bst_dims_vec.size(); + int x_bst_ndim = static_cast(x_bst_dims_vec.size()); + int y_bst_ndim = static_cast(y_bst_dims_vec.size()); // Tensor broadcast to 'out' and temp 'x_bst' IntArray x_bst_dims(x_bst_dims_vec); @@ -56,7 +56,7 @@ void TriangularSolveKernel(const Context& dev_ctx, int N = static_cast(y_bst_dims_vec[y_bst_ndim - 1]); int batch_size = 1; for (int i = 0; i < x_bst_ndim - 2; i++) { - batch_size *= x_bst_dims_vec[i]; + batch_size *= static_cast(x_bst_dims_vec[i]); } auto blas = phi::funcs::GetBlas(dev_ctx); diff --git a/paddle/phi/kernels/cpu/trunc_grad_kernel.cc b/paddle/phi/kernels/cpu/trunc_grad_kernel.cc index fe14eb77f5afc..b4d16c0f9ee76 100644 --- a/paddle/phi/kernels/cpu/trunc_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/trunc_grad_kernel.cc @@ -25,7 +25,7 @@ void TruncGradKernel(const Context& dev_ctx, DenseTensor* in_grad) { T* dx_data = dev_ctx.template Alloc(in_grad); - int numel = in_grad->numel(); + int numel = static_cast(in_grad->numel()); memset(dx_data, 0.0, numel * sizeof(T)); } diff --git a/paddle/phi/kernels/cpu/unpool_grad_kernel.cc b/paddle/phi/kernels/cpu/unpool_grad_kernel.cc index 7e87ae23f71cf..c71c7cc563372 100644 --- a/paddle/phi/kernels/cpu/unpool_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/unpool_grad_kernel.cc @@ -40,12 +40,12 @@ void UnpoolGradKernel(const Context& dev_ctx, const T* output_grad_data = out_grad.data(); phi::funcs::SetConstant zero; zero(dev_ctx, x_grad, static_cast(0)); - const int batch_size = x.dims()[0]; - const int input_height = x.dims()[2]; - const int input_width = x.dims()[3]; - const int output_channels = out.dims()[1]; - const int output_height = out.dims()[2]; - const int output_width = out.dims()[3]; + const int batch_size = static_cast(x.dims()[0]); + const int input_height = static_cast(x.dims()[2]); + const int input_width = static_cast(x.dims()[3]); + const int output_channels = static_cast(out.dims()[1]); + const int output_height = static_cast(out.dims()[2]); + const int output_width = static_cast(out.dims()[3]); int input_feasize = input_height * input_width; int output_feasize = output_height * output_width; const int* indices_data = indices.data(); @@ -91,14 +91,14 @@ void Unpool3dGradKernel(const Context& dev_ctx, phi::funcs::SetConstant zero; zero(dev_ctx, x_grad, static_cast(0)); - const int batch_size = x.dims()[0]; - const int input_depth = x.dims()[2]; - const int input_height = x.dims()[3]; - const int input_width = x.dims()[4]; - const int output_channels = out.dims()[1]; - const int output_depth = out.dims()[2]; - const int output_height = out.dims()[3]; - const int output_width = out.dims()[4]; + const int batch_size = static_cast(x.dims()[0]); + const int input_depth = static_cast(x.dims()[2]); + const int input_height = static_cast(x.dims()[3]); + const int input_width = static_cast(x.dims()[4]); + const int output_channels = static_cast(out.dims()[1]); + const int output_depth = static_cast(out.dims()[2]); + const int output_height = static_cast(out.dims()[3]); + const int output_width = static_cast(out.dims()[4]); int input_feasize = input_depth * input_height * input_width; int output_feasize = output_depth * output_height * output_width; const int* indices_data = indices.data(); diff --git a/paddle/phi/kernels/cpu/unpool_kernel.cc b/paddle/phi/kernels/cpu/unpool_kernel.cc index 6ffa9069cbcfc..65660adf1bd27 100644 --- a/paddle/phi/kernels/cpu/unpool_kernel.cc +++ b/paddle/phi/kernels/cpu/unpool_kernel.cc @@ -38,12 +38,12 @@ void UnpoolKernel(const Context& dev_ctx, phi::funcs::SetConstant set_zero; set_zero(dev_ctx, out, static_cast(0)); } - const int batch_size = x.dims()[0]; - const int input_height = x.dims()[2]; - const int input_width = x.dims()[3]; - const int output_channels = out->dims()[1]; - const int output_height = out->dims()[2]; - const int output_width = out->dims()[3]; + const int batch_size = static_cast(x.dims()[0]); + const int input_height = static_cast(x.dims()[2]); + const int input_width = static_cast(x.dims()[3]); + const int output_channels = static_cast(out->dims()[1]); + const int output_height = static_cast(out->dims()[2]); + const int output_width = static_cast(out->dims()[3]); int input_feasize = input_height * input_width; int output_feasize = output_height * output_width; const T* input_data = x.data(); @@ -87,14 +87,14 @@ void Unpool3dKernel(const Context& dev_ctx, phi::funcs::SetConstant set_zero; set_zero(dev_ctx, out, static_cast(0)); } - const int batch_size = x.dims()[0]; - const int input_depth = x.dims()[2]; - const int input_height = x.dims()[3]; - const int input_width = x.dims()[4]; - const int output_channels = out->dims()[1]; - const int output_depth = out->dims()[2]; - const int output_height = out->dims()[3]; - const int output_width = out->dims()[4]; + const int batch_size = static_cast(x.dims()[0]); + const int input_depth = static_cast(x.dims()[2]); + const int input_height = static_cast(x.dims()[3]); + const int input_width = static_cast(x.dims()[4]); + const int output_channels = static_cast(out->dims()[1]); + const int output_depth = static_cast(out->dims()[2]); + const int output_height = static_cast(out->dims()[3]); + const int output_width = static_cast(out->dims()[4]); int input_feasize = input_depth * input_height * input_width; int output_feasize = output_depth * output_height * output_width; const T* input_data = x.data(); diff --git a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc index f72f98f5cf5bd..e137e37a6bd19 100644 --- a/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc +++ b/paddle/phi/kernels/cpu/weighted_sample_neighbors_kernel.cc @@ -219,7 +219,7 @@ void WeightedSampleNeighborsKernel(const Context& dev_ctx, const T* x_data = x.data(); const T* eids_data = (eids.get_ptr() == nullptr ? nullptr : eids.get_ptr()->data()); - int bs = x.dims()[0]; + int bs = static_cast(x.dims()[0]); std::vector output; std::vector output_count; diff --git a/paddle/phi/kernels/cpu/yolo_box_kernel.cc b/paddle/phi/kernels/cpu/yolo_box_kernel.cc index 9e596f6d1da89..c6b1948f3c9a9 100644 --- a/paddle/phi/kernels/cpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_box_kernel.cc @@ -38,13 +38,13 @@ void YoloBoxKernel(const Context& dev_ctx, auto* input = &x; auto* imgsize = &img_size; float scale = scale_x_y; - float bias = -0.5 * (scale - 1.); + float bias = -0.5f * (scale - 1.f); - const int n = input->dims()[0]; - const int h = input->dims()[2]; - const int w = input->dims()[3]; - const int box_num = boxes->dims()[1]; - const int an_num = anchors.size() / 2; + const int n = static_cast(input->dims()[0]); + const int h = static_cast(input->dims()[2]); + const int w = static_cast(input->dims()[3]); + const int box_num = static_cast(boxes->dims()[1]); + const int an_num = static_cast(anchors.size() / 2); int input_size_h = downsample_ratio * h; int input_size_w = downsample_ratio * w; diff --git a/paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc b/paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc index 39d6c7a368ba2..75fcf48cd4acf 100644 --- a/paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc @@ -140,12 +140,12 @@ void YoloLossGradKernel(const Context& dev_ctx, auto input_grad = x_grad; auto* objness_mask = &objectness_mask; - const int n = input_grad->dims()[0]; - const int c = input_grad->dims()[1]; - const int h = input_grad->dims()[2]; - const int w = input_grad->dims()[3]; - const int mask_num = anchor_mask.size(); - const int b = gt_match_mask.dims()[1]; + const int n = static_cast(input_grad->dims()[0]); + const int c = static_cast(input_grad->dims()[1]); + const int h = static_cast(input_grad->dims()[2]); + const int w = static_cast(input_grad->dims()[3]); + const int mask_num = static_cast(anchor_mask.size()); + const int b = static_cast(gt_match_mask.dims()[1]); int input_size = downsample_ratio * h; const int stride = h * w; diff --git a/paddle/phi/kernels/cpu/yolo_loss_kernel.cc b/paddle/phi/kernels/cpu/yolo_loss_kernel.cc index 788f6bb4f0bf6..275e83cc9b40f 100644 --- a/paddle/phi/kernels/cpu/yolo_loss_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_loss_kernel.cc @@ -40,7 +40,7 @@ static T L1Loss(T x, T y) { } static int GetMaskIndex(std::vector mask, int val) { - for (size_t i = 0; i < mask.size(); i++) { + for (int i = 0; i < static_cast(mask.size()); i++) { if (mask[i] == val) { return i; } @@ -196,14 +196,14 @@ void YoloLossKernel(const Context& dev_ctx, auto* input = &x; auto objness_mask = objectness_mask; float scale = scale_x_y; - float bias = -0.5 * (scale - 1.); + float bias = -0.5f * (scale - 1.f); - const int n = input->dims()[0]; - const int h = input->dims()[2]; - const int w = input->dims()[3]; - const int an_num = anchors.size() / 2; - const int mask_num = anchor_mask.size(); - const int b = gt_box.dims()[1]; + const int n = static_cast(input->dims()[0]); + const int h = static_cast(input->dims()[2]); + const int w = static_cast(input->dims()[3]); + const int an_num = static_cast(anchors.size() / 2); + const int mask_num = static_cast(anchor_mask.size()); + const int b = static_cast(gt_box.dims()[1]); int input_size = downsample_ratio * h; const int stride = h * w; diff --git a/paddle/phi/kernels/funcs/jit/benchmark.cc b/paddle/phi/kernels/funcs/jit/benchmark.cc index d5d1da7385797..83a9a4a45d643 100644 --- a/paddle/phi/kernels/funcs/jit/benchmark.cc +++ b/paddle/phi/kernels/funcs/jit/benchmark.cc @@ -97,11 +97,11 @@ struct BenchFunc { for (int i = 0; i < FLAGS_burning; ++i) { tgt(args...); } - auto start = phi::PosixInNsec() * 1e-3; + double start = static_cast(phi::PosixInNsec()) * 1e-3; for (int i = 0; i < FLAGS_repeat; ++i) { tgt(args...); } - auto end = phi::PosixInNsec() * 1e-3; + double end = static_cast(phi::PosixInNsec()) * 1e-3; return static_cast(end - start) / FLAGS_repeat; } }; diff --git a/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc b/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc index b93975c188b01..78d34fa14295c 100644 --- a/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc +++ b/paddle/phi/kernels/selected_rows/cpu/adam_kernel.cc @@ -192,12 +192,12 @@ void AdamDenseParamSparseGradKernel( "multi thread, currently " << param_row_count; } - for (size_t i = 0; i < grad_rows.size(); ++i) { + for (int i = 0; i < static_cast(grad_rows.size()); ++i) { row_id_to_grad_row_offset[grad_rows[i]] = i; } std::vector> fs; - int64_t line_in_each_thread = - param_row_count / FLAGS_inner_op_parallelism + 1; + int64_t line_in_each_thread = static_cast( + param_row_count / FLAGS_inner_op_parallelism + static_cast(1)); for (int i = 0; i < FLAGS_inner_op_parallelism; ++i) { int64_t start = i * line_in_each_thread; int64_t end = (i + 1) * line_in_each_thread; diff --git a/paddle/phi/kernels/selected_rows/hsigmoid_loss_grad_kernel.cc b/paddle/phi/kernels/selected_rows/hsigmoid_loss_grad_kernel.cc index 9d450f1d5dbed..77ae06206f19d 100644 --- a/paddle/phi/kernels/selected_rows/hsigmoid_loss_grad_kernel.cc +++ b/paddle/phi/kernels/selected_rows/hsigmoid_loss_grad_kernel.cc @@ -61,7 +61,7 @@ void HSigmoidLossGradKernel(const Context& ctx, w_grad->set_height(w.dims()[0]); auto* w_grad_value = w_grad->mutable_value(); phi::DDim temp_dim(w.dims()); - temp_dim[0] = real_rows.size(); + temp_dim[0] = static_cast(real_rows.size()); w_grad_value->Resize(temp_dim); phi::HSigmoidLossGradKernelImpl(ctx, x, diff --git a/paddle/phi/kernels/sparse/cpu/coalesce_kernel.cc b/paddle/phi/kernels/sparse/cpu/coalesce_kernel.cc index b8d25741e4254..045b620b9ea95 100644 --- a/paddle/phi/kernels/sparse/cpu/coalesce_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/coalesce_kernel.cc @@ -52,7 +52,7 @@ void CoalesceCooCPUKernel(const CPUContext& dev_ctx, IntT index = x_indexs[i]; if (indices_to_index.find(index) == indices_to_index.end()) { std::vector indexs; - indexs.push_back(i); + indexs.push_back(static_cast(i)); indices_to_index[index] = indexs; } else { indices_to_index[index].push_back(i); diff --git a/paddle/phi/kernels/sparse/cpu/conv_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/conv_grad_kernel.cc index 97d630da141cd..d18bdc4b12e96 100644 --- a/paddle/phi/kernels/sparse/cpu/conv_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/conv_grad_kernel.cc @@ -49,10 +49,12 @@ void Conv3dCooGradCPUKernel(const CPUContext& dev_ctx, const auto& kernel_dims = kernel.dims(); const bool is2D = kernel_dims.size() == 4 ? true : false; const int kernel_size = - is2D ? kernel_dims[0] * kernel_dims[1] - : kernel_dims[0] * kernel_dims[1] * kernel_dims[2]; - const int in_channels = is2D ? kernel_dims[2] : kernel_dims[3]; - const int out_channels = is2D ? kernel_dims[3] : kernel_dims[4]; + static_cast(is2D ? kernel_dims[0] * kernel_dims[1] + : kernel_dims[0] * kernel_dims[1] * kernel_dims[2]); + const int in_channels = + static_cast(is2D ? kernel_dims[2] : kernel_dims[3]); + const int out_channels = + static_cast(is2D ? kernel_dims[3] : kernel_dims[4]); int rulebook_len = 0; const IntT* rulebook_ptr = phi::funcs::sparse::GetRulebookPtr( diff --git a/paddle/phi/kernels/sparse/cpu/conv_kernel.cc b/paddle/phi/kernels/sparse/cpu/conv_kernel.cc index 6339405c3e914..15b1a54640dbc 100644 --- a/paddle/phi/kernels/sparse/cpu/conv_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/conv_kernel.cc @@ -47,8 +47,9 @@ void Conv3dCooCPUKernel(const CPUContext& dev_ctx, const auto& x_dims = x.dims(); const bool is2D = x_dims.size() == 4 ? true : false; const auto& kernel_dims = kernel.dims(); - int kernel_size = is2D ? kernel_dims[0] * kernel_dims[1] - : kernel_dims[0] * kernel_dims[1] * kernel_dims[2]; + int kernel_size = + static_cast(is2D ? kernel_dims[0] * kernel_dims[1] + : kernel_dims[0] * kernel_dims[1] * kernel_dims[2]); int count_tmp = is2D ? 4 : 5; std::vector out_dims_vec(count_tmp, 1); @@ -56,7 +57,7 @@ void Conv3dCooCPUKernel(const CPUContext& dev_ctx, std::vector kernel_sizes(kernel_dims.size()); for (int i = 0; i < kernel_dims.size(); i++) { - kernel_sizes[i] = kernel_dims[i]; + kernel_sizes[i] = static_cast(kernel_dims[i]); } std::vector subm_paddings(paddings), subm_strides(strides); @@ -69,8 +70,10 @@ void Conv3dCooCPUKernel(const CPUContext& dev_ctx, phi::funcs::sparse::GetOutShape( x_dims, kernel_sizes, subm_paddings, dilations, subm_strides, &out_dims); - const int in_channels = is2D ? kernel_dims[2] : kernel_dims[3]; - const int out_channels = is2D ? kernel_dims[3] : kernel_dims[4]; + const int in_channels = + static_cast(is2D ? kernel_dims[2] : kernel_dims[3]); + const int out_channels = + static_cast(is2D ? kernel_dims[3] : kernel_dims[4]); // Second algorithm: // https://pdfs.semanticscholar.org/5125/a16039cabc6320c908a4764f32596e018ad3.pdf @@ -112,7 +115,7 @@ void Conv3dCooCPUKernel(const CPUContext& dev_ctx, UpdateRulebookAndOutIndex( dev_ctx, x, kernel_size, out_channels, out_dims, &tmp_rulebook, out); - n = tmp_rulebook.dims()[1]; + n = static_cast(tmp_rulebook.dims()[1]); rulebook_ptr = tmp_rulebook.data(); phi::funcs::sparse::SaveToTable( diff --git a/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc index 2a609d8c1f073..776f6611fd590 100644 --- a/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc @@ -122,8 +122,8 @@ void CopyCsrValues(const Context& dev_ctx, Copy(dev_ctx, x.cols(), dev_ctx.GetPlace(), false, dx->mutable_cols()); const auto& x_dims = x.dims(); - int batch = x_dims.size() == 2 ? 1 : x_dims[0]; - int rows = x_dims.size() == 2 ? x_dims[0] : x_dims[1]; + int batch = static_cast(x_dims.size() == 2 ? 1 : x_dims[0]); + int rows = static_cast(x_dims.size() == 2 ? x_dims[0] : x_dims[1]); const IntT* x_crows_ptr = x.crows().data(); const IntT* x_cols_ptr = x.cols().data(); diff --git a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc index 517c4c078e66a..d4e240d5e8203 100644 --- a/paddle/phi/kernels/sparse/cpu/mask_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/mask_kernel.cc @@ -52,7 +52,7 @@ void MaskCooCPUKernel(const CPUContext& dev_ctx, const int64_t non_zero_num = mask.nnz(); auto dims_2d = flatten_to_2d(dims, sparse_dim); - const int cols = dims_2d[1]; + const int cols = static_cast(dims_2d[1]); const IntT* indices_ptr = indices.data(); std::vector out_indexs(non_zero_num), sparse_offsets(sparse_dim); diff --git a/paddle/phi/kernels/sparse/cpu/pool_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/pool_grad_kernel.cc index 53d1233fa647a..d9692acb649aa 100644 --- a/paddle/phi/kernels/sparse/cpu/pool_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/pool_grad_kernel.cc @@ -34,8 +34,8 @@ void MaxPoolCooGradCPUKernel(const CPUContext& dev_ctx, const std::vector& kernel_sizes, SparseCooTensor* x_grad) { int kernel_size = kernel_sizes[0] * kernel_sizes[1] * kernel_sizes[2]; - const int channels = x.dims()[4]; - int rulebook_len = rulebook.dims()[1]; + const int channels = static_cast(x.dims()[4]); + int rulebook_len = static_cast(rulebook.dims()[1]); const IntT* rulebook_ptr = rulebook.data(); std::vector offsets(kernel_size + 1); const int* counter_ptr = counter.data(); diff --git a/paddle/phi/kernels/sparse/cpu/pool_kernel.cc b/paddle/phi/kernels/sparse/cpu/pool_kernel.cc index 41e4bd045faa4..ecd25f269dcba 100644 --- a/paddle/phi/kernels/sparse/cpu/pool_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/pool_kernel.cc @@ -42,7 +42,9 @@ void MaxPoolCooCPUKernel(const CPUContext& dev_ctx, const auto& x_dims = x.dims(); int kernel_size = kernel_sizes[0] * kernel_sizes[1] * kernel_sizes[2]; const std::vector& real_kernel_sizes = - phi::funcs::sparse::PoolResetKernel(kernel_sizes, x_dims[4], x_dims[4]); + phi::funcs::sparse::PoolResetKernel(kernel_sizes, + static_cast(x_dims[4]), + static_cast(x_dims[4])); DDim out_dims = {1, 1, 1, 1, 1}; phi::funcs::sparse::GetOutShape( x_dims, real_kernel_sizes, paddings, dilations, strides, &out_dims); @@ -66,7 +68,7 @@ void MaxPoolCooCPUKernel(const CPUContext& dev_ctx, UpdateRulebookAndOutIndex( dev_ctx, x, kernel_size, in_channels, out_dims, rulebook, out); - int rulebook_len = rulebook->dims()[1]; + int rulebook_len = static_cast(rulebook->dims()[1]); const IntT* rulebook_ptr = rulebook->data(); counter->Resize({kernel_size}); diff --git a/paddle/phi/kernels/sparse/cpu/reshape_kernel.cc b/paddle/phi/kernels/sparse/cpu/reshape_kernel.cc index a1413e0f89de8..e8badf3d6e824 100644 --- a/paddle/phi/kernels/sparse/cpu/reshape_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/reshape_kernel.cc @@ -68,7 +68,7 @@ void ReshapeCooCPUKernel(const Context& dev_ctx, for (int i = 0; i < x.sparse_dim(); ++i) { location += x_indices_data[i * x_nnz + j] * x_sparse_part_strides[i]; } - for (size_t i = 0; i < out_sparse_part_dims.size(); ++i) { + for (int i = 0; i < static_cast(out_sparse_part_dims.size()); ++i) { out_indices_data[i * x_nnz + j] = location / out_sparse_part_strides[i]; location %= out_sparse_part_strides[i]; } diff --git a/paddle/phi/kernels/sparse/cpu/softmax_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/softmax_grad_kernel.cc index e7e9577fe8607..3c55d12dcf051 100644 --- a/paddle/phi/kernels/sparse/cpu/softmax_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/softmax_grad_kernel.cc @@ -48,9 +48,9 @@ void SoftmaxCsrGradKernel(const Context& dev_ctx, int row_number = 1; for (int i = 0; i < out_rank - 1; ++i) { if (i < out_rank - 2) { - batch_size *= out_dim[i]; + batch_size *= static_cast(out_dim[i]); } else if (i == out_rank - 2) { - row_number = out_dim[i]; + row_number = static_cast(out_dim[i]); } } diff --git a/paddle/phi/kernels/sparse/cpu/softmax_kernel.cc b/paddle/phi/kernels/sparse/cpu/softmax_kernel.cc index 992b185f4786b..5ccf157a9dd40 100644 --- a/paddle/phi/kernels/sparse/cpu/softmax_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/softmax_kernel.cc @@ -45,9 +45,9 @@ void SoftmaxCsrKernel(const Context& dev_ctx, int row_number = 1; for (int i = 0; i < x_rank - 1; ++i) { if (i < x_rank - 2) { - batch_size *= x_dim[i]; + batch_size *= static_cast(x_dim[i]); } else if (i == x_rank - 2) { - row_number = x_dim[i]; + row_number = static_cast(x_dim[i]); } } diff --git a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc index d50d4568cc3f9..f3d26568f5068 100644 --- a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc @@ -48,9 +48,9 @@ inline int64_t GetNonZeroNum(const DenseTensor& dense, sparse_dim, dims.size())); - auto dims_2d = flatten_to_2d(dims, sparse_dim); - const int rows = dims_2d[0]; - const int cols = dims_2d[1]; + auto dims_2d = flatten_to_2d(dims, static_cast(sparse_dim)); + const int rows = static_cast(dims_2d[0]); + const int cols = static_cast(dims_2d[1]); const T* data = dense.data(); int64_t non_zero_num = 0; @@ -87,15 +87,15 @@ void DenseToCooKernel(const Context& dev_ctx, int64_t* indices_data = indices.data(); T* values_data = values.data(); - auto dims_2d = flatten_to_2d(x_dims, sparse_dim); - const int rows = dims_2d[0]; - const int cols = dims_2d[1]; + auto dims_2d = flatten_to_2d(x_dims, static_cast(sparse_dim)); + const int rows = static_cast(dims_2d[0]); + const int cols = static_cast(dims_2d[1]); int index = 0; for (int i = 0; i < rows; i++) { if (!IsZero(x_data + i * cols, cols)) { int64_t sparse_index = i; - for (int64_t j = sparse_dim - 1; j >= 0; j--) { + for (int j = static_cast(sparse_dim - 1); j >= 0; j--) { indices_data[j * non_zero_num + index] = sparse_index % x_dims[j]; sparse_index /= x_dims[j]; } @@ -138,8 +138,8 @@ void CsrToCooCPUKernel(const CPUContext& dev_ctx, IntT* coo_cols_data = coo_rows_data + non_zero_num; T* coo_values_data = values.data(); - int batch = x_dims.size() == 2 ? 1 : x_dims[0]; - int rows = x_dims.size() == 2 ? x_dims[0] : x_dims[1]; + int batch = static_cast(x_dims.size() == 2 ? 1 : x_dims[0]); + int rows = static_cast(x_dims.size() == 2 ? x_dims[0] : x_dims[1]); int index = 0; for (int b = 0; b < batch; b++) { @@ -182,8 +182,8 @@ void CooToCsrCPUKernel(const CPUContext& dev_ctx, "SparseCsrTensor only support 2-D or 3-D matrix")); const int64_t non_zero_num = x.nnz(); - int batchs = x_dims.size() == 2 ? 1 : x_dims[0]; - int rows = x_dims.size() == 2 ? x_dims[0] : x_dims[1]; + int batchs = static_cast(x_dims.size() == 2 ? 1 : x_dims[0]); + int rows = static_cast(x_dims.size() == 2 ? x_dims[0] : x_dims[1]); phi::DenseTensor crows = phi::Empty(dev_ctx, {batchs * (rows + 1)}); phi::DenseTensor cols = phi::Empty(dev_ctx, {non_zero_num}); @@ -221,9 +221,9 @@ void CooToCsrCPUKernel(const CPUContext& dev_ctx, for (int b = 0; b < batchs; b++) { int batch_start = 0; - int batch_non_zero_num = offsets[b]; + int batch_non_zero_num = static_cast(offsets[b]); if (b > 0) { - batch_start = offsets[b - 1]; + batch_start = static_cast(offsets[b - 1]); batch_non_zero_num -= batch_start; } auto* coo_rows_ptr = coo_rows_data + batch_start; @@ -283,11 +283,11 @@ void CooToDenseCPUKernel(const CPUContext& dev_ctx, int64_t base_offset = 1; for (int64_t i = 0; i < dense_dim; i++) { - base_offset *= dense_dims[sparse_dim + i]; + base_offset *= dense_dims[static_cast(sparse_dim + i)]; } std::vector sparse_offsets(sparse_dim); int64_t offset = 1; - for (int i = sparse_dim - 1; i >= 0; i--) { + for (int i = static_cast(sparse_dim - 1); i >= 0; i--) { sparse_offsets[i] = offset; offset *= dense_dims[i]; } diff --git a/paddle/phi/kernels/sparse/cpu/transpose_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/transpose_grad_kernel.cc index 87822a9375ef5..58a9720e1732b 100644 --- a/paddle/phi/kernels/sparse/cpu/transpose_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/transpose_grad_kernel.cc @@ -26,7 +26,7 @@ namespace sparse { std::vector get_cpu_grad_perm(std::vector perm) { std::vector grad_perm(perm.size()); for (unsigned int i = 0; i < perm.size(); ++i) { - grad_perm[perm[i]] = i; + grad_perm[perm[i]] = static_cast(i); } return grad_perm; } diff --git a/paddle/phi/kernels/sparse/cpu/transpose_kernel.cc b/paddle/phi/kernels/sparse/cpu/transpose_kernel.cc index 366215e2a10de..bee2fe61ded54 100644 --- a/paddle/phi/kernels/sparse/cpu/transpose_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/transpose_kernel.cc @@ -179,7 +179,7 @@ void TransposeCsrKernel(const Context& dev_ctx, int64_t x_cols_offset = 0; int out_cols_index = 0; for (int i = 0; i < x.dims()[0]; ++i) { - int x_crows_index = i * (x_n_rows + 1); + int x_crows_index = static_cast(i * (x_n_rows + 1)); int64_t start = x_crows_data[x_crows_index + k]; int64_t end = x_crows_data[x_crows_index + 1 + k]; out_crows_data[i + 1] = end - start; diff --git a/paddle/phi/kernels/stride/as_strided_kernel.cc b/paddle/phi/kernels/stride/as_strided_kernel.cc index ce8a8c06aeabf..f8aa3d81ffe67 100644 --- a/paddle/phi/kernels/stride/as_strided_kernel.cc +++ b/paddle/phi/kernels/stride/as_strided_kernel.cc @@ -24,8 +24,8 @@ void AsStridedKernel(const Context& dev_ctx, const std::vector& stride, int64_t offset, DenseTensor* out) { - out->Resize(DDim(dims.data(), dims.size())); - out->set_strides(DDim(stride.data(), stride.size())); + out->Resize(DDim(dims.data(), static_cast(dims.size()))); + out->set_strides(DDim(stride.data(), static_cast(stride.size()))); out->set_offset(offset); out->ResetHolder(input.Holder()); } diff --git a/paddle/phi/kernels/stride/diagonal_kernel.cc b/paddle/phi/kernels/stride/diagonal_kernel.cc index 26d8fd5b4aba3..e8929e6773f53 100644 --- a/paddle/phi/kernels/stride/diagonal_kernel.cc +++ b/paddle/phi/kernels/stride/diagonal_kernel.cc @@ -30,25 +30,27 @@ void DiagonalStridedKernel(const Context& dev_ctx, DenseTensor* out) { size_t x_rank = x.dims().size(); if (axis1 < 0) { - axis1 += x_rank; + axis1 += static_cast(x_rank); } if (axis2 < 0) { - axis2 += x_rank; + axis2 += static_cast(x_rank); } int64_t diag_size; - int64_t x_offset = x.offset(); + int64_t x_offset = static_cast(x.offset()); if (offset >= 0) { diag_size = std::max( std::min(x.dims()[axis1], x.dims()[axis2] - offset), 0); if (diag_size != 0) { - x_offset += offset * x.strides()[axis2] * SizeOf(x.dtype()); + x_offset += + static_cast(offset * x.strides()[axis2] * SizeOf(x.dtype())); } } else { diag_size = std::max( std::min(x.dims()[axis1] + offset, x.dims()[axis2]), 0); if (diag_size != 0) { - x_offset -= offset * x.strides()[axis1] * SizeOf(x.dtype()); + x_offset -= + static_cast(offset * x.strides()[axis1] * SizeOf(x.dtype())); } } @@ -62,7 +64,7 @@ void DiagonalStridedKernel(const Context& dev_ctx, stride.push_back(x.strides()[axis1] + x.strides()[axis2]); auto meta = out->meta(); - auto tmp_dim = DDim(shape.data(), shape.size()); + auto tmp_dim = DDim(shape.data(), static_cast(shape.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Diagonal kernel stride compute diff, infer shape @@ -72,7 +74,7 @@ void DiagonalStridedKernel(const Context& dev_ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(stride.data(), stride.size()); + meta.strides = DDim(stride.data(), static_cast(stride.size())); meta.offset = x_offset; out->set_meta(meta); out->ResetHolder(x.Holder()); diff --git a/paddle/phi/kernels/stride/index_select_kernel.cc b/paddle/phi/kernels/stride/index_select_kernel.cc index 069dbc766e1b3..e5db5f9d18f76 100644 --- a/paddle/phi/kernels/stride/index_select_kernel.cc +++ b/paddle/phi/kernels/stride/index_select_kernel.cc @@ -32,15 +32,16 @@ void IndexSelectStridedKernel(const Context& ctx, std::vector shape = phi::vectorize(x.dims()); std::vector stride = phi::vectorize(x.strides()); - int64_t offset = x.offset(); + int64_t offset = static_cast(x.offset()); - offset = offset + index * stride[dim] * SizeOf(output->dtype()); + offset = static_cast(offset + + index * stride[dim] * SizeOf(output->dtype())); shape.erase(shape.begin() + dim); stride.erase(stride.begin() + dim); auto meta = output->meta(); meta.offset = offset; - auto tmp_dim = DDim(shape.data(), shape.size()); + auto tmp_dim = DDim(shape.data(), static_cast(shape.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Index_select kernel stride compute diff, infer " @@ -49,7 +50,7 @@ void IndexSelectStridedKernel(const Context& ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(stride.data(), stride.size()); + meta.strides = DDim(stride.data(), static_cast(stride.size())); output->set_meta(meta); output->ResetHolder(x.Holder()); } diff --git a/paddle/phi/kernels/stride/slice_kernel.cc b/paddle/phi/kernels/stride/slice_kernel.cc index bc96eada0a84d..195034fb92249 100644 --- a/paddle/phi/kernels/stride/slice_kernel.cc +++ b/paddle/phi/kernels/stride/slice_kernel.cc @@ -49,17 +49,18 @@ void SliceStridedKernel(const Context& ctx, std::vector output_dims = phi::vectorize(input.dims()); std::vector output_stride = phi::vectorize(input.strides()); - int64_t output_offset = input.offset(); + int64_t output_offset = static_cast(input.offset()); for (size_t i = 0; i < new_axes.size(); ++i) { - output_offset = output_offset + starts[i] * output_stride[new_axes[i]] * - SizeOf(out->dtype()); + output_offset = static_cast( + output_offset + + starts[i] * output_stride[new_axes[i]] * SizeOf(out->dtype())); output_dims[new_axes[i]] = ends[i] - starts[i]; } std::vector decrease_flag(output_dims.size(), 0); if (decrease_axis.size() > 0) { - for (size_t i = 0; i < decrease_axis.size(); ++i) { + for (int i = 0; i < static_cast(decrease_axis.size()); ++i) { int64_t axis = decrease_axis[i]; decrease_flag[axis] = 1; } @@ -84,7 +85,7 @@ void SliceStridedKernel(const Context& ctx, auto meta = out->meta(); meta.offset = output_offset; - auto tmp_dim = DDim(output_dims.data(), output_dims.size()); + auto tmp_dim = DDim(output_dims.data(), static_cast(output_dims.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Slice kernel stride compute diff, infer shape is @@ -94,7 +95,8 @@ void SliceStridedKernel(const Context& ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(output_stride.data(), output_stride.size()); + meta.strides = + DDim(output_stride.data(), static_cast(output_stride.size())); out->set_meta(meta); out->ResetHolder(input.Holder()); } diff --git a/paddle/phi/kernels/stride/split_kernel.cc b/paddle/phi/kernels/stride/split_kernel.cc index b468f77fbd841..5a92cd3bf7804 100644 --- a/paddle/phi/kernels/stride/split_kernel.cc +++ b/paddle/phi/kernels/stride/split_kernel.cc @@ -28,7 +28,7 @@ void SplitStridedKernel(const Context& dev_ctx, const IntArray& sections UNUSED, const Scalar& axis_scalar, std::vector outs) { - int64_t num = outs.size(); + int64_t num = static_cast(outs.size()); int64_t start = 0; int axis = axis_scalar.to(); diff --git a/paddle/phi/kernels/stride/squeeze_kernel.cc b/paddle/phi/kernels/stride/squeeze_kernel.cc index d31082d22a3a8..90f240c7ce865 100644 --- a/paddle/phi/kernels/stride/squeeze_kernel.cc +++ b/paddle/phi/kernels/stride/squeeze_kernel.cc @@ -49,7 +49,8 @@ void SqueezeInferStridedKernel(const Context& dev_ctx, } else { for (auto& item : axes) { item = item < 0 ? item + input_stride.size() : item; - if (item != 0 && input_stride[item] == input_stride[item - 1]) { + if (item != 0 && input_stride[static_cast(item)] == + input_stride[static_cast(item) - 1]) { axes_set.insert(item); } } @@ -65,7 +66,8 @@ void SqueezeInferStridedKernel(const Context& dev_ctx, auto meta = out->meta(); meta.offset = input.offset(); - meta.strides = DDim(output_stride.data(), output_stride.size()); + meta.strides = + DDim(output_stride.data(), static_cast(output_stride.size())); out->set_meta(meta); return; } @@ -80,7 +82,7 @@ void SqueezeInferStridedKernel(const Context& dev_ctx, } else { for (auto item : axes) { auto axis = item < 0 ? item + input_dims.size() : item; - if (input_dims[axis] == 1) { + if (input_dims[static_cast(axis)] == 1) { axes_set.insert(axis); } } @@ -94,7 +96,7 @@ void SqueezeInferStridedKernel(const Context& dev_ctx, } auto meta = out->meta(); - auto tmp_dim = DDim(output_dims.data(), output_dims.size()); + auto tmp_dim = DDim(output_dims.data(), static_cast(output_dims.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Unsqueeze kernel stride compute diff, infer @@ -104,7 +106,8 @@ void SqueezeInferStridedKernel(const Context& dev_ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(output_stride.data(), output_stride.size()); + meta.strides = + DDim(output_stride.data(), static_cast(output_stride.size())); meta.offset = input.offset(); out->set_meta(meta); out->ResetHolder(input.Holder()); diff --git a/paddle/phi/kernels/stride/strided_slice_kernel.cc b/paddle/phi/kernels/stride/strided_slice_kernel.cc index 648251f416172..9bc36d5b15f97 100644 --- a/paddle/phi/kernels/stride/strided_slice_kernel.cc +++ b/paddle/phi/kernels/stride/strided_slice_kernel.cc @@ -37,7 +37,7 @@ void StridedSliceRawStridedKernel(const Context& dev_ctx, std::vector output_dims = phi::vectorize(input.dims()); std::vector output_stride = phi::vectorize(input.strides()); - int64_t output_offset = input.offset(); + int64_t output_offset = static_cast(input.offset()); for (size_t i = 0; i < axes.size(); ++i) { int64_t axis_size = input.dims()[axes[i]]; @@ -82,7 +82,8 @@ void StridedSliceRawStridedKernel(const Context& dev_ctx, starts[i] = (strides[i] < 0) ? axis_size - 1 : axis_size; } - output_offset += starts[i] * output_stride[axes[i]] * SizeOf(out->dtype()); + output_offset += static_cast(starts[i] * output_stride[axes[i]] * + SizeOf(out->dtype())); output_dims[axes[i]] = dim; output_stride[axes[i]] *= strides[i]; } @@ -107,7 +108,7 @@ void StridedSliceRawStridedKernel(const Context& dev_ctx, auto meta = out->meta(); meta.offset = output_offset; - auto tmp_dim = DDim(output_dims.data(), output_dims.size()); + auto tmp_dim = DDim(output_dims.data(), static_cast(output_dims.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Striede_slice kernel stride compute diff, infer " @@ -116,7 +117,8 @@ void StridedSliceRawStridedKernel(const Context& dev_ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(output_stride.data(), output_stride.size()); + meta.strides = + DDim(output_stride.data(), static_cast(output_stride.size())); out->set_meta(meta); out->ResetHolder(input.Holder()); } diff --git a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc index 222a1acf5ef1c..9207dd6a7fde9 100644 --- a/paddle/phi/kernels/stride/tensor_unfold_kernel.cc +++ b/paddle/phi/kernels/stride/tensor_unfold_kernel.cc @@ -30,7 +30,8 @@ void TensorUnfoldKernel(const Context& dev_ctx, const DDim& input_dims = input.dims(); const DDim& input_stride = input.strides(); - int64_t max_size = input_dims.size() == 0 ? 1 : input_dims[axis]; + int64_t max_size = + input_dims.size() == 0 ? 1 : input_dims[static_cast(axis)]; PADDLE_ENFORCE_LE( size, @@ -48,7 +49,8 @@ void TensorUnfoldKernel(const Context& dev_ctx, std::vector stride(input_dims.size() + 1); shape[input_dims.size()] = size; - stride[input_dims.size()] = input_dims.size() == 0 ? 1 : input_stride[axis]; + stride[input_dims.size()] = + input_dims.size() == 0 ? 1 : input_stride[static_cast(axis)]; for (int i = 0; i < input_dims.size(); ++i) { if (i == axis) { shape[i] = (input_dims[i] - size) / step + 1; @@ -59,8 +61,8 @@ void TensorUnfoldKernel(const Context& dev_ctx, } } - out->Resize(DDim(shape.data(), shape.size())); - out->set_strides(DDim(stride.data(), stride.size())); + out->Resize(DDim(shape.data(), static_cast(shape.size()))); + out->set_strides(DDim(stride.data(), static_cast(stride.size()))); out->set_offset(input.offset()); out->ResetHolder(input.Holder()); } diff --git a/paddle/phi/kernels/stride/transpose_grad_kernel.cc b/paddle/phi/kernels/stride/transpose_grad_kernel.cc index 4ece96ba2eb42..51295658393c4 100644 --- a/paddle/phi/kernels/stride/transpose_grad_kernel.cc +++ b/paddle/phi/kernels/stride/transpose_grad_kernel.cc @@ -28,12 +28,12 @@ void TransposeGradStridedKernel(const Context& dev_ctx, std::vector formated_axis = axis; for (size_t i = 0; i < axis_size; i++) { if (axis[i] < 0) { - formated_axis[i] = axis[i] + axis_size; + formated_axis[i] = static_cast(axis[i] + axis_size); } } std::vector reversed_axis(axis); - for (size_t i = 0; i < axis_size; i++) { + for (int i = 0; i < static_cast(axis_size); i++) { reversed_axis[formated_axis[i]] = i; } diff --git a/paddle/phi/kernels/stride/transpose_kernel.cc b/paddle/phi/kernels/stride/transpose_kernel.cc index 67e252a283ce6..748beb5194d4a 100644 --- a/paddle/phi/kernels/stride/transpose_kernel.cc +++ b/paddle/phi/kernels/stride/transpose_kernel.cc @@ -27,7 +27,7 @@ void TransposeStridedKernel(const Context& ctx, std::vector formated_axis = axis; for (size_t i = 0; i < axis.size(); i++) { if (axis[i] < 0) { - formated_axis[i] = axis[i] + x_rank; + formated_axis[i] = static_cast(axis[i] + x_rank); } } @@ -35,7 +35,7 @@ void TransposeStridedKernel(const Context& ctx, auto in_stride = x.strides(); auto in_dims = x.dims(); meta.strides = in_stride; - for (size_t i = 0; i < formated_axis.size(); i++) { + for (int i = 0; i < static_cast(formated_axis.size()); i++) { meta.strides[i] = in_stride[formated_axis[i]]; meta.dims[i] = in_dims[formated_axis[i]]; } diff --git a/paddle/phi/kernels/stride/unbind_kernel.cc b/paddle/phi/kernels/stride/unbind_kernel.cc index 48e85cc66ecc8..4409fa7e786c7 100644 --- a/paddle/phi/kernels/stride/unbind_kernel.cc +++ b/paddle/phi/kernels/stride/unbind_kernel.cc @@ -24,7 +24,7 @@ void UnbindStridedKernel(const Context& dev_ctx, const DenseTensor& x, int axis, std::vector outs) { - int64_t num = outs.size(); + int64_t num = static_cast(outs.size()); int64_t start = 0; axis = axis < 0 ? axis + x.dims().size() : axis; diff --git a/paddle/phi/kernels/stride/unsqueeze_kernel.cc b/paddle/phi/kernels/stride/unsqueeze_kernel.cc index dfde2a02e2238..45c50c37fe800 100644 --- a/paddle/phi/kernels/stride/unsqueeze_kernel.cc +++ b/paddle/phi/kernels/stride/unsqueeze_kernel.cc @@ -34,7 +34,8 @@ void UnsqueezeInferStridedKernel(const Context& dev_ctx, if (input.Holder() == out->Holder() && input.meta() == out->meta()) { input_dims = phi::vectorize(out->dims()); for (int64_t i = static_cast(axes.size() - 1); i >= 0; --i) { - axes[i] = axes[i] < 0 ? axes[i] + input_dims.size() : axes[i]; + axes[i] = static_cast(axes[i] < 0 ? axes[i] + input_dims.size() + : axes[i]); axes[i] = axes[i] < 0 ? 0 : axes[i]; input_dims.erase(input_dims.begin() + axes[i]); } @@ -43,8 +44,9 @@ void UnsqueezeInferStridedKernel(const Context& dev_ctx, std::vector output_dims = input_dims; std::vector output_stride = input_stride; - for (auto item : axes) { - item = item < 0 ? item + output_dims.size() + 1 : item; + for (int64_t item : axes) { + item = + static_cast(item < 0 ? item + output_dims.size() + 1 : item); item = item < 0 ? 0 : item; int64_t stride = static_cast(item) >= output_dims.size() ? 1 @@ -54,7 +56,7 @@ void UnsqueezeInferStridedKernel(const Context& dev_ctx, } auto meta = out->meta(); - auto tmp_dim = DDim(output_dims.data(), output_dims.size()); + auto tmp_dim = DDim(output_dims.data(), static_cast(output_dims.size())); // if (product(meta.dims) > 0 && meta.dims != tmp_dim) { // PADDLE_THROW( // phi::errors::Fatal("Unsqueeze kernel stride compute diff, infer @@ -64,7 +66,8 @@ void UnsqueezeInferStridedKernel(const Context& dev_ctx, // tmp_dim)); // } meta.dims = tmp_dim; - meta.strides = DDim(output_stride.data(), output_stride.size()); + meta.strides = + DDim(output_stride.data(), static_cast(output_stride.size())); meta.offset = input.offset(); out->set_meta(meta); out->ResetHolder(input.Holder()); diff --git a/paddle/phi/kernels/stride/view_kernel.cc b/paddle/phi/kernels/stride/view_kernel.cc index 216d4de96bf8c..ff92086ff2f88 100644 --- a/paddle/phi/kernels/stride/view_kernel.cc +++ b/paddle/phi/kernels/stride/view_kernel.cc @@ -23,7 +23,7 @@ void ViewShapeKernel(const Context& dev_ctx, const DenseTensor& input, const std::vector& dims, DenseTensor* out) { - DDim new_dims = DDim(dims.data(), dims.size()); + DDim new_dims = DDim(dims.data(), static_cast(dims.size())); DDim stride; if (ReshapeStride(input.dims(), input.strides(), new_dims, stride)) { auto meta = input.meta(); @@ -67,11 +67,11 @@ void ViewDtypeKernel(const Context& dev_ctx, DDim output_dims = input.dims(); output_dims[output_dims.size() - 1] = - output_dims[output_dims.size() - 1] * times; + output_dims[output_dims.size() - 1] * times; // NOLINT DDim output_stride = input.strides(); for (int i = 0; i < output_stride.size(); i++) { - output_stride[i] = output_stride[i] * times; + output_stride[i] = output_stride[i] * times; // NOLINT } output_stride[output_stride.size() - 1] = 1; @@ -115,7 +115,7 @@ void ViewDtypeKernel(const Context& dev_ctx, DDim output_dims = input.dims(); output_dims[output_dims.size() - 1] = - output_dims[output_dims.size() - 1] / times; + output_dims[output_dims.size() - 1] / times; // NOLINT DDim output_stride = input.strides(); for (int i = 0; i < output_stride.size(); i++) { @@ -129,7 +129,7 @@ void ViewDtypeKernel(const Context& dev_ctx, times, input.dtype(), dtype)); - output_stride[i] = output_stride[i] / times; + output_stride[i] = output_stride[i] / times; // NOLINT } output_stride[output_stride.size() - 1] = 1; diff --git a/paddle/phi/kernels/strings/unicode.cc b/paddle/phi/kernels/strings/unicode.cc index 2d92104fdb53f..292160e2b2db1 100644 --- a/paddle/phi/kernels/strings/unicode.cc +++ b/paddle/phi/kernels/strings/unicode.cc @@ -35,10 +35,10 @@ const uint8_t* GetUniFlagMap() { const uint16_t* GetCharcasesMap() { if (utils_map[0] == nullptr) { for (uint32_t i = 0; i < 65536; ++i) { - if (utf8proc_islower(i)) { - CHARCASES_MAP[i] = utf8proc_toupper(i); - } else if (utf8proc_isupper(i)) { - CHARCASES_MAP[i] = utf8proc_tolower(i); + if (utf8proc_islower(static_cast(i))) { + CHARCASES_MAP[i] = utf8proc_toupper(static_cast(i)); + } else if (utf8proc_isupper(static_cast(i))) { + CHARCASES_MAP[i] = utf8proc_tolower(static_cast(i)); } } utils_map[0] = CHARCASES_MAP; From d2fedeac0a35e4df470fced3e19029edd8f13bf2 Mon Sep 17 00:00:00 2001 From: Ghost Screaming Date: Tue, 5 Sep 2023 14:32:57 +0800 Subject: [PATCH 124/194] [Auto Parallel]: Support std::vector input and output for DistTensor. (#56602) * [WIP] Support std::vector input and output for DistTensor. Concat forward and backward are verified. * Polish code for new dist tensor implementation. * Fix bug of DistTensor upgrade. Add support functions for std::vector -> std::vector. * Add support for DistTensor type of std::vector as input or output of operators. Following testcases are passed. 1. concat: std::vector -> phi::Tensor 2. unbind: phi::Tensor -> std::vector 3. broadcast_tensors: std::vector -> std::vector * Polish code. Remove useless comments. * Add update_loss_scaling in skip_op_lists. * Polish code. --- paddle/fluid/eager/tensor_wrapper.h | 12 ++ paddle/phi/api/lib/api_gen_utils.cc | 42 +++- paddle/phi/api/lib/api_gen_utils.h | 4 + paddle/phi/api/lib/data_transform.cc | 42 ++++ paddle/phi/api/lib/data_transform.h | 6 + paddle/phi/api/lib/kernel_dispatch.h | 10 + paddle/phi/api/yaml/generator/dist_api_gen.py | 186 +++++++++++++++--- .../phi/api/yaml/generator/dist_bw_api_gen.py | 11 ++ test/auto_parallel/test_dist_tensor.py | 55 ++++++ 9 files changed, 334 insertions(+), 34 deletions(-) diff --git a/paddle/fluid/eager/tensor_wrapper.h b/paddle/fluid/eager/tensor_wrapper.h index 47b205e6fc76e..f94ec416d4f3c 100644 --- a/paddle/fluid/eager/tensor_wrapper.h +++ b/paddle/fluid/eager/tensor_wrapper.h @@ -32,6 +32,8 @@ #ifndef PADDLE_NO_PYTHON #include "paddle/fluid/eager/hooks.h" #endif +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" namespace egr { class TensorWrapper { @@ -66,6 +68,16 @@ class TensorWrapper { intermidiate_tensor_.set_impl(std::make_shared( std::make_shared(nullptr, 0, tensor.place()), dense_tensor->meta())); + } else if (phi::distributed::DistTensor::classof(tensor.impl().get())) { + // Only Copy Meta + phi::distributed::DistTensor* dist_tensor = + static_cast(tensor.impl().get()); + intermidiate_tensor_.set_impl( + std::make_shared( + phi::DenseTensor(std::make_shared( + nullptr, 0, tensor.place()), + dist_tensor->value().meta()), + dist_tensor->dist_attr())); } else { PADDLE_THROW(paddle::platform::errors::Fatal( "Unrecognized tensor type for no_need_buffer feature")); diff --git a/paddle/phi/api/lib/api_gen_utils.cc b/paddle/phi/api/lib/api_gen_utils.cc index 51875ed9175ec..c6da10d12dea3 100644 --- a/paddle/phi/api/lib/api_gen_utils.cc +++ b/paddle/phi/api/lib/api_gen_utils.cc @@ -192,8 +192,6 @@ std::vector MakeMetaTensor( return meta_tensors; } -/* ------------------ for output ----------------------- */ - phi::DenseTensor* SetKernelOutput(Tensor* out) { if (out) { if (out->impl() == nullptr) { @@ -546,5 +544,45 @@ phi::distributed::DistTensor* SetKernelDistOutput(Tensor* out) { return nullptr; } +std::vector SetKernelDistOutput( + std::vector out) { + std::vector result; + for (auto tmp : out) { + if (tmp) { + // TODO(GhostScreaming): now all dist case are nullptr + if (tmp->impl() == nullptr) { + phi::DenseTensor dense_t; + // TODO(GhostScreaming): polish code, dist_attr is null now + phi::distributed::TensorDistAttr dist_attr; + auto dist_t = + std::make_shared(dense_t, dist_attr); + tmp->set_impl(dist_t); + } + result.emplace_back( + static_cast(tmp->impl().get())); + } else { + result.emplace_back(nullptr); + } + } + return result; +} + +std::vector SetKernelDistOutput( + size_t out_size, std::vector* out) { + out->reserve(out_size); + std::vector results(out_size); + for (size_t i = 0; i < out_size; ++i) { + phi::DenseTensor dense_t; + // TODO(GhostScreaming): polish code, dist_attr is null now + phi::distributed::TensorDistAttr dist_attr; + auto dist_t = + std::make_shared(dense_t, dist_attr); + results[i] = dist_t.get(); + out->emplace_back(); + out->back().set_impl(dist_t); + } + return results; +} + } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/api_gen_utils.h b/paddle/phi/api/lib/api_gen_utils.h index 605423b431a7c..997bb6f8dc805 100644 --- a/paddle/phi/api/lib/api_gen_utils.h +++ b/paddle/phi/api/lib/api_gen_utils.h @@ -140,6 +140,10 @@ void TransStrideLegacy(phi::DeviceContext* dev_ctx, /* ------------------ for auto parallel ----------------------- */ phi::distributed::DistTensor* SetKernelDistOutput(Tensor* out); +std::vector SetKernelDistOutput( + std::vector out); +std::vector SetKernelDistOutput( + size_t out_size, std::vector* out); } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/data_transform.cc b/paddle/phi/api/lib/data_transform.cc index b1559908ebc3f..0e86b84e074fe 100644 --- a/paddle/phi/api/lib/data_transform.cc +++ b/paddle/phi/api/lib/data_transform.cc @@ -632,5 +632,47 @@ std::shared_ptr PrepareDataForDistTensor( return nullptr; } +std::vector> +PrepareDataForDistTensor(const std::vector& input, + const phi::TensorArgDef& target_args_def, + const TransformFlag& transform_flag, + bool is_stride_kernel) { + std::vector> out; + for (auto x : input) { + const auto& tensor_in = x.impl(); + if (tensor_in) { + phi::distributed::DistTensor* dist_tensor = + static_cast(tensor_in.get()); + const phi::DenseTensor& dense_tensor = dist_tensor->value(); + if (!transform_flag.NeedTransform() || !dense_tensor.initialized() || + (!NeedTransformPlace( + dense_tensor.place(), target_args_def.backend, transform_flag) && + !NeedTransformDataType( + dense_tensor.dtype(), target_args_def.dtype, transform_flag) && + !NeedTransformLayout(dense_tensor.layout(), + target_args_def.layout, + dense_tensor.place(), + transform_flag) && + !NeedTransform2Contiguous(is_stride_kernel, + dense_tensor.meta().is_contiguous()))) { + out.push_back( + std::static_pointer_cast(tensor_in)); + continue; + } + phi::DenseTensor trans_in_tensor = TransformData( + dense_tensor, target_args_def, transform_flag, is_stride_kernel); + // TODO(GhostScreaming): The global meta in DistTensor is not changed, + // but the local meta in DenseTensor maybe changed, such as layout + // change(NCHW->NHWC), so the new DistTensor's meta maybe not unified. + VLOG(6) << "PrepareDataForDistTensor return transformed dist tensor"; + out.push_back(std::make_shared( + trans_in_tensor, dist_tensor->dist_attr())); + } else { + out.push_back(nullptr); + } + } + return out; +} + } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/data_transform.h b/paddle/phi/api/lib/data_transform.h index bc59ac8cfa73e..4247317857c23 100644 --- a/paddle/phi/api/lib/data_transform.h +++ b/paddle/phi/api/lib/data_transform.h @@ -180,5 +180,11 @@ std::shared_ptr PrepareDataForDistTensor( const TransformFlag& transform_flag, bool is_stride_kernel); +std::vector> +PrepareDataForDistTensor(const std::vector& input, + const phi::TensorArgDef& target_args_def, + const TransformFlag& transform_flag, + bool is_stride_kernel); + } // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/kernel_dispatch.h b/paddle/phi/api/lib/kernel_dispatch.h index 4fd684b0bd6e7..7ff9ab3b33f53 100644 --- a/paddle/phi/api/lib/kernel_dispatch.h +++ b/paddle/phi/api/lib/kernel_dispatch.h @@ -191,6 +191,16 @@ struct DistTensorTypeParser : ArgsIterator { } } + void operator()(const paddle::optional>& x) { + if (x) { + if (!(x.get_ptr()->empty())) { + for (auto& t : *(x.get_ptr())) { + result &= t.is_dist_tensor(); + } + } + } + } + // skip other type args, these args don't used in kernel selection template void operator()(const T& x) { diff --git a/paddle/phi/api/yaml/generator/dist_api_gen.py b/paddle/phi/api/yaml/generator/dist_api_gen.py index a1d07b55772d3..5c0b642228ba7 100644 --- a/paddle/phi/api/yaml/generator/dist_api_gen.py +++ b/paddle/phi/api/yaml/generator/dist_api_gen.py @@ -75,14 +75,21 @@ auto dist_out_{} = SetKernelDistOutput({}); auto dense_out_{} = const_cast(&dist_out_{}->value()); """ - -# TODO(chenweihang): support vector and tuple output later VECTOR_OUT_CREATION_TEMPLATE = """ + auto dist_out = SetKernelDistOutput({}, &api_output); + std::vector dense_out(dist_out.size()); + for (size_t i = 0; i < dist_out.size(); i++) {{ + dense_out[i] = const_cast(&dist_out[i]->value()); + }} """ MULTI_VECTOR_OUT_CREATION_TEMPLATE = """ - auto dist_out_{} = {}({}, {}); - auto dense_out_{} = const_cast(&dist_out_{}->value()); + auto dist_out_{out_name} = SetKernelDistOutput({size}, {in_name}); + std::vector dense_out_{out_name}(dist_out_{out_name}.size()); + for (size_t i = 0; i < dist_out_{out_name}.size(); i++) {{ + dense_out_{out_name}[i] = const_cast(&dist_out_{out_name}[i]->value()); + }} """ +# TODO(GhostScreaming): support tuple output later TUPLE_OUT_CREATION_TEMPLATE = """ """ @@ -90,13 +97,32 @@ # Call InferMeta now, replace by InferSPMD function later # TODO(chenweihang): InferSPMD function design SINGLE_DIST_META_IN_TEMPLATE = """MakeMetaTensor(*{}.impl()), """ -# TODO(chenweihang): support vector and optional args later -VECTOR_DIST_META_IN_TEMPLATE = """ +VECTOR_DIST_META_IN_TEMPLATE = """{}_meta_ptr_vec, """ +VECTOR_DIST_META_IN_DECL_TEMPLATE = """ + std::vector {name}_meta_vec; + for (auto tmp : {name}) {{ + {name}_meta_vec.emplace_back(MakeMetaTensor(*tmp.impl())); + }} + std::vector {name}_meta_ptr_vec({name}_meta_vec.size()); + for (size_t i=0; i<{name}_meta_ptr_vec.size(); i++) {{ + {name}_meta_ptr_vec[i] = &{name}_meta_vec[i]; + }} """ +# TODO(GhostScreaming): support optional args later OPTIONAL_DIST_VECTOR_META_IN_TEMPLATE = """ """ SINGLE_DIST_META_OUT_DECL_TEMPLATE = """ phi::MetaTensor meta_{}({});""" +VECTOR_DIST_META_OUT_DECL_TEMPLATE = """ + std::vector {name}_meta_vec; + for (auto tmp : {name}) {{ + {name}_meta_vec.emplace_back(phi::MetaTensor(tmp)); + }} + std::vector {name}_meta_ptr_vec({name}.size()); + for (size_t i=0; i<{name}_meta_vec.size(); i++) {{ + {name}_meta_ptr_vec[i] = &{name}_meta_vec[i]; + }} +""" INFER_SPMD_TEMPLATE = """ phi::{}({}{}); """ @@ -120,6 +146,18 @@ auto dist_input_{} = PrepareDataForDistTensor({}, GetKernelInputArgDef(kernel.InputAt({}), kernel_backend), {}, kernel_result.is_stride_kernel); auto input_{} = &dist_input_{}->value(); """ +VECTOR_PREPARE_DATA_TEMPLATE = """ + auto dist_input_{name}_vec = PrepareDataForDistTensor({name}, GetKernelInputArgDef(kernel.InputAt({index}), kernel_backend), {trans_flag}, kernel_result.is_stride_kernel); + std::vector dense_input_{name}_vec; + for (auto tmp : dist_input_{name}_vec) {{ + dense_input_{name}_vec.emplace_back(&tmp->value()); + }} + std::vector dense_input_{name}_meta_vec = MakeMetaTensor(dense_input_{name}_vec); + std::vector dense_input_{name}_meta_ptr_vec(dense_input_{name}_meta_vec.size()); + for (size_t i=0; i(dist_input_{}.get())->value()); @@ -134,13 +172,19 @@ # 6. Infer Local DenseTensor Meta SINGLE_META_IN_TEMPLATE = """MakeMetaTensor(*input_{}), """ -# TODO(chenweihang): support vector and optional args later -VECTOR_META_IN_TEMPLATE = """ -""" +# TODO(GhostScreaming): support optional args later +VECTOR_META_IN_TEMPLATE = """dense_input_{}_meta_ptr_vec, """ OPTIONAL_VECTOR_META_IN_TEMPLATE = """ """ SINGLE_META_OUT_DECL_TEMPLATE = """ phi::MetaTensor meta_{}({});""" +VECTOR_META_OUT_DECL_TEMPLATE = """ + std::vector {name}_meta_vec = MakeMetaTensor({name}); + std::vector {name}_meta_ptr_vec({name}_meta_vec.size()); + for (size_t i=0; i<{name}_meta_vec.size(); i++) {{ + {name}_meta_ptr_vec[i] = &{name}_meta_vec[i]; + }} +""" INFER_META_TEMPLATE = """ phi::{}({}{}); """ @@ -158,6 +202,8 @@ auto* kernel_fn = kernel.GetVariadicKernelFn(); (*kernel_fn)({}, {}); """ +PREFIX_VECTOR_TENSOR_NAME = "dense_input_" +SUFFIX_VECTOR_TENSOR_NAME = "_vec" # 8. Reshard Output OUTPUT_RESHARD_TEMPLATE = """ @@ -175,6 +221,15 @@ # types : [], list of output types # out_size_expr : [], expression for getting size of vector +# TODO(GhostScreaming): Support std::tuple<...> type of input and output later. +skip_op_lists = [ + "check_finite_and_unscale", # std::vector&, const Tensor& -> std::tuple&, Tensor> + "coalesce_tensor", # const std::vector&, DataType, bool, bool, bool, float, bool, int, int, const std::vector&, const std::vector& -> std::tuple, Tensor> + "update_loss_scaling", # std::vector, const Tensor, ... -> std::tuple, Tensor, Tensor, Tensor> + "einsum", + "einsum_grad", # const std::vector&, const std::string& -> std::tuple, std::vector> +] + class DistForwardAPI(ForwardAPI): def __init__(self, api_item_yaml): @@ -184,10 +239,13 @@ def __init__(self, api_item_yaml): def init_dist_api_members(self): self.gene_dist_input_func = { "const Tensor&": { - "dense": self.generate_dense_input, + "dense": self.generate_single_dense_input, }, "const paddle::optional&": { - "dense": self.generate_dense_input, + "dense": self.generate_single_dense_input, + }, + "const std::vector&": { + "dense": self.generate_vector_dense_input, }, } @@ -254,6 +312,10 @@ def generate_output_creation_code(self) -> str: self.dense_output_args.append('dense_out') if self.outputs['types'][0] == 'Tensor': output_creation_code += SINGLE_OUT_CREATION_TEMPLATE + elif self.outputs['types'][0] == 'std::vector': + output_creation_code += VECTOR_OUT_CREATION_TEMPLATE.format( + self.outputs['out_size_expr'][0] + ) else: self.vector_output_size_assertion_check() elif output_num > 1: @@ -285,10 +347,10 @@ def generate_output_creation_code(self) -> str: get_out_code = f"std::get<{i}>(api_output).get_ptr()" if out_type == 'std::vector': - self.vector_output_size_assertion_check(i) + self.vector_output_size_assertion_check() # Special case for inplace vector and inplace optional # TODO(chenweihang): support this branch later - if self.is_inplace_output(): + if self.is_inplace_output(i): set_out_func = "SetInplaceVectorKernelOutput" if self.is_inplace_and_optional_output(i): set_out_func = ( @@ -297,12 +359,9 @@ def generate_output_creation_code(self) -> str: get_out_code = f"std::get<{i}>(api_output)" output_creation_code += ( MULTI_VECTOR_OUT_CREATION_TEMPLATE.format( - i, - set_out_func, - self.outputs['out_size_expr'][i], - get_out_code, - i, - i, + out_name=i, + size=self.outputs['out_size_expr'][i], + in_name=get_out_code, ) ) else: @@ -335,6 +394,7 @@ def generate_infer_spmd_code(self) -> str: if infer_meta['param'] is not None else input_names + attr_names ) + input_meta_code = "" input_args_code = "" for param in infer_meta_params: if param in input_names: @@ -342,6 +402,16 @@ def generate_infer_spmd_code(self) -> str: input_args_code += SINGLE_DIST_META_IN_TEMPLATE.format( param ) + elif ( + self.inputs['input_info'][param] + == "const std::vector&" + ): + input_args_code += VECTOR_DIST_META_IN_TEMPLATE.format( + param + ) + input_meta_code += VECTOR_DIST_META_IN_DECL_TEMPLATE.format( + name=param + ) else: raise ValueError( f"{self.api} : Param of infer_spmd error : {self.inputs['input_info'][param]} type is not supported." @@ -360,8 +430,15 @@ def generate_infer_spmd_code(self) -> str: output_args_code = "" for i, out_name in enumerate(self.dist_output_args): if self.outputs['types'][i] == 'std::vector': - # TODO(chenweihang): support vector output later - pass + output_decl_code += VECTOR_DIST_META_OUT_DECL_TEMPLATE.format( + name=out_name + ) + if len(self.dense_output_args) == 1: + output_args_code += f"{out_name}_meta_ptr_vec, " + else: + output_args_code += ( + f"{out_name} ? {out_name}_meta_ptr_vec : nullptr, " + ) else: output_decl_code += SINGLE_DIST_META_OUT_DECL_TEMPLATE.format( out_name, out_name @@ -374,8 +451,12 @@ def generate_infer_spmd_code(self) -> str: ) output_args_code = output_args_code[:-2] - return output_decl_code + INFER_SPMD_TEMPLATE.format( - infer_meta_func_code, input_args_code, output_args_code + return ( + output_decl_code + + input_meta_code + + INFER_SPMD_TEMPLATE.format( + infer_meta_func_code, input_args_code, output_args_code + ) ) def generate_kernel_selection_code(self) -> str: @@ -386,8 +467,7 @@ def generate_kernel_selection_code(self) -> str: def generate_reshard_input_code(self) -> str: return INPUT_RESHARD_TEMPLATE.format() - # override BaseAPI's method - def generate_dense_input( + def generate_single_dense_input( self, input_name, ): @@ -410,6 +490,26 @@ def generate_dense_input( return input_tensor_code + def generate_vector_dense_input( + self, + input_name, + ): + input_tensor_code = "" + trans_flag = self.gene_trans_flag(input_name) + input_names = self.inputs['names'] + attr_names = self.attrs['names'] + kernel_param = self.kernel['param'] + if kernel_param is None: + kernel_param = input_names + attr_names + + input_tensor_code += VECTOR_PREPARE_DATA_TEMPLATE.format( + name=input_name, + index=kernel_param.index(input_name), + trans_flag=trans_flag, + ) + + return input_tensor_code + def generate_prepare_data_code(self) -> str: input_names = self.inputs['names'] attr_names = self.attrs['names'] @@ -420,7 +520,7 @@ def generate_prepare_data_code(self) -> str: for i, input_name in enumerate(input_names): # set input code if input_name in kernel_param: - # onlu support dense tensor + # only support dense tensor api_tensor_type = self.inputs['input_info'][input_name] phi_tensor_type = 'dense' if api_tensor_type in self.gene_dist_input_func.keys(): @@ -480,6 +580,11 @@ def generate_infer_meta_code(self) -> str: if param in input_names: if self.inputs['input_info'][param] == "const Tensor&": input_args_code += SINGLE_META_IN_TEMPLATE.format(param) + elif ( + self.inputs['input_info'][param] + == "const std::vector&" + ): + input_args_code += VECTOR_META_IN_TEMPLATE.format(param) else: raise ValueError( f"{self.api} : Param of infer_meta error : {self.inputs['input_info'][param]} type is not supported." @@ -498,8 +603,15 @@ def generate_infer_meta_code(self) -> str: output_args_code = "" for i, out_name in enumerate(self.dense_output_args): if self.outputs['types'][i] == 'std::vector': - # TODO(chenweihang): support vector output later - pass + output_decl_code += VECTOR_META_OUT_DECL_TEMPLATE.format( + name=out_name + ) + if len(self.dense_output_args) == 1: + output_args_code += f"{out_name}_meta_ptr_vec, " + else: + output_args_code += ( + f"{out_name} ? {out_name}_meta_ptr_vec : nullptr, " + ) else: output_decl_code += SINGLE_META_OUT_DECL_TEMPLATE.format( out_name, out_name @@ -548,7 +660,11 @@ def generate_kernel_call_code(self) -> str: if input_infos[arg] == "const Tensor&": input_args.append("*" + PREFIX_TENSOR_NAME + arg) elif input_infos[arg] == "const std::vector&": - input_args.append(PREFIX_TENSOR_NAME + arg) + input_args.append( + PREFIX_VECTOR_TENSOR_NAME + + arg + + SUFFIX_VECTOR_TENSOR_NAME + ) else: # do nothing pass @@ -614,12 +730,19 @@ def generate_auto_paralel_branch(self) -> str: ) def check_argument_whether_support_auto_parallel(self): + global skip_op_lists for name in self.inputs['names']: - if self.inputs['input_info'][name] != "const Tensor&": + if self.inputs['input_info'][name] not in [ + "const Tensor&", + "const std::vector&", + ]: return False for out_type in self.outputs['types']: - if out_type != "Tensor": + if out_type not in ["Tensor", "std::vector"]: return False + + if self.kernel['func'][0] in skip_op_lists: + return False return True # override BaseAPI's method @@ -661,7 +784,6 @@ def gene_base_api_code(self, inplace_flag=False): and self.check_argument_whether_support_auto_parallel() ): dist_branch_code = self.generate_auto_paralel_branch() - return API_IMPL_TEMPLATE.format( self.get_return_type(inplace_flag), api_func_name, diff --git a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py index 95a6f94706eee..487d6e3a25720 100644 --- a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py +++ b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py @@ -27,6 +27,13 @@ auto dist_out = SetKernelDistOutput({}); auto dense_out = const_cast(&dist_out->value()); """ +VECTOR_OUT_CREATION_TEMPLATE = """ + auto dist_out = SetKernelDistOutput({name}); + std::vector dense_out(dist_out.size()); + for (size_t i=0; i(&dist_out[i]->value()); + }} +""" INPLACE_OUT_CREATION_TEMPLATE = """ *{} = {}; """ @@ -53,6 +60,10 @@ def generate_output_creation_code(self) -> str: output_creation_code += SINGLE_OUT_CREATION_TEMPLATE.format( self.outputs['names'][0] ) + elif self.outputs['types'][0] == 'std::vector': + output_creation_code += VECTOR_OUT_CREATION_TEMPLATE.format( + name=self.outputs['names'][0] + ) else: self.vector_output_size_assertion_check() elif output_num > 1: diff --git a/test/auto_parallel/test_dist_tensor.py b/test/auto_parallel/test_dist_tensor.py index 0bf2d88db4237..e82a3cc2fe6b2 100644 --- a/test/auto_parallel/test_dist_tensor.py +++ b/test/auto_parallel/test_dist_tensor.py @@ -83,6 +83,61 @@ def test_relu_api_for_dist_tensor(self): dist_out.backward() self.check_tensor_eq(local_in.grad, dist_in.grad) + # input: std::vector, output: phi::Tensor + def test_concat_for_dist_tensor(self): + x1 = np.random.random(size=[4, 4]).astype("float32") + x2 = np.random.random(size=[4, 4]).astype("float32") + x3 = np.random.random(size=[4, 4]).astype("float32") + local_in1, dist_in1 = self.create_local_and_dist_tensor_pair(x1) + local_in2, dist_in2 = self.create_local_and_dist_tensor_pair(x2) + local_in3, dist_in3 = self.create_local_and_dist_tensor_pair(x3) + local_out = paddle.concat([local_in1, local_in2, local_in3]) + dist_out = paddle.concat([dist_in1, dist_in2, dist_in3]) + self.check_tensor_eq(local_out, dist_out) + local_out.backward() + dist_out.backward() + self.check_tensor_eq(local_in1.grad, dist_in1.grad) + self.check_tensor_eq(local_in2.grad, dist_in2.grad) + self.check_tensor_eq(local_in3.grad, dist_in3.grad) + + # input: std::vector, output: std::vector + def test_broadcast_tensors_for_dist_tensor(self): + x1 = np.random.random(size=[4, 4]).astype("float32") + x2 = np.random.random(size=[4, 4]).astype("float32") + local_in1, dist_in1 = self.create_local_and_dist_tensor_pair(x1) + local_in2, dist_in2 = self.create_local_and_dist_tensor_pair(x2) + + local_out1, local_out2 = paddle.broadcast_tensors( + [local_in1, local_in2] + ) + dist_out1, dist_out2 = paddle.broadcast_tensors([dist_in1, dist_in2]) + self.check_tensor_eq(local_out1, dist_out1) + self.check_tensor_eq(local_out2, dist_out2) + + local_out = local_out1 + local_out2 + dist_out = dist_out1 + dist_out2 + + local_out.backward() + dist_out.backward() + self.check_tensor_eq(local_in1.grad, dist_in1.grad) + self.check_tensor_eq(local_in2.grad, dist_in2.grad) + + # input: phi::Tensor, output: std::vector + def test_unbind_api_for_dist_tensor(self): + x = np.random.random(size=[2, 8]).astype("float32") + local_in, dist_in = self.create_local_and_dist_tensor_pair(x) + local_out1, local_out2 = paddle.unbind(local_in, axis=0) + dist_out1, dist_out2 = paddle.unbind(dist_in, axis=0) + self.check_tensor_eq(local_out1, dist_out1) + self.check_tensor_eq(local_out2, dist_out2) + + local_out = local_out1 + local_out2 + dist_out = dist_out1 + dist_out2 + + local_out.backward() + dist_out.backward() + self.check_tensor_eq(local_in.grad, dist_in.grad) + def test_matmul_api_for_dist_tensor(self): x = np.random.random(size=[4, 4]).astype("float32") y = np.random.random(size=[4, 4]).astype("float32") From e08d0646c6009438c05408eb5a3b2cd3fc9ba38d Mon Sep 17 00:00:00 2001 From: XiaociZhang Date: Tue, 5 Sep 2023 15:18:42 +0800 Subject: [PATCH 125/194] [XPU] remove range check for ignore index (#56869) * [XPU] remove range check for ignore index * add a log --- .../c_softmax_with_cross_entropy_op_xpu.cc | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/operators/collective/c_softmax_with_cross_entropy_op_xpu.cc b/paddle/fluid/operators/collective/c_softmax_with_cross_entropy_op_xpu.cc index fa17b99e79f2a..ec4f872a7a8ca 100644 --- a/paddle/fluid/operators/collective/c_softmax_with_cross_entropy_op_xpu.cc +++ b/paddle/fluid/operators/collective/c_softmax_with_cross_entropy_op_xpu.cc @@ -34,12 +34,9 @@ class CSoftmaxWithCrossEntropyOp : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const int64_t ignore_index = ctx.Attr("ignore_index"); - PADDLE_ENFORCE_LT(ignore_index, - 0, - platform::errors::InvalidArgument( - "When SoftmaxWithCrossEntropy run on XPU, " - "ignore_index should be <=0, however it's %ld", - ignore_index)); + if (ignore_index >= 0) { + LOG_FIRST_N(INFO, 1) << "XPU does not support ignore_index in mp."; + } const int rid = ctx.Attr("ring_id"); auto map = distributed::ProcessGroupMapFromGid::getInstance(); if (map->has(rid)) { @@ -467,12 +464,9 @@ class CSoftmaxWithCrossEntropyGrad : public framework::OpKernel { const phi::DenseTensor* softmax = context.Input("Softmax"); const int64_t ignore_index = context.Attr("ignore_index"); - PADDLE_ENFORCE_LT(ignore_index, - 0, - platform::errors::InvalidArgument( - "When SoftmaxWithCrossEntropy run on XPU, " - "ignore_index should be <=0, however it's %ld", - ignore_index)); + if (ignore_index >= 0) { + LOG_FIRST_N(INFO, 1) << "XPU does not support ignore_index in mp."; + } const int rank = context.Attr("rank"); auto& dev_ctx = context.template device_context(); From 89b91021b77d9f1ba54efe97410a842363e79a09 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Tue, 5 Sep 2023 17:26:12 +0800 Subject: [PATCH 126/194] add informata for strided grad kernel (#56947) --- paddle/phi/kernels/stride/diagonal_grad_kernel.cc | 5 +++++ paddle/phi/kernels/stride/index_select_grad_kernel.cc | 5 +++++ paddle/phi/kernels/stride/strided_slice_grad_kernel.cc | 4 ++++ paddle/phi/kernels/stride/tensor_unfold_grad_kernel.cc | 5 +++++ 4 files changed, 19 insertions(+) diff --git a/paddle/phi/kernels/stride/diagonal_grad_kernel.cc b/paddle/phi/kernels/stride/diagonal_grad_kernel.cc index 21f4b7564e660..d5ebcd6f4ab8a 100644 --- a/paddle/phi/kernels/stride/diagonal_grad_kernel.cc +++ b/paddle/phi/kernels/stride/diagonal_grad_kernel.cc @@ -36,6 +36,11 @@ void DiagonalGradStridedKernel(const Context& dev_ctx, dev_ctx, *in_grad, 0, in_grad); })); DenseTensor tmp; + tmp.set_layout(out_grad.layout()); + tmp.set_lod(out_grad.lod()); + tmp.set_type(out_grad.dtype()); + tmp.Resize(out_grad.dims()); + DiagonalStridedKernel(dev_ctx, *in_grad, offset, axis1, axis2, &tmp); PD_VISIT_ALL_TYPES(out_grad.dtype(), "DiagonalGradStridedKernel", ([&] { phi::StridedCopyKernel( diff --git a/paddle/phi/kernels/stride/index_select_grad_kernel.cc b/paddle/phi/kernels/stride/index_select_grad_kernel.cc index 977c5c51e4927..15ab602fe5304 100644 --- a/paddle/phi/kernels/stride/index_select_grad_kernel.cc +++ b/paddle/phi/kernels/stride/index_select_grad_kernel.cc @@ -34,6 +34,11 @@ void IndexSelectGradStridedKernel(const Context& dev_ctx, dev_ctx, *x_grad, 0, x_grad); })); DenseTensor tmp; + tmp.set_layout(out_grad.layout()); + tmp.set_lod(out_grad.lod()); + tmp.set_type(out_grad.dtype()); + tmp.Resize(out_grad.dims()); + IndexSelectStridedKernel(dev_ctx, *x_grad, index, dim, &tmp); PD_VISIT_ALL_TYPES(out_grad.dtype(), "IndexSelectGradStridedKernel", ([&] { phi::StridedCopyKernel( diff --git a/paddle/phi/kernels/stride/strided_slice_grad_kernel.cc b/paddle/phi/kernels/stride/strided_slice_grad_kernel.cc index 5e55f38d34285..9b2d03a00e86e 100644 --- a/paddle/phi/kernels/stride/strided_slice_grad_kernel.cc +++ b/paddle/phi/kernels/stride/strided_slice_grad_kernel.cc @@ -38,6 +38,10 @@ void StridedSliceRawGradStridedKernel(const Context& dev_ctx, dev_ctx, *x_grad, 0, x_grad); })); DenseTensor tmp; + tmp.set_layout(out_grad.layout()); + tmp.set_lod(out_grad.lod()); + tmp.set_type(out_grad.dtype()); + tmp.Resize(out_grad.dims()); StridedSliceRawStridedKernel(dev_ctx, *x_grad, axes, diff --git a/paddle/phi/kernels/stride/tensor_unfold_grad_kernel.cc b/paddle/phi/kernels/stride/tensor_unfold_grad_kernel.cc index 5f9e03ac53396..620d7bbb46ddc 100644 --- a/paddle/phi/kernels/stride/tensor_unfold_grad_kernel.cc +++ b/paddle/phi/kernels/stride/tensor_unfold_grad_kernel.cc @@ -40,6 +40,11 @@ void TensorUnfoldGradKernel(const Context& dev_ctx, })); } DenseTensor tmp; + tmp.set_layout(out_grad.layout()); + tmp.set_lod(out_grad.lod()); + tmp.set_type(out_grad.dtype()); + tmp.Resize(out_grad.dims()); + TensorUnfoldKernel(dev_ctx, *input_grad, axis, size, step, &tmp); PD_VISIT_ALL_TYPES(out_grad.dtype(), "TensorUnfoldGradKernel", ([&] { phi::StridedCopyKernel( From c71f5f9ca793954c9b2e6bd30761714e604ea09e Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Tue, 5 Sep 2023 17:36:32 +0800 Subject: [PATCH 127/194] fix bug (#56951) --- .../fleet_executor/test/CMakeLists.txt | 260 ++++++++---------- 1 file changed, 114 insertions(+), 146 deletions(-) diff --git a/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt b/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt index e3eb700e91bf9..d7f0a0478505f 100644 --- a/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt +++ b/paddle/fluid/distributed/fleet_executor/test/CMakeLists.txt @@ -1,154 +1,122 @@ -get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME) -set_source_files_properties( - interceptor_ping_pong_test.cc PROPERTIES COMPILE_FLAGS - ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old(interceptor_ping_pong_test SRCS interceptor_ping_pong_test.cc - DEPS fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - interceptor_ping_pong_test - SRCS - interceptor_ping_pong_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# NOTE(risemeup1):The compile target of these unit tests requires occupying more than 4GB of memory, +# which lead a intractable compilation failure issue on Coverage CI. +# We temporarily disabled them, and will restore after all compilation issues are resolved. -set_source_files_properties( - compute_interceptor_test.cc PROPERTIES COMPILE_FLAGS - ${DISTRIBUTE_COMPILE_FLAGS}) +# get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME) +# set_source_files_properties( +# interceptor_ping_pong_test.cc PROPERTIES COMPILE_FLAGS +# ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old(interceptor_ping_pong_test SRCS interceptor_ping_pong_test.cc +# DEPS fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old( +# interceptor_ping_pong_test +# SRCS +# interceptor_ping_pong_test.cc +# DEPS +# ${paddle_lib} +# python +# fleet_executor) +# endif() -if(WIN32 AND WITH_TESTING) - cc_test_old(compute_interceptor_test SRCS compute_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - compute_interceptor_test - SRCS - compute_interceptor_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# set_source_files_properties( +# compute_interceptor_test.cc PROPERTIES COMPILE_FLAGS +# ${DISTRIBUTE_COMPILE_FLAGS}) -set_source_files_properties( - source_interceptor_test.cc PROPERTIES COMPILE_FLAGS - ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old(source_interceptor_test SRCS source_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - source_interceptor_test - SRCS - source_interceptor_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# if(WIN32 AND WITH_TESTING) +# cc_test_old(compute_interceptor_test SRCS compute_interceptor_test.cc DEPS +# fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old(compute_interceptor_test SRCS compute_interceptor_test.cc DEPS +# ${paddle_lib} python) +# endif() -set_source_files_properties( - sink_interceptor_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old(sink_interceptor_test SRCS sink_interceptor_test.cc DEPS - fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - sink_interceptor_test - SRCS - sink_interceptor_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# set_source_files_properties( +# source_interceptor_test.cc PROPERTIES COMPILE_FLAGS +# ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old(source_interceptor_test SRCS source_interceptor_test.cc DEPS +# fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old(source_interceptor_test SRCS source_interceptor_test.cc DEPS +# ${paddle_lib} python) +# endif() -set_source_files_properties( - interceptor_pipeline_short_path_test.cc - PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old( - interceptor_pipeline_short_path_test SRCS - interceptor_pipeline_short_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - interceptor_pipeline_short_path_test - SRCS - interceptor_pipeline_short_path_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# set_source_files_properties( +# sink_interceptor_test.cc PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old(sink_interceptor_test SRCS sink_interceptor_test.cc DEPS +# fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old(sink_interceptor_test SRCS sink_interceptor_test.cc DEPS +# ${paddle_lib} python) +# endif() -set_source_files_properties( - interceptor_pipeline_long_path_test.cc PROPERTIES COMPILE_FLAGS - ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old( - interceptor_pipeline_long_path_test SRCS - interceptor_pipeline_long_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) -else() - cc_test_old( - interceptor_pipeline_long_path_test - SRCS - interceptor_pipeline_long_path_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) -endif() +# set_source_files_properties( +# interceptor_pipeline_short_path_test.cc +# PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old( +# interceptor_pipeline_short_path_test SRCS +# interceptor_pipeline_short_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old(interceptor_pipeline_short_path_test SRCS +# interceptor_pipeline_short_path_test.cc DEPS ${paddle_lib} python) +# endif() -set_source_files_properties( - compute_interceptor_run_op_test.cc PROPERTIES COMPILE_FLAGS - ${DISTRIBUTE_COMPILE_FLAGS}) -if(WIN32 AND WITH_TESTING) - cc_test_old( - compute_interceptor_run_op_test - SRCS - compute_interceptor_run_op_test.cc - DEPS - fleet_executor - naive_executor - fill_constant_op - op_registry - elementwise_add_op - scope - device_context - ${BRPC_DEPS}) -else() - cc_test_old( - compute_interceptor_run_op_test - SRCS - compute_interceptor_run_op_test.cc - DEPS - ${paddle_lib} - python - fleet_executor - fill_constant_op - elementwise_add_op) -endif() +# set_source_files_properties( +# interceptor_pipeline_long_path_test.cc PROPERTIES COMPILE_FLAGS +# ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old( +# interceptor_pipeline_long_path_test SRCS +# interceptor_pipeline_long_path_test.cc DEPS fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old(interceptor_pipeline_long_path_test SRCS +# interceptor_pipeline_long_path_test.cc DEPS ${paddle_lib} python) +# endif() -if(WITH_DISTRIBUTE AND NOT WITH_PSLIB) - set_source_files_properties( - interceptor_ping_pong_with_brpc_test.cc - PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) - if(WIN32 AND WITH_TESTING) - cc_test_old( - interceptor_ping_pong_with_brpc_test SRCS - interceptor_ping_pong_with_brpc_test.cc DEPS fleet_executor ${BRPC_DEPS}) - else() - cc_test_old( - interceptor_ping_pong_with_brpc_test - SRCS - interceptor_ping_pong_with_brpc_test.cc - DEPS - ${paddle_lib} - python - fleet_executor) - endif() -endif() +# set_source_files_properties( +# compute_interceptor_run_op_test.cc PROPERTIES COMPILE_FLAGS +# ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old( +# compute_interceptor_run_op_test +# SRCS +# compute_interceptor_run_op_test.cc +# DEPS +# fleet_executor +# naive_executor +# fill_constant_op +# op_registry +# elementwise_add_op +# scope +# device_context +# ${BRPC_DEPS}) +# else() +# cc_test_old( +# compute_interceptor_run_op_test +# SRCS +# compute_interceptor_run_op_test.cc +# DEPS +# ${paddle_lib} +# python +# fill_constant_op +# elementwise_add_op) +# endif() + +# if(WITH_DISTRIBUTE AND NOT WITH_PSLIB) +# set_source_files_properties( +# interceptor_ping_pong_with_brpc_test.cc +# PROPERTIES COMPILE_FLAGS ${DISTRIBUTE_COMPILE_FLAGS}) +# if(WIN32 AND WITH_TESTING) +# cc_test_old( +# interceptor_ping_pong_with_brpc_test SRCS +# interceptor_ping_pong_with_brpc_test.cc DEPS fleet_executor ${BRPC_DEPS}) +# else() +# cc_test_old( +# interceptor_ping_pong_with_brpc_test SRCS +# interceptor_ping_pong_with_brpc_test.cc DEPS ${paddle_lib} python) +# endif() +# endif() From 99ae88f10d2293c18cadfed06156b5bd79510ee0 Mon Sep 17 00:00:00 2001 From: cyber-pioneer <116002591+cyber-pioneer@users.noreply.github.com> Date: Tue, 5 Sep 2023 19:15:26 +0800 Subject: [PATCH 128/194] fix addn infermeta (#56934) * fix addn infermeta * fix rule bug --- .../op_generator/vjp_interface_gen_op_list.py | 14 +++ .../dialect/paddle_dialect/ir/pd_manual_op.cc | 4 +- .../dialect/paddle_dialect/ir/pd_manual_op.h | 2 +- python/paddle/decomposition/decomp.py | 11 ++- python/paddle/decomposition/rules.py | 39 ++++++++ test/prim/new_ir_prim/CMakeLists.txt | 2 +- test/prim/new_ir_prim/test_prim_simpnet.py | 94 +++++++++++++++++++ 7 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 test/prim/new_ir_prim/test_prim_simpnet.py diff --git a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py index 4fc85a07511f6..cb130ae0b2365 100644 --- a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py +++ b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py @@ -30,6 +30,13 @@ "add", "concat", "split", + "gelu", + "matmul", + "erf", + "multiply", + "subtract", + "pow", + "rsqrt", ] vjp_interface_implementation_gen_op_list = [ "tanh", @@ -38,4 +45,11 @@ "add", "concat", "split", + "gelu", + "matmul", + "erf", + "multiply", + "subtract", + "pow", + "rsqrt", ] diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index 64cb1d69b210a..33469fef8fa32 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -39,8 +39,8 @@ OpInfoTuple AddNOp::GetOpInfo() { std::vector attributes = {}; std::vector outputs = { OpOutputInfo("out", "paddle::dialect::DenseTensorType", false, false)}; - paddle::dialect::OpRunTimeInfo run_time_info = - OpRunTimeInfo("", {""}, {""}, {""}, {""}, {}, {}, {}); + paddle::dialect::OpRunTimeInfo run_time_info = OpRunTimeInfo( + "AddNInferMeta", {"inputs"}, {"add_n"}, {"inputs"}, {}, {}, {}, {}); return std::make_tuple(inputs, attributes, outputs, run_time_info, "add_n"); } diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h index fe9beb46012ed..b0ff45d9baaff 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h @@ -34,7 +34,7 @@ paddle::dialect::AddNOp, paddle::dialect::SplitGradOp namespace paddle { namespace dialect { -class AddNOp : public ir::Op { +class AddNOp : public ir::Op { public: using Op::Op; static const char *name() { return "pd.add_n"; } diff --git a/python/paddle/decomposition/decomp.py b/python/paddle/decomposition/decomp.py index 940249d0ae526..c166c131d10cb 100644 --- a/python/paddle/decomposition/decomp.py +++ b/python/paddle/decomposition/decomp.py @@ -169,11 +169,14 @@ def decompose( dst_vars, op_filter, ) - for item in dst_vars: + for idx, item in enumerate(dst_vars): if not isinstance(item, ir.OpResult): - raise TypeError( - f"Each var in dst_vars should map corresponding var in src_vars, but got type {type(item)} in {dst_vars}." - ) + if item is None: + dst_vars[idx] = src_vars[idx] + else: + raise TypeError( + f"Each var in dst_vars should map corresponding var in src_vars, but got type {type(item)} in {dst_vars}." + ) logging.debug( "Decompose composite forward ops finish: {}".format( core.prim_config["composite_ops_record"] diff --git a/python/paddle/decomposition/rules.py b/python/paddle/decomposition/rules.py index 4184dbdbea62e..ef225ce461382 100644 --- a/python/paddle/decomposition/rules.py +++ b/python/paddle/decomposition/rules.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from paddle import _ir_ops + from .primitives import * # noqa: F403 from .register import register_decomp @@ -34,3 +36,40 @@ def mean(x, axis, keepdim): ) res = divide(sum_x, norm) return res + + +@register_decomp('pd.gelu') +def gelu_composite(x, approximate): + """define composite rule of op gelu""" + M_SQRT1_2 = ( + 0.70710678118654752440 # /* 1/sqrt(2) */ copy from gelu-kernel.cc + ) + M_2_SQRTPI = 1.12837916709551257390 # /* 2/sqrt(pi) */ + full_shape = x.shape if len(x.shape) == 0 else [1] + one = ones(full_shape, x.dtype) + half = full(full_shape, 0.5, x.dtype) + # Todo(cz): after symbol overload, add and multiply will be replaced by "+" and "*" + if approximate: + # gelu(x) = 0.5 * x * (1 + tanh(sqrt(2 / \pi) * (x + 0.044715 * x^{3}))) + kAlpha = full(full_shape, M_2_SQRTPI * M_SQRT1_2, x.dtype) + GELU_CONSTANT = full(full_shape, 0.044715, x.dtype) + tanh_out = tanh(kAlpha * (x + GELU_CONSTANT * x * x * x)) + out = x * half * (one + tanh_out) + return out + + else: + # gelu(x) = 0.5 * x * (1 + erf(x / sqrt(2))) + + cdf = _ir_ops.multiply( + half, + ( + _ir_ops.add( + one, + _ir_ops.erf( + _ir_ops.multiply(x, full(x.shape, M_SQRT1_2, x.dtype)) + ), + ) + ), + ) + out = _ir_ops.multiply(x, cdf) + return out diff --git a/test/prim/new_ir_prim/CMakeLists.txt b/test/prim/new_ir_prim/CMakeLists.txt index 85611d846cbbe..72fe311f270e6 100644 --- a/test/prim/new_ir_prim/CMakeLists.txt +++ b/test/prim/new_ir_prim/CMakeLists.txt @@ -1,4 +1,4 @@ -set(TEST_PRIM_PURE_NEW_IR_CASES test_prim_program) +set(TEST_PRIM_PURE_NEW_IR_CASES test_prim_program test_prim_simpnet) foreach(target ${TEST_PRIM_PURE_NEW_IR_CASES}) py_test_modules(${target} MODULES ${target} ENVS GLOG_v=1 diff --git a/test/prim/new_ir_prim/test_prim_simpnet.py b/test/prim/new_ir_prim/test_prim_simpnet.py new file mode 100644 index 0000000000000..505152354b986 --- /dev/null +++ b/test/prim/new_ir_prim/test_prim_simpnet.py @@ -0,0 +1,94 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle import _ir_ops, nn +from paddle.autograd.ir_backward import grad +from paddle.decomposition import decompose +from paddle.framework import core + +paddle.enable_static() + + +class SimpNet(nn.Layer): + def __init__(self): + super().__init__() + + def forward(self, x, linear1_weight, linear2_weight): + x2 = _ir_ops.matmul(x, linear1_weight, False, False) + x3 = _ir_ops.gelu(x2, False) + res = _ir_ops.matmul(x3, linear2_weight, False, False) + return res + + +class TestPrimMode(unittest.TestCase): + def setUp(self): + np.random.seed(2023) + self.shape_x = [2, 1024, 1024] + self.shape_y = [2, 1024, 1024] + self.shape_l1_w = [2, 1024, 4096] + self.shape_l2_w = [2, 4096, 1024] + self.x = np.random.random(self.shape_x).astype("float32") + self.y = np.random.random(self.shape_y).astype("float32") + self.l1_w = np.random.random(self.shape_l1_w).astype("float32") + self.l2_w = np.random.random(self.shape_l2_w).astype("float32") + + def base_net(self, flag=None): + if flag == "all": + core._set_prim_all_enabled(True) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + net = SimpNet() + x = paddle.static.data('x', self.shape_x, dtype='float32') + y = paddle.static.data('y', self.shape_y, dtype='float32') + x.stop_gradient = False + y.stop_gradient = False + l1_w = paddle.static.data('l1_w', self.shape_l1_w, dtype='float32') + l2_w = paddle.static.data('l2_w', self.shape_l2_w, dtype='float32') + divide_out = paddle.divide(x, y) + res = net(divide_out, l1_w, l2_w) + [res2] = decompose(main_program, [res]) + gradients = grad(res2, (x, y)) + exe = paddle.static.Executor() + outs = exe.run( + feed={ + 'x': self.x, + 'y': self.y, + 'l1_w': self.l1_w, + 'l2_w': self.l2_w, + }, + fetch_list=[res2, gradients[0], gradients[1]], + ) + + whole_ops = [op.name() for op in main_program.block().ops] + if flag == "all": + core._set_prim_all_enabled(False) + assert ( + 'pd.gelu' not in whole_ops and 'pd.divide_grad' not in whole_ops + ) + return outs + + def test_prim_all(self): + res_ref = self.base_net() + res = self.base_net("all") + for ref, actual in zip(res_ref, res): + np.testing.assert_allclose(ref, actual, rtol=1e-6) + + +if __name__ == "__main__": + unittest.main() From 52a0a6774178552afcb4c2b5f416ff9e384f2da6 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Tue, 5 Sep 2023 19:46:34 +0800 Subject: [PATCH 129/194] [Fluid] move lars_momentum_op InferShape to phi (#56749) * move to phi * fix * fix type --- .../operators/optimizers/lars_momentum_op.cc | 120 ++---------------- paddle/phi/infermeta/multiary.cc | 86 +++++++++++++ paddle/phi/infermeta/multiary.h | 16 +++ 3 files changed, 110 insertions(+), 112 deletions(-) diff --git a/paddle/fluid/operators/optimizers/lars_momentum_op.cc b/paddle/fluid/operators/optimizers/lars_momentum_op.cc index e6b04a8a3caaa..57abbab884e30 100644 --- a/paddle/fluid/operators/optimizers/lars_momentum_op.cc +++ b/paddle/fluid/operators/optimizers/lars_momentum_op.cc @@ -13,7 +13,9 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/framework/eigen.h" +#include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/infermeta/multiary.h" namespace paddle { namespace operators { @@ -22,117 +24,6 @@ class LarsMomentumOp : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; - protected: - void InferShape(framework::InferShapeContext* ctx) const override { - OP_INOUT_CHECK(ctx->HasInputs("Param"), "Input", "Param", "LarsMomentum"); - OP_INOUT_CHECK(ctx->HasInputs("Grad"), "Input", "Grad", "LarsMomentum"); - OP_INOUT_CHECK( - ctx->HasInputs("Velocity"), "Input", "Velocity", "LarsMomentum"); - OP_INOUT_CHECK(ctx->HasInputs("LearningRate"), - "Input", - "LearningRate", - "LarsMomentum"); - OP_INOUT_CHECK( - ctx->HasOutputs("ParamOut"), "Output", "ParamOut", "LarsMomentum"); - OP_INOUT_CHECK(ctx->HasOutputs("VelocityOut"), - "Output", - "VelocityOut", - "LarsMomentum"); - PADDLE_ENFORCE_EQ(ctx->GetInputsVarType("Param").front(), - framework::proto::VarType::LOD_TENSOR, - platform::errors::InvalidArgument( - "The input var's type should be phi::DenseTensor, " - "but the received is %s", - ctx->GetInputsVarType("Param").front())); - - auto lr_dims = ctx->GetInputsDim("LearningRate"); - auto grad_dim = ctx->GetInputsDim("Grad"); - auto param_dim = ctx->GetInputsDim("Param"); - auto velocity_dim = ctx->GetInputsDim("Velocity"); - auto lars_weight_decays = - ctx->Attrs().Get>("lars_weight_decay"); - auto multi_precision = ctx->Attrs().Get("multi_precision"); - - PADDLE_ENFORCE_EQ( - param_dim.size(), - grad_dim.size(), - platform::errors::InvalidArgument( - "Input(Param) and Input(Grad) of LarsMomentumOp should have " - "same quantity. But number of Param is [%d] and Grad is [%d].", - param_dim.size(), - grad_dim.size())); - PADDLE_ENFORCE_EQ( - param_dim.size(), - velocity_dim.size(), - platform::errors::InvalidArgument( - "Input(Param) and Input(Velocity) of LarsMomentumOp should " - "have same quantity. But number of Param is [%d] and Velocity " - "is [%d].", - param_dim.size(), - velocity_dim.size())); - PADDLE_ENFORCE_EQ( - lars_weight_decays.size(), - grad_dim.size(), - platform::errors::InvalidArgument( - "Attr(Lars_weight_decay) and " - "Input(Grad) of LarsMomentumOp should have same quantity. " - "But number of Lars_weight_decay is [%d] and Grad is [%d].", - lars_weight_decays.size(), - grad_dim.size())); - - if (multi_precision) { - OP_INOUT_CHECK(ctx->HasInputs("MasterParam"), - "Input", - "MasterParam", - "LarsMomentumMultiPrecision"); - OP_INOUT_CHECK(ctx->HasOutputs("MasterParamOut"), - "Output", - "MasterParamOut", - "LarsMomentumMultiPrecision"); - } - for (auto& lr_dim : lr_dims) { - PADDLE_ENFORCE_EQ(phi::product(lr_dim), - 1, - platform::errors::InvalidArgument( - "Learning_rate should be a scalar. But Received " - "LearningRate's dim [%s]", - phi::product(lr_dim))); - } - - for (size_t i = 0; i < param_dim.size(); ++i) { - PADDLE_ENFORCE_EQ(ctx->GetInputsVarType("Grad")[i], - framework::proto::VarType::LOD_TENSOR, - platform::errors::InvalidArgument( - "The Var(%s)'s type should be phi::DenseTensor, " - "but the received is %s", - ctx->Inputs("Grad")[i].front(), - ctx->GetInputsVarType("Grad")[i])); - PADDLE_ENFORCE_EQ( - param_dim[i], - grad_dim[i], - platform::errors::InvalidArgument( - "Input(Param) and Input(Grad) input of LarsMomentumOp shall " - "have same dimension. But Param`s dim is [%s] and Grad's dim " - "is [%s].", - param_dim[i], - grad_dim[i])); - PADDLE_ENFORCE_EQ( - param_dim[i], - velocity_dim[i], - platform::errors::InvalidArgument( - "Input(Param) and Input(Velocity) of LarsMomentumOp shall have " - "same dimension. But Param dim [%s] differs with Velocity dim " - "[%s].", - param_dim[i], - velocity_dim[i])); - } - ctx->SetOutputsDim("ParamOut", param_dim); - ctx->SetOutputsDim("VelocityOut", param_dim); - if (ctx->HasOutputs("MasterParamOut")) { - ctx->SetOutputsDim("MasterParamOut", param_dim); - } - } - protected: phi::KernelKey GetExpectedKernelType( const framework::ExecutionContext& ctx) const override { @@ -226,6 +117,10 @@ class LarsMomentumOpVarTypeInference : public framework::VarTypeInference { } // namespace operators } // namespace paddle +DECLARE_INFER_SHAPE_FUNCTOR(lars_momentum, + LarsMomentumInferShapeFunctor, + PD_INFER_META(phi::LarsMomentumInferMeta)); + namespace ops = paddle::operators; REGISTER_OPERATOR( lars_momentum, @@ -233,4 +128,5 @@ REGISTER_OPERATOR( ops::LarsMomentumOpMaker, paddle::framework::EmptyGradOpMaker, paddle::framework::EmptyGradOpMaker, - ops::LarsMomentumOpVarTypeInference); + ops::LarsMomentumOpVarTypeInference, + LarsMomentumInferShapeFunctor); diff --git a/paddle/phi/infermeta/multiary.cc b/paddle/phi/infermeta/multiary.cc index 958bfa76fe9c5..0701afa51af79 100644 --- a/paddle/phi/infermeta/multiary.cc +++ b/paddle/phi/infermeta/multiary.cc @@ -2577,6 +2577,92 @@ void LambInferMeta(const MetaTensor& param, } } +void LarsMomentumInferMeta( + const std::vector& param, + const std::vector& velocity, + const std::vector& learning_rate, + const std::vector& grad, + const paddle::optional>& master_param, + const std::vector& lars_weight_decay, + float mu, + float lars_coeff, + float epsilon, + bool multi_precision, + float rescale_grad, + std::vector param_out, + std::vector velocity_out, + std::vector master_param_out) { + std::vector lr_dims = GetMetaTensorsDim(learning_rate); + std::vector grad_dim = GetMetaTensorsDim(grad); + std::vector param_dim = GetMetaTensorsDim(param); + std::vector velocity_dim = GetMetaTensorsDim(velocity); + + PADDLE_ENFORCE_EQ( + param_dim.size(), + grad_dim.size(), + phi::errors::InvalidArgument( + "Input(Param) and Input(Grad) of LarsMomentumOp should have " + "same quantity. But number of Param is [%d] and Grad is [%d].", + param_dim.size(), + grad_dim.size())); + PADDLE_ENFORCE_EQ( + param_dim.size(), + velocity_dim.size(), + phi::errors::InvalidArgument( + "Input(Param) and Input(Velocity) of LarsMomentumOp should " + "have same quantity. But number of Param is [%d] and Velocity " + "is [%d].", + param_dim.size(), + velocity_dim.size())); + PADDLE_ENFORCE_EQ( + lars_weight_decay.size(), + grad_dim.size(), + phi::errors::InvalidArgument( + "Attr(Lars_weight_decay) and " + "Input(Grad) of LarsMomentumOp should have same quantity. " + "But number of Lars_weight_decay is [%d] and Grad is [%d].", + lars_weight_decay.size(), + grad_dim.size())); + + for (auto& lr_dim : lr_dims) { + PADDLE_ENFORCE_EQ(phi::product(lr_dim), + 1, + phi::errors::InvalidArgument( + "Learning_rate should be a scalar. But Received " + "LearningRate's dim [%s]", + phi::product(lr_dim))); + } + + for (size_t i = 0; i < param_dim.size(); ++i) { + PADDLE_ENFORCE_EQ( + param_dim[i], + grad_dim[i], + phi::errors::InvalidArgument( + "Input(Param) and Input(Grad) input of LarsMomentumOp shall " + "have same dimension. But Param`s dim is [%s] and Grad's dim " + "is [%s].", + param_dim[i], + grad_dim[i])); + PADDLE_ENFORCE_EQ( + param_dim[i], + velocity_dim[i], + phi::errors::InvalidArgument( + "Input(Param) and Input(Velocity) of LarsMomentumOp shall have " + "same dimension. But Param dim [%s] differs with Velocity dim " + "[%s].", + param_dim[i], + velocity_dim[i])); + } + + for (size_t i = 0; i < param_out.size(); i++) { + param_out[i]->set_dims(param_dim[i]); + velocity_out[i]->set_dims(param_dim[i]); + if (master_param != nullptr) { + master_param_out[i]->set_dims(param_dim[i]); + } + } +} + void LLMInt8LinearInferMeta(const MetaTensor& x, const MetaTensor& weight, const MetaTensor& bias, diff --git a/paddle/phi/infermeta/multiary.h b/paddle/phi/infermeta/multiary.h index 9beb9d213899d..ee62d6d51d655 100644 --- a/paddle/phi/infermeta/multiary.h +++ b/paddle/phi/infermeta/multiary.h @@ -459,6 +459,22 @@ void LambInferMeta(const MetaTensor& param, MetaTensor* beta2_pow_out, MetaTensor* master_param_outs); +void LarsMomentumInferMeta( + const std::vector& param, + const std::vector& velocity, + const std::vector& learning_rate, + const std::vector& grad, + const paddle::optional>& master_param, + const std::vector& lars_weight_decay, + float mu, + float lars_coeff, + float epsilon, + bool multi_precision, + float rescale_grad, + std::vector param_out, + std::vector velocity_out, + std::vector master_param_out); + void LLMInt8LinearInferMeta(const MetaTensor& x, const MetaTensor& weight, const MetaTensor& bias, From 04332fa44c072c9082dd26eaa55509accdd5f1f2 Mon Sep 17 00:00:00 2001 From: tianshuo78520a <707759223@qq.com> Date: Tue, 5 Sep 2023 20:41:38 +0800 Subject: [PATCH 130/194] test=document_fix (#56974) --- paddle/scripts/paddle_build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index 3dc17f2979994..f83028c0ffb4f 100644 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -3265,11 +3265,11 @@ function build_pr_and_develop() { git checkout $BRANCH dev_commit=`git log -1|head -1|awk '{print $2}'` - dev_url="https://xly-devops.bj.bcebos.com/PR/build_whl/0/${dev_commit}/paddlepaddle_gpu-0.0.0-cp37-cp37m-linux_x86_64.whl" + dev_url="https://xly-devops.bj.bcebos.com/PR/build_whl/0/${dev_commit}/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl" url_return=`curl -s -m 5 -IL ${dev_url} |awk 'NR==1{print $2}'` if [ "$url_return" == '200' ];then mkdir ${PADDLE_ROOT}/build/dev_whl && wget -q -P ${PADDLE_ROOT}/build/dev_whl ${dev_url} - cp ${PADDLE_ROOT}/build/dev_whl/paddlepaddle_gpu-0.0.0-cp37-cp37m-linux_x86_64.whl ${PADDLE_ROOT}/build/python/dist + cp ${PADDLE_ROOT}/build/dev_whl/paddlepaddle_gpu-0.0.0-cp310-cp310-linux_x86_64.whl ${PADDLE_ROOT}/build/python/dist else if [[ ${cmake_change} ]];then rm -rf ${PADDLE_ROOT}/build/Makefile ${PADDLE_ROOT}/build/CMakeCache.txt ${PADDLE_ROOT}/build/build.ninja From f5497fd0e964d51b4ecc6b911faf4f014c347714 Mon Sep 17 00:00:00 2001 From: lzydev Date: Tue, 5 Sep 2023 20:42:22 +0800 Subject: [PATCH 131/194] Add attributes to support to analyse the stream across interpreters (#56814) * fix static_build for pp * add mannual_event to support streams across progs * revert static_build.sh * fix coverage-ci * modify the method to name events * change code according to review --- .../distributed/auto_parallel/dist_attr.cc | 3 + .../distributed/auto_parallel/dist_attr.h | 28 +++ .../interpreter/interpreter_util.cc | 4 + .../interpreter/stream_analyzer.cc | 68 ++++++ .../interpreter/stream_analyzer.h | 8 + .../new_executor/new_executor_defs.h | 11 + .../new_executor/program_interpreter.cc | 1 + .../new_executor/program_interpreter.h | 14 ++ .../new_executor/standalone_executor.cc | 10 + .../new_executor/standalone_executor.h | 3 + paddle/fluid/pybind/auto_parallel_py.cc | 10 + .../paddle/distributed/passes/pass_utils.py | 17 ++ test/standalone_executor/CMakeLists.txt | 5 +- .../test_standalone_custom_event.py | 203 ++++++++++++++++++ 14 files changed, 383 insertions(+), 2 deletions(-) create mode 100644 test/standalone_executor/test_standalone_custom_event.py diff --git a/paddle/fluid/distributed/auto_parallel/dist_attr.cc b/paddle/fluid/distributed/auto_parallel/dist_attr.cc index 5ba9a700e3e29..b7bb47b3b859e 100644 --- a/paddle/fluid/distributed/auto_parallel/dist_attr.cc +++ b/paddle/fluid/distributed/auto_parallel/dist_attr.cc @@ -120,6 +120,9 @@ void OperatorDistAttr::copy_from(const OperatorDistAttr& dist_attr) { set_is_recompute(dist_attr.is_recompute()); set_execution_stream(dist_attr.execution_stream()); set_stream_priority(dist_attr.stream_priority()); + set_force_record_event(dist_attr.force_record_event()); + set_event_to_record(dist_attr.event_to_record()); + set_events_to_wait(dist_attr.events_to_wait()); set_scheduling_priority(dist_attr.scheduling_priority()); set_annotated(dist_attr.annotated()); } diff --git a/paddle/fluid/distributed/auto_parallel/dist_attr.h b/paddle/fluid/distributed/auto_parallel/dist_attr.h index 3d71c0fd75a4c..347c7fc05dfa0 100644 --- a/paddle/fluid/distributed/auto_parallel/dist_attr.h +++ b/paddle/fluid/distributed/auto_parallel/dist_attr.h @@ -141,6 +141,26 @@ class OperatorDistAttr { execution_stream_ = execution_stream; } + void set_event_to_record(const std::string& event_name) { + event_to_record_ = event_name; + } + + void set_force_record_event(bool force_record_event) { + force_record_event_ = force_record_event; + } + + void set_events_to_wait(const std::vector& events_to_wait) { + events_to_wait_ = events_to_wait; + } + + bool force_record_event() const { return force_record_event_; } + + const std::string& event_to_record() const { return event_to_record_; } + + const std::vector& events_to_wait() const { + return events_to_wait_; + } + int stream_priority() const { return stream_priority_; } void set_stream_priority(int stream_priority) { @@ -204,6 +224,11 @@ class OperatorDistAttr { void parse_from_string(const std::string& data); + static std::string unique_name(std::string key) { + static std::atomic id_{0}; + return key + "_" + std::to_string(id_++); + } + private: static std::vector fields_; std::map input_dist_attrs_; @@ -214,6 +239,9 @@ class OperatorDistAttr { int64_t impl_idx_ = 0; bool is_recompute_ = false; std::string execution_stream_ = kDefault; + bool force_record_event_ = false; + std::vector events_to_wait_; + std::string event_to_record_ = unique_name("event"); // event_idx int stream_priority_ = 0; // lower value, higher priority int64_t scheduling_priority_ = 0; // lower value, higher priority std::map annotated_; diff --git a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc index bbb3a66bac233..be87549ecbfbc 100644 --- a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc +++ b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc @@ -656,6 +656,10 @@ void BuildOpFuncList(const platform::Place& place, } op_func_node.stream_priority_ = dist_attr->stream_priority(); op_func_node.scheduling_priority_ = dist_attr->scheduling_priority(); + // set mannual event information + op_func_node.force_record_event_ = dist_attr->force_record_event(); + op_func_node.events_to_wait_ = dist_attr->events_to_wait(); + op_func_node.event_to_record_ = dist_attr->event_to_record(); } else { if (interpreter::IsCommunicationOp(op)) { // NOTE(Ruibiao): Dispatching computation before communication improves diff --git a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc index 3fc9dde363039..27ac1681a4008 100644 --- a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc +++ b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc @@ -97,6 +97,15 @@ void StreamAnalyzer::ConstructEvents(std::vector* instructions) { platform::GenerateDeviceEventFlag()); recorder_instr.AddEventToRecord(device_event, platform::kCUDA /*unused*/); + // It means the event will be waited for other interpreter that the + // event name of a operator is not 'default'. + if (recorder_instr.OpFunc()->force_record_event_ == true && + (*program_force_events_to_wait_) + .count(recorder_instr.OpFunc()->event_to_record_) == 0) { + (*program_force_events_to_wait_)[recorder_instr.OpFunc() + ->event_to_record_] = + recorder_instr.EventToRecord(); + } instr2event.emplace(recorder_instr_id, device_event); } @@ -108,6 +117,65 @@ void StreamAnalyzer::ConstructEvents(std::vector* instructions) { } } } + // NOTE(lizhiyu): The mannual event only support the program_interpreter to + // annalyze the streams across the sub_programs. construct mannual events to + // record + for (auto& instruction : *instructions) { + // create extra event to record + auto op_func_node = instruction.OpFunc(); + if (op_func_node->force_record_event_ && + instruction.EventToRecord() == nullptr) { + auto place = instruction.DeviceContext().GetPlace(); + if (platform::is_gpu_place(place)) { + PADDLE_ENFORCE_NE( + op_func_node->event_to_record_, + "default", + phi::errors::InvalidArgument( + "If the attribute 'force_record_event_' of one " + "operator is 'true', the 'event_to_record_' of this " + "operator can not be 'default'. But the " + "'event_name' of the operator %s is 'default'.", + instruction.OpBase()->Type().c_str())); + PADDLE_ENFORCE_EQ( + (*program_force_events_to_wait_) + .find(op_func_node->event_to_record_), + (*program_force_events_to_wait_).end(), + phi::errors::InvalidArgument( + "The program_force_events_to_wait_ had the event " + "that belongs to the operator : %s before the operator create " + "the event, " + "This is is werid.", + instruction.OpBase()->Type().c_str())); + std::shared_ptr device_event = + std::make_shared(place, + platform::GenerateDeviceEventFlag()); + instruction.AddEventToRecord(device_event, platform::kCUDA /*unused*/); + (*program_force_events_to_wait_)[op_func_node->event_to_record_] = + instruction.EventToRecord(); + VLOG(6) << "Create mannual event: " << op_func_node->event_to_record_ + << " for the operator: " << instruction.OpBase()->Type(); + } + } + // add extra mannual events + if (!(op_func_node->events_to_wait_.empty())) { + for (auto event_name : op_func_node->events_to_wait_) { + PADDLE_ENFORCE_NE( + (*program_force_events_to_wait_).find(event_name), + (*program_force_events_to_wait_).end(), + phi::errors::InvalidArgument( + "The program_force_events_to_wait_ don't have the event %s " + "for the operator: %s to wait. The event should had been " + "created by the operator " + "whose event_to_record_ is %s.", + event_name.c_str(), + instruction.OpBase()->Type().c_str(), + event_name.c_str())); + + instruction.AddEventToWait( + (*program_force_events_to_wait_)[event_name].get()); + } + } + } } DeviceContext* StreamAnalyzer::ParseDeviceContext( diff --git a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.h b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.h index f8ba8103620a2..8f2ee33ca4ed5 100644 --- a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.h +++ b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.h @@ -86,6 +86,12 @@ class StreamAnalyzer { void ShareEventInfoFrom(const StreamAnalyzer& src); + void SetForceEventsToWaitInfo( + std::unordered_map>* + program_force_events_to_wait) { + program_force_events_to_wait_ = program_force_events_to_wait; + } + std::shared_ptr< std::map>>> GetEventInfo() const; @@ -114,6 +120,8 @@ class StreamAnalyzer { std::shared_ptr< std::map>>> event_info_; + std::unordered_map>* + program_force_events_to_wait_; // not owned }; /// ======================== /// diff --git a/paddle/fluid/framework/new_executor/new_executor_defs.h b/paddle/fluid/framework/new_executor/new_executor_defs.h index edce1554b0a90..bf0c0880f385d 100644 --- a/paddle/fluid/framework/new_executor/new_executor_defs.h +++ b/paddle/fluid/framework/new_executor/new_executor_defs.h @@ -167,6 +167,9 @@ struct OpFuncNode { // TODO(zhiqiu): Better make it unique_ptr std::shared_ptr operator_base_{nullptr}; std::string execution_stream_{kDefaultStream}; + bool force_record_event_{false}; + std::vector events_to_wait_; + std::string event_to_record_{"default"}; OpFuncType type_; OpKernelComputeFunc kernel_func_; @@ -212,10 +215,18 @@ class Instruction { events_to_wait_.emplace_back(instr_id, event, waiter_type); } + void AddEventToWait(const EventInter* event_inter) { + events_to_wait_.push_back(*event_inter); + } + const std::vector& EventsToWait() const { return events_to_wait_; } + const std::shared_ptr& EventToRecord() const { + return event_to_record_; + } + void AddNextInstrInDifferentThread(size_t id) { next_instrs_in_different_thread.push_back(id); } diff --git a/paddle/fluid/framework/new_executor/program_interpreter.cc b/paddle/fluid/framework/new_executor/program_interpreter.cc index 43e5301476d9b..29998e61f0111 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.cc +++ b/paddle/fluid/framework/new_executor/program_interpreter.cc @@ -615,6 +615,7 @@ void ProgramInterpreter::Convert( vec_instruction_.reserve(op_nums); for (size_t op_idx = 0; op_idx < op_nums; ++op_idx) { auto& op_func_node = nodes[op_idx]; + stream_analyzer_.SetForceEventsToWaitInfo(force_evnets_to_wait_); auto* dev_ctx_ = stream_analyzer_.ParseDeviceContext(op_func_node); #ifdef PADDLE_WITH_CUDA if (FLAGS_new_executor_use_cuda_graph) { diff --git a/paddle/fluid/framework/new_executor/program_interpreter.h b/paddle/fluid/framework/new_executor/program_interpreter.h index 29ec71059c778..27348d57fcd17 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.h +++ b/paddle/fluid/framework/new_executor/program_interpreter.h @@ -81,6 +81,17 @@ class ProgramInterpreter : public InterpreterBaseImpl { hookfuncs_ = hookfuncs; } + std::unordered_map>* + GetForceEventsToWaitInfo() { + return force_evnets_to_wait_; + } + + void SetForceEventsToWaitInfo( + std::unordered_map>* + force_evnets_to_wait) { + force_evnets_to_wait_ = force_evnets_to_wait; + } + bool IsStaticBuild() const { return static_build_; } private: @@ -162,6 +173,9 @@ class ProgramInterpreter : public InterpreterBaseImpl { ExecutionConfig execution_config_; + std::unordered_map>* + force_evnets_to_wait_; + VariableScope var_scope_; Scope* local_scope_{nullptr}; // not owned diff --git a/paddle/fluid/framework/new_executor/standalone_executor.cc b/paddle/fluid/framework/new_executor/standalone_executor.cc index 7d65fe33818a1..303a4f8478127 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor.cc @@ -41,6 +41,7 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, Scope* scope) : place_(place), plan_(plan), scope_(scope) { int64_t micro_batch_num = plan_.MicroBatchNum(); + vec_force_events_to_wait_.resize(micro_batch_num); for (int64_t i = 0; i < micro_batch_num; ++i) { micro_batch_scopes_.emplace_back(&scope->NewScope()); } @@ -129,6 +130,14 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, micro_batch_scopes_[micro_batch_id], execution_config)); interpretercores_.back()->SetCopyProgram(program); + + // Note(lizhiyu): Add mannual event info + auto prog_inter = const_cast( + static_cast( + interpretercores_.back()->Impl())); + prog_inter->SetForceEventsToWaitInfo( + &(vec_force_events_to_wait_[micro_batch_id])); + // NOTE(lizhiyu): Now we only check backward subprogram. After static // build strategy is completely, we should // check all the program in the PP strategy. @@ -181,6 +190,7 @@ paddle::framework::FetchList StandaloneExecutor::Run( VLOG(6) << "Run job (" << job_idx << "), type = " << job_type << ", micro_batch_id =" << job->MicroBatchId(); + // Note(sonder): Share build results don't work for new IR now. if (type_to_first_id.count(job_type) != 0 && !FLAGS_enable_new_ir_in_executor) { diff --git a/paddle/fluid/framework/new_executor/standalone_executor.h b/paddle/fluid/framework/new_executor/standalone_executor.h index 1da628fe27bb7..bec52add981bf 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.h +++ b/paddle/fluid/framework/new_executor/standalone_executor.h @@ -52,6 +52,9 @@ class StandaloneExecutor { Scope* scope_; std::vector fetch_var_names_; + + std::vector>> + vec_force_events_to_wait_; }; } // namespace framework diff --git a/paddle/fluid/pybind/auto_parallel_py.cc b/paddle/fluid/pybind/auto_parallel_py.cc index bdd467fbefa8b..0619ad319af9a 100644 --- a/paddle/fluid/pybind/auto_parallel_py.cc +++ b/paddle/fluid/pybind/auto_parallel_py.cc @@ -441,6 +441,16 @@ void BindAutoParallel(py::module *m) { .def_property("scheduling_priority", &OperatorDistAttr::scheduling_priority, &OperatorDistAttr::set_scheduling_priority) + .def_property("force_record_event", + &OperatorDistAttr::force_record_event, + &OperatorDistAttr::set_force_record_event) + .def_property("events_to_wait", + &OperatorDistAttr::events_to_wait, + &OperatorDistAttr::set_events_to_wait, + pybind11::return_value_policy::reference) + .def_property("event_to_record", + &OperatorDistAttr::event_to_record, + &OperatorDistAttr::set_event_to_record) .def_property("annotated", &OperatorDistAttr::annotated, &OperatorDistAttr::set_annotated) diff --git a/python/paddle/distributed/passes/pass_utils.py b/python/paddle/distributed/passes/pass_utils.py index ba502a043ab35..89c9c65ba1761 100644 --- a/python/paddle/distributed/passes/pass_utils.py +++ b/python/paddle/distributed/passes/pass_utils.py @@ -525,3 +525,20 @@ def _add_ops_into_block(src_block, dst_block, ops): # It MUST return in this order return [lr_prog, fwd_prog, bwd_prog, opt_prog] + + +def _add_event_dependency(recorder_op_desc, waiter_op_desc): + ''' + Add the extra event dependcy of the two operators. + This function mainly aims for the cross-programs in pipeline parallelism, + especial for the 'send_v2' 'recv_v2' etc. + ''' + if not recorder_op_desc.dist_attr.force_record_event: + recorder_op_desc.dist_attr.force_record_event = True + # NOTE(lizhiyu): Here is the copy of 'waiter_op_desc.dist_attr.events_to_wait' not the reference, + # because the type of 'events_to_wait' is 'const vector&' while the type of + # 'waiter_wait_list' is python list. + waiter_wait_list = waiter_op_desc.dist_attr.events_to_wait + if recorder_op_desc.dist_attr.event_to_record not in waiter_wait_list: + waiter_wait_list.append(recorder_op_desc.dist_attr.event_to_record) + waiter_op_desc.dist_attr.events_to_wait = waiter_wait_list diff --git a/test/standalone_executor/CMakeLists.txt b/test/standalone_executor/CMakeLists.txt index a2b55191d53fe..1e351d176bb15 100644 --- a/test/standalone_executor/CMakeLists.txt +++ b/test/standalone_executor/CMakeLists.txt @@ -2,6 +2,7 @@ file( GLOB TEST_INTERP_CASES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_*.py") +list(REMOVE_ITEM TEST_INTERP_CASES "test_standalone_custom_event.py") string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") foreach(target ${TEST_INTERP_CASES}) @@ -31,8 +32,8 @@ py_test_modules( # These UTs are to temporarily test static build for standalone_executor, will be removed after static build is enabled by default. set(STATIC_BUILD_TESTS test_standalone_controlflow test_standalone_cuda_graph_multi_stream - test_standalone_custom_stream test_standalone_executor - test_standalone_multiply_write) + test_standalone_custom_stream test_standalone_custom_event + test_standalone_executor test_standalone_multiply_write) foreach(STATIC_BUILD_TEST ${STATIC_BUILD_TESTS}) py_test_modules( diff --git a/test/standalone_executor/test_standalone_custom_event.py b/test/standalone_executor/test_standalone_custom_event.py new file mode 100644 index 0000000000000..3b9fe7a3197b6 --- /dev/null +++ b/test/standalone_executor/test_standalone_custom_event.py @@ -0,0 +1,203 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import paddle +from paddle.distributed.passes.pass_utils import ( + _add_event_dependency, + get_skip_gc_vars, + split_program, +) +from paddle.fluid import core +from paddle.fluid.executor import _add_feed_fetch_ops, _StandaloneExecutor + +paddle.enable_static() + + +def build_program(): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + + with paddle.static.program_guard(main_program, startup_program): + # data -> [matmul] -> out ->[add] -> add_out + with paddle.static.device_guard('gpu'): + data = paddle.ones([1024, 2048], dtype='float32', name='data') + weight = paddle.randn([2048, 2048], name='weight') # gpu + matmul_out = paddle.matmul(data, weight, name='matmul_out') # gpus + bias = paddle.ones([1024, 2048], dtype='float32', name='bias') + add_out = paddle.add(matmul_out, bias, name='add_out') + # add_out -> [sub] -> sub_out -> [tanh] -> tanh_out + sub_out = paddle.subtract(add_out, data, name='sub_out') + tanh_out = paddle.tanh(sub_out, name='tanh_out') + bias_1 = paddle.add(bias, sub_out, name='bias_1') + out_before = paddle.tanh(bias_1, name='out_before') + out_last = paddle.subtract(tanh_out, data, name='out_last') + out_last2 = paddle.matmul(out_last, weight, name="matmul_2_out") + + out = paddle.add(out_before, out_last2, name='out') + mean = paddle.mean(out, name='mean_out') + + return main_program, startup_program, [mean] + + +class TestMannulEvent(unittest.TestCase): + """ + fill_constant(def) gaussian_random(def) + | | | | + | | matmul_v2(s1) fill_constant(def) + | | | | | | + | | elementwise_add(s1) | + | | | | + | elementwise_sub(s1) | + | | | | + | tanh(s1) elementwise_add(s1) + | | | + elementwise_sub(s1) tanh(s1) + | | + matmul_v2(s1) | + | | ---split prog---- + elementwise_add(s2) + | + reduce_mean(s2) + """ + + def setUp(self): + self.steps = 3 + self.place_desc = ( + paddle.CUDAPlace(0) + if core.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + self.place = core.Place() + self.place.set_place(self.place_desc) + + def set_custom_stream(self, prog): + op_index_for_stream1 = [2, 4, 5, 6, 7, 8, 9, 10] + op_index_for_stream2 = [11, 12] + ops = prog.global_block().ops + for op_index in op_index_for_stream1: + ops[op_index].dist_attr.execution_stream = "s1" + ops[op_index].dist_attr.stream_priority = 0 + for op_index in op_index_for_stream2: + ops[op_index].dist_attr.execution_stream = "s2" + ops[op_index].dist_attr.stream_priority = -1 + + def split_program(self, prog, apply_mannual_event=False): + # split two subprograms + waiter_recorder_events_map = {11: [8, 10]} + prog_block = prog.global_block() + ops = prog_block.ops + if apply_mannual_event: + for waiter, recorders in waiter_recorder_events_map.items(): + for recorder in recorders: + _add_event_dependency(ops[recorder].desc, ops[waiter].desc) + main_progs, _, _ = split_program(prog, [11]) + return main_progs + + def create_standalone_exe(self, main_progs, startup_progs, fetch_list): + micro_batch_num = 1 + micro_batch_id = 0 + job_list = [] + prog_num = len(main_progs) + fetch_op_num = len(fetch_list) + skip_gc_vars = get_skip_gc_vars(main_progs) + + if prog_num == 1: # single prog + main_progs[0] = _add_feed_fetch_ops( + main_progs[0], + [], + fetch_list, + "feed", + "fetch", + use_fetch_v2=True, + ) + op_num = len(main_progs[0].block(0).ops) + fetch_op_indics = list(range(op_num - fetch_op_num, op_num)) + else: + main_progs[-1] = _add_feed_fetch_ops( + main_progs[-1], + [], + fetch_list, + "feed", + "fetch", + use_fetch_v2=True, + ) + op_num = len(main_progs[-1].block(0).ops) + fetch_op_indics = list(range(op_num - fetch_op_num, op_num)) + + # create jobs + for program_id in range(prog_num): + job = core.Job(f"prog_{program_id}") + job.set_skip_gc_vars(skip_gc_vars[program_id]) + # Set col_attr info for fetch_op to fetch the correct data after running multiple micro batch + if program_id == prog_num - 1: + for i in range(fetch_op_num): + job.set_col_attr_for_fetch_op( + fetch_op_indics[i], + i * micro_batch_num + micro_batch_id, + ) + job_list.append(job) + + type_to_program = {} + for program_id in range(prog_num): + type_to_program[f"prog_{program_id}"] = main_progs[program_id].desc + + plan = core.Plan(job_list, type_to_program) + scope = core.Scope() + main_exe = _StandaloneExecutor(self.place, plan, scope) + return main_exe + + def run_program( + self, + apply_custom_stream=False, + split_prog=False, + apply_mannual_event=False, + ): + paddle.seed(2022) + main_program, startup_program, fetch_list = build_program() + self.assertEqual(len(startup_program.global_block().ops), 0) + + if apply_custom_stream: + self.set_custom_stream(main_program) + main_progs = [main_program] + startup_progs = [startup_program] + if apply_custom_stream and split_prog: + main_progs = self.split_program(main_program, apply_mannual_event) + outs = [] + exe = self.create_standalone_exe(main_progs, startup_progs, fetch_list) + for i in range(self.steps): + outs.append(exe.run(feed_names=[])) + return outs + + def test_result(self): + if not core.is_compiled_with_cuda(): + return + + baselines = self.run_program() + stream_outs = self.run_program(apply_custom_stream=True) + split_outs = self.run_program(apply_custom_stream=True, split_prog=True) + mannual_outs = self.run_program( + apply_custom_stream=True, split_prog=True, apply_mannual_event=True + ) + for bl, out0, out1, out2 in zip( + baselines, stream_outs, split_outs, mannual_outs + ): + self.assertEqual(bl[0], out0[0]) + self.assertEqual(bl[0], out2[0]) + # self.assertNotEqual(bl[0], out1[0]) + + +if __name__ == "__main__": + unittest.main() From e2b05dcc79bb3c763da44cd914544bd435fcb570 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Wed, 6 Sep 2023 09:45:24 +0800 Subject: [PATCH 132/194] refine bilinear interp grad register (#56976) --- paddle/phi/kernels/gpu/interpolate_grad_kernel.cu | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu b/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu index ebc12bc770659..da633b73bf6e4 100644 --- a/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/interpolate_grad_kernel.cu @@ -1572,6 +1572,7 @@ PD_REGISTER_KERNEL(bilinear_interp_grad, double, phi::dtype::float16, phi::dtype::bfloat16) { + kernel->InputAt(1).SetBackend(phi::Backend::CPU); kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND); kernel->InputAt(3).SetBackend(phi::Backend::ALL_BACKEND); } @@ -1583,6 +1584,7 @@ PD_REGISTER_KERNEL(nearest_interp_grad, double, phi::dtype::float16, phi::dtype::bfloat16) { + kernel->InputAt(1).SetBackend(phi::Backend::CPU); kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND); kernel->InputAt(3).SetBackend(phi::Backend::ALL_BACKEND); } @@ -1594,6 +1596,7 @@ PD_REGISTER_KERNEL(trilinear_interp_grad, double, phi::dtype::float16, phi::dtype::bfloat16) { + kernel->InputAt(1).SetBackend(phi::Backend::CPU); kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND); kernel->InputAt(3).SetBackend(phi::Backend::ALL_BACKEND); } @@ -1605,6 +1608,7 @@ PD_REGISTER_KERNEL(linear_interp_grad, double, phi::dtype::float16, phi::dtype::bfloat16) { + kernel->InputAt(1).SetBackend(phi::Backend::CPU); kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND); kernel->InputAt(3).SetBackend(phi::Backend::ALL_BACKEND); } @@ -1616,6 +1620,7 @@ PD_REGISTER_KERNEL(bicubic_interp_grad, double, phi::dtype::float16, phi::dtype::bfloat16) { + kernel->InputAt(1).SetBackend(phi::Backend::CPU); kernel->InputAt(2).SetBackend(phi::Backend::ALL_BACKEND); kernel->InputAt(3).SetBackend(phi::Backend::ALL_BACKEND); } From d121cf2927437670bc303935ea6f2480345bfa41 Mon Sep 17 00:00:00 2001 From: liuzhenhai93 Date: Wed, 6 Sep 2023 09:52:16 +0800 Subject: [PATCH 133/194] add sep group (#56271) * sep group * add test * test ok * polish * test cmake script generated * add sep group * format * polish * polish --- .../framework/distributed_strategy.proto | 7 +- .../fleet/base/distributed_strategy.py | 13 +- .../paddle/distributed/fleet/base/topology.py | 115 +++++++- python/paddle/distributed/fleet/fleet.py | 8 +- test/collective/fleet/CMakeLists.txt | 29 +- .../fleet/hybrid_parallel_sep_model.py | 51 ++++ .../test_parallel_dygraph_sep_parallel.py | 26 ++ test/collective/fleet/testslist.csv | 100 +++---- .../test_hybrid_parallel_topology.py | 271 ++++++++++++++++++ 9 files changed, 547 insertions(+), 73 deletions(-) create mode 100644 test/collective/fleet/hybrid_parallel_sep_model.py create mode 100644 test/collective/fleet/test_parallel_dygraph_sep_parallel.py diff --git a/paddle/fluid/framework/distributed_strategy.proto b/paddle/fluid/framework/distributed_strategy.proto index 38b7acd93c445..64a2efcfdccda 100755 --- a/paddle/fluid/framework/distributed_strategy.proto +++ b/paddle/fluid/framework/distributed_strategy.proto @@ -83,9 +83,10 @@ message HybridConfig { optional int32 mp_degree = 2 [ default = 1 ]; optional int32 pp_degree = 3 [ default = 1 ]; optional int32 sharding_degree = 4 [ default = 1 ]; - optional MpConfig mp_configs = 5; - optional PpConfig pp_configs = 6; - optional DygraphShardingConfig sharding_configs = 7; + optional int32 sep_degree = 5 [ default = 1 ]; + optional MpConfig mp_configs = 6; + optional PpConfig pp_configs = 7; + optional DygraphShardingConfig sharding_configs = 8; } message AMPConfig { diff --git a/python/paddle/distributed/fleet/base/distributed_strategy.py b/python/paddle/distributed/fleet/base/distributed_strategy.py index 23d9327623fbc..5cb9480eca8b6 100755 --- a/python/paddle/distributed/fleet/base/distributed_strategy.py +++ b/python/paddle/distributed/fleet/base/distributed_strategy.py @@ -153,7 +153,7 @@ def __init__(self): if _global_flags().is_public(key): self.strategy.sync_nccl_allreduce = bool(_global_flags()[key]) - self.hybrid_parallel_order = ['dp', 'pp', 'sharding', 'mp'] + self.hybrid_parallel_order = ['dp', 'pp', 'sharding', 'sep', 'mp'] self.sync_param_name = ["embedding", "layer_norm", ".b_"] self.__lock_attr = True @@ -1718,10 +1718,10 @@ def tensor_parallel_configs(self, configs): def hybrid_configs(self): """ - Dynamic graph hybrid parallel strategy configuration. Three-way hybrid parallelism + Dynamic graph hybrid parallel strategy configuration. Five-way hybrid parallelism needs to meet the following relationships - total_number_GPUs = dp_degree * mp_degree * pp_degree + total_number_GPUs = dp_degree * mp_degree * pp_degree * sharding_degree * sep_degree **Note**: **dp_degree(int)**: set number of GPUs in a data parallel group. Default -1. @@ -1732,8 +1732,9 @@ def hybrid_configs(self): **mp_degree(int)**: set number of GPUs in a model parallel group. Default 1 **pp_degree(int)**: set number of GPUs in a pipeline parallel group. Default 1 - - **order(list(string))**: set hybrid parallel dimensions, the order is from outside to inside. Default ['dp','pp','sharding','mp'] + **sep_degree(int)**: set number of GPUs in a sep parallel group. Default 1 + **sharding_degree(int)**: set number of GPUs in a sharding parallel group. Default 1 + **order(list(string))**: set hybrid parallel dimensions, the order is from outside to inside. Default ['dp','pp','sharding','sep', 'mp'] Examples: .. code-block:: python @@ -1744,7 +1745,7 @@ def hybrid_configs(self): "dp_degree": 1, "mp_degree": 2, "pp_degree": 1, - "order":['dp','pp','sharding','mp']} + "order":['dp','pp','sharding', 'sep', 'mp']} """ return get_msg_dict(self.strategy.hybrid_configs) diff --git a/python/paddle/distributed/fleet/base/topology.py b/python/paddle/distributed/fleet/base/topology.py index 4c655a0295c4f..bec592e6bb534 100644 --- a/python/paddle/distributed/fleet/base/topology.py +++ b/python/paddle/distributed/fleet/base/topology.py @@ -60,8 +60,8 @@ class ParallelMode: class CommunicateTopology: def __init__( self, - hybrid_group_names=["data", "pipe", "sharding", "model"], - dims=[1, 1, 1, 1], + hybrid_group_names=["data", "pipe", "sharding", "sep", "model"], + dims=[1, 1, 1, 1, 1], ): self._parallel_names = hybrid_group_names self._dims = dims @@ -112,6 +112,33 @@ def get_dim_size(self, axis_name): assert axis_name in self._parallel_names return self._dims[self._parallel_names.index(axis_name)] + def get_fused_ranks(self, fused_axis): + non_fused_axis = list(set(self._parallel_names).difference(fused_axis)) + non_fused_ranges = [] + for axis_name in non_fused_axis: + non_fused_ranges.append( + range(self._dims[self._parallel_names.index(axis_name)]) + ) + fused_ranges = [] + for axis_name in fused_axis: + fused_ranges.append( + range(self._dims[self._parallel_names.index(axis_name)]) + ) + + rank_list = [] + for non_fused_ranks in product(*non_fused_ranges): + coord_dict = {} + ranks = [] + for i, non_fused_rank in enumerate(non_fused_ranks): + coord_dict[non_fused_axis[i]] = non_fused_rank + for fused_ranks in product(*fused_ranges): + for i, fused_rank in enumerate(fused_ranks): + coord_dict[fused_axis[i]] = fused_rank + ranks.append(self._coord2rank[self.coordinate(**coord_dict)]) + rank_list.append(ranks) + + return rank_list + def get_comm_list(self, axis_name): assert axis_name in self._parallel_names other_axis_names = [ @@ -153,20 +180,23 @@ def __init__(self, topology): self._mp_degree = self._topo.get_dim('model') self._pp_degree = self._topo.get_dim('pipe') self._sharding_degree = self._topo.get_dim('sharding') + self._sep_degree = self._topo.get_dim('sep') self._data_parallel_id = self._get_data_parallel_id() self._model_parallel_id = self._get_model_parallel_id() self._sharding_parallel_id = self._get_sharding_parallel_id() + self._sep_parallel_id = self._get_sep_parallel_id() self.stage_id = self._get_pipe_parallel_id() assert self._check_vaild_topo(), ( "Here is an unreasonable topogy setting. world_size: {}, but" - "mp_num: {}, sharding_num: {}, pp_num: {}, dp_num: {}".format( + "mp_num: {}, sharding_num: {}, pp_num: {}, dp_num: {}, sep_num: {}".format( self.nranks, self._mp_degree, self._sharding_degree, self._pp_degree, self._dp_degree, + self._sep_degree, ) ) @@ -183,6 +213,10 @@ def __init__(self, topology): self._sharding_group, self._sharding_comm_group = self._set_comm_group( "sharding" ) + self._sep_group = None + if self._sep_degree > 1: + # create comm group for sep parallel + self._sep_group, self._sep_comm_group = self._set_comm_group("sep") # create global group for check inf_nan / clip global norm self._check_group, self._check_comm_group = self._set_check_group( @@ -195,6 +229,16 @@ def __init__(self, topology): self.sharding_check_comm_group, ) = self._set_check_group("sharding") + # create fused comm group + if self._sep_degree > 1: + ( + self._dp_sep_group, + self._dp_sep_comm_group, + ) = self.create_fuse_group(["data", "sep"]) + self._pp_mp_group, self._pp_mp_comm_group = self.create_fuse_group( + ["pipe", "model"] + ) + # create p2p group self.is_first_stage = self.stage_id == 0 self.is_last_stage = self.stage_id == (self._pp_degree - 1) @@ -209,20 +253,22 @@ def __init__(self, topology): debug_str = ( "HybridParallelInfo: rank_id: %d, mp_degree: %d, " - "sharding_degree: %d, pp_degree: %d, dp_degree: %d" + "sharding_degree: %d, pp_degree: %d, dp_degree: %d, sep_degree: %d" % ( self.global_rank, self._mp_degree, self._sharding_degree, self._pp_degree, self._dp_degree, + self._sep_degree, ) ) - debug_str += ", mp_group: {}, sharding_group: {}, pp_group: {}, dp_group: {}, check/clip group: {}".format( + debug_str += ", mp_group: {}, sharding_group: {}, pp_group: {}, dp_group: {}, sep:group: {}, check/clip group: {}".format( self._mp_group, self._sharding_group, self._pp_group, self._dp_group, + self._sep_group, self._check_group, ) logger.info(debug_str) @@ -257,9 +303,13 @@ def _check_vaild_topo(self): * self._mp_degree * self._pp_degree * self._sharding_degree + * self._sep_degree == self.nranks ) + def _check_sep_exist(self): + assert self._sep_degree > 1, "sep not exist" + def _set_comm_group(self, parallel_method="data"): parallel_group = [] parallel_comm_group = None @@ -404,6 +454,23 @@ def get_stage_id(self): def get_pipe_parallel_world_size(self): return self._pp_degree + def _get_sep_parallel_id(self): + return self._topo.get_coord(self.global_rank).sep + + def get_sep_parallel_rank(self): + return self._sep_parallel_id + + def get_sep_parallel_world_size(self): + return self._sep_degree + + def get_sep_parallel_group(self): + self._check_sep_exist() + return self._sep_comm_group + + def get_sep_parallel_group_src_rank(self): + self._check_sep_exist() + return self._sep_comm_group.ranks[0] + def get_pipe_parallel_group(self): return self._pp_comm_group @@ -447,6 +514,44 @@ def get_rank_from_stage(self, stage_id, **kwargs): self.global_rank, pipe=stage_id, **kwargs ) + # fuse comm group message + def get_dp_sep_parallel_group(self): + self._check_sep_exist() + return self._dp_sep_comm_group + + def get_pp_mp_parallel_group(self): + self._check_sep_exist() + return self._pp_mp_comm_group + + def create_fuse_group(self, fused_strategy_list): + assert ( + len(fused_strategy_list) > 0 + ), "the length of fused_strategy_list must be greater than 0." + + parallel_group = [] + parallel_comm_group = [] + parallel_groups = self._topo.get_fused_ranks(fused_strategy_list) + parallel_groups.sort() + + for group in parallel_groups: + comm_group = paddle.distributed.new_group(ranks=group) + if self.global_rank in group: + parallel_group.append(group) + parallel_comm_group.append(comm_group) + + assert len(parallel_group) > 0 + assert len(parallel_comm_group) > 0 + + logger.info( + "Total {} comm group(s) of fused {} create successfully!".format( + len(parallel_groups), fused_strategy_list + ) + ) + if len(parallel_group) > 1: + return parallel_group, parallel_comm_group + else: + return parallel_group[0], parallel_comm_group[0] + class _CommunicateGroup: """tmp for static""" diff --git a/python/paddle/distributed/fleet/fleet.py b/python/paddle/distributed/fleet/fleet.py index 2dab355264b4d..df0c39ee119ab 100755 --- a/python/paddle/distributed/fleet/fleet.py +++ b/python/paddle/distributed/fleet/fleet.py @@ -370,21 +370,26 @@ def init( return self def _init_hybrid_parallel_env(self): - """initialize the hybrid environment""" + """initialize the hybrid environment.""" self.hybrid_configs = self._user_defined_strategy.hybrid_configs self.dp_degree = self.hybrid_configs["dp_degree"] self.mp_degree = self.hybrid_configs["mp_degree"] self.pp_degree = self.hybrid_configs["pp_degree"] + self.sep_degree = self.hybrid_configs["sep_degree"] self.sharding_degree = self.hybrid_configs["sharding_degree"] assert self.mp_degree >= 0, "mp_degree should be greater or equal to 0" assert self.pp_degree >= 0, "pp_degree should be greater or equal to 0" + assert ( + self.sep_degree >= 0 + ), "sep_degree should be greater or equal to 0" assert ( self.sharding_degree >= 0 ), "sharding_degree should be greater or equal to 0" self.mp_degree = max(self.mp_degree, 1) self.pp_degree = max(self.pp_degree, 1) + self.sep_degree = max(self.sep_degree, 1) if self.dp_degree < 0: nranks = paddle.distributed.get_world_size() @@ -397,6 +402,7 @@ def _init_hybrid_parallel_env(self): "pp": ['pipe', self.pp_degree], "sharding": ['sharding', self.sharding_degree], "mp": ['model', self.mp_degree], + "sep": ["sep", self.sep_degree], } order = self._user_defined_strategy.hybrid_parallel_order diff --git a/test/collective/fleet/CMakeLists.txt b/test/collective/fleet/CMakeLists.txt index 9d7f9bcdb96c9..74e4ab4ae64b2 100644 --- a/test/collective/fleet/CMakeLists.txt +++ b/test/collective/fleet/CMakeLists.txt @@ -243,7 +243,7 @@ if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) ENVS "PADDLE_DIST_UT_PORT=21222;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" ) - set_tests_properties(test_pipeline PROPERTIES TIMEOUT "120") + set_tests_properties(test_pipeline PROPERTIES TIMEOUT "160") endif() if(LOCAL_ALL_ARCH AND (LINUX OR APPLE)) py_test_modules( @@ -332,7 +332,7 @@ if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) LABELS "RUN_TYPE=DIST" ENVS - "NVIDIA_TF32_OVERRIDE=0;PADDLE_DIST_UT_PORT=21234;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" + "PADDLE_DIST_UT_PORT=21234;NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" ) set_tests_properties(test_hybrid_parallel_inference_helper PROPERTIES TIMEOUT "120") @@ -351,12 +351,8 @@ if((WITH_GPU OR WITH_ROCM) AND (LINUX OR WIN32)) endif() if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) py_test_modules( - test_recv_save_op - MODULES - test_recv_save_op - ENVS - "NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" - ) + test_recv_save_op MODULES test_recv_save_op ENVS + "http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python") endif() if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) py_test_modules( @@ -435,6 +431,21 @@ if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) set_tests_properties(test_parallel_dygraph_tensor_parallel PROPERTIES TIMEOUT "200") endif() +if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) + bash_test_modules( + test_parallel_dygraph_sep_parallel + START_BASH + ../../legacy_test/dist_test.sh + TIMEOUT + "120" + LABELS + "RUN_TYPE=DIST" + ENVS + "PADDLE_DIST_UT_PORT=21242;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" + ) + set_tests_properties(test_parallel_dygraph_sep_parallel PROPERTIES TIMEOUT + "120") +endif() if(LOCAL_ALL_ARCH AND LOCAL_ALL_PLAT) bash_test_modules( test_dygraph_group_sharded_api_for_eager @@ -705,7 +716,7 @@ if((WITH_GPU OR WITH_ROCM) AND LOCAL_ALL_PLAT) LABELS "RUN_TYPE=DIST" ENVS - "NVIDIA_TF32_OVERRIDE=0;PADDLE_DIST_UT_PORT=21274;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" + "PADDLE_DIST_UT_PORT=21274;NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python" ) set_tests_properties(test_parallel_dygraph_mnist PROPERTIES TIMEOUT "200") endif() diff --git a/test/collective/fleet/hybrid_parallel_sep_model.py b/test/collective/fleet/hybrid_parallel_sep_model.py new file mode 100644 index 0000000000000..3bb83f27dc01d --- /dev/null +++ b/test/collective/fleet/hybrid_parallel_sep_model.py @@ -0,0 +1,51 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import random +import unittest + +import numpy as np + +import paddle +from paddle.distributed import fleet + + +class TestDistMPTraining(unittest.TestCase): + def setUp(self): + random.seed(2023) + np.random.seed(2023) + paddle.seed(2023) + + self.strategy = fleet.DistributedStrategy() + self.strategy.hybrid_configs = { + "sharding_degree": 1, + "dp_degree": 1, + "mp_degree": 1, + "pp_degree": 1, + "sep_degree": 2, + } + fleet.init(is_collective=True, strategy=self.strategy) + + def test_basic_hcg(self): + hcg = fleet.get_hybrid_communicate_group() + assert hcg.get_sep_parallel_rank() >= 0 + assert hcg.get_sep_parallel_world_size() == 2 + assert hcg.get_sep_parallel_group_src_rank() == 0 + assert hcg.get_sep_parallel_group() is not None + assert hcg.get_dp_sep_parallel_group() is not None + assert hcg.get_pp_mp_parallel_group() is not None + + +if __name__ == "__main__": + unittest.main() diff --git a/test/collective/fleet/test_parallel_dygraph_sep_parallel.py b/test/collective/fleet/test_parallel_dygraph_sep_parallel.py new file mode 100644 index 0000000000000..fd3525d5792b8 --- /dev/null +++ b/test/collective/fleet/test_parallel_dygraph_sep_parallel.py @@ -0,0 +1,26 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +from legacy_test.test_parallel_dygraph_dataparallel import TestMultipleGpus + + +class TestHybridParallel(TestMultipleGpus): + def test_hybrid_parallel_hcg(self): + self.run_mnist_2gpu('hybrid_parallel_sep_model.py') + + +if __name__ == "__main__": + unittest.main() diff --git a/test/collective/fleet/testslist.csv b/test/collective/fleet/testslist.csv index 751a67754cf55..c0369da06ac06 100644 --- a/test/collective/fleet/testslist.csv +++ b/test/collective/fleet/testslist.csv @@ -3,81 +3,83 @@ test_fleet_sharding_meta_optimizer,,GPU;XPU,350,DIST,test_runner.py,2,,http_prox test_fleet_static_mp_layers,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_dgc_op,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_DGC test_dgc_optimizer,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_DGC -test_parallel_margin_cross_entropy,,GPU,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL -test_parallel_dygraph_transformer,,GPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 -test_parallel_dygraph_transformer,,ROCM,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_margin_cross_entropy,,GPU,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL +test_parallel_dygraph_transformer,,GPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 +test_parallel_dygraph_transformer,,ROCM,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_fp16_allreduce_meta_optimizer,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_rnn_dp,,GPU;XPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_mp_layers,,GPU,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL -test_tcp_store,LINUX;APPLE,,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_dygraph_sharding_stage3_for_eager,,,350,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_rnn_dp,,GPU;XPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_mp_layers,,GPU,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL +test_tcp_store,LINUX;APPLE,,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_dygraph_sharding_stage3_for_eager,,,350,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_communicator_half_async,,,120,DIST,test_runner.py,2,,FLAGS_communicator_send_queue_size=1;FLAGS_communicator_max_merge_var_num=1;http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL -test_parallel_dygraph_pipeline_parallel,,GPU,500,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_pipeline_parallel_sync_send,,GPU;XPU,300,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..;PADDLE_P2P_SYNC_SEND=1, -test_parallel_dygraph_pipeline_parallel_with_virtual_stage,,GPU,500,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_pp_adaptor,,GPU,500,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_pipeline_parallel,,GPU,500,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_pipeline_parallel_sync_send,,GPU;XPU,300,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..;PADDLE_P2P_SYNC_SEND=1, +test_parallel_dygraph_pipeline_parallel_with_virtual_stage,,GPU,500,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_pp_adaptor,,GPU,500,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_localsgd_meta_optimizer,LINUX,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_class_center_sample,,GPU,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL -test_pipeline,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_class_center_sample,,GPU,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL +test_pipeline,,,160,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_utils,LINUX;APPLE,,120,DIST,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_static_model_parallel,,,240,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_no_sync,,GPU,300,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL -test_dygraph_sharding_stage2,,,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_control_flow,,,350,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_fleet_lars_meta_optimizer,,GPU;XPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_hybrid_parallel_inference_helper,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_static_model_parallel,,,240,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_no_sync,,GPU,300,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL +test_dygraph_sharding_stage2,,,200,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_control_flow,,,350,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_fleet_lars_meta_optimizer,,GPU;XPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_hybrid_parallel_inference_helper,,,120,DIST,../../legacy_test/dist_test.sh,2,,NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_rolemaker_new,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_dist_mnist_gradient_merge,LINUX;WIN32,GPU;ROCM,360,DIST,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_recv_save_op,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_communicator_sync,,,,,test_runner.py,2,,FLAGS_communicator_send_queue_size=1;FLAGS_communicator_max_merge_var_num=1;http_proxy=;https_proxy=;PYTHONPATH=../.., -test_fleet_pipeline_meta_optimizer,,GPU;XPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_fleet_pipeline_meta_optimizer,,GPU;XPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_gradient_merge_meta_optimizer,,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_amp_init,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_meta_optimizer_base,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_fleet_raw_program_meta_optimizer,,GPU;XPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_sharding_parallel,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_tensor_parallel,,,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_dygraph_group_sharded_api_for_eager,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_fleet_raw_program_meta_optimizer,,GPU;XPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_sharding_parallel,,,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_tensor_parallel,,,200,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_sep_parallel,,,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_dygraph_group_sharded_api_for_eager,,,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_distributed_strategy,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_dgc_meta_optimizer,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_DGC -test_parallel_dygraph_unused_variables,,,350,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_unused_variables,,,350,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_lamb_meta_optimizer,LINUX,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_dgc_momentum_op,,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_DGC -test_parallel_dygraph_no_sync_gradient_check,,,60,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_fleet_pipeline_meta_optimizer_with_recompute,,GPU;XPU,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_no_sync_gradient_check,,,60,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_fleet_pipeline_meta_optimizer_with_recompute,,GPU;XPU,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_hybrid_meta_optimizer,LINUX;WIN32,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_qat,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_sparse_embedding,,GPU,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 -test_parallel_dygraph_sparse_embedding,,ROCM,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_qat,,,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_sparse_embedding,,GPU,200,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 +test_parallel_dygraph_sparse_embedding,,ROCM,200,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_amp_meta_optimizer,,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_sparse_embedding_over_height,,GPU,150,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 -test_parallel_dygraph_sparse_embedding_over_height,,ROCM,350,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_fleet_qat_meta_optimizer,,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_sparse_embedding_over_height,,GPU,150,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL;${NCCL_VERSION} VERSION_GREATER_EQUAL 2212 +test_parallel_dygraph_sparse_embedding_over_height,,ROCM,350,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_distributed_strategy,LINUX;APPLE,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_parallel_parallelizer,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_parallel_parallelizer,,,120,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_recompute_meta_optimizer,LINUX;WIN32,GPU;XPU,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_private_function,LINUX;WIN32,,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_new_group,,GPU;XPU,,DIST,test_new_group.sh,2,,http_proxy=;https_proxy=, test_c_comm_init_op,LINUX,GPU;XPU,120,DIST,test_c_comm_init_op.sh,2,,http_proxy=;https_proxy=, test_fused_attention_pass_with_mp,LINUX,GPU,120,DIST,test_fused_attention_pass_with_mp.sh,2,,http_proxy=;https_proxy=, -test_ir_pass_pipeline,,,120,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_mnist,,GPU;ROCM,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_parallel_dygraph_se_resnext,,GPU;ROCM,200,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_ir_pass_pipeline,,,240,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_mnist,,GPU;ROCM,200,DIST,../../legacy_test/dist_test.sh,2,,NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../.., +test_parallel_dygraph_se_resnext,,GPU;ROCM,200,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_parallel_dygraph_sync_batch_norm,,GPU;ROCM,120,DIST,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_imperative_auto_mixed_precision_for_eager,,GPU;ROCM,300,DIST,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_mixed_precision,,GPU;ROCM,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_dygraph_recompute_for_eager,,GPU;ROCM,,,test_runner.py,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_dist_mnist_dgc_nccl,,,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL OR WITH_RCCL;WITH_DGC -test_dist_se_resnext_dgc,,,,DIST,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL OR WITH_RCCL;WITH_DGC -test_auto_checkpoint,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_checkpoint1,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_checkpoint2,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_checkpoint3,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_checkpoint_multiple,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_auto_checkpoint_dist_basic,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_hdfs1,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_hdfs2,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_hdfs3,LINUX,,200,EXCLUSIVE:NIGHTLY,../../dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_dist_mnist_dgc_nccl,,,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL OR WITH_RCCL;WITH_DGC +test_dist_se_resnext_dgc,,,,DIST,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../..,WITH_NCCL OR WITH_RCCL;WITH_DGC +test_auto_checkpoint,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_checkpoint1,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_checkpoint2,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_checkpoint3,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_checkpoint_multiple,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_auto_checkpoint_dist_basic,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_hdfs1,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_hdfs2,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_hdfs3,LINUX,,200,EXCLUSIVE:NIGHTLY,../../legacy_test/dist_test.sh,2,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_checkpoint,LINUX,GPU;ROCM,200,EXCLUSIVE:NIGHTLY,test_runner.py,,,http_proxy=;https_proxy=;PYTHONPATH=../.., test_fleet_log,,,200,DIST,test_runner.py,,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_dygraph_dist_save_load,LINUX,GPU,200,DIST,test_runner.py,,,http_proxy=;https_proxy=;PYTHONPATH=../.., -test_dygraph_save_for_auto_infer,LINUX,GPU,300,DIST,test_runner.py,,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_dygraph_dist_save_load,LINUX,GPU,300,DIST,test_runner.py,,,http_proxy=;https_proxy=;PYTHONPATH=../.., +test_dygraph_save_for_auto_infer,LINUX,GPU,300,DIST,test_runner.py,,,NVIDIA_TF32_OVERRIDE=0;http_proxy=;https_proxy=;PYTHONPATH=../.., diff --git a/test/legacy_test/test_hybrid_parallel_topology.py b/test/legacy_test/test_hybrid_parallel_topology.py index 33d7bca3f4dcb..6614a59ed8e12 100644 --- a/test/legacy_test/test_hybrid_parallel_topology.py +++ b/test/legacy_test/test_hybrid_parallel_topology.py @@ -214,6 +214,277 @@ def test_topology_4D(self): self.assertEqual(topo.get_dim_size("pp"), 2) self.assertEqual(topo.get_dim_size("sharding"), 2) + def test_topology_5D(self): + topo = fleet.CommunicateTopology( + ["dp", "pp", "sharding", "sep", "mp"], [2, 2, 2, 2, 2] + ) + + # test get_comm_list + dp_comm_list = [ + [0, 16], + [1, 17], + [2, 18], + [3, 19], + [4, 20], + [5, 21], + [6, 22], + [7, 23], + [8, 24], + [9, 25], + [10, 26], + [11, 27], + [12, 28], + [13, 29], + [14, 30], + [15, 31], + ] + mp_comm_list = [ + [0, 1], + [2, 3], + [4, 5], + [6, 7], + [8, 9], + [10, 11], + [12, 13], + [14, 15], + [16, 17], + [18, 19], + [20, 21], + [22, 23], + [24, 25], + [26, 27], + [28, 29], + [30, 31], + ] + pp_comm_list = [ + [0, 8], + [1, 9], + [2, 10], + [3, 11], + [4, 12], + [5, 13], + [6, 14], + [7, 15], + [16, 24], + [17, 25], + [18, 26], + [19, 27], + [20, 28], + [21, 29], + [22, 30], + [23, 31], + ] + sharding_comm_list = [ + [0, 4], + [1, 5], + [2, 6], + [3, 7], + [8, 12], + [9, 13], + [10, 14], + [11, 15], + [16, 20], + [17, 21], + [18, 22], + [19, 23], + [24, 28], + [25, 29], + [26, 30], + [27, 31], + ] + sep_comm_list = [ + [0, 2], + [1, 3], + [4, 6], + [5, 7], + [8, 10], + [9, 11], + [12, 14], + [13, 15], + [16, 18], + [17, 19], + [20, 22], + [21, 23], + [24, 26], + [25, 27], + [28, 30], + [29, 31], + ] + + np.testing.assert_array_equal(dp_comm_list, topo.get_comm_list("dp")) + np.testing.assert_array_equal(mp_comm_list, topo.get_comm_list("mp")) + np.testing.assert_array_equal(pp_comm_list, topo.get_comm_list("pp")) + np.testing.assert_array_equal( + sharding_comm_list, topo.get_comm_list("sharding") + ) + np.testing.assert_array_equal(sep_comm_list, topo.get_comm_list("sep")) + + # test get_fused_ranks + dp_sep_fuse_comm_list = [ + [0, 2, 16, 18], + [1, 3, 17, 19], + [4, 6, 20, 22], + [5, 7, 21, 23], + [8, 10, 24, 26], + [9, 11, 25, 27], + [12, 14, 28, 30], + [13, 15, 29, 31], + ] + pp_mp_fuse_comm_list = [ + [0, 1, 8, 9], + [2, 3, 10, 11], + [4, 5, 12, 13], + [6, 7, 14, 15], + [16, 17, 24, 25], + [18, 19, 26, 27], + [20, 21, 28, 29], + [22, 23, 30, 31], + ] + + np.testing.assert_array_equal( + sorted(dp_sep_fuse_comm_list), + sorted(topo.get_fused_ranks(["dp", "sep"])), + ) + np.testing.assert_array_equal( + sorted(pp_mp_fuse_comm_list), + sorted(topo.get_fused_ranks(["pp", "mp"])), + ) + + # test get_hybrid_group_names + parallel_names = ["dp", "pp", "sharding", "sep", "mp"] + np.testing.assert_array_equal( + parallel_names, topo.get_hybrid_group_names() + ) + + # test get_dims + np.testing.assert_array_equal(2, topo.get_dim("dp")) + np.testing.assert_array_equal(2, topo.get_dim("mp")) + np.testing.assert_array_equal(2, topo.get_dim("pp")) + np.testing.assert_array_equal(2, topo.get_dim("sharding")) + np.testing.assert_array_equal(2, topo.get_dim("sep")) + + # test world size + self.assertEqual(topo.world_size(), 32) + + # test get_rank + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=0, sep=0, mp=0), 0) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=0, sep=0, mp=1), 1) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=0, sep=1, mp=0), 2) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=0, sep=1, mp=1), 3) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=1, sep=0, mp=0), 4) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=1, sep=0, mp=1), 5) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=1, sep=1, mp=0), 6) + self.assertEqual(topo.get_rank(dp=0, pp=0, sharding=1, sep=1, mp=1), 7) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=0, sep=0, mp=0), 8) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=0, sep=0, mp=1), 9) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=0, sep=1, mp=0), 10) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=0, sep=1, mp=1), 11) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=1, sep=0, mp=0), 12) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=1, sep=0, mp=1), 13) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=1, sep=1, mp=0), 14) + self.assertEqual(topo.get_rank(dp=0, pp=1, sharding=1, sep=1, mp=1), 15) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=0, sep=0, mp=0), 16) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=0, sep=0, mp=1), 17) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=0, sep=1, mp=0), 18) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=0, sep=1, mp=1), 19) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=1, sep=0, mp=0), 20) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=1, sep=0, mp=1), 21) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=1, sep=1, mp=0), 22) + self.assertEqual(topo.get_rank(dp=1, pp=0, sharding=1, sep=1, mp=1), 23) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=0, sep=0, mp=0), 24) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=0, sep=0, mp=1), 25) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=0, sep=1, mp=0), 26) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=0, sep=1, mp=1), 27) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=1, sep=0, mp=0), 28) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=1, sep=0, mp=1), 29) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=1, sep=1, mp=0), 30) + self.assertEqual(topo.get_rank(dp=1, pp=1, sharding=1, sep=1, mp=1), 31) + + # test get_coord + self.assertEqual(topo.get_coord(0), topo.coordinate(0, 0, 0, 0, 0)) + self.assertEqual(topo.get_coord(1), topo.coordinate(0, 0, 0, 0, 1)) + self.assertEqual(topo.get_coord(2), topo.coordinate(0, 0, 0, 1, 0)) + self.assertEqual(topo.get_coord(3), topo.coordinate(0, 0, 0, 1, 1)) + self.assertEqual(topo.get_coord(4), topo.coordinate(0, 0, 1, 0, 0)) + self.assertEqual(topo.get_coord(5), topo.coordinate(0, 0, 1, 0, 1)) + self.assertEqual(topo.get_coord(6), topo.coordinate(0, 0, 1, 1, 0)) + self.assertEqual(topo.get_coord(7), topo.coordinate(0, 0, 1, 1, 1)) + self.assertEqual(topo.get_coord(8), topo.coordinate(0, 1, 0, 0, 0)) + self.assertEqual(topo.get_coord(9), topo.coordinate(0, 1, 0, 0, 1)) + self.assertEqual(topo.get_coord(10), topo.coordinate(0, 1, 0, 1, 0)) + self.assertEqual(topo.get_coord(11), topo.coordinate(0, 1, 0, 1, 1)) + self.assertEqual(topo.get_coord(12), topo.coordinate(0, 1, 1, 0, 0)) + self.assertEqual(topo.get_coord(13), topo.coordinate(0, 1, 1, 0, 1)) + self.assertEqual(topo.get_coord(14), topo.coordinate(0, 1, 1, 1, 0)) + self.assertEqual(topo.get_coord(15), topo.coordinate(0, 1, 1, 1, 1)) + self.assertEqual(topo.get_coord(16), topo.coordinate(1, 0, 0, 0, 0)) + self.assertEqual(topo.get_coord(17), topo.coordinate(1, 0, 0, 0, 1)) + self.assertEqual(topo.get_coord(18), topo.coordinate(1, 0, 0, 1, 0)) + self.assertEqual(topo.get_coord(19), topo.coordinate(1, 0, 0, 1, 1)) + self.assertEqual(topo.get_coord(20), topo.coordinate(1, 0, 1, 0, 0)) + self.assertEqual(topo.get_coord(21), topo.coordinate(1, 0, 1, 0, 1)) + self.assertEqual(topo.get_coord(22), topo.coordinate(1, 0, 1, 1, 0)) + self.assertEqual(topo.get_coord(23), topo.coordinate(1, 0, 1, 1, 1)) + self.assertEqual(topo.get_coord(24), topo.coordinate(1, 1, 0, 0, 0)) + self.assertEqual(topo.get_coord(25), topo.coordinate(1, 1, 0, 0, 1)) + self.assertEqual(topo.get_coord(26), topo.coordinate(1, 1, 0, 1, 0)) + self.assertEqual(topo.get_coord(27), topo.coordinate(1, 1, 0, 1, 1)) + self.assertEqual(topo.get_coord(28), topo.coordinate(1, 1, 1, 0, 0)) + self.assertEqual(topo.get_coord(29), topo.coordinate(1, 1, 1, 0, 1)) + self.assertEqual(topo.get_coord(30), topo.coordinate(1, 1, 1, 1, 0)) + self.assertEqual(topo.get_coord(31), topo.coordinate(1, 1, 1, 1, 1)) + + # test get_axis_list + self.assertEqual( + topo.get_axis_list("dp", 0), + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + ) + self.assertEqual( + topo.get_axis_list("dp", 1), + [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], + ) + + self.assertEqual( + topo.get_axis_list("sep", 0), + [0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29], + ) + self.assertEqual( + topo.get_axis_list("sep", 1), + [2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31], + ) + + self.assertEqual( + topo.get_axis_list("mp", 0), + [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30], + ) + self.assertEqual( + topo.get_axis_list("mp", 1), + [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31], + ) + self.assertEqual( + topo.get_axis_list("pp", 0), + [0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23], + ) + self.assertEqual( + topo.get_axis_list("pp", 1), + [8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31], + ) + self.assertEqual( + topo.get_axis_list("sharding", 0), + [0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27], + ) + self.assertEqual( + topo.get_axis_list("sharding", 1), + [4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31], + ) + + # test get_dim_size + self.assertEqual(topo.get_dim_size("dp"), 2) + self.assertEqual(topo.get_dim_size("mp"), 2) + self.assertEqual(topo.get_dim_size("pp"), 2) + self.assertEqual(topo.get_dim_size("sharding"), 2) + self.assertEqual(topo.get_dim_size("sep"), 2) + if __name__ == '__main__': unittest.main() From 5406699dfa2af53c4c9711fdba390da5d10baad2 Mon Sep 17 00:00:00 2001 From: Yuanle Liu Date: Wed, 6 Sep 2023 10:18:30 +0800 Subject: [PATCH 134/194] [IR&PASS] fix constant_folding_pass and add use_count api for Value (#56967) --- .../fluid/ir/transforms/constant_folding_pass.cc | 14 ++++++-------- paddle/ir/core/value.cc | 9 +++++++++ paddle/ir/core/value.h | 2 ++ paddle/ir/pass/pass.h | 2 +- .../ir/pattern_rewrite/pattern_rewrite_driver.cc | 14 +++++++++----- .../cpp/ir/pattern_rewrite/pattern_rewrite_test.cc | 2 +- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/ir/transforms/constant_folding_pass.cc b/paddle/fluid/ir/transforms/constant_folding_pass.cc index e114cf2ca0317..93699e3eae165 100644 --- a/paddle/fluid/ir/transforms/constant_folding_pass.cc +++ b/paddle/fluid/ir/transforms/constant_folding_pass.cc @@ -92,14 +92,13 @@ class ConstantFoldingPattern : public ir::RewritePattern { } // Execute program - paddle::framework::interpreter::ExecutionConfig exe_config; - exe_config.create_local_scope = false; + exe_config_.create_local_scope = false; paddle::framework::InterpreterCore core( phi::CPUPlace{}, fetch_var_names, paddle::dialect::PdOpLowerToKernelPass(temp_program.get()), &scope_, - exe_config); + exe_config_); paddle::framework::FetchList fetch_list = core.Run({}); @@ -112,6 +111,7 @@ class ConstantFoldingPattern : public ir::RewritePattern { std::string param_name = "@constant_folding_pass@_" + std::to_string(suffix_++); + exe_config_.skip_gc_vars.insert(param_name); auto* param_var = scope_.Var(param_name); auto* param_tensor = param_var->GetMutable(); @@ -180,13 +180,11 @@ class ConstantFoldingPattern : public ir::RewritePattern { } private: - static size_t suffix_; - static paddle::framework::Scope scope_; + inline static size_t suffix_{0}; + inline static paddle::framework::Scope scope_{}; + inline static paddle::framework::interpreter::ExecutionConfig exe_config_{}; }; -size_t ConstantFoldingPattern::suffix_ = 0; -paddle::framework::Scope ConstantFoldingPattern::scope_ = {}; - class ConstantFoldingPass : public ir::Pass { public: // TODO(liuyuanle): Naming convention for pass. diff --git a/paddle/ir/core/value.cc b/paddle/ir/core/value.cc index 58622a4754023..c652ef23a6dde 100644 --- a/paddle/ir/core/value.cc +++ b/paddle/ir/core/value.cc @@ -13,6 +13,9 @@ // limitations under the License. #include "paddle/ir/core/value.h" + +#include + #include "paddle/ir/core/enforce.h" #include "paddle/ir/core/operation.h" #include "paddle/ir/core/value_impl.h" @@ -128,6 +131,12 @@ bool Value::HasOneUse() const { return impl_->HasOneUse(); } +size_t Value::use_count() const { + size_t count = 0; + for (auto it = use_begin(); it != use_end(); ++it) count++; + return count; +} + void Value::ReplaceUsesWithIf( Value new_value, const std::function &should_replace) const { diff --git a/paddle/ir/core/value.h b/paddle/ir/core/value.h index 6594016e74402..3bbeaa4e55a58 100644 --- a/paddle/ir/core/value.h +++ b/paddle/ir/core/value.h @@ -123,6 +123,8 @@ class IR_API Value { bool HasOneUse() const; + size_t use_count() const; + friend struct std::hash; void ReplaceUsesWithIf( diff --git a/paddle/ir/pass/pass.h b/paddle/ir/pass/pass.h index 4a4cbf629d678..5499f2172f294 100644 --- a/paddle/ir/pass/pass.h +++ b/paddle/ir/pass/pass.h @@ -78,7 +78,7 @@ class IR_API Pass { virtual ~Pass(); - std::string name() const { return pass_info().name; } + const std::string& name() const { return pass_info().name; } const detail::PassInfo& pass_info() const { return pass_info_; } diff --git a/paddle/ir/pattern_rewrite/pattern_rewrite_driver.cc b/paddle/ir/pattern_rewrite/pattern_rewrite_driver.cc index 01d660159f015..f574ed24afe27 100644 --- a/paddle/ir/pattern_rewrite/pattern_rewrite_driver.cc +++ b/paddle/ir/pattern_rewrite/pattern_rewrite_driver.cc @@ -131,11 +131,15 @@ class GreedyPatternRewriteDriver : public ir::PatternRewriter { for (uint32_t i = 0; i < op->num_operands(); ++i) { AddOperandToWorklist(op->operand_source(i)); } - for (uint32_t i = 0; i < op->num_regions(); ++i) { - auto& region = op->region(i); - for (auto& block : region) { - for (auto& op_item : *block) { - RemoveFromWorklist(op_item); + if (op->num_regions() == 0) { + RemoveFromWorklist(op); + } else { + for (uint32_t i = 0; i < op->num_regions(); ++i) { + auto& region = op->region(i); + for (auto& block : region) { + for (auto& op_item : *block) { + RemoveFromWorklist(op_item); + } } } } diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index c10dc01d44ced..e007b73c9f0ed 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -1097,7 +1097,7 @@ TEST(pattern_rewrite, Patterns) { ir::PassManager pm(ctx); pm.AddPass(std::make_unique()); - // pm.AddPass(ir::CreateConstantFoldingPass()); + pm.AddPass(ir::CreateConstantFoldingPass()); pm.AddPass(ir::CreateDeadCodeEliminationPass()); pm.EnablePassTiming(); pm.EnableIRPrinting(); From 7314cf6954bf572334ee513709d4fe0549ecf311 Mon Sep 17 00:00:00 2001 From: umiswing Date: Wed, 6 Sep 2023 10:27:50 +0800 Subject: [PATCH 135/194] Add flash_attention_deterministic to ci gpups. (#56930) --- tools/gpups_test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/gpups_test.sh b/tools/gpups_test.sh index fe2f93a34c91e..77f9a313f8103 100644 --- a/tools/gpups_test.sh +++ b/tools/gpups_test.sh @@ -47,6 +47,7 @@ parallel_list="^init_phi_test$|\ ^test_dist_fleet_ps12$|\ ^test_executor_feed_non_tensor$|\ ^test_flash_attention$|\ +^test_flash_attention_deterministic$|\ ^test_fused_adam_op$|\ ^test_fused_attention_no_dropout$|\ ^test_fused_attention_op$|\ From 4dbe441c6c497056eb942124727c160f6676dc14 Mon Sep 17 00:00:00 2001 From: cyberslack_lee Date: Wed, 6 Sep 2023 10:28:13 +0800 Subject: [PATCH 136/194] [xdoctest] reformat example code with google style in No. 250-260 (#56541) * test=docs_preview * test=docs_preview * test=docs_preview * test=docs_preview * test=docs_preview * test=docs_preview * fix * test=docs_preview * test=docs_preview * fix * move stmts under imports --------- Co-authored-by: SigureMo --- .../fleet/parameter_server/pslib/__init__.py | 583 ++++--- .../distributed/models/moe/grad_clip.py | 30 +- .../distributed/models/moe/moe_layer.py | 113 +- python/paddle/incubate/layers/nn.py | 798 +++++---- .../nn/functional/fused_dropout_add.py | 30 +- .../incubate/nn/functional/fused_ec_moe.py | 31 +- .../nn/functional/fused_gate_attention.py | 126 +- .../nn/functional/fused_matmul_bias.py | 91 +- .../fused_rotary_position_embedding.py | 5 +- python/paddle/tensor/linalg.py | 1481 +++++++++-------- python/paddle/tensor/logic.py | 377 +++-- 11 files changed, 1959 insertions(+), 1706 deletions(-) diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py b/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py index d8b61aadb5c0c..3f36562b2adc2 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py @@ -67,8 +67,8 @@ def init_worker(self): should call init_worker() to initialize global information about worker and connect worker with pserver. You should run startup program before init_worker. Args: - executor(Executor): The executor to run for init server. - programs(Program|None): The program that need to run. + executor (Executor): The executor to run for init server. + programs (Program|None): The program that need to run. """ if len(self._main_programs) == 0: @@ -167,16 +167,24 @@ def init_worker(self): def init_server(self, model_dir=None, **kwargs): """ - init_server() will be called by user. It will load model from model_dir. + Called by user. It will load model from model_dir. + Args: - model_dir(str): load model path, can be local or hdfs/afs path. - kwargs: user-defined attributes, currently support following: - model(int): load model mode. - 0 is for load whole model, - 1 is for load delta model (load diff), - default is 0. - Example: - >>> fleet.init_server("/you/path/to/model", mode = 0) + model_dir(str, optional): Load model path, can be local or hdfs/afs path. Default is None. + kwargs: User-defined attributes, currently support following: + + - model(int): Load model mode. + + 0 is for load whole model, + 1 is for load delta model (load diff). + Default is 0. + + Examples: + + .. code-block:: text + + fleet.init_server("/you/path/to/model", mode = 0) + """ mode = kwargs.get("mode", 0) if isinstance(self._role_maker, HeterRoleMaker): @@ -192,8 +200,7 @@ def init_server(self, model_dir=None, **kwargs): def run_server(self): """ - init_pserver(): will be called by user. When a user knows current process is_worker(), he/she - should call init_pserver() to initialize global information about parameter server + Called by user. When a user init server, after that he/she should run run_server() to start. """ if self._opt_info: if "fleet_desc" in self._opt_info: @@ -296,8 +303,8 @@ def start_heter_trainer( def stop_worker(self): """ - stop(): will be called after a user finishes his/her training task. Fleet instance will be - destroyed when stop() is called. + Will be called after a user finishes his/her training task. Fleet instance will be + destroyed when stop_worker() is called. """ self._role_maker._barrier_worker() # all worker should be finalize first @@ -315,14 +322,20 @@ def stop_worker(self): def distributed_optimizer(self, optimizer, strategy={}): """ distributed_optimizer + Args: - optimizer(Optimizer): optimizer - strategy(dict): strategy - Examples: - .. code-block:: python - fleet.distributed_optimizer(optimizer) + optimizer (Optimizer): Optimizer. + strategy (dict): Strategy. + Returns: - optimizer(DownpourOptimizer): downpour optimizer + optimizer(DownpourOptimizer): Downpour optimizer. + + Examples: + + .. code-block:: text + + fleet.distributed_optimizer(optimizer) + """ self._optimizer = DownpourOptimizer(optimizer, strategy) return self._optimizer @@ -337,29 +350,42 @@ def save_inference_model( export_for_deployment=True, ): """ - save pserver model called from a worker + Save pserver model called from a worker. + Args: - executor(Executor): fluid executor - dirname(str): save model path - feeded_var_names(list): default None - target_vars(list): default None - main_program(Program): default None - export_for_deployment(bool): default None + + executor (Executor): Fluid executor. + dirname (str): Save model path. + feeded_var_names (list, optional): Default None. + target_vars (list, optional): Default None. + main_program (Program, optional): Default None. + export_for_deployment (bool, optional): Default None. + Examples: - .. code-block:: python - fleet.save_inference_model(dirname="hdfs:/my/path") + + .. code-block:: text + + fleet.save_inference_model(dirname="hdfs:/my/path") + """ self._fleet_ptr.save_model(dirname, 0) def print_table_stat(self, table_id, pass_id, threshold): """ - print stat info of table_id, - format: tableid, feasign size, mf size + Print stat info of table_id, format: tableid, feasign size, mf size. + Args: - table_id(int): the id of table - Example: - .. code-block:: python - fleet.print_table_stat(0) + + table_id (int): The id of table. + pass_id (int): The id of pass. + threshold (float): The threshold of print. + + Examples: + + .. code-block:: text + + fleet.print_table_stat(0) + """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -368,13 +394,19 @@ def print_table_stat(self, table_id, pass_id, threshold): def set_file_num_one_shard(self, table_id, file_num): """ - set file_num in one shard + Set file_num in one shard. + Args: - table_id(int): the id of table - file_num(int): file num in one shard - Example: - .. code-block:: python - fleet.set_file_num_one_shard(0, 5) + + table_id (int): The id of table. + file_num (int): File num in one shard. + + Examples: + + .. code-block:: text + + fleet.set_file_num_one_shard(0, 5) + """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -383,20 +415,28 @@ def set_file_num_one_shard(self, table_id, file_num): def save_persistables(self, executor, dirname, main_program=None, **kwargs): """ - save presistable parameters, - when using fleet, it will save sparse and dense feature + Save presistable parameters, + when using fleet, it will save sparse and dense feature. + Args: - executor(Executor): fluid executor - dirname(str): save path. It can be hdfs/afs path or local path - main_program(Program): fluid program, default None - kwargs: use define property, current support following - mode(int): 0 means save all pserver model, - 1 means save delta pserver model (save diff), - 2 means save xbox base, - 3 means save batch model. - Example: - .. code-block:: python - fleet.save_persistables(dirname="/you/path/to/model", mode = 0) + + executor (Executor): Fluid executor. + dirname (str): Save path. It can be hdfs/afs path or local path. + main_program (Program, optional): Fluid program, default None. + kwargs: Use define property, current support following + + - mode (int): + 0 means save all pserver model, + 1 means save delta pserver model (save diff), + 2 means save xbox base, + 3 means save batch model. + + Examples: + + .. code-block:: test + + fleet.save_persistables(dirname="/you/path/to/model", mode = 0) + """ mode = kwargs.get("mode", 0) self._fleet_ptr.client_flush() @@ -409,23 +449,28 @@ def save_model_with_whitelist( self, executor, dirname, whitelist_path, main_program=None, **kwargs ): """ - save whitelist, mode is consistent with fleet.save_persistables, - when using fleet, it will save sparse and dense feature + Save whitelist, mode is consistent with fleet.save_persistables, + when using fleet, it will save sparse and dense feature. Args: - executor(Executor): fluid executor - dirname(str): save path. It can be hdfs/afs path or local path - main_program(Program): fluid program, default None - kwargs: use define property, current support following - mode(int): 0 means save all pserver model, - 1 means save delta pserver model (save diff), - 2 means save xbox base, - 3 means save batch model. - Example: - .. code-block:: python + executor (Executor): Fluid executor. + dirname (str): save path. It can be hdfs/afs path or local path. + whitelist_path (str): whitelist path. It can be hdfs/afs path or local path. + main_program (Program, optional): fluid program, default None. + kwargs: Use define property, current support following - fleet.save_persistables(dirname="/you/path/to/model", mode = 0) + - mode (int): + 0 means save all pserver model, + 1 means save delta pserver model (save diff), + 2 means save xbox base, + 3 means save batch model. + + Examples: + + .. code-block:: text + + fleet.save_persistables(dirname="/you/path/to/model", mode = 0) """ mode = kwargs.get("mode", 0) @@ -440,18 +485,23 @@ def save_model_with_whitelist( def save_multi_table_one_path(self, table_ids, model_dir, **kwargs): """ - save pslib multi sparse table in one path. + Save pslib multi sparse table in one path. + Args: - table_ids(list): table ids - model_dir(str): if you use hdfs, model_dir should starts with - 'hdfs:', otherwise means local dir - kwargs(dict): user-defined properties. - mode(int): the modes illustrated above, default 0 - prefix(str): the parts to save can have prefix, - for example, part-prefix-000-00000 + + table_ids (list): Table ids. + model_dir (str): If you use hdfs, model_dir should starts with 'hdfs:', otherwise means local dir. + kwargs (dict): User-defined properties. + + - mode (int): The modes illustrated above, default 0. + - prefix (str): the parts to save can have prefix, for example, part-prefix-000-00000. + Examples: - .. code-block:: python - fleet.save_multi_table_one_path("[0, 1]", "afs:/user/path/") + + .. code-block:: text + + fleet.save_multi_table_one_path("[0, 1]", "afs:/user/path/") + """ mode = kwargs.get("mode", 0) self._role_maker._barrier_worker() @@ -463,21 +513,30 @@ def save_multi_table_one_path(self, table_ids, model_dir, **kwargs): def save_cache_model(self, executor, dirname, main_program=None, **kwargs): """ - save sparse cache table, - when using fleet, it will save sparse cache table + Save sparse cache table, + when using fleet, it will save sparse cache table. + Args: - executor(Executor): fluid executor - dirname(str): save path. It can be hdfs/afs path or local path - main_program(Program): fluid program, default None - kwargs: use define property, current support following - mode(int): define for feature extension in the future, - currently no use, will pass a default value 0 - table_id(int): which table to save cache, default is 0 + + executor (Executor): Fluid executor. + dirname (str): Save path. It can be hdfs/afs path or local path. + main_program (Program, optional): Fluid program, default None. + kwargs: Use define property, current support following + + - mode (int): Define for feature extension in the future, + currently no use, will pass a default value 0. + - table_id (int): Which table to save cache, default is 0. + Returns: - feasign_num(int): cache feasign num - Example: - .. code-block:: python - fleet.save_cache_model(None, dirname="/you/path/to/model", mode = 0) + + feasign_num (int): cache feasign num. + + Examples: + + .. code-block:: text + + fleet.save_cache_model(None, dirname="/you/path/to/model", mode = 0) + """ mode = kwargs.get("mode", 0) table_id = kwargs.get("table_id", 0) @@ -506,10 +565,15 @@ def save_cache_model(self, executor, dirname, main_program=None, **kwargs): def shrink_sparse_table(self): """ - shrink cvm of all sparse embedding in pserver, the decay rate - is defined as "show_click_decay_rate" in fleet_desc.prototxt - Example: - >>> fleet.shrink_sparse_table() + Shrink cvm of all sparse embedding in pserver, the decay rate + is defined as "show_click_decay_rate" in fleet_desc.prototxt. + + Examples: + + .. code-block:: text + + fleet.shrink_sparse_table() + """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -523,18 +587,22 @@ def shrink_sparse_table(self): def shrink_dense_table(self, decay, emb_dim=11, scope=None, table_id=None): """ - shrink batch_sum in pserver by multiplying by decay + Shrink batch_sum in pserver by multiplying by decay. + Args: - decay(float): the decay rate, usually range in (0, 1) - emb_dim(int): one element's length in datanorm layer - scope(Scope): Scope object, default is fluid.global_scope() - table_id(int): table id of shrinking dense table. None means shrink all, - you should specify it when using multiple scopes, - default is None. - Example: - >>> fleet.shrink_dense_table(0.98, 11, myscope1, 1) - >>> fleet.shrink_dense_table(0.98, 11, myscope1, 2) - >>> fleet.shrink_dense_table(0.98, 11, myscope2, 3) + decay (float): The decay rate, usually range in (0, 1). + emb_dim (int, optional): One element's length in datanorm layer. Default is 11. + scope (Scope, optional): Scope object, default is fluid.global_scope(). Default is None. + table_id (int, optional): Table id of shrinking dense table. None means shrink all, + you should specify it when using multiple scopes, default is None. + + Examples: + + .. code-block:: text + + fleet.shrink_dense_table(0.98, 11, myscope1, 1) + fleet.shrink_dense_table(0.98, 11, myscope1, 2) + fleet.shrink_dense_table(0.98, 11, myscope2, 3) """ if scope is None: scope = paddle.static.global_scope() @@ -559,12 +627,17 @@ def shrink_dense_table(self, decay, emb_dim=11, scope=None, table_id=None): def clear_one_table(self, table_id): """ - clear_one_table() will be called by user. It will clear one table. + This function will be called by user. It will clear one table. + Args: - table_id(int): table id + + table_id (int): Table id. + Examples: - .. code-block:: python - fleet.clear_one_table(0) + + .. code-block:: text + + fleet.clear_one_table(0) """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -573,10 +646,13 @@ def clear_one_table(self, table_id): def clear_model(self): """ - clear_model() will be called by user. It will clear sparse model. + This function will be called by user. It will clear sparse model. + Examples: - .. code-block:: python - fleet.clear_model() + + .. code-block:: text + + fleet.clear_model() """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -585,40 +661,39 @@ def clear_model(self): def load_pslib_whitelist(self, table_id, model_path, **kwargs): """ - load pslib model for one table with whitelist + Load pslib model for one table with whitelist. Args: - table_id(int): load table id - model_path(str): load model path, can be local or hdfs/afs path - kwargs(dict): user defined params, currently support following: - only for load pslib model for one table: - mode(int): load model mode. 0 is for load whole model, 1 is - for load delta model (load diff), default is 0. - only for load params from paddle model: - scope(Scope): Scope object - model_proto_file(str): path of program desc proto binary - file, can be local or hdfs/afs file - var_names(list): var name list - load_combine(bool): load from a file or split param files - default False. + table_id (int): Load table id. + model_path (str): Load model path, can be local or hdfs/afs path. + kwargs (dict): User defined params, currently support following: + + - only for load pslib model for one table: + mode (int): load model mode. 0 is for load whole model, 1 is for load delta model (load diff), default is 0. + - only for load params from paddle model: + scope (Scope): Scope object. + model_proto_file (str): Path of program desc proto binary file, can be local or hdfs/afs file. + var_names (list): Var name list. + load_combine (bool): Load from a file or split param files, default False. Examples: - .. code-block:: python - # load pslib model for one table - fleet.load_one_table(0, "hdfs:/my_fleet_model/20190714/0/") - fleet.load_one_table(1, "hdfs:/xx/xxx", mode = 0) + .. code-block:: text + + # load pslib model for one table + fleet.load_one_table(0, "hdfs:/my_fleet_model/20190714/0/") + fleet.load_one_table(1, "hdfs:/xx/xxx", mode = 0) - # load params from paddle model - fleet.load_one_table(2, "hdfs:/my_paddle_model/", - scope = my_scope, - model_proto_file = "./my_program.bin", - load_combine = False) + # load params from paddle model + fleet.load_one_table(2, "hdfs:/my_paddle_model/", + scope = my_scope, + model_proto_file = "./my_program.bin", + load_combine = False) - # below is how to save proto binary file - with open("my_program.bin", "wb") as fout: - my_program = fluid.default_main_program() - fout.write(my_program.desc.serialize_to_string()) + # below is how to save proto binary file + with open("my_program.bin", "wb") as fout: + my_program = fluid.default_main_program() + fout.write(my_program.desc.serialize_to_string()) """ self._role_maker._barrier_worker() @@ -631,35 +706,39 @@ def load_pslib_whitelist(self, table_id, model_path, **kwargs): def load_one_table(self, table_id, model_path, **kwargs): """ - load pslib model for one table or load params from paddle model + Load pslib model for one table or load params from paddle model. + Args: - table_id(int): load table id - model_path(str): load model path, can be local or hdfs/afs path - kwargs(dict): user defined params, currently support following: - only for load pslib model for one table: - mode(int): load model mode. 0 is for load whole model, 1 is - for load delta model (load diff), default is 0. - only for load params from paddle model: - scope(Scope): Scope object - model_proto_file(str): path of program desc proto binary - file, can be local or hdfs/afs file - var_names(list): var name list - load_combine(bool): load from a file or split param files - default False. + + table_id (int): Load table id. + model_path (str): Load model path, can be local or hdfs/afs path. + kwargs (dict): user defined params, currently support following: + + - only for load pslib model for one table: + mode(int): load model mode. 0 is for load whole model, 1 is for load delta model (load diff), default is 0. + - only for load params from paddle model: + scope(Scope): Scope object. + model_proto_file(str): Path of program desc proto binary file, can be local or hdfs/afs file. + var_names(list): var name list. + load_combine(bool): load from a file or split param files, default False. + Examples: - .. code-block:: python - # load pslib model for one table - fleet.load_one_table(0, "hdfs:/my_fleet_model/20190714/0/") - fleet.load_one_table(1, "hdfs:/xx/xxx", mode = 0) - # load params from paddle model - fleet.load_one_table(2, "hdfs:/my_paddle_model/", - scope = my_scope, - model_proto_file = "./my_program.bin", - load_combine = False) - # below is how to save proto binary file - with open("my_program.bin", "wb") as fout: - my_program = fluid.default_main_program() - fout.write(my_program.desc.serialize_to_string()) + + .. code-block:: text + + # load pslib model for one table + fleet.load_one_table(0, "hdfs:/my_fleet_model/20190714/0/") + fleet.load_one_table(1, "hdfs:/xx/xxx", mode = 0) + # load params from paddle model + fleet.load_one_table(2, "hdfs:/my_paddle_model/", + scope = my_scope, + model_proto_file = "./my_program.bin", + load_combine = False) + # below is how to save proto binary file + with open("my_program.bin", "wb") as fout: + my_program = fluid.default_main_program() + fout.write(my_program.desc.serialize_to_string()) + """ self._role_maker._barrier_worker() mode = kwargs.get("mode", 0) @@ -691,15 +770,16 @@ def _load_one_table_from_paddle_model( load_combine=False, ): """ - load params from paddle model, and push params to pserver + Load params from paddle model, and push params to pserver. + Args: - scope(Scope): Scope object - table_id(int): the id of table to load - model_path(str): path of paddle model, can be local or hdfs/afs file - model_proto_file(str): path of program desc proto binary file, - can be local or hdfs/afs file - var_names(list): load var names - load_combine(bool): load from a file or split param files + scope (Scope): Scope object. + table_id (int): The id of table to load. + model_path (str): Path of paddle model, can be local or hdfs/afs file. + model_proto_file (str): Path of program desc proto binary file, can be local or hdfs/afs file. + var_names (list, optional): Load var names. Default is None. + load_combine (bool, optional): Load from a file or split param files. Default is False. + """ self._role_maker._barrier_worker() if self._role_maker.is_first_worker(): @@ -800,14 +880,18 @@ def load_model(self, model_dir=None, **kwargs): usually for online predict) 3: load batch model (do some statistic works in checkpoint, such as calculate unseen days of each feasign) + Args: - model_dir(str): if you use hdfs, model_dir should starts with - 'hdfs:', otherwise means local dir - kwargs(dict): user-defined properties. - mode(int): the modes illustrated above, default 0 + model_dir (str, optional): If you use hdfs, model_dir should starts with + 'hdfs:', otherwise means local dir. Default is None. + kwargs (dict): user-defined properties. + + - mode (int): The modes illustrated above, default 0. + Examples: - .. code-block:: python - fleet.load_model("afs:/user/path/") + .. code-block:: text + + fleet.load_model("afs:/user/path/") """ mode = kwargs.get("mode", 0) self._role_maker._barrier_worker() @@ -818,14 +902,19 @@ def load_model(self, model_dir=None, **kwargs): def save_model(self, model_dir=None, **kwargs): """ save pslib model, the modes are same with load model. + Args: - model_dir(str): if you use hdfs, model_dir should starts with - 'hdfs:', otherwise means local dir - kwargs(dict): user-defined properties. - mode(int): the modes illustrated above, default 0 + model_dir (str, optional): If you use hdfs, model_dir should starts with + 'hdfs:', otherwise means local dir. Default is None. + kwargs (dict): user-defined properties. + + - mode (int): The modes illustrated above, default 0. + Examples: - .. code-block:: python - fleet.save_model("afs:/user/path/") + .. code-block:: text + + fleet.save_model("afs:/user/path/") + """ mode = kwargs.get("mode", 0) prefix = kwargs.get("prefix", None) @@ -836,18 +925,21 @@ def save_model(self, model_dir=None, **kwargs): def save_one_table(self, table_id, model_dir, **kwargs): """ - save pslib model's one table, the modes are same with load model. + Save pslib model's one table, the modes are same with load model. + Args: - table_id(int): table id - model_dir(str): if you use hdfs, model_dir should starts with - 'hdfs:', otherwise means local dir - kwargs(dict): user-defined properties. - mode(int): the modes illustrated above, default 0 - prefix(str): the parts to save can have prefix, - for example, part-prefix-000-00000 + table_id (int): Table id. + model_dir (str): if you use hdfs, model_dir should starts with + 'hdfs:', otherwise means local dir. + kwargs (dict): user-defined properties. + + - mode (int): the modes illustrated above, default 0. + - prefix (str): the parts to save can have prefix, for example, part-prefix-000-00000. + Examples: - .. code-block:: python - fleet.save_one_table("afs:/user/path/") + .. code-block:: text + + fleet.save_one_table("afs:/user/path/") """ mode = kwargs.get("mode", 0) prefix = kwargs.get("prefix", None) @@ -890,15 +982,17 @@ def _prepare_params( dtype='float32', ): """ - preprocess params, this interface is not for users. + Preprocess params, this interface is not for users. + Args: - input(Variable|list of Variable): Input is a Tensor Variable - size(list of int): the embedding dim - is_sparse(bool): whether input is sparse ids - is_distributed(bool): whether in distributed mode - padding_idx(int): padding idx of input - param_attr(ParamAttr): To specify the weight parameter property - dtype(str): data type of output + input (Variable|list of Variable): Input is a Tensor Variable. + size (list of int): The embedding dim. + is_sparse (bool, optional): Whether input is sparse ids. Default is False. + is_distributed (bool, optional): Whether in distributed mode. Default is False. + padding_idx (int, optional): Padding idx of input. Default is None. + param_attr (ParamAttr, optional): To specify the weight parameter property. Default is None. + dtype (str, optional): Data type of output. Default is 'float32'. + """ if param_attr is None: raise ValueError("param_attr must be set") @@ -953,15 +1047,16 @@ def _fleet_embedding( dtype='float32', ): """ - add fleet embedding, this interface is not for users. + Add fleet embedding, this interface is not for users. + Args: - input(Variable|list of Variable): Input is a Tensor Variable - size(list of int): the embedding dim - is_sparse(bool): whether input is sparse ids - is_distributed(bool): whether in distributed mode - padding_idx(int): padding idx of input - param_attr(ParamAttr): To specify the weight parameter property - dtype(str): data type of output + input (Variable|list of Variable): Input is a Tensor Variable. + size (list[int]): The embedding dim. + is_sparse (bool, optional): Whether input is sparse ids. Default is False. + is_distributed (bool, optional): Whether in distributed mode. Default is False. + padding_idx (int, optional): Padding idx of input. Default is None. + param_attr (ParamAttr, optional): To specify the weight parameter property. Default is None. + dtype (str, optional): Data type of output. Default is 'float32'. """ def _pull_sparse( @@ -1041,15 +1136,16 @@ def _fleet_embedding_v2( dtype='float32', ): """ - add fleet embedding v2, this interface is not for users. + Add fleet embedding v2, this interface is not for users. + Args: - input(Variable|list of Variable): Input is a Tensor Variable - size(list of int): the embedding dim - is_sparse(bool): whether input is sparse ids - is_distributed(bool): whether in distributed mode - padding_idx(int): padding idx of input - param_attr(ParamAttr): To specify the weight parameter property - dtype(str): data type of output + input (Variable|list of Variable): Input is a Tensor Variable. + size (list[int]): The embedding dim. + is_sparse (bool, optional): Whether input is sparse ids. Default is False. + is_distributed (bool, optional): Whether in distributed mode. Default is False. + padding_idx (int, optional): Padding idx of input. Default is None. + param_attr (ParamAttr, optional): To specify the weight parameter property. Default is None. + dtype (str, optional): Data type of output. Default is 'float32'. """ def _pull_sparse_v2( @@ -1120,16 +1216,19 @@ def _pull_sparse_v2( class fleet_embedding: """ - fleet embedding class, it is used as a wrapper - Example: - .. code-block:: python - with fleet_embedding(click_name=label.name): - emb = paddle.static.nn.embedding( - input=var, - size=[-1, 11], - is_sparse=True, - is_distributed=True, - param_attr=fluid.ParamAttr(name="embedding")) + Fleet embedding class, it is used as a wrapper. + + Examples: + + .. code-block:: text + + with fleet_embedding(click_name=label.name): + emb = paddle.static.nn.embedding( + input=var, + size=[-1, 11], + is_sparse=True, + is_distributed=True, + param_attr=fluid.ParamAttr(name="embedding")) """ def __init__(self, click_name, scale_sparse_grad=True): @@ -1165,9 +1264,11 @@ class DownpourOptimizer(DistributedOptimizer): run distributed training. The optimized information will be stored in Fleet() instance who holds the global information about current distributed training. + Args: optimizer(Optimizer): subclass of Optimizer. strategy(any): config for DownpourOptimizer. + Returns: None """ @@ -1270,22 +1371,24 @@ def minimize( program_mode="all_reduce", ): """ - minimize a program through loss, loss can be a list in DistributedOptimizer. + Minimize a program through loss, loss can be a list in DistributedOptimizer. Note that in parameter server mode, a worker will not get anything about optimize_os Because optimizer algorithms run on pserver side. We will make this usable in pserver process, but currently the optimization part is written into Fleet(). A user does not need to care about how to startup a pserver node. + Args: - losses (Variable|Variable List): loss variable or loss variable list to run optimization. - scopes (Scope| Scope List): scope instance. - startup_programs (Program|Program List): startup_program for initializing parameters - in `parameter_list`. - parameter_list (list): list of Variables to update. - no_grad_set (set|None): set of Variables should be ignored. - program_mode (str|"all_reduce"): grad action for grogram when use_ps_gpu. + losses (Variable|Variable List): Loss variable or loss variable list to run optimization. + scopes (Scope|Scope List, Optional): Scope instance. Default is None. + startup_programs (Program|Program List, Optional): Startup_program for initializing parameters + in `parameter_list`. Default is None. + parameter_list (list, Optional): List of Variables to update. Default is None. + no_grad_set (set, Optional): Set of Variables should be ignored. Default is None. + program_mode (str, Optional): Grad action for grogram when use_ps_gpu. Default is "all_reduce". + Returns: tuple: (optimize_ops, params_grads) which are, list of operators appended; - and list of (param, grad) Variables pair for optimization. + and list of (param, grad) Variables pair for optimization. """ if not isinstance(losses, list): diff --git a/python/paddle/incubate/distributed/models/moe/grad_clip.py b/python/paddle/incubate/distributed/models/moe/grad_clip.py index 59ba6bb8f9454..2a0dd89b77ff9 100644 --- a/python/paddle/incubate/distributed/models/moe/grad_clip.py +++ b/python/paddle/incubate/distributed/models/moe/grad_clip.py @@ -22,7 +22,7 @@ class ClipGradForMOEByGlobalNorm(ClipGradBase): r""" - The Algrithm is the same as paddle.nn.ClipGradByGlobalNorm + The Algorithm is the same as paddle.nn.ClipGradByGlobalNorm Given a list of Tensor :math:`t\_list` , calculate the global norm for the elements of all tensors in :math:`t\_list` , and limit it to ``clip_norm`` . @@ -50,7 +50,7 @@ class ClipGradForMOEByGlobalNorm(ClipGradBase): Note: ``need_clip`` of ``ClipGradyGlobalNorm`` HAS BEEN DEPRECATED since 2.0. - Please use ``need_clip`` in ``ParamAttr`` to speficiy the clip scope. + Please use ``need_clip`` in ``ParamAttr`` to specify the clip scope. Reference: https://github.com/laekov/fastmoe/blob/master/examples/megatron/clip-grad-v2.2.patch @@ -64,22 +64,22 @@ class ClipGradForMOEByGlobalNorm(ClipGradBase): group_name (str, optional): The group name for this clip. Default value is ``default_moe_group``. Examples: + .. code-block:: python - import paddle + >>> import paddle - x = paddle.uniform([10, 10], min=-1.0, max=1.0, dtype='float32') - linear = paddle.nn.Linear(in_features=10, out_features=10, - weight_attr=paddle.ParamAttr(need_clip=True), - bias_attr=paddle.ParamAttr(need_clip=False)) - out = linear(x) - loss = paddle.mean(out) - loss.backward() + >>> x = paddle.uniform([10, 10], min=-1.0, max=1.0, dtype='float32') + >>> linear = paddle.nn.Linear(in_features=10, out_features=10, + ... weight_attr=paddle.ParamAttr(need_clip=True), + ... bias_attr=paddle.ParamAttr(need_clip=False)) + >>> out = linear(x) + >>> loss = paddle.mean(out) + >>> loss.backward() - is_expert_func = lambda param: "expert_" in param.name - clip = paddle.nn.ClipGradForMOEByGlobalNorm(clip_norm=1.0,is_expert_func, None) - sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters(), grad_clip=clip) - sdg.step() + >>> clip = paddle.nn.ClipGradByGlobalNorm(clip_norm=1.0) # Cause paddle.nn hasn't this interface, so we use ClipGradByGlobalNorm here. + >>> sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters(), grad_clip=clip) + >>> sdg.step() """ def __init__( @@ -124,7 +124,7 @@ def get_l2_norm_pow(params_grads, sum_dtype=None): else: sum_square_list.append(sum_square) - # all parameters have been filterd out + # all parameters have been filtered out if ( len(sum_square_list) + len(sum_square_list_fp16) diff --git a/python/paddle/incubate/distributed/models/moe/moe_layer.py b/python/paddle/incubate/distributed/models/moe/moe_layer.py index 7c63c431bb950..023dd8fa24764 100644 --- a/python/paddle/incubate/distributed/models/moe/moe_layer.py +++ b/python/paddle/incubate/distributed/models/moe/moe_layer.py @@ -263,63 +263,68 @@ def prepare_forward(gate, num_expert, world_size, moe_group): class MoELayer(nn.Layer): """MoE Layer Args: - d_model: (int) model dimention - experts: (nn.LayerList) expert networks list - gate: (dict|NaiveGate|SwitchGate|NaiveGate): - if gate is a dict: - gate is a gate network config, containing 2 keys: - `type`(str) value can be: "naive", "gshard", "switch" or None, default is "gshard" - `top_k`(int) default value is 2 - else gate is an instance of NaiveGate|SwitchGate|NaiveGate: - - moe_group: moe group for experts communication - mp_group: mp group for mp commutication - recompute_interval(int, optional): whether to use recompute, default 0, means to disable recompute. - recompute_ctx(dict, optional): the context for recompute, if recompute_interval > 1, recompute_ctx must be given. + d_model (int): Model dimention. + experts (nn.LayerList): Expert networks list. + gate (dict|NaiveGate|SwitchGate|NaiveGate): + + - If gate is a dict: + gate is a gate network config, containing 2 keys: + `type` (str) value can be: "naive", "gshard", "switch" or None, default is "gshard". + `top_k` (int) Default value is 2. + else gate is an instance of NaiveGate|SwitchGate|NaiveGate: + + moe_group: moe group for experts communication. + mp_group: mp group for mp communication. + recompute_interval (int, optional): Whether to use recompute, default 0, means to disable recompute. + recompute_ctx (dict, optional): The context for recompute, if recompute_interval > 1, recompute_ctx must be given. + Examples: + .. code-block:: python - from paddle.nn import layer, LayerList - from paddle.distributed.moe import MoElayer - from paddle.distributed.collective import Group - from paddle.distributed import fleet - - moe_group = Group(fleet.worker_index(), - 0, - list(range(fleet.worker_num()))) - mp_group = None - - num_experts=8 - dim_feedforward=512 - d_model=8 - top_k=2 - - class ExpertLayer(Layer): - def __init__(self, d_model, d_hidden, name=None,rank=0, windex = 0, num_expert=1): - super().__init__() - self.htoh4 = nn.Linear(d_model, d_hidden) - self.h4toh = nn.Linear(d_hidden, d_model) - - def forward(self, x): - x = self.htoh4(x) - x = self.h4toh(x) - return x - gate_config = { - "type": "gshard", - "top_k": top_k, - } - - experts_list = LayerList() - for expi in range(num_experts): - exp_layer = ExpertLayer(d_model, dim_feedforward // top_k, windex=expi, num_expert=num_experts) - experts_list.append(exp_layer) - - moeLayer = MoELayer(d_model = d_model, - experts=experts_list, - gate=gate_config, - moe_group=moe_group, - mp_group=mp_group, - recompute_interval=0) + >>> # doctest: +SKIP('Until Distributed move successfully, just skip it') + >>> from paddle.nn import layer, LayerList + >>> from paddle.distributed.moe import MoElayer + >>> from paddle.distributed.collective import Group + >>> from paddle.distributed import fleet + + >>> moe_group = Group(fleet.worker_index(), + ... 0, + ... list(range(fleet.worker_num()))) + >>> mp_group = None + + >>> num_experts=8 + >>> dim_feedforward=512 + >>> d_model=8 + >>> top_k=2 + + >>> class ExpertLayer(Layer): + ... def __init__(self, d_model, d_hidden, name=None,rank=0, windex = 0, num_expert=1): + ... super().__init__() + ... self.htoh4 = nn.Linear(d_model, d_hidden) + ... self.h4toh = nn.Linear(d_hidden, d_model) + + ... def forward(self, x): + ... x = self.htoh4(x) + ... x = self.h4toh(x) + ... return x + + >>> gate_config = { + ... "type": "gshard", + ... "top_k": top_k, + ... } + + >>> experts_list = LayerList() + >>> for expi in range(num_experts): + ... exp_layer = ExpertLayer(d_model, dim_feedforward // top_k, windex=expi, num_expert=num_experts) + ... experts_list.append(exp_layer) + + >>> moeLayer = MoELayer(d_model = d_model, + ... experts=experts_list, + ... gate=gate_config, + ... moe_group=moe_group, + ... mp_group=mp_group, + ... recompute_interval=0) """ diff --git a/python/paddle/incubate/layers/nn.py b/python/paddle/incubate/layers/nn.py index f1b2c8fbff246..1777aaa9a4b24 100644 --- a/python/paddle/incubate/layers/nn.py +++ b/python/paddle/incubate/layers/nn.py @@ -54,37 +54,40 @@ def fused_embedding_seq_pool( size (tuple|list): The shape of the lookup_table parameter. It should have two elements which indicate the size of the dictionary of embedding and the size of each embedding vector respectively. - is_sparse (bool): The flag indicating whether to use sparse update. + is_sparse (bool, optional): The flag indicating whether to use sparse update. Default: False. - padding_idx (int|long|None): It will output all-zero padding data whenever + padding_idx (int|long|None, optional): It will output all-zero padding data whenever lookup encounters :math:`padding\_idx` in Ids. If set :attr:`None`, it makes no effect to output. If :math:`padding\_idx < 0`, the :math:`padding\_idx` will automatically be converted to :math:`size[0] + padding\_idx` to use. Default: None. - combiner (str): The pooling type of sequence_pool, and only support `sum`. + combiner (str, optional): The pooling type of sequence_pool, and only support `sum`. Default: sum. - param_attr (ParamAttr): Parameters for this layer. - dtype (np.dtype|core.VarDesc.VarType|str): The dtype refers to the data type of output - tensor. It can be float32, float_16, int etc. + param_attr (ParamAttr, optional): Parameters for this layer. Default: None. + dtype (np.dtype|core.VarDesc.VarType|str, optional): The dtype refers to the data type of output + tensor. It can be float32, float_16, int etc. Default: float32. + Returns: The Tensor of sequence pooling. + Examples: .. code-block:: python - import numpy as np - import paddle.fluid as fluid - import paddle - paddle.enable_static() - - dict_size = 20 - data_t = paddle.static.data( - name='word', shape=[-1, 1], dtype='int64', lod_level=1) - padding_idx = np.random.randint(1, 10) - out = paddle.incubate.layers.fused_embedding_seq_pool( - input=data_t, - size=[dict_size, 32], - param_attr='w', - padding_idx=padding_idx, - is_sparse=False) + + >>> import numpy as np + >>> import paddle + >>> paddle.enable_static() + + >>> dict_size = 20 + >>> data_t = paddle.static.data( + ... name='word', shape=[-1, 1], dtype='int64', lod_level=1) + >>> padding_idx = np.random.randint(1, 10) + >>> out = paddle.incubate.layers.fused_embedding_seq_pool( + ... input=data_t, + ... size=[dict_size, 32], + ... param_attr='w', + ... padding_idx=padding_idx, + ... is_sparse=False) + """ helper = LayerHelper('fused_embedding_seq_pool', **locals()) w = helper.create_parameter( @@ -130,27 +133,25 @@ def fused_seqpool_cvm( cvm_offset(int, optional): cvm offset. Default: 2, which means cvm contains show, click. Returns: - Tensor : The tensor storing sequence pool and cvm - of input. + Tensor : The tensor storing sequence pool and cvm of input. Examples: .. code-block:: python - import paddle - import paddle.fluid as fluid - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - data = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=1) - data2 = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=1) - inputs = [data, data2] - embs = paddle.incubate.layers.nn._pull_box_sparse(input=inputs, size=11, is_distributed=True, is_sparse=True) + >>> data = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=1) + >>> data2 = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=1) + >>> inputs = [data, data2] + >>> embs = paddle.incubate.layers.nn._pull_box_sparse(input=inputs, size=11, is_distributed=True, is_sparse=True) - label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64", lod_level=1) - ones = fluid.layers.fill_constant_batch_size_like(input=label, shape=[-1, 1], dtype="int64", value=1) - show_clk = paddle.cast(paddle.concat([ones, label], axis=1), dtype='float32') - show_clk.stop_gradient = True + >>> label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64", lod_level=1) + >>> ones = paddle.static.data(name="ones", shape=[-1, 1], dtype="int64", lod_level=1) + >>> show_clk = paddle.cast(paddle.concat([ones, label], axis=1), dtype='float32') + >>> show_clk.stop_gradient = True - cvms = paddle.incubate.layers.fused_seqpool_cvm(embs, 'sum', show_clk) + >>> cvms = paddle.incubate.layers.fused_seqpool_cvm(embs, 'sum', show_clk) """ @@ -212,10 +213,10 @@ def multiclass_nms2( In the NMS step, this operator greedily selects a subset of detection bounding boxes that have high scores larger than score_threshold, if providing this threshold, then selects the largest nms_top_k confidences scores if nms_top_k - is larger than -1. Then this operator pruns away boxes that have high IOU + is larger than -1. Then this operator prunes away boxes that have high IOU (intersection over union) overlap with already selected boxes by adaptive threshold NMS based on parameters of nms_threshold and nms_eta. - Aftern NMS step, at most keep_top_k number of total bboxes are to be kept + After NMS step, at most keep_top_k number of total bboxes are to be kept per image if keep_top_k is larger than -1. Args: @@ -228,7 +229,7 @@ def multiclass_nms2( [xmin, ymin, xmax, ymax], when box size equals to 4. 2. (LoDTensor) A 3-D Tensor with shape [M, C, 4] M is the number of bounding boxes, C is the - class number + class number. scores (Tensor): Two types of scores are supported: 1. (Tensor) A 3-D Tensor with shape [N, C, M] represents the predicted confidence predictions. @@ -241,22 +242,22 @@ class number M is the number of bbox, C is the class number. In this case, input BBoxes should be the second case with shape [M, C, 4]. - background_label (int): The index of background label, the background - label will be ignored. If set to -1, then all - categories will be considered. Default: 0 score_threshold (float): Threshold to filter out bounding boxes with low confidence score. If not provided, consider all boxes. nms_top_k (int): Maximum number of detections to be kept according to the confidences after the filtering detections based on score_threshold. - nms_threshold (float): The threshold to be used in NMS. Default: 0.3 - nms_eta (float): The threshold to be used in NMS. Default: 1.0 keep_top_k (int): Number of total bboxes to be kept per image after NMS step. -1 means keeping all bboxes after NMS step. - normalized (bool): Whether detections are normalized. Default: True - return_index(bool): Whether return selected index. Default: False - name(str): Name of the multiclass nms op. Default: None. + nms_threshold (float, optional): The threshold to be used in NMS. Default: 0.3. + normalized (bool, optional): Whether detections are normalized. Default: True. + nms_eta (float, optional): The threshold to be used in NMS. Default: 1.0. + background_label (int, optional): The index of background label, the background + label will be ignored. If set to -1, then all + categories will be considered. Default: 0. + return_index(bool, optional): Whether return selected index. Default: False. + name(str, optional): Name of the multiclass nms op. Default: None. Returns: A tuple with two dimensions of the tensor: (Out, Index) if return_index is True, @@ -279,23 +280,21 @@ class number Examples: .. code-block:: python - - import paddle.fluid as fluid - import paddle - paddle.enable_static() - boxes = paddle.static.data(name='bboxes', shape=[-1, 81, 4], - dtype='float32', lod_level=1) - scores = paddle.static.data(name='scores', shape=[-1, 81], - dtype='float32', lod_level=1) - out, index = paddle.incubate.layers.multiclass_nms2(bboxes=boxes, - scores=scores, - background_label=0, - score_threshold=0.5, - nms_top_k=400, - nms_threshold=0.3, - keep_top_k=200, - normalized=False, - return_index=True) + >>> import paddle + >>> paddle.enable_static() + >>> boxes = paddle.static.data(name='bboxes', shape=[-1, 81, 4], + ... dtype='float32', lod_level=1) + >>> scores = paddle.static.data(name='scores', shape=[-1, 81], + ... dtype='float32', lod_level=1) + >>> out, index = paddle.incubate.layers.multiclass_nms2(bboxes=boxes, + ... scores=scores, + ... background_label=0, + ... score_threshold=0.5, + ... nms_top_k=400, + ... nms_threshold=0.3, + ... keep_top_k=200, + ... normalized=False, + ... return_index=True) """ helper = LayerHelper('multiclass_nms2', **locals()) @@ -353,26 +352,27 @@ def search_pyramid_hash( pyramid_layer (int): The number of pyramid layers. It should be greater than 2. rand_len (int): The minimum length of pyramid hash cell. drop_out_percent (float): The probability of dropping out the input token randomly. - It should satisfy: [0., 1.] + It should satisfy: [0., 1.]. is_training (bool): Whether in training or testing phrase. - use_filter(bool): If set True, the white filter and black filter should be given by + use_filter (bool): If set True, the white filter and black filter should be given by :attr:`param_attr_wl` and :attr:`param_attr_bl` . - white_list_len(int): If set :math:`white_list_len>0` , white filter with shape [white_list_len, 1] + white_list_len (int): If set :math:`white_list_len>0` , white filter with shape [white_list_len, 1] should be provided by param_attr_wl. - black_list_len(int): If set :math:`black_list_len>0` , black filter with shape [black_list_len, 1] + black_list_len (int): If set :math:`black_list_len>0` , black filter with shape [black_list_len, 1] should be provided by param_attr_bl. - seed(int): The number of random seed. - lr(float): The learning rate of weight created by :attr:`param_attr` with shape [space_len+rand_len, 1] + seed (int): The number of random seed. + lr (float): The learning rate of weight created by :attr:`param_attr` with shape [space_len+rand_len, 1] in this layer. - param_attr(ParamAttr): To specify the weight parameter property. Default: None, which means the + param_attr (ParamAttr, optional): To specify the weight parameter property. Default: None, which means the default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr` . - param_attr_wl(ParamAttr): Specified parameters of white filter. - param_attr_bl(ParamAttr): Specified parameters of black filter. - distribute_update_vars(list[ParamAttr.name]): Decided which params should be updated in distribute training. - Used in Distribute Transpiler to create a trainer/server program. - name(str, optional): The default value is None. Normally there is no need for user to set this property. - For more information, please refer to :ref:`api_guide_Name` . - dtype(str): The data type of output Tensor, float32. + param_attr_wl (ParamAttr, optional): Specified parameters of white filter. Default: None. + param_attr_bl (ParamAttr, optional): Specified parameters of black filter. Default: None. + distribute_update_vars(list[ParamAttr.name], optional): Decided which params should be updated in distribute training. + Used in Distribute Transpiler to create a trainer/server program. Default: None. + name (str, optional): The default value is None. Normally there is no need for user to set this property. + For more information, please refer to :ref:`api_guide_Name` . Default: None. + dtype (str, optional): The data type of output Tensor, float32. Default: float32. + Returns: Tensor: LoDTensor of pyramid hash embedding. """ @@ -451,25 +451,25 @@ def shuffle_batch(x, seed=None): :attr:`x` is a LoDTensor to be shuffled with shape :math:`[N_1, N_2, ..., N_k, D]` . Note that the last dim of input will not be shuffled. :math:`N_1 * N_2 * ... * N_k` numbers of elements with length :math:`D` will be shuffled randomly. - For Example: + Examples: - .. code-block:: text + .. code-block:: text - Input: - x.data = [[1, 2], [3, 4], [5, 6], [7, 8]] - x.dims = [4, 2] + Input: + x.data = [[1, 2], [3, 4], [5, 6], [7, 8]] + x.dims = [4, 2] - Attrs: - seed = 2019 + Attrs: + seed = 2019 - Output: - Out.data =[[7, 8], [1, 2], [3, 4], [5, 6]] - Out.dims = [4, 2] + Output: + Out.data =[[7, 8], [1, 2], [3, 4], [5, 6]] + Out.dims = [4, 2] Args: x (Tensor): The input Tensor. The input Tensor is a N-D LoDTensor with type int, float32 or float64. - seed (None|int|Tensor): The start up seed. If set, seed will be set as the start up seed of shuffle engine. - If not set(Default), start up seed of shuffle engine will be generated randomly. + seed (None|int|Tensor, optional): The start up seed. If set, seed will be set as the start up seed of shuffle engine. + If not set(Default), start up seed of shuffle engine will be generated randomly. Default: None. Returns: Tensor: The shuffled LoDTensor with the same shape and lod as input. @@ -478,11 +478,10 @@ def shuffle_batch(x, seed=None): .. code-block:: python - import paddle.fluid as fluid - import paddle - paddle.enable_static() - x = paddle.static.data(name="x", shape=[-1, 4]) - out = paddle.incubate.layers.shuffle_batch(x) + >>> import paddle + >>> paddle.enable_static() + >>> x = paddle.static.data(name="x", shape=[-1, 4]) + >>> out = paddle.incubate.layers.shuffle_batch(x) """ helper = LayerHelper('shuffle_batch', **locals()) @@ -526,7 +525,7 @@ def partial_concat(input, start_index=0, length=-1): [9, 10, 11]] output = partial_concat([x, y], start_index=0, length=2) - we get: + We get: output = [[0, 1, 6, 7], [3, 4, 9, 10]] @@ -534,20 +533,22 @@ def partial_concat(input, start_index=0, length=-1): Args: input(list): List of input Tensors with data type float32, float64, int32, int64. - start_index(int32): The start index of each instance for partial concatenation. + start_index(int32, optional): The start index of each instance for partial concatenation. Default is 0. - length(int32): The length of each instance for partial concatenation. Default is -1. + length(int32, optional): The length of each instance for partial concatenation. Default is -1. Negative values for all elements after start_index. + Returns: Tensor: A Tensor with the same data type as input's. + Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle - x = paddle.randn(name="x", shape=[1,3], dtype="float32") - y = paddle.randn(name="y", shape=[1,3], dtype="float32") - concat = paddle.incubate.layers.partial_concat( - [x, y], start_index=0, length=2) + + >>> import paddle + >>> x = paddle.randn(name="x", shape=[1,3], dtype="float32") + >>> y = paddle.randn(name="y", shape=[1,3], dtype="float32") + >>> concat = paddle.incubate.layers.partial_concat( + ... [x, y], start_index=0, length=2) """ if not isinstance(input, list): warnings.warn( @@ -584,6 +585,7 @@ def partial_sum(input, start_index=0, length=-1): This Op exists in incubate layers, which means that it is not shown to the public. Only 2-D Tensor or LodTensor input is supported. Slice and concat can only be performed along the second dimension. + .. code-block:: text Given: @@ -592,30 +594,29 @@ def partial_sum(input, start_index=0, length=-1): y = [[6, 7 ,8], [9, 10, 11]] output = partial_sum([x, y], start_index=0, length=2) - we get: + + We get: output = [[6, 8], [12, 14]] Args: - input(list): List of input Tensors with data type float32, float64, int32, + input (list): List of input Tensors with data type float32, float64, int32, int64. + start_index (int32, optional): The start index of each instance for partial sum. Default is 0. + length (int32, optional): The length of each instance for partial sum. Default is -1. + Returns: Tensor: A Tensor with the same data type as input's. + Examples: .. code-block:: python - import paddle.fluid as fluid - import numpy as np - import paddle - paddle.enable_static() - - x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") - y = paddle.static.data(name="y", shape=[2, 3], dtype="float32") - sum = paddle.incubate.layers.partial_sum([x,y], start_index=0, length=2) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - xx = np.array([1,2,3,4,5,6]).reshape((2,3)).astype("float32") - yy = np.array([6,5,4,4,5,6]).reshape((2,3)).astype("float32") - out = exe.run(feed={"x":xx, "y":yy}, fetch_list=[sum]) + + >>> import paddle + >>> paddle.enable_static() + + >>> x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") + >>> y = paddle.static.data(name="y", shape=[2, 3], dtype="float32") + >>> sum = paddle.incubate.layers.partial_sum([x,y], start_index=0, length=2) """ for id, x in enumerate(input): check_variable_and_dtype( @@ -642,6 +643,7 @@ def tdm_child(x, node_nums, child_nums, param_attr=None, dtype='int32'): **Tdm Child** According to the input node_id on the given tree, return the corresponding child node_id and whether child is a leaf node by leaf_mask value. + .. code-block:: text Given: @@ -650,25 +652,26 @@ def tdm_child(x, node_nums, child_nums, param_attr=None, dtype='int32'): node_nums = 7 child_nums = 2 - we get: + We get: child = [[5, 6], [0, 0]] leaf_mask = [[1, 1], [0, 0]] + Args: - x(Tensor): Tensor contained the node_id information, dtype support int32/int64. - node_nums(int): Number of total nodes. - child_nums(int): Maximum number of child nodes per node. - param_attr(ParamAttr): To specify the tdm-tree-info parameter property. Default: None, which means the + x (Tensor): Tensor contained the node_id information, dtype support int32/int64. + node_nums (int): Number of total nodes. + child_nums (int): Maximum number of child nodes per node. + param_attr (ParamAttr, optional): To specify the tdm-tree-info parameter property. Default: None, which means the default weight parameter property is used. See usage for details in: ref: `api_fluid_ParamAttr`, should - has shape(node_nums, 3 + child_nums), dtype support int32/int64. + has shape (node_nums, 3 + child_nums), dtype support int32/int64. The dimension[1] of tdm-tree-info contains the following: - 1. Item_id(int, shape(1)), if node is a leaf node, give its item_id corresponding to node_id, else give 0. - 2. Layer_id(int, shape(1)), indicates which layer the node is on. - 3. Parent_id(int, shape(1)), node's parent node. - 4. Child_id(int, shape(child_nums)), all child node's node_id of this node should be given. - If the number of child nodes is insufficient, padding 0 until child nums equal to child_nums - dtype(str): The data type of output child and leaf_mask, support int32/int64. + 1. Item_id (int, shape(1)), if node is a leaf node, give its item_id corresponding to node_id, else give 0. + 2. Layer_id (int, shape(1)), indicates which layer the node is on. + 3. Parent_id (int, shape(1)), node's parent node. + 4. Child_id (int, shape(child_nums)), all child node's node_id of this node should be given. + If the number of child nodes is insufficient, padding 0 until child nums equal to child_nums. + dtype (str, optional): The data type of output child and leaf_mask, support int32/int64. Default: int32. Returns: tuple: A tuple including input node's child(Tensor) and leaf_mask(Tensor). @@ -676,27 +679,23 @@ def tdm_child(x, node_nums, child_nums, param_attr=None, dtype='int32'): Examples: .. code-block:: python - import paddle - import paddle.fluid as fluid - import numpy as np - paddle.enable_static() - x = paddle.static.data(name="x", shape=[None, 1], dtype="int32", lod_level=1) - tree_info = [[0,0,0,1,2], - [0,1,0,3,4],[0,1,0,5,6], - [0,2,1,0,0],[1,2,1,0,0],[2,2,2,0,0],[3,2,2,0,0]] - tree_info_np = np.array(tree_info) - tree_info_np = np.reshape(tree_info_np, (7,5)) - node_nums = 7 - child_nums = 2 - child, leaf_mask = paddle.incubate.layers.tdm_child(x, node_nums, child_nums, - param_attr=fluid.ParamAttr( - initializer=paddle.nn.initializer.Assign( - tree_info_np))) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - xx = np.array([[2],[3]]).reshape((2,1)).astype("int32") - child_res, leaf_mask_res = exe.run(feed={"x":xx}, fetch_list=[child, leaf_mask]) + + >>> import paddle + >>> import numpy as np + >>> paddle.enable_static() + + >>> x = paddle.static.data(name="x", shape=[None, 1], dtype="int32", lod_level=1) + >>> tree_info = [[0,0,0,1,2], + ... [0,1,0,3,4],[0,1,0,5,6], + ... [0,2,1,0,0],[1,2,1,0,0],[2,2,2,0,0],[3,2,2,0,0]] + >>> tree_info_np = np.array(tree_info) + >>> tree_info_np = np.reshape(tree_info_np, (7,5)) + >>> node_nums = 7 + >>> child_nums = 2 + >>> child, leaf_mask = paddle.incubate.layers.tdm_child(x, node_nums, child_nums, + ... param_attr=paddle.ParamAttr( + ... initializer=paddle.nn.initializer.Assign(tree_info_np))) + """ helper = LayerHelper("tdm_child", **locals()) check_dtype( @@ -740,6 +739,7 @@ def tdm_sampler( """ **Tdm Sampler** According to the input positive samples at leaf node(x), do negative sampling layer by layer on the given tree. + .. code-block:: text Given: @@ -753,7 +753,7 @@ def tdm_sampler( leaf_node_num = 4 output_list = False - we get: + We get: out = [[1, 3], [1, 4], [2, 5], [2, 6]] labels = [[1, 1], [1, 1], [1, 1], [1, 1]] mask = [[1, 1], [1, 1], [1, 1], [1, 1]] @@ -763,21 +763,21 @@ def tdm_sampler( neg_samples_num_list (list(int)): Number of negative samples per layer. layer_node_num_list (list(int)): Number of nodes per layer, must has same shape with neg_samples_num_list. leaf_node_num (int): Number of leaf nodes. - tree_travel_attr (ParamAttr): To specify the tdm-travel parameter property. Default: None, which means the + tree_travel_attr (ParamAttr, optional): To specify the tdm-travel parameter property. Default: None, which means the default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr`, should has shape (leaf_node_num, len(layer_node_num_list)), dtype support int32/int64. - tree_layer_attr (ParamAttr): To specify the tdm-layer parameter property. Default: None, which means the + tree_layer_attr (ParamAttr, optional): To specify the tdm-layer parameter property. Default: None, which means the default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr`, should has shape (node_num, 1), dtype support int32/int64. - output_positive (bool): Whether to output positive samples (includ label and mask )at the same time. - output_list (bool): Whether to divide the output into layers and organize it into list format. - seed (int): The number of random seed. - tree_dtype(np.dtype|core.VarDesc.VarType|str): The dtype of tdm-travel and tdm-layer, support int32/int64 - dtype(np.dtype|core.VarDesc.VarType|str): The dtype of output(sampling results, labels and masks) + output_positive (bool, optional): Whether to output positive samples (include label and mask )at the same time. Default: True. + output_list (bool, optional): Whether to divide the output into layers and organize it into list format. Default: True. + seed (int, optional): The number of random seed. Default: 0. + tree_dtype (np.dtype|core.VarDesc.VarType|str, optional): The dtype of tdm-travel and tdm-layer, support int32/int64. Default: int32. + dtype (np.dtype|core.VarDesc.VarType|str, optional): The dtype of output(sampling results, labels and masks). Default: int32. Returns: tuple: A tuple including sampling results, corresponding labels and masks. if output_positive = True, sampling - result will include both positive and negative samples. If sampling reseult is a positive sample, the label is 1, + result will include both positive and negative samples. If sampling result is a positive sample, the label is 1, and if it is a negative sample, it is 0. If the tree is unbalanced, in order to ensure the consistency of the sampling result shape, the padding sample's mask = 0, the real sample's mask value = 1. If output_list = True, the result will organize into list format specified by layer information. @@ -785,43 +785,37 @@ def tdm_sampler( Examples: .. code-block:: python - import paddle - import paddle.fluid as fluid - import numpy as np - paddle.enable_static() - x = paddle.static.data(name="x", shape=[None, 1], dtype="int32", lod_level=1) - travel_list = [[1, 3], [1, 4], [2, 5], [2, 6]] # leaf node's travel path, shape(leaf_node_num, layer_num) - layer_list_flat = [[1], [2], [3], [4], [5], [6]] # shape(node_nums, 1) - - neg_samples_num_list = [0, 0] # negative sample nums = 0 - layer_node_num_list = [2, 4] #two layer (exclude root node) - leaf_node_num = 4 - - travel_array = np.array(travel_list) - layer_array = np.array(layer_list_flat) - - sample, label, mask = paddle.incubate.layers.tdm_sampler( - x, - neg_samples_num_list, - layer_node_num_list, - leaf_node_num, - tree_travel_attr=fluid.ParamAttr( - initializer=paddle.nn.initializer.Assign( - travel_array)), - tree_layer_attr=fluid.ParamAttr( - initializer=paddle.nn.initializer.Assign( - layer_array)), - output_positive=True, - output_list=True, - seed=0, - tree_dtype='int32') - - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - xx = np.array([[0],[1]]).reshape((2,1)).astype("int32") - - exe.run(feed={"x":xx}) + + >>> import paddle + >>> import numpy as np + >>> paddle.enable_static() + + >>> x = paddle.static.data(name="x", shape=[None, 1], dtype="int32", lod_level=1) + >>> travel_list = [[1, 3], [1, 4], [2, 5], [2, 6]] # leaf node's travel path, shape(leaf_node_num, layer_num) + >>> layer_list_flat = [[1], [2], [3], [4], [5], [6]] # shape(node_nums, 1) + + >>> neg_samples_num_list = [0, 0] # negative sample nums = 0 + >>> layer_node_num_list = [2, 4] #two layer (exclude root node) + >>> leaf_node_num = 4 + + >>> travel_array = np.array(travel_list) + >>> layer_array = np.array(layer_list_flat) + + >>> sample, label, mask = paddle.incubate.layers.tdm_sampler( + ... x, + ... neg_samples_num_list, + ... layer_node_num_list, + ... leaf_node_num, + ... tree_travel_attr=paddle.ParamAttr( + ... initializer=paddle.nn.initializer.Assign( + ... travel_array)), + ... tree_layer_attr=paddle.ParamAttr( + ... initializer=paddle.nn.initializer.Assign( + ... layer_array)), + ... output_positive=True, + ... output_list=True, + ... seed=0, + ... tree_dtype='int32') """ helper = LayerHelper("tdm_sampler", **locals()) @@ -968,30 +962,33 @@ def rank_attention( rank_param gives the organization of data. Notice: It currently supports GPU device. This Op exists in incubate layers, which means that it is not shown to the public. + Args: - input: Tensor with data type float32, float64. - rank_offset: Tensor with data type int32. - rank_para_shape: The shape of rank_param. - rank_param_attr: Attribute initializer of rank_param. - max_rank: The max rank of input's ranks. + input (Tensor): Tensor with data type float32, float64. + rank_offset (Tensor): Tensor with data type int32. + rank_para_shape (list[int]): The shape of rank_param. + rank_param_attr (ParamAttr): Attribute initializer of rank_param. + max_rank (int, optional): The max rank of input's ranks. Default is 3. + max_size (int, optional): The max size of input's ranks. Default is 0. Returns: Tensor: A Tensor with the same data type as input's. + Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle - paddle.enable_static() - - input = paddle.static.data(name="input", shape=[None, 2], dtype="float32") - rank_offset = paddle.static.data(name="rank_offset", shape=[None, 7], dtype="int32") - out = paddle.incubate.layers.rank_attention(input=input, - rank_offset=rank_offset, - rank_param_shape=[18,3], - rank_param_attr= - paddle.ParamAttr(learning_rate=1.0, - name="ubm_rank_param.w_0"), - max_rank=3, - max_size=0) + + >>> import paddle + >>> paddle.enable_static() + + >>> input = paddle.static.data(name="input", shape=[None, 2], dtype="float32") + >>> rank_offset = paddle.static.data(name="rank_offset", shape=[None, 7], dtype="int32") + >>> out = paddle.incubate.layers.rank_attention(input=input, + ... rank_offset=rank_offset, + ... rank_param_shape=[18,3], + ... rank_param_attr= + ... paddle.ParamAttr(learning_rate=1.0, + ... name="ubm_rank_param.w_0"), + ... max_rank=3, + ... max_size=0) """ helper = LayerHelper('rank_attention', **locals()) dtype = helper.input_dtype(input_param_name='input') @@ -1027,34 +1024,35 @@ def batch_fc(input, param_size, param_attr, bias_size, bias_attr, act=None): except that the bias and relu activation layers are added. Notice: It currently supports GPU device. This Op exists in incubate layers, which means that it is not shown to the public. + Args: - input: Tensor with data type float32, float64. - param_size: The size of w. - param_attr: Attribute initializer of w. - bias_size: The size of bias. - bias_attr: Attribute initializer of bias. - act: Activation to be applied to the output of this layer. + input (Tensor): Tensor with data type float32, float64. + param_size (list[int]): The size of w. + param_attr (ParamAttr): Attribute initializer of w. + bias_size (list[int]): The size of bias. + bias_attr (ParamAttr): Attribute initializer of bias. + act (str, optional): Activation to be applied to the output of this layer. Default is None. Returns: Tensor: A Tensor with the same data type as input's. + Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle - - paddle.enable_static() - - input = paddle.static.data(name="input", shape=[16, 2, 3], dtype="float32") - out = paddle.incubate.layers.batch_fc(input=input, - param_size=[16, 3, 10], - param_attr= - paddle.ParamAttr(learning_rate=1.0, - name="w_0"), - bias_size=[16, 10], - bias_attr= - paddle.ParamAttr(learning_rate=1.0, - name="b_0"), - act="relu") + + >>> import paddle + >>> paddle.enable_static() + + >>> input = paddle.static.data(name="input", shape=[16, 2, 3], dtype="float32") + >>> out = paddle.incubate.layers.batch_fc(input=input, + ... param_size=[16, 3, 10], + ... param_attr= + ... paddle.ParamAttr(learning_rate=1.0, + ... name="w_0"), + ... bias_size=[16, 10], + ... bias_attr= + ... paddle.ParamAttr(learning_rate=1.0, + ... name="b_0"), + ... act="relu") """ helper = LayerHelper("batch_fc", **locals()) @@ -1089,23 +1087,26 @@ def _pull_box_extended_sparse(input, size, extend_size=64, dtype='float32'): This layer is used to lookup embeddings of IDs, provided by :attr:`input`, in BoxPS lookup table. The result of this lookup is the embedding of each ID in the :attr:`input`. + Args: - input(Tensor): Input is a Tensor, which - contains the IDs information. - size(int): The embedding size parameter, which indicates the size of + input (Tensor): Input is a Tensor, which contains the IDs information. + size (int): The embedding size parameter, which indicates the size of each embedding vector respectively. - extend_size(int): The embedding size parameter in extended dim, - which indicates the size of each embedding vector respectively. - dtype(str): The dtype refers to the data type of output tensor. Only supports - float32 now. + extend_size (int, optional): The embedding size parameter in extended dim, + which indicates the size of each embedding vector respectively. Default is 64. + dtype (str, optional): The dtype refers to the data type of output tensor. Only supports float32 now. Default is float32. + Returns: - Tensor: The tensor storing the embeddings of the \ - supplied inputs. + Tensor: The tensor storing the embeddings of the supplied inputs. + Examples: .. code-block:: python - import paddle.fluid as fluid - data = paddle.static.data(name='sequence', shape=[-1, 1], dtype='int64', lod_level=1) - emb, emb_ex = paddle.incubate.layers._pull_box_extended_sparse(input=data, size=8, extend_size=128) + + >>> import paddle + >>> paddle.enable_static() + + >>> data = paddle.static.data(name='sequence', shape=[-1, 1], dtype='int64', lod_level=1) + >>> emb, emb_ex = paddle.incubate.layers._pull_box_extended_sparse(input=data, size=8, extend_size=128) """ helper = LayerHelper('pull_box_extended_sparse', **locals()) helper.input_dtype() @@ -1139,16 +1140,16 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): For more information of bilateral slicing, please refer to Deep Bilateral Learning for Real-Time Image Enhancement _ Args: - x(Tensor): The input tensor, which is a 4-D tensor with shape + x (Tensor): The input tensor, which is a 4-D tensor with shape [N, C, H, W], N is the batch size, C is the channel number, H and W is the feature height and width. The data type is float32 and float64. - guide(Tensor): Input grid tensor of shape [N, H, W]. The + guide (Tensor): Input grid tensor of shape [N, H, W]. The data type is float32 and float64. - grid(Tensor): Input grid tensor of shape [N, C, D, H, W]. The + grid (Tensor): Input grid tensor of shape [N, C, D, H, W]. The data type is float32 and float64. - has_offset(bool): Whether to slice with affine offset. - name(str, optional): For detailed information, please refer + has_offset (bool): Whether to slice with affine offset. + name (str, optional): For detailed information, please refer to :ref:`api_guide_Name`. Usually name is no need to set and None by default. @@ -1159,19 +1160,18 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): .. code-block:: python - import paddle.fluid as fluid - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.randn(name='x', shape=[1, 3, 101, 60], dtype='float32') - guide = paddle.randn(name='guide', shape=[1, 101, 60], dtype='float32') - grid = paddle.randn(name='grid', shape=[1, 12, 8, 10, 6], dtype='float32') + >>> x = paddle.randn(name='x', shape=[1, 3, 101, 60], dtype='float32') + >>> guide = paddle.randn(name='guide', shape=[1, 101, 60], dtype='float32') + >>> grid = paddle.randn(name='grid', shape=[1, 12, 8, 10, 6], dtype='float32') - # without offset - output = paddle.incubate.layers.bilateral_slice(x, guide, grid, has_offset=False) + >>> # without offset + >>> output = paddle.incubate.layers.bilateral_slice(x, guide, grid, has_offset=False) - # has offset - output = paddle.incubate.layers.bilateral_slice(x, guide, grid, has_offset=True) + >>> # has offset + >>> output = paddle.incubate.layers.bilateral_slice(x, guide, grid, has_offset=True) """ if paddle.in_dynamic_mode(): @@ -1215,13 +1215,13 @@ def correlation( _ Args: - x(Tensor): The input x is 4-D Tensor with shape [N, C, H, W]. The data type is float32 and float64. - y(Tensor): The input y is 4-D Tensor with shape [N, C, H, W]. The data type is float32 and float64. - pad_size(int): Pad size. The data type is int. - max_displacement(int): Max displacement. The data type is int. - stride1(int): stride size of x. The data type is int. - stride2(int): stride size of y. The data type is int. - corr_type_multiply(int, optional): The type of multiply. The data type is int. Default: 1. + x (Tensor): The input x is 4-D Tensor with shape [N, C, H, W]. The data type is float32 and float64. + y (Tensor): The input y is 4-D Tensor with shape [N, C, H, W]. The data type is float32 and float64. + pad_size (int): Pad size. The data type is int. + max_displacement (int): Max displacement. The data type is int. + stride1 (int): stride size of x. The data type is int. + stride2 (int): stride size of y. The data type is int. + corr_type_multiply (int, optional): The type of multiply. The data type is int. Default: 1. Returns: Tensor: The data type is same as input tensor. @@ -1230,25 +1230,24 @@ def correlation( .. code-block:: python - import paddle.fluid as fluid - import paddle - paddle.enable_static() - x1 = paddle.static.data(name='x1', - shape=[2,3,4,5], - dtype="float32") - x2 = paddle.static.data(name='x2', - shape=[2,3,4,5], - dtype="float32") - - - out = paddle.incubate.layers.correlation( - x1, - x2, - pad_size=4, - kernel_size=1, - max_displacement=4, - stride1=1, - stride2=1) + >>> import paddle + >>> paddle.enable_static() + >>> x1 = paddle.static.data(name='x1', + ... shape=[2, 3, 4, 5], + ... dtype="float32") + >>> x2 = paddle.static.data(name='x2', + ... shape=[2, 3, 4, 5], + ... dtype="float32") + + + >>> out = paddle.incubate.layers.correlation( + ... x1, + ... x2, + ... pad_size=4, + ... kernel_size=1, + ... max_displacement=4, + ... stride1=1, + ... stride2=1) """ @@ -1305,105 +1304,97 @@ def fused_bn_add_act( `[batch, in_height, in_width, in_channels]`. Args: - x(Tensor): The rank of input tensor can be 2, 3, 4, 5. The data type + x (Tensor): The rank of input tensor can be 2, 3, 4, 5. The data type is float16. - y(Tensor): The rank of input tensor can be 2, 3, 4, 5. The data type + y (Tensor): The rank of input tensor can be 2, 3, 4, 5. The data type is float16. - momentum(float|Tensor, optional): The value used for the moving_mean and + momentum (float|Tensor, optional): The value used for the moving_mean and moving_var computation. This should be a float number or a tensor with shape [1] and data type as float32. The updated formula is: :math:`moving\_mean = moving\_mean * momentum + new\_mean * (1. - momentum)` :math:`moving\_var = moving\_var * momentum + new\_var * (1. - momentum)` Default is 0.9. - epsilon(float, optional): A value added to the denominator for - numerical stability. Default is 1e-5. - param_attr(ParamAttr, optional): The parameter attribute for Parameter `scale` + epsilon (float, optional): A value added to the denominator for + numerical stability. Default is 1e-05. + param_attr (ParamAttr, optional): The parameter attribute for Parameter `scale` of batch_norm. If it is set to None or one attribute of ParamAttr, batch_norm - will create ParamAttr as param_attr, the name of scale can be set in ParamAttr. - If the Initializer of the param_attr is not set, the parameter is initialized - with Xavier. Default: None. - bias_attr(ParamAttr, optional): The parameter attribute for the bias of batch_norm. + will create ParamAttr as param_attr, the name of scale can be set in ParamAttr. + If the Initializer of the param_attr is not set, the parameter is initialized + with Xavier. Default: None. + bias_attr (ParamAttr, optional): The parameter attribute for the bias of batch_norm. If it is set to None or one attribute of ParamAttr, batch_norm - will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. - If the Initializer of the bias_attr is not set, the bias is initialized zero. - Default: None. - moving_mean_name(str, optional): The name of moving_mean which store the global Mean. If it + will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. + If the Initializer of the bias_attr is not set, the bias is initialized zero. + Default: None. + moving_mean_name (str, optional): The name of moving_mean which store the global Mean. If it is set to None, batch_norm will save global mean with a random name, otherwise, batch_norm - will save global mean with the string. - moving_variance_name(str, optional): The name of the moving_variance which store the global Variance. + will save global mean with the string. Default: None. + moving_variance_name (str, optional): The name of the moving_variance which store the global Variance. If it is set to None, batch_norm will save global variance with a random name, otherwise, batch_norm - will save global variance with the string. - act(string, optional): Activation type, linear|relu|prelu|... - name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`. - Usually name is no need to set and None by default. + will save global variance with the string. Default: None. + act (string, optional): Activation type, linear|relu|prelu|... Default: None. + name (str, optional): For detailed information, please refer to :ref:`api_guide_Name`. + Usually name is no need to set and None by default. Default: None. Examples: - .. code-block:: python - - import paddle - import paddle.fluid as fluid - - paddle.enable_static() - # required: gpu - def build_program(main_program, startup_program): - with fluid.program_guard(main_program, startup_program): - x = paddle.static.data(name='x', shape=[-1, 1, 28, 28], dtype='float32') - y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') - conv1_1 = paddle.static.nn.conv2d( - input=x, - filter_size=3, - num_filters=32, - stride=1, - padding=1, - act=None, - bias_attr=False, - data_format='NHWC') - conv1_2 = paddle.static.nn.conv2d( - input=x, - filter_size=3, - num_filters=32, - stride=1, - padding=1, - act=None, - bias_attr=False, - data_format='NHWC') - bn = paddle.static.nn.batch_norm( - input=conv1_1, - act=None, - data_layout='NHWC') - fused_bn_add_act = paddle.incubate.layers.fused_bn_add_act(conv1_2, bn) - prediction = paddle.static.nn.fc(x=fused_bn_add_act, size=10, activation='softmax') - loss = paddle.nn.functional.cross_entropy( - input=prediction, label=y, - reduction='none', use_softmax=False - ) - loss = paddle.mean(loss) - sgd = fluid.optimizer.SGD(learning_rate=0.001) - sgd = paddle.static.amp.decorate( - sgd, use_dynamic_loss_scaling=True, init_loss_scaling=128.0) - sgd.minimize(loss) - - return x, y, loss - - iters = 5 - batch_size = 16 - support_gpu = fluid.is_compiled_with_cuda() - if support_gpu: - main_program = fluid.Program() - startup_program = fluid.Program() - place = fluid.CUDAPlace(0) - x, y, loss = build_program(main_program, startup_program) - - feeder = fluid.DataFeeder(feed_list=[x, y], place=place) - train_reader = paddle.batch( - paddle.dataset.mnist.train(), batch_size=batch_size) - exe = fluid.Executor(place) - scope = fluid.Scope() - with fluid.scope_guard(scope): - exe.run(startup_program) - for _ in range(iters): - data = next(train_reader()) - loss_v = exe.run(main_program, feed=feeder.feed(data), fetch_list=[loss]) + .. code-block:: python + + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.enable_static() + + >>> def build_program(main_program, startup_program): + ... with paddle.static.program_guard(main_program, startup_program): + ... x = paddle.static.data(name='x', shape=[-1, 1, 28, 28], dtype='float32') + ... y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') + ... conv1_1 = paddle.static.nn.conv2d( + ... input=x, + ... filter_size=3, + ... num_filters=32, + ... stride=1, + ... padding=1, + ... act=None, + ... bias_attr=False, + ... data_format='NHWC') + ... conv1_2 = paddle.static.nn.conv2d( + ... input=x, + ... filter_size=3, + ... num_filters=32, + ... stride=1, + ... padding=1, + ... act=None, + ... bias_attr=False, + ... data_format='NHWC') + ... bn = paddle.static.nn.batch_norm( + ... input=conv1_1, + ... act=None, + ... data_layout='NHWC') + ... fused_bn_add_act = paddle.incubate.layers.fused_bn_add_act(conv1_2, bn) + ... prediction = paddle.static.nn.fc(x=fused_bn_add_act, size=10, activation='softmax') + ... loss = paddle.nn.functional.cross_entropy( + ... input=prediction, label=y, + ... reduction='none', use_softmax=False + ... ) + ... loss = paddle.mean(loss) + ... sgd = paddle.optimizer.SGD(learning_rate=0.001) + ... sgd = paddle.static.amp.decorate( + ... sgd, use_dynamic_loss_scaling=True, init_loss_scaling=128.0) + ... sgd.minimize(loss) + ... + ... return x, y, loss + + >>> iters = 5 + >>> batch_size = 16 + >>> support_gpu = paddle.is_compiled_with_cuda() + >>> if support_gpu: + ... main_program = paddle.static.Program() + ... startup_program = paddle.static.Program() + ... place = paddle.CUDAPlace(0) + ... x, y, loss = build_program(main_program, startup_program) + ... + ... feeder = paddle.DataFeeder(feed_list=[x, y], place=place) + ... train_reader = paddle.batch( + ... paddle.dataset.mnist.train(), batch_size=batch_size) """ helper = LayerHelper('fused_bn_add_act', **locals()) @@ -1550,27 +1541,29 @@ def _pull_gpups_sparse( :attr:`input`. Args: - input(Tensor): Input is a Tensor, which - contains the IDs information. - size(int|list of int): The embedding size parameter of each input, which indicates the size of + input (Tensor): Input is a Tensor, which contains the IDs information. + size (int|list of int): The embedding size parameter of each input, which indicates the size of each embedding vector respectively. - dtype(str): The dtype refers to the data type of output tensor. Only supports - float32 now. + dtype (str, optional): The dtype refers to the data type of output tensor. Only supportsfloat32 now. Default is float32. + is_distributed (bool, optional): Whether to use distributed mode. Default is False. + is_sparse (bool, optional): Whether to use sparse mode. Default is False. Returns: - Tensor: The tensor storing the embeddings of the \ - supplied inputs, whose size are indicated by size respectively. + Tensor: The tensor storing the embeddings of the supplied inputs, whose size are indicated by size respectively. Examples: .. code-block:: python - import paddle.incubate as incubate - slots = [] - data_1 = paddle.static.data(name='sequence', shape=[-1,1], dtype='int64', lod_level=1) - slots.append(data_1) - data_2 = paddle.static.data(name='sequence', shape=[-1,1], dtype='int64', lod_level=1) - slots.append(data_2) - embs = incubate.layers.pull_gpups_sparse(input=slots, size=[11, 35]) + >>> import paddle.incubate as incubate + >>> import paddle + >>> paddle.enable_static() + + >>> slots = [] + >>> data_1 = paddle.static.data(name='sequence', shape=[-1,1], dtype='int64', lod_level=1) + >>> slots.append(data_1) + >>> data_2 = paddle.static.data(name='sequence', shape=[-1,1], dtype='int64', lod_level=1) + >>> slots.append(data_2) + >>> embs = incubate.layers.pull_gpups_sparse(input=slots, size=[11, 35]) """ helper = LayerHelper('pull_gpups_sparse', **locals()) if dtype != 'float32': @@ -1613,23 +1606,26 @@ def _pull_box_sparse( :attr:`input`. Args: - input(Tensor): Input is a Tensor, which - contains the IDs information. - size(int): The embedding size parameter, which indicates the size of + input (Tensor): Input is a Tensor, which contains the IDs information. + size (int): The embedding size parameter, which indicates the size of each embedding vector respectively. - dtype(str): The dtype refers to the data type of output tensor. Only supports - float32 now. + dtype (str, optional): The dtype refers to the data type of output tensor. Only supports float32 now. Default is float32. + is_distributed (bool, optional): Whether to use distributed mode. Default is False. + is_sparse (bool, optional): Whether to use sparse mode. Default is False. Returns: - Tensor: The tensor storing the embeddings of the \ - supplied inputs. + Tensor: The tensor storing the embeddings of the supplied inputs. Examples: .. code-block:: python - import paddle.incubate as incubate - data = paddle.static.data(name='sequence', shape=[-1,1], dtype='int64', lod_level=1) - emb = incubate.layers.pull_box_sparse(input=data, size=[11]) + >>> import paddle.incubate as incubate + >>> import paddle + >>> paddle.enable_static() + + >>> x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=1) + >>> y = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=1) + >>> emb_x, emb_y = incubate.layers._pull_box_sparse([x, y], size=1) """ helper = LayerHelper('pull_box_sparse', **locals()) if dtype != 'float32': diff --git a/python/paddle/incubate/nn/functional/fused_dropout_add.py b/python/paddle/incubate/nn/functional/fused_dropout_add.py index 79f5adfcc331e..4c47a2302ef6c 100644 --- a/python/paddle/incubate/nn/functional/fused_dropout_add.py +++ b/python/paddle/incubate/nn/functional/fused_dropout_add.py @@ -51,15 +51,27 @@ def fused_dropout_add( Examples: - .. code-block:: python - - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_dropout_add - - x = paddle.randn([4, 10], dtype='float16') - y = paddle.randn([4, 10], dtype='float16') - out = fused_dropout_add(x, y, p=0.5) + .. code-block:: python + + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_dropout_add + + >>> paddle.set_device('gpu') + >>> paddle.seed(2023) + >>> x = paddle.randn([4, 10], dtype="float32") + >>> y = paddle.randn([4, 10], dtype="float32") + >>> out = fused_dropout_add(x, y, p=0.5) + >>> print(out) + Tensor(shape=[4, 10], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[-0.49133155, 0.53819323, -2.58393312, 0.06336236, -1.09908366, + 0.22085167, 2.19751787, 0.05034769, 0.53417486, 0.84864247], + [ 0.78248203, -1.59652555, -0.14399840, -0.77985179, -0.17006736, + -0.30991879, -0.36593807, -0.51025450, 1.46401680, 0.61627960], + [ 4.50472546, -0.48472026, 0.60729283, 0.33509624, -0.25593102, + -1.45173049, 1.06727099, 0.00440830, -0.77340341, 0.67393088], + [ 1.29453969, 0.07568165, 0.71947742, -0.71768606, -2.57172823, + 1.89179027, 3.26482797, 1.10493207, -1.04569530, -1.04862499]]) """ if isinstance(p, (int, float)): # fast return for p == 0 diff --git a/python/paddle/incubate/nn/functional/fused_ec_moe.py b/python/paddle/incubate/nn/functional/fused_ec_moe.py index ca2057fc01685..9f067acbb0de0 100644 --- a/python/paddle/incubate/nn/functional/fused_ec_moe.py +++ b/python/paddle/incubate/nn/functional/fused_ec_moe.py @@ -37,25 +37,20 @@ def fused_ec_moe( Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_ec_moe + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_ec_moe - batch = 10 - seq_len = 128 - d_model = 1024 - d_feed_forward = d_model * 4 - num_expert = 8 - - x = paddle.randn([batch, seq_len, d_model]) - gate = paddle.randn([batch, seq_len, num_expert]) - bmm0_weight = paddle.randn([num_expert, d_model, d_feed_forward]) - bmm0_bias = paddle.randn([num_expert, d_model, d_feed_forward]) - bmm1_weight = paddle.randn([num_expert, d_model, d_feed_forward]) - bmm1_bias = paddle.randn([num_expert, d_model, d_feed_forward]) - out = fused_ec_moe(x, gate, bmm0_weight, bmm0_bias, bmm1_weight, bmm1_bias, act_type="gelu") - - print(out.shape) # [batch, seq_len, num_expert] + >>> paddle.set_device('gpu') + >>> x = paddle.randn([10, 128, 1024]) + >>> gate = paddle.randn([10, 128, 8]) + >>> bmm0_weight = paddle.randn([8, 1024, 4096]) + >>> bmm0_bias = paddle.randn([8, 1024, 4096]) + >>> bmm1_weight = paddle.randn([8, 1024, 4096]) + >>> bmm1_bias = paddle.randn([8, 1024, 4096]) + >>> out = fused_ec_moe(x, gate, bmm0_weight, bmm0_bias, bmm1_weight, bmm1_bias, act_type="gelu") + >>> print(out.shape) + [10, 128, 1024] """ helper = LayerHelper('fused_moe', **locals()) out = helper.create_variable_for_type_inference(dtype=x.dtype) diff --git a/python/paddle/incubate/nn/functional/fused_gate_attention.py b/python/paddle/incubate/nn/functional/fused_gate_attention.py index 5bc2211c33c05..560d6717fda91 100644 --- a/python/paddle/incubate/nn/functional/fused_gate_attention.py +++ b/python/paddle/incubate/nn/functional/fused_gate_attention.py @@ -39,7 +39,7 @@ def fused_gate_attention( to information from different representation subspaces. This API only support self_attention. The pseudo code is as follows: - .. code-block:: python + .. code-block:: text c = c ** (-0.5) q = paddle.einsum('nbqa,ahc->nbqhc', q_data, query_w) * c @@ -64,20 +64,20 @@ def fused_gate_attention( Args: query (Tensor): The input query tensor. The shape is [batch_size, msa_len, res_len, q_dim]. key (Tensor, optional): The input key tensor, which can be set when - merge_qkv is False. The shape is [batch_size, msa_len, m_size, kv_dim]. - query_weight (Tensor, optional): The weight of query linear, which - should be set when input key is not None. The shape is [q_dim, num_heads, head_dim]. - key_weight (Tensor, optional): The weight of key linear, which should - be set when input key is not None. The shape is [kv_dim, num_heads, head_dim]. - value_weight (Tensor, optional): The weight of value linear, which should - be set when input key is not None. The shape is [kv_dim, num_heads, head_dim]. - qkv_weight (Tensor, optional): The weight of qkv linear, which should - be set when merge_qkv is True. The shape is [3, num_heads, head_dim, q_dim]. - gate_linear_weight (Tensor, optional): The weight of gating linear, - which should be set when has_gating is True. The shape is [q_dim, num_heads, head_dim]. - gate_linear_bias (Tensor, optional): The bias of gating linear, which - should be set when has_gating is True. The shape is [num_heads, head_dim]. Default None. - out_linear_weight (Tensor, optional): The weight of output linear. The shape is [num_heads, head_dim, q_dim]. + merge_qkv is False. The shape is [batch_size, msa_len, m_size, kv_dim]. Default None. + query_weight (Tensor, optional): The weight of query linear, which should be set when input + key is not None. The shape is [q_dim, num_heads, head_dim]. Default None. + key_weight (Tensor, optional): The weight of key linear, which should be set when input key + is not None. The shape is [kv_dim, num_heads, head_dim]. Default None. + value_weight (Tensor, optional): The weight of value linear, which should be set when input + key is not None. The shape is [kv_dim, num_heads, head_dim]. Default None. + qkv_weight (Tensor, optional): The weight of qkv linear, which should be set when merge_qkv + is True. The shape is [3, num_heads, head_dim, q_dim]. Default None. + gate_linear_weight (Tensor, optional): The weight of gating linear, which should be set when + has_gating is True. The shape is [q_dim, num_heads, head_dim]. Default None. + gate_linear_bias (Tensor, optional): The bias of gating linear, which should be set when + has_gating is True. The shape is [num_heads, head_dim]. Default None. + out_linear_weight (Tensor, optional): The weight of output linear. The shape is [num_heads, head_dim, q_dim]. Default None. out_linear_bias (Tensor): The bias of output linear, the shape is [q_dim]. Default None. nonbatched_bias (Tensor, optional): The extra bias. The shape is [batch_size, 1, num_heads, res_len, m_size]. Default None. attn_mask (Tensor, optional): The attention mask. The shape is [batch_size, msa_len, 1, 1, res_len]. Default None. @@ -92,54 +92,54 @@ def fused_gate_attention( .. code-block:: python - # required: gpu - import paddle - import paddle.incubate.nn.functional as F - - # batch_size = 2 - # msa_len = 4 - # res_len = 2 - # q_dim = 4 - # num_heads = 8 - # head_dim = 4 - # m_size = res_len (when merge_qkv is True) - - # query: [batch_size, msa_len, res_len, q_dim] - query = paddle.rand(shape=[2, 4, 2, 4], dtype="float32") - - # qkv_weight: [3, n_heads, head_dim, q_dim] - qkv_weight = paddle.rand(shape=[3, 8, 4, 4], dtype="float32") - - # nonbatched_bias: [batch_size, 1, num_heads, res_len, m_size] - nonbatched_bias = paddle.rand(shape=[2, 1, 8, 2, 2], dtype="float32") - - # attn_mask: [batch_size, msa_len, 1, 1, m_size] - attn_mask = paddle.rand(shape=[2, 4, 1, 1, 2], dtype="float32") - - # gate_linear_weight: [q_dim, num_heads, head_dim] - gate_linear_weight = paddle.rand(shape=[4, 8, 4], dtype="float32") - # gate_bias: [num_heads, head_dim] - gate_linear_bias = paddle.rand(shape=[8, 4], dtype="float32") - - # out_linear_weight: [num_heads, head_dim, q_dim] - out_linear_weight = paddle.rand(shape=[8, 4, 4], dtype="float32") - # out_linear_bias: [q_dim] - out_linear_bias = paddle.rand(shape=[4], dtype="float32") - - # output: [batch_size, msa_len, res_len, q_dim] - output = F.fused_gate_attention( - query=query, - qkv_weight=qkv_weight, - gate_linear_weight=gate_linear_weight, - gate_linear_bias=gate_linear_bias, - out_linear_weight=out_linear_weight, - out_linear_bias=out_linear_bias, - nonbatched_bias=nonbatched_bias, - attn_mask=attn_mask, - has_gating=True, - merge_qkv=True) - print(output.shape) - # [2, 4, 2, 4] + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> import paddle.incubate.nn.functional as F + + >>> # batch_size = 2 + >>> # msa_len = 4 + >>> # res_len = 2 + >>> # q_dim = 4 + >>> # num_heads = 8 + >>> # head_dim = 4 + >>> # m_size = res_len (when merge_qkv is True) + + >>> # query: [batch_size, msa_len, res_len, q_dim] + >>> query = paddle.rand(shape=[2, 4, 2, 4], dtype="float32") + + >>> # qkv_weight: [3, n_heads, head_dim, q_dim] + >>> qkv_weight = paddle.rand(shape=[3, 8, 4, 4], dtype="float32") + + >>> # nonbatched_bias: [batch_size, 1, num_heads, res_len, m_size] + >>> nonbatched_bias = paddle.rand(shape=[2, 1, 8, 2, 2], dtype="float32") + + >>> # attn_mask: [batch_size, msa_len, 1, 1, m_size] + >>> attn_mask = paddle.rand(shape=[2, 4, 1, 1, 2], dtype="float32") + + >>> # gate_linear_weight: [q_dim, num_heads, head_dim] + >>> gate_linear_weight = paddle.rand(shape=[4, 8, 4], dtype="float32") + >>> # gate_bias: [num_heads, head_dim] + >>> gate_linear_bias = paddle.rand(shape=[8, 4], dtype="float32") + + >>> # out_linear_weight: [num_heads, head_dim, q_dim] + >>> out_linear_weight = paddle.rand(shape=[8, 4, 4], dtype="float32") + >>> # out_linear_bias: [q_dim] + >>> out_linear_bias = paddle.rand(shape=[4], dtype="float32") + + >>> # output: [batch_size, msa_len, res_len, q_dim] + >>> output = F.fused_gate_attention( + ... query=query, + ... qkv_weight=qkv_weight, + ... gate_linear_weight=gate_linear_weight, + ... gate_linear_bias=gate_linear_bias, + ... out_linear_weight=out_linear_weight, + ... out_linear_bias=out_linear_bias, + ... nonbatched_bias=nonbatched_bias, + ... attn_mask=attn_mask, + ... has_gating=True, + ... merge_qkv=True) + >>> print(output.shape) + [2, 4, 2, 4] """ if in_dynamic_mode(): diff --git a/python/paddle/incubate/nn/functional/fused_matmul_bias.py b/python/paddle/incubate/nn/functional/fused_matmul_bias.py index 0fbb63025e3cf..526f8a3fec05f 100644 --- a/python/paddle/incubate/nn/functional/fused_matmul_bias.py +++ b/python/paddle/incubate/nn/functional/fused_matmul_bias.py @@ -28,11 +28,11 @@ def fused_matmul_bias( Args: x (Tensor): the first input Tensor to be multiplied. y (Tensor): the second input Tensor to be multiplied. Its rank must be 2. - bias (Tensor|None): the input bias Tensor. If it is None, no bias addition would - be performed. Otherwise, the bias is added to the matrix multiplication result. - transpose_x (bool): Whether to transpose :math:`x` before multiplication. - transpose_y (bool): Whether to transpose :math:`y` before multiplication. - name(str|None): For detailed information, please refer to + bias (Tensor, optional): the input bias Tensor. If it is None, no bias addition would + be performed. Otherwise, the bias is added to the matrix multiplication result. Default: None. + transpose_x (bool, optional): Whether to transpose :math:`x` before multiplication. Default: False. + transpose_y (bool, optional): Whether to transpose :math:`y` before multiplication. Default: False. + name (str, optional): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. Returns: @@ -41,15 +41,18 @@ def fused_matmul_bias( Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_matmul_bias - - x = paddle.randn([3, 4]) - y = paddle.randn([4, 5]) - bias = paddle.randn([5]) - out = fused_matmul_bias(x, y, bias) - print(out.shape) # [3, 5] + >>> # doctest: +SKIP('fused_gemm_epilogue is only supported when CUDA version >= 11.6') + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_matmul_bias + + >>> paddle.set_device('gpu') + >>> x = paddle.randn([3, 5]) + >>> y = paddle.randn([4, 5]) + >>> bias = paddle.randn([5]) + >>> out = fused_matmul_bias(x, y, bias) + >>> print(out.shape) + [3, 5] """ if bias is None: return matmul(x, y, transpose_x, transpose_y, name) @@ -76,10 +79,10 @@ def fused_linear(x, weight, bias=None, transpose_weight=False, name=None): Args: x (Tensor): the input Tensor to be multiplied. weight (Tensor): the weight Tensor to be multiplied. Its rank must be 2. - bias (Tensor|None): the input bias Tensor. If it is None, no bias addition would - be performed. Otherwise, the bias is added to the matrix multiplication result. - transpose_weight (bool): Whether to transpose :math:`weight` before multiplication. - name(str|None): For detailed information, please refer to + bias (Tensor, optional): the input bias Tensor. If it is None, no bias addition would + be performed. Otherwise, the bias is added to the matrix multiplication result. Default: None. + transpose_weight (bool, optional): Whether to transpose :math:`weight` before multiplication. Default: False. + name (str, optional): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. Returns: @@ -88,15 +91,18 @@ def fused_linear(x, weight, bias=None, transpose_weight=False, name=None): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_linear - - x = paddle.randn([3, 4]) - weight = paddle.randn([4, 5]) - bias = paddle.randn([5]) - out = fused_linear(x, weight, bias) - print(out.shape) # [3, 5] + >>> # doctest: +SKIP('fused_gemm_epilogue is only supported when CUDA version >= 11.6') + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_linear + + >>> paddle.set_device('gpu') + >>> x = paddle.randn([3, 4]) + >>> weight = paddle.randn([4, 5]) + >>> bias = paddle.randn([5]) + >>> out = fused_linear(x, weight, bias) + >>> print(out.shape) + [3, 5] """ return fused_matmul_bias(x, weight, bias, False, transpose_weight, name) @@ -109,25 +115,32 @@ def fused_linear_activation( Args: x (Tensor): the input Tensor to be multiplied. - weight (Tensor): the weight Tensor to be multiplied. Its rank must be 2. + y (Tensor): the weight Tensor to be multiplied. Its rank must be 2. bias (Tensor): the input bias Tensor, the bias is added to the matrix multiplication result. - transpose_weight (bool): Whether to transpose :math:`weight` before multiplication. - activation(str|None): Activation function, Currently, the available activation functions are limited to "gelu" (Gaussian Error Linear Unit) and "relu" (Rectified Linear Unit). These activation functions are applied to the output of the bias add. + trans_x (bool, optional): Whether to transpose :math:`x` before multiplication. + trans_y (bool, optional): Whether to transpose :math:`y` before multiplication. + activation (str, optional): Activation function, Currently, the available activation functions are + limited to "gelu" (Gaussian Error Linear Unit) and "relu" (Rectified Linear Unit). + These activation functions are applied to the output of the bias add. Default: None. + Returns: Tensor: the output Tensor. Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.functional import fused_linear_activation - - x = paddle.randn([3, 4]) - weight = paddle.randn([4, 5]) - bias = paddle.randn([5]) - out = fused_linear_activation(x, weight, bias) - print(out.shape) # [3, 5] + >>> # doctest: +SKIP('fused_gemm_epilogue is only supported when CUDA version >= 11.6') + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn.functional import fused_linear_activation + + >>> paddle.set_device('gpu') + >>> x = paddle.randn([3, 4]) + >>> weight = paddle.randn([4, 5]) + >>> bias = paddle.randn([5]) + >>> out = fused_linear_activation(x, weight, bias) + >>> print(out.shape) + [3, 5] """ if activation is None: activation = "none" diff --git a/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py b/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py index f68dfb1dcd53f..0b667687c114b 100644 --- a/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py +++ b/python/paddle/incubate/nn/functional/fused_rotary_position_embedding.py @@ -44,14 +44,13 @@ def fused_rotary_position_embedding( Examples: - .. code-block:: python + .. code-block:: python - >>> # required: gpu >>> # doctest: +REQUIRES(env:GPU) >>> import paddle >>> from paddle.incubate.nn.functional import fused_rotary_position_embedding - >>> paddle.device.set_device('gpu') + >>> paddle.set_device('gpu') >>> # batch_size = 2 >>> # seq_len = 2 diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 0e522533c8512..c8ed387c5b71b 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -45,44 +45,44 @@ def transpose(x, perm, name=None): Args: x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32. perm (list|tuple): Permute the input according to the data of perm. - name (str): The name of this layer. It is optional. + name (str, optional): The name of this layer. For more information, please refer to :ref:`api_guide_Name`. Default is None. Returns: Tensor: A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64. - For Example: + Examples: .. code-block:: text - x = [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] - [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] - shape(x) = [2,3,4] - - # Example 1 - perm0 = [1,0,2] - y_perm0 = [[[ 1 2 3 4] [13 14 15 16]] - [[ 5 6 7 8] [17 18 19 20]] - [[ 9 10 11 12] [21 22 23 24]]] - shape(y_perm0) = [3,2,4] - - # Example 2 - perm1 = [2,1,0] - y_perm1 = [[[ 1 13] [ 5 17] [ 9 21]] - [[ 2 14] [ 6 18] [10 22]] - [[ 3 15] [ 7 19] [11 23]] - [[ 4 16] [ 8 20] [12 24]]] - shape(y_perm1) = [4,3,2] + x = [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] + [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] + shape(x) = [2,3,4] + + # Example 1 + perm0 = [1,0,2] + y_perm0 = [[[ 1 2 3 4] [13 14 15 16]] + [[ 5 6 7 8] [17 18 19 20]] + [[ 9 10 11 12] [21 22 23 24]]] + shape(y_perm0) = [3,2,4] + + # Example 2 + perm1 = [2,1,0] + y_perm1 = [[[ 1 13] [ 5 17] [ 9 21]] + [[ 2 14] [ 6 18] [10 22]] + [[ 3 15] [ 7 19] [11 23]] + [[ 4 16] [ 8 20] [12 24]]] + shape(y_perm1) = [4,3,2] Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.randn([2, 3, 4]) - x_transposed = paddle.transpose(x, perm=[1, 0, 2]) - print(x_transposed.shape) - # [3L, 2L, 4L] + >>> x = paddle.randn([2, 3, 4]) + >>> x_transposed = paddle.transpose(x, perm=[1, 0, 2]) + >>> print(x_transposed.shape) + [3, 2, 4] """ if in_dynamic_mode(): @@ -180,10 +180,9 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None): Args: x (Tensor): The input tensor which is a Tensor. y (Tensor): The input tensor which is a Tensor. - transpose_x (bool, optional): Whether to transpose :math:`x` before multiplication. - transpose_y (bool, optional): Whether to transpose :math:`y` before multiplication. - name(str, optional): A name for this layer(optional). If set None, the layer - will be named automatically. + transpose_x (bool, optional): Whether to transpose :math:`x` before multiplication. Default is False. + transpose_y (bool, optional): Whether to transpose :math:`y` before multiplication. Default is False. + name (str, optional): If set None, the layer will be named automatically. For more information, please refer to :ref:`api_guide_Name`. Default is None. Returns: Tensor: The output Tensor. @@ -192,42 +191,42 @@ def matmul(x, y, transpose_x=False, transpose_y=False, name=None): .. code-block:: python - import paddle - - # vector * vector - x = paddle.rand([10]) - y = paddle.rand([10]) - z = paddle.matmul(x, y) - print(z.shape) - # () - - # matrix * vector - x = paddle.rand([10, 5]) - y = paddle.rand([5]) - z = paddle.matmul(x, y) - print(z.shape) - # (10,) - - # batched matrix * broadcasted vector - x = paddle.rand([10, 5, 2]) - y = paddle.rand([2]) - z = paddle.matmul(x, y) - print(z.shape) - # (10, 5) - - # batched matrix * batched matrix - x = paddle.rand([10, 5, 2]) - y = paddle.rand([10, 2, 5]) - z = paddle.matmul(x, y) - print(z.shape) - # (10, 5, 5) - - # batched matrix * broadcasted matrix - x = paddle.rand([10, 1, 5, 2]) - y = paddle.rand([1, 3, 2, 5]) - z = paddle.matmul(x, y) - print(z.shape) - # (10, 3, 5, 5) + >>> import paddle + + >>> # vector * vector + >>> x = paddle.rand([10]) + >>> y = paddle.rand([10]) + >>> z = paddle.matmul(x, y) + >>> print(z.shape) + [] + + >>> # matrix * vector + >>> x = paddle.rand([10, 5]) + >>> y = paddle.rand([5]) + >>> z = paddle.matmul(x, y) + >>> print(z.shape) + [10] + + >>> # batched matrix * broadcasted vector + >>> x = paddle.rand([10, 5, 2]) + >>> y = paddle.rand([2]) + >>> z = paddle.matmul(x, y) + >>> print(z.shape) + [10, 5] + + >>> # batched matrix * batched matrix + >>> x = paddle.rand([10, 5, 2]) + >>> y = paddle.rand([10, 2, 5]) + >>> z = paddle.matmul(x, y) + >>> print(z.shape) + [10, 5, 5] + + >>> # batched matrix * broadcasted matrix + >>> x = paddle.rand([10, 1, 5, 2]) + >>> y = paddle.rand([1, 3, 2, 5]) + >>> z = paddle.matmul(x, y) + >>> print(z.shape) + [10, 3, 5, 5] """ if in_dynamic_mode(): @@ -305,54 +304,61 @@ def norm(x, p='fro', axis=None, keepdim=False, name=None): Examples: .. code-block:: python - import paddle - x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12 - # x: Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[[-12., -11., -10., -9. ], - # [-8. , -7. , -6. , -5. ], - # [-4. , -3. , -2. , -1. ]], - - # [[ 0. , 1. , 2. , 3. ], - # [ 4. , 5. , 6. , 7. ], - # [ 8. , 9. , 10., 11.]]]) - - # compute frobenius norm along last two dimensions. - out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1]) - # out_fro: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, - # [17.43559647, 16.91153526, 16.73320007, 16.91153526]) - - # compute 2-order vector norm along last dimension. - out_pnorm = paddle.linalg.norm(x, p=2, axis=-1) - # out_pnorm: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[21.11871147, 13.19090557, 5.47722578 ], - # [3.74165750 , 11.22497177, 19.13112640]]) - - # compute 2-order norm along [0,1] dimension. - out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1]) - # out_pnorm: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, - # [17.43559647, 16.91153526, 16.73320007, 16.91153526]) - - # compute inf-order norm - out_pnorm = paddle.linalg.norm(x, p=float("inf")) - # out_pnorm = Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - # 12.) - - out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0) - # out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[12., 11., 10., 9. ], - # [8. , 7. , 6. , 7. ], - # [8. , 9. , 10., 11.]]) - - # compute -inf-order norm - out_pnorm = paddle.linalg.norm(x, p=-float("inf")) - # out_pnorm: Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - # 0.) - - out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0) - # out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[0., 1., 2., 3.], - # [4., 5., 6., 5.], - # [4., 3., 2., 1.]]) + >>> import paddle + >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12 + >>> print(x) + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[-12., -11., -10., -9. ], + [-8. , -7. , -6. , -5. ], + [-4. , -3. , -2. , -1. ]], + [[ 0. , 1. , 2. , 3. ], + [ 4. , 5. , 6. , 7. ], + [ 8. , 9. , 10., 11.]]]) + + >>> # compute frobenius norm along last two dimensions. + >>> out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1]) + >>> print(out_fro) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [17.43559647, 16.91153526, 16.73320007, 16.91153526]) + + >>> # compute 2-order vector norm along last dimension. + >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=-1) + >>> print(out_pnorm) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[21.11871147, 13.19090557, 5.47722578 ], + [3.74165750 , 11.22497177, 19.13112640]]) + + >>> # compute 2-order norm along [0,1] dimension. + >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1]) + >>> print(out_pnorm) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [17.43559647, 16.91153526, 16.73320007, 16.91153526]) + + >>> # compute inf-order norm + >>> out_pnorm = paddle.linalg.norm(x, p=float("inf")) + >>> print(out_pnorm) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 12.) + + >>> out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0) + >>> print(out_pnorm) + Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[12., 11., 10., 9. ], + [8. , 7. , 6. , 7. ], + [8. , 9. , 10., 11.]]) + + >>> # compute -inf-order norm + >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf")) + >>> print(out_pnorm) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.) + + >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0) + >>> print(out_pnorm) + Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0., 1., 2., 3.], + [4., 5., 6., 5.], + [4., 3., 2., 1.]]) """ def frobenius_norm(input, dim=None, keepdim=False, name=None): @@ -360,8 +366,10 @@ def frobenius_norm(input, dim=None, keepdim=False, name=None): The frobenius norm OP is to calculate the frobenius norm of certain two dimensions of Tensor `input`. Args: input (Variable): Tensor, data type float32, float64. - dim (list, optional): None for last two dimensions. + dim (list, optional): None for last two dimensions. Default None. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. """ if dim is not None and not (isinstance(dim, list) and len(dim) == 2): raise ValueError( @@ -400,9 +408,12 @@ def vector_norm( Calculate the p-order vector norm for certain dimension of Tensor `input`. Args: input (Variable): Tensor, data type float32, float64. - porder (float, optional): None for porder=2.0. - axis (int, optional): None for last dimension. + porder (float, optional): None for porder=2.0. Default None. + axis (int, optional): None for last dimension. Default None. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False. + asvector (bool, optional): Whether keep the result as a vector, Default False. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. """ if in_dynamic_mode(): if axis is None: @@ -682,21 +693,29 @@ def dist(x, y, p=2, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[3, 3],[3, 3]], dtype="float32") - y = paddle.to_tensor([[3, 3],[3, 1]], dtype="float32") - out = paddle.dist(x, y, 0) - print(out) # out = 1. - - out = paddle.dist(x, y, 2) - print(out) # out = 2. - - out = paddle.dist(x, y, float("inf")) - print(out) # out = 2. - - out = paddle.dist(x, y, float("-inf")) - print(out) # out = 0. + >>> import paddle + + >>> x = paddle.to_tensor([[3, 3],[3, 3]], dtype="float32") + >>> y = paddle.to_tensor([[3, 3],[3, 1]], dtype="float32") + >>> out = paddle.dist(x, y, 0) + >>> print(out) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.) + + >>> out = paddle.dist(x, y, 2) + >>> print(out) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 2.) + + >>> out = paddle.dist(x, y, float("inf")) + >>> print(out) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 2.) + + >>> out = paddle.dist(x, y, float("-inf")) + >>> print(out) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.) """ if in_dynamic_mode(): return _C_ops.dist(x, y, p) @@ -740,83 +759,95 @@ def cond(x, p=None, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]]) - - # compute conditional number when p is None - out = paddle.linalg.cond(x) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.41421342) - - # compute conditional number when order of the norm is 'fro' - out_fro = paddle.linalg.cond(x, p='fro') - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 3.16227770) - - # compute conditional number when order of the norm is 'nuc' - out_nuc = paddle.linalg.cond(x, p='nuc') - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 9.24263859) - - # compute conditional number when order of the norm is 1 - out_1 = paddle.linalg.cond(x, p=1) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 2.) - - # compute conditional number when order of the norm is -1 - out_minus_1 = paddle.linalg.cond(x, p=-1) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.) - - # compute conditional number when order of the norm is 2 - out_2 = paddle.linalg.cond(x, p=2) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.41421342) - - # compute conditional number when order of the norm is -1 - out_minus_2 = paddle.linalg.cond(x, p=-2) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 0.70710683) - - # compute conditional number when order of the norm is inf - out_inf = paddle.linalg.cond(x, p=float("inf")) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 2.) - - # compute conditional number when order of the norm is -inf - out_minus_inf = paddle.linalg.cond(x, p=-float("inf")) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.) - - a = paddle.randn([2, 4, 4]) - # Tensor(shape=[2, 4, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[-0.06784091, -0.07095790, 1.31792855, -0.58959651], - # [ 0.20818676, -0.85640615, -0.89998871, -1.47439921], - # [-0.49132481, 0.42250812, -0.77383220, -2.19794774], - # [-0.33551720, -1.70003879, -1.09795380, -0.63737559]], - - # [[ 1.12026262, -0.16119350, -1.21157813, 2.74383283], - # [-0.15999718, 0.18798758, -0.69392562, 1.35720372], - # [-0.53013402, -2.26304483, 1.40843511, -1.02288902], - # [ 0.69533503, 2.05261683, -0.02251151, -1.43127477]]]) - - a_cond_fro = paddle.linalg.cond(a, p='fro') - # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [8.86691189 , 75.23817444]) - - b = paddle.randn([2, 3, 4]) - # Tensor(shape=[2, 3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[-0.43754861, 1.80796063, -0.78729683, -1.82264030], - # [-0.27670753, 0.06620564, 0.29072434, -0.31155765], - # [ 0.34123746, -0.05444612, 0.05001324, -1.46877074]], - - # [[-0.64331555, -1.51103854, -1.26277697, -0.68024760], - # [ 2.59375715, -1.06665540, 0.96575671, -0.73330832], - # [-0.47064447, -0.23945692, -0.95150250, -1.07125998]]]) - b_cond_2 = paddle.linalg.cond(b, p=2) - # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [6.64228773, 3.89068866]) + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]]) + + >>> # compute conditional number when p is None + >>> out = paddle.linalg.cond(x) + >>> print(out) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.41421378) + + >>> # compute conditional number when order of the norm is 'fro' + >>> out_fro = paddle.linalg.cond(x, p='fro') + >>> print(out_fro) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 3.16227770) + + >>> # compute conditional number when order of the norm is 'nuc' + >>> out_nuc = paddle.linalg.cond(x, p='nuc') + >>> print(out_nuc) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 9.24264145) + + >>> # compute conditional number when order of the norm is 1 + >>> out_1 = paddle.linalg.cond(x, p=1) + >>> print(out_1) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 2.) + + >>> # compute conditional number when order of the norm is -1 + >>> out_minus_1 = paddle.linalg.cond(x, p=-1) + >>> print(out_minus_1) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.) + + >>> # compute conditional number when order of the norm is 2 + >>> out_2 = paddle.linalg.cond(x, p=2) + >>> print(out_2) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.41421378) + + >>> # compute conditional number when order of the norm is -1 + >>> out_minus_2 = paddle.linalg.cond(x, p=-2) + >>> print(out_minus_2) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.70710671) + + >>> # compute conditional number when order of the norm is inf + >>> out_inf = paddle.linalg.cond(x, p=float("inf")) + >>> print(out_inf) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 2.) + + >>> # compute conditional number when order of the norm is -inf + >>> out_minus_inf = paddle.linalg.cond(x, p=-float("inf")) + >>> print(out_minus_inf) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.) + + >>> a = paddle.randn([2, 4, 4]) + >>> print(a) + Tensor(shape=[2, 4, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[ 0.06132207, 1.11349595, 0.41906244, -0.24858207], + [-1.85169315, -1.50370061, 1.73954511, 0.13331604], + [ 1.66359663, -0.55764782, -0.59911072, -0.57773495], + [-1.03176904, -0.33741450, -0.29695082, -1.50258386]], + [[ 0.67233968, -1.07747352, 0.80170447, -0.06695852], + [-1.85003340, -0.23008066, 0.65083790, 0.75387722], + [ 0.61212337, -0.52664012, 0.19209868, -0.18707706], + [-0.00711021, 0.35236868, -0.40404350, 1.28656745]]]) + + >>> a_cond_fro = paddle.linalg.cond(a, p='fro') + >>> print(a_cond_fro) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [6.37173700 , 35.15114594]) + + >>> b = paddle.randn([2, 3, 4]) + >>> print(b) + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[ 0.03306439, 0.70149767, 0.77064633, -0.55978841], + [-0.84461296, 0.99335045, -1.23486686, 0.59551388], + [-0.63035583, -0.98797107, 0.09410731, 0.47007179]], + [[ 0.85850012, -0.98949534, -1.63086998, 1.07340240], + [-0.05492965, 1.04750168, -2.33754158, 1.16518629], + [ 0.66847134, -1.05326962, -0.05703246, -0.48190674]]]) + + >>> b_cond_2 = paddle.linalg.cond(b, p=2) + >>> print(b_cond_2) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [2.86566353, 6.85834455]) """ @@ -1081,7 +1112,7 @@ def dot(x, y, name=None): Parameters: x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` - y(Tensor): 1-D or 2-D ``Tensor``. Its dtype soulde be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` + y(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` Returns: @@ -1089,21 +1120,25 @@ def dot(x, y, name=None): Examples: - .. code-block:: python + .. code-block:: python - import paddle + >>> import paddle - # 1-D Tensor * 1-D Tensor - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([4, 5, 6]) - z = paddle.dot(x, y) - print(z) # 32 + >>> # 1-D Tensor * 1-D Tensor + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([4, 5, 6]) + >>> z = paddle.dot(x, y) + >>> print(z) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 32) - # 2-D Tensor * 2-D Tensor - x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]]) - y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]]) - z = paddle.dot(x, y) - print(z) # [32, 64] + >>> # 2-D Tensor * 2-D Tensor + >>> x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]]) + >>> y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]]) + >>> z = paddle.dot(x, y) + >>> print(z) + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [32, 64]) """ if in_dynamic_mode(): @@ -1167,31 +1202,30 @@ def cov(x, rowvar=True, ddof=True, fweights=None, aweights=None, name=None): element Cij is the covariance of xi and xj. The element Cii is the variance of xi itself. Parameters: - x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. - rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True - ddof(Bool, optional): If ddof=True will return the unbiased estimate, and ddof=False will return the simple average. Default: True - fweights(Tensor, optional): 1-D Tensor of integer frequency weights; The number of times each observation vector should be repeated. Default: None - aweights(Tensor, optional): 1-D Tensor of observation vector weights. How important of the observation vector, larger data means this element is more important. Default: None - name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` + x (Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. + rowvar (Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True. + ddof (Bool, optional): If ddof=True will return the unbiased estimate, and ddof=False will return the simple average. Default: True. + fweights (Tensor, optional): 1-D Tensor of integer frequency weights; The number of times each observation vector should be repeated. Default: None. + aweights (Tensor, optional): 1-D Tensor of observation vector weights. How important of the observation vector, larger data means this element is more important. Default: None. + name (str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` . Returns: Tensor: The covariance matrix Tensor of the variables. Examples: - .. code-block:: python - - import paddle + .. code-block:: python - xt = paddle.rand((3, 4)) - paddle.linalg.cov(xt) + >>> import paddle + >>> paddle.seed(2023) - ''' - Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - [[0.07918842, 0.06127326, 0.01493049], - [0.06127326, 0.06166256, 0.00302668], - [0.01493049, 0.00302668, 0.01632146]]) - ''' + >>> xt = paddle.rand((3, 4)) + >>> paddle.linalg.cov(xt) + >>> print(xt) + Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.86583614, 0.52014720, 0.25960937, 0.90525323], + [0.42400089, 0.40641287, 0.97020894, 0.74437362], + [0.51785129, 0.73292869, 0.97786582, 0.04315904]]) """ op_type = 'cov' if len(x.shape) > 2 or len(x.shape) < 1: @@ -1289,35 +1323,48 @@ def t(input, name=None): Args: input (Tensor): The input Tensor. It is a N-D (N<=2) Tensor of data types float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name` + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name` . + Returns: Tensor: A transposed n-D Tensor, with data type being float16, float32, float64, int32, int64. Examples: .. code-block:: python - :name: code-example - import paddle - - # Example 1 (0-D tensor) - x = paddle.to_tensor([0.79]) - paddle.t(x) # [0.79] - - # Example 2 (1-D tensor) - x = paddle.to_tensor([0.79, 0.84, 0.32]) - paddle.t(x) # [0.79000002, 0.83999997, 0.31999999] - paddle.t(x).shape # [3] - - # Example 3 (2-D tensor) - x = paddle.to_tensor([[0.79, 0.84, 0.32], - [0.64, 0.14, 0.57]]) - x.shape # [2, 3] - paddle.t(x) - # [[0.79000002, 0.63999999], - # [0.83999997, 0.14000000], - # [0.31999999, 0.56999999]] - paddle.t(x).shape # [3, 2] + :name: code-example + + >>> import paddle + + >>> # Example 1 (0-D tensor) + >>> x = paddle.to_tensor([0.79]) + >>> out = paddle.t(x) + >>> print(out) + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.79000002]) + + >>> # Example 2 (1-D tensor) + >>> x = paddle.to_tensor([0.79, 0.84, 0.32]) + >>> out2 = paddle.t(x) + >>> print(out2) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.79000002, 0.83999997, 0.31999999]) + >>> print(paddle.t(x).shape) + [3] + + >>> # Example 3 (2-D tensor) + >>> x = paddle.to_tensor([[0.79, 0.84, 0.32], + ... [0.64, 0.14, 0.57]]) + >>> print(x.shape) + [2, 3] + >>> out3 = paddle.t(x) + >>> print(out3) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.79000002, 0.63999999], + [0.83999997, 0.14000000], + [0.31999999, 0.56999999]]) + >>> print(paddle.t(x).shape) + [3, 2] """ if len(input.shape) > 2: @@ -1375,24 +1422,28 @@ def cross(x, y, axis=9, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 1.0, 1.0], - [2.0, 2.0, 2.0], - [3.0, 3.0, 3.0]]) - y = paddle.to_tensor([[1.0, 1.0, 1.0], - [1.0, 1.0, 1.0], - [1.0, 1.0, 1.0]]) - - z1 = paddle.cross(x, y) - # [[-1. -1. -1.] - # [ 2. 2. 2.] - # [-1. -1. -1.]] - - z2 = paddle.cross(x, y, axis=1) - # [[0. 0. 0.] - # [0. 0. 0.] - # [0. 0. 0.]] + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 1.0, 1.0], + ... [2.0, 2.0, 2.0], + ... [3.0, 3.0, 3.0]]) + >>> y = paddle.to_tensor([[1.0, 1.0, 1.0], + ... [1.0, 1.0, 1.0], + ... [1.0, 1.0, 1.0]]) + ... + >>> z1 = paddle.cross(x, y) + >>> print(z1) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-1., -1., -1.], + [ 2., 2., 2.], + [-1., -1., -1.]]) + + >>> z2 = paddle.cross(x, y, axis=1) + >>> print(z2) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0., 0., 0.], + [0., 0., 0.], + [0., 0., 0.]]) """ if in_dynamic_mode(): axis = K_DEFAULT_DIM if axis is None else axis @@ -1439,7 +1490,7 @@ def cholesky(x, upper=False, name=None): where * is zero or more batch dimensions, and matrices on the inner-most 2 dimensions all should be symmetric positive-definite. Its data type should be float32 or float64. - upper (bool): The flag indicating whether to return upper or lower + upper (bool, optional): The flag indicating whether to return upper or lower triangular matrices. Default: False. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -1451,14 +1502,19 @@ def cholesky(x, upper=False, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.seed(2023) - a = paddle.rand([3, 3], dtype="float32") - a_t = paddle.transpose(a, [1, 0]) - x = paddle.matmul(a, a_t) + 1e-03 + >>> a = paddle.rand([3, 3], dtype="float32") + >>> a_t = paddle.transpose(a, [1, 0]) + >>> x = paddle.matmul(a, a_t) + 1e-03 - out = paddle.linalg.cholesky(x, upper=False) - print(out) + >>> out = paddle.linalg.cholesky(x, upper=False) + >>> print(out) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1.04337072, 0. , 0. ], + [1.06467664, 0.17859250, 0. ], + [1.30602181, 0.08326444, 0.22790681]]) """ if in_dynamic_mode(): return _C_ops.cholesky(x, upper) @@ -1486,12 +1542,12 @@ def matrix_rank(x, tol=None, hermitian=False, name=None): Args: x (Tensor): The input tensor. Its shape should be `[..., m, n]`, where `...` is zero or more batch dimensions. If `x` is a batch of matrices then the output has the same batch dimensions. The data type of `x` should be float32 or float64. - tol (float,Tensor,optional): the tolerance value. Default: None. If `tol` is not specified, and `sigma` is the largest - singular value (or eigenvalues in absolute value), and `eps` is the epsilon value for the dtype of `x`, then `tol` is computed - with formula `tol=sigma * max(m,n) * eps`. Note that if `x` is a batch of matrices, `tol` is computed this way for every batch. - hermitian (bool,optional): indicates whether `x` is Hermitian. Default: False. When hermitian=True, `x` is assumed to be Hermitian, + tol (float|Tensor, optional): the tolerance value. If `tol` is not specified, and `sigma` is the largest singular value + (or eigenvalues in absolute value), and `eps` is the epsilon value for the dtype of `x`, then `tol` is computed with formula + `tol=sigma * max(m,n) * eps`. Note that if `x` is a batch of matrices, `tol` is computed this way for every batch. Default: None. + hermitian (bool, optional): indicates whether `x` is Hermitian. Default: False. When hermitian=True, `x` is assumed to be Hermitian, enabling a more efficient method for finding eigenvalues, but `x` is not checked inside the function. Instead, We just use - the lower triangular of the matrix to compute. + the lower triangular of the matrix to compute. Default: False. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -1500,19 +1556,21 @@ def matrix_rank(x, tol=None, hermitian=False, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - a = paddle.eye(10) - b = paddle.linalg.matrix_rank(a) - print(b) - # b = 10 + >>> a = paddle.eye(10) + >>> b = paddle.linalg.matrix_rank(a) + >>> print(b) + Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True, + 10) - c = paddle.ones(shape=[3, 4, 5, 5]) - d = paddle.linalg.matrix_rank(c, tol=0.01, hermitian=True) - print(d) - # d = [[1, 1, 1, 1], - # [1, 1, 1, 1], - # [1, 1, 1, 1]] + >>> c = paddle.ones(shape=[3, 4, 5, 5]) + >>> d = paddle.linalg.matrix_rank(c, tol=0.01, hermitian=True) + >>> print(d) + Tensor(shape=[3, 4], dtype=int32, place=Place(cpu), stop_gradient=True, + [[1, 1, 1, 1], + [1, 1, 1, 1], + [1, 1, 1, 1]]) """ if in_dynamic_mode(): @@ -1567,13 +1625,13 @@ def bmm(x, y, name=None): Both of the two input tensors must be three-dementional and share the same batch size. - if x is a (b, m, k) tensor, y is a (b, k, n) tensor, the output will be a (b, m, n) tensor. + If x is a (b, m, k) tensor, y is a (b, k, n) tensor, the output will be a (b, m, n) tensor. Args: x (Tensor): The input Tensor. y (Tensor): The input Tensor. - name(str|None): A name for this layer(optional). If set None, the layer - will be named automatically. + name (str|None): A name for this layer(optional). If set None, the layer + will be named automatically. Default: None. Returns: Tensor: The product Tensor. @@ -1581,23 +1639,23 @@ def bmm(x, y, name=None): Examples: .. code-block:: python - import paddle - - # In imperative mode: - # size x: (2, 2, 3) and y: (2, 3, 2) - x = paddle.to_tensor([[[1.0, 1.0, 1.0], - [2.0, 2.0, 2.0]], - [[3.0, 3.0, 3.0], - [4.0, 4.0, 4.0]]]) - y = paddle.to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]], - [[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]]) - out = paddle.bmm(x, y) - # Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[[6. , 6. ], - # [12., 12.]], - - # [[45., 45.], - # [60., 60.]]]) + >>> import paddle + + >>> # In imperative mode: + >>> # size x: (2, 2, 3) and y: (2, 3, 2) + >>> x = paddle.to_tensor([[[1.0, 1.0, 1.0], + ... [2.0, 2.0, 2.0]], + ... [[3.0, 3.0, 3.0], + ... [4.0, 4.0, 4.0]]]) + >>> y = paddle.to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]], + ... [[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]]) + >>> out = paddle.bmm(x, y) + >>> print(out) + Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[6. , 6. ], + [12., 12.]], + [[45., 45.], + [60., 60.]]]) """ if in_dynamic_mode(): @@ -1639,9 +1697,9 @@ def histogram(input, bins=100, min=0, max=0, name=None): Args: input (Tensor): A Tensor(or LoDTensor) with shape :math:`[N_1, N_2,..., N_k]` . The data type of the input Tensor should be float32, float64, int32, int64. - bins (int, optional): number of histogram bins. - min (int, optional): lower end of the range (inclusive). - max (int, optional): upper end of the range (inclusive). + bins (int, optional): number of histogram bins. Default: 100. + min (int, optional): lower end of the range (inclusive). Default: 0. + max (int, optional): upper end of the range (inclusive). Default: 0. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: @@ -1650,11 +1708,13 @@ def histogram(input, bins=100, min=0, max=0, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - inputs = paddle.to_tensor([1, 2, 1]) - result = paddle.histogram(inputs, bins=4, min=0, max=3) - print(result) # [0, 2, 1, 0] + >>> inputs = paddle.to_tensor([1, 2, 1]) + >>> result = paddle.histogram(inputs, bins=4, min=0, max=3) + >>> print(result) + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 2, 1, 0]) """ if in_dynamic_mode(): return _C_ops.histogram(input, bins, min, max) @@ -1681,8 +1741,8 @@ def bincount(x, weights=None, minlength=0, name=None): x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor. weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. Default is None. minlength (int, optional): Minimum number of bins. Should be non-negative integer. Default is 0. - name(str, optional): The default value is None. Normally there is no need for user to set this - property. For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): Normally there is no need for user to set this property. + For more information, please refer to :ref:`api_guide_Name`. Default is None. Returns: Tensor: The tensor of frequency. @@ -1690,15 +1750,19 @@ def bincount(x, weights=None, minlength=0, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 1, 4, 5]) - result1 = paddle.bincount(x) - print(result1) # [0, 2, 1, 0, 1, 1] + >>> x = paddle.to_tensor([1, 2, 1, 4, 5]) + >>> result1 = paddle.bincount(x) + >>> print(result1) + Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 2, 1, 0, 1, 1]) - w = paddle.to_tensor([2.1, 0.4, 0.1, 0.5, 0.5]) - result2 = paddle.bincount(x, weights=w) - print(result2) # [0., 2.19999981, 0.40000001, 0., 0.50000000, 0.50000000] + >>> w = paddle.to_tensor([2.1, 0.4, 0.1, 0.5, 0.5]) + >>> result2 = paddle.bincount(x, weights=w) + >>> print(result2) + Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 2.19999981, 0.40000001, 0. , 0.50000000, 0.50000000]) """ if x.dtype not in [paddle.int32, paddle.int64]: raise TypeError("Elements in Input(x) should all be integers") @@ -1738,8 +1802,8 @@ def mv(x, vec, name=None): should be one of float32, float64. vec (Tensor): A tensor with shape :math:`[N]` , The data type of the input Tensor x should be one of float32, float64. - name(str, optional): The default value is None. Normally there is no need for user to set this - property. For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): Normally there is no need for user to set this property. + For more information, please refer to :ref:`api_guide_Name`. Default is None. Returns: Tensor: The tensor which is producted by x and vec. @@ -1747,17 +1811,17 @@ def mv(x, vec, name=None): Examples: .. code-block:: python - # x: [M, N], vec: [N] - # paddle.mv(x, vec) # out: [M] + >>> # x: [M, N], vec: [N] + >>> # paddle.mv(x, vec) # out: [M] - import paddle + >>> import paddle - x = paddle.to_tensor([[2, 1, 3], [3, 0, 1]]).astype("float64") - vec = paddle.to_tensor([3, 5, 1]).astype("float64") - out = paddle.mv(x, vec) - print(out) - # Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, - # [14., 10.]) + >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1]]).astype("float64") + >>> vec = paddle.to_tensor([3, 5, 1]).astype("float64") + >>> out = paddle.mv(x, vec) + >>> print(out) + Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, + [14., 10.]) """ if in_dynamic_mode(): return _C_ops.mv(x, vec) @@ -1803,8 +1867,8 @@ def det(x, name=None): x (Tensor): the input matrix of size `(n, n)` or the batch of matrices of size `(*, n, n)` where `*` is one or more batch dimensions. - name(str, optional): Name of the output. Default is None. It's used - to print debug info for developers. Details: :ref:`api_guide_Name` + name (str, optional): Name of the output.It's used to print debug info for + developers. Details: :ref:`api_guide_Name`. Default is None. Returns: Tensor, the determinant value of a square matrix or batches of square matrices. @@ -1812,15 +1876,13 @@ def det(x, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn([3,3,3]) - - A = paddle.linalg.det(x) - - print(A) - - # [ 0.02547996, 2.52317095, -6.15900707]) + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.randn([3,3,3]) + >>> A = paddle.linalg.det(x) + >>> print(A) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [-1.29280925, 0.77832544, 0.89754158]) """ @@ -1854,15 +1916,17 @@ def slogdet(x, name=None): """ Calculates the sign and natural logarithm of the absolute value of a square matrix's or batches square matrices' determinant. - The determinant can be computed with ``sign * exp`` (logabsdet) + The determinant can be computed with ``sign * exp`` (logabsdet). - Supports input of float, double + Supports input of float, double. - Note that for matrices that have zero determinant, this returns ``(0, -inf)`` + Note that for matrices that have zero determinant, this returns ``(0, -inf)``. Args: x (Tensor): the batch of matrices of size :math:`(*, n, n)` where math:`*` is one or more batch dimensions. + name (str, optional): Name of the output.It's used to print debug info for + developers. Details: :ref:`api_guide_Name`. Default is None. Returns: y (Tensor), A tensor containing the sign of the determinant and the natural logarithm @@ -1871,16 +1935,16 @@ def slogdet(x, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn([3,3,3]) - - A = paddle.linalg.slogdet(x) - - print(A) - - # [[ 1. , 1. , -1. ], - # [-0.98610914, -0.43010661, -0.10872950]]) + >>> import paddle + >>> paddle.seed(2023) + >>> x = paddle.randn([3,3,3]) + >>> A = paddle.linalg.slogdet(x) + >>> print(A) + >>> # doctest: +SKIP + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-1. , 1. , 1. ], + [ 0.25681755, -0.25061053, -0.10809582]]) + >>> # doctest: -SKIP """ if in_dynamic_mode(): @@ -1931,8 +1995,8 @@ def svd(x, full_matrices=False, name=None): If full_matrices = False, svd op will use a economic method to store U and V. which means shape of U is `[..., N, K]`, shape of V is `[..., M, K]`. K = min(M, N). Default value is False. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): Name for the operation. For more information, + please refer to :ref:`api_guide_Name`. Default value is None. Returns: - U (Tensor), is the singular value decomposition result U. @@ -1944,25 +2008,29 @@ def svd(x, full_matrices=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0], [1.0, 3.0], [4.0, 6.0]]).astype('float64') - x = x.reshape([3, 2]) - u, s, vh = paddle.linalg.svd(x) - print (u) - #U = [[ 0.27364809, -0.21695147 ], - # [ 0.37892198, -0.87112408 ], - # [ 0.8840446 , 0.44053933 ]] - - print (s) - #S = [8.14753743, 0.78589688] - print (vh) - #VT= [[ 0.51411221, 0.85772294], - # [ 0.85772294, -0.51411221]] - - # one can verify : U * S * VT == X - # U * UH == I - # V * VH == I + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0], [1.0, 3.0], [4.0, 6.0]]).astype('float64') + >>> x = x.reshape([3, 2]) + >>> u, s, vh = paddle.linalg.svd(x) + >>> print (u) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-0.27364809, -0.21695147], + [-0.37892198, -0.87112408], + [-0.88404460, 0.44053933]]) + + >>> print (s) + Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, + [8.14753743, 0.78589688]) + + >>> print (vh) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-0.51411221, -0.85772294], + [ 0.85772294, -0.51411221]]) + + >>> # one can verify : U * S * VT == X + >>> # U * UH == I + >>> # V * VH == I """ if in_dynamic_mode(): @@ -2002,8 +2070,9 @@ def pca_lowrank(x, q=None, center=True, niter=2, name=None): Default value is :math:`q=min(6,N,M)`. center (bool, optional): if True, center the input tensor. Default value is True. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + niter (int, optional): number of iterations to perform. Default: 2. + name (str, optional): Name for the operation. For more information, + please refer to :ref:`api_guide_Name`. Default: None. Returns: - Tensor U, is N x q matrix. @@ -2015,29 +2084,30 @@ def pca_lowrank(x, q=None, center=True, niter=2, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn((5, 5), dtype='float64') - U, S, V = paddle.linalg.pca_lowrank(x) - print(U) - # Tensor(shape=[5, 5], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # [[ 0.41057070, 0.40364287, 0.59099574, -0.34529432, 0.44721360], - # [-0.30243321, 0.55670611, -0.15025419, 0.61321785, 0.44721360], - # [ 0.57427340, -0.15936327, -0.66414981, -0.06097905, 0.44721360], - # [-0.63897516, -0.09968973, -0.17298615, -0.59316819, 0.44721360], - # [-0.04343573, -0.70129598, 0.39639442, 0.38622370, 0.44721360]]) - - print(S) - # Tensor(shape=[5], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # [3.33724265, 2.57573259, 1.69479048, 0.68069312, 0.00000000]) - - print(V) - # Tensor(shape=[5, 5], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # [[ 0.09800724, -0.32627008, -0.23593953, 0.81840445, 0.39810690], - # [-0.60100303, 0.63741176, -0.01953663, 0.09023999, 0.47326173], - # [ 0.25073864, -0.21305240, -0.32662950, -0.54786156, 0.69634740], - # [ 0.33057205, 0.48282641, -0.75998527, 0.06744040, -0.27472705], - # [ 0.67604895, 0.45688227, 0.50959437, 0.13179682, 0.23908071]]) + >>> import paddle + >>> paddle.seed(2023) + + >>> x = paddle.randn((5, 5), dtype='float64') + >>> U, S, V = paddle.linalg.pca_lowrank(x) + >>> print(U) + Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True, + [[ 0.80131563, 0.11962647, 0.27667179, -0.25891214, 0.44721360], + [-0.12642301, 0.69917551, -0.17899393, 0.51296394, 0.44721360], + [ 0.08997135, -0.69821706, -0.20059228, 0.51396579, 0.44721360], + [-0.23871837, -0.02815453, -0.59888153, -0.61932365, 0.44721360], + [-0.52614559, -0.09243040, 0.70179595, -0.14869394, 0.44721360]]) + + >>> print(S) + Tensor(shape=[5], dtype=float64, place=Place(cpu), stop_gradient=True, + [2.60101614, 2.40554940, 1.49768346, 0.19064830, 0.00000000]) + + >>> print(V) + Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True, + [[ 0.58339481, -0.17143771, 0.00522143, 0.57976310, 0.54231640], + [ 0.22334335, 0.72963474, -0.30148399, -0.39388750, 0.41438019], + [ 0.05416913, 0.34666487, 0.93549758, 0.00063507, 0.04162998], + [-0.39519094, 0.53074980, -0.16687419, 0.71175586, -0.16638919], + [-0.67131070, -0.19071018, 0.07795789, -0.04615811, 0.71046714]]) """ def conjugate(x): @@ -2172,25 +2242,28 @@ def matrix_power(x, n, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1, 2, 3], - [1, 4, 9], - [1, 8, 27]], dtype='float64') - print(paddle.linalg.matrix_power(x, 2)) - # [[6. , 34. , 102.], - # [14. , 90. , 282.], - # [36. , 250., 804.]] - - print(paddle.linalg.matrix_power(x, 0)) - # [[1., 0., 0.], - # [0., 1., 0.], - # [0., 0., 1.]] - - print(paddle.linalg.matrix_power(x, -2)) - # [[ 12.91666667, -12.75000000, 2.83333333 ], - # [-7.66666667 , 8. , -1.83333333 ], - # [ 1.80555556 , -1.91666667 , 0.44444444 ]] + >>> import paddle + + >>> x = paddle.to_tensor([[1, 2, 3], + ... [1, 4, 9], + ... [1, 8, 27]], dtype='float64') + >>> print(paddle.linalg.matrix_power(x, 2)) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[6. , 34. , 102.], + [14. , 90. , 282.], + [36. , 250., 804.]]) + + >>> print(paddle.linalg.matrix_power(x, 0)) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.]]) + + >>> print(paddle.linalg.matrix_power(x, -2)) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[ 12.91666667, -12.75000000, 2.83333333 ], + [-7.66666667 , 8. , -1.83333333 ], + [ 1.80555556 , -1.91666667 , 0.44444444 ]]) """ if in_dynamic_mode(): return _C_ops.matrix_power(x, n) @@ -2218,14 +2291,14 @@ def qr(x, mode="reduced", name=None): x (Tensor): The input tensor. Its shape should be `[..., M, N]`, where ... is zero or more batch dimensions. M and N can be arbitrary positive number. The data type of x should be float32 or float64. - mode (str, optional): A flag to control the behavior of qr, the default is "reduced". + mode (str, optional): A flag to control the behavior of qr. Suppose x's shape is `[..., M, N]` and denoting `K = min(M, N)`: If mode = "reduced", qr op will return reduced Q and R matrices, which means Q's shape is `[..., M, K]` and R's shape is `[..., K, N]`. If mode = "complete", qr op will return complete Q and R matrices, which means Q's shape is `[..., M, M]` and R's shape is `[..., M, N]`. If mode = "r", qr op will only return reduced R matrix, which means - R's shape is `[..., K, N]`. + R's shape is `[..., K, N]`. Default: "reduced". name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -2236,21 +2309,21 @@ def qr(x, mode="reduced", name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') - q, r = paddle.linalg.qr(x) - print (q) - print (r) - - # Q = [[-0.16903085, 0.89708523], - # [-0.50709255, 0.27602622], - # [-0.84515425, -0.34503278]]) - - # R = [[-5.91607978, -7.43735744], - # [ 0. , 0.82807867]]) - - # one can verify : X = Q * R ; + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') + >>> q, r = paddle.linalg.qr(x) + >>> print (q) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-0.16903085, 0.89708523], + [-0.50709255, 0.27602622], + [-0.84515425, -0.34503278]]) + >>> print (r) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-5.91607978, -7.43735744], + [ 0. , 0.82807867]]) + + >>> # one can verify : X = Q * R ; """ if in_dynamic_mode(): q, r = _C_ops.qr(x, mode) @@ -2318,42 +2391,41 @@ def lu(x, pivot=True, get_infos=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') - lu,p,info = paddle.linalg.lu(x, get_infos=True) - - # >>> lu: - # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[5. , 6. ], - # [0.20000000, 0.80000000], - # [0.60000000, 0.50000000]]) - # >>> p - # Tensor(shape=[2], dtype=int32, place=CUDAPlace(0), stop_gradient=True, - # [3, 3]) - # >>> info - # Tensor(shape=[], dtype=int32, place=CUDAPlace(0), stop_gradient=True, - # 0) - - P,L,U = paddle.linalg.lu_unpack(lu,p) - - # >>> P - # (Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[0., 1., 0.], - # [0., 0., 1.], - # [1., 0., 0.]]), - # >>> L - # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[1. , 0. ], - # [0.20000000, 1. ], - # [0.60000000, 0.50000000]]), - # >>> U - # Tensor(shape=[2, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[5. , 6. ], - # [0. , 0.80000000]])) - - - # one can verify : X = P @ L @ U ; + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') + >>> lu,p,info = paddle.linalg.lu(x, get_infos=True) + + >>> print(lu) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[5. , 6. ], + [0.20000000, 0.80000000], + [0.60000000, 0.50000000]]) + >>> print(p) + Tensor(shape=[2], dtype=int32, place=Place(cpu), stop_gradient=True, + [3, 3]) + >>> print(info) + Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True, + [0]) + + >>> P,L,U = paddle.linalg.lu_unpack(lu,p) + + >>> print(P) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[0., 1., 0.], + [0., 0., 1.], + [1., 0., 0.]]) + >>> print(L) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[1. , 0. ], + [0.20000000, 1. ], + [0.60000000, 0.50000000]]) + >>> print(U) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[5. , 6. ], + [0. , 0.80000000]]) + + >>> # one can verify : X = P @ L @ U ; """ if in_dynamic_mode(): @@ -2397,7 +2469,7 @@ def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None): y (Tensor): Pivots get from paddle.linalg.lu. - unpack_ludata (bool,optional): whether to unpack L and U from x. Default: True. + unpack_ludata (bool, optional): whether to unpack L and U from x. Default: True. unpack_pivots (bool, optional): whether to unpack permutation matrix P from Pivtos. Default: True. @@ -2415,41 +2487,41 @@ def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') - lu,p,info = paddle.linalg.lu(x, get_infos=True) - - # >>> lu: - # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[5. , 6. ], - # [0.20000000, 0.80000000], - # [0.60000000, 0.50000000]]) - # >>> p - # Tensor(shape=[2], dtype=int32, place=CUDAPlace(0), stop_gradient=True, - # [3, 3]) - # >>> info - # Tensor(shape=[], dtype=int32, place=CUDAPlace(0), stop_gradient=True, - # 0) - - P,L,U = paddle.linalg.lu_unpack(lu,p) - - # >>> P - # (Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[0., 1., 0.], - # [0., 0., 1.], - # [1., 0., 0.]]), - # >>> L - # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[1. , 0. ], - # [0.20000000, 1. ], - # [0.60000000, 0.50000000]]), - # >>> U - # Tensor(shape=[2, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[5. , 6. ], - # [0. , 0.80000000]])) - - # one can verify : X = P @ L @ U ; + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64') + >>> lu,p,info = paddle.linalg.lu(x, get_infos=True) + + >>> print(lu) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[5. , 6. ], + [0.20000000, 0.80000000], + [0.60000000, 0.50000000]]) + >>> print(p) + Tensor(shape=[2], dtype=int32, place=Place(cpu), stop_gradient=True, + [3, 3]) + >>> print(info) + Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True, + [0]) + + >>> P,L,U = paddle.linalg.lu_unpack(lu,p) + + >>> print(P) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[0., 1., 0.], + [0., 0., 1.], + [1., 0., 0.]]) + >>> print(L) + Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[1. , 0. ], + [0.20000000, 1. ], + [0.60000000, 0.50000000]]) + >>> print(U) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[5. , 6. ], + [0. , 0.80000000]]) + + >>> # one can verify : X = P @ L @ U ; """ if x.ndim < 2: raise ValueError( @@ -2507,27 +2579,25 @@ def eig(x, name=None): Examples: .. code-block:: python - import paddle - - paddle.device.set_device("cpu") - - x = paddle.to_tensor([[1.6707249, 7.2249975, 6.5045543], - [9.956216, 8.749598, 6.066444 ], - [4.4251957, 1.7983172, 0.370647 ]]) - w, v = paddle.linalg.eig(x) - print(v) - # Tensor(shape=[3, 3], dtype=complex128, place=CPUPlace, stop_gradient=False, - # [[(-0.5061363550800655+0j) , (-0.7971760990842826+0j) , - # (0.18518077798279986+0j)], - # [(-0.8308237755993192+0j) , (0.3463813401919749+0j) , - # (-0.6837005269141947+0j) ], - # [(-0.23142567697893396+0j), (0.4944999840400175+0j) , - # (0.7058765252952796+0j) ]]) - - print(w) - # Tensor(shape=[3], dtype=complex128, place=CPUPlace, stop_gradient=False, - # [ (16.50471283351188+0j) , (-5.5034820550763515+0j) , - # (-0.21026087843552282+0j)]) + >>> import paddle + + >>> x = paddle.to_tensor([[1.6707249, 7.2249975, 6.5045543], + ... [9.956216, 8.749598, 6.066444 ], + ... [4.4251957, 1.7983172, 0.370647 ]]) + >>> w, v = paddle.linalg.eig(x) + >>> print(v) + Tensor(shape=[3, 3], dtype=complex64, place=Place(cpu), stop_gradient=True, + [[ (0.5061365365982056+0j) , (0.7971761226654053+0j) , + (0.1851806491613388+0j) ], + [ (0.8308236598968506+0j) , (-0.3463813066482544+0j) , + (-0.6837005615234375+0j) ], + [ (0.23142573237419128+0j), (-0.49449989199638367+0j), + (0.7058765292167664+0j) ]]) + + >>> print(w) + Tensor(shape=[3], dtype=complex64, place=Place(cpu), stop_gradient=True, + [ (16.50470733642578+0j) , (-5.503481388092041+0j) , + (-0.21026138961315155+0j)]) """ if in_dynamic_mode(): @@ -2570,18 +2640,20 @@ def eigvals(x, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.seed(2023) - paddle.set_device("cpu") - paddle.seed(1234) + >>> x = paddle.rand(shape=[3, 3], dtype='float64') + >>> print(x) + Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[0.86583615, 0.52014721, 0.25960938], + [0.90525323, 0.42400090, 0.40641288], + [0.97020893, 0.74437359, 0.51785128]]) - x = paddle.rand(shape=[3, 3], dtype='float64') - # [[0.02773777, 0.93004224, 0.06911496], - # [0.24831591, 0.45733623, 0.07717843], - # [0.48016702, 0.14235102, 0.42620817]]) - - print(paddle.linalg.eigvals(x)) - # [(-0.27078833542132674+0j), (0.29962280156230725+0j), (0.8824477020120244+0j)] #complex128 + >>> print(paddle.linalg.eigvals(x)) + Tensor(shape=[3], dtype=complex128, place=Place(cpu), stop_gradient=True, + [ (1.788956694280852+0j) , (0.16364484879581526+0j), + (-0.14491322408727625+0j)]) """ x_shape = list(x.shape) @@ -2641,33 +2713,32 @@ def multi_dot(x, name=None): Args: x ([Tensor]): The input tensors which is a list Tensor. - name(str|None): A name for this layer(optional). If set None, the layer - will be named automatically. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: The output Tensor. - Examples: - .. code-block:: python + .. code-block:: python - import paddle + >>> import paddle - # A * B - A = paddle.rand([3, 4]) - B = paddle.rand([4, 5]) - out = paddle.linalg.multi_dot([A, B]) - print(out.shape) - # [3, 5] + >>> # A * B + >>> A = paddle.rand([3, 4]) + >>> B = paddle.rand([4, 5]) + >>> out = paddle.linalg.multi_dot([A, B]) + >>> print(out.shape) + [3, 5] - # A * B * C - A = paddle.rand([10, 5]) - B = paddle.rand([5, 8]) - C = paddle.rand([8, 7]) - out = paddle.linalg.multi_dot([A, B, C]) - print(out.shape) - # [10, 7] + >>> # A * B * C + >>> A = paddle.rand([10, 5]) + >>> B = paddle.rand([5, 8]) + >>> C = paddle.rand([8, 7]) + >>> out = paddle.linalg.multi_dot([A, B, C]) + >>> print(out.shape) + [10, 7] """ if in_dynamic_mode(): @@ -2703,9 +2774,9 @@ def eigh(x, UPLO='L', name=None): Args: x (Tensor): A tensor with shape :math:`[*, N, N]` , The data type of the input Tensor x should be one of float32, float64, complex64, complex128. - UPLO(str, optional): (string, default 'L'), 'L' represents the lower triangular matrix, - "'U' represents the upper triangular matrix.". - name(str, optional): The default value is None. Normally there is no need for user to set this + UPLO (str, optional): (string, default 'L'), 'L' represents the lower triangular matrix, + "'U' represents the upper triangular matrix.". Default: 'L'. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -2717,15 +2788,17 @@ def eigh(x, UPLO='L', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[1, -2j], [2j, 5]]) - out_value, out_vector = paddle.linalg.eigh(x, UPLO='L') - print(out_value) - #[0.17157288, 5.82842712] - print(out_vector) - #[(-0.9238795325112867+0j), (-0.3826834323650898+0j)], - #[ 0.3826834323650898j , -0.9238795325112867j ]] + >>> x = paddle.to_tensor([[1, -2j], [2j, 5]]) + >>> out_value, out_vector = paddle.linalg.eigh(x, UPLO='L') + >>> print(out_value) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.17157286, 5.82842731]) + >>> print(out_vector) + Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True, + [[(-0.9238795042037964+0j), (-0.3826833963394165+0j)], + [ 0.3826833963394165j , -0.9238795042037964j ]]) """ if in_dynamic_mode(): @@ -2789,21 +2862,18 @@ def pinv(x, rcond=1e-15, hermitian=False, name=None): If x is hermitian or symmetric matrix, svd will be replaced with eigh. Args: - x(Tensor): The input tensor. Its shape should be (*, m, n) + x (Tensor): The input tensor. Its shape should be (*, m, n) where * is zero or more batch dimensions. m and n can be arbitraty positive number. The data type of x should be float32 or float64 or complex64 or complex128. When data type is complex64 or cpmplex128, hermitian should be set True. - - rcond(Tensor, optional): the tolerance value to determine + rcond (Tensor, optional): the tolerance value to determine when is a singular value zero. Default:1e-15. - - hermitian(bool, optional): indicates whether x is Hermitian + hermitian (bool, optional): indicates whether x is Hermitian if complex or symmetric if real. Default: False. - - name(str|None): A name for this layer(optional). If set None, - the layer will be named automatically. + name (str, optional): The default value is None. Normally there is no need for user to set this + property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: The tensor with same data type with x. it represents @@ -2812,25 +2882,24 @@ def pinv(x, rcond=1e-15, hermitian=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.arange(15).reshape((3, 5)).astype('float64') - input = paddle.to_tensor(x) - out = paddle.linalg.pinv(input) - print(input) - print(out) - - # input: - # [[0. , 1. , 2. , 3. , 4. ], - # [5. , 6. , 7. , 8. , 9. ], - # [10., 11., 12., 13., 14.]] - - # out: - # [[-0.22666667, -0.06666667, 0.09333333], - # [-0.12333333, -0.03333333, 0.05666667], - # [-0.02000000, 0.00000000, 0.02000000], - # [ 0.08333333, 0.03333333, -0.01666667], - # [ 0.18666667, 0.06666667, -0.05333333]] + >>> import paddle + + >>> x = paddle.arange(15).reshape((3, 5)).astype('float64') + >>> input = paddle.to_tensor(x) + >>> out = paddle.linalg.pinv(input) + >>> print(input) + Tensor(shape=[3, 5], dtype=float64, place=Place(cpu), stop_gradient=True, + [[0. , 1. , 2. , 3. , 4. ], + [5. , 6. , 7. , 8. , 9. ], + [10., 11., 12., 13., 14.]]) + + >>> print(out) + Tensor(shape=[5, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-0.22666667, -0.06666667, 0.09333333], + [-0.12333333, -0.03333333, 0.05666667], + [-0.02000000, -0.00000000, 0.02000000], + [ 0.08333333, 0.03333333, -0.01666667], + [ 0.18666667, 0.06666667, -0.05333333]]) # one can verify : x * out * x = x ; # or out * x * out = x ; @@ -3034,7 +3103,7 @@ def solve(x, y, name=None): more batch dimensions. Its data type should be float32 or float64. y (Tensor): A vector/matrix or a batch of vectors/matrices. Its shape should be ``[*, M, K]``, where ``*`` is zero or more batch dimensions. Its data type should be float32 or float64. - name(str, optional): Name for the operation (optional, default is None). + name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -3045,18 +3114,19 @@ def solve(x, y, name=None): .. code-block:: python - # a square system of linear equations: - # 2*X0 + X1 = 9 - # X0 + 2*X1 = 8 + >>> # a square system of linear equations: + >>> # 2*X0 + X1 = 9 + >>> # X0 + 2*X1 = 8 - import paddle + >>> import paddle - x = paddle.to_tensor([[3, 1],[1, 2]], dtype="float64") - y = paddle.to_tensor([9, 8], dtype="float64") - out = paddle.linalg.solve(x, y) + >>> x = paddle.to_tensor([[3, 1],[1, 2]], dtype="float64") + >>> y = paddle.to_tensor([9, 8], dtype="float64") + >>> out = paddle.linalg.solve(x, y) - print(out) - # [2., 3.]) + >>> print(out) + Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True, + [2., 3.]) """ if in_dynamic_mode(): return _C_ops.solve(x, y) @@ -3077,7 +3147,7 @@ def triangular_solve( x, y, upper=True, transpose=False, unitriangular=False, name=None ): r""" - Computes the solution of a system of equations with a triangular coefficient. `x` is coefficient matrix + Computes the solution of a system of equations with a triangular coefficient. `x` is coefficient matrix `y` is multiple right-hand sides of equations. Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs is also @@ -3103,7 +3173,7 @@ def triangular_solve( transpose (bool, optional): whether `x` should be transposed before calculation. Default: False. unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed to be 1 and not referenced from `x` . Default: False. - name(str, optional): Name for the operation (optional, default is None). + name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -3112,20 +3182,23 @@ def triangular_solve( Examples: .. code-block:: python - # a square system of linear equations: - # x1 + x2 + x3 = 0 - # 2*x2 + x3 = -9 - # -x3 = 5 - - import paddle - x = paddle.to_tensor([[1, 1, 1], - [0, 2, 1], - [0, 0,-1]], dtype="float64") - y = paddle.to_tensor([[0], [-9], [5]], dtype="float64") - out = paddle.linalg.triangular_solve(x, y, upper=True) - - print(out) - # [7, -2, -5] + >>> # a square system of linear equations: + >>> # x1 + x2 + x3 = 0 + >>> # 2*x2 + x3 = -9 + >>> # -x3 = 5 + + >>> import paddle + >>> x = paddle.to_tensor([[1, 1, 1], + ... [0, 2, 1], + ... [0, 0,-1]], dtype="float64") + >>> y = paddle.to_tensor([[0], [-9], [5]], dtype="float64") + >>> out = paddle.linalg.triangular_solve(x, y, upper=True) + + >>> print(out) + Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True, + [[ 7.], + [-2.], + [-5.]]) """ if in_dynamic_mode(): return _C_ops.triangular_solve(x, y, upper, transpose, unitriangular) @@ -3166,7 +3239,7 @@ def cholesky_solve(x, y, upper=False, name=None): y (Tensor): The input matrix which is upper or lower triangular Cholesky factor of square matrix A. Its shape should be `[*, M, M]`, where `*` is zero or more batch dimensions. Its data type should be float32 or float64. upper (bool, optional): whether to consider the Cholesky factor as a lower or upper triangular matrix. Default: False. - name(str, optional): Name for the operation (optional, default is None). + name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -3175,16 +3248,19 @@ def cholesky_solve(x, y, upper=False, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - u = paddle.to_tensor([[1, 1, 1], - [0, 2, 1], - [0, 0,-1]], dtype="float64") - b = paddle.to_tensor([[0], [-9], [5]], dtype="float64") - out = paddle.linalg.cholesky_solve(b, u, upper=True) + >>> u = paddle.to_tensor([[1, 1, 1], + ... [0, 2, 1], + ... [0, 0,-1]], dtype="float64") + >>> b = paddle.to_tensor([[0], [-9], [5]], dtype="float64") + >>> out = paddle.linalg.cholesky_solve(b, u, upper=True) - print(out) - # [-2.5, -7, 9.5] + >>> print(out) + Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True, + [[-2.50000000], + [-7. ], + [ 9.50000000]]) """ if in_dynamic_mode(): return _C_ops.cholesky_solve(x, y, upper) @@ -3225,13 +3301,13 @@ def eigvalsh(x, UPLO='L', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[1, -2j], [2j, 5]]) - out_value = paddle.eigvalsh(x, UPLO='L') - print(out_value) - # Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, - # [0.17157286, 5.82842731]) + >>> x = paddle.to_tensor([[1, -2j], [2j, 5]]) + >>> out_value = paddle.eigvalsh(x, UPLO='L') + >>> print(out_value) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.17157286, 5.82842731]) """ if in_dynamic_mode(): values, _ = _C_ops.eigvalsh(x, UPLO, x.stop_gradient) @@ -3312,31 +3388,36 @@ def lstsq(x, y, rcond=None, driver=None, name=None): Examples: .. code-block:: python - import paddle - - paddle.set_device("cpu") - x = paddle.to_tensor([[1, 3], [3, 2], [5, 6.]]) - y = paddle.to_tensor([[3, 4, 6], [5, 3, 4], [1, 2, 1.]]) - results = paddle.linalg.lstsq(x, y, driver="gelsd") - print(results[0]) - # [[ 0.78350395, -0.22165027, -0.62371236], - # [-0.11340097, 0.78866047, 1.14948535]] - print(results[1]) - # [19.81443405, 10.43814468, 30.56185532]) - print(results[2]) - # 2 - print(results[3]) - # [9.03455734, 1.54167950] - - x = paddle.to_tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12.]]) - y = paddle.to_tensor([[4, 2, 9], [2, 0, 3], [2, 5, 3.]]) - results = paddle.linalg.lstsq(x, y, driver="gels") - print(results[0]) - # [[ 0.39386186, 0.10230173, 0.93606132], - # [ 0.10741687, -0.29028133, 0.11892585], - # [-0.05115091, 0.51918161, -0.19948854]] - print(results[1]) - # [] + >>> import paddle + + >>> x = paddle.to_tensor([[1, 3], [3, 2], [5, 6.]]) + >>> y = paddle.to_tensor([[3, 4, 6], [5, 3, 4], [1, 2, 1.]]) + >>> results = paddle.linalg.lstsq(x, y, driver="gelsd") + >>> print(results[0]) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.78350395, -0.22165027, -0.62371236], + [-0.11340097, 0.78866047, 1.14948535]]) + >>> print(results[1]) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [19.81443405, 10.43814468, 30.56185532]) + >>> print(results[2]) + Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True, + 2) + >>> print(results[3]) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [9.03455734, 1.54167950]) + + >>> x = paddle.to_tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12.]]) + >>> y = paddle.to_tensor([[4, 2, 9], [2, 0, 3], [2, 5, 3.]]) + >>> results = paddle.linalg.lstsq(x, y, driver="gels") + >>> print(results[0]) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.39386186, 0.10230169, 0.93606132], + [ 0.10741688, -0.29028130, 0.11892584], + [-0.05115093, 0.51918161, -0.19948851]]) + >>> print(results[1]) + Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True, + []) """ device = paddle.get_device() if device == "cpu": @@ -3456,11 +3537,11 @@ def corrcoef(x, rowvar=True, name=None): The values of `R` are between -1 and 1. - Parameters: + Args: - x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. - rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True. - name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`. + x (Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below. + rowvar (bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True. + name (str, optional): Name of the output. It's used to print debug info for developers. Details: :ref:`api_guide_Name`. Default: None. Returns: @@ -3469,15 +3550,15 @@ def corrcoef(x, rowvar=True, name=None): Examples: .. code-block:: python - import paddle - - xt = paddle.rand((3,4)) - print(paddle.linalg.corrcoef(xt)) + >>> import paddle + >>> paddle.seed(2023) - # Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, - # [[ 1. , -0.73702252, 0.66228950], - # [-0.73702258, 1. , -0.77104872], - # [ 0.66228974, -0.77104825, 1. ]]) + >>> xt = paddle.rand((3,4)) + >>> print(paddle.linalg.corrcoef(xt)) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.99999988, -0.47689581, -0.89559376], + [-0.47689593, 1. , 0.16345492], + [-0.89559382, 0.16345496, 1. ]]) """ if len(x.shape) > 2 or len(x.shape) < 1: @@ -3545,13 +3626,15 @@ def cdist( Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]], dtype=paddle.float32) - y = paddle.to_tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]], dtype=paddle.float32) - distance = paddle.cdist(x, y) - print(distance) - # Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[3.1193, 2.0959], [2.7138, 3.8322], [2.2830, 0.3791]]) + >>> import paddle + >>> x = paddle.to_tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]], dtype=paddle.float32) + >>> y = paddle.to_tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]], dtype=paddle.float32) + >>> distance = paddle.cdist(x, y) + >>> print(distance) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[3.11927032, 2.09589314], + [2.71384072, 3.83217239], + [2.28300953, 0.37910119]]) """ check_variable_and_dtype(x, 'x', ('float32', 'float64'), 'cdist') diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 58a3b1fc0ea89..7bf39eb2e59df 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -129,12 +129,15 @@ def logical_and(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle + + >>> x = paddle.to_tensor([True]) + >>> y = paddle.to_tensor([True, False, True, False]) + >>> res = paddle.logical_and(x, y) + >>> print(res) + Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False, True , False]) - x = paddle.to_tensor([True]) - y = paddle.to_tensor([True, False, True, False]) - res = paddle.logical_and(x, y) - print(res) # [True False True False] """ if in_dynamic_mode(): return _C_ops.logical_and(x, y) @@ -188,15 +191,15 @@ def logical_or(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) - y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) - res = paddle.logical_or(x, y) - print(res) - # Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, - # [[True , True ], - # [True , False]]) + >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) + >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) + >>> res = paddle.logical_or(x, y) + >>> print(res) + Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, + [[True , True ], + [True , False]]) """ if in_dynamic_mode(): return _C_ops.logical_or(x, y) @@ -249,15 +252,15 @@ def logical_xor(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) - y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) - res = paddle.logical_xor(x, y) - print(res) - # Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, - # [[False, True ], - # [True , False]]) + >>> x = paddle.to_tensor([True, False], dtype="bool").reshape([2, 1]) + >>> y = paddle.to_tensor([True, False, True, False], dtype="bool").reshape([2, 2]) + >>> res = paddle.logical_xor(x, y) + >>> print(res) + Tensor(shape=[2, 2], dtype=bool, place=Place(cpu), stop_gradient=True, + [[False, True ], + [True , False]]) """ if in_dynamic_mode(): return _C_ops.logical_xor(x, y) @@ -300,6 +303,7 @@ def logical_not(x, out=None, name=None): .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: + x(Tensor): Operand of logical_not operator. Must be a Tensor of type bool, int8, int16, in32, in64, float16, float32, or float64, complex64, complex128. out(Tensor): The ``Tensor`` that specifies the output of the operator, which can be any ``Tensor`` that has been created in the program. The default value is None, and a new ``Tensor` will be created to save the output. name(str|None): The default value is None. Normally there is no need for users to set this property. For more information, please refer to :ref:`api_guide_Name`. @@ -310,11 +314,13 @@ def logical_not(x, out=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([True, False, True, False]) - res = paddle.logical_not(x) - print(res) # [False True False True] + >>> x = paddle.to_tensor([True, False, True, False]) + >>> res = paddle.logical_not(x) + >>> print(res) + Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True, + [False, True , False, True ]) """ if in_dynamic_mode(): return _C_ops.logical_not(x) @@ -340,9 +346,7 @@ def is_empty(x, name=None): Args: x (Tensor): The Tensor to be tested. - name (str, optional): The default value is ``None`` . Normally users - don't have to set this parameter. For more information, - please refer to :ref:`api_guide_Name` . + name (str, optional): The default value is ``None`` . Normally users don't have to set this parameter. For more information, please refer to :ref:`api_guide_Name` . Returns: Tensor: A bool scalar Tensor. True if 'x' is an empty Tensor. @@ -350,12 +354,13 @@ def is_empty(x, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - input = paddle.rand(shape=[4, 32, 32], dtype='float32') - res = paddle.is_empty(x=input) - # res: Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, - # False) + >>> input = paddle.rand(shape=[4, 32, 32], dtype='float32') + >>> res = paddle.is_empty(x=input) + >>> print(res) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + False) """ if in_dynamic_mode(): @@ -394,15 +399,19 @@ def equal_all(x, y, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 2, 3]) - z = paddle.to_tensor([1, 4, 3]) - result1 = paddle.equal_all(x, y) - print(result1) # result1 = True - result2 = paddle.equal_all(x, z) - print(result2) # result2 = False + >>> import paddle + + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 2, 3]) + >>> z = paddle.to_tensor([1, 4, 3]) + >>> result1 = paddle.equal_all(x, y) + >>> print(result1) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + True) + >>> result2 = paddle.equal_all(x, z) + >>> print(result2) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + False) """ if in_dynamic_mode(): return _C_ops.equal_all(x, y) @@ -429,11 +438,11 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): two tensors are elementwise equal within a tolerance. Args: - x(Tensor): The input tensor, it's data type should be float16, float32, float64.. - y(Tensor): The input tensor, it's data type should be float16, float32, float64.. - rtol(rtoltype, optional): The relative tolerance. Default: :math:`1e-5` . - atol(atoltype, optional): The absolute tolerance. Default: :math:`1e-8` . - equal_nan(equalnantype, optional): ${equal_nan_comment}. + x (Tensor): The input tensor, it's data type should be float16, float32, float64. + y (Tensor): The input tensor, it's data type should be float16, float32, float64. + rtol (rtoltype, optional): The relative tolerance. Default: :math:`1e-5` . + atol (atoltype, optional): The absolute tolerance. Default: :math:`1e-8` . + equal_nan (equalnantype, optional): ${equal_nan_comment}. Default: False. name (str, optional): Name for the operation. For more information, please refer to :ref:`api_guide_Name`. Default: None. @@ -443,27 +452,28 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([10000., 1e-07]) - y = paddle.to_tensor([10000.1, 1e-08]) - result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=False, name="ignore_nan") - # False - - result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=True, name="equal_nan") - # False - - x = paddle.to_tensor([1.0, float('nan')]) - y = paddle.to_tensor([1.0, float('nan')]) - result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=False, name="ignore_nan") - # False - - result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=True, name="equal_nan") - # True + >>> import paddle + + >>> x = paddle.to_tensor([10000., 1e-07]) + >>> y = paddle.to_tensor([10000.1, 1e-08]) + >>> result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan") + >>> print(result1) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + False) + >>> result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan") + >>> print(result2) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + False) + >>> x = paddle.to_tensor([1.0, float('nan')]) + >>> y = paddle.to_tensor([1.0, float('nan')]) + >>> result1 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name="ignore_nan") + >>> print(result1) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + False) + >>> result2 = paddle.allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=True, name="equal_nan") + >>> print(result2) + Tensor(shape=[], dtype=bool, place=Place(cpu), stop_gradient=True, + True) """ if in_dynamic_mode(): @@ -502,9 +512,9 @@ def equal(x, y, name=None): The output has no gradient. Args: - x(Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64. - y(Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64. + y (Tensor): Tensor, data type is bool, float16, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -514,12 +524,14 @@ def equal(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.equal(x, y) - print(result1) # result1 = [True False False] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.equal(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False, False]) """ if not isinstance(y, (int, bool, float, Variable)): raise TypeError( @@ -599,9 +611,9 @@ def greater_equal(x, y, name=None): The output has no gradient. Args: - x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: The output shape is same as input :attr:`x`. The output data type is bool. @@ -609,12 +621,14 @@ def greater_equal(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.greater_equal(x, y) - print(result1) # result1 = [True False True] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.greater_equal(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False, True ]) """ if in_dynamic_mode(): return _C_ops.greater_equal(x, y) @@ -685,9 +699,9 @@ def greater_than(x, y, name=None): The output has no gradient. Args: - x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: The output shape is same as input :attr:`x`. The output data type is bool. @@ -695,12 +709,14 @@ def greater_than(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.greater_than(x, y) - print(result1) # result1 = [False False True] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.greater_than(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [False, False, True ]) """ if in_dynamic_mode(): return _C_ops.greater_than(x, y) @@ -771,9 +787,9 @@ def less_equal(x, y, name=None): The output has no gradient. Args: - x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -782,12 +798,14 @@ def less_equal(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.less_equal(x, y) - print(result1) # result1 = [True True False] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.less_equal(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , True , False]) """ if in_dynamic_mode(): return _C_ops.less_equal(x, y) @@ -858,9 +876,9 @@ def less_than(x, y, name=None): The output has no gradient. Args: - x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float16, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -869,12 +887,14 @@ def less_than(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.less_than(x, y) - print(result1) # result1 = [False True False] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.less_than(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [False, True , False]) """ if in_dynamic_mode(): return _C_ops.less_than(x, y) @@ -945,9 +965,9 @@ def not_equal(x, y, name=None): The output has no gradient. Args: - x(Tensor): First input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64. - y(Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64. - name(str, optional): The default value is None. Normally there is no need for + x (Tensor): First input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64. + y (Tensor): Second input to compare which is N-D tensor. The input data type should be bool, float32, float64, int32, int64. + name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -956,12 +976,14 @@ def not_equal(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([1, 2, 3]) - y = paddle.to_tensor([1, 3, 2]) - result1 = paddle.not_equal(x, y) - print(result1) # result1 = [False True True] + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([1, 3, 2]) + >>> result1 = paddle.not_equal(x, y) + >>> print(result1) + Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True, + [False, True , True ]) """ if in_dynamic_mode(): return _C_ops.not_equal(x, y) @@ -1037,15 +1059,17 @@ def is_tensor(x): Examples: .. code-block:: python - import paddle + >>> import paddle - input1 = paddle.rand(shape=[2, 3, 5], dtype='float32') - check = paddle.is_tensor(input1) - print(check) #True + >>> input1 = paddle.rand(shape=[2, 3, 5], dtype='float32') + >>> check = paddle.is_tensor(input1) + >>> print(check) + True - input3 = [1, 4] - check = paddle.is_tensor(input3) - print(check) #False + >>> input3 = [1, 4] + >>> check = paddle.is_tensor(input3) + >>> print(check) + False """ if in_dynamic_mode(): @@ -1113,7 +1137,9 @@ def bitwise_and(x, y, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_and`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. - out(Tensor): Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor. + out (Tensor, optional): Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: Result of ``bitwise_and`` . It is a N-D Tensor with the same data type of input Tensor. @@ -1121,11 +1147,13 @@ def bitwise_and(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([-5, -1, 1]) - y = paddle.to_tensor([4, 2, -3]) - res = paddle.bitwise_and(x, y) - print(res) # [0, 2, 1] + >>> import paddle + >>> x = paddle.to_tensor([-5, -1, 1]) + >>> y = paddle.to_tensor([4, 2, -3]) + >>> res = paddle.bitwise_and(x, y) + >>> print(res) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 2, 1]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_and(x, y) @@ -1167,7 +1195,9 @@ def bitwise_or(x, y, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_or`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. - out(Tensor): Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor. + out (Tensor, optional): Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: Result of ``bitwise_or`` . It is a N-D Tensor with the same data type of input Tensor. @@ -1175,11 +1205,13 @@ def bitwise_or(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([-5, -1, 1]) - y = paddle.to_tensor([4, 2, -3]) - res = paddle.bitwise_or(x, y) - print(res) # [-1, -1, -3] + >>> import paddle + >>> x = paddle.to_tensor([-5, -1, 1]) + >>> y = paddle.to_tensor([4, 2, -3]) + >>> res = paddle.bitwise_or(x, y) + >>> print(res) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [-1, -1, -3]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_or(x, y) @@ -1222,7 +1254,9 @@ def bitwise_xor(x, y, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_xor`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. - out(Tensor): Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor. + out (Tensor, optional): Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: Result of ``bitwise_xor`` . It is a N-D Tensor with the same data type of input Tensor. @@ -1230,11 +1264,13 @@ def bitwise_xor(x, y, out=None, name=None): Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([-5, -1, 1]) - y = paddle.to_tensor([4, 2, -3]) - res = paddle.bitwise_xor(x, y) - print(res) # [-1, -3, -4] + >>> import paddle + >>> x = paddle.to_tensor([-5, -1, 1]) + >>> y = paddle.to_tensor([4, 2, -3]) + >>> res = paddle.bitwise_xor(x, y) + >>> print(res) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [-1, -3, -4]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_xor(x, y) @@ -1275,7 +1311,9 @@ def bitwise_not(x, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_not`` . It is a N-D Tensor of bool, uint8, int8, int16, int32, int64. - out(Tensor): Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor. + out (Tensor, optional): Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. Returns: Tensor: Result of ``bitwise_not`` . It is a N-D Tensor with the same data type of input Tensor. @@ -1283,10 +1321,12 @@ def bitwise_not(x, out=None, name=None): Examples: .. code-block:: python - import paddle - x = paddle.to_tensor([-5, -1, 1]) - res = paddle.bitwise_not(x) - print(res) # [4, 0, -2] + >>> import paddle + >>> x = paddle.to_tensor([-5, -1, 1]) + >>> res = paddle.bitwise_not(x) + >>> print(res) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [ 4, 0, -2]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_not(x) @@ -1334,25 +1374,32 @@ def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([10000., 1e-07]) - y = paddle.to_tensor([10000.1, 1e-08]) - result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=False, name="ignore_nan") - # [True, False] - result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=True, name="equal_nan") - # [True, False] - - x = paddle.to_tensor([1.0, float('nan')]) - y = paddle.to_tensor([1.0, float('nan')]) - result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=False, name="ignore_nan") - # [True, False] - result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, - equal_nan=True, name="equal_nan") - # [True, True] + >>> import paddle + + >>> x = paddle.to_tensor([10000., 1e-07]) + >>> y = paddle.to_tensor([10000.1, 1e-08]) + >>> result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, + ... equal_nan=False, name="ignore_nan") + >>> print(result1) + Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False]) + >>> result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, + ... equal_nan=True, name="equal_nan") + >>> print(result2) + Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False]) + >>> x = paddle.to_tensor([1.0, float('nan')]) + >>> y = paddle.to_tensor([1.0, float('nan')]) + >>> result1 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, + ... equal_nan=False, name="ignore_nan") + >>> print(result1) + Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True, + [True , False]) + >>> result2 = paddle.isclose(x, y, rtol=1e-05, atol=1e-08, + ... equal_nan=True, name="equal_nan") + >>> print(result2) + Tensor(shape=[2], dtype=bool, place=Place(cpu), stop_gradient=True, + [True, True]) """ if in_dynamic_mode(): From 875a4ea9add9d46d79a6fbcc7cf000c90a0240b6 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Wed, 6 Sep 2023 10:29:15 +0800 Subject: [PATCH 137/194] [xdoctest][task 79] reformat example code with google style in `python/paddle/nn/functional/loss.py` (#56901) * [Doctest] fix No.79, test=docs_preview * Apply suggestions from code review * fix gpu * fix * fix --------- Co-authored-by: Nyakku Shigure --- python/paddle/nn/functional/loss.py | 1150 ++++++++++++++------------- 1 file changed, 613 insertions(+), 537 deletions(-) diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index beb12e2a08dfc..dbcc4f0c05fda 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -66,13 +66,13 @@ def dice_loss(input, label, epsilon=0.00001, name=None): Example: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - x = paddle.randn((3,224,224,2)) - label = paddle.randint(high=2, shape=(3,224,224,1)) - predictions = F.softmax(x) - loss = F.dice_loss(input=predictions, label=label) + >>> x = paddle.randn((3,224,224,2)) + >>> label = paddle.randint(high=2, shape=(3,224,224,1)) + >>> predictions = F.softmax(x) + >>> loss = F.dice_loss(input=predictions, label=label) """ assert input.dtype in (paddle.float32, paddle.float64) assert label.dtype in (paddle.int32, paddle.int64) @@ -136,12 +136,12 @@ def log_loss(input, label, epsilon=1e-4, name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - label = paddle.randn((10,1)) - prob = paddle.randn((10,1)) - cost = F.log_loss(input=prob, label=label) + >>> label = paddle.randn((10,1)) + >>> prob = paddle.randn((10,1)) + >>> cost = F.log_loss(input=prob, label=label) """ if in_dynamic_mode(): return _C_ops.log_loss(input, label, epsilon) @@ -244,15 +244,16 @@ def fluid_softmax_with_cross_entropy( Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.seed(2023) - logits = paddle.to_tensor([0.4, 0.6, 0.9]) - label = paddle.randint(high=2, shape=[1], dtype="int64") + >>> logits = paddle.to_tensor([0.4, 0.6, 0.9]) + >>> label = paddle.randint(high=2, shape=[1], dtype="int64") - out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label) - print(out) - # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [1.15328646]) + >>> out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label) + >>> print(out) + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [1.15328646]) """ input_dims = len(list(logits.shape)) if input_dims == 0: @@ -331,18 +332,20 @@ def npair_loss(anchor, positive, labels, l2_reg=0.002): Examples: - .. code-block:: python - - import paddle + .. code-block:: python - DATATYPE = "float32" + >>> import paddle + >>> DATATYPE = "float32" + >>> paddle.seed(2023) - anchor = paddle.rand(shape=(18, 6), dtype=DATATYPE) - positive = paddle.rand(shape=(18, 6), dtype=DATATYPE) - labels = paddle.rand(shape=(18,), dtype=DATATYPE) + >>> anchor = paddle.rand(shape=(18, 6), dtype=DATATYPE) + >>> positive = paddle.rand(shape=(18, 6), dtype=DATATYPE) + >>> labels = paddle.rand(shape=(18,), dtype=DATATYPE) - npair_loss = paddle.nn.functional.npair_loss(anchor, positive, labels, l2_reg = 0.002) - print(npair_loss) + >>> npair_loss = paddle.nn.functional.npair_loss(anchor, positive, labels, l2_reg = 0.002) + >>> print(npair_loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 2.94269347) """ if anchor.size == 0: @@ -410,12 +413,13 @@ def square_error_cost(input, label): .. code-block:: python - import paddle - input = paddle.to_tensor([1.1, 1.9]) - label = paddle.to_tensor([1.0, 2.0]) - output = paddle.nn.functional.square_error_cost(input, label) - print(output) - # [0.01, 0.01] + >>> import paddle + >>> input = paddle.to_tensor([1.1, 1.9]) + >>> label = paddle.to_tensor([1.0, 2.0]) + >>> output = paddle.nn.functional.square_error_cost(input, label) + >>> print(output) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.01000000, 0.01000000]) """ if in_dynamic_mode(): @@ -496,29 +500,35 @@ def edit_distance( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input = paddle.to_tensor([[1,2,3],[4,5,6],[4,4,4],[1,1,1]], dtype='int64') - label = paddle.to_tensor([[1,3,4,1],[4,5,8,1],[7,7,7,1],[1,1,1,1]], dtype='int64') - input_len = paddle.to_tensor([3,3,3,3], dtype='int64') - label_len = paddle.to_tensor([4,4,4,4], dtype='int64') - - distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=False) - - # print(distance) - # [[3.] - # [2.] - # [4.] - # [1.]] - # if set normalized to True - # [[0.75] - # [0.5 ] - # [1. ] - # [0.25] - # - # print(sequence_num) - # [4] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> input = paddle.to_tensor([[1,2,3],[4,5,6],[4,4,4],[1,1,1]], dtype='int64') + >>> label = paddle.to_tensor([[1,3,4,1],[4,5,8,1],[7,7,7,1],[1,1,1,1]], dtype='int64') + >>> input_len = paddle.to_tensor([3,3,3,3], dtype='int64') + >>> label_len = paddle.to_tensor([4,4,4,4], dtype='int64') + + >>> distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=False) + >>> print(distance) + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [4]) + >>> print(sequence_num) + Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True, + [[3.], + [2.], + [4.], + [1.]]) + + >>> distance, sequence_num = F.loss.edit_distance(input=input, label=label, input_length=input_len, label_length=label_len, normalized=True) + >>> print(distance) + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [4]) + >>> print(sequence_num) + Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.75000000], + [0.50000000], + [1. ], + [0.25000000]]) """ @@ -629,12 +639,14 @@ def binary_cross_entropy( Examples: .. code-block:: python - import paddle + >>> import paddle - input = paddle.to_tensor([0.5, 0.6, 0.7], 'float32') - label = paddle.to_tensor([1.0, 0.0, 1.0], 'float32') - output = paddle.nn.functional.binary_cross_entropy(input, label) - print(output) # 0.65537095 + >>> input = paddle.to_tensor([0.5, 0.6, 0.7], 'float32') + >>> label = paddle.to_tensor([1.0, 0.0, 1.0], 'float32') + >>> output = paddle.nn.functional.binary_cross_entropy(input, label) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.65537095) """ if reduction not in ['sum', 'mean', 'none']: @@ -769,12 +781,14 @@ def binary_cross_entropy_with_logits( .. code-block:: python - import paddle + >>> import paddle - logit = paddle.to_tensor([5.0, 1.0, 3.0]) - label = paddle.to_tensor([1.0, 0.0, 1.0]) - output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label) - print(output) # 0.45618808 + >>> logit = paddle.to_tensor([5.0, 1.0, 3.0]) + >>> label = paddle.to_tensor([1.0, 0.0, 1.0]) + >>> output = paddle.nn.functional.binary_cross_entropy_with_logits(logit, label) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.45618808) """ if reduction not in ['sum', 'mean', 'none']: @@ -933,29 +947,36 @@ def hsigmoid_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - paddle.set_device('cpu') - - input = paddle.uniform([4, 3]) - # [[0.45424712 -0.77296764 0.82943869] # random - # [0.85062802 0.63303483 0.35312140] # random - # [0.57170701 0.16627562 0.21588242] # random - # [0.27610803 -0.99303514 -0.17114788]] # random - label = paddle.to_tensor([0, 1, 4, 5]) - num_classes = 5 - weight=paddle.uniform([num_classes-1, 3]) - # [[-0.64477652 0.24821866 -0.17456549] # random - # [-0.04635394 0.07473493 -0.25081766] # random - # [ 0.05986035 -0.12185556 0.45153677] # random - # [-0.66236806 0.91271877 -0.88088769]] # random - - out=F.hsigmoid_loss(input, label, num_classes, weight) - # [[1.96709502] - # [2.40019274] - # [2.11009121] - # [1.92374969]] + >>> import paddle + >>> import paddle.nn.functional as F + + >>> paddle.set_device('cpu') + >>> paddle.seed(2023) + + >>> input = paddle.uniform([4, 3]) + >>> print(input) + Tensor(shape=[4, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.73167229, 0.04029441, -0.48078126], + [ 0.81050646, -0.15199822, -0.18717426], + [ 0.94041789, 0.48874724, 0.03570259], + [ 0.46585739, 0.95573163, -0.91368192]]) + >>> label = paddle.to_tensor([0, 1, 4, 5]) + >>> num_classes = 5 + >>> weight = paddle.uniform([num_classes - 1, 3]) + >>> print(weight) + Tensor(shape=[4, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[-0.14721161, 0.43916738, -0.58377075], + [-0.60536981, -0.23151302, -0.70793629], + [-0.54572451, -0.10784978, -0.56684279], + [ 0.35370791, -0.07079649, 0.84765708]]) + >>> out = F.hsigmoid_loss(input, label, num_classes, weight) + >>> print(out) + Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True, + [[2.23681736], + [1.97140026], + [1.66425037], + [2.54727197]]) + """ if num_classes < 2: raise ValueError(f'Expected num_classes >= 2 (got {num_classes})') @@ -1067,13 +1088,16 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle + >>> paddle.seed(2023) + + >>> input = paddle.rand([3, 3]).astype('float32') + >>> label = paddle.rand([3, 3]).astype('float32') + >>> output = paddle.nn.functional.smooth_l1_loss(input, label) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.08307374) - input = paddle.rand([3, 3]).astype('float32') - label = paddle.rand([3, 3]).astype('float32') - output = paddle.nn.functional.smooth_l1_loss(input, label) - print(output) - # 0.068004 """ if in_dynamic_mode(): @@ -1155,13 +1179,16 @@ def margin_ranking_loss( .. code-block:: python - import paddle + >>> import paddle + + >>> input = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32') + >>> other = paddle.to_tensor([[2, 1], [2, 4]], dtype='float32') + >>> label = paddle.to_tensor([[1, -1], [-1, -1]], dtype='float32') + >>> loss = paddle.nn.functional.margin_ranking_loss(input, other, label) + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.75000000) - input = paddle.to_tensor([[1, 2], [3, 4]], dtype='float32') - other = paddle.to_tensor([[2, 1], [2, 4]], dtype='float32') - label = paddle.to_tensor([[1, -1], [-1, -1]], dtype='float32') - loss = paddle.nn.functional.margin_ranking_loss(input, other, label) - print(loss) # 0.75 """ if reduction not in ['sum', 'mean', 'none']: raise ValueError( @@ -1271,26 +1298,26 @@ def l1_loss(input, label, reduction='mean', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]]) - label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]]) + >>> input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]]) + >>> label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]]) - l1_loss = paddle.nn.functional.l1_loss(input, label) - print(l1_loss) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 0.34999999) + >>> l1_loss = paddle.nn.functional.l1_loss(input, label) + >>> print(l1_loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.34999999) - l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='none') - print(l1_loss) - # Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0.20000005, 0.19999999], - # [0.20000000, 0.79999995]]) + >>> l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='none') + >>> print(l1_loss) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.20000005, 0.19999999], + [0.20000000, 0.79999995]]) - l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='sum') - print(l1_loss) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.39999998) + >>> l1_loss = paddle.nn.functional.l1_loss(input, label, reduction='sum') + >>> print(l1_loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.39999998) """ if reduction not in ['sum', 'mean', 'none']: @@ -1367,19 +1394,22 @@ def nll_loss( Examples: .. code-block:: python - import paddle - from paddle.nn.functional import nll_loss - log_softmax = paddle.nn.LogSoftmax(axis=1) - - input = paddle.to_tensor([[0.88103855, 0.9908683 , 0.6226845 ], - [0.53331435, 0.07999352, 0.8549948 ], - [0.25879037, 0.39530203, 0.698465 ], - [0.73427284, 0.63575995, 0.18827209], - [0.05689114, 0.0862954 , 0.6325046 ]], "float32") - log_out = log_softmax(input) - label = paddle.to_tensor([0, 2, 1, 1, 0], "int64") - result = nll_loss(log_out, label) - print(result) # Tensor(shape=[], dtype=float32, place=CPUPlace, stop_gradient=True, 1.07202101) + >>> import paddle + >>> from paddle.nn.functional import nll_loss + >>> log_softmax = paddle.nn.LogSoftmax(axis=1) + + >>> input = paddle.to_tensor([[0.88103855, 0.9908683 , 0.6226845 ], + ... [0.53331435, 0.07999352, 0.8549948 ], + ... [0.25879037, 0.39530203, 0.698465 ], + ... [0.73427284, 0.63575995, 0.18827209], + ... [0.05689114, 0.0862954 , 0.6325046 ]], "float32") + >>> log_out = log_softmax(input) + >>> label = paddle.to_tensor([0, 2, 1, 1, 0], "int64") + >>> result = nll_loss(log_out, label) + >>> print(result) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.07202101) + """ if reduction not in ['sum', 'mean', 'none']: raise ValueError( @@ -1502,15 +1532,24 @@ def poisson_nll_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input = paddle.randn([5, 2], dtype=paddle.float32) - label = paddle.randn([5, 2], dtype=paddle.float32) - loss = F.poisson_nll_loss(input, label, log_input=True, reduction='none') - print(loss) - loss = F.poisson_nll_loss(input, label, reduction='mean') - print(loss) + >>> import paddle + >>> import paddle.nn.functional as F + >>> paddle.seed(2023) + + >>> input = paddle.randn([5, 2], dtype=paddle.float32) + >>> label = paddle.randn([5, 2], dtype=paddle.float32) + >>> loss = F.poisson_nll_loss(input, label, log_input=True, reduction='none') + >>> print(loss) + Tensor(shape=[5, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 1.09998012, 3.68829036], + [ 1.95291090, 0.69603068], + [-0.39289063, -2.03713036], + [ 4.52518702, 1.28625548], + [ 3.94454789, 0.53521496]]) + >>> loss = F.poisson_nll_loss(input, label, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.52983975) """ # check parameter values @@ -1606,31 +1645,36 @@ def kl_div(input, label, reduction='mean', name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F + >>> paddle.seed(2023) - shape = (5, 20) + >>> shape = (5, 20) - # input(x) should be a distribution in the log space - x = F.log_softmax(paddle.randn(shape), axis=1).astype('float32') + >>> # input(x) should be a distribution in the log space + >>> x = F.log_softmax(paddle.randn(shape), axis=1).astype('float32') - target = paddle.uniform(shape, min=-10, max=10).astype('float32') + >>> target = paddle.uniform(shape, min=-10, max=10).astype('float32') - # 'batchmean' reduction, loss shape will be [], who is 0-D Tensor - pred_loss = F.kl_div(x, target, reduction='batchmean') - # shape=[] + >>> # 'batchmean' reduction, loss shape will be [], who is 0-D Tensor + >>> pred_loss = F.kl_div(x, target, reduction='batchmean') + >>> print(pred_loss.shape) + [] - # 'mean' reduction, loss shape will be [], who is 0-D Tensor - pred_loss = F.kl_div(x, target, reduction='mean') - # shape=[] + >>> # 'mean' reduction, loss shape will be [], who is 0-D Tensor + >>> pred_loss = F.kl_div(x, target, reduction='mean') + >>> print(pred_loss.shape) + [] - # 'sum' reduction, loss shape will be [], who is 0-D Tensor - pred_loss = F.kl_div(x, target, reduction='sum') - # shape=[] + >>> # 'sum' reduction, loss shape will be [], who is 0-D Tensor + >>> pred_loss = F.kl_div(x, target, reduction='sum') + >>> print(pred_loss.shape) + [] - # 'none' reduction, loss shape is same with input shape - pred_loss = F.kl_div(x, target, reduction='none') - # shape=[5, 20] + >>> # 'none' reduction, loss shape is same with input shape + >>> pred_loss = F.kl_div(x, target, reduction='none') + >>> print(pred_loss.shape) + [5, 20] """ # ugly type promotion @@ -1723,13 +1767,14 @@ def mse_loss(input, label, reduction='mean', name=None): .. code-block:: python - import paddle - mse_loss = paddle.nn.loss.MSELoss() - input = paddle.to_tensor(1.5) - label = paddle.to_tensor(1.7) - output = mse_loss(input, label) - print(output) - # 0.04000002 + >>> import paddle + >>> mse_loss = paddle.nn.loss.MSELoss() + >>> input = paddle.to_tensor(1.5) + >>> label = paddle.to_tensor(1.7) + >>> output = mse_loss(input, label) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.04000002) """ @@ -1791,56 +1836,54 @@ def ctc_loss( .. code-block:: python - # declarative mode - import paddle.nn.functional as F - import paddle - - # length of the longest logit sequence - max_seq_length = 4 - #length of the longest label sequence - max_label_length = 3 - # number of logit sequences - batch_size = 2 - # class num - class_num = 3 - - log_probs = paddle.to_tensor([[[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], - [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], - - [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], - [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], - - [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], - [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], - - [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], - [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]], - - [[8.76389146e-01, 8.94606650e-01, 8.50442126e-02], - [3.90547849e-02, 1.69830427e-01, 8.78142476e-01]]], - dtype="float32") - labels = paddle.to_tensor([[1, 2, 2], - [1, 2, 2]], dtype="int32") - input_lengths = paddle.to_tensor([5, 5], dtype="int64") - label_lengths = paddle.to_tensor([3, 3], dtype="int64") - - loss = F.ctc_loss(log_probs, labels, - input_lengths, - label_lengths, - blank=0, - reduction='none') - print(loss) - # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [3.91798496, 2.90765190]) - - loss = F.ctc_loss(log_probs, labels, - input_lengths, - label_lengths, - blank=0, - reduction='mean') - print(loss) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 1.13760614) + >>> # declarative mode + >>> import paddle.nn.functional as F + >>> import paddle + >>> import numpy as np + + >>> # length of the longest logit sequence + >>> max_seq_length = 4 + >>> #length of the longest label sequence + >>> max_label_length = 3 + >>> # number of logit sequences + >>> batch_size = 2 + >>> # class num + >>> class_num = 3 + + >>> log_probs = paddle.to_tensor(np.array([ + ... [[4.17021990e-01, 7.20324516e-01, 1.14374816e-04], + ... [3.02332580e-01, 1.46755889e-01, 9.23385918e-02]], + ... [[1.86260208e-01, 3.45560730e-01, 3.96767467e-01], + ... [5.38816750e-01, 4.19194520e-01, 6.85219526e-01]], + ... [[2.04452246e-01, 8.78117442e-01, 2.73875929e-02], + ... [6.70467496e-01, 4.17304814e-01, 5.58689833e-01]], + ... [[1.40386939e-01, 1.98101491e-01, 8.00744593e-01], + ... [9.68261600e-01, 3.13424170e-01, 6.92322612e-01]], + ... [[8.76389146e-01, 8.94606650e-01, 8.50442126e-02], + ... [3.90547849e-02, 1.69830427e-01, 8.78142476e-01]] + ... ]), dtype="float32") + >>> labels = paddle.to_tensor([[1, 2, 2], + ... [1, 2, 2]], dtype="int32") + >>> input_lengths = paddle.to_tensor([5, 5], dtype="int64") + >>> label_lengths = paddle.to_tensor([3, 3], dtype="int64") + + >>> loss = F.ctc_loss(log_probs, labels, + ... input_lengths, + ... label_lengths, + ... blank=0, + ... reduction='none') + >>> print(loss) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [3.91798496, 2.90765190]) + + >>> loss = F.ctc_loss(log_probs, labels, + ... input_lengths, + ... label_lengths, + ... blank=0, + ... reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.13760614) """ @@ -1940,34 +1983,37 @@ def rnnt_loss( .. code-block:: python - # declarative mode - import paddle.nn.functional as F - import numpy as np - import paddle - import functools - - fn = functools.partial(F.rnnt_loss, reduction='sum', fastemit_lambda=0.0, blank=0) - - acts = np.array([[[[0.1, 0.6, 0.1, 0.1, 0.1], - [0.1, 0.1, 0.6, 0.1, 0.1], - [0.1, 0.1, 0.2, 0.8, 0.1]], - [[0.1, 0.6, 0.1, 0.1, 0.1], - [0.1, 0.1, 0.2, 0.1, 0.1], - [0.7, 0.1, 0.2, 0.1, 0.1]]]]) - labels = [[1, 2]] - - acts = paddle.to_tensor(acts, stop_gradient=False) - - lengths = [acts.shape[1]] * acts.shape[0] - label_lengths = [len(l) for l in labels] - labels = paddle.to_tensor(labels, paddle.int32) - lengths = paddle.to_tensor(lengths, paddle.int32) - label_lengths = paddle.to_tensor(label_lengths, paddle.int32) - - costs = fn(acts, labels, lengths, label_lengths) - print(costs) - # Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=False, - # 4.49566677) + >>> # declarative mode + >>> import paddle.nn.functional as F + >>> import numpy as np + >>> import paddle + >>> import functools + + >>> fn = functools.partial(F.rnnt_loss, reduction='sum', fastemit_lambda=0.0, blank=0) + + >>> acts = np.array([[ + ... [[0.1, 0.6, 0.1, 0.1, 0.1], + ... [0.1, 0.1, 0.6, 0.1, 0.1], + ... [0.1, 0.1, 0.2, 0.8, 0.1]], + ... [[0.1, 0.6, 0.1, 0.1, 0.1], + ... [0.1, 0.1, 0.2, 0.1, 0.1], + ... [0.7, 0.1, 0.2, 0.1, 0.1]] + ... ]]) + >>> labels = [[1, 2]] + + >>> acts = paddle.to_tensor(acts, stop_gradient=False) + + >>> lengths = [acts.shape[1]] * acts.shape[0] + >>> label_lengths = [len(l) for l in labels] + >>> labels = paddle.to_tensor(labels, paddle.int32) + >>> lengths = paddle.to_tensor(lengths, paddle.int32) + >>> label_lengths = paddle.to_tensor(label_lengths, paddle.int32) + + >>> costs = fn(acts, labels, lengths, label_lengths) + >>> print(costs) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=False, + -2.85042444) + """ def warprnnt( @@ -2087,144 +2133,148 @@ def margin_cross_entropy( Examples: - .. code-block:: python - :name: code-example1 - - # required: gpu - # Single GPU - import paddle - m1 = 1.0 - m2 = 0.5 - m3 = 0.0 - s = 64.0 - batch_size = 2 - feature_length = 4 - num_classes = 4 - - label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64') - - X = paddle.randn( - shape=[batch_size, feature_length], - dtype='float64') - X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) - X = paddle.divide(X, X_l2) - - W = paddle.randn( - shape=[feature_length, num_classes], - dtype='float64') - W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) - W = paddle.divide(W, W_l2) - - logits = paddle.matmul(X, W) - loss, softmax = paddle.nn.functional.margin_cross_entropy( - logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) - - print(logits) - print(label) - print(loss) - print(softmax) - - #Tensor(shape=[2, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[ 0.85204151, -0.55557678, 0.04994566, 0.71986042], - # [-0.20198586, -0.35270476, -0.55182702, 0.09749021]]) - #Tensor(shape=[2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [2, 3]) - #Tensor(shape=[2, 1], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[82.37059586], - # [12.13448420]]) - #Tensor(shape=[2, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[0.99978819, 0.00000000, 0.00000000, 0.00021181], - # [0.99992995, 0.00006468, 0.00000000, 0.00000537]]) - - .. code-block:: python - :name: code-example2 - - # required: distributed - # Multi GPU, test_margin_cross_entropy.py - import paddle - import paddle.distributed as dist - strategy = dist.fleet.DistributedStrategy() - dist.fleet.init(is_collective=True, strategy=strategy) - rank_id = dist.get_rank() - m1 = 1.0 - m2 = 0.5 - m3 = 0.0 - s = 64.0 - batch_size = 2 - feature_length = 4 - num_class_per_card = [4, 8] - num_classes = paddle.sum(paddle.to_tensor(num_class_per_card)) - - label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64') - label_list = [] - dist.all_gather(label_list, label) - label = paddle.concat(label_list, axis=0) - - X = paddle.randn( - shape=[batch_size, feature_length], - dtype='float64') - X_list = [] - dist.all_gather(X_list, X) - X = paddle.concat(X_list, axis=0) - X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) - X = paddle.divide(X, X_l2) - - W = paddle.randn( - shape=[feature_length, num_class_per_card[rank_id]], - dtype='float64') - W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) - W = paddle.divide(W, W_l2) - - logits = paddle.matmul(X, W) - loss, softmax = paddle.nn.functional.margin_cross_entropy( - logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) - - print(logits) - print(label) - print(loss) - print(softmax) - - # python -m paddle.distributed.launch --gpus=0,1 test_margin_cross_entropy.py - ## for rank0 input - #Tensor(shape=[4, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[ 0.32888934, 0.02408748, -0.02763289, 0.18173063], - # [-0.52893978, -0.10623845, -0.21596515, -0.06432517], - # [-0.00536345, -0.03924667, 0.66735314, -0.28640926], - # [-0.09907366, -0.48534973, -0.10365338, -0.39472322]]) - #Tensor(shape=[4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [11, 1 , 10, 11]) - - ## for rank1 input - #Tensor(shape=[4, 8], dtype=float64, place=CUDAPlace(1), stop_gradient=True, - # [[ 0.68654754, 0.28137170, 0.69694954, -0.60923933, -0.57077653, 0.54576703, -0.38709028, 0.56028204], - # [-0.80360371, -0.03042448, -0.45107338, 0.49559349, 0.69998950, -0.45411693, 0.61927630, -0.82808600], - # [ 0.11457570, -0.34785879, -0.68819499, -0.26189226, -0.48241491, -0.67685711, 0.06510185, 0.49660849], - # [ 0.31604851, 0.52087884, 0.53124749, -0.86176582, -0.43426329, 0.34786144, -0.10850784, 0.51566383]]) - #Tensor(shape=[4], dtype=int64, place=CUDAPlace(1), stop_gradient=True, - # [11, 1 , 10, 11]) - - ## for rank0 output - #Tensor(shape=[4, 1], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[38.96608230], - # [81.28152394], - # [69.67229865], - # [31.74197251]]) - #Tensor(shape=[4, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, - # [[0.00000000, 0.00000000, 0.00000000, 0.00000000], - # [0.00000000, 0.00000000, 0.00000000, 0.00000000], - # [0.00000000, 0.00000000, 0.99998205, 0.00000000], - # [0.00000000, 0.00000000, 0.00000000, 0.00000000]]) - ## for rank1 output - #Tensor(shape=[4, 1], dtype=float64, place=CUDAPlace(1), stop_gradient=True, - # [[38.96608230], - # [81.28152394], - # [69.67229865], - # [31.74197251]]) - #Tensor(shape=[4, 8], dtype=float64, place=CUDAPlace(1), stop_gradient=True, - # [[0.33943993, 0.00000000, 0.66051859, 0.00000000, 0.00000000, 0.00004148, 0.00000000, 0.00000000], - # [0.00000000, 0.00000000, 0.00000000, 0.00000207, 0.99432097, 0.00000000, 0.00567696, 0.00000000], - # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00001795], - # [0.00000069, 0.33993085, 0.66006319, 0.00000000, 0.00000000, 0.00000528, 0.00000000, 0.00000000]]) + .. code-block:: python + :name: code-example1 + + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.seed(2023) + >>> paddle.device.set_device('gpu') + >>> m1 = 1.0 + >>> m2 = 0.5 + >>> m3 = 0.0 + >>> s = 64.0 + >>> batch_size = 2 + >>> feature_length = 4 + >>> num_classes = 4 + + >>> label = paddle.randint(low=0, high=num_classes, shape=[batch_size], dtype='int64') + + >>> X = paddle.randn( + ... shape=[batch_size, feature_length], + ... dtype='float64') + >>> X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) + >>> X = paddle.divide(X, X_l2) + + >>> W = paddle.randn( + ... shape=[feature_length, num_classes], + ... dtype='float64') + >>> W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) + >>> W = paddle.divide(W, W_l2) + + >>> logits = paddle.matmul(X, W) + >>> loss, softmax = paddle.nn.functional.margin_cross_entropy( + ... logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) + >>> print(logits) + Tensor(shape=[2, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True, + [[-0.59561850, 0.32797505, 0.80279214, 0.00144975], + [-0.16265212, 0.84155098, 0.62008629, 0.79126072]]) + >>> print(label) + Tensor(shape=[2], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 0]) + >>> print(loss) + Tensor(shape=[2, 1], dtype=float64, place=Place(gpu:0), stop_gradient=True, + [[61.94391901], + [93.30853839]]) + >>> print(softmax) + Tensor(shape=[2, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True, + [[0.00000000, 0.00000000, 1. , 0.00000000], + [0.00000000, 0.96152676, 0.00000067, 0.03847257]]) + + .. code-block:: python + :name: code-example2 + + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # Multi GPU, test_margin_cross_entropy.py + >>> import paddle + >>> import paddle.distributed as dist + >>> paddle.seed(2023) + >>> strategy = dist.fleet.DistributedStrategy() + >>> dist.fleet.init(is_collective=True, strategy=strategy) + >>> rank_id = dist.get_rank() + >>> m1 = 1.0 + >>> m2 = 0.5 + >>> m3 = 0.0 + >>> s = 64.0 + >>> batch_size = 2 + >>> feature_length = 4 + >>> num_class_per_card = [4, 8] + >>> num_classes = paddle.sum(paddle.to_tensor(num_class_per_card)) + + >>> label = paddle.randint(low=0, high=num_classes.item(), shape=[batch_size], dtype='int64') + >>> label_list = [] + >>> dist.all_gather(label_list, label) + >>> label = paddle.concat(label_list, axis=0) + + >>> X = paddle.randn( + ... shape=[batch_size, feature_length], + ... dtype='float64') + >>> X_list = [] + >>> dist.all_gather(X_list, X) + >>> X = paddle.concat(X_list, axis=0) + >>> X_l2 = paddle.sqrt(paddle.sum(paddle.square(X), axis=1, keepdim=True)) + >>> X = paddle.divide(X, X_l2) + + >>> W = paddle.randn( + ... shape=[feature_length, num_class_per_card[rank_id]], + ... dtype='float64') + >>> W_l2 = paddle.sqrt(paddle.sum(paddle.square(W), axis=0, keepdim=True)) + >>> W = paddle.divide(W, W_l2) + + >>> logits = paddle.matmul(X, W) + >>> loss, softmax = paddle.nn.functional.margin_cross_entropy( + ... logits, label, margin1=m1, margin2=m2, margin3=m3, scale=s, return_softmax=True, reduction=None) + >>> print(logits) + >>> print(label) + >>> print(loss) + >>> print(softmax) + + >>> # python -m paddle.distributed.launch --gpus=0,1 --log_dir log test_margin_cross_entropy.py + >>> # cat log/workerlog.0 + >>> # Tensor(shape=[4, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True, + >>> # [[-0.59561850, 0.32797505, 0.80279214, 0.00144975], + >>> # [-0.16265212, 0.84155098, 0.62008629, 0.79126072], + >>> # [-0.59561850, 0.32797505, 0.80279214, 0.00144975], + >>> # [-0.16265212, 0.84155098, 0.62008629, 0.79126072]]) + >>> # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + >>> # [5, 4, 5, 4]) + >>> # Tensor(shape=[4, 1], dtype=float64, place=Place(gpu:0), stop_gradient=True, + >>> # [[104.27437027], + >>> # [113.40243782], + >>> # [104.27437027], + >>> # [113.40243782]]) + >>> # Tensor(shape=[4, 4], dtype=float64, place=Place(gpu:0), stop_gradient=True, + >>> # [[0.00000000, 0.00000000, 0.01210039, 0.00000000], + >>> # [0.00000000, 0.96152674, 0.00000067, 0.03847257], + >>> # [0.00000000, 0.00000000, 0.01210039, 0.00000000], + >>> # [0.00000000, 0.96152674, 0.00000067, 0.03847257]]) + >>> # cat log/workerlog.1 + >>> # Tensor(shape=[4, 8], dtype=float64, place=Place(gpu:1), stop_gradient=True, + >>> # [[-0.34913275, -0.35180883, -0.53976657, -0.75234331, 0.70534995, + >>> # 0.87157838, 0.31064437, 0.19537700], + >>> # [-0.63941012, -0.05631600, -0.02561853, 0.09363013, 0.56571130, + >>> # 0.13611246, 0.08849565, 0.39219619], + >>> # [-0.34913275, -0.35180883, -0.53976657, -0.75234331, 0.70534995, + >>> # 0.87157838, 0.31064437, 0.19537700], + >>> # [-0.63941012, -0.05631600, -0.02561853, 0.09363013, 0.56571130, + >>> # 0.13611246, 0.08849565, 0.39219619]]) + >>> # Tensor(shape=[4], dtype=int64, place=Place(gpu:1), stop_gradient=True, + >>> # [5, 4, 5, 4]) + >>> # Tensor(shape=[4, 1], dtype=float64, place=Place(gpu:1), stop_gradient=True, + >>> # [[104.27437027], + >>> # [113.40243782], + >>> # [104.27437027], + >>> # [113.40243782]]) + >>> # Tensor(shape=[4, 8], dtype=float64, place=Place(gpu:1), stop_gradient=True, + >>> # [[0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00002368, 0.98787593, + >>> # 0.00000000, 0.00000000], + >>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000002, 0.00000000, + >>> # 0.00000000, 0.00000000], + >>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00002368, 0.98787593, + >>> # 0.00000000, 0.00000000], + >>> # [0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000002, 0.00000000, + >>> # 0.00000000, 0.00000000]]) + """ assert reduction in ['mean', 'sum', 'none', None] @@ -2422,15 +2472,16 @@ def softmax_with_cross_entropy( Examples: .. code-block:: python - import paddle + >>> import paddle + + >>> logits = paddle.to_tensor([0.4, 0.6, 0.9], dtype="float32") + >>> label = paddle.to_tensor([1], dtype="int64") - logits = paddle.to_tensor([0.4, 0.6, 0.9], dtype="float32") - label = paddle.to_tensor([1], dtype="int64") + >>> out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label) + >>> print(out) + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [1.15328646]) - out = paddle.nn.functional.softmax_with_cross_entropy(logits=logits, label=label) - print(out) - # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [1.15328646]) """ return fluid_softmax_with_cross_entropy( logits, @@ -2622,50 +2673,50 @@ def cross_entropy( Examples: .. code-block:: python - # hard labels - import paddle - paddle.seed(99999) - N=100 - C=200 - reduction='mean' - input = paddle.rand([N, C], dtype='float64') - label = paddle.randint(0, C, shape=[N], dtype='int64') - weight = paddle.rand([C], dtype='float64') - - cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=weight, reduction=reduction) - dy_ret = cross_entropy_loss( - input, - label) - print(dy_ret) - # Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # 5.34043430) + >>> # hard labels + >>> import paddle + >>> paddle.seed(99999) + >>> N=100 + >>> C=200 + >>> reduction='mean' + >>> input = paddle.rand([N, C], dtype='float64') + >>> label = paddle.randint(0, C, shape=[N], dtype='int64') + >>> weight = paddle.rand([C], dtype='float64') + + >>> cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( + ... weight=weight, reduction=reduction) + >>> dy_ret = cross_entropy_loss( + ... input, + ... label) + >>> print(dy_ret) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, + 5.35419278) .. code-block:: python - # soft labels - import paddle - paddle.seed(99999) - axis = -1 - ignore_index = -100 - N = 4 - C = 3 - shape = [N, C] - reduction='mean' - weight = None - logits = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) - labels = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) - labels /= paddle.sum(labels, axis=axis, keepdim=True) - paddle_loss_mean = paddle.nn.functional.cross_entropy( - logits, - labels, - soft_label=True, - axis=axis, - weight=weight, - reduction=reduction) - print(paddle_loss_mean) - # Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True, - # 1.11043464) + >>> # soft labels + >>> import paddle + >>> paddle.seed(99999) + >>> axis = -1 + >>> ignore_index = -100 + >>> N = 4 + >>> C = 3 + >>> shape = [N, C] + >>> reduction='mean' + >>> weight = None + >>> logits = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) + >>> labels = paddle.uniform(shape, dtype='float64', min=0.1, max=1.0) + >>> labels /= paddle.sum(labels, axis=axis, keepdim=True) + >>> paddle_loss_mean = paddle.nn.functional.cross_entropy( + ... logits, + ... labels, + ... soft_label=True, + ... axis=axis, + ... weight=weight, + ... reduction=reduction) + >>> print(paddle_loss_mean) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, + 1.12801195) """ @@ -3022,15 +3073,17 @@ def sigmoid_focal_loss( .. code-block:: python - import paddle + >>> import paddle - logit = paddle.to_tensor([[0.97, 0.91, 0.03], [0.55, 0.43, 0.71]], dtype='float32') - label = paddle.to_tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype='float32') - one = paddle.to_tensor([1.], dtype='float32') - fg_label = paddle.greater_equal(label, one) - fg_num = paddle.sum(paddle.cast(fg_label, dtype='float32')) - output = paddle.nn.functional.sigmoid_focal_loss(logit, label, normalizer=fg_num) - print(output) # 0.65782464 + >>> logit = paddle.to_tensor([[0.97, 0.91, 0.03], [0.55, 0.43, 0.71]], dtype='float32') + >>> label = paddle.to_tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype='float32') + >>> one = paddle.to_tensor([1.], dtype='float32') + >>> fg_label = paddle.greater_equal(label, one) + >>> fg_num = paddle.sum(paddle.cast(fg_label, dtype='float32')) + >>> output = paddle.nn.functional.sigmoid_focal_loss(logit, label, normalizer=fg_num) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.65782464) """ if reduction not in ['sum', 'mean', 'none']: @@ -3177,17 +3230,20 @@ def multi_label_soft_margin_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) - # label elements in {1., -1.} - label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) - loss = F.multi_label_soft_margin_loss(input, label, reduction='none') - print(loss) - # Tensor([3.49625897, 0.71111226, 0.43989015]) - loss = F.multi_label_soft_margin_loss(input, label, reduction='mean') - print(loss) - # Tensor(1.54908717) + >>> import paddle + >>> import paddle.nn.functional as F + >>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) + >>> # label elements in {1., -1.} + >>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) + >>> loss = F.multi_label_soft_margin_loss(input, label, reduction='none') + >>> print(loss) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [3.49625897, 0.71111226, 0.43989015]) + >>> loss = F.multi_label_soft_margin_loss(input, label, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 1.54908717) + """ if reduction not in ['sum', 'mean', 'none']: raise ValueError( @@ -3296,22 +3352,24 @@ def hinge_embedding_loss(input, label, margin=1.0, reduction='mean', name=None): Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) - # label elements in {1., -1.} - label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) + >>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32) + >>> # label elements in {1., -1.} + >>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32) - loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='none') - print(loss) - # Tensor([[0., -2., 0.], - # [0., -1., 2.], - # [1., 1., 1.]]) + >>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='none') + >>> print(loss) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0., -2., 0.], + [ 0., -1., 2.], + [ 1., 1., 1.]]) + >>> loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.22222222) - loss = F.hinge_embedding_loss(input, label, margin=1.0, reduction='mean') - print(loss) - # Tensor(0.22222222) """ if reduction not in ['sum', 'mean', 'none']: @@ -3386,20 +3444,24 @@ def cosine_embedding_loss( Examples: .. code-block:: python - import paddle - - input1 = paddle.to_tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]], 'float32') - input2 = paddle.to_tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]], 'float32') - label = paddle.to_tensor([1, -1], 'int64') - - output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='mean') - print(output) # 0.21155193 - - output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='sum') - print(output) # 0.42310387 - - output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='none') - print(output) # [0.42310387, 0. ] + >>> import paddle + + >>> input1 = paddle.to_tensor([[1.6, 1.2, -0.5], [3.2, 2.6, -5.8]], 'float32') + >>> input2 = paddle.to_tensor([[0.5, 0.5, -1.8], [2.3, -1.4, 1.1]], 'float32') + >>> label = paddle.to_tensor([1, -1], 'int64') + + >>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='mean') + >>> print(output) # 0.21155193 + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.21155193) + >>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='sum') + >>> print(output) # 0.42310387 + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.42310387) + >>> output = paddle.nn.functional.cosine_embedding_loss(input1, input2, label, margin=0.5, reduction='none') + >>> print(output) # [0.42310387, 0. ] + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [0.42310387, 0. ]) """ if len(label.shape) != 1: @@ -3519,20 +3581,21 @@ def triplet_margin_with_distance_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) - positive= paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32) - negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32) - loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='none') - print(loss) - # Tensor([0. , 0.57496738, 0. ]) + >>> import paddle + >>> import paddle.nn.functional as F + >>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) + >>> positive = paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32) + >>> negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32) + >>> loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='none') + >>> print(loss) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 0.57496595, 0. ]) - loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='mean') - print(loss) - # Tensor(0.19165580) + >>> loss = F.triplet_margin_with_distance_loss(input, positive, negative, margin=1.0, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.19165532) """ if reduction not in ['sum', 'mean', 'none']: @@ -3669,20 +3732,21 @@ def triplet_margin_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) - positive= paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32) - negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32) - loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='none') - print(loss) - # Tensor([0. , 0.57496738, 0. ]) + >>> import paddle + >>> import paddle.nn.functional as F + >>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) + >>> positive = paddle.to_tensor([[5, 1, 2], [3, 2, 1], [3, -1, 1]], dtype=paddle.float32) + >>> negative = paddle.to_tensor([[2, 1, -3], [1, 1, -1], [4, -2, 1]], dtype=paddle.float32) + >>> loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='none') + >>> print(loss) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 0.57496595, 0. ]) - loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='mean') - print(loss) - # Tensor(0.19165580) + >>> loss = F.triplet_margin_loss(input, positive, negative, margin=1.0, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.19165532) """ if reduction not in ['sum', 'mean', 'none']: @@ -3789,13 +3853,15 @@ def multi_margin_loss( Examples: .. code-block:: python - import paddle - import paddle.nn.functional as F + >>> import paddle + >>> import paddle.nn.functional as F - input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) - label = paddle.to_tensor([1, 2, 1], dtype=paddle.int32) - loss = F.multi_margin_loss(input, label, margin=1.0, reduction='none') - print(loss) + >>> input = paddle.to_tensor([[1, 5, 3], [0, 3, 2], [1, 4, 1]], dtype=paddle.float32) + >>> label = paddle.to_tensor([1, 2, 1], dtype=paddle.int32) + >>> loss = F.multi_margin_loss(input, label, margin=1.0, reduction='none') + >>> print(loss) + Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, + [0. , 0.66666663, 0. ]) """ if reduction not in ['sum', 'mean', 'none']: @@ -3895,27 +3961,28 @@ def soft_margin_loss(input, label, reduction='mean', name=None): Examples: .. code-block:: python - import paddle - - input = paddle.to_tensor([[0.5, 0.6, 0.7],[0.3, 0.5, 0.2]], 'float32') - label = paddle.to_tensor([[1.0, -1.0, 1.0],[-1.0, 1.0, 1.0]], 'float32') - output = paddle.nn.functional.soft_margin_loss(input, label) - print(output) - # Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # 0.64022040) - - input = paddle.uniform(shape=(5, 5), dtype="float32", min=0.1, max=0.8) - label = paddle.randint(0, 2, shape=(5, 5), dtype="int64") - label[label==0]=-1 - - output = paddle.nn.functional.soft_margin_loss(input, label, reduction='none') - print(output) - # Tensor(shape=[5, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[1.09917796, 0.52613139, 0.56263304, 0.82736146, 0.38776723], - # [1.07179427, 1.11924267, 0.49877715, 1.10026348, 0.46184641], - # [0.84367639, 0.74795729, 0.44629076, 0.55123353, 0.77659678], - # [0.39465919, 0.76651484, 0.54485321, 0.76609844, 0.77166790], - # [0.51283568, 0.84757161, 0.78913331, 1.05268764, 0.45318675]]) + >>> import paddle + >>> paddle.seed(2023) + + >>> input = paddle.to_tensor([[0.5, 0.6, 0.7],[0.3, 0.5, 0.2]], 'float32') + >>> label = paddle.to_tensor([[1.0, -1.0, 1.0],[-1.0, 1.0, 1.0]], 'float32') + >>> output = paddle.nn.functional.soft_margin_loss(input, label) + >>> print(output) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.64022040) + + >>> input = paddle.uniform(shape=(5, 5), dtype="float32", min=0.1, max=0.8) + >>> label = paddle.randint(0, 2, shape=(5, 5), dtype="int64") + >>> label[label==0] = -1 + + >>> output = paddle.nn.functional.soft_margin_loss(input, label, reduction='none') + >>> print(output) + Tensor(shape=[5, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1.10725629, 0.48778144, 0.56217247, 1.12581408, 0.51430041], + [0.90375793, 0.37761253, 0.43007556, 0.95089805, 0.43288314], + [1.16043591, 0.63015938, 0.51362717, 0.43617544, 0.57783306], + [0.81927848, 0.52558368, 0.59713912, 0.83100700, 0.50811619], + [0.82684207, 1.02064908, 0.50296998, 1.13461733, 0.93222517]]) """ if reduction not in ['sum', 'mean', 'none']: @@ -4007,18 +4074,27 @@ def gaussian_nll_loss( Examples:: .. code-block:: python - import paddle - import paddle.nn.functional as F - - input = paddle.randn([5, 2], dtype=paddle.float32) - label = paddle.randn([5, 2], dtype=paddle.float32) - variance = paddle.ones([5, 2], dtype=paddle.float32) - - loss = F.gaussian_nll_loss(input, label, variance, reduction='none') - print(loss) - - loss = F.gaussian_nll_loss(input, label, variance, reduction='mean') - print(loss) + >>> import paddle + >>> import paddle.nn.functional as F + >>> paddle.seed(2023) + + >>> input = paddle.randn([5, 2], dtype=paddle.float32) + >>> label = paddle.randn([5, 2], dtype=paddle.float32) + >>> variance = paddle.ones([5, 2], dtype=paddle.float32) + + >>> loss = F.gaussian_nll_loss(input, label, variance, reduction='none') + >>> print(loss) + Tensor(shape=[5, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.21808575, 1.43013096], + [1.05245590, 0.00394560], + [1.20861185, 0.00000062], + [0.56946373, 0.73300570], + [0.37142906, 0.12038800]]) + + >>> loss = F.gaussian_nll_loss(input, label, variance, reduction='mean') + >>> print(loss) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + 0.57075173) Note: The clamping of ``variance`` is ignored with respect to autograd, and so the From 9d1836625c957c4da2a06e9219882a12764484ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A3=9E=E7=8C=AA?= <106524776+ooooo-create@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:30:07 +0800 Subject: [PATCH 138/194] [xdoctest][task 268] reformat example code with google style in `/incubate/nn/layer/fused_transformer.py` (#56965) * [Doctest]fix No.268, test=docs_preview * Apply suggestions from code review --------- Co-authored-by: Nyakku Shigure --- .../incubate/nn/layer/fused_transformer.py | 202 ++++++++++-------- 1 file changed, 109 insertions(+), 93 deletions(-) diff --git a/python/paddle/incubate/nn/layer/fused_transformer.py b/python/paddle/incubate/nn/layer/fused_transformer.py index b70517156fe6f..a3f3ea1d83af4 100644 --- a/python/paddle/incubate/nn/layer/fused_transformer.py +++ b/python/paddle/incubate/nn/layer/fused_transformer.py @@ -100,14 +100,17 @@ class FusedBiasDropoutResidualLayerNorm(Layer): .. code-block:: python - # required: gpu - import paddle - # input: [batch_size, seq_len, embed_dim] - x = paddle.rand((2, 4, 128)) - # residual: [batch_size, seq_len, embed_dim] - residual = paddle.rand((2, 4, 128)) - fused_bias_dropout_residual_ln = paddle.incubate.nn.FusedBiasDropoutResidualLayerNorm(128) - output = fused_bias_dropout_residual_ln(x, residual) # [2, 4, 128] + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> # input: [batch_size, seq_len, embed_dim] + >>> x = paddle.rand((2, 4, 128)) + >>> # residual: [batch_size, seq_len, embed_dim] + >>> residual = paddle.rand((2, 4, 128)) + >>> fused_bias_dropout_residual_ln = paddle.incubate.nn.FusedBiasDropoutResidualLayerNorm(128) + >>> output = fused_bias_dropout_residual_ln(x, residual) + >>> print(output.shape) + [2, 4, 128] """ def __init__( @@ -259,14 +262,17 @@ class FusedMultiHeadAttention(Layer): .. code-block:: python - # required: gpu - import paddle - # input: [batch_size, sequence_length, embed_dim] - query = paddle.rand((2, 4, 128)) - # self attention mask: [batch_size, num_heads, query_len, query_len] - attn_mask = paddle.rand((2, 2, 4, 4)) - multi_head_attn = paddle.incubate.nn.FusedMultiHeadAttention(128, 2) - output = multi_head_attn(query, None, None, attn_mask=attn_mask) # [2, 4, 128] + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> # input: [batch_size, sequence_length, embed_dim] + >>> query = paddle.rand((2, 4, 128)) + >>> # self attention mask: [batch_size, num_heads, query_len, query_len] + >>> attn_mask = paddle.rand((2, 2, 4, 4)) + >>> multi_head_attn = paddle.incubate.nn.FusedMultiHeadAttention(128, 2) + >>> output = multi_head_attn(query, None, None, attn_mask=attn_mask) + >>> print(output.shape) + [2, 4, 128] """ def __init__( @@ -545,15 +551,16 @@ class FusedFeedForward(Layer): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn import FusedFeedForward + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn import FusedFeedForward + >>> paddle.device.set_device('gpu') - fused_feedforward_layer = FusedFeedForward(8, 8) - x = paddle.rand((1, 8, 8)) - out = fused_feedforward_layer(x) - print(out.shape) - # [1, 8, 8] + >>> fused_feedforward_layer = FusedFeedForward(8, 8) + >>> x = paddle.rand((1, 8, 8)) + >>> out = fused_feedforward_layer(x) + >>> print(out.shape) + [1, 8, 8] """ def __init__( @@ -768,16 +775,19 @@ class FusedTransformerEncoderLayer(Layer): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn import FusedTransformerEncoderLayer + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn import FusedTransformerEncoderLayer + >>> paddle.device.set_device('gpu') - # encoder input: [batch_size, src_len, d_model] - enc_input = paddle.rand((2, 4, 128)) - # self attention mask: [batch_size, n_head, src_len, src_len] - attn_mask = paddle.rand((2, 2, 4, 4)) - encoder_layer = FusedTransformerEncoderLayer(128, 2, 512) - enc_output = encoder_layer(enc_input, attn_mask) # [2, 4, 128] + >>> # encoder input: [batch_size, src_len, d_model] + >>> enc_input = paddle.rand((2, 4, 128)) + >>> # self attention mask: [batch_size, n_head, src_len, src_len] + >>> attn_mask = paddle.rand((2, 2, 4, 4)) + >>> encoder_layer = FusedTransformerEncoderLayer(128, 2, 512) + >>> enc_output = encoder_layer(enc_input, attn_mask) + >>> print(enc_output.shape) + [2, 4, 128] """ @@ -973,25 +983,27 @@ class FusedTransformer(Layer): .. code-block:: python - import paddle - from paddle.nn import Transformer - - # src: [batch_size, tgt_len, d_model] - enc_input = paddle.rand((2, 4, 128)) - # tgt: [batch_size, src_len, d_model] - dec_input = paddle.rand((2, 6, 128)) - # src_mask: [batch_size, n_head, src_len, src_len] - enc_self_attn_mask = paddle.rand((2, 2, 4, 4)) - # tgt_mask: [batch_size, n_head, tgt_len, tgt_len] - dec_self_attn_mask = paddle.rand((2, 2, 6, 6)) - # memory_mask: [batch_size, n_head, tgt_len, src_len] - cross_attn_mask = paddle.rand((2, 2, 6, 4)) - transformer = Transformer(128, 2, 4, 4, 512) - output = transformer(enc_input, - dec_input, - enc_self_attn_mask, - dec_self_attn_mask, - cross_attn_mask) # [2, 6, 128] + >>> import paddle + >>> from paddle.nn import Transformer + + >>> # src: [batch_size, tgt_len, d_model] + >>> enc_input = paddle.rand((2, 4, 128)) + >>> # tgt: [batch_size, src_len, d_model] + >>> dec_input = paddle.rand((2, 6, 128)) + >>> # src_mask: [batch_size, n_head, src_len, src_len] + >>> enc_self_attn_mask = paddle.rand((2, 2, 4, 4)) + >>> # tgt_mask: [batch_size, n_head, tgt_len, tgt_len] + >>> dec_self_attn_mask = paddle.rand((2, 2, 6, 6)) + >>> # memory_mask: [batch_size, n_head, tgt_len, src_len] + >>> cross_attn_mask = paddle.rand((2, 2, 6, 4)) + >>> transformer = Transformer(128, 2, 4, 4, 512) + >>> output = transformer(enc_input, + ... dec_input, + ... enc_self_attn_mask, + ... dec_self_attn_mask, + ... cross_attn_mask) + >>> print(output.shape) + [2, 6, 128] """ def __init__( @@ -1026,37 +1038,38 @@ class FusedMultiTransformer(Layer): .. code-block:: python - if pre_layer_norm: - out = layer_norm(x) - out = qkv_linear(out) + qkv_bias - else: - out = qkv_linear(x) + qkv_bias - out = transpose(out, perm=[2, 0, 3, 1, 4]) - # extract q, k and v from out. - q = out[0:1, ::] - k = out[1:2, ::] - v = out[2:3, ::] - out = q * k^t - out = attn_mask + out - out = softmax(out) - out = dropout(out) - out = out * v - out = transpose(out, perm=[0, 2, 1, 3]) - out = linear(out) - if pre_layer_norm: - out = x + dropout(out + bias) - else: - out = layer_norm(x + dropout(out + bias)) - - residual = out; - if pre_layer_norm: - out = ffn_layer_norm(out) - out = ffn1_linear(out) - out = dropout(activation(out + ffn1_bias)) - out = ffn2_linear(out) - out = residual + dropout(out + ffn2_bias) - if not pre_layer_norm: - out = ffn_layer_norm(out) + >>> # doctest: +SKIP('This is not an example') + >>> if pre_layer_norm: + ... out = layer_norm(x) + ... out = qkv_linear(out) + qkv_bias + ... else: + ... out = qkv_linear(x) + qkv_bias + >>> out = transpose(out, perm=[2, 0, 3, 1, 4]) + >>> # extract q, k and v from out. + >>> q = out[0:1, ::] + >>> k = out[1:2, ::] + >>> v = out[2:3, ::] + >>> out = q * k^t + >>> out = attn_mask + out + >>> out = softmax(out) + >>> out = dropout(out) + >>> out = out * v + >>> out = transpose(out, perm=[0, 2, 1, 3]) + >>> out = linear(out) + >>> if pre_layer_norm: + ... out = x + dropout(out + bias) + ... else: + ... out = layer_norm(x + dropout(out + bias)) + + >>> residual = out; + >>> if pre_layer_norm: + ... out = ffn_layer_norm(out) + >>> out = ffn1_linear(out) + >>> out = dropout(activation(out + ffn1_bias)) + >>> out = ffn2_linear(out) + >>> out = residual + dropout(out + ffn2_bias) + >>> if not pre_layer_norm: + ... out = ffn_layer_norm(out) Parameters: embed_dim (int): The expected feature size in the input and output. @@ -1166,16 +1179,19 @@ class FusedMultiTransformer(Layer): .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn import FusedMultiTransformer - - # encoder input: [batch_size, src_len, d_model] - enc_input = paddle.rand((2, 4, 128)) - # self attention mask: [batch_size, 1, src_len, src_len] - attn_mask = paddle.rand((2, 1, 4, 4)) - encoder_layers = FusedMultiTransformer(128, 2, 512, num_layers=1) - enc_output = encoder_layers(enc_input, attn_mask) # [2, 4, 128] + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> from paddle.incubate.nn import FusedMultiTransformer + >>> paddle.device.set_device('gpu') + + >>> # encoder input: [batch_size, src_len, d_model] + >>> enc_input = paddle.rand((2, 4, 128)) + >>> # self attention mask: [batch_size, 1, src_len, src_len] + >>> attn_mask = paddle.rand((2, 1, 4, 4)) + >>> encoder_layers = FusedMultiTransformer(128, 2, 512, num_layers=1) + >>> enc_output = encoder_layers(enc_input, attn_mask) + >>> print(enc_output.shape) + [2, 4, 128] """ def __init__( From 7daffbf88d342c289239cf49d00d4fe8f46147ff Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:30:53 +0800 Subject: [PATCH 139/194] =?UTF-8?q?[xdoctest]=20reformat=20example=20code?= =?UTF-8?q?=20with=20google=20style=20in=20No.297=E3=80=81298=E3=80=81302?= =?UTF-8?q?=20(#56861)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 更改相关文件 * Update ir.py * 恢复相关文件 * Update ir.py * Delete python/paddle/incubate/optimizer/modelaverage.py * Delete modelaverage.py * 尝试恢复文件 * Revert "尝试恢复文件" This reverts commit 8a263cfd4642614a48a30f648c61fc801331e550. * Revert "恢复相关文件" This reverts commit 24249b8191fb3901681ffa9d0e1ad79ef43454de. * Revert "Revert "尝试恢复文件"" This reverts commit 1b833d623770a851f202c68fff51e77723121a9d. * Revert "Revert "Revert "尝试恢复文件""" This reverts commit 64b3a816d1f0fef5ee9100480b8354749005a463. * Revert "Delete python/paddle/incubate/optimizer/modelaverage.py" This reverts commit 61986296bf48f7b9bef878bed6890c5dc2971481. * Revert "更改相关文件" This reverts commit a5ba675a948534401247b779d6a0fba0581d0628. * Apply suggestions from code review --------- Co-authored-by: Nyakku Shigure --- .../incubate/optimizer/functional/bfgs.py | 72 +++++++++---------- .../incubate/optimizer/functional/lbfgs.py | 72 +++++++++---------- python/paddle/incubate/passes/ir.py | 20 +++--- 3 files changed, 82 insertions(+), 82 deletions(-) diff --git a/python/paddle/incubate/optimizer/functional/bfgs.py b/python/paddle/incubate/optimizer/functional/bfgs.py index 9d98460e03c3c..bc244d9c19da1 100644 --- a/python/paddle/incubate/optimizer/functional/bfgs.py +++ b/python/paddle/incubate/optimizer/functional/bfgs.py @@ -81,46 +81,46 @@ def minimize_bfgs( .. code-block:: python :name: code-example1 - # Example1: 1D Grid Parameters - import paddle - # Randomly simulate a batch of input data - inputs = paddle. normal(shape=(100, 1)) - labels = inputs * 2.0 - # define the loss function - def loss(w): - y = w * inputs - return paddle.nn.functional.square_error_cost(y, labels).mean() - # Initialize weight parameters - w = paddle.normal(shape=(1,)) - # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters - for epoch in range(0, 10): - # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight - w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] - # Use paddle.assign to update parameters in place - paddle. assign(w_update, w) + >>> # Example1: 1D Grid Parameters + >>> import paddle + >>> # Randomly simulate a batch of input data + >>> inputs = paddle. normal(shape=(100, 1)) + >>> labels = inputs * 2.0 + >>> # define the loss function + >>> def loss(w): + ... y = w * inputs + ... return paddle.nn.functional.square_error_cost(y, labels).mean() + >>> # Initialize weight parameters + >>> w = paddle.normal(shape=(1,)) + >>> # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters + >>> for epoch in range(0, 10): + ... # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight + ... w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] + ... # Use paddle.assign to update parameters in place + ... paddle. assign(w_update, w) .. code-block:: python :name: code-example2 - # Example2: Multidimensional Grid Parameters - import paddle - def flatten(x): - return x. flatten() - def unflatten(x): - return x.reshape((2,2)) - # Assume the network parameters are more than one dimension - def net(x): - assert len(x.shape) > 1 - return x.square().mean() - # function to be optimized - def bfgs_f(flatten_x): - return net(unflatten(flatten_x)) - x = paddle.rand([2,2]) - for i in range(0, 10): - # Flatten x before using minimize_bfgs - x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] - # unflatten x_update, then update parameters - paddle. assign(unflatten(x_update), x) + >>> # Example2: Multidimensional Grid Parameters + >>> import paddle + >>> def flatten(x): + ... return x. flatten() + >>> def unflatten(x): + ... return x.reshape((2,2)) + >>> # Assume the network parameters are more than one dimension + >>> def net(x): + ... assert len(x.shape) > 1 + ... return x.square().mean() + >>> # function to be optimized + >>> def bfgs_f(flatten_x): + ... return net(unflatten(flatten_x)) + >>> x = paddle.rand([2,2]) + >>> for i in range(0, 10): + ... # Flatten x before using minimize_bfgs + ... x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] + ... # unflatten x_update, then update parameters + ... paddle.assign(unflatten(x_update), x) """ if dtype not in ['float32', 'float64']: diff --git a/python/paddle/incubate/optimizer/functional/lbfgs.py b/python/paddle/incubate/optimizer/functional/lbfgs.py index af30efe44a8da..fc482e4ca18b5 100644 --- a/python/paddle/incubate/optimizer/functional/lbfgs.py +++ b/python/paddle/incubate/optimizer/functional/lbfgs.py @@ -82,46 +82,46 @@ def minimize_lbfgs( .. code-block:: python :name: code-example1 - # Example1: 1D Grid Parameters - import paddle - # Randomly simulate a batch of input data - inputs = paddle. normal(shape=(100, 1)) - labels = inputs * 2.0 - # define the loss function - def loss(w): - y = w * inputs - return paddle.nn.functional.square_error_cost(y, labels).mean() - # Initialize weight parameters - w = paddle.normal(shape=(1,)) - # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters - for epoch in range(0, 10): - # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight - w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] - # Use paddle.assign to update parameters in place - paddle. assign(w_update, w) + >>> # Example1: 1D Grid Parameters + >>> import paddle + >>> # Randomly simulate a batch of input data + >>> inputs = paddle. normal(shape=(100, 1)) + >>> labels = inputs * 2.0 + >>> # define the loss function + >>> def loss(w): + ... y = w * inputs + ... return paddle.nn.functional.square_error_cost(y, labels).mean() + >>> # Initialize weight parameters + >>> w = paddle.normal(shape=(1,)) + >>> # Call the bfgs method to solve the weight that makes the loss the smallest, and update the parameters + >>> for epoch in range(0, 10): + ... # Call the bfgs method to optimize the loss, note that the third parameter returned represents the weight + ... w_update = paddle.incubate.optimizer.functional.minimize_bfgs(loss, w)[2] + ... # Use paddle.assign to update parameters in place + ... paddle.assign(w_update, w) .. code-block:: python :name: code-example2 - # Example2: Multidimensional Grid Parameters - import paddle - def flatten(x): - return x. flatten() - def unflatten(x): - return x.reshape((2,2)) - # Assume the network parameters are more than one dimension - def net(x): - assert len(x.shape) > 1 - return x.square().mean() - # function to be optimized - def bfgs_f(flatten_x): - return net(unflatten(flatten_x)) - x = paddle.rand([2,2]) - for i in range(0, 10): - # Flatten x before using minimize_bfgs - x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] - # unflatten x_update, then update parameters - paddle. assign(unflatten(x_update), x) + >>> # Example2: Multidimensional Grid Parameters + >>> import paddle + >>> def flatten(x): + ... return x. flatten() + >>> def unflatten(x): + ... return x.reshape((2,2)) + >>> # Assume the network parameters are more than one dimension + >>> def net(x): + ... assert len(x.shape) > 1 + ... return x.square().mean() + >>> # function to be optimized + >>> def bfgs_f(flatten_x): + ... return net(unflatten(flatten_x)) + >>> x = paddle.rand([2,2]) + >>> for i in range(0, 10): + ... # Flatten x before using minimize_bfgs + ... x_update = paddle.incubate.optimizer.functional.minimize_bfgs(bfgs_f, flatten(x))[2] + ... # unflatten x_update, then update parameters + ... paddle.assign(unflatten(x_update), x) """ if dtype not in ['float32', 'float64']: diff --git a/python/paddle/incubate/passes/ir.py b/python/paddle/incubate/passes/ir.py index 0e292e51a0aea..c657f20abcb99 100644 --- a/python/paddle/incubate/passes/ir.py +++ b/python/paddle/incubate/passes/ir.py @@ -469,16 +469,16 @@ def RegisterPass(function=None, input_specs={}): Examples: .. code-block:: python - import paddle - from paddle.fluid.ir import RegisterPass - - @RegisterPass - def multi_add_to_addn(): - def pattern(x, y, z): - return paddle.add(paddle.add(x, y), z) - def replace(x, y, z): - return paddle.add_n([x, y, z]) - return pattern, replace + >>> import paddle + >>> from paddle.fluid.ir import RegisterPass + + >>> @RegisterPass + >>> def multi_add_to_addn(): + ... def pattern(x, y, z): + ... return paddle.add(paddle.add(x, y), z) + ... def replace(x, y, z): + ... return paddle.add_n([x, y, z]) + ... return pattern, replace """ def _is_pass_pair(check_pair): From 0dc6efaaa728d28f7990a84d01773b6cc8ea4293 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Wed, 6 Sep 2023 10:31:57 +0800 Subject: [PATCH 140/194] [NewIR]Support Build(GroupPtr) Logic in NewIRCompiler and Add UT (#56960) * [NewIR]Support Build(GroupOps) in NewIRCompiler and Add UT * fix unittest --- paddle/cinn/hlir/framework/new_ir/group.h | 25 ++++--- .../hlir/framework/new_ir/op_lowering_impl.cc | 26 ++++--- paddle/cinn/hlir/framework/new_ir/utils.cc | 17 ++--- paddle/cinn/hlir/framework/new_ir/utils.h | 7 +- paddle/cinn/hlir/framework/new_ir_compiler.cc | 35 ++++----- paddle/cinn/hlir/framework/new_ir_compiler.h | 4 +- test/cpp/ir/cinn/new_ir_compiler_test.cc | 74 ++++++++++++++++--- 7 files changed, 120 insertions(+), 68 deletions(-) diff --git a/paddle/cinn/hlir/framework/new_ir/group.h b/paddle/cinn/hlir/framework/new_ir/group.h index 2462fb8c4ce91..b62c315873c70 100644 --- a/paddle/cinn/hlir/framework/new_ir/group.h +++ b/paddle/cinn/hlir/framework/new_ir/group.h @@ -16,6 +16,7 @@ #include #include +#include "paddle/cinn/hlir/framework/new_ir/utils.h" #include "paddle/cinn/hlir/framework/op.h" #include "paddle/ir/core/operation.h" @@ -30,20 +31,26 @@ struct Group { public: explicit Group(const std::vector<::ir::Operation*>& group_ops) : ops(group_ops) { - op_pattern_kind = OpPatternKind::kElementWise; - fn_name = "fn_"; - for (auto& op : group_ops) { - fn_name += "_" + op->name(); - } + Initialize(); + } + + explicit Group(std::initializer_list<::ir::Operation*> group_ops) + : ops(group_ops) { + Initialize(); } + int group_id; + std::string fn_name; + OpPatternKind op_pattern_kind; std::vector<::ir::Operation*> ops; std::vector input_names; std::vector output_names; - int group_id; - // FIXME(Aurelius84): This should be refactored with CinnGroupOp - OpPatternKind op_pattern_kind; - std::string fn_name; + + private: + void Initialize() { + op_pattern_kind = OpPatternKind::kElementWise; + fn_name = CompatibleInfo::GroupOpsName(ops); + } }; } // namespace newir diff --git a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc index 040ea9cf87c98..d291aba2e406e 100644 --- a/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc +++ b/paddle/cinn/hlir/framework/new_ir/op_lowering_impl.cc @@ -43,7 +43,7 @@ ir::Tensor GetTensor(const ::ir::Value& value) { auto type_info = value.type().dyn_cast(); auto in_shape = phi::vectorize(type_info.dims()); auto dtype = type_info.dtype(); - std::string input_id = CompatibleInfo::InputName(value); + std::string input_id = CompatibleInfo::ValueName(value); return lang::CreatePlaceHolder( in_shape, utils::ConvertIRType(dtype), input_id); } @@ -56,15 +56,16 @@ std::vector CollectInputTensor( for (auto& operand : op->operands()) { CHECK(operand); auto in_value = operand.source(); - ir::Tensor tensor; + VLOG(4) << "input tensor name: " << CompatibleInfo::ValueName(in_value); + // NOTE(Aurelius84): Need always to create placeholder for input tensor. + ir::Tensor tensor = details::GetTensor(in_value); if (!tensor_map->count(in_value)) { - tensor = details::GetTensor(in_value); // record tensor. (*tensor_map)[in_value] = tensor; // record func input args - if (func_args != nullptr) func_args->push_back(tensor); - } else { - tensor = tensor_map->at(in_value); + if (func_args != nullptr) { + func_args->push_back(tensor); + } } tensors.push_back(tensor); } @@ -76,7 +77,7 @@ void CollectOutputInfo(const ::ir::Operation* op, std::vector>* out_shapes) { auto op_results = op->results(); for (auto& out_value : op_results) { - std::string output_id = CompatibleInfo::OutputName(out_value); + std::string output_id = CompatibleInfo::ValueName(out_value); // group->output_names.push_back(output_id); auto type_info = out_value.type().dyn_cast(); @@ -265,11 +266,11 @@ std::vector OpLowererImpl::PostProcess( // output arg tensors group_func_arg_tensors->push_back(tensor); // output args + group->output_names.push_back(tensor->name); group_func_args.emplace_back(tensor->buffer, ir::Argument::IO::kOutput); arg_name_set.insert(tensor->buffer->name); } } - if (!done_op_schedule) { std::unordered_set args_set; for (auto arg : group_func_args) { @@ -329,6 +330,8 @@ std::vector OpLowererImpl::LowerOps( std::vector op_func_arg_tensors = details::CollectInputTensor(op, group_func_arg_tensors, tensor_map); + VLOG(4) << "input size:" << op_func_arg_tensors.size(); + std::string cinn_op_name = CompatibleInfo::OpName(*op); const hlir::framework::Operator* cinn_op = Operator::Get(cinn_op_name); auto op_impl = OpStrategy::SelectImpl(strategy[cinn_op]( @@ -348,6 +351,9 @@ std::vector OpLowererImpl::LowerOps( } } + VLOG(4) << "group_func_arg_tensors.size(): " + << group_func_arg_tensors->size(); + return func_bodies; } @@ -364,7 +370,7 @@ std::vector OpLowererImpl::DoOpLower( // set tensor name = operand hash name auto op_results = op->results(); for (const auto& result : op_results) { - std::string output_id = CompatibleInfo::OutputName(result); + std::string output_id = CompatibleInfo::ValueName(result); cinn_inputs.push_back(common::CINNValue(output_id)); } @@ -400,6 +406,8 @@ std::vector OpLowererImpl::DoOpLower( } } + VLOG(4) << "op_func_arg_tensors.size(): " << op_func_arg_tensors->size(); + // 2.Do lower std::string lower_fn_name = CompatibleInfo::OpFuncName(*op); std::vector funcs = lang::LowerVec(lower_fn_name, diff --git a/paddle/cinn/hlir/framework/new_ir/utils.cc b/paddle/cinn/hlir/framework/new_ir/utils.cc index 12b3783e7c85d..38bfcf05776e0 100644 --- a/paddle/cinn/hlir/framework/new_ir/utils.cc +++ b/paddle/cinn/hlir/framework/new_ir/utils.cc @@ -36,13 +36,8 @@ std::string CompatibleInfo::OpName(const ::ir::Operation& op) { return cinn_op_name; } -std::string CompatibleInfo::InputName(const ::ir::Value& value) { - return CompatibleInfo::kInputPrefix + - std::to_string(std::hash<::ir::Value>()(value)); -} - -std::string CompatibleInfo::OutputName(const ::ir::Value& value) { - return CompatibleInfo::kOutputPrefix + +std::string CompatibleInfo::ValueName(const ::ir::Value& value) { + return CompatibleInfo::kNamePrefix + std::to_string(std::hash<::ir::Value>()(value)); } @@ -55,10 +50,10 @@ std::string CompatibleInfo::OpFuncName(const ::ir::Operation& op) { std::string CompatibleInfo::GroupOpsName( const std::vector<::ir::Operation*>& ops) { - std::string name = "fn_"; + std::string name = "fn"; for (auto* op : ops) { std::string op_name = OpName(*op); - name += cinn::common::Context::Global().NewName(op_name); + name += "_" + cinn::common::Context::Global().NewName(op_name); } return name; } @@ -69,7 +64,7 @@ std::vector CompatibleInfo::InputNames(const ::ir::Operation& op, std::unordered_set repeat; for (int i = 0; i < op.num_operands(); ++i) { auto value = op.operand_source(i); - std::string name = CompatibleInfo::InputName(value); + std::string name = CompatibleInfo::ValueName(value); if (!allow_duplicate && repeat.count(name)) { continue; } @@ -84,7 +79,7 @@ std::vector CompatibleInfo::OutputNames( std::vector names; for (int i = 0; i < op.num_results(); ++i) { auto value = op.result(i); - std::string name = CompatibleInfo::OutputName(value); + std::string name = CompatibleInfo::ValueName(value); names.push_back(std::move(name)); } return names; diff --git a/paddle/cinn/hlir/framework/new_ir/utils.h b/paddle/cinn/hlir/framework/new_ir/utils.h index 7796899ce3482..4c437dd19ef8a 100644 --- a/paddle/cinn/hlir/framework/new_ir/utils.h +++ b/paddle/cinn/hlir/framework/new_ir/utils.h @@ -24,17 +24,14 @@ namespace framework { namespace newir { struct CompatibleInfo { - static constexpr char* kInputPrefix = "input_"; - static constexpr char* kOutputPrefix = "output_"; + static constexpr char* kNamePrefix = "var_"; // TODO(Aurelius): Need add name mapping logic in REGISTER_CINN_OP // macros or attempt to unify Op name with Paddle and CINN. static const std::unordered_map OP_NAMES; static std::string OpName(const ::ir::Operation& op); - static std::string InputName(const ::ir::Value& value); - - static std::string OutputName(const ::ir::Value& value); + static std::string ValueName(const ::ir::Value& value); static std::string OpFuncName(const ::ir::Operation& op); diff --git a/paddle/cinn/hlir/framework/new_ir_compiler.cc b/paddle/cinn/hlir/framework/new_ir_compiler.cc index f6954514ace6f..bcc7c0f1c2a05 100644 --- a/paddle/cinn/hlir/framework/new_ir_compiler.cc +++ b/paddle/cinn/hlir/framework/new_ir_compiler.cc @@ -35,7 +35,6 @@ std::unique_ptr NewIRCompiler::Build() { ++it) { std::vector<::ir::Operation*> ops = {*it}; groups.push_back(std::make_shared(ops)); - groups.back()->fn_name = CompatibleInfo::GroupOpsName(groups.back()->ops); } VLOG(4) << "Groups size: " << groups.size(); return std::move(Build(groups)); @@ -103,23 +102,20 @@ std::vector> NewIRCompiler::BuildInstructions( const std::vector& groups) { std::vector> instructions; for (int idx = 0; idx < groups.size(); ++idx) { - // TODO(Aurelius84): only support single op in groups - auto& op = *(groups[idx]->ops[0]); - auto& fn_name = groups[idx]->fn_name; - auto instr = std::unique_ptr( - new Instruction(target_, - scope_.get(), - CompatibleInfo::InputNames(op), - CompatibleInfo::OutputNames(op), - fn_name)); + auto instr = + std::unique_ptr(new Instruction(target_, + scope_.get(), + groups[idx]->input_names, + groups[idx]->output_names, + fn_name)); VLOG(1) << "Lookup kernel name: " << fn_name; auto* fn_ptr = compiler_->Lookup(fn_name); CHECK(fn_ptr); instr->SetLoweredFunc(reinterpret_cast(fn_ptr), fn_name); // As some instruction like reduce, will generate more than one kernel. // So try to find the rest kernel, if it exists. - // SetSubKernels(instr.get(), op_func_name); + // SetSubKernels(instr.get(), fn_name); instr->Finalize(); instructions.push_back(std::move(instr)); } @@ -131,16 +127,15 @@ std::shared_ptr BuildScope(const Target& target, std::unordered_set<::ir::Value> visited; auto scope = std::make_shared(); - auto create_var = [&](const std::string& name_prefix, ::ir::Value value) { + auto create_var = [&](::ir::Value value) { if (visited.count(value) > 0) return; visited.emplace(value); - std::string name = - name_prefix + std::to_string(std::hash<::ir::Value>()(value)); + std::string name = CompatibleInfo::ValueName(value); auto type_info = value.type().dyn_cast(); auto* var = scope->Var(name); auto& tensor = absl::get(*var); - // NOTE: can be replaced with phi::vectorized ? + std::vector shape; for (auto i = 0; i < type_info.dims().size(); ++i) { shape.push_back(Shape::dim_t(type_info.dims()[i])); @@ -150,14 +145,12 @@ std::shared_ptr BuildScope(const Target& target, }; for (auto it = program.block()->begin(); it != program.block()->end(); ++it) { - for (auto i = 0; i < (*it)->num_operands(); ++i) { - auto in_value = (*it)->operand_source(i); - create_var(CompatibleInfo::kInputPrefix, in_value); + for (auto& oprand : (*it)->operands()) { + create_var(oprand.source()); } - for (auto i = 0; i < (*it)->num_results(); ++i) { - auto out_value = (*it)->result(i); - create_var(CompatibleInfo::kOutputPrefix, out_value); + for (auto& result : (*it)->results()) { + create_var(result); } } return scope; diff --git a/paddle/cinn/hlir/framework/new_ir_compiler.h b/paddle/cinn/hlir/framework/new_ir_compiler.h index c9a430e39c57a..bb18da54bc4f3 100644 --- a/paddle/cinn/hlir/framework/new_ir_compiler.h +++ b/paddle/cinn/hlir/framework/new_ir_compiler.h @@ -40,11 +40,11 @@ class NewIRCompiler final { std::unique_ptr Build(); + std::unique_ptr Build(const std::vector& groups); + private: CINN_DISALLOW_COPY_AND_ASSIGN(NewIRCompiler); - std::unique_ptr Build(const std::vector& groups); - std::vector GetOpFunc(const ::ir::Operation& op, int idx); void ProcessFunction(const std::vector& lowered_funcs); diff --git a/test/cpp/ir/cinn/new_ir_compiler_test.cc b/test/cpp/ir/cinn/new_ir_compiler_test.cc index 1ea7a455e1217..91fb7cb13cc09 100644 --- a/test/cpp/ir/cinn/new_ir_compiler_test.cc +++ b/test/cpp/ir/cinn/new_ir_compiler_test.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h" @@ -31,10 +32,15 @@ #include "paddle/cinn/hlir/framework/convert_to_dialect.h" #include "paddle/cinn/hlir/framework/new_ir_compiler.h" -std::unique_ptr<::ir::Program> BuildProgram() { +using cinn::hlir::framework::newir::Group; +using cinn::hlir::framework::newir::GroupPtr; + +using ProgramInfo = + std::tuple, std::vector>; +ProgramInfo BuildProgram() { ::ir::IrContext* ctx = ::ir::IrContext::Instance(); ctx->GetOrRegisterDialect(); - auto program = std::make_unique<::ir::Program>(ctx); + auto program = std::make_shared<::ir::Program>(ctx); ::ir::Builder builder = ::ir::Builder(ctx, program->block()); const float value_one = 1.0; // relu(tan(1.)) = 1.5; @@ -51,17 +57,30 @@ std::unique_ptr<::ir::Program> BuildProgram() { phi::DataType::FLOAT32, phi::GPUPlace()); - auto tanh_op_x = builder.Build(full_op_x->result(0)); - auto relu_op_x = builder.Build(tanh_op_x->result(0)); - auto tanh_op_y = builder.Build(full_op_y->result(0)); - auto relu_op_y = builder.Build(tanh_op_y->result(0)); - - return std::move(program); + auto tan_op_x = builder.Build(full_op_x->result(0)); + auto relu_op_x = builder.Build(tan_op_x->result(0)); + auto tan_op_y = builder.Build(relu_op_x->result(0)); + auto relu_op_y = builder.Build(tan_op_y->result(0)); + + std::vector groups; + groups.emplace_back( + std::make_shared(std::initializer_list<::ir::Operation*>( + {full_op_x.operation()}))); // For coverage + groups.emplace_back(std::make_shared( + std::initializer_list<::ir::Operation*>({full_op_y.operation()}))); + groups.emplace_back(std::make_shared( + std::vector<::ir::Operation*>({tan_op_x.operation(), + relu_op_x.operation(), + tan_op_y.operation(), + relu_op_y.operation()}))); + + return {program, groups}; } TEST(NewIRCompier, CompilerAndRun) { // Step 1: Construct ir::Program - std::unique_ptr<::ir::Program> program = BuildProgram(); + auto prog_info = BuildProgram(); + std::shared_ptr<::ir::Program> program = std::get<0>(prog_info); EXPECT_EQ(program->block()->size(), 6u); LOG(INFO) << program->block()->size(); @@ -89,9 +108,42 @@ TEST(NewIRCompier, CompilerAndRun) { } } +TEST(NewIRCompier, CompileGroupOps) { + // Step 1: Construct ir::Program + auto prog_info = BuildProgram(); + std::shared_ptr<::ir::Program> program = std::get<0>(prog_info); + std::vector groups = std::get<1>(prog_info); + EXPECT_EQ(program->block()->size(), 6u); + LOG(INFO) << program->block()->size(); + + std::stringstream ss; + program->Print(ss); + LOG(INFO) << ss.str(); + + // Step 2: Compiler New ir::Program into Runtime Program + auto target = cinn::common::DefaultNVGPUTarget(); + auto scope = cinn::hlir::framework::BuildScope(target, *program); + ASSERT_EQ(scope->var_names().size(), 6); + + cinn::hlir::framework::NewIRCompiler ir_compiler(*program, target, scope); + auto runtime_program = ir_compiler.Build(groups); + + // Step 3: Execute Runtime Instruction and check Scope. + ASSERT_NO_THROW(runtime_program->Execute()); + for (auto& var_name : scope->var_names()) { + std::string name = {var_name.begin(), var_name.end()}; + std::vector data = + cinn::GetTensorData(scope->GetTensor(name), target); + for (int i = 0; i < 1; ++i) { + LOG_FIRST_N(INFO, 10) << "data: " << data[i]; + } + } +} + TEST(RuntimeDialect, CompilerAndRun) { // Step 1: Construct ir::Program - std::unique_ptr<::ir::Program> program = BuildProgram(); + auto prog_info = BuildProgram(); + std::shared_ptr<::ir::Program> program = std::get<0>(prog_info); EXPECT_EQ(program->block()->size(), 6u); // Step 2: Compiler New ir::Program into Runtime Program @@ -103,7 +155,7 @@ TEST(RuntimeDialect, CompilerAndRun) { auto runtime_program = ir_compiler.Build(); // Step 3: Convert into cinn::dialect::RuntimeDialect - std::unique_ptr<::ir::Program> ir_runtime_program = + std::shared_ptr<::ir::Program> ir_runtime_program = cinn::hlir::framework::ConvertToRuntimeDialect(*runtime_program); // Step 4: Run cinn::dialect::RuntimeDialect From 80563b0525516ca7f2088d0615b33f2b0a8d1130 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:38:40 +0800 Subject: [PATCH 141/194] make test_lite_engine_op linked to libpaddle.so (#56978) * fix bug * fix bug --- test/cpp/fluid/lite/CMakeLists.txt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/cpp/fluid/lite/CMakeLists.txt b/test/cpp/fluid/lite/CMakeLists.txt index 46c2ff159bb7d..f02783b313639 100644 --- a/test/cpp/fluid/lite/CMakeLists.txt +++ b/test/cpp/fluid/lite/CMakeLists.txt @@ -1,8 +1,4 @@ -cc_test_old( - test_lite_engine_op - SRCS - lite_engine_op_test.cc - DEPS - fleet_executor - lite_engine_op - analysis) +get_property(paddle_lib GLOBAL PROPERTY PADDLE_LIB_NAME) + +cc_test_old(test_lite_engine_op SRCS lite_engine_op_test.cc DEPS ${paddle_lib} + python) From 2a8839b0117cbfc53dc99c02d21d51fa79e29f38 Mon Sep 17 00:00:00 2001 From: Ruibiao Chen Date: Wed, 6 Sep 2023 10:56:03 +0800 Subject: [PATCH 142/194] Fix event info bug in StreamAnalyzer (#56982) --- .../fluid/framework/new_executor/interpreter/stream_analyzer.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc index 27ac1681a4008..3dc9175dbfd4b 100644 --- a/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc +++ b/paddle/fluid/framework/new_executor/interpreter/stream_analyzer.cc @@ -414,7 +414,6 @@ void analyse_event_info_for_two_instructions( if (has_data_dependency( instructions[cur_instr_id], instructions[next_instr_id]) || - !run_type_info[next_instr_id][DownstreamRunType::kEventRun].empty() || instructions[next_instr_id]->OpBase()->Type() == "depend") { waiter_instr_ids->insert(next_instr_id); return; @@ -474,7 +473,6 @@ void analyse_event_info_for_two_instructions< if (has_data_dependency( instructions[cur_instr_id], instructions[next_instr_id]) || - !run_type_info[next_instr_id][DownstreamRunType::kEventRun].empty() || instructions[next_instr_id]->Name() == "pd.depend") { waiter_instr_ids->insert(next_instr_id); return; From 85dbcefd103ab87dc3bd6b505ca0e0a65e9bd57a Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Wed, 6 Sep 2023 11:06:41 +0800 Subject: [PATCH 143/194] [NewIR] add_n and combine support selected rows (#56754) * add_n and combine support selected rows --- .../fluid/ir/dialect/op_generator/op_gen.py | 2 +- .../dialect/paddle_dialect/ir/pd_dialect.cc | 5 +- .../dialect/paddle_dialect/ir/pd_manual_op.cc | 270 +++++++++++++++++- .../dialect/paddle_dialect/ir/pd_manual_op.h | 44 +++ .../ir/phi_kernel_adaptor/phi_kernel_util.h | 28 +- .../ir/transforms/pd_op_to_kernel_pass.cc | 138 +++++++-- .../ir_adaptor/translator/op_translator.cc | 4 +- test/dygraph_to_static/test_simnet.py | 2 + test/dygraph_to_static/test_simnet_v2.py | 2 + 9 files changed, 454 insertions(+), 41 deletions(-) diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index 5d51a731c546a..1c19874dd9bb7 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -172,7 +172,7 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ 'bool': 'ir::BoolAttribute', } -_NO_NEED_GEN_OPS = {'add_n', 'split_grad'} +_NO_NEED_GEN_OPS = {'add_n', 'add_n_', 'add_n_with_kernel', 'split_grad'} def to_phi_and_fluid_op_name(op_item): diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc index 19b8b133559b7..9d24dcd277884 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc @@ -48,7 +48,10 @@ void PaddleDialect::initialize() { #define GET_OP_LIST #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.h" // NOLINT >(); - RegisterOps(); + RegisterOps(); RegisterInterfaces(); } diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index 33469fef8fa32..d50faedfd565f 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -57,13 +57,18 @@ void AddNOp::Verify() { "The size %d of inputs must be equal to 1.", input_size)); if (auto vec_type = (*this)->operand(0).type().dyn_cast()) { for (size_t i = 0; i < vec_type.size(); ++i) { - PADDLE_ENFORCE(vec_type[i].isa(), + PADDLE_ENFORCE(vec_type[i].isa() || + vec_type[i].isa(), phi::errors::PreconditionNotMet( "Type validation failed for the 0th input.")); } } else { PADDLE_ENFORCE( - (*this)->operand(0).type().isa(), + (*this)->operand(0).type().isa() || + (*this) + ->operand(0) + .type() + .isa(), phi::errors::PreconditionNotMet( "Type validation failed for the 0th input.")); } @@ -81,7 +86,8 @@ void AddNOp::Verify() { phi::errors::PreconditionNotMet( "The size %d of outputs must be equal to 1.", output_size)); PADDLE_ENFORCE( - (*this)->result(0).type().isa(), + (*this)->result(0).type().isa() || + (*this)->result(0).type().isa(), phi::errors::PreconditionNotMet( "Type validation failed for the 0th output.")); } @@ -146,6 +152,262 @@ void AddNOp::InferMeta(phi::InferMetaContext *infer_meta) { fn(infer_meta); } +OpInfoTuple AddN_Op::GetOpInfo() { + std::vector inputs = { + paddle::dialect::OpInputInfo( + "inputs", + "ir::VectorType", + false, + false, + false)}; + std::vector attributes = {}; + std::vector outputs = { + paddle::dialect::OpOutputInfo( + "out", "paddle::dialect::DenseTensorType", false, false)}; + paddle::dialect::OpRunTimeInfo run_time_info = paddle::dialect::OpRunTimeInfo( + "AddNInferMeta", {"inputs"}, {"add_n"}, {"inputs"}, {}, {}, {}, {}); + return std::make_tuple(inputs, attributes, outputs, run_time_info, "add_n_"); +} + +void AddN_Op::Build(ir::Builder &builder, + ir::OperationArgument &argument, + ir::OpResult inputs_) { + VLOG(4) << "Builder construction inputs"; + std::vector argument_inputs = {inputs_}; + argument.AddOperands(argument_inputs.begin(), argument_inputs.end()); + + VLOG(4) << "Builder construction attributes"; + + VLOG(4) << "Builder construction outputs"; + ir::VectorType inputs = inputs_.type().dyn_cast(); + (void)inputs; + std::vector vec_dense_inputs; + for (size_t i = 0; i < static_cast(inputs.size()); i++) { + vec_dense_inputs.push_back(phi::DenseTensor( + std::make_unique( + paddle::platform::CPUPlace()) + .get(), + phi::DenseTensorMeta( + paddle::dialect::TransToPhiDataType( + inputs[i].dyn_cast().dtype()), + inputs[i].dyn_cast().dims(), + inputs[i] + .dyn_cast() + .data_layout(), + inputs[i].dyn_cast().lod(), + inputs[i].dyn_cast().offset()))); + } + std::vector vec_meta_inputs; + for (size_t i = 0; i < vec_dense_inputs.size(); i++) { + vec_meta_inputs.push_back(phi::MetaTensor(&vec_dense_inputs[i])); + } + + std::vector meta_inputs; + for (size_t i = 0; i < static_cast(vec_meta_inputs.size()); i++) { + meta_inputs.push_back(&vec_meta_inputs[i]); + } + phi::DenseTensor dense_out; + phi::MetaTensor meta_out(&dense_out); + + phi::AddNInferMeta(meta_inputs, &meta_out); + + std::vector argument_outputs; + ir::Type out_dense_tensor_type = paddle::dialect::DenseTensorType::get( + ir::IrContext::Instance(), + paddle::dialect::TransToIrDataType(dense_out.dtype()), + dense_out.dims(), + dense_out.layout(), + dense_out.lod(), + dense_out.offset()); + argument_outputs.push_back(out_dense_tensor_type); + argument.AddOutputs(argument_outputs.begin(), argument_outputs.end()); +} + +void AddN_Op::Verify() { + VLOG(4) << "Start Verifying inputs, outputs and attributes for: AddN_Op."; + VLOG(4) << "Verifying inputs:"; + { + auto input_size = num_operands(); + PADDLE_ENFORCE_EQ( + input_size, + 1u, + phi::errors::PreconditionNotMet( + "The size %d of inputs must be equal to 1.", input_size)); + if (auto vec_type = + (*this)->operand_source(0).type().dyn_cast()) { + for (size_t i = 0; i < vec_type.size(); ++i) { + PADDLE_ENFORCE(vec_type[i].isa() || + vec_type[i].isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th input.")); + } + } else { + PADDLE_ENFORCE((*this)->operand_source(0) + .type() + .isa() || + (*this) + ->operand_source(0) + .type() + .isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th input.")); + } + } + VLOG(4) << "Verifying attributes:"; + { + // Attributes num is 0, not need to check attributes type. + } + VLOG(4) << "Verifying outputs:"; + { + auto output_size = num_results(); + PADDLE_ENFORCE_EQ( + output_size, + 1u, + phi::errors::PreconditionNotMet( + "The size %d of outputs must be equal to 1.", output_size)); + PADDLE_ENFORCE( + (*this)->result(0).type().isa() || + (*this)->result(0).type().isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th output.")); + } + VLOG(4) << "End Verifying for: AddN_Op."; +} + +void AddN_Op::InferMeta(phi::InferMetaContext *infer_meta) { + auto fn = PD_INFER_META(phi::AddNInferMeta); + fn(infer_meta); +} + +OpInfoTuple AddNWithKernelOp::GetOpInfo() { + std::vector inputs = { + paddle::dialect::OpInputInfo( + "inputs", + "ir::VectorType", + false, + false, + false)}; + std::vector attributes = {}; + std::vector outputs = { + paddle::dialect::OpOutputInfo( + "out", "paddle::dialect::DenseTensorType", false, false)}; + paddle::dialect::OpRunTimeInfo run_time_info = paddle::dialect::OpRunTimeInfo( + "AddNInferMeta", {"inputs"}, {"add_n"}, {"inputs"}, {}, {}, {}, {}); + return std::make_tuple( + inputs, attributes, outputs, run_time_info, "add_n_with_kernel"); +} + +void AddNWithKernelOp::Build(ir::Builder &builder, + ir::OperationArgument &argument, + ir::OpResult inputs_) { + VLOG(4) << "Builder construction inputs"; + std::vector argument_inputs = {inputs_}; + argument.AddOperands(argument_inputs.begin(), argument_inputs.end()); + + VLOG(4) << "Builder construction attributes"; + + VLOG(4) << "Builder construction outputs"; + ir::VectorType inputs = inputs_.type().dyn_cast(); + (void)inputs; + std::vector vec_dense_inputs; + for (size_t i = 0; i < static_cast(inputs.size()); i++) { + vec_dense_inputs.push_back(phi::DenseTensor( + std::make_unique( + paddle::platform::CPUPlace()) + .get(), + phi::DenseTensorMeta( + paddle::dialect::TransToPhiDataType( + inputs[i].dyn_cast().dtype()), + inputs[i].dyn_cast().dims(), + inputs[i] + .dyn_cast() + .data_layout(), + inputs[i].dyn_cast().lod(), + inputs[i].dyn_cast().offset()))); + } + std::vector vec_meta_inputs; + for (size_t i = 0; i < vec_dense_inputs.size(); i++) { + vec_meta_inputs.push_back(phi::MetaTensor(&vec_dense_inputs[i])); + } + + std::vector meta_inputs; + for (size_t i = 0; i < static_cast(vec_meta_inputs.size()); i++) { + meta_inputs.push_back(&vec_meta_inputs[i]); + } + phi::DenseTensor dense_out; + phi::MetaTensor meta_out(&dense_out); + + phi::AddNInferMeta(meta_inputs, &meta_out); + + std::vector argument_outputs; + ir::Type out_dense_tensor_type = paddle::dialect::DenseTensorType::get( + ir::IrContext::Instance(), + paddle::dialect::TransToIrDataType(dense_out.dtype()), + dense_out.dims(), + dense_out.layout(), + dense_out.lod(), + dense_out.offset()); + argument_outputs.push_back(out_dense_tensor_type); + argument.AddOutputs(argument_outputs.begin(), argument_outputs.end()); +} + +void AddNWithKernelOp::Verify() { + VLOG(4) << "Start Verifying inputs, outputs and attributes for: " + "AddNWithKernelOp."; + VLOG(4) << "Verifying inputs:"; + { + auto input_size = num_operands(); + PADDLE_ENFORCE_EQ( + input_size, + 1u, + phi::errors::PreconditionNotMet( + "The size %d of inputs must be equal to 1.", input_size)); + if (auto vec_type = + (*this)->operand_source(0).type().dyn_cast()) { + for (size_t i = 0; i < vec_type.size(); ++i) { + PADDLE_ENFORCE(vec_type[i].isa() || + vec_type[i].isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th input.")); + } + } else { + PADDLE_ENFORCE((*this)->operand_source(0) + .type() + .isa() || + (*this) + ->operand_source(0) + .type() + .isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th input.")); + } + } + VLOG(4) << "Verifying attributes:"; + { + // Attributes num is 0, not need to check attributes type. + } + VLOG(4) << "Verifying outputs:"; + { + auto output_size = num_results(); + PADDLE_ENFORCE_EQ( + output_size, + 1u, + phi::errors::PreconditionNotMet( + "The size %d of outputs must be equal to 1.", output_size)); + PADDLE_ENFORCE( + (*this)->result(0).type().isa() || + (*this)->result(0).type().isa(), + phi::errors::PreconditionNotMet( + "Type validation failed for the 0th output.")); + } + VLOG(4) << "End Verifying for: AddNWithKernelOp."; +} + +void AddNWithKernelOp::InferMeta(phi::InferMetaContext *infer_meta) { + auto fn = PD_INFER_META(phi::AddNInferMeta); + fn(infer_meta); +} + const char *SplitGradOp::attributes_name[1] = {"axis"}; OpInfoTuple SplitGradOp::GetOpInfo() { @@ -364,3 +626,5 @@ void SplitGradOp::InferMeta(phi::InferMetaContext *infer_meta) { IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddNOp) IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::SplitGradOp) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddN_Op) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddNWithKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h index b0ff45d9baaff..8f0dbd86d1d80 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h @@ -24,6 +24,7 @@ paddle::dialect::AddNOp, paddle::dialect::SplitGradOp #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/ir/dialect/paddle_dialect/interface/infermeta.h" #include "paddle/fluid/ir/dialect/paddle_dialect/interface/op_yaml_info.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/trait/inplace.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" #include "paddle/ir/core/builder.h" @@ -51,6 +52,47 @@ class AddNOp : public ir::Op { static void InferMeta(phi::InferMetaContext *infer_meta); }; +class AddN_Op : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd.add_n_"; } + static constexpr const char **attributes_name = nullptr; + static constexpr uint32_t attributes_num = 0; + static OpInfoTuple GetOpInfo(); + static void Build(ir::Builder &builder, // NOLINT + ir::OperationArgument &argument, // NOLINT + ir::OpResult inputs_); + + void Verify(); + ir::Value inputs() { return operand_source(0); } + ir::OpResult out() { return result(0); } + + static void InferMeta(phi::InferMetaContext *infer_meta); +}; + +class AddNWithKernelOp : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd.add_n_with_kernel"; } + static constexpr const char **attributes_name = nullptr; + static constexpr uint32_t attributes_num = 0; + static OpInfoTuple GetOpInfo(); + static void Build(ir::Builder &builder, // NOLINT + ir::OperationArgument &argument, // NOLINT + ir::OpResult inputs_); + + void Verify(); + ir::Value inputs() { return operand_source(0); } + ir::OpResult out() { return result(0); } + + static void InferMeta(phi::InferMetaContext *infer_meta); +}; + class SplitGradOp : public ir::Op { public: using Op::Op; @@ -79,5 +121,7 @@ class SplitGradOp : public ir::Op { IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddNOp) IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::SplitGradOp) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddN_Op) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddNWithKernelOp) #endif diff --git a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h index 1958a9444bcb9..b1916d5418f77 100644 --- a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h +++ b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h @@ -118,8 +118,18 @@ void BuildPhiContext(ir::Operation* op, InListType inputs; auto& variable_array = var->Get(); for (size_t i = 0; i < variable_array.size(); ++i) { - inputs.emplace_back(InType(const_cast( - &(variable_array[i]->Get())))); + if (variable_array[i]->IsType()) { + inputs.emplace_back(InType(const_cast( + &(variable_array[i]->Get())))); + } else if (variable_array[i]->IsType()) { + inputs.emplace_back(InType(const_cast( + &(variable_array[i]->Get())))); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "Only support Vector and vector now, " + "not support vector<%d>.", + variable_array[i]->Type())); + } } ctx->EmplaceBackInputs(inputs); } else { @@ -315,8 +325,18 @@ void BuildPhiContext(ir::Operation* op, auto& variable_array = inner_scope->FindVar(name_map.at(out_ptr)) ->Get(); for (size_t i = 0; i < variable_array.size(); ++i) { - outputs.emplace_back(OutType(const_cast( - &(variable_array[i]->Get())))); + if (variable_array[i]->IsType()) { + outputs.emplace_back(OutType(const_cast( + &(variable_array[i]->Get())))); + } else if (variable_array[i]->IsType()) { + outputs.emplace_back(OutType(const_cast( + &(variable_array[i]->Get())))); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "Only support Vector and vector now, " + "not support vector<%d>.", + variable_array[i]->Type())); + } } ctx->EmplaceBackOutputs(outputs); } else { diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 84f18baa55aea..d75c7cc4779ff 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -149,34 +149,67 @@ bool SkipFeedOp(ir::Operation* op, const std::set& feed_names) { op->attributes().at("name").dyn_cast().AsString()); } -std::vector GetFakeTensorList(ir::Value new_input_tmp) { - std::vector vec_res; +std::vector> GetFakeTensorList( + ir::Value new_input_tmp) { + std::vector> vec_res; auto input_type = new_input_tmp.type(); - std::vector types; - if (input_type.isa()) { - types.push_back(input_type.dyn_cast()); - } else if (input_type.isa()) { - auto vec_inner_types = input_type.dyn_cast().data(); - for (size_t i = 0; i < vec_inner_types.size(); ++i) { - types.push_back( - vec_inner_types[0].dyn_cast()); - } - } - for (auto& type : types) { - auto ptr = new phi::Allocation(nullptr, 0, type.place()); + auto build_fake_dense_tensor = + [](const dialect::AllocatedDenseTensorType& type) { + auto ptr = new phi::Allocation(nullptr, 0, type.place()); + + std::shared_ptr holder(ptr); + + auto dtype = TransToPhiDataType(type.dtype()); - std::shared_ptr holder(ptr); + phi::DenseTensorMeta meta( + dtype, type.dims(), type.data_layout(), type.lod(), type.offset()); - auto dtype = TransToPhiDataType(type.dtype()); + return std::make_shared(holder, meta); + }; - phi::DenseTensorMeta meta( - dtype, type.dims(), type.data_layout(), type.lod(), type.offset()); + auto build_fake_selected_rows = + [](const dialect::AllocatedSelectedRowsType& type) { + auto ptr = new phi::Allocation(nullptr, 0, type.place()); - phi::DenseTensor fake_tensor(holder, meta); + std::shared_ptr holder(ptr); - vec_res.push_back(fake_tensor); + auto dtype = TransToPhiDataType(type.dtype()); + + phi::DenseTensorMeta meta( + dtype, type.dims(), type.data_layout(), type.lod(), type.offset()); + + std::vector rows; + int64_t height = 0; + rows.clear(); + + auto sr = std::make_shared(rows, height); + + phi::DenseTensor dense_tensor(holder, meta); + *(sr->mutable_value()) = dense_tensor; + + return sr; + }; + + if (input_type.isa()) { + vec_res.push_back(build_fake_dense_tensor( + input_type.dyn_cast())); + } else if (input_type.isa()) { + vec_res.push_back(build_fake_selected_rows( + input_type.dyn_cast())); + } else if (input_type.isa()) { + auto vec_inner_types = input_type.dyn_cast().data(); + for (size_t i = 0; i < vec_inner_types.size(); ++i) { + if (vec_inner_types[0].isa()) { + vec_res.push_back(build_fake_dense_tensor( + vec_inner_types[0].dyn_cast())); + } else if (vec_inner_types[0].isa()) { + vec_res.push_back(build_fake_selected_rows( + vec_inner_types[0].dyn_cast())); + } + } } + return vec_res; } @@ -514,7 +547,7 @@ phi::KernelKey GetKernelKey( auto fake_tensors = GetFakeTensorList(new_input_tmp); for (auto& fake_tensor : fake_tensors) { - kernel_key_parser.AssignKernelKeySet(fake_tensor); + kernel_key_parser.AssignKernelKeySet(*fake_tensor); } // Because we can't make sure the place when build data op @@ -617,6 +650,12 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, new_in.type() .dyn_cast() .place()); + } else if (new_in.type() + .isa()) { + out_places.push_back( + new_in.type() + .dyn_cast() + .place()); } else { PADDLE_THROW(phi::errors::Unimplemented( "only support dense tensor type for now")); @@ -759,6 +798,14 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, if (op_info_parser != nullptr) { kernel_fn_str = op_info_parser->OpRuntimeInfo().kernel_func[0]; } + + if (op_item->name() == "pd.add_n_" || + op_item->name() == "pd.add_n_with_kernel") { + if (op_item->result(0).type().isa()) { + kernel_fn_str = "add_n_sr"; + } + } + auto kernel_key = GetKernelKey(op_item, place, map_value_pair, op_info_parser.get()); VLOG(6) << "kernel type " << kernel_key; @@ -929,9 +976,22 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, for (size_t j = 0; j < pre_define_op->num_operands(); ++j) { auto in_i = map_value_pair.at(pre_define_op->operand_source(j)); auto in_i_type = in_i.type(); - auto place = - in_i_type.dyn_cast() - .place(); + phi::Place place; + if (in_i_type.isa()) { + place = + in_i_type.dyn_cast() + .place(); + } else if (in_i_type + .isa()) { + place = + in_i_type.dyn_cast() + .place(); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "builtin.combine Input type only support " + "VectorType and " + "VectorType")); + } // get input args def type auto args_def = kernel.args_def(); @@ -949,12 +1009,30 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, // build memcopy op auto out_place = phi::TransToPhiPlace(kernel.InputAt(i).backend); - auto out_type = dialect::AllocatedDenseTensorType::get( - ctx, - out_place, - pre_define_op->operand_source(j) - .type() - .dyn_cast()); + + ir::Type out_type; + if (in_i_type.isa()) { + out_type = dialect::AllocatedDenseTensorType::get( + ctx, + out_place, + pre_define_op->operand_source(j) + .type() + .dyn_cast()); + } else if (in_i_type + .isa()) { + out_type = dialect::AllocatedSelectedRowsType::get( + ctx, + out_place, + pre_define_op->operand_source(j) + .type() + .dyn_cast()); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "builtin.combine Input type only support " + "VectorType and " + "VectorType")); + } + in_i = AddPlaceTransferOp(in_i, out_type, place, diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 38d833fc312de..e22fa5f3b3779 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -1112,8 +1112,8 @@ struct AddNOpTranscriber : public OpTranscriber { } const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name); if (!op_info) { - IR_THROW( - "Op assign_value should have corresponding OpInfo pd.assign_value_"); + IR_THROW("Op assign_value should have corresponding OpInfo %s", + target_op_name); } return op_info; diff --git a/test/dygraph_to_static/test_simnet.py b/test/dygraph_to_static/test_simnet.py index 2c69cf2072cf9..09ea063f9ad8e 100644 --- a/test/dygraph_to_static/test_simnet.py +++ b/test/dygraph_to_static/test_simnet.py @@ -17,6 +17,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir from simnet_dygraph_model import BOW, HingeLoss import paddle @@ -176,6 +177,7 @@ def train(conf_dict, to_static): class TestSimnet(unittest.TestCase): + @test_and_compare_with_new_ir(True) def test_dygraph_static_same_loss(self): if fluid.is_compiled_with_cuda(): fluid.set_flags({"FLAGS_cudnn_deterministic": True}) diff --git a/test/dygraph_to_static/test_simnet_v2.py b/test/dygraph_to_static/test_simnet_v2.py index a49cc23af11f8..316464ab79132 100644 --- a/test/dygraph_to_static/test_simnet_v2.py +++ b/test/dygraph_to_static/test_simnet_v2.py @@ -17,6 +17,7 @@ import unittest import numpy as np +from dygraph_to_static_util import test_and_compare_with_new_ir from simnet_dygraph_model_v2 import BOW, HingeLoss import paddle @@ -176,6 +177,7 @@ def train(conf_dict, to_static): class TestSimnet(unittest.TestCase): + @test_and_compare_with_new_ir(True) def test_dygraph_static_same_loss(self): if paddle.is_compiled_with_cuda(): paddle.fluid.set_flags({"FLAGS_cudnn_deterministic": True}) From 7edb0bf99e6181ea3ba4bc5f6476fa83bf3925da Mon Sep 17 00:00:00 2001 From: ronnywang Date: Wed, 6 Sep 2023 11:22:10 +0800 Subject: [PATCH 144/194] [CustomDevice] fix bug in custom allocator (#56977) --- paddle/fluid/memory/allocation/custom_allocator.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/memory/allocation/custom_allocator.cc b/paddle/fluid/memory/allocation/custom_allocator.cc index 37503105c8bc0..c8225a160428f 100644 --- a/paddle/fluid/memory/allocation/custom_allocator.cc +++ b/paddle/fluid/memory/allocation/custom_allocator.cc @@ -29,7 +29,8 @@ void CustomAllocator::FreeImpl(phi::Allocation* allocation) { platform::errors::PermissionDenied("CustomDevice memory is " "freed in incorrect device. " "This may be a bug")); - + phi::DeviceManager::GetDeviceWithPlace(place_)->MemoryDeallocate( + allocation->ptr(), allocation->size()); delete allocation; } From c34853af71b0ef5384d8f55eabe7f84ad7487c14 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 6 Sep 2023 11:23:27 +0800 Subject: [PATCH 145/194] add run check for new ir (#56704) --- test/legacy_test/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 05f30dca257f1..46a0136167e9e 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -592,6 +592,10 @@ py_test_modules( py_test_modules(test_install_check MODULES test_install_check ENVS FLAGS_cudnn_deterministic=1) set_tests_properties(test_install_check PROPERTIES LABELS "RUN_TYPE=DIST") +py_test_modules(test_install_check_new_ir MODULES test_install_check ENVS + FLAGS_cudnn_deterministic=1 FLAGS_enable_new_ir_in_executor=1) +set_tests_properties(test_install_check_new_ir PROPERTIES LABELS + "RUN_TYPE=DIST") if((WITH_GPU) AND (CUDA_VERSION GREATER_EQUAL 11.6)) py_test_modules(test_fused_gemm_epilogue_op MODULES From f6a8125fd4f766c0be562b5f1ef5088d41ed5ce6 Mon Sep 17 00:00:00 2001 From: akshatvishu <33392262+akshatvishu@users.noreply.github.com> Date: Wed, 6 Sep 2023 09:10:09 +0530 Subject: [PATCH 146/194] make loss cond. on label while using Stirling approximation (#56992) --- python/paddle/nn/functional/loss.py | 4 ++-- test/legacy_test/test_poisson_nll_loss.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index dbcc4f0c05fda..f0411d096dee4 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -1594,9 +1594,9 @@ def poisson_nll_loss( + 0.5 * paddle.log(2 * math.pi * label) ) loss_out += paddle.where( - stirling_approx <= 1, - paddle.zeros_like(stirling_approx), + label > 1, stirling_approx, + paddle.zeros_like(stirling_approx), ) if reduction == 'mean': loss_out = paddle.mean(loss_out) diff --git a/test/legacy_test/test_poisson_nll_loss.py b/test/legacy_test/test_poisson_nll_loss.py index 096018a6e2bf0..14ad375519914 100644 --- a/test/legacy_test/test_poisson_nll_loss.py +++ b/test/legacy_test/test_poisson_nll_loss.py @@ -51,7 +51,9 @@ def ref_poisson_nll_loss( stirling_approx = ( label * np.log(label) - label + 0.5 * np.log(2 * np.pi * label) ) - loss_out += np.where(stirling_approx <= 1, 0, stirling_approx) + loss_out += np.where( + label > 1, stirling_approx, np.zeros_like(stirling_approx) + ) if reduction == 'none': return loss_out From 1b7c1c56ade3504cafb63a3f478e7767007c2dbc Mon Sep 17 00:00:00 2001 From: huangjiyi <43315610+huangjiyi@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:03:46 +0800 Subject: [PATCH 147/194] fix compile errors with gflags (#56993) * update * update * update * update * update --- cmake/configure.cmake | 4 ---- cmake/external/gflags.cmake | 1 + paddle/utils/flags.h | 8 ++++---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 4d0b04209c059..dc661fce388fe 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -201,10 +201,6 @@ if(WITH_DISTRIBUTE) add_definitions(-DPADDLE_WITH_DISTRIBUTE) endif() -if(WITH_GFLAGS) - add_definitions(-DPADDLE_WITH_GFLAGS) -endif() - if(WITH_PSCORE) add_definitions(-DPADDLE_WITH_PSCORE) endif() diff --git a/cmake/external/gflags.cmake b/cmake/external/gflags.cmake index 75436783c7ede..3398f8a28307e 100755 --- a/cmake/external/gflags.cmake +++ b/cmake/external/gflags.cmake @@ -110,6 +110,7 @@ endif() set(flags_dep) if(WITH_GFLAGS) list(APPEND flags_dep gflags) + add_definitions(-DPADDLE_WITH_GFLAGS) else() list(APPEND flags_dep paddle_flags) endif() diff --git a/paddle/utils/flags.h b/paddle/utils/flags.h index 3f68ba2f5dc1f..06c84ae15ab7f 100644 --- a/paddle/utils/flags.h +++ b/paddle/utils/flags.h @@ -79,8 +79,8 @@ using gflags::DoubleFromEnv; using gflags::Int32FromEnv; using gflags::Int64FromEnv; using gflags::StringFromEnv; -using gflags::UInt32FromEnv; -using gflags::UInt64FromEnv; +using gflags::Uint32FromEnv; +using gflags::Uint64FromEnv; #else #define DEFINE_FROM_ENV_FUNC(type, name) \ inline type name##FromEnv(const std::string& env_var_name, \ @@ -90,9 +90,9 @@ using gflags::UInt64FromEnv; DEFINE_FROM_ENV_FUNC(bool, Bool); DEFINE_FROM_ENV_FUNC(int32_t, Int32); -DEFINE_FROM_ENV_FUNC(uint32_t, UInt32); +DEFINE_FROM_ENV_FUNC(uint32_t, Uint32); DEFINE_FROM_ENV_FUNC(int64_t, Int64); -DEFINE_FROM_ENV_FUNC(uint64_t, UInt64); +DEFINE_FROM_ENV_FUNC(uint64_t, Uint64); DEFINE_FROM_ENV_FUNC(double, Double); DEFINE_FROM_ENV_FUNC(std::string, String); From 75c4a24ca32ac2c07d7e271a5d15ddbc52df1b6c Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:09:06 +0800 Subject: [PATCH 148/194] [IR] Add IrMetaTensor (#56973) * add meta tensor * refine code * fix bug * fix bug --- paddle/fluid/framework/type_info.cc | 2 + .../ir/dialect/op_generator/op_build_gen.py | 24 +++--- .../fluid/ir/dialect/op_generator/op_gen.py | 1 + .../dialect/paddle_dialect/ir/CMakeLists.txt | 2 +- .../paddle_dialect/ir/pd_meta_tensor.cc | 68 ++++++++++++++++ .../paddle_dialect/ir/pd_meta_tensor.h | 80 +++++++++++++++++++ paddle/phi/core/meta_tensor.cc | 3 + paddle/phi/core/utils/type_info.cc | 2 + 8 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc create mode 100644 paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h diff --git a/paddle/fluid/framework/type_info.cc b/paddle/fluid/framework/type_info.cc index 442800d035f55..cb7dae540d119 100644 --- a/paddle/fluid/framework/type_info.cc +++ b/paddle/fluid/framework/type_info.cc @@ -16,6 +16,7 @@ limitations under the License. */ #include "paddle/fluid/framework/feed_fetch_type.h" #include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/string_array.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/fluid/prim/utils/static/desc_tensor.h" #include "paddle/fluid/primitive/type/lazy_tensor.h" @@ -44,5 +45,6 @@ template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; +template class TypeInfoTraits; } // namespace phi diff --git a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py index d9747b47e8747..d36c269648315 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py @@ -286,27 +286,25 @@ def GenBuildOutputs( build_output_str = ' VLOG(4) << "Builder construction outputs";\n' CREATE_INPUT_METATENSOR_TEMPLATE = """ VLOG(4) << "Builder construction dense_{name}"; - phi::DenseTensor dense_{name}(std::make_unique(paddle::platform::CPUPlace()).get(), - phi::DenseTensorMeta(paddle::dialect::TransToPhiDataType({name}.dtype()), - {name}.dims(), - {name}.data_layout(), - {name}.lod(), - {name}.offset())); + paddle::dialect::IrMetaTensor ir_meta_tensor_{name}(paddle::dialect::TransToPhiDataType({name}.dtype()), + {name}.dims(), + {name}.data_layout(), + {name}.lod(), + {name}.offset()); VLOG(4) << "Builder construction meta_{name}"; - phi::MetaTensor meta_{name}(&dense_{name}); + phi::MetaTensor meta_{name}(&ir_meta_tensor_{name}); """ - CREATE_INPUT_VEC_METATENSOR_TEMPLATE = """ std::vector vec_dense_{name}; + CREATE_INPUT_VEC_METATENSOR_TEMPLATE = """ std::vector vec_ir_meta_tensor_{name}; for (size_t i=0; i < static_cast({name}.size()); i++) {{ - vec_dense_{name}.push_back(phi::DenseTensor(std::make_unique(paddle::platform::CPUPlace()).get(), - phi::DenseTensorMeta(paddle::dialect::TransToPhiDataType({name}[i].dyn_cast().dtype()), + vec_ir_meta_tensor_{name}.push_back(paddle::dialect::IrMetaTensor(paddle::dialect::TransToPhiDataType({name}[i].dyn_cast().dtype()), {name}[i].dyn_cast().dims(), {name}[i].dyn_cast().data_layout(), {name}[i].dyn_cast().lod(), - {name}[i].dyn_cast().offset()))); + {name}[i].dyn_cast().offset())); }} std::vector vec_meta_{name}; - for (size_t i=0; i < vec_dense_{name}.size(); i++) {{ - vec_meta_{name}.push_back(phi::MetaTensor(&vec_dense_{name}[i])); + for (size_t i=0; i < vec_ir_meta_tensor_{name}.size(); i++) {{ + vec_meta_{name}.push_back(phi::MetaTensor(&vec_ir_meta_tensor_{name}[i])); }} std::vector meta_{name}; diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index 1c19874dd9bb7..7ee65d050581b 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -101,6 +101,7 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ #include "{h_file}" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_type.h" #include "paddle/ir/core/builtin_op.h" diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index cc8d1357bf070..08cc463c34c9b 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -183,7 +183,7 @@ add_custom_target(ops_api_gen ALL DEPENDS ${ops_api_source_file}) cc_library( pd_dialect_core - SRCS pd_attribute.cc pd_type.cc + SRCS pd_attribute.cc pd_type.cc pd_meta_tensor.cc DEPS phi pd_interface pd_trait type_info) cc_library( pd_dialect_op diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc new file mode 100644 index 0000000000000..2da7b098a6556 --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.cc @@ -0,0 +1,68 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" + +#include "paddle/ir/core/enforce.h" + +namespace paddle { +namespace dialect { +IrMetaTensor::IrMetaTensor(phi::DataType dtype, + const phi::DDim& dims, + phi::DataLayout layout, + const LoD& lod, + size_t offset) + : dims_(dims), dtype_(dtype), layout_(layout), lod_(lod), offset_(offset) {} + +IrMetaTensor::IrMetaTensor(const IrMetaTensor& other) { + dims_ = other.dims(); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = other.lod(); + offset_ = other.offset(); +} + +IrMetaTensor& IrMetaTensor::operator=(const IrMetaTensor& other) { + dims_ = other.dims(); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = other.lod(); + offset_ = other.offset(); + return *this; +} + +IrMetaTensor& IrMetaTensor::operator=(IrMetaTensor&& other) noexcept { + dims_ = std::move(other.dims()); + dtype_ = other.dtype(); + layout_ = other.layout(); + lod_ = std::move(other.lod()); + offset_ = other.offset(); + return *this; +} + +int64_t IrMetaTensor::numel() const { return phi::product(dims_); } + +const phi::Place& IrMetaTensor::place() const { + IR_THROW("Don't use IrMetaTensor::place method."); +} + +void* IrMetaTensor::AllocateFrom(phi::Allocator* allocator, + phi::DataType dtype, + size_t requested_size, + bool fake_alloc) { + IR_THROW("Don't use IrMetaTensor::AllocateFrom method."); +} + +} // namespace dialect +} // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h new file mode 100644 index 0000000000000..ffcbd415c368a --- /dev/null +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h @@ -0,0 +1,80 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/phi/core/allocator.h" +#include "paddle/phi/core/tensor_base.h" +#include "paddle/phi/core/tensor_meta.h" + +namespace paddle { +namespace dialect { + +using LoD = std::vector>; + +class IrMetaTensor : public phi::TensorBase, + public phi::TypeInfoTraits { + public: + IrMetaTensor(phi::DataType dtype, + const phi::DDim& dims, + phi::DataLayout layout, + const LoD& lod, + size_t offset = 0); + + IrMetaTensor(IrMetaTensor&& other) = default; + + IrMetaTensor(const IrMetaTensor& other); + + IrMetaTensor& operator=(const IrMetaTensor& other); + + IrMetaTensor& operator=(IrMetaTensor&& other) noexcept; + + virtual ~IrMetaTensor() = default; + + public: + static const char* name() { return "IrMetaTensor"; } + + int64_t numel() const override; + + const phi::DDim& dims() const noexcept override { return dims_; } + + const phi::Place& place() const override; + + phi::DataType dtype() const noexcept override { return dtype_; } + + phi::DataLayout layout() const noexcept override { return layout_; } + + const LoD& lod() const noexcept { return lod_; } + + size_t offset() const noexcept { return offset_; } + + bool valid() const noexcept override { return true; } + + bool initialized() const override { return true; } + + void* AllocateFrom(phi::Allocator* allocator, + phi::DataType dtype, + size_t requested_size = 0, + bool fake_alloc = false) override; + + private: + phi::DDim dims_; + phi::DataType dtype_{phi::DataType::UNDEFINED}; + phi::DataLayout layout_{phi::DataLayout::NCHW}; + LoD lod_; + size_t offset_{0}; +}; + +} // namespace dialect +} // namespace paddle diff --git a/paddle/phi/core/meta_tensor.cc b/paddle/phi/core/meta_tensor.cc index 5ea5a07960923..9b9df5c1ff4aa 100644 --- a/paddle/phi/core/meta_tensor.cc +++ b/paddle/phi/core/meta_tensor.cc @@ -16,6 +16,7 @@ limitations under the License. */ #include "glog/logging.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/enforce.h" @@ -271,6 +272,8 @@ const LoD& MetaTensor::lod() const { return static_cast(tensor_)->non_zero_elements().lod(); } else if (phi::SparseCsrTensor::classof(tensor_)) { return static_cast(tensor_)->non_zero_elements().lod(); + } else if (paddle::dialect::IrMetaTensor::classof(tensor_)) { + return static_cast(tensor_)->lod(); } else { PADDLE_THROW(phi::errors::Unimplemented("Unsupported getting lod of `%s`.", tensor_->type_info().name())); diff --git a/paddle/phi/core/utils/type_info.cc b/paddle/phi/core/utils/type_info.cc index 38e17b57f633d..99b134b6e7960 100644 --- a/paddle/phi/core/utils/type_info.cc +++ b/paddle/phi/core/utils/type_info.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_meta_tensor.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/custom/custom_context.h" #include "paddle/phi/backends/gpu/gpu_context.h" @@ -50,6 +51,7 @@ template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; +template class TypeInfoTraits; template class TypeInfoTraits; template class TypeInfoTraits; From 14ede2b94575c07e23e20e0f8a58ef8d9c1b3459 Mon Sep 17 00:00:00 2001 From: liuruyan <44316842+liuruyan@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:11:43 +0800 Subject: [PATCH 149/194] Add constraint related func into SymDimMgr. (#56727) * add symbolicDimProduct & symbolicDimMgr without method shape_constraint related. * split ddim in phi, add a target ddim, used by pd_type. * add pd_type.cc to ir_shape CMakeLists. * add dimOp, tieProductEqualOp. access constraint_func in SymbolTable. * put DenseTensorType into builtin_type. * add constraint related Mgr func. * move to out assert. --- paddle/ir/dialect/shape/ir/shape_op.cc | 1 + paddle/ir/dialect/shape/utils/shape_utils.cc | 298 ++++++++++++++++++ paddle/ir/dialect/shape/utils/shape_utils.h | 24 +- test/cpp/ir/shape_dialect/symbolic_op_test.cc | 174 +++++++++- 4 files changed, 494 insertions(+), 3 deletions(-) diff --git a/paddle/ir/dialect/shape/ir/shape_op.cc b/paddle/ir/dialect/shape/ir/shape_op.cc index 3681aafa36520..776503ea269e3 100644 --- a/paddle/ir/dialect/shape/ir/shape_op.cc +++ b/paddle/ir/dialect/shape/ir/shape_op.cc @@ -112,6 +112,7 @@ bool SymbolicDim::merge(SymbolicDim other) { if (!isDynamic() && !other.isDynamic() && getValue() != other.getValue()) return false; if (isDynamic() && !other.isDynamic()) updateValue(other.getValue()); + if (!isDynamic() && other.isDynamic()) other.updateValue(getValue()); bool knownNonNegativeFlag = getKnownNonNegative() || other.getKnownNonNegative(); diff --git a/paddle/ir/dialect/shape/utils/shape_utils.cc b/paddle/ir/dialect/shape/utils/shape_utils.cc index 182d335f71c3d..f9d78a63184cb 100644 --- a/paddle/ir/dialect/shape/utils/shape_utils.cc +++ b/paddle/ir/dialect/shape/utils/shape_utils.cc @@ -46,6 +46,154 @@ const std::string SymbolTable::insert(ir::Operation* symbol) { return name; } +bool SymbolicDimMgr::load() { + for (auto op_it = m_.block()->begin(); op_it != m_.block()->end(); op_it++) { + symbolTable_.insert(*op_it); + SymbolicDim op = (*op_it)->dyn_cast(); + if (!op) continue; + symbolDimUnionSet_[op] = op; + symbolNameSet_.insert(op.getSymName()); + } + return loadShapeConstraintGraph(); +} + +bool SymbolicDimMgr::loadShapeConstraintGraph() { + // TODO(liujinnan): add more constraint function. currently, only support + // tie_product_equal. + auto constraint_vec = + symbolTable_.lookup("tie_product_equal"); + + if (!constraint_vec.size()) return true; + + auto build_sym_product = [&](std::vector range, + SymbolicDimProduct& product) { + for (Value v : range) { + auto definingOp = v.GetDefiningOp(); + if (auto constOp = definingOp->dyn_cast()) { + product.factor *= constOp.value().dyn_cast().data(); + continue; + } else if (auto dimOp = definingOp->dyn_cast()) { + auto sym = symbolTable_.lookup(dimOp.getName()); + if (!sym) return false; + product.symbols.push_back(sym); + continue; + } + return false; + } + return true; + }; + for (auto op : constraint_vec) { + SymbolicDimProduct lhs, rhs; + if (!build_sym_product(op.getLhs(), lhs) || + !build_sym_product(op.getRhs(), rhs) || + !mapSymbolicDimProductEqual(lhs, rhs)) + return false; + } + return true; +} + +int64_t gcd(int64_t m, int64_t n) { + if (!m) return n; + if (!n) return m; + return (m < n) ? gcd(m, n % m) : gcd(m % n, n); +} + +bool SymbolicDimMgr::mapSymbolicDimProductEqual(const SymbolicDimProduct& lhs, + const SymbolicDimProduct& rhs) { + SymbolicDimProduct newLhs, newRhs; + std::tie(newLhs, newRhs) = simplifySymbolicDimProductPair(lhs, rhs); + + // early return for identity case. + if (newLhs == newRhs) return true; + + if (newLhs.factor == newRhs.factor && newLhs.symbols.size() == 1 && + newRhs.symbols.size() == 1) { + return mapSymbolicDimEqual(newLhs.symbols[0], newRhs.symbols[0]); + } else if (newLhs.symbols.size() == 0 && newRhs.symbols.size() == 1 && + newRhs.factor == 1) { + return mapSymbolicDimEqual(newConstantSymbolicDim(newLhs.factor), + newRhs.symbols[0]); + } else if (newRhs.symbols.size() == 0 && newLhs.symbols.size() == 1 && + newLhs.factor == 1) { + return mapSymbolicDimEqual(newConstantSymbolicDim(newRhs.factor), + newLhs.symbols[0]); + } + + productEqualityMap_[newLhs][newRhs] = productEqualityMap_[newRhs][newLhs] = + true; + + productEqualityMapUpdated_ = false; + return true; +} + +std::pair +SymbolicDimMgr::simplifySymbolicDimProductPair(const SymbolicDimProduct& x, + const SymbolicDimProduct& y) { + auto lhs = simplifySymbolicDimProduct(x); + auto rhs = simplifySymbolicDimProduct(y); + + SymbolicDimProduct newLhs, newRhs; + int64_t gcdFactor = gcd(std::abs(lhs.factor), std::abs(rhs.factor)); + if (!gcdFactor) return std::make_pair(std::move(newLhs), std::move(newRhs)); + if (std::abs(lhs.factor) < std::abs(rhs.factor)) { + if (lhs.factor < 0) gcdFactor = -gcdFactor; + } else { + if (rhs.factor < 0) gcdFactor = -gcdFactor; + } + + newLhs.factor = lhs.factor / gcdFactor; + newRhs.factor = rhs.factor / gcdFactor; + + std::unordered_map lhsSymbolMap; + std::unordered_map rhsSymbolMap; + for (SymbolicDim op : lhs.symbols) ++lhsSymbolMap[op]; + for (SymbolicDim op : rhs.symbols) ++rhsSymbolMap[op]; + + for (SymbolicDim op : lhs.symbols) { + auto it = rhsSymbolMap.find(op); + if (it != rhsSymbolMap.end() && op.getKnownNonSizeZero()) { + if (--it->second == 0) rhsSymbolMap.erase(it); + continue; + } + newLhs.symbols.push_back(op); + } + + for (SymbolicDim op : rhs.symbols) { + auto it = lhsSymbolMap.find(op); + if (it != lhsSymbolMap.end() && op.getKnownNonSizeZero()) { + if (--it->second == 0) lhsSymbolMap.erase(it); + continue; + } + newRhs.symbols.push_back(op); + } + + if (!newLhs.factor) newLhs.symbols.clear(); + if (!newRhs.factor) newRhs.symbols.clear(); + + return std::make_pair(std::move(newLhs), std::move(newRhs)); +} + +SymbolicDimProduct SymbolicDimMgr::simplifySymbolicDimProduct( + const SymbolicDimProduct& x) { + std::vector copied; + copied.reserve(x.symbols.size()); + for (SymbolicDim op : x.symbols) copied.push_back(getRootSymbolicDim(op)); + + sort(copied.begin(), copied.end(), [&](SymbolicDim lhs, SymbolicDim rhs) { + return compareSymbolicDimNames(lhs.getSymName(), rhs.getSymName()); + }); + SymbolicDimProduct newX; + newX.factor = x.factor; + for (SymbolicDim op : copied) { + if (!op.isDynamic()) { + newX.factor *= op.getValue(); + } else { + newX.symbols.push_back(op); + } + } + return newX; +} + const std::string SymbolicDimMgr::getNextName() { std::string name; do { @@ -123,4 +271,154 @@ bool SymbolicDimMgr::mapSymbolicDimEqual(SymbolicDim lhs, SymbolicDim rhs) { return true; } +SymbolicDimProduct* SymbolicDimMgr::symbolicDimProductDivide( + const SymbolicDimProduct& lhs, const SymbolicDimProduct& rhs) { + SymbolicDimProduct newLhs, newRhs; + std::tie(newLhs, newRhs) = simplifySymbolicDimProductPair(lhs, rhs); + + if (newLhs.factor == 0 || newRhs.factor == 0) return nullptr; + if (newLhs.factor % newRhs.factor != 0) return nullptr; + if (newLhs.symbols.size() < newRhs.symbols.size()) return nullptr; + + SymbolicDimProduct* result = new SymbolicDimProduct(); + result->factor = newLhs.factor / newRhs.factor; + + std::unordered_map symProcMap; + for (SymbolicDim sym : newRhs.symbols) ++symProcMap[sym]; + + for (SymbolicDim sym : newLhs.symbols) { + auto it = symProcMap.find(sym); + if (it == symProcMap.end()) { + result->symbols.push_back(sym); + continue; + } + if (--it->second == 0) { + symProcMap.erase(it); + continue; + } + } + + if (!symProcMap.empty()) return nullptr; + return result; +} + +bool SymbolicDimMgr::isMultipleOfKnownSymbolicDimProductEqualPair( + const SymbolicDimProduct& lhs, const SymbolicDimProduct& rhs) { + for (auto& pairOutter : productEqualityMap_) { + const SymbolicDimProduct& x = pairOutter.first; + auto factorX = symbolicDimProductDivide(lhs, x); + if (!factorX) continue; + for (auto& pairInner : pairOutter.second) { + if (!pairInner.second) continue; + const SymbolicDimProduct& y = pairInner.first; + auto factorY = symbolicDimProductDivide(rhs, y); + if (!factorY || (*factorX) != (*factorY)) continue; + return true; + } + } + + return false; +} + +bool SymbolicDimMgr::updateProductEqualityMap() { + // early return if nothing is updated. + if (productEqualityMapUpdated_) return true; + + SymbolicDimProductMap newMap; + std::unordered_set productSet; + for (auto& pairOutter : productEqualityMap_) { + const SymbolicDimProduct& x = pairOutter.first; + for (auto& pairInner : pairOutter.second) { + if (!pairInner.second) continue; + const SymbolicDimProduct& y = pairInner.first; + SymbolicDimProduct newX, newY; + std::tie(newX, newY) = simplifySymbolicDimProductPair(x, y); + if (newX == newY) continue; + newMap[newX][newY] = newMap[newY][newX] = true; + productSet.insert(newX); + productSet.insert(newY); + } + } + // hash function of SymbolicDimProduct is expensive, thus we map it to integer + // domain first. + std::unordered_map symProd2Idx; + std::vector idx2SymProd(productSet.size()); + std::vector idx2root(productSet.size()); + for (auto& x : productSet) { + size_t idx = symProd2Idx.size(); + symProd2Idx[&x] = idx; + idx2SymProd[idx] = &x; + idx2root[idx] = idx; + } + + auto getRootIdx = [&](size_t root) { + std::vector path; + while (idx2root[root] != root) { + path.push_back(root); + root = idx2root[root]; + } + for (size_t idx : path) idx2root[idx] = root; + return root; + }; + + for (size_t x = 0; x < symProd2Idx.size(); ++x) { + auto& xProd = *idx2SymProd[x]; + auto& rowMap = newMap[xProd]; + size_t xRoot = getRootIdx(x); + for (size_t y = x; y < symProd2Idx.size(); ++y) { + auto& yProd = *idx2SymProd[y]; + if (!rowMap[yProd]) continue; + idx2root[getRootIdx(y)] = xRoot; + } + } + + for (size_t x = 0; x < symProd2Idx.size(); ++x) + for (size_t y = x; y < symProd2Idx.size(); ++y) { + if (getRootIdx(x) != getRootIdx(y)) continue; + auto& xSymProd = *idx2SymProd[x]; + auto& ySymProd = *idx2SymProd[y]; + + newMap[xSymProd][ySymProd] = newMap[ySymProd][xSymProd] = true; + } + + productEqualityMap_ = std::move(newMap); + + for (auto& x : productSet) + for (auto& y : productSet) { + if (!productEqualityMap_[x][y]) continue; + productEqualityMap_[x][y] = productEqualityMap_[y][x] = false; + if (!isMultipleOfKnownSymbolicDimProductEqualPair(x, y)) { + productEqualityMap_[x][y] = productEqualityMap_[y][x] = true; + } + } + + std::unordered_set toRemove; + for (auto& x : productSet) { + if (std::all_of(productSet.begin(), + productSet.end(), + [&](const SymbolicDimProduct& y) { + return !productEqualityMap_[x][y]; + })) { + toRemove.insert(x); + } + } + + for (auto& x : toRemove) { + productEqualityMap_.erase(x); + } + + productEqualityMapUpdated_ = true; + return true; +} + +bool SymbolicDimMgr::isSymbolicDimProductEqual(const SymbolicDimProduct& lhs, + const SymbolicDimProduct& rhs) { + SymbolicDimProduct newLhs, newRhs; + std::tie(newLhs, newRhs) = simplifySymbolicDimProductPair(lhs, rhs); + + // early return for identity case. + if (newLhs == newRhs) return true; + IR_ENFORCE(updateProductEqualityMap(), "Update product equality map failed."); + return isMultipleOfKnownSymbolicDimProductEqualPair(newLhs, newRhs); +} } // namespace ir diff --git a/paddle/ir/dialect/shape/utils/shape_utils.h b/paddle/ir/dialect/shape/utils/shape_utils.h index 70f2a16c4481e..8d5fab1a1c811 100644 --- a/paddle/ir/dialect/shape/utils/shape_utils.h +++ b/paddle/ir/dialect/shape/utils/shape_utils.h @@ -18,6 +18,7 @@ #include #include #include +#include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_op.h" #include "paddle/ir/core/utils.h" #include "paddle/ir/dialect/shape/ir/shape_op.h" @@ -45,7 +46,6 @@ class SymbolTable { public: explicit SymbolTable(ir::Operation* symbolTableOp) : symbolTableOp_(symbolTableOp) {} - template typename std::enable_if::value, SymbolicDim>::type @@ -97,6 +97,7 @@ struct SymProductHasher { class SymbolicDimMgr { public: explicit SymbolicDimMgr(ir::ModuleOp m); + bool load(); SymbolicDim newSymbolicDim(const std::string& name = {}); SymbolicDim newConstantSymbolicDim(int64_t val); std::vector createSymbolicDimsForRankedValue(Value value); @@ -104,9 +105,28 @@ class SymbolicDimMgr { bool isSymbolicDimEqual(SymbolicDim lhs, SymbolicDim rhs); SymbolTable& symbolTable() { return symbolTable_; } bool mapSymbolicDimEqual(SymbolicDim lhs, SymbolicDim rhs); + SymbolicDimProduct simplifySymbolicDimProduct(const SymbolicDimProduct& x); + std::pair + simplifySymbolicDimProductPair(const SymbolicDimProduct& x, + const SymbolicDimProduct& y); + SymbolicDimProduct* symbolicDimProductDivide(const SymbolicDimProduct& x, + const SymbolicDimProduct& y); + + bool save(); // TODO(liujinnan): load constraint func + + bool isSymbolicDimProductEqual(const SymbolicDimProduct& lhs, + const SymbolicDimProduct& rhs); + bool mapSymbolicDimProductEqual(const SymbolicDimProduct& lhs, + const SymbolicDimProduct& rhs); private: const std::string getNextName(); + bool updateProductEqualityMap(); + bool isMultipleOfKnownSymbolicDimProductEqualPair( + const SymbolicDimProduct& lhs, const SymbolicDimProduct& rhs); + bool saveShapeConstraintGraph(); // TODO(liujinnan): load & save + // shape_constraint_func + bool loadShapeConstraintGraph(); private: ir::ModuleOp m_; @@ -127,6 +147,6 @@ class SymbolicDimMgr { std::unordered_map, SymProductHasher>; SymbolicDimProductMap productEqualityMap_; + bool productEqualityMapUpdated_ = true; }; - } // namespace ir diff --git a/test/cpp/ir/shape_dialect/symbolic_op_test.cc b/test/cpp/ir/shape_dialect/symbolic_op_test.cc index 7b0751d17ac13..138e5e5b0d8c9 100644 --- a/test/cpp/ir/shape_dialect/symbolic_op_test.cc +++ b/test/cpp/ir/shape_dialect/symbolic_op_test.cc @@ -93,7 +93,10 @@ TEST(assist_struct_test, symbolic_dim_table) { EXPECT_FALSE(symbolTable.lookup("S1")); } -TEST(assist_struct_test, symbolic_dim_mgr) { +TEST(assist_struct_test, symbolic_dim_mgr_simple) { + /******************************************************/ + /* Mgr simple version, only SymbolicDim related func. */ + /******************************************************/ ir::IrContext *ctx = ir::IrContext::Instance(); ir::Program program(ctx); ctx->GetOrRegisterDialect(); @@ -141,6 +144,175 @@ TEST(assist_struct_test, symbolic_dim_mgr) { EXPECT_FALSE(symDimMgr.isSymbolicDimEqual(symDimS0, symDimC10)); } +TEST(assist_struct_test, symbolic_dim_mgr_complex) { + /***************************************************************/ + /* Mgr with constraintOp, and SymbolicDimProduct related func. */ + /***************************************************************/ + ir::IrContext *ctx = ir::IrContext::Instance(); + ir::Program program(ctx); + ctx->GetOrRegisterDialect(); + ctx->GetOrRegisterDialect(); + ir::Builder builder = ir::Builder(ctx, program.block()); + + ir::dialect::SymbolicDim symDimS0 = builder.Build( + "S0", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS1 = builder.Build( + "S1", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS2 = builder.Build( + "S2", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS3 = builder.Build( + "S3", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS4 = builder.Build( + "S4", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS5 = builder.Build( + "S5", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS6 = builder.Build( + "S6", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS7 = builder.Build( + "S7", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS8 = builder.Build( + "S8", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS9 = builder.Build( + "S9", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS10 = builder.Build( + "S10", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS11 = builder.Build( + "S11", -100000, false, false, true, true); + ir::dialect::SymbolicDim symDimS12 = builder.Build( + "S12", -100000, false, false, true, false); + ir::dialect::SymbolicDim symDimC10 = builder.Build( + "C10", 10, true, false, true, true); + ir::dialect::SymbolicDim symDimC20 = builder.Build( + "C20", 20, true, false, true, true); + + ir::OpResult dimOpS0 = builder.Build("S0").out(); + ir::OpResult dimOpS1 = builder.Build("S1").out(); + ir::OpResult dimOpS2 = builder.Build("S2").out(); + ir::OpResult dimOpS3 = builder.Build("S3").out(); + ir::OpResult dimOpS4 = builder.Build("S4").out(); + ir::OpResult dimOpS5 = builder.Build("S5").out(); + ir::OpResult dimOpS6 = builder.Build("S6").out(); + ir::OpResult dimOpS7 = builder.Build("S7").out(); + ir::OpResult dimOpS8 = builder.Build("S8").out(); + ir::OpResult dimOpS9 = builder.Build("S9").out(); + ir::OpResult dimOpS10 = builder.Build("S10").out(); + ir::OpResult dimOpS11 = builder.Build("S11").out(); + ir::OpResult dimOpC10 = builder.Build("C10").out(); + ir::OpResult dimOpC20 = builder.Build("C20").out(); + ir::OpResult constant = + builder + .Build(ir::Int32Attribute::get(ctx, 2), + ir::Int32Type::get(ctx)) + ->result(0); + + // Mark S1 == S2. + builder.Build( + 2, 2, std::vector{constant, dimOpS1, dimOpS2, constant}); + // Mark S0 * S1 == S2 * S3, For check S0 == S3. + builder.Build( + 2, 2, std::vector{dimOpS0, dimOpS1, dimOpS2, dimOpS3}); + // Mark S4 * S0 * S1 == S2 * S3 * S5, For check S4 == S5. + builder.Build( + 3, + 3, + std::vector{ + dimOpS4, dimOpS0, dimOpS1, dimOpS2, dimOpS3, dimOpS5}); + // For check S6 == C10 * C20. + builder.Build( + 1, 2, std::vector{dimOpS6, dimOpC10, dimOpC20}); + // Mark C10 * S0 * S1 == S2 * S3 * S7, for check C10 == S7. + builder.Build( + 3, + 3, + std::vector{ + dimOpC10, dimOpS0, dimOpS1, dimOpS2, dimOpS3, dimOpS7}); + + // Mark S8 * S9 == S10 * S11, for unsimplify product case + builder.Build( + 2, 2, std::vector{dimOpS8, dimOpS9, dimOpS10, dimOpS11}); + + ir::SymbolicDimMgr symDimMgr(program.module_op()); + + symDimMgr.load(); + + // For check indirect equality: S1 * S4 == S2 * S5 + ir::SymbolicDimProduct symDimProductLhs; + ir::SymbolicDimProduct symDimProductRhs; + + symDimProductLhs.symbols.push_back(symDimS1); + symDimProductLhs.symbols.push_back(symDimS4); + + symDimProductRhs.symbols.push_back(symDimS2); + symDimProductRhs.symbols.push_back(symDimS5); + + // For uncompletely simplied product check: S8 * S9 * S12 == S10 * S11 * S12 + ir::SymbolicDimProduct symDimProductLhs_; + ir::SymbolicDimProduct symDimProductRhs_; + + symDimProductLhs_.symbols.push_back(symDimS8); + symDimProductLhs_.symbols.push_back(symDimS9); + symDimProductLhs_.symbols.push_back(symDimS12); + + symDimProductRhs_.symbols.push_back(symDimS10); + symDimProductRhs_.symbols.push_back(symDimS11); + symDimProductRhs_.symbols.push_back(symDimS12); + + // For check simplifySymbolicDimProduct, {factor = 1, Sym = {S7}} => {factor = + // 10} + ir::SymbolicDimProduct symDimProductS7; + symDimProductS7.symbols.push_back(symDimS7); + ir::SymbolicDimProduct simplifiedProductS7 = + symDimMgr.simplifySymbolicDimProduct(symDimProductS7); + + // For check simplifySymbolicDimProductPair, X * Y * Y, Y * Y * Z => X, Z + ir::SymbolicDimProduct symDimProductPairLhs; + ir::SymbolicDimProduct symDimProductPairRhs; + ir::SymbolicDimProduct newLhs, newRhs; + symDimProductPairLhs.symbols.push_back(symDimS4); + symDimProductPairLhs.symbols.push_back(symDimS1); + symDimProductPairLhs.symbols.push_back(symDimS2); + symDimProductPairRhs.symbols.push_back(symDimS1); + symDimProductPairRhs.symbols.push_back(symDimS2); + symDimProductPairRhs.symbols.push_back(symDimS3); + + std::tie(newLhs, newRhs) = symDimMgr.simplifySymbolicDimProductPair( + symDimProductPairLhs, symDimProductPairRhs); + + // For check symbolicDimProductDivide, {S4 * S1 * C20} / {S1 * C10} => {factor + // = 2 Sym = {S4}} + ir::SymbolicDimProduct symDimProductDivLhs; + ir::SymbolicDimProduct symDimProductDivRhs; + symDimProductDivLhs.symbols.push_back(symDimS4); + symDimProductDivLhs.symbols.push_back(symDimS1); + symDimProductDivLhs.symbols.push_back(symDimC20); + symDimProductDivRhs.symbols.push_back(symDimS1); + symDimProductDivRhs.symbols.push_back(symDimC10); + + ir::SymbolicDimProduct *divRes = symDimMgr.symbolicDimProductDivide( + symDimProductDivLhs, symDimProductDivRhs); + + EXPECT_TRUE(symDimMgr.isSymbolicDimEqual(symDimS1, symDimS2)); + EXPECT_TRUE(symDimMgr.isSymbolicDimEqual(symDimS0, symDimS3)); + EXPECT_TRUE(symDimMgr.isSymbolicDimEqual(symDimS4, symDimS5)); + EXPECT_EQ(symDimS6.getValue(), 200); + EXPECT_EQ(symDimMgr.symbolTable().lookup("C20"), + symDimC20); + EXPECT_EQ(symDimS7.getValue(), symDimC10.getValue()); + EXPECT_EQ(simplifiedProductS7.factor, 10); + EXPECT_EQ(simplifiedProductS7.symbols.size(), static_cast(0)); + EXPECT_EQ(newLhs.symbols.size(), static_cast(1)); + EXPECT_EQ(newRhs.symbols.size(), static_cast(1)); + EXPECT_EQ(newLhs.symbols[0], symDimMgr.getRootSymbolicDim(symDimS4)); + EXPECT_EQ(newRhs.symbols[0], symDimMgr.getRootSymbolicDim(symDimS3)); + EXPECT_EQ(divRes->factor, 2); + EXPECT_EQ(divRes->symbols.size(), static_cast(1)); + EXPECT_EQ(divRes->symbols[0], symDimMgr.getRootSymbolicDim(symDimS4)); + EXPECT_TRUE( + symDimMgr.isSymbolicDimProductEqual(symDimProductLhs, symDimProductRhs)); + EXPECT_TRUE(symDimMgr.isSymbolicDimProductEqual(symDimProductLhs_, + symDimProductRhs_)); +} + TEST(assist_struct_test, dim) { ir::IrContext *ctx = ir::IrContext::Instance(); ir::Program program(ctx); From 3bcc91e4a1cb661faa489940598e4b0221b0d668 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Wed, 6 Sep 2023 14:19:45 +0800 Subject: [PATCH 150/194] add reorder_block_ops_pass (#56990) --- paddle/ir/core/block.cc | 33 ++++++ paddle/ir/core/block.h | 4 + .../ir/transforms/reorder_block_ops_pass.cc | 105 ++++++++++++++++++ paddle/ir/transforms/reorder_block_ops_pass.h | 26 +++++ .../pattern_rewrite/pattern_rewrite_test.cc | 2 + 5 files changed, 170 insertions(+) create mode 100644 paddle/ir/transforms/reorder_block_ops_pass.cc create mode 100644 paddle/ir/transforms/reorder_block_ops_pass.h diff --git a/paddle/ir/core/block.cc b/paddle/ir/core/block.cc index f99ec340e4c49..04d59e2582ebe 100644 --- a/paddle/ir/core/block.cc +++ b/paddle/ir/core/block.cc @@ -13,6 +13,9 @@ // limitations under the License. #include "paddle/ir/core/block.h" + +#include + #include "paddle/ir/core/enforce.h" #include "paddle/ir/core/operation.h" #include "paddle/ir/core/region.h" @@ -60,4 +63,34 @@ Block::UseIterator Block::use_end() const { return Block::UseIterator(); } bool Block::HasOneUse() const { return first_use_ && !first_use_.next_use(); } +void Block::ResetOpListOrder(const OpListType &new_op_list) { + IR_ENFORCE(new_op_list.size() == ops_.size(), + "The size of new_op_list not same with ops_."); + IR_ENFORCE(TopoOrderCheck(new_op_list), + "The new_op_list is not in topological order."); + + ops_.clear(); + for (Operation *op : new_op_list) { + push_back(op); + } +} + +bool Block::TopoOrderCheck(const OpListType &op_list) { + std::unordered_set visited_values; + for (const Operation *op : op_list) { + if (op->num_operands() > 0) { + for (size_t i = 0; i < op->num_operands(); ++i) { + auto operand = op->operand_source(i); + if (operand && visited_values.count(op->operand_source(i)) == 0) { + return false; + } + } + } + for (size_t i = 0; i < op->results().size(); ++i) { + visited_values.insert(op->result(i)); + } + } + return true; +} + } // namespace ir diff --git a/paddle/ir/core/block.h b/paddle/ir/core/block.h index ebe4b6cb8ecf4..2cf00037eb5fc 100644 --- a/paddle/ir/core/block.h +++ b/paddle/ir/core/block.h @@ -70,6 +70,8 @@ class IR_API Block { bool HasOneUse() const; BlockOperand *first_use_addr() { return &first_use_; } + void ResetOpListOrder(const OpListType &new_op_list); + private: Block(Block &) = delete; Block &operator=(const Block &) = delete; @@ -78,6 +80,8 @@ class IR_API Block { friend class Region; void SetParent(Region *parent, Region::iterator position); + static bool TopoOrderCheck(const OpListType &op_list); + private: Region *parent_; // not owned OpListType ops_; // owned diff --git a/paddle/ir/transforms/reorder_block_ops_pass.cc b/paddle/ir/transforms/reorder_block_ops_pass.cc new file mode 100644 index 0000000000000..d922326677985 --- /dev/null +++ b/paddle/ir/transforms/reorder_block_ops_pass.cc @@ -0,0 +1,105 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/ir/transforms/reorder_block_ops_pass.h" + +#include + +#include "paddle/ir/core/builtin_op.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/pass/pass.h" + +namespace { + +// TODO(wilber): After support SideEffectTrait, Only NoSideEffectTrait op can be +// removed by dce pass. +// Now just a naive implementation. +class ReorderBlockOpsPass : public ir::Pass { + public: + ReorderBlockOpsPass() : ir::Pass("ReorderBlockOpsPass", 0) {} + + void Run(ir::Operation *op) override { + IR_ENFORCE(op->num_regions() > 0, + "ReorderBlockOpsPass should run on Operation which regions " + "number greater than 0."); + for (size_t i = 0; i < op->num_regions(); ++i) { + for (auto *block : op->region(i)) { + std::list res_op_list; + std::unordered_map + reorder_op_dep_cnt; // op -> dependent input count + std::unordered_set visited_values; + std::queue op_que; + + auto update_op_que = [&](ir::Operation *op) { + for (size_t i = 0; i < op->results().size(); ++i) { + auto result = op->result(i); + visited_values.insert(result); + for (auto it = result.use_begin(); it != result.use_end(); ++it) { + if (reorder_op_dep_cnt.count(it->owner())) { + reorder_op_dep_cnt[it->owner()]--; + if (reorder_op_dep_cnt[it->owner()] == 0) { + op_que.push(it->owner()); + } + } + } + } + }; + + for (auto &op : *block) { + bool has_dependency = false; + if (op->num_operands() > 0) { + for (size_t i = 0; i < op->num_operands(); ++i) { + auto operand = op->operand_source(i); + if (operand && visited_values.count(op->operand_source(i)) == 0) { + reorder_op_dep_cnt[op]++; + has_dependency = true; + } + } + } + if (!has_dependency) { + res_op_list.push_back(op); + update_op_que(op); + } + } + + if (reorder_op_dep_cnt.empty()) { + return; + } + + while (!op_que.empty()) { + auto *op = op_que.front(); + op_que.pop(); + res_op_list.push_back(op); + update_op_que(op); + } + VLOG(4) << "ReorderBlockOpsPass is applied."; + block->ResetOpListOrder(res_op_list); + } + } + } + + bool CanApplyOn(ir::Operation *op) const override { + return op->num_regions() > 0; + } +}; + +} // namespace + +namespace ir { + +std::unique_ptr CreateReorderBlockOpsPass() { + return std::make_unique(); +} + +} // namespace ir diff --git a/paddle/ir/transforms/reorder_block_ops_pass.h b/paddle/ir/transforms/reorder_block_ops_pass.h new file mode 100644 index 0000000000000..f668471fc9e04 --- /dev/null +++ b/paddle/ir/transforms/reorder_block_ops_pass.h @@ -0,0 +1,26 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include +#include "paddle/ir/core/dll_decl.h" + +namespace ir { + +class Pass; + +IR_API std::unique_ptr CreateReorderBlockOpsPass(); + +} // namespace ir diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index e007b73c9f0ed..fcca8cde7d5aa 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -42,6 +42,7 @@ #include "paddle/ir/pattern_rewrite/pattern_match.h" #include "paddle/ir/pattern_rewrite/pattern_rewrite_driver.h" #include "paddle/ir/transforms/dead_code_elimination_pass.h" +#include "paddle/ir/transforms/reorder_block_ops_pass.h" #include "paddle/phi/core/kernel_registry.h" // NOTE(zhangbo9674): File pd_op.h is generated by op_gen.py, see details in @@ -1099,6 +1100,7 @@ TEST(pattern_rewrite, Patterns) { pm.AddPass(std::make_unique()); pm.AddPass(ir::CreateConstantFoldingPass()); pm.AddPass(ir::CreateDeadCodeEliminationPass()); + pm.AddPass(ir::CreateReorderBlockOpsPass()); pm.EnablePassTiming(); pm.EnableIRPrinting(); // pm.EnableIRPrinting(std::make_unique( From c62902ee7d895572ccb4ba8ddbe2a7304cdc6c1c Mon Sep 17 00:00:00 2001 From: zhanglirong1999 <56445728+zhanglirong1999@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:34:43 +0800 Subject: [PATCH 151/194] [ONEDNN] fix accuracy issue of fc when the input shapes are dynamic --- paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc | 11 ++++++-- paddle/phi/backends/onednn/onednn_helper.h | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc index 18680fe678b5d..a7f6bc512ffce 100644 --- a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc @@ -284,7 +284,13 @@ class FCMKLDNNHandler std::shared_ptr AcquireWeightsMemoryWithReorder( const phi::DenseTensor* weights, const std::vector& scale_data) { - const std::string weights_key = this->memory_key_ + "@weights"; + const std::string weights_base_key = this->memory_key_ + "@weights"; + std::string weights_key; + weights_key.reserve(128); + weights_key = phi::funcs::ExtendKeyWithThreadInfoIfNeeded( + dev_ctx_, + phi::funcs::CreateKey( + dev_ctx_, weights_base_key, this->fwd_pd_->weights_desc())); auto memory_p = std::static_pointer_cast( this->dev_ctx_.GetBlob(weights_key)); @@ -410,7 +416,8 @@ class FCMKLDNNKernel : public framework::OpKernel { phi::funcs::CreateKey(dev_ctx, ctx.InputName("Input"), ctx.InputName("W"), - phi::vectorize(x->dims()))); + phi::vectorize(x->dims()), + phi::vectorize(weights->dims()))); auto inner_product_cache = std::static_pointer_cast(dev_ctx.GetBlob(cache_key)); diff --git a/paddle/phi/backends/onednn/onednn_helper.h b/paddle/phi/backends/onednn/onednn_helper.h index 84e36a26ca487..1d61004b36161 100644 --- a/paddle/phi/backends/onednn/onednn_helper.h +++ b/paddle/phi/backends/onednn/onednn_helper.h @@ -154,6 +154,12 @@ inline void AppendKey(std::string* key, const T& num) { key->append(std::to_string(num)); } +template <> +inline void AppendKey(std::string* key, + const dnnl::memory::format_kind& format) { + key->append(std::to_string(static_cast(format))); +} + template <> inline void AppendKey(std::string* key, const dnnl::memory::format_tag& format) { @@ -171,6 +177,25 @@ inline void AppendKey(std::string* key, const dnnl::algorithm& algorithm) { key->append(std::to_string(static_cast(algorithm))); } +template <> +inline void AppendKey(std::string* key, const dnnl::memory::dims& dims) { + for (size_t i = 0; i < dims.size(); i++) { + AppendKey(key, static_cast(dims[i])); + } +} + +template <> +inline void AppendKey(std::string* key, const dnnl::memory::desc& md) { + AppendKey(key, md.get_dims()); + AppendKey(key, md.get_data_type()); + AppendKey(key, md.get_format_kind()); + AppendKey(key, md.get_inner_blks()); + AppendKey(key, md.get_inner_idxs()); + AppendKey(key, md.get_inner_nblks()); + AppendKey(key, md.get_padded_dims()); + AppendKey(key, md.get_strides()); +} + template <> inline void AppendKey(std::string* key, const dnnl::normalization_flags& flags) { From 3eafa1fc54cf57713e58b6d694826ab9c1868ca4 Mon Sep 17 00:00:00 2001 From: Xianduo Li <30922914+lxd-cumt@users.noreply.github.com> Date: Wed, 6 Sep 2023 14:36:36 +0800 Subject: [PATCH 152/194] Auto codegen for supporting calling new_ir api in static operants (#56955) * support new ir primitive operator in static operants * support more vjp code gen * support more vjp code gen * support more vjp code gen * use code gen * fix operants codegen * support more vjp code gen * Fix ci build error * set FLAGS_tensor_operants_mode to static in generated_vjp for testing * fix bugs * change the order of ops_name of divide_grad * replace FLAGS_enable_new_ir_in_executor by FLAGS_enable_new_ir_api in codegen and test_vjp_prim --------- Co-authored-by: Charles-hit Co-authored-by: 0x45f --- .../tensor_operants_gen.py | 82 +++++++++++++++---- paddle/fluid/prim/utils/static/CMakeLists.txt | 2 +- .../rule/vjp/generated/generated_vjp.cc.j2 | 3 + paddle/fluid/primitive/rule/vjp/details.h | 7 +- paddle/phi/core/extended_tensor.cc | 2 +- test/prim/new_ir_prim/test_vjp_prim.py | 6 +- 6 files changed, 80 insertions(+), 22 deletions(-) diff --git a/paddle/fluid/prim/api/auto_code_generated/tensor_operants_gen.py b/paddle/fluid/prim/api/auto_code_generated/tensor_operants_gen.py index 8322b0ba2be83..783066f0fc906 100644 --- a/paddle/fluid/prim/api/auto_code_generated/tensor_operants_gen.py +++ b/paddle/fluid/prim/api/auto_code_generated/tensor_operants_gen.py @@ -211,6 +211,11 @@ class StaticTensorOperants : public TensorOperantsBase { #include "paddle/fluid/prim/api/manual_prim/prim_manual_api.h" #include "paddle/fluid/prim/utils/static/desc_tensor.h" +#include "paddle/fluid/primitive/backend/backend.h" +#include "paddle/fluid/primitive/type/lazy_tensor.h" + +PHI_DECLARE_bool(enable_new_ir_api); + """ @@ -219,47 +224,88 @@ class StaticTensorOperants : public TensorOperantsBase { namespace prim { using DescTensor = paddle::prim::DescTensor; +using LazyTensor = paddle::primitive::LazyTensor; Tensor StaticTensorOperants::add(const Tensor& x, const Scalar& y) { - return paddle::prim::add(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::add(x, paddle::primitive::backend::full(x.shape(), y, x.dtype(), x.place())); + } else { + return paddle::prim::add(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + } } Tensor StaticTensorOperants::subtract(const Tensor& x, const Scalar& y) { - return paddle::prim::subtract(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::subtract(x, paddle::primitive::backend::full(x.shape(), y, x.dtype(), x.place())); + } else { + return paddle::prim::subtract(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + } } Tensor StaticTensorOperants::multiply(const Tensor& x, const Scalar& y) { - return paddle::prim::scale(x, y, 0.0f, true); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::scale(x, y, 0.0f, true); + } else { + return paddle::prim::scale(x, y, 0.0f, true); + } } Tensor StaticTensorOperants::divide(const Tensor& x, const Scalar& y) { - return paddle::prim::divide(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::divide(x, paddle::primitive::backend::full(x.shape(), y, x.dtype(), x.place())); + } else { + return paddle::prim::divide(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + } } Tensor StaticTensorOperants::add(const Scalar& x, const Tensor& y) { - return paddle::prim::add(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::add(paddle::primitive::backend::full(y.shape(), x, y.dtype(), y.place()), y); + } else { + return paddle::prim::add(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + } } + Tensor StaticTensorOperants::subtract(const Scalar& x, const Tensor& y) { - return paddle::prim::subtract(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::subtract(paddle::primitive::backend::full(y.shape(), x, y.dtype(), y.place()), y); + } else { + return paddle::prim::subtract(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + } } Tensor StaticTensorOperants::multiply(const Scalar& x, const Tensor& y) { - return paddle::prim::scale(y, x, 0.0f, true); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::scale(y, x, 0.0f, true); + } else { + return paddle::prim::scale(y, x, 0.0f, true); + } } Tensor StaticTensorOperants::divide(const Scalar& x, const Tensor& y) { - return paddle::prim::divide(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::divide(paddle::primitive::backend::full(y.shape(), x, y.dtype(), y.place()), y); + } else { + return paddle::prim::divide(paddle::prim::full(y.shape(), x, y.dtype(), y.place()), y); + } } Tensor StaticTensorOperants::pow(const Tensor& x, const Tensor& y) { - return paddle::prim::elementwise_pow(x, y); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::elementwise_pow(x, y); + } else { + return paddle::prim::elementwise_pow(x, y); + } } Tensor StaticTensorOperants::pow(const Tensor& x, const Scalar& y) { - return paddle::prim::elementwise_pow(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + if (FLAGS_enable_new_ir_api) { + return paddle::primitive::backend::elementwise_pow(x, paddle::primitive::backend::full(x.shape(), y, x.dtype(), x.place())); + } else { + return paddle::prim::elementwise_pow(x, paddle::prim::full(x.shape(), y, x.dtype(), x.place())); + } } - """ @@ -339,13 +385,21 @@ def gene_eager_tensor_operants_implementation(self): def gene_static_tensor_func_call(self): api_func_name = self.get_api_func_name() - + backend_static_func_name = ( + 'paddle::primitive::backend::' + api_func_name + '' + ) prim_static_func_name = ( 'paddle::prim::' + api_func_name + '' ) - prim_static_func_parameters = self.get_func_args() + static_func_parameters = self.get_func_args() + + static_tensor_func_call = f"""if (FLAGS_enable_new_ir_api) {{ + return {backend_static_func_name}({static_func_parameters}); + }} else {{ + return {prim_static_func_name}({static_func_parameters}); + }}""" - return f"""return {prim_static_func_name}({prim_static_func_parameters});""" + return static_tensor_func_call def gene_static_tensor_operants_implementation(self): api_code = "" diff --git a/paddle/fluid/prim/utils/static/CMakeLists.txt b/paddle/fluid/prim/utils/static/CMakeLists.txt index aa72fadb591a6..483c3eabc05d1 100644 --- a/paddle/fluid/prim/utils/static/CMakeLists.txt +++ b/paddle/fluid/prim/utils/static/CMakeLists.txt @@ -6,4 +6,4 @@ cc_library( cc_library( static_tensor_operants SRCS static_tensor_operants.cc - DEPS static_prim_api) + DEPS static_prim_api primitive_backend_static_experimental) diff --git a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 index ab040254355f5..6d69433737633 100644 --- a/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 +++ b/paddle/fluid/primitive/codegen/templates/rule/vjp/generated/generated_vjp.cc.j2 @@ -10,7 +10,9 @@ #include "paddle/fluid/primitive/type/lazy_tensor.h" #include "paddle/fluid/primitive/utils/utils.h" #include "paddle/ir/core/operation.h" +#include "paddle/phi/core/flags.h" +PHI_DECLARE_string(tensor_operants_mode); namespace paddle { namespace primitive { @@ -95,6 +97,7 @@ for (size_t i=0; i< stop_gradients[0].size(); i++ ) { {% endmacro %} {% macro body_prim(api) %} +FLAGS_tensor_operants_mode = "static"; {% for i in range(api.outputs|length) %} {% if api.outputs[i].typename=='Tensor' %} paddle::Tensor* {{api.outputs[i].name}} = !stop_gradients[{{i}}][0] ? &vjp_res[{{i}}][0] : nullptr; diff --git a/paddle/fluid/primitive/rule/vjp/details.h b/paddle/fluid/primitive/rule/vjp/details.h index e018cccdef7a0..12fb66127a298 100644 --- a/paddle/fluid/primitive/rule/vjp/details.h +++ b/paddle/fluid/primitive/rule/vjp/details.h @@ -39,10 +39,7 @@ void divide_grad(const Tensor& x, Tensor* dy) { if (dy) { // dy = -(x/y^2) * dout - auto denominator = - elementwise_pow(y, full(y.shape(), 2.0, y.dtype(), y.place())); - auto dy_res = scale( - multiply(divide(x, denominator), out_grad), -1.0, 0.0, true); + auto dy_res = -(x / y.pow(2.0)) * out_grad; if (x.dims() != y.dims()) { // Maybe need reduce here phi::DDim reduce_dim = get_reduce_dims(y.dims(), x.dims()); @@ -61,7 +58,7 @@ void divide_grad(const Tensor& x, if (dx) { // dx = (1/y) * dout auto one_tensor = full(phi::vectorize(y.dims()), 1.0, y.dtype()); - auto dx_res = multiply(divide(one_tensor, y), out_grad); + auto dx_res = one_tensor / y * out_grad; if (y.dims() != x.dims()) { // Maybe need reduce here auto reduce_dim = get_reduce_dims(x.dims(), y.dims()); diff --git a/paddle/phi/core/extended_tensor.cc b/paddle/phi/core/extended_tensor.cc index e5b5c3773f867..31d0fb25c88c1 100644 --- a/paddle/phi/core/extended_tensor.cc +++ b/paddle/phi/core/extended_tensor.cc @@ -38,7 +38,7 @@ DataType ExtendedTensor::dtype() const { DataLayout ExtendedTensor::layout() const { PADDLE_THROW(phi::errors::Unavailable( - "ExtendedTensor does not support `dtype` method.")); + "ExtendedTensor does not support `layout` method.")); } bool ExtendedTensor::valid() const { diff --git a/test/prim/new_ir_prim/test_vjp_prim.py b/test/prim/new_ir_prim/test_vjp_prim.py index 2a29ae9f69fc2..22309a08823ec 100644 --- a/test/prim/new_ir_prim/test_vjp_prim.py +++ b/test/prim/new_ir_prim/test_vjp_prim.py @@ -63,6 +63,7 @@ class TestVjpPrim(unittest.TestCase): def test_divide_grad_prim_case1(self): newir_program = get_ir_divide_program() paddle.framework.core._set_prim_backward_enabled(True) + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) dout = newir_program.block().ops[-2].result(0) out_grads = [[dout]] stop_gradients = [[False], [False]] @@ -83,9 +84,9 @@ def test_divide_grad_prim_case1(self): "pd.full", "pd.elementwise_pow", "pd.divide", - "pd.multiply", "pd.full", "pd.scale", + "pd.multiply", "pd.full_int_array", "pd.sum", "pd.full_int_array", @@ -101,6 +102,7 @@ def test_divide_grad_prim_case1(self): for idx, op in enumerate(newir_program.block().ops): self.assertEqual(op.name(), all_op_names[idx]) paddle.framework.core._set_prim_backward_enabled(False) + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) def test_divide_grad_no_prim(self): newir_program = get_ir_divide_program() @@ -123,6 +125,7 @@ def test_divide_grad_no_prim(self): def test_sum_grad_prim(self): newir_program = get_ir_sum_program() paddle.framework.core._set_prim_backward_enabled(True) + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) dout = newir_program.block().ops[-3].result(0) out_grads = [[dout]] stop_gradients = [[False], [True]] @@ -147,6 +150,7 @@ def test_sum_grad_prim(self): for idx, op in enumerate(newir_program.block().ops): self.assertEqual(op.name(), all_op_names[idx]) paddle.framework.core._set_prim_backward_enabled(False) + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) def test_sum_grad_no_prim(self): newir_program = get_ir_sum_program() From 6d0ef3420e898ad23d05996c3578a6f513481b10 Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Wed, 6 Sep 2023 14:56:16 +0800 Subject: [PATCH 153/194] =?UTF-8?q?=E3=80=90New=20IR=E3=80=91New=20ir=20op?= =?UTF-8?q?=20test=20v1.2=20=20(#56931)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * div passed v1.0 * IrChange->IrGuard & optimize static input dtype * clean * remove IrChange and optimize IrGuard --- python/paddle/__init__.py | 4 +-- python/paddle/new_ir_utils.py | 24 +++++-------- python/paddle/static/input.py | 37 +++++++------------ test/legacy_test/eager_op_test.py | 60 ++++++++++++++++--------------- 4 files changed, 55 insertions(+), 70 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 666935934235f..cc59217e480cc 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -479,9 +479,9 @@ disable_static() -from .new_ir_utils import IrChange # noqa: F401 +from .new_ir_utils import IrGuard # noqa: F401 -ir_change = IrChange() +ir_change = IrGuard() ir_change._switch_to_new_ir() __all__ = [ # noqa diff --git a/python/paddle/new_ir_utils.py b/python/paddle/new_ir_utils.py index 83c9b5f826d8d..88a5415e3ec3e 100644 --- a/python/paddle/new_ir_utils.py +++ b/python/paddle/new_ir_utils.py @@ -15,10 +15,8 @@ import paddle -from .fluid.wrapped_decorator import signature_safe_contextmanager - -class IrChange: +class IrGuard: def __init__(self): old_flag = paddle.fluid.framework.get_flags("FLAGS_enable_new_ir_api") paddle.fluid.framework.set_flags({"FLAGS_enable_new_ir_api": False}) @@ -33,6 +31,14 @@ def __init__(self): ) paddle.fluid.framework.set_flags(old_flag) + def __enter__(self): + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) + self._switch_to_new_ir() + + def __exit__(self, exc_type, exc_val, exc_tb): + paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) + self._switch_to_old_ir() + def _switch_to_new_ir(self): if paddle.ir.core._use_new_ir_api(): paddle.framework.set_flags( @@ -64,15 +70,3 @@ def _switch_to_old_ir(self): "IrChange._switch_to_old_ir only work when paddle.ir.core._use_new_ir_api() is false, \ please set FLAGS_enable_new_ir_api = false" ) - - -@signature_safe_contextmanager -def _newir_guard(): - ir_change = IrChange() - paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) - ir_change._switch_to_new_ir() - try: - yield - finally: - paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) - ir_change._switch_to_old_ir() diff --git a/python/paddle/static/input.py b/python/paddle/static/input.py index 30a853336c976..1382ff591b7f9 100644 --- a/python/paddle/static/input.py +++ b/python/paddle/static/input.py @@ -99,9 +99,9 @@ def data(name, shape, dtype=None, lod_level=0): """ + if not dtype: + dtype = paddle.get_default_dtype() if paddle.ir.core._use_new_ir_api(): - if not dtype: - dtype = paddle.get_default_dtype() ir_dtype = paddle.ir.core.convert_np_dtype_to_dtype_(dtype) return paddle._ir_ops.data(name, shape, ir_dtype, core.Place()) @@ -115,29 +115,16 @@ def data(name, shape, dtype=None, lod_level=0): if shape[i] is None: shape[i] = -1 - if dtype: - out = helper.create_global_variable( - name=name, - shape=shape, - dtype=dtype, - type=core.VarDesc.VarType.LOD_TENSOR, - stop_gradient=True, - lod_level=lod_level, - is_data=True, - need_check_feed=True, - ) - - else: - out = helper.create_global_variable( - name=name, - shape=shape, - dtype=paddle.get_default_dtype(), - type=core.VarDesc.VarType.LOD_TENSOR, - stop_gradient=True, - lod_level=lod_level, - is_data=True, - need_check_feed=True, - ) + out = helper.create_global_variable( + name=name, + shape=shape, + dtype=dtype, + type=core.VarDesc.VarType.LOD_TENSOR, + stop_gradient=True, + lod_level=lod_level, + is_data=True, + need_check_feed=True, + ) is_new_ir_mode = os.environ.get("FLAGS_enable_new_ir_in_executor", None) if evaluate_flag(is_new_ir_mode): diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index 817d39b7d879d..66a48acc6c5f6 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -1261,7 +1261,7 @@ def get_ir_input_attr_dict_and_feed(self, stop_gradient): static_inputs = defaultdict(list) feed = {} for name, item in self.inputs.items(): - if isinstance(item, list): + if isinstance(item, (list, tuple)): for tup in item: dtype = ( "bfloat16" @@ -1355,9 +1355,7 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): # executor run executor = Executor(place) (outs,) = executor.run( - ir_program, - feed=feed, - fetch_list=fetch_list, + ir_program, feed=feed, fetch_list=[fetch_list] ) return outs @@ -2473,7 +2471,7 @@ def _is_skip_name(self, name): or type(place) is paddle.fluid.libpaddle.CUDAPlace ): print("New IR checker begins...........") - with paddle.new_ir_utils._newir_guard(): + with paddle.new_ir_utils.IrGuard(): new_ir_checker = NewIRChecker(self, self.outputs) new_ir_checker.check() @@ -3020,6 +3018,7 @@ def check_grad_with_place( "Gradient Check On %s" % str(place), atol=atol, ) + # get new ir gradient if ( self.op_type @@ -3031,7 +3030,7 @@ def check_grad_with_place( or type(place) is paddle.fluid.libpaddle.CUDAPlace ): print("New IR gradient begins...........") - with paddle.new_ir_utils._newir_guard(): + with paddle.new_ir_utils.IrGuard(): new_ir_grad = self._get_ir_gradient( inputs_to_check, place, @@ -3042,7 +3041,7 @@ def check_grad_with_place( print("New IR gradient ends...........") self._assert_is_close( numeric_grads, - [new_ir_grad], + new_ir_grad, inputs_to_check, max_relative_error, "Gradient Check On %s" % str(place), @@ -3448,23 +3447,38 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): args = OpTestUtils.assumption_assert_and_transform( args, len(inputs_sig) ) + grad_outputs = [] + if user_defined_grad_outputs is not None: + # user_defined_grad_outputs here are numpy arrays + if not isinstance(user_defined_grad_outputs, list): + user_defined_grad_outputs = [user_defined_grad_outputs] + for grad_out_value, idx in zip( + user_defined_grad_outputs, + range(len(user_defined_grad_outputs)), + ): + grad_val = paddle.static.data( + name='val_grad_%s' % idx, + shape=grad_out_value.shape, + dtype=grad_out_value.dtype, + ) + grad_outputs.append(grad_val) + feed.update({'val_grad_%s' % idx: grad_out_value}) + # delete the inputs which no need to calculate grad + for no_grad_val in no_grad_set: + del static_inputs[no_grad_val] + ret_tuple = self.python_api(*args) - result = construct_output_dict_by_kernel_sig(ret_tuple, outputs_sig) + outputs = construct_output_dict_by_kernel_sig( + ret_tuple, outputs_sig + ) if hasattr(self, "python_out_sig_sub_name"): for key in self.python_out_sig_sub_name.keys(): for i in range(len(self.python_out_sig_sub_name[key])): - result[key][0][i].name = self.python_out_sig_sub_name[ + outputs[key][0][i].name = self.python_out_sig_sub_name[ key ][i] fetch_list = getattr(self, "fetch_list", []) - if len(fetch_list) == 0: - for var in result.items(): - if isinstance(var[1], list): - for v in var[1]: - fetch_list.append(v) - else: - fetch_list.append(var[1]) - outputs = result + outputs_valid = outputs grad_inputs = inputs_to_check if user_defined_grad_outputs is None: @@ -3477,16 +3491,6 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): grad_outputs=None, ) else: - # user_defined_grad_outputs here are numpy arrays - if not isinstance(user_defined_grad_outputs, list): - user_defined_grad_outputs = [user_defined_grad_outputs] - grad_outputs = [] - for grad_out_value in user_defined_grad_outputs: - grad_outputs.append(paddle.to_tensor(grad_out_value)) - # delete the inputs which no need to calculate grad - for no_grad_val in no_grad_set: - del static_inputs[no_grad_val] - grad_inputs = ir_grad( outputs=paddle.utils.flatten(outputs), inputs=paddle.utils.flatten(static_inputs), @@ -3496,7 +3500,7 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): # executor run executor = paddle.static.Executor() - (outs,) = executor.run( + outs = executor.run( ir_program, feed=feed, fetch_list=fetch_list, From ada16f94f2148c995eda68156a3fe514ac4cf4fd Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Wed, 6 Sep 2023 15:02:51 +0800 Subject: [PATCH 154/194] [IR] Refine the Build interface of split op (#56924) * fix bug * fix bug --- .../new_executor/standalone_executor.cc | 7 +- .../ir/dialect/op_generator/op_build_gen.py | 79 ++++++++++++++++--- paddle/fluid/ir/transforms/inplace_pass.cc | 20 ++--- paddle/ir/core/ir_context.cc | 2 +- paddle/phi/core/flags.cc | 15 +++- python/paddle/nn/functional/activation.py | 2 +- test/ir/new_ir/test_pd_inplace_pass.py | 3 +- 7 files changed, 100 insertions(+), 28 deletions(-) diff --git a/paddle/fluid/framework/new_executor/standalone_executor.cc b/paddle/fluid/framework/new_executor/standalone_executor.cc index 303a4f8478127..ed109f9cd0b96 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor.cc @@ -16,8 +16,8 @@ #include "paddle/fluid/framework/new_executor/feed_fetch_utils.h" #include "paddle/fluid/framework/new_executor/interpreter/interpreter_util.h" #include "paddle/fluid/framework/new_executor/program_interpreter.h" -#include "paddle/fluid/platform/flags.h" #include "paddle/fluid/platform/profiler/event_tracing.h" +#include "paddle/phi/core/flags.h" #include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h" @@ -29,10 +29,7 @@ PHI_DECLARE_bool(enable_new_ir_in_executor); PHI_DECLARE_bool(enable_new_ir_api); - -PADDLE_DEFINE_EXPORTED_bool(new_ir_apply_inplace_pass, - true, - "new ir kernel program apply inplace pass."); +PHI_DECLARE_bool(new_ir_apply_inplace_pass); namespace paddle { namespace framework { diff --git a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py index d36c269648315..2a78ea6b6e995 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py @@ -13,6 +13,10 @@ # limitations under the License. # generator build function +_INFERMETA_NEED_META_CONFIG = {'SplitInferMeta'} + +_PREPARE_DATA_WITH_UNKNOW_ATTRIBUTE = {'SplitOp'} + OP_BUILD_TEMPLATE = """ void {op_name}::Build({build_args}) {{ {get_attributes} @@ -273,6 +277,7 @@ def GenBuildAttributes( def GenBuildOutputs( + op_class_name, op_input_name_list, op_input_type_list, op_mutable_attribute_name_list, @@ -316,6 +321,40 @@ def GenBuildOutputs( CREATE_INTARRAY_MUTABLE_ATTRIBUE_TEMPLATE = """ std::vector {name} = {name}_.owner()->dyn_cast().attributes().at("value").dyn_cast().data().GetData(); (void){name};\n""" CREATE_SCALAR_MUTABLE_ATTRIBUE_TEMPLATE = """ {dtype} {name} = {name}_.owner()->dyn_cast().attributes().at("value").dyn_cast().data().to<{dtype}>(); (void){name};\n""" + CREATE_INTARRAY_MUTABLE_ATTRIBUE_WITH_UNKONW_DATA_TEMPLATE = """ phi::IntArray {name}; + if ({name}_.owner()->info().id() == ir::TypeId::get()) {{ + {name} = std::move(phi::IntArray({name}_.owner() + ->dyn_cast() + .attributes() + .at("value") + .dyn_cast() + .data() + .GetData())); + }} + else {{ + PADDLE_ENFORCE( + {name}_.type().isa(), + phi::errors::PreconditionNotMet("section Type should be VectorType.")); + size_t {name}_size = {name}_.type().dyn_cast().size(); + {name} = std::move(phi::IntArray(std::vector({name}_size, -1))); + {name}.SetFromTensor(true); + }}\n""" + + CREATE_SCALAR_MUTABLE_ATTRIBUE_WITH_UNKONW_DATA_TEMPLATE = """ phi::Scalar {name}; + if ({name}_.owner()->info().id() == ir::TypeId::get()) {{ + {name} = std::move(phi::Scalar({name}_.owner() + ->dyn_cast() + .attributes() + .at("value") + .dyn_cast() + .data() + .to())); + }} + else {{ + {name} = std::move(phi::Scalar(-1)); + {name}.SetFromTensor(true); + }}\n""" + CREATE_OUTPUT_METATENSOR_TEMPLATE = """ phi::DenseTensor dense_{name}; phi::MetaTensor meta_{name}(&dense_{name}); """ @@ -348,19 +387,30 @@ def GenBuildOutputs( attr_dtype = op_mutable_attribute_type_list[idx] # int_array if attr_dtype[0] == "paddle::dialect::IntArrayAttribute": - build_output_str += ( - CREATE_INTARRAY_MUTABLE_ATTRIBUE_TEMPLATE.format( + if op_class_name in _PREPARE_DATA_WITH_UNKNOW_ATTRIBUTE: + build_output_str += CREATE_INTARRAY_MUTABLE_ATTRIBUE_WITH_UNKONW_DATA_TEMPLATE.format( name=op_mutable_attribute_name_list[idx] ) - ) + else: + build_output_str += ( + CREATE_INTARRAY_MUTABLE_ATTRIBUE_TEMPLATE.format( + name=op_mutable_attribute_name_list[idx] + ) + ) # scalar elif attr_dtype[0] == "paddle::dialect::ScalarAttribute": - build_output_str += ( - CREATE_SCALAR_MUTABLE_ATTRIBUE_TEMPLATE.format( + if op_class_name in _PREPARE_DATA_WITH_UNKNOW_ATTRIBUTE: + build_output_str += CREATE_SCALAR_MUTABLE_ATTRIBUE_WITH_UNKONW_DATA_TEMPLATE.format( name=op_mutable_attribute_name_list[idx], dtype=attr_dtype[1], ) - ) + else: + build_output_str += ( + CREATE_SCALAR_MUTABLE_ATTRIBUE_TEMPLATE.format( + name=op_mutable_attribute_name_list[idx], + dtype=attr_dtype[1], + ) + ) # string elif attr_dtype[0] == "ir::StrAttribute": build_output_str += "" @@ -421,9 +471,19 @@ def GenBuildOutputs( CREATE_INFER_META_FUNC_TEMPLATE = """ phi::{func}({args}); """ - build_output_str += CREATE_INFER_META_FUNC_TEMPLATE.format( - func=op_infer_meta_map['func'], args=", ".join(infer_meta_args) - ) + CREATE_INFER_META_FUNC_WITH_METACINFIG_TEMPLATE = """ + phi::{func}({args}, phi::MetaConfig(false, false)); +""" + if op_infer_meta_map['func'] in _INFERMETA_NEED_META_CONFIG: + build_output_str += ( + CREATE_INFER_META_FUNC_WITH_METACINFIG_TEMPLATE.format( + func=op_infer_meta_map['func'], args=", ".join(infer_meta_args) + ) + ) + else: + build_output_str += CREATE_INFER_META_FUNC_TEMPLATE.format( + func=op_infer_meta_map['func'], args=", ".join(infer_meta_args) + ) # use dense_{name} or vec_dense_{name} to create Outputs type build_output_str += "\n std::vector argument_outputs;" @@ -528,6 +588,7 @@ def gen_build_func_str( op_non_mutable_attribute_type_list, ) build_outputs_str = GenBuildOutputs( + op_class_name, op_input_name_list, op_input_type_list, op_mutable_attribute_name_list, diff --git a/paddle/fluid/ir/transforms/inplace_pass.cc b/paddle/fluid/ir/transforms/inplace_pass.cc index 2ce0f663ecd92..28c733e0928ac 100644 --- a/paddle/fluid/ir/transforms/inplace_pass.cc +++ b/paddle/fluid/ir/transforms/inplace_pass.cc @@ -25,10 +25,11 @@ #include "paddle/ir/pass/pass.h" #include "paddle/ir/pass/pass_registry.h" +namespace details { // NOTE(zhangbo): Which kind of value can be deleted? // (1) Value's type needs to be AllocatedDenseTensorType or // AllocatedSelectedRowsType; (2) Value's is not persisable. -bool CanBeDeleted(ir::Value value) { +static bool CanBeDeleted(ir::Value value) { if (!value.type()) { return false; } @@ -47,9 +48,9 @@ bool CanBeDeleted(ir::Value value) { return true; } -bool CanDoInplace(const std::unordered_set& eager_dels, - ir::Value input, - ir::Value output) { +static bool CanDoInplace(const std::unordered_set& eager_dels, + ir::Value input, + ir::Value output) { if (input.type() != output.type()) { VLOG(9) << " -- input's type != output's type, can't do inplace"; return false; @@ -61,7 +62,7 @@ bool CanDoInplace(const std::unordered_set& eager_dels, return true; } -bool IsNoNeedBuffer(ir::Operation* op, ir::Value value) { +static bool IsNoNeedBuffer(ir::Operation* op, ir::Value value) { if (op->dialect()->name().compare( paddle::dialect::PaddleKernelDialect::name()) != 0) { VLOG(8) << op->name() @@ -90,7 +91,7 @@ bool IsNoNeedBuffer(ir::Operation* op, ir::Value value) { // NOTE(zhangbo): pd.feed's output and pd.fetch's input can not be eager // deleted. -std::unordered_set GetSkipDeletionValues(ir::Block* block) { +static std::unordered_set GetSkipDeletionValues(ir::Block* block) { std::unordered_set skip_dels; for (auto& op : *block) { if (op->dialect()->name().compare( @@ -119,7 +120,7 @@ std::unordered_set GetSkipDeletionValues(ir::Block* block) { // NOTE(zhangbo): For inplace Pass, currently only the kernel_dialect operator // is supported. Therefore, this function only returns the values in the // kernel_dialect operator that can be eager deleted. -std::unordered_map> +static std::unordered_map> GetEagerDeletionValues(ir::Block* block) { std::unordered_set skip_dels = GetSkipDeletionValues(block); @@ -167,7 +168,7 @@ GetEagerDeletionValues(ir::Block* block) { return eager_dels; } -std::unordered_map GetInplaceOps( +static std::unordered_map GetInplaceOps( ir::Block* block) { const auto eager_dels = GetEagerDeletionValues(block); @@ -282,6 +283,7 @@ std::unordered_map GetInplaceOps( } return inplace_ops; } +} // namespace details class InplacePass : public ir::Pass { public: @@ -292,7 +294,7 @@ class InplacePass : public ir::Pass { IR_ENFORCE(module_op, "DcePass should run on module op."); auto* block = module_op.block(); - auto inplace_ops = GetInplaceOps(block); + auto inplace_ops = details::GetInplaceOps(block); for (auto kv : inplace_ops) { VLOG(6) << "Do inplace for: " diff --git a/paddle/ir/core/ir_context.cc b/paddle/ir/core/ir_context.cc index 54865e1ec38bd..9fe79ac84b6a4 100644 --- a/paddle/ir/core/ir_context.cc +++ b/paddle/ir/core/ir_context.cc @@ -118,7 +118,7 @@ class IrContextImpl { << ", OpInfo: ptr=" << iter->second.AsOpaquePointer() << "]."; return iter->second; } - LOG(WARNING) << "No cache found operation of: [Name=" << name << "]."; + VLOG(8) << "No cache found operation of: [Name=" << name << "]."; return OpInfo(); } const OpInfoMap ®istered_op_info_map() { return registed_op_infos_; } diff --git a/paddle/phi/core/flags.cc b/paddle/phi/core/flags.cc index f458316fc4c0a..be4224037ba76 100644 --- a/paddle/phi/core/flags.cc +++ b/paddle/phi/core/flags.cc @@ -1289,7 +1289,7 @@ PHI_DEFINE_EXPORTED_bool(enable_new_ir_api, "Enable new IR API in Python"); /** - * Using new IR in executor FLAG + * Using new IR in executor FLAG * Name: enable_new_ir_in_executor_trace_run * Since Version: 2.6.0 * Value Range: bool, default=false @@ -1301,6 +1301,19 @@ PHI_DEFINE_EXPORTED_bool(enable_new_ir_in_executor_trace_run, false, "Enable new IR in executor"); +/** + * Apply inplace pass to new IR FLAG + * Name: new_ir_apply_inplace_pass + * Since Version: 2.6.0 + * Value Range: bool, default=true + * Example: + * Note: If Ture, will apply inplace pass to new IR. + */ +PHI_DEFINE_EXPORTED_bool(new_ir_apply_inplace_pass, + true, + "Whether to apply inplace pass on lowering " + "::ir::Program to Kernel Dialect"); + PHI_DEFINE_EXPORTED_bool(enable_record_memory, false, "Enable memory recorder"); PHI_DEFINE_EXPORTED_bool( diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index bc8a49d3e8c3e..aacc0232be2f3 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -759,7 +759,7 @@ def relu(x, name=None): if in_dynamic_mode(): return _C_ops.relu(x) else: - if paddle.ir.core._use_new_ir_api(): + if paddle.framework.in_dynamic_or_new_ir_mode(): # Below code will be removed after we can generate IR api automatically return paddle._ir_ops.relu(x) diff --git a/test/ir/new_ir/test_pd_inplace_pass.py b/test/ir/new_ir/test_pd_inplace_pass.py index e00f34389d3b4..5ba935deec0ef 100644 --- a/test/ir/new_ir/test_pd_inplace_pass.py +++ b/test/ir/new_ir/test_pd_inplace_pass.py @@ -17,14 +17,13 @@ import numpy as np import paddle -from paddle.fluid import core paddle.enable_static() class TestPdInplacePass(unittest.TestCase): def test_pd_inplace_pass(self): - place = core.Place() + place = paddle.framework.core.Place() place.set_place(paddle.CPUPlace()) new_scope = paddle.static.Scope() main_program = paddle.static.Program() From c96b9cbbf124e74d82df68d9b41db4ee22dcd742 Mon Sep 17 00:00:00 2001 From: JZ-LIANG Date: Wed, 6 Sep 2023 15:10:02 +0800 Subject: [PATCH 155/194] update doc (#56957) --- python/paddle/distributed/auto_parallel/static/engine.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/paddle/distributed/auto_parallel/static/engine.py b/python/paddle/distributed/auto_parallel/static/engine.py index 2b66ac4e16a22..07bebf3d0c5ae 100644 --- a/python/paddle/distributed/auto_parallel/static/engine.py +++ b/python/paddle/distributed/auto_parallel/static/engine.py @@ -54,10 +54,8 @@ class Engine: """ - An Engine object can provide the full power of auto parallel to users. - With the help of it, users can easily obtain the abilities of the - distributed training and inference. It also support the dynamic graph and - static graph at the same time. + An High-Level API for auto parallel, which could be used for distributed Training (engine.fit) and Inferenced (engine.predict). + Static graph mode is supported natively, Dynamic graph mode is also supported under `@to_static `_ . Args: model (paddle.nn.Layer, optional): The model is an instance of From 0900a7902efb467d3b4c445c127305ea74938f54 Mon Sep 17 00:00:00 2001 From: JYChen Date: Wed, 6 Sep 2023 16:05:13 +0800 Subject: [PATCH 156/194] fix setvalue dtype error when using dy2st and amp O2 (#56868) * fix setvalue dtype error when using dy2st and amp O2 * add one test * remove test share_buffer since win/linux have different number --- python/paddle/static/amp/fp16_utils.py | 4 +++ test/amp/test_amp_api.py | 46 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/python/paddle/static/amp/fp16_utils.py b/python/paddle/static/amp/fp16_utils.py index 46c669ba54e46..b5c78c69f6679 100644 --- a/python/paddle/static/amp/fp16_utils.py +++ b/python/paddle/static/amp/fp16_utils.py @@ -642,6 +642,10 @@ def cast_model_to_fp16( def need_process(op): need_process = True + if op.type in ["set_value"]: + # NOTE(zoooo0820): OP set_value has attribute "dtype", but its output type is + # determined by the input.dtype instead of attribute. So, here we still process it. + return need_process if op.type in ["create_py_reader", "read"]: need_process = False else: diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index 607117c84aa04..58d7c1dd2f8e2 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -258,5 +258,51 @@ def run_example_code(): paddle.disable_static() +class SimpleModelIncludeSetValue(nn.Layer): + def __init__(self): + super().__init__() + self.norm = nn.LayerNorm(3) + + def forward(self, x): + x = x + 1 + tmp = x * 1 + y = self.norm(tmp) + x[:] = y + + z = x * 1 + return z + + +@unittest.skipIf( + not core.is_compiled_with_cuda() + or paddle.device.cuda.get_device_capability()[0] < 7.0, + "run test when gpu's compute capability is at least 7.0.", +) +class TestDy2STWithSetValue(AmpTestBase): + def test_op_called_as_expected(self): + expected_fp16_calls = { + "cast": 0, + "layer_norm": 1, + "scale": 3, + "set_value": 1, + } + + func = SimpleModelIncludeSetValue() + func = paddle.amp.decorate(func, level='O2') + func = paddle.jit.to_static(func) + input = paddle.randn((2, 3)) + + with paddle.amp.auto_cast(level='O2'): + res = func(input) + loss = res.sum() + prog = func.forward.get_concrete_program(input)[1].forward_program + amp.debugging.collect_operator_stats(prog) + op_stats_list = amp.debugging._get_op_stats_list(prog) + loss.backward() + self._check_op_calls( + op_stats_list[0], expected_fp16_calls=expected_fp16_calls + ) + + if __name__ == '__main__': unittest.main() From c170074df0137ed578553b3ce6095308407c3fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=89=91=E5=AF=92?= Date: Wed, 6 Sep 2023 16:11:29 +0800 Subject: [PATCH 157/194] rename StoreButMutator and fix RampMutator (#56966) --- paddle/cinn/optim/ir_simplify.cc | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/paddle/cinn/optim/ir_simplify.cc b/paddle/cinn/optim/ir_simplify.cc index 3d187f8a4138c..3c0f9298dae2c 100644 --- a/paddle/cinn/optim/ir_simplify.cc +++ b/paddle/cinn/optim/ir_simplify.cc @@ -53,9 +53,9 @@ void PartialSimplify( } //! Simplify the expression but Load. -struct SimplifyButStoreLoadMutator : public ir::IRMutator { +struct SimplifyNoPureMathMutator : public ir::IRMutator { common::cas_intervals_t& var_intervals; - explicit SimplifyButStoreLoadMutator( + explicit SimplifyNoPureMathMutator( common::cas_intervals_t& var_intervals) // NOLINT : var_intervals(var_intervals) {} @@ -76,19 +76,6 @@ struct SimplifyButStoreLoadMutator : public ir::IRMutator { __(Max) #undef __ - void Visit(const Ramp* op, Expr* expr) override { - auto* node = expr->As(); - CHECK(common::IsPureMath(node->base)); - CHECK(common::IsPureMath(node->stride)); - PartialSimplify(&node->base, var_intervals); - PartialSimplify(&node->stride, var_intervals); - } - - void Visit(const Cast* op, Expr* expr) override { - auto* node = expr->As(); - Visit(&node->v(), &node->v()); - } - void Visit(const PolyFor* op, Expr* expr) override { auto* node = expr->As(); node->condition = common::SolveInequality(op->condition, op->iterator); @@ -138,7 +125,7 @@ struct SimplifyLoadMutator : public ir::IRMutator { if (common::IsPureMath(idx)) { PartialSimplify(&idx, var_intervals_); } else { - SimplifyButStoreLoadMutator mutator(var_intervals_); + SimplifyNoPureMathMutator mutator(var_intervals_); mutator(&idx); } } @@ -176,7 +163,7 @@ struct SimplifyStoreMutator : public ir::IRMutator { if (common::IsPureMath(idx)) { PartialSimplify(&idx, var_intervals_); } else { - SimplifyButStoreLoadMutator mutator(var_intervals_); + SimplifyNoPureMathMutator mutator(var_intervals_); mutator(&idx); } } @@ -215,8 +202,8 @@ struct SimplifyRampMutator : public ir::IRMutator { CHECK(common::IsPureMath(node->stride)) << node->stride << "is not a pure math!"; - Simplify(&node->base); - Simplify(&node->stride); + PartialSimplify(&node->base); + PartialSimplify(&node->stride); } // ramp + ramp void Visit(const Add* op, Expr* expr) override { @@ -370,7 +357,7 @@ void Simplify(Expr* expr) { SimplifyIfThenElseMutator()(expr); common::cas_intervals_t var_intervals; - SimplifyButStoreLoadMutator mutator(var_intervals); + SimplifyNoPureMathMutator mutator(var_intervals); mutator(expr); ReplaceFracWithDivMutator()(expr); From 7c8c9b7d87b51838ae6ed379ac9bc3d5685b7bee Mon Sep 17 00:00:00 2001 From: leolishaohao <138780481+leolishaohao@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:13:28 +0800 Subject: [PATCH 158/194] [XPU] add squeeze_excitation_block_xpu op&pass to optimize ppocr_v3_det model (#56773) * [XPU] add squeeze_excitation_block_xpu op&pass to optimize ppocr_v3_det model test=kunlun * fix * fix Codestype * remove xpu name --- paddle/fluid/framework/ir/CMakeLists.txt | 6 + .../ir/xpu/squeeze_excitation_fuse_pass.cc | 567 ++++++++++++++++++ .../ir/xpu/squeeze_excitation_fuse_pass.h | 82 +++ .../xpu/squeeze_excitation_fuse_pass_test.cc | 63 ++ .../inference/api/paddle_pass_builder.cc | 1 + paddle/phi/api/yaml/fused_ops.yaml | 10 + paddle/phi/backends/xpu/xpu2_op_list.cc | 1 + paddle/phi/infermeta/fusion.cc | 25 + paddle/phi/infermeta/fusion.h | 10 + .../squeeze_excitation_block_xpu_kernel.cc | 126 ++++ .../test_xpu_squeeze_excitation_fuse_pass.py | 163 +++++ 11 files changed, 1054 insertions(+) create mode 100644 paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.cc create mode 100644 paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.h create mode 100644 paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass_test.cc create mode 100644 paddle/phi/kernels/fusion/xpu/squeeze_excitation_block_xpu_kernel.cc create mode 100644 test/ir/inference/test_xpu_squeeze_excitation_fuse_pass.py diff --git a/paddle/fluid/framework/ir/CMakeLists.txt b/paddle/fluid/framework/ir/CMakeLists.txt index f856eca2aa526..b6143f335d163 100755 --- a/paddle/fluid/framework/ir/CMakeLists.txt +++ b/paddle/fluid/framework/ir/CMakeLists.txt @@ -290,6 +290,8 @@ if(WITH_XPU) pass_library(fast_where_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS}) pass_library(fast_layernorm_xpu_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS}) + pass_library(squeeze_excitation_fuse_pass inference DIR xpu DEPS + ${XPU_PASS_DEPS}) pass_library(elementwise_mul_add_fuse_pass inference DIR xpu DEPS ${XPU_PASS_DEPS}) endif() @@ -615,4 +617,8 @@ if(WITH_XPU) test_fast_where_xpu_fuse_pass SRCS xpu/fast_where_xpu_fuse_pass_test.cc DEPS fast_where_xpu_fuse_pass) + cc_test( + test_squeeze_excitation_fuse_pass + SRCS xpu/squeeze_excitation_fuse_pass_test.cc + DEPS squeeze_excitation_fuse_pass) endif() diff --git a/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.cc b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.cc new file mode 100644 index 0000000000000..60bb47156a676 --- /dev/null +++ b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.cc @@ -0,0 +1,567 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.h" +#include + +#include "glog/logging.h" + +#include "paddle/fluid/framework/ir/graph_pattern_detector.h" +#include "paddle/fluid/framework/ir/xpu/pass_utils.h" +#include "paddle/fluid/framework/op_version_registry.h" +#include "paddle/fluid/platform/enforce.h" + +namespace paddle { +namespace framework { +namespace ir { + +namespace math { + +template +static inline void Transpose(const T* in, T* out, int h, int w) { + for (int h1 = 0; h1 < w; ++h1) { + for (int w1 = 0; w1 < h; ++w1) { + out[h1 * h + w1] = in[w1 * w + h1]; + } + } +} + +} // namespace math + +namespace patterns { + +struct SqueezeExcitationFusePattern : public PatternBase { + SqueezeExcitationFusePattern(PDPattern* pattern, + const std::string& name_scope, + const std::string& op_type, + const std::string& act_type, + bool with_branch, + bool with_bias); + + // declare operator node`s name + PATTERN_DECL_NODE(pool2d); + PATTERN_DECL_NODE(mul_1); + PATTERN_DECL_NODE(mul_2); + PATTERN_DECL_NODE(ew_mul); + PATTERN_DECL_NODE(ew_branch_add); + PATTERN_DECL_NODE(block_act); + + // declare variable node`s name + PATTERN_DECL_NODE(x); + PATTERN_DECL_NODE(pool2d_out); + PATTERN_DECL_NODE(mul_1_w); + PATTERN_DECL_NODE(mul_1_w_max); + PATTERN_DECL_NODE(mul_1_bias); + PATTERN_DECL_NODE(mul_1_out); + PATTERN_DECL_NODE(mul_1_out_max); + PATTERN_DECL_NODE(mul_2_w); + PATTERN_DECL_NODE(mul_2_w_max); + PATTERN_DECL_NODE(mul_2_bias); + PATTERN_DECL_NODE(mul_2_out); + PATTERN_DECL_NODE(mul_2_out_max); + PATTERN_DECL_NODE(ew_mul_out); + PATTERN_DECL_NODE(ew_branch_add_in); + PATTERN_DECL_NODE(ew_branch_add_out); + PATTERN_DECL_NODE(block_act_out); +}; + +SqueezeExcitationFusePattern::SqueezeExcitationFusePattern( + PDPattern* pattern, + const std::string& name_scope, + const std::string& op_type, + const std::string& act_type, + bool with_branch, + bool with_bias) + : PatternBase(pattern, name_scope, name_scope) { + auto* x = pattern->NewNode(x_repr()) + ->assert_is_op_input("pool2d", "X") + ->assert_is_op_input("elementwise_mul", "X") + ->AsInput(); + + auto pool2d_teller = [](const Node* x) { + auto* op_desc = x->Op(); + bool has_adap = op_desc->HasAttr("adaptive"); + if (has_adap) { + auto ksize = + PADDLE_GET_CONST(std::vector, op_desc->GetAttr("ksize")); + if (ksize[0] != 1 || ksize[1] != 1) { + return false; + } + } else if (PADDLE_GET_CONST(bool, op_desc->GetAttr("global_pooling")) == + false) { + return false; + } + return true; + }; + + auto* pool2d = pattern->NewNode(pool2d_repr()) + ->assert_is_op("pool2d") + ->assert_op_attr("pooling_type", "avg") + ->assert_more(pool2d_teller); + + auto* pool2d_out = pattern->NewNode(pool2d_out_repr()) + ->assert_is_op_output("pool2d", "Out") + ->assert_is_op_input(op_type, "x"); + + auto mul_w_teller = [](const Node* x) { + auto* var_desc = x->Var(); + auto filter_dims = var_desc->GetShape(); + auto in_c = filter_dims[0]; + auto out_c = filter_dims[1]; + auto bigger = std::max(in_c, out_c); + auto smaller = std::min(in_c, out_c); + if (bigger % smaller != 0) { + return false; + } + return true; + }; + + auto* mul_1 = pattern->NewNode(mul_1_repr())->assert_is_op(op_type); + auto* mul_1_w = pattern->NewNode(mul_1_w_repr()) + ->assert_is_op_input(op_type, "filter") + ->assert_more(mul_w_teller); + auto* mul_1_w_max = pattern->NewNode(mul_1_w_max_repr()) + ->assert_is_op_input(op_type, "filter_max"); + auto* mul_1_out = pattern->NewNode(mul_1_out_repr()) + ->assert_is_op_output(op_type, "out") + ->assert_is_op_input(op_type, "x"); + auto* mul_1_out_max = pattern->NewNode(mul_1_out_max_repr()) + ->assert_is_op_output(op_type, "out_max"); + auto* mul_2 = pattern->NewNode(mul_2_repr())->assert_is_op(op_type); + auto* mul_2_w = pattern->NewNode(mul_2_w_repr()) + ->assert_is_op_input(op_type, "filter") + ->assert_more(mul_w_teller); + auto* mul_2_w_max = pattern->NewNode(mul_2_w_max_repr()) + ->assert_is_op_input(op_type, "filter_max"); + auto* mul_2_out = pattern->NewNode(mul_2_out_repr()) + ->assert_is_op_output(op_type, "out") + ->assert_is_op_input("elementwise_mul", "Y"); + auto* mul_2_out_max = pattern->NewNode(mul_2_out_max_repr()) + ->assert_is_op_output(op_type, "out_max"); + + PDNode* mul_1_bias = nullptr; + PDNode* mul_2_bias = nullptr; + if (with_bias) { + mul_1_bias = pattern->NewNode(mul_1_bias_repr()) + ->assert_is_op_input(op_type, "bias"); + mul_2_bias = pattern->NewNode(mul_2_bias_repr()) + ->assert_is_op_input(op_type, "bias"); + } + auto* ew_mul = + pattern->NewNode(ew_mul_repr())->assert_is_op("elementwise_mul"); + auto* ew_mul_out = pattern->NewNode(ew_mul_out_repr()) + ->assert_is_op_output("elementwise_mul", "Out"); + + // branch + PDNode* ew_branch_add_in = nullptr; + PDNode* ew_branch_add = nullptr; + PDNode* ew_branch_add_out = nullptr; + if (with_branch) { + ew_branch_add_in = pattern->NewNode(ew_branch_add_in_repr()) + ->assert_is_op_input("elementwise_add", "X") + ->AsInput(); + ew_branch_add = + pattern->NewNode(ew_branch_add_repr())->assert_is_op("elementwise_add"); + ew_branch_add_out = pattern->NewNode(ew_branch_add_out_repr()) + ->assert_is_op_output("elementwise_add", "out"); + } + // act + PDNode* block_act = nullptr; + PDNode* block_act_out = nullptr; + if (act_type != "linear") { + block_act = pattern->NewNode(block_act_repr())->assert_is_op(act_type); + block_act_out = pattern->NewNode(block_act_out_repr()) + ->assert_is_op_output(act_type, "Out"); + } + + // pass + pool2d->LinksFrom({x}).LinksTo({pool2d_out}); + mul_1->LinksFrom({mul_1_w, mul_1_w_max, pool2d_out}) + .LinksTo({mul_1_out, mul_1_out_max}); + mul_2->LinksFrom({mul_2_w, mul_2_w_max, mul_1_out}) + .LinksTo({mul_2_out, mul_2_out_max}); + ew_mul->LinksFrom({x, mul_2_out}).LinksTo({ew_mul_out}); + + if (with_branch) { + ew_mul_out->assert_is_op_input("elementwise_add", "Y"); + ew_branch_add->LinksFrom({ew_mul_out, ew_branch_add_in}) + .LinksTo({ew_branch_add_out}); + } else { + ew_branch_add_out = ew_mul_out; + } + if (act_type != "linear") { + ew_branch_add_out->assert_is_op_input(act_type, "X"); + block_act->LinksFrom({ew_branch_add_out}).LinksTo({block_act_out}); + } else { + block_act_out = ew_branch_add_out; + } + if (with_bias) { + mul_1->LinksFrom({mul_1_bias}); + mul_2->LinksFrom({mul_2_bias}); + } +} + +} // namespace patterns + +void SqueezeExcitationFusePass::ApplyImpl(ir::Graph* graph) const { + PADDLE_ENFORCE_NOT_NULL( + graph, + platform::errors::PreconditionNotMet("graph should not be null. ")); + Init(name_scope_, graph); + + int found_subgraph_count = 0; + for (auto with_branch : {true, false}) { + for (auto with_bias : {true, false}) { + for (auto op_type : {"conv2d_xpu"}) { + for (auto act_type : {"relu", + "sigmoid", + "tanh", + "leaky_relu", + "hard_swish", + "hard_sigmoid", + "relu6", + "linear"}) { + found_subgraph_count += + ApplyImpl(graph, op_type, act_type, with_branch, with_bias); + } + } + } + } + AddStatis(found_subgraph_count); +} + +int SqueezeExcitationFusePass::ApplyImpl(ir::Graph* graph, + const std::string& op_type, + const std::string& act_type, + bool with_branch, + bool with_bias) const { + GraphPatternDetector gpd; + patterns::SqueezeExcitationFusePattern pattern(gpd.mutable_pattern(), + name_scope_, + op_type, + act_type, + with_branch, + with_bias); + + int found_subgraph_count = 0; + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, + Graph* graph) { + VLOG(4) << "handle SqueezeExcitationFusePass"; + /* declare operator node's name */ + GET_IR_NODE(pool2d); + GET_IR_NODE(mul_1); + GET_IR_NODE(mul_2); + GET_IR_NODE(ew_mul); + GET_IR_NODE(ew_branch_add); + GET_IR_NODE(block_act) + /* declare variable node's name*/ + GET_IR_NODE(x); + GET_IR_NODE(mul_1_w); + GET_IR_NODE(mul_1_w_max); + GET_IR_NODE(mul_1_bias); + GET_IR_NODE(mul_1_out); + GET_IR_NODE(mul_2_w); + GET_IR_NODE(mul_2_w_max); + GET_IR_NODE(mul_2_bias); + GET_IR_NODE(mul_2_out); + GET_IR_NODE(ew_mul_out); + GET_IR_NODE(ew_branch_add_in); + GET_IR_NODE(ew_branch_add_out); + GET_IR_NODE(block_act_out); + + auto* block = pool2d->Op()->Block(); + auto* scope = param_scope(); + PADDLE_ENFORCE_NOT_NULL( + scope, platform::errors::InvalidArgument("Scope cannot be nullptr.")); + + framework::OpDesc fused_op_desc(block); + fused_op_desc.SetType("squeeze_excitation_block"); + fused_op_desc.SetInput("x", {x->Name()}); + if (with_branch) { + fused_op_desc.SetInput("branch", {ew_branch_add_in->Name()}); + } + // filter + auto mul_1_w_name = mul_1_w->Name(); + auto* mul_1_w_t = + scope->FindVar(mul_1_w_name)->GetMutable(); + auto mul_1_w_dims = mul_1_w_t->dims(); + auto mul_1_w_len = mul_1_w_t->numel(); + int16_t* mul_1_w_ptr = mul_1_w_t->data(); + auto* mul_2_w_t = + scope->FindVar(mul_2_w->Name())->GetMutable(); + auto mul_2_w_dims = mul_2_w_t->dims(); + auto mul_2_w_len = mul_2_w_t->numel(); + int16_t* mul_2_w_ptr = mul_2_w_t->data(); + if (mul_1_w_dims[0] != mul_2_w_dims[1] || + mul_1_w_dims[1] != mul_2_w_dims[0] || + mul_1_w_len != mul_1_w_dims[0] * mul_1_w_dims[1]) { + LOG(FATAL) << "Error: Dims of excitation mul1 weight is: " << mul_1_w_dims + << ", but get dims of excitation mul2 weight is: " + << mul_2_w_dims; + } + std::vector encode_filter_int16; + encode_filter_int16.resize(mul_1_w_len + mul_2_w_len); + + PADDLE_ENFORCE_EQ(mul_1_w_dims[1] % mul_1_w_dims[0] == 0, + 1, + platform::errors::InvalidArgument( + "Reduction ratio of excitation is not an integer." + "Received mul_1_w_dims[1]: %d, mul_1_w_dims[0]: %d", + mul_1_w_dims[1], + mul_1_w_dims[0])); + fused_op_desc.SetAttr( + "filter_dims", + std::vector{static_cast(mul_1_w_dims[1] / mul_1_w_dims[0]), + static_cast(mul_1_w_dims[1])}); + + paddle::framework::ir::math::Transpose(mul_1_w_ptr, + encode_filter_int16.data(), + mul_1_w_dims[0], + mul_1_w_dims[1]), + paddle::framework::ir::math::Transpose( + mul_2_w_ptr, + encode_filter_int16.data() + mul_1_w_len, + mul_2_w_dims[0], + mul_2_w_dims[1]); + + std::string new_filter_name = "se_" + mul_1_w_name; + Node* new_filter_node = nullptr; + VarDesc dst_desc(new_filter_name); + dst_desc.SetPersistable(true); + dst_desc.SetShape({mul_1_w_len + mul_2_w_len}); + dst_desc.SetDataType(framework::TransToProtoVarType(mul_1_w_t->dtype())); + new_filter_node = graph->CreateVarNode(&dst_desc); + auto* block_dst_desc = block->Var(new_filter_name); + block_dst_desc->SetPersistable(dst_desc.Persistable()); + block_dst_desc->SetShape(dst_desc.GetShape()); + block_dst_desc->SetDataType(dst_desc.GetDataType()); + + phi::DenseTensor new_filter_t; + new_filter_t.Resize(DDim({mul_1_w_len + mul_2_w_len})); + new_filter_t.set_type(phi::DataType::INT16); + auto* cpu_ctx = static_cast( + platform::DeviceContextPool::Instance().Get(phi::CPUPlace())); + auto* new_filter_data = cpu_ctx->Alloc(&new_filter_t); + + memcpy(new_filter_data, + encode_filter_int16.data(), + (mul_1_w_len + mul_2_w_len) * sizeof(int16_t)); + + Assign(new_filter_t, + scope->Var(new_filter_name)->GetMutable()); + fused_op_desc.SetInput("filter", {new_filter_name}); + + // filter max + std::vector encode_filter_max; + int max_ptr_size = phi::backends::xpu::get_xpu_max_ptr_size(-1); + int filter_max_size = max_ptr_size + max_ptr_size; + encode_filter_max.resize(filter_max_size); + + auto mul_1_w_max_name = mul_1_w_max->Name(); + auto mul_2_w_max_name = mul_2_w_max->Name(); + auto* mul_1_w_max_t = + scope->FindVar(mul_1_w_max_name)->GetMutable(); + auto* mul_2_w_max_t = + scope->FindVar(mul_2_w_max_name)->GetMutable(); + + float* mul_1_w_max_ptr = mul_1_w_max_t->data(); + float* mul_2_w_max_ptr = mul_2_w_max_t->data(); + memcpy(encode_filter_max.data(), + mul_1_w_max_ptr, + max_ptr_size * sizeof(float)); + memcpy(encode_filter_max.data() + max_ptr_size, + mul_2_w_max_ptr, + max_ptr_size * sizeof(float)); + + std::string new_filter_max_name = new_filter_name + "_max"; + Node* new_filter_max_node = nullptr; + VarDesc filter_max_desc(new_filter_max_name); + filter_max_desc.SetPersistable(true); + filter_max_desc.SetShape({filter_max_size}); + filter_max_desc.SetDataType( + framework::TransToProtoVarType(mul_1_w_max_t->dtype())); + new_filter_max_node = graph->CreateVarNode(&filter_max_desc); + auto* block_filter_max_desc = block->Var(new_filter_max_name); + block_filter_max_desc->SetPersistable(filter_max_desc.Persistable()); + block_filter_max_desc->SetShape(filter_max_desc.GetShape()); + block_filter_max_desc->SetDataType(filter_max_desc.GetDataType()); + + phi::DenseTensor new_filter_max_t; + new_filter_max_t.Resize(DDim({filter_max_size})); + new_filter_max_t.set_type(phi::DataType::FLOAT32); + auto* new_filter_max_data = cpu_ctx->Alloc(&new_filter_max_t); + + memcpy(new_filter_max_data, + encode_filter_max.data(), + (filter_max_size) * sizeof(float)); + + Assign(new_filter_max_t, + scope->Var(new_filter_max_name)->GetMutable()); + + fused_op_desc.SetInput("filter_max", {new_filter_max_name}); + + // bias + std::string new_bias_name = new_filter_name + "_bias"; + VarDesc new_bias_desc(new_bias_name); + new_bias_desc.SetPersistable(true); + new_bias_desc.SetDataType(proto::VarType::Type::VarType_Type_FP32); + Node* new_bias_node = graph->CreateVarNode(&new_bias_desc); + if (with_bias) { + auto mul_1_bias_name = mul_1_bias->Name(); + auto mul_2_bias_name = mul_2_bias->Name(); + auto* mul_1_bias_t = + scope->FindVar(mul_1_bias_name)->GetMutable(); + auto* mul_2_bias_t = + scope->FindVar(mul_2_bias_name)->GetMutable(); + int mul_1_bias_numel = mul_1_bias_t->numel(); + int mul_2_bias_numel = mul_2_bias_t->numel(); + + std::vector encode_bias; + encode_bias.resize(mul_1_bias_numel + mul_2_bias_numel); + float* mul_1_bias_ptr = mul_1_bias_t->data(); + float* mul_2_bias_ptr = mul_2_bias_t->data(); + + memcpy( + encode_bias.data(), mul_1_bias_ptr, mul_1_bias_numel * sizeof(float)); + memcpy(encode_bias.data() + mul_1_bias_numel, + mul_2_bias_ptr, + mul_2_bias_numel * sizeof(float)); + + new_bias_desc.SetShape({mul_1_bias_numel + mul_2_bias_numel}); + auto* block_new_bias_dst_desc = block->Var(new_bias_name); + block_new_bias_dst_desc->SetPersistable(new_bias_desc.Persistable()); + block_new_bias_dst_desc->SetShape(new_bias_desc.GetShape()); + block_new_bias_dst_desc->SetDataType(new_bias_desc.GetDataType()); + + phi::DenseTensor new_bias_t; + new_bias_t.Resize(DDim({mul_1_bias_numel + mul_2_bias_numel})); + new_bias_t.set_type(phi::DataType::FLOAT32); + auto* cpu_ctx = static_cast( + platform::DeviceContextPool::Instance().Get(phi::CPUPlace())); + auto* new_bias_data = cpu_ctx->Alloc(&new_bias_t); + + memcpy(new_bias_data, + encode_bias.data(), + (mul_1_bias_numel + mul_2_bias_numel) * sizeof(float)); + Assign(new_bias_t, + scope->Var(new_bias_name)->GetMutable()); + fused_op_desc.SetInput("bias", {new_bias_name}); + } + fused_op_desc.SetAttr("has_bias", with_bias); + fused_op_desc.SetAttr("has_branch", with_branch); + std::string output_name; + if (act_type != "linear") { + output_name = block_act_out->Name(); + } else if (with_branch) { + output_name = ew_branch_add_out->Name(); + } else { + output_name = ew_mul_out->Name(); + } + fused_op_desc.SetOutput("out", {output_name}); + std::string max_output_name = output_name + "_max"; + VarDesc max_out_desc(max_output_name); + auto* max_output_node = graph->CreateVarNode(&max_out_desc); + + fused_op_desc.SetOutput("out_max", {max_output_name}); + fused_op_desc.SetAttr("op_type", std::vector{4}); + fused_op_desc.SetAttr("place_x", std::vector{0}); + fused_op_desc.SetAttr("place_y", std::vector{9}); + fused_op_desc.SetAttr("place_z", std::vector{10}); + fused_op_desc.SetAttr("strides", std::vector{}); + fused_op_desc.SetAttr("paddings", std::vector{}); + fused_op_desc.SetAttr("dilations", std::vector{}); + fused_op_desc.SetAttr("groups", std::vector{}); + fused_op_desc.SetAttr("block_lod", std::vector{1}); + fused_op_desc.SetAttr("conv_bias", std::vector{with_bias}); + + std::map act_map{{"linear", 0}, + {"relu", 1}, + {"sigmoid", 2}, + {"tanh", 3}, + {"leaky_relu", 5}, + {"hard_swish", 14}, + {"hard_sigmoid", 15}, + {"relu6", 17}}; + + float block_act_param_ = 0.f; + if (act_type == "leak_relu") { + block_act_param_ = + PADDLE_GET_CONST(float, block_act->Op()->GetAttr("alpha")); + } else if (act_type == "hard_sigmoid") { + block_act_param_ = + PADDLE_GET_CONST(float, block_act->Op()->GetAttr("slope")); + } + fused_op_desc.SetAttr( + "act_type", + std::vector{ + PADDLE_GET_CONST(int, mul_1->Op()->GetAttr("act_type")), + PADDLE_GET_CONST(int, mul_2->Op()->GetAttr("act_type")), + act_map[act_type]}); + + fused_op_desc.SetAttr( + "act_param", + std::vector{ + PADDLE_GET_CONST(float, mul_1->Op()->GetAttr("act_param")), + PADDLE_GET_CONST(float, mul_2->Op()->GetAttr("act_param")), + block_act_param_}); + + auto* new_op_node = graph->CreateOpNode(&fused_op_desc); + IR_NODE_LINK_TO(x, new_op_node); + if (with_branch) { + IR_NODE_LINK_TO(ew_branch_add_in, new_op_node); + } + IR_NODE_LINK_TO(new_filter_node, new_op_node); + IR_NODE_LINK_TO(new_filter_max_node, new_op_node); + + if (with_bias) { + IR_NODE_LINK_TO(new_bias_node, new_op_node); + } + + if (act_type != "linear") { + IR_NODE_LINK_TO(new_op_node, block_act_out); + } else if (with_branch) { + IR_NODE_LINK_TO(new_op_node, ew_branch_add_out); + } else { + IR_NODE_LINK_TO(new_op_node, ew_mul_out); + } + IR_NODE_LINK_TO(new_op_node, max_output_node); + // delete useless node + std::unordered_set delete_nodes = { + pool2d, mul_1, mul_1_out, mul_2, mul_2_out, ew_mul}; + if (with_bias) { + delete_nodes.insert(mul_1_bias); + delete_nodes.insert(mul_2_bias); + } + if (with_branch) { + delete_nodes.insert(ew_branch_add); + } + GraphSafeRemoveNodes(graph, delete_nodes); + found_subgraph_count++; + }; + + gpd(graph, handler); + return found_subgraph_count; +} + +} // namespace ir +} // namespace framework +} // namespace paddle + +REGISTER_PASS(squeeze_excitation_fuse_pass, + paddle::framework::ir::SqueezeExcitationFusePass); + +REGISTER_PASS_CAPABILITY(squeeze_excitation_fuse_pass) + .AddCombination( + paddle::framework::compatible::OpVersionComparatorCombination().EQ( + "squeeze_excitation_block", 0)); diff --git a/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.h b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.h new file mode 100644 index 0000000000000..72249f6fdc087 --- /dev/null +++ b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass.h @@ -0,0 +1,82 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +#pragma once +#include +#include + +#include "paddle/fluid/framework/ir/fuse_pass_base.h" +#include "paddle/fluid/framework/ir/pass.h" + +namespace phi { +class DenseTensor; +} // namespace phi + +namespace paddle { +namespace framework { +class Scope; +} // namespace framework +} // namespace paddle + +namespace paddle { +namespace framework { +namespace ir { +/* +Squeeze and Excitaion Block Fusion for SE-ResNet +Origin subgraph + Input + | \ + | \ + | \ + | | + | Global Pooling + | | + | conv2d_xpu + | | + | | + | conv2d_xpu + \ | + \ | + elementwise_mul + | + Output +------------------------------------------------------ +After the pass is applied: + + in_Input + in_Filter | in_FilterMax + \ | / + \ | / + in_Branch ------- squeeze_excitation_block ------ in_Bias + | + | + | + out_Output +*/ +class SqueezeExcitationFusePass : public FusePassBase { + protected: + void ApplyImpl(ir::Graph* graph) const override; + + private: + int ApplyImpl(ir::Graph* graph, + const std::string& op_type, + const std::string& act_type, + bool with_branch, + bool with_bias) const; + + const std::string name_scope_{"squeeze_excitation_fuse_pass"}; +}; + +} // namespace ir +} // namespace framework +} // namespace paddle diff --git a/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass_test.cc b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass_test.cc new file mode 100644 index 0000000000000..9442049220d29 --- /dev/null +++ b/paddle/fluid/framework/ir/xpu/squeeze_excitation_fuse_pass_test.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include +#include "paddle/fluid/framework/ir/pass.h" +#include "paddle/fluid/framework/ir/pass_tester_helper.h" + +namespace paddle { +namespace framework { +namespace ir { + +TEST(SqueezeExcitationFusePass, V1) { + Layers layers; + auto* block = layers.Block(); + + auto* pool2d_inp = layers.data("pool2d_inp", {1, 24, 14, 14}); + auto* pool2d_out = layers.pool2d(pool2d_inp, false); + + auto* conv2d_xpu_op1_out = layers.data("conv2d_xpu_op1_out"); + OpDesc* conv2d_xpu_op1 = block->AppendOp(); + conv2d_xpu_op1->SetType("conv2d_xpu"); + conv2d_xpu_op1->SetInput("x", {pool2d_out->Name()}); + conv2d_xpu_op1->SetOutput("out", {conv2d_xpu_op1_out->Name()}); + + auto* conv2d_xpu_op2_out = layers.data("conv2d_xpu_op2_out"); + OpDesc* conv2d_xpu_op2 = block->AppendOp(); + conv2d_xpu_op2->SetType("conv2d_xpu"); + conv2d_xpu_op2->SetInput("x", {conv2d_xpu_op1_out->Name()}); + conv2d_xpu_op2->SetOutput("out", {conv2d_xpu_op2_out->Name()}); + + layers.elementwise_mul(pool2d_inp, conv2d_xpu_op2_out); + + std::unique_ptr graph(new ir::Graph(layers.main_program())); + auto pass = PassRegistry::Instance().Get("squeeze_excitation_fuse_pass"); + pass->Apply(graph.get()); + auto num = GetNumOpNodes(graph, "pool2d") + + GetNumOpNodes(graph, "conv2d_xpu") + + GetNumOpNodes(graph, "elementwise_mul"); + PADDLE_ENFORCE_EQ(num, + 0, + platform::errors::PreconditionNotMet( + "pool2d/conv2d_xpu/elementwise_mul ops should be " + "removed from graph, but graph " + "still has %d ops. ", + num)); +} + +} // namespace ir +} // namespace framework +} // namespace paddle + +USE_PASS(squeeze_excitation_fuse_pass); diff --git a/paddle/fluid/inference/api/paddle_pass_builder.cc b/paddle/fluid/inference/api/paddle_pass_builder.cc index 0c5423fe4d915..ba71eff17387d 100644 --- a/paddle/fluid/inference/api/paddle_pass_builder.cc +++ b/paddle/fluid/inference/api/paddle_pass_builder.cc @@ -547,6 +547,7 @@ XpuPassStrategy::XpuPassStrategy() : PassStrategy({}) { "fc_xpu_fuse_pass", "conv2d_xpu_fuse_pass", "conv2d_transpose_xpu_fuse_pass", + "squeeze_excitation_fuse_pass", "add_activation_xpu_fuse_pass", "add_layernorm_xpu_fuse_pass", "fast_layernorm_xpu_fuse_pass", diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index 53da3a659758c..ac19987119d10 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -208,6 +208,16 @@ data_type : input optional : bias_qk +- op : squeeze_excitation_block + args : (Tensor x, Tensor filter, Tensor filter_max, Tensor bias, Tensor branch, int[] act_type, float[] act_param, int[] filter_dims) + output : Tensor(out) + infer_meta : + func : SqueezeExcitationInferMeta + kernel : + func : squeeze_excitation_block + data_type : x + optional : bias, branch + - op : yolo_box_xpu args : (Tensor x, Tensor x_max, Tensor grid, Tensor stride, Tensor anchor_grid, float offset) output : Tensor(out), Tensor(out_max) diff --git a/paddle/phi/backends/xpu/xpu2_op_list.cc b/paddle/phi/backends/xpu/xpu2_op_list.cc index d52769723e34f..41e69df801e82 100644 --- a/paddle/phi/backends/xpu/xpu2_op_list.cc +++ b/paddle/phi/backends/xpu/xpu2_op_list.cc @@ -1005,6 +1005,7 @@ XPUOpMap& get_kl2_ops() { {"sequence_conv_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"sequence_unpad", XPUKernelSet({phi::DataType::FLOAT32})}, // Fused op + {"squeeze_excitation_block", XPUKernelSet({phi::DataType::FLOAT32})}, {"resnet_basic_block_grad", XPUKernelSet({phi::DataType::FLOAT32})}, {"resnet_basic_block", XPUKernelSet({phi::DataType::FLOAT32})}, {"fused_gemm_epilogue", diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 7a10fd752acf3..49ec2405051fe 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -964,4 +964,29 @@ void FusedScaleBiasReluConvBnstatsInferMeta( eq_bias->set_dims(c_dims); } +void SqueezeExcitationInferMeta(const MetaTensor& x, + const MetaTensor& filter, + const MetaTensor& filter_max, + const MetaTensor& bias, + const MetaTensor& branch, + const std::vector& act_type, + const std::vector& act_param, + const std::vector& filter_dims, + MetaTensor* out) { + auto in_dims = x.dims(); + // do some checks + PADDLE_ENFORCE_EQ( + in_dims.size(), + 4, + phi::errors::InvalidArgument( + "The input should be a 4-D Tensor. But " + "received: input's dimension is %u, input's shape is [%s].", + in_dims.size(), + in_dims)); + std::vector out_shape( + {in_dims[0], filter_dims[1], in_dims[2], in_dims[3]}); + // set output dims + out->set_dims(DDim(out_shape.data(), out_shape.size())); +} + } // namespace phi diff --git a/paddle/phi/infermeta/fusion.h b/paddle/phi/infermeta/fusion.h index ee41d55ca5524..dd5fcfcbf8589 100644 --- a/paddle/phi/infermeta/fusion.h +++ b/paddle/phi/infermeta/fusion.h @@ -234,4 +234,14 @@ void FusedScaleBiasReluConvBnstatsInferMeta( MetaTensor* eq_scale, MetaTensor* eq_bias); +void SqueezeExcitationInferMeta(const MetaTensor& x, + const MetaTensor& filter, + const MetaTensor& filter_max, + const MetaTensor& bias, + const MetaTensor& branch, + const std::vector& act_type, + const std::vector& act_param, + const std::vector& filter_dims, + MetaTensor* out); + } // namespace phi diff --git a/paddle/phi/kernels/fusion/xpu/squeeze_excitation_block_xpu_kernel.cc b/paddle/phi/kernels/fusion/xpu/squeeze_excitation_block_xpu_kernel.cc new file mode 100644 index 0000000000000..c3ded28ecad97 --- /dev/null +++ b/paddle/phi/kernels/fusion/xpu/squeeze_excitation_block_xpu_kernel.cc @@ -0,0 +1,126 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "glog/logging.h" +#include "paddle/phi/backends/xpu/enforce_xpu.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { +namespace fusion { + +template +void SqueezeExcitationKernelImpl(const Context& ctx, + const DenseTensor& x, + const DenseTensor& filter, + const DenseTensor& filter_max, + const paddle::optional& bias, + const paddle::optional& branch, + const std::vector& act_type, + const std::vector& act_param, + const std::vector& filter_dims, + DenseTensor* out) { + using XPUTypeX = typename XPUTypeTrait::Type; + using XPUTypeW = typename XPUTypeTrait::Type; + + auto* weight1_ptr = filter.data(); + auto weight_len = filter.numel(); + auto weight1_len = weight_len / 2; + auto* weight2_ptr = weight1_ptr + weight1_len; + + auto input_dims = x.dims(); + + int batch = static_cast(input_dims[0]); + int channel = static_cast(input_dims[1]); + int h = static_cast(input_dims[2]); + int w = static_cast(input_dims[3]); + auto* input_data = reinterpret_cast(x.data()); + const XPUTypeX* branch_data = nullptr; + auto* branch_tensor = branch.get_ptr(); + if (branch_tensor != nullptr) { + branch_data = reinterpret_cast(branch_tensor->data()); + } + const float* bias1_ptr = + bias.get_ptr() == nullptr ? nullptr : bias.get_ptr()->data(); + const float* bias2_ptr = (bias1_ptr != nullptr) + ? (bias1_ptr + filter_dims[1] / filter_dims[0]) + : nullptr; + int max_ptr_size = 6; + const float* w1_maxptr = filter_max.data(); + const float* w2_maxptr = w1_maxptr + max_ptr_size; + auto* out_data = + reinterpret_cast(ctx.template Alloc(out)); + + std::vector act; + for (size_t i = 0; i < 3; i++) { + xpu::Activation_t cur_act = (xpu::Activation_t::act_enum)act_type[i]; + if (act_type[i] == 5) { + cur_act.leaky_alpha = act_param[i]; + } else if (act_type[i] == 15) { + cur_act.hard_sigmoid_slope = act_param[i]; + } + act.push_back(cur_act); + } + int r = xpu::squeeze_excitation_block( + /* baidu::xpu::api::Context* ctx */ ctx.x_context(), + /* const T* x */ input_data, + /* const TW* weight1 */ reinterpret_cast(weight1_ptr), + /* const TW* weight2 */ reinterpret_cast(weight2_ptr), + /* T* y */ out_data, + /* int64_t n x */ batch, + /* int64_t c x */ channel, + /* int64_t h */ h, + /* int64_t w */ w, + /* int64_t r */ filter_dims[0], + /* const float* w1_maxptr */ reinterpret_cast(w1_maxptr), + /* const float* w2_maxptr */ reinterpret_cast(w2_maxptr), + /* const float* bias1 x */ bias1_ptr, + /* const float* bias2 */ bias2_ptr, + /* const T* branch */ branch_data, + /* const Activation_t& excitation_act1 */ act[0], + /* const Activation_t& excitation_act2 */ act[1], + /* const Activation_t& block_act */ act[2]); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "squeeze_excitation_block"); +} + +template +void SqueezeExcitationKernel(const Context& ctx, + const DenseTensor& x, + const DenseTensor& filter, + const DenseTensor& filter_max, + const paddle::optional& bias, + const paddle::optional& branch, + const std::vector& act_type, + const std::vector& act_param, + const std::vector& filter_dims, + DenseTensor* out) { + SqueezeExcitationKernelImpl(ctx, + x, + filter, + filter_max, + bias, + branch, + act_type, + act_param, + filter_dims, + out); +} + +} // namespace fusion +} // namespace phi + +PD_REGISTER_KERNEL(squeeze_excitation_block, + XPU, + ALL_LAYOUT, + phi::fusion::SqueezeExcitationKernel, + float) {} diff --git a/test/ir/inference/test_xpu_squeeze_excitation_fuse_pass.py b/test/ir/inference/test_xpu_squeeze_excitation_fuse_pass.py new file mode 100644 index 0000000000000..de32b3730787f --- /dev/null +++ b/test/ir/inference/test_xpu_squeeze_excitation_fuse_pass.py @@ -0,0 +1,163 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest +from functools import partial + +import hypothesis.strategies as st +import numpy as np +from auto_scan_test import PassAutoScanTest +from program_config import OpConfig, ProgramConfig, TensorConfig + + +class TestSqueezeExcitationFusePass(PassAutoScanTest): + def sample_predictor_configs(self, program_config): + config = self.create_inference_config(use_xpu=True) + yield config, ["squeeze_excitation_block"], (1e-3, 1e-3) + + def sample_program_config(self, draw): + def generate_data(shape): + return np.random.random(shape).astype(np.float32) + + x_shape = draw( + st.lists( + st.integers(min_value=1, max_value=12), min_size=4, max_size=4 + ) + ) + x_shape[1] = 24 + + oc = 6 + conv2d_op1_w_shape = [oc, x_shape[1], 1, 1] + conv2d_op1_b_shape = [oc] + conv2d_op2_w_shape = [x_shape[1], oc, 1, 1] + conv2d_op2_b_shape = [x_shape[1]] + + # Random choose if add a relu operator + has_relu = draw(st.sampled_from([True, False])) + + pool2d_op = OpConfig( + type="pool2d", + inputs={"X": ["pool2d_x"]}, + outputs={"Out": ["pool2d_out"]}, + adaptive=True, + data_format="NCHW", + global_pooling=False, + ksize=[1, 1], + pooling_type="avg", + ) + ops = [pool2d_op] + + conv2d_op = OpConfig( + "conv2d", + inputs={ + "Input": ["pool2d_out"], + "Filter": ["conv2d_weight"], + }, + outputs={"Output": ["conv2d_out"]}, + data_format="NCHW", + dilations=[1, 1], + padding_algorithm="EXPLICIT", + groups=1, + paddings=[0, 0, 0, 0], + strides=[1, 1], + has_bias=False, + ) + + ew_bias_op = OpConfig( + "elementwise_add", + inputs={"X": ["conv2d_out"], "Y": ["ew_bias"]}, + outputs={"Out": ["add_out"]}, + axis=1, + ) + ops.extend([conv2d_op, ew_bias_op]) + conv2d_input = "add_out" + # 3. activation + if has_relu: + relu_op = OpConfig( + "relu", inputs={"X": ["add_out"]}, outputs={"Out": ["relu_out"]} + ) + conv2d_input = "relu_out" + ops.append(relu_op) + + conv2d_op2 = OpConfig( + "conv2d", + inputs={ + "Input": [conv2d_input], + "Filter": ["conv2d_weight2"], + }, + outputs={"Output": ["conv2d_out2"]}, + data_format="NCHW", + dilations=[1, 1], + padding_algorithm="EXPLICIT", + groups=1, + paddings=[0, 0, 0, 0], + strides=[1, 1], + has_bias=False, + ) + + ew_bias_op2 = OpConfig( + "elementwise_add", + inputs={"X": ["conv2d_out2"], "Y": ["ew_bias2"]}, + outputs={"Out": ["add_out2"]}, + axis=1, + ) + ops.extend([conv2d_op2, ew_bias_op2]) + ele_mul_input = "add_out2" + # 3. activation + if has_relu: + relu_op2 = OpConfig( + "relu", + inputs={"X": ["add_out2"]}, + outputs={"Out": ["relu_out2"]}, + ) + ele_mul_input = "relu_out2" + ops.append(relu_op2) + + ew_mul_op = OpConfig( + "elementwise_mul", + inputs={"X": ["pool2d_x"], "Y": [ele_mul_input]}, + outputs={"Out": ["ew_mul_out"]}, + axis=-1, + ) + ops.append(ew_mul_op) + + program_config = ProgramConfig( + ops=ops, + weights={ + "conv2d_weight": TensorConfig( + data_gen=partial(generate_data, conv2d_op1_w_shape) + ), + "ew_bias": TensorConfig(shape=conv2d_op1_b_shape), + "conv2d_weight2": TensorConfig( + data_gen=partial(generate_data, conv2d_op2_w_shape) + ), + "ew_bias2": TensorConfig(shape=conv2d_op2_b_shape), + }, + inputs={ + "pool2d_x": TensorConfig(shape=x_shape), + }, + outputs=ops[-1].outputs["Out"], + ) + return program_config + + def test(self): + self.run_and_statis( + quant=False, + max_examples=25, + passes=["squeeze_excitation_fuse_pass"], + ) + + +if __name__ == "__main__": + unittest.main() From d8122a23d5068d21f74c11eb4bb8e0a5b779d567 Mon Sep 17 00:00:00 2001 From: Xianduo Li <30922914+lxd-cumt@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:56:50 +0800 Subject: [PATCH 159/194] support for checking op's inputs grad semantic (#56925) * add in OpInputInfo to represent whether an input of OP has grad semantics * add support for check OP's input grad semantic by comparing fwd_op inputs and bwd_op outputs * add pybind interface to support checking OP's inputs grad semantic in python-level * add test * fix bugs * fix bugs in op_gen * fix bugs in op_gen * add test for multiply_op * fix bugs in codestype * fix bugs in codestype --- .../fluid/ir/dialect/op_generator/op_gen.py | 94 ++++++++++++++++++- .../dialect/paddle_dialect/ir/pd_manual_op.cc | 14 ++- .../paddle_dialect/utils/op_yaml_info_util.h | 7 +- paddle/fluid/pybind/ir.cc | 11 +++ .../pattern_rewrite/pattern_rewrite_test.cc | 32 +++++-- test/ir/test_op_input_grad_semantic.py | 69 ++++++++++++++ 6 files changed, 212 insertions(+), 15 deletions(-) create mode 100644 test/ir/test_op_input_grad_semantic.py diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index 7ee65d050581b..12a39f82ddea7 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -156,7 +156,7 @@ class {op_name} : public ir::Op<{op_name}{interfaces}{traits}> {{ return std::make_tuple(inputs, attributes, outputs, run_time_info, "{origin_op_name}"); }} """ -CONSTRUCT_INPUT_INFO_TEMPLATE = """paddle::dialect::OpInputInfo("{name}", "{typename}", {optional}, {no_need_buffer}, {is_mutable_attribute})""" +CONSTRUCT_INPUT_INFO_TEMPLATE = """paddle::dialect::OpInputInfo("{name}", "{typename}", {optional}, {no_need_buffer}, {is_mutable_attribute}, {with_grad_semantic})""" CONSTRUCT_OUTPUT_INFO_TEMPLATE = """paddle::dialect::OpOutputInfo("{name}", "{typename}", {optional}, {intermediate})""" CONSTRUCT_ATTRIBUTE_INFO_TEMPLATE = """paddle::dialect::OpAttributeInfo("{name}", "{typename}", "{data_type}")""" @@ -345,6 +345,23 @@ def __init__(self, op_yaml_item, op_compat_item): # parse has_custom_verify self.custom_verify = self.parse_custom_verify() + # parse forward input name list and attribute name list + self.forward_input_name_list = self.parse_forward_input_name() + + def parse_forward_input_name(self): + if 'forward' in self.op_yaml_item: + forward_input_name_list = [] + forward_map = self.op_yaml_item['forward'] + if forward_map is not None: + inputs = forward_map['inputs'] + for input in inputs: + forward_input_name_list.append(input['name']) + return forward_input_name_list + else: + return None + else: + return None + def cross_check(self, name_list, type_list, optional_list=None): assert len(name_list) == len( type_list @@ -705,6 +722,69 @@ def to_pascal_case(s): return "".join([word.capitalize() for word in words]) + "" +def OpInputGradSemanticCheck(op_info, op_info_items): + input_grad_semantic_list = [] + num_inputs = len(op_info.input_name_list) + + # get backward op + bwd_op_name = op_info.backward_name + if (bwd_op_name is None) or (bwd_op_name not in op_info_items.keys()): + input_grad_semantic_list = ["false" for i in range(num_inputs)] + else: + bwd_op_info = op_info_items[bwd_op_name] + + # cut "_grad" of each output of bwd_op, and then compare each modified output with corresponding input + # thus determine whether each input has grad semantic + bwd_output_list = bwd_op_info.output_name_list + bwd_output_list_new = [] + for bwd_output in bwd_output_list: + bwd_output_list_new.append(bwd_output[:-5]) # cut _grad + + bwd_fwd_input_list = bwd_op_info.forward_input_name_list + if bwd_fwd_input_list is not None: + assert ( + len(bwd_fwd_input_list) == num_inputs + ), "Configuration of forward op and backward op is not match." + for i in range(num_inputs): + if bwd_fwd_input_list[i] in bwd_output_list_new: + input_grad_semantic_list.append("true") + else: + input_grad_semantic_list.append("false") + else: + input_grad_semantic_list = ["false" for i in range(num_inputs)] + + return input_grad_semantic_list + + +def OpMutableAttributeGradSemanticCheck(op_info, op_info_items): + mutable_attribute_grad_semantic_list = [] + fwd_mutable_attribute_list = op_info.mutable_attribute_name_list + + # get backward op + bwd_op_name = op_info.backward_name + if (bwd_op_name is None) or (bwd_op_name not in op_info_items.keys()): + mutable_attribute_grad_semantic_list = [ + "false" for i in range(len(fwd_mutable_attribute_list)) + ] + else: + bwd_op_info = op_info_items[bwd_op_name] + + # cut "_grad" of each output of bwd_op, and then compare each modified output with corresponding attribute + # thus determine whether each attribute has grad semantic + bwd_output_list = bwd_op_info.output_name_list + bwd_output_list_new = [] + for bwd_output in bwd_output_list: + bwd_output_list_new.append(bwd_output[:-5]) + + for i in range(len(fwd_mutable_attribute_list)): + if fwd_mutable_attribute_list[i] in bwd_output_list_new: + mutable_attribute_grad_semantic_list.append("true") + else: + mutable_attribute_grad_semantic_list.append("false") + + return mutable_attribute_grad_semantic_list + + def OpGenerator( op_yaml_files, op_compat_yaml_file, @@ -793,6 +873,14 @@ def OpGenerator( op_interfaces += ["paddle::dialect::VjpInterface"] exclusive_interface_str = gen_exclusive_interface_str(op_info) + # check op inputs and mutable_attributes grad semantics + input_grad_semantic_list = OpInputGradSemanticCheck( + op_info, op_info_items + ) + mutable_attribute_grad_semantic_list = ( + OpMutableAttributeGradSemanticCheck(op_info, op_info_items) + ) + # If op has inplace info, we will generate inplace op and non-inplace op. for op_name in op_info.op_phi_name: if op_name in _NO_NEED_GEN_OPS: @@ -971,6 +1059,7 @@ def OpGenerator( optional=op_input_optional_list[idx], no_need_buffer=op_input_no_need_buffer_list[idx], is_mutable_attribute='false', + with_grad_semantic=input_grad_semantic_list[idx], ) ) for idx in range(len(op_mutable_attribute_name_list)): @@ -981,6 +1070,9 @@ def OpGenerator( optional='false', no_need_buffer='false', is_mutable_attribute='true', + with_grad_semantic=mutable_attribute_grad_semantic_list[ + idx + ], ) ) if len(input_info_list) > 0: diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index d50faedfd565f..d6e1ad5252426 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -35,7 +35,8 @@ OpInfoTuple AddNOp::GetOpInfo() { "ir::VectorType", false, false, - false)}; + false, + true)}; std::vector attributes = {}; std::vector outputs = { OpOutputInfo("out", "paddle::dialect::DenseTensorType", false, false)}; @@ -416,9 +417,14 @@ OpInfoTuple SplitGradOp::GetOpInfo() { "ir::VectorType", false, false, - false), - OpInputInfo( - "axis", "paddle::dialect::ScalarAttribute", false, false, true)}; + false, + true), + OpInputInfo("axis", + "paddle::dialect::ScalarAttribute", + false, + false, + true, + false)}; std::vector attributes = {}; std::vector outputs = { OpOutputInfo("x_grad", "paddle::dialect::DenseTensorType", false, false)}; diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h index eaa37a3a7de9f..69a4d848cfe60 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h @@ -27,6 +27,7 @@ struct OpInputInfo { bool optional = false; bool no_need_buffer = false; bool is_mutable_attribute = false; + bool with_grad_semantic = true; OpInputInfo() = default; OpInputInfo(const OpInputInfo& input_info) = default; @@ -34,12 +35,14 @@ struct OpInputInfo { const std::string& type_name, bool optional, bool no_need_buffer, - bool is_mutable_attribute) + bool is_mutable_attribute, + bool with_grad_semantic) : name(name), type_name(type_name), optional(optional), no_need_buffer(no_need_buffer), - is_mutable_attribute(is_mutable_attribute) {} + is_mutable_attribute(is_mutable_attribute), + with_grad_semantic(with_grad_semantic) {} }; struct OpOutputInfo { diff --git a/paddle/fluid/pybind/ir.cc b/paddle/fluid/pybind/ir.cc index 2838bfa2fb2b9..4dc36fe785ecc 100644 --- a/paddle/fluid/pybind/ir.cc +++ b/paddle/fluid/pybind/ir.cc @@ -247,6 +247,17 @@ void BindOperation(py::module *m) { } return op_list; }) + .def("get_input_grad_semantics", + [](Operation &self) -> py::list { + py::list op_list; + paddle::dialect::OpYamlInfoInterface yaml_interface = + self.dyn_cast(); + auto inputs_grad_info = std::get<0>(yaml_interface.GetOpInfo()); + for (auto &input_grad_info : inputs_grad_info) { + op_list.append(input_grad_info.with_grad_semantic); + } + return op_list; + }) .def("replace_all_uses_with", [](Operation &self, const std::vector &op_results) { self.ReplaceAllUsesWith(op_results); diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index fcca8cde7d5aa..6dc24d09d78de 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -429,14 +429,30 @@ const char *Conv2dFusionOpTest::attributes_name[10] = { // NOLINT OpInfoTuple Conv2dFusionOpTest::GetOpInfo() { std::vector inputs = { - OpInputInfo( - "input", "paddle::dialect::DenseTensorType", false, false, false), - OpInputInfo( - "filter", "paddle::dialect::DenseTensorType", false, false, false), - OpInputInfo( - "bias", "paddle::dialect::DenseTensorType", false, false, false), - OpInputInfo( - "residual", "paddle::dialect::DenseTensorType", true, false, false)}; + OpInputInfo("input", + "paddle::dialect::DenseTensorType", + false, + false, + false, + true), + OpInputInfo("filter", + "paddle::dialect::DenseTensorType", + false, + false, + false, + true), + OpInputInfo("bias", + "paddle::dialect::DenseTensorType", + false, + false, + false, + true), + OpInputInfo("residual", + "paddle::dialect::DenseTensorType", + true, + false, + false, + true)}; std::vector attributes = { OpAttributeInfo("strides", "ir::ArrayAttribute", ""), OpAttributeInfo( diff --git a/test/ir/test_op_input_grad_semantic.py b/test/ir/test_op_input_grad_semantic.py new file mode 100644 index 0000000000000..f204cff0aeaf3 --- /dev/null +++ b/test/ir/test_op_input_grad_semantic.py @@ -0,0 +1,69 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import paddle +from paddle import ir + +paddle.enable_static() + + +def get_ir_program_0(): + main_program, start_program = ( + paddle.static.Program(), + paddle.static.Program(), + ) + with paddle.static.program_guard(main_program, start_program): + x = paddle.tensor.fill_constant( + shape=[3, 4], dtype='float32', value=2.0 + ) + index = paddle.tensor.fill_constant(shape=[1], dtype='int32', value=1.0) + axis = paddle.tensor.fill_constant(shape=[1], dtype='int32', value=2.0) + out = paddle.gather(x, index, axis) + newir_program = ir.translate_to_new_ir(main_program.desc) + return newir_program + + +def get_ir_program_1(): + main_program, start_program = ( + paddle.static.Program(), + paddle.static.Program(), + ) + with paddle.static.program_guard(main_program, start_program): + x = paddle.tensor.fill_constant( + shape=[3, 4], dtype='float32', value=2.0 + ) + y = paddle.tensor.fill_constant( + shape=[3, 4], dtype='float32', value=3.0 + ) + out = paddle.multiply(x, y) + newir_program = ir.translate_to_new_ir(main_program.desc) + return newir_program + + +class TestOpInputGradSemantic(unittest.TestCase): + def test_gatherop_input_grad_semantic(self): + newir_program = get_ir_program_0() + op = newir_program.block().ops[-1] + self.assertEqual(op.get_input_grad_semantics(), [True, False, False]) + + def test_multiplyop_input_grad_semantic(self): + newir_program = get_ir_program_1() + op = newir_program.block().ops[-1] + self.assertEqual(op.get_input_grad_semantics(), [True, True]) + + +if __name__ == "__main__": + unittest.main() From b39a2ca5b6af90bb7fcf1681610bcfe126279b53 Mon Sep 17 00:00:00 2001 From: Charles-hit <56987902+Charles-hit@users.noreply.github.com> Date: Wed, 6 Sep 2023 19:37:15 +0800 Subject: [PATCH 160/194] fix ci (#57033) --- paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index d6e1ad5252426..8e27526cebefa 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -160,7 +160,8 @@ OpInfoTuple AddN_Op::GetOpInfo() { "ir::VectorType", false, false, - false)}; + false, + true)}; std::vector attributes = {}; std::vector outputs = { paddle::dialect::OpOutputInfo( @@ -287,7 +288,8 @@ OpInfoTuple AddNWithKernelOp::GetOpInfo() { "ir::VectorType", false, false, - false)}; + false, + true)}; std::vector attributes = {}; std::vector outputs = { paddle::dialect::OpOutputInfo( From 3b5bb4ba5b72d6e08624a0631c3bf1ff37aa69ff Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 6 Sep 2023 19:59:53 +0800 Subject: [PATCH 161/194] fix place setting when translatring fill_constant (#56929) --- paddle/fluid/ir_adaptor/translator/op_translator.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index e22fa5f3b3779..86f31fd92ae90 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -1181,11 +1181,20 @@ struct FillConstant2FullTranscriber : public OpTranscriber { ctx, paddle::dialect::VarTypeToDataType( static_cast(dtype)))}}; + + if (op_desc.HasAttr("force_cpu")) { + bool force_cpu = PADDLE_GET_CONST(bool, op_desc.GetAttr("force_cpu")); + if (force_cpu) { + attribute_map["place"] = + paddle::dialect::PlaceAttribute::get(ctx, phi::CPUPlace()); + } + } + int place_type = PADDLE_GET_CONST(int, op_desc.GetAttr("place_type")); switch (place_type) { case -1: - attribute_map["place"] = - paddle::dialect::PlaceAttribute::get(ctx, phi::CPUPlace()); + attribute_map["place"] = paddle::dialect::PlaceAttribute::get( + ctx, phi::Place(phi::AllocationType::UNDEFINED)); break; case 0: attribute_map["place"] = From e9364a3897fab2e7d8051177ca7b8dde8d76a8ae Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 6 Sep 2023 20:48:32 +0800 Subject: [PATCH 162/194] [AutoParallel] Generate spmd rule and reshard impl in phi api (#56831) * add spmd and reshard code gen * add backward reshard code gen * test matmul forward success * polish test impl * add unsafe mutable value * polish details and add test * fix unittest time out * fix typo * refactor reshard input generate impl * resolve conflict with develop * fix compile error --- .../fluid/operators/generator/parse_utils.py | 2 +- paddle/phi/api/lib/api_gen_utils.cc | 14 +- paddle/phi/api/lib/api_gen_utils.h | 19 +- paddle/phi/api/lib/data_transform.cc | 52 +++- paddle/phi/api/lib/data_transform.h | 14 +- paddle/phi/api/lib/kernel_dispatch.cc | 3 + paddle/phi/api/lib/kernel_dispatch.h | 2 - paddle/phi/api/yaml/generator/api_gen.py | 4 + paddle/phi/api/yaml/generator/dist_api_gen.py | 246 +++++++++++++----- .../phi/api/yaml/generator/dist_bw_api_gen.py | 47 +++- paddle/phi/api/yaml/legacy_ops.yaml | 1 + .../distributed/auto_parallel/dist_tensor.h | 9 + .../auto_parallel/inferspmd_utils.cc | 5 - paddle/phi/infermeta/spmd_rules/rules.h | 6 +- test/auto_parallel/CMakeLists.txt | 4 + .../semi_auto_parallel_for_matmul.py | 84 ++++++ .../test_semi_auto_parallel_basic.py | 41 +++ 17 files changed, 455 insertions(+), 98 deletions(-) create mode 100644 test/auto_parallel/semi_auto_parallel_for_matmul.py create mode 100644 test/auto_parallel/test_semi_auto_parallel_basic.py diff --git a/paddle/fluid/operators/generator/parse_utils.py b/paddle/fluid/operators/generator/parse_utils.py index 92834be0f01d5..3a2429f534573 100644 --- a/paddle/fluid/operators/generator/parse_utils.py +++ b/paddle/fluid/operators/generator/parse_utils.py @@ -366,7 +366,7 @@ def check_op_config(op_entry, op_name): 'composite', 'support_dygraph_mode', ) - infer_meta_key_set = ('func', 'param') + infer_meta_key_set = ('func', 'param', 'spmd_rule') kernel_key_set = ( 'func', 'param', diff --git a/paddle/phi/api/lib/api_gen_utils.cc b/paddle/phi/api/lib/api_gen_utils.cc index c6da10d12dea3..c7501494b1e71 100644 --- a/paddle/phi/api/lib/api_gen_utils.cc +++ b/paddle/phi/api/lib/api_gen_utils.cc @@ -22,6 +22,7 @@ PHI_DECLARE_bool(use_stride_kernel); #include "glog/logging.h" #include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" namespace paddle { @@ -530,13 +531,18 @@ void TransStride(phi::DeviceContext* dev_ctx, /* ------------------ for auto parallel ----------------------- */ -phi::distributed::DistTensor* SetKernelDistOutput(Tensor* out) { +phi::distributed::DistMetaTensor MakeDistMetaTensor( + const phi::TensorBase& tensor) { + return phi::distributed::DistMetaTensor(tensor); +} + +phi::distributed::DistTensor* SetKernelDistOutput( + Tensor* out, const phi::distributed::TensorDistAttr& dist_attr) { if (out) { // TODO(chenweihang): now all dist case are nullptr if (out->impl() == nullptr) { - // TODO(chenweihang): polish code, dist_attr is null now - auto dist_t = std::make_shared( - phi::DDim(), phi::distributed::TensorDistAttr()); + auto dist_t = std::make_shared(phi::DDim(), + dist_attr); out->set_impl(dist_t); } return static_cast(out->impl().get()); diff --git a/paddle/phi/api/lib/api_gen_utils.h b/paddle/phi/api/lib/api_gen_utils.h index 997bb6f8dc805..d0281dfc68184 100644 --- a/paddle/phi/api/lib/api_gen_utils.h +++ b/paddle/phi/api/lib/api_gen_utils.h @@ -18,18 +18,15 @@ limitations under the License. */ #include "paddle/phi/backends/all_context.h" #include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_attr.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_meta_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/meta_tensor.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/core/string_tensor.h" -namespace phi { -namespace distributed { -class DistTensor; -} // namespace distributed -} // namespace phi - namespace paddle { namespace experimental { @@ -139,9 +136,17 @@ void TransStrideLegacy(phi::DeviceContext* dev_ctx, /* ------------------ for auto parallel ----------------------- */ -phi::distributed::DistTensor* SetKernelDistOutput(Tensor* out); +phi::distributed::DistMetaTensor MakeDistMetaTensor( + const phi::TensorBase& tensor); + +phi::distributed::DistTensor* SetKernelDistOutput( + Tensor* out, + const phi::distributed::TensorDistAttr& dist_attr = + phi::distributed::TensorDistAttr()); + std::vector SetKernelDistOutput( std::vector out); + std::vector SetKernelDistOutput( size_t out_size, std::vector* out); diff --git a/paddle/phi/api/lib/data_transform.cc b/paddle/phi/api/lib/data_transform.cc index 0e86b84e074fe..7515ff917f10e 100644 --- a/paddle/phi/api/lib/data_transform.cc +++ b/paddle/phi/api/lib/data_transform.cc @@ -21,6 +21,8 @@ limitations under the License. */ #include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/backends/context_pool.h" #include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" +#include "paddle/phi/core/distributed/auto_parallel/reshard_function.h" +#include "paddle/phi/core/distributed/auto_parallel/reshard_utils.h" #include "paddle/phi/core/flags.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/tensor_utils.h" @@ -597,15 +599,45 @@ void TransDataBackend(const phi::SelectedRows* tensor, /* ------------------ for auto parallel ----------------------- */ +std::shared_ptr ReshardDistTensor( + phi::DeviceContext* dev_ctx, + const Tensor& tensor, + const phi::distributed::TensorDistAttr& dist_attr) { + auto tensor_in = tensor.impl(); + if (tensor_in) { + phi::distributed::DistTensor* dist_tensor = + static_cast(tensor_in.get()); + if (dist_tensor->dist_attr() != dist_attr) { + VLOG(6) << "Reshard tensor from " << dist_tensor->dist_attr() << " to " + << dist_attr; + auto* func = phi::distributed::ChooseProperReshardFunction(*dist_tensor, + dist_attr); + return func->Eval(dev_ctx, *dist_tensor, dist_attr); + } + return std::static_pointer_cast(tensor_in); + } + return nullptr; +} + std::shared_ptr PrepareDataForDistTensor( const Tensor& input, const phi::TensorArgDef& target_args_def, const TransformFlag& transform_flag, bool is_stride_kernel) { - const auto& tensor_in = input.impl(); - if (tensor_in) { - phi::distributed::DistTensor* dist_tensor = - static_cast(tensor_in.get()); + return PrepareDataForDistTensor( + std::static_pointer_cast(input.impl()), + target_args_def, + transform_flag, + is_stride_kernel); +} + +std::shared_ptr PrepareDataForDistTensor( + const std::shared_ptr& input, + const phi::TensorArgDef& target_args_def, + const TransformFlag& transform_flag, + bool is_stride_kernel) { + if (input) { + phi::distributed::DistTensor* dist_tensor = input.get(); const phi::DenseTensor& dense_tensor = dist_tensor->value(); if (!transform_flag.NeedTransform() || !dense_tensor.initialized() || (!NeedTransformPlace( @@ -618,16 +650,18 @@ std::shared_ptr PrepareDataForDistTensor( transform_flag) && !NeedTransform2Contiguous(is_stride_kernel, dense_tensor.meta().is_contiguous()))) { - return std::static_pointer_cast(tensor_in); + return input; } - phi::DenseTensor out = TransformData( - dense_tensor, target_args_def, transform_flag, is_stride_kernel); // TODO(chenweihang): The global meta in DistTensor is not changed, // but the local meta in DenseTensor maybe changed, such as layout // change(NCHW->NHWC), so the new DistTensor's meta maybe not unified. VLOG(6) << "PrepareDataForDistTensor return transformed dist tensor"; - return std::make_shared( - out, dist_tensor->dist_attr()); + auto dist_out = std::make_shared( + dist_tensor->dims(), dist_tensor->dist_attr()); + auto* out = dist_out->unsafe_mutable_value(); + *out = TransformData( + dense_tensor, target_args_def, transform_flag, is_stride_kernel); + return dist_out; } return nullptr; } diff --git a/paddle/phi/api/lib/data_transform.h b/paddle/phi/api/lib/data_transform.h index 4247317857c23..3ac1b94f144ba 100644 --- a/paddle/phi/api/lib/data_transform.h +++ b/paddle/phi/api/lib/data_transform.h @@ -21,8 +21,10 @@ limitations under the License. */ #include "paddle/phi/core/sparse_csr_tensor.h" namespace phi { +class DeviceContext; namespace distributed { class DistTensor; +class TensorDistAttr; } // namespace distributed } // namespace phi @@ -173,13 +175,23 @@ inline bool NeedTransformPlace(const phi::Place& src_place, /* ------------------ for auto parallel ----------------------- */ -// TODO(chenweihang): impl Reshard input and output function +std::shared_ptr ReshardDistTensor( + phi::DeviceContext* dev_ctx, + const Tensor& tensor, + const phi::distributed::TensorDistAttr& dist_attr); + std::shared_ptr PrepareDataForDistTensor( const Tensor& input, const phi::TensorArgDef& target_args_def, const TransformFlag& transform_flag, bool is_stride_kernel); +std::shared_ptr PrepareDataForDistTensor( + const std::shared_ptr& input, + const phi::TensorArgDef& target_args_def, + const TransformFlag& transform_flag, + bool is_stride_kernel); + std::vector> PrepareDataForDistTensor(const std::vector& input, const phi::TensorArgDef& target_args_def, diff --git a/paddle/phi/api/lib/kernel_dispatch.cc b/paddle/phi/api/lib/kernel_dispatch.cc index 81b90769e819e..2ebd3c46d5f72 100644 --- a/paddle/phi/api/lib/kernel_dispatch.cc +++ b/paddle/phi/api/lib/kernel_dispatch.cc @@ -20,6 +20,7 @@ limitations under the License. */ #include "paddle/phi/api/include/context_pool.h" #include "paddle/phi/core/compat/convert_utils.h" +#include "paddle/phi/core/distributed/auto_parallel/dist_tensor.h" #include "paddle/phi/core/string_tensor_utils.h" #include "paddle/phi/core/tensor_utils.h" #ifdef PADDLE_WITH_CUSTOM_DEVICE @@ -50,6 +51,8 @@ bool HasAllocation(const phi::TensorBase& t) { } else if (phi::StringTensor::classof(&t)) { return phi::StringTensorUtils::GetHolder( static_cast(t)) != nullptr; + } else if (phi::distributed::DistTensor::classof(&t)) { + return static_cast(t).defined(); } else { return false; } diff --git a/paddle/phi/api/lib/kernel_dispatch.h b/paddle/phi/api/lib/kernel_dispatch.h index 7ff9ab3b33f53..6acc23b2db7e1 100644 --- a/paddle/phi/api/lib/kernel_dispatch.h +++ b/paddle/phi/api/lib/kernel_dispatch.h @@ -96,8 +96,6 @@ struct KernelKeyParser : ArgsIterator { // data_promote DataTypeSet dtype_set{DataType::UNDEFINED}; - // TODO(chenweihang): deal with multiple diff input Tensors - // TODO(chenweihang): add global device guard method to set backend inline void AssignKernelKeySet(const phi::TensorBase& tensor) { // assign Backend BackendSet tensor_backend_set = detail::GetTensorBackendSet(tensor); diff --git a/paddle/phi/api/yaml/generator/api_gen.py b/paddle/phi/api/yaml/generator/api_gen.py index 5164ebda84035..0c47c23276822 100644 --- a/paddle/phi/api/yaml/generator/api_gen.py +++ b/paddle/phi/api/yaml/generator/api_gen.py @@ -379,6 +379,10 @@ def source_include(header_file_path): #include "paddle/phi/api/profiler/event_tracing.h" #include "paddle/phi/api/profiler/supplement_tracing.h" +#ifdef PADDLE_WITH_DISTRIBUTE +#include "paddle/phi/infermeta/spmd_rules/rules.h" +#endif + PD_DECLARE_bool(conv2d_disable_cudnn); PD_DECLARE_int32(low_precision_op_list); """ diff --git a/paddle/phi/api/yaml/generator/dist_api_gen.py b/paddle/phi/api/yaml/generator/dist_api_gen.py index 5c0b642228ba7..ed671ecdfebd6 100644 --- a/paddle/phi/api/yaml/generator/dist_api_gen.py +++ b/paddle/phi/api/yaml/generator/dist_api_gen.py @@ -44,14 +44,14 @@ MAIN_DIST_BRANCH_TEMPLATE = """ // Auto Parallel condition if ({}) {{ - // 1. Create API Output & Prepare Dist and Dense Output{} - // 2. InferSPMD (Infer Global Shape and DistAttr of Inputs&Outputs){} - // 3. Select Kernel{} - // 4. Reshard Input{} - // 5. PrepareData (DataTransform & Prepare Dist and Dense Input){} - // 6. Infer Local DenseTensor Meta{} - // 7. DenseTensor Kernel Call{} - // 8. Reshard Output{} + // 1. InferSpmd (Infer DistAttr of Inputs&Outputs){} + // 2. Create API Output & Prepare Dist and Dense Output{} + // 3. Infer DistTensor's Global Shape{} + // 4. Select Kernel{} + // 5. Reshard Input{}\n + // 6. PrepareData (DataTransform & Prepare Dense Input){} + // 7. Infer Local DenseTensor Meta{} + // 8. DenseTensor Kernel Call{} // 9. Return {} }} @@ -60,20 +60,35 @@ # Auto Parallel condition AUTO_PARALLEL_COND_TEMPLATE = """AllInputsAreDistTensor({})""" -# 1. Create API Outputs +# 1. InferSPMD +SINGLE_DIST_META_IN_TEMPLATE = """ + auto meta_dist_{} = MakeDistMetaTensor(*{}.impl());""" +INFER_SPMD_TEMPLATE = """ + auto spmd_info = phi::distributed::{}({}); +""" + +# 2. Create API Outputs API_OUT_CREATION_TEMPLATE = """ {} api_output{}; """ INPLACE_API_OUT_CREATION_TEMPLATE = """ {} api_output{{{}}}; """ -SINGLE_OUT_CREATION_TEMPLATE = """ +SINGLE_OUT_CREATION_TEMPLATE_NO_SPMD = """ auto dist_out = SetKernelDistOutput(&api_output); - auto dense_out = const_cast(&dist_out->value()); + auto dense_out = dist_out->unsafe_mutable_value(); +""" +MULTI_SINGLE_OUT_CREATION_TEMPLATE_NO_SPMD = """ + auto dist_out_{idx} = SetKernelDistOutput({out}); + auto dense_out_{idx} = dist_out_{idx}->unsafe_mutable_value(); +""" +SINGLE_OUT_CREATION_TEMPLATE = """ + auto dist_out = SetKernelDistOutput(&api_output, spmd_info.second[0]); + auto dense_out = dist_out->unsafe_mutable_value(); """ MULTI_SINGLE_OUT_CREATION_TEMPLATE = """ - auto dist_out_{} = SetKernelDistOutput({}); - auto dense_out_{} = const_cast(&dist_out_{}->value()); + auto dist_out_{idx} = SetKernelDistOutput({out}, spmd_info.second[{idx}]); + auto dense_out_{idx} = dist_out_{idx}->unsafe_mutable_value(); """ VECTOR_OUT_CREATION_TEMPLATE = """ auto dist_out = SetKernelDistOutput({}, &api_output); @@ -93,12 +108,12 @@ TUPLE_OUT_CREATION_TEMPLATE = """ """ -# 2. InferSPMD -# Call InferMeta now, replace by InferSPMD function later -# TODO(chenweihang): InferSPMD function design -SINGLE_DIST_META_IN_TEMPLATE = """MakeMetaTensor(*{}.impl()), """ -VECTOR_DIST_META_IN_TEMPLATE = """{}_meta_ptr_vec, """ -VECTOR_DIST_META_IN_DECL_TEMPLATE = """ +# 3. Infer Global Shape +# TODO(chenweihang): the input MetaTensor created by Inferspmd can be reused +# for InferGlobalShape to avoid creating repeated inputs. +SINGLE_GLOBAL_META_IN_TEMPLATE = """MakeMetaTensor(*{}.impl()), """ +VECTOR_GLOBAL_META_IN_TEMPLATE = """{}_meta_ptr_vec, """ +VECTOR_GLOBAL_META_IN_DECL_TEMPLATE = """ std::vector {name}_meta_vec; for (auto tmp : {name}) {{ {name}_meta_vec.emplace_back(MakeMetaTensor(*tmp.impl())); @@ -109,11 +124,11 @@ }} """ # TODO(GhostScreaming): support optional args later -OPTIONAL_DIST_VECTOR_META_IN_TEMPLATE = """ +OPTIONAL_GLOBAL_VECTOR_META_IN_TEMPLATE = """ """ -SINGLE_DIST_META_OUT_DECL_TEMPLATE = """ +SINGLE_GLOBAL_META_OUT_DECL_TEMPLATE = """ phi::MetaTensor meta_{}({});""" -VECTOR_DIST_META_OUT_DECL_TEMPLATE = """ +VECTOR_GLOBAL_META_OUT_DECL_TEMPLATE = """ std::vector {name}_meta_vec; for (auto tmp : {name}) {{ {name}_meta_vec.emplace_back(phi::MetaTensor(tmp)); @@ -123,11 +138,11 @@ {name}_meta_ptr_vec[i] = &{name}_meta_vec[i]; }} """ -INFER_SPMD_TEMPLATE = """ +INFER_GLOBAL_SHAPE_TEMPLATE = """ phi::{}({}{}); """ -# 3. Select Kernel +# 4. Select Kernel KERNEL_SELECTION_TEMPLATE = """ VLOG(6) << "{} API dist branch: kernel key: [" << kernel_backend << ", " << kernel_layout << ", "<< kernel_data_type << "]"; auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError( @@ -137,14 +152,18 @@ auto* dev_ctx = GetDeviceContextByBackend(kernel_result.has_fallback_cpu ? Backend::CPU : kernel_backend); """ -# 4. Reshard Input -INPUT_RESHARD_TEMPLATE = """ -""" +# 5. Reshard Input +SINGLE_INPUT_RESHARD_TEMPLATE = """ + auto dist_input_{arg} = ReshardDistTensor(dev_ctx, {arg}, spmd_info.first[{idx}]);""" -# 5. PrepareData +# 6. PrepareData SINGLE_PREPARE_DATA_TEMPLATE = """ - auto dist_input_{} = PrepareDataForDistTensor({}, GetKernelInputArgDef(kernel.InputAt({}), kernel_backend), {}, kernel_result.is_stride_kernel); - auto input_{} = &dist_input_{}->value(); + dist_input_{arg} = PrepareDataForDistTensor(dist_input_{arg}, GetKernelInputArgDef(kernel.InputAt({idx}), kernel_backend), {flag}, kernel_result.is_stride_kernel); + auto input_{arg} = &dist_input_{arg}->value(); +""" +SINGLE_PREPARE_DATA_TEMPLATE_NO_RESHARD = """ + auto dist_input_{arg} = PrepareDataForDistTensor({arg}, GetKernelInputArgDef(kernel.InputAt({idx}), kernel_backend), {flag}, kernel_result.is_stride_kernel); + auto input_{arg} = &dist_input_{arg}->value(); """ VECTOR_PREPARE_DATA_TEMPLATE = """ auto dist_input_{name}_vec = PrepareDataForDistTensor({name}, GetKernelInputArgDef(kernel.InputAt({index}), kernel_backend), {trans_flag}, kernel_result.is_stride_kernel); @@ -170,7 +189,7 @@ const auto& input_{} = *input_{}_uq_ptr; """ -# 6. Infer Local DenseTensor Meta +# 7. Infer Local DenseTensor Meta SINGLE_META_IN_TEMPLATE = """MakeMetaTensor(*input_{}), """ # TODO(GhostScreaming): support optional args later VECTOR_META_IN_TEMPLATE = """dense_input_{}_meta_ptr_vec, """ @@ -189,7 +208,7 @@ phi::{}({}{}); """ -# 7. DenseTensor Kernel Call +# 8. DenseTensor Kernel Call # TODO(chenweihang): support kernel fallback later SINGLE_OUTPUT_NAME = """dense_out""" # TODO(chenweihang): support vector and tuple output later @@ -205,10 +224,6 @@ PREFIX_VECTOR_TENSOR_NAME = "dense_input_" SUFFIX_VECTOR_TENSOR_NAME = "_vec" -# 8. Reshard Output -OUTPUT_RESHARD_TEMPLATE = """ -""" - # BaseAPI members: # inputs: # names : [], list of input names @@ -252,6 +267,17 @@ def init_dist_api_members(self): self.inplace_flag = False self.dist_output_args = [] self.dense_output_args = [] + self.input_args_code = "" + + # override BaseAPI's method + def parse_infer_meta(self, infer_meta_config): + infer_meta = infer_meta_config + if 'param' not in infer_meta_config: + infer_meta['param'] = None + if 'spmd_rule' not in infer_meta_config: + infer_meta['spmd_rule'] = None + + return infer_meta def need_to_generate_code_for_inplace_impl(self, i): return ( @@ -289,6 +315,55 @@ def generate_if_condition_code(self) -> str: input_args = input_args[:-2] return AUTO_PARALLEL_COND_TEMPLATE.format(input_args) + def generate_infer_spmd_code(self) -> str: + if self.infer_meta['spmd_rule'] is not None: + input_names = self.inputs['names'] + attr_names = self.attrs['names'] + + infer_meta_params = ( + self.infer_meta['param'] + if self.infer_meta['param'] is not None + else input_names + attr_names + ) + input_decl_code = "" + self.input_args_code = "" + for param in infer_meta_params: + if param in input_names: + if self.inputs['input_info'][param] == "const Tensor&": + input_decl_code += SINGLE_DIST_META_IN_TEMPLATE.format( + param, param + ) + self.input_args_code += "meta_dist_" + param + ", " + else: + raise ValueError( + f"{self.api} : Param of infer_spmd error : {self.inputs['input_info'][param]} type is not supported." + ) + elif param in attr_names: + self.input_args_code = self.input_args_code + param + ", " + elif isinstance(param, str): + self.input_args_code = ( + self.input_args_code + "\"" + param + "\", " + ) + elif isinstance(param, bool): + self.input_args_code = ( + self.input_args_code + str(param).lower() + ", " + ) + else: + self.input_args_code = ( + self.input_args_code + str(param) + ", " + ) + + # TODO(chenweihang): add general spmd rule later + infer_spmd_code = "" + infer_spmd_func_code = self.infer_meta['spmd_rule'] + infer_spmd_code = INFER_SPMD_TEMPLATE.format( + infer_spmd_func_code, self.input_args_code[:-2] + ) + + return input_decl_code + infer_spmd_code + else: + return "" + def generate_output_creation_code(self) -> str: # forward api need to generate api and kernel outputs output_num = len(self.outputs['types']) @@ -311,7 +386,10 @@ def generate_output_creation_code(self) -> str: self.dist_output_args.append('dist_out') self.dense_output_args.append('dense_out') if self.outputs['types'][0] == 'Tensor': - output_creation_code += SINGLE_OUT_CREATION_TEMPLATE + if self.infer_meta['spmd_rule'] is not None: + output_creation_code += SINGLE_OUT_CREATION_TEMPLATE + else: + output_creation_code += SINGLE_OUT_CREATION_TEMPLATE_NO_SPMD elif self.outputs['types'][0] == 'std::vector': output_creation_code += VECTOR_OUT_CREATION_TEMPLATE.format( self.outputs['out_size_expr'][0] @@ -365,11 +443,18 @@ def generate_output_creation_code(self) -> str: ) ) else: - output_creation_code += ( - MULTI_SINGLE_OUT_CREATION_TEMPLATE.format( - i, get_out_code, i, i + if self.infer_meta['spmd_rule'] is not None: + output_creation_code += ( + MULTI_SINGLE_OUT_CREATION_TEMPLATE.format( + idx=i, out=get_out_code + ) + ) + else: + output_creation_code += ( + MULTI_SINGLE_OUT_CREATION_TEMPLATE_NO_SPMD.format( + idx=i, out=get_out_code + ) ) - ) else: raise ValueError( "{} : Output error: the output should not be empty.".format( @@ -379,10 +464,9 @@ def generate_output_creation_code(self) -> str: return output_creation_code - def generate_infer_spmd_code(self) -> str: + def generate_infer_global_shape_code(self) -> str: input_names = self.inputs['names'] attr_names = self.attrs['names'] - output_names = self.outputs['names'] # 1. get infer meta func name infer_meta = self.infer_meta @@ -399,18 +483,18 @@ def generate_infer_spmd_code(self) -> str: for param in infer_meta_params: if param in input_names: if self.inputs['input_info'][param] == "const Tensor&": - input_args_code += SINGLE_DIST_META_IN_TEMPLATE.format( + input_args_code += SINGLE_GLOBAL_META_IN_TEMPLATE.format( param ) elif ( self.inputs['input_info'][param] == "const std::vector&" ): - input_args_code += VECTOR_DIST_META_IN_TEMPLATE.format( + input_args_code += VECTOR_GLOBAL_META_IN_TEMPLATE.format( param ) - input_meta_code += VECTOR_DIST_META_IN_DECL_TEMPLATE.format( - name=param + input_meta_code += ( + VECTOR_GLOBAL_META_IN_DECL_TEMPLATE.format(name=param) ) else: raise ValueError( @@ -430,7 +514,7 @@ def generate_infer_spmd_code(self) -> str: output_args_code = "" for i, out_name in enumerate(self.dist_output_args): if self.outputs['types'][i] == 'std::vector': - output_decl_code += VECTOR_DIST_META_OUT_DECL_TEMPLATE.format( + output_decl_code += VECTOR_GLOBAL_META_OUT_DECL_TEMPLATE.format( name=out_name ) if len(self.dense_output_args) == 1: @@ -440,7 +524,7 @@ def generate_infer_spmd_code(self) -> str: f"{out_name} ? {out_name}_meta_ptr_vec : nullptr, " ) else: - output_decl_code += SINGLE_DIST_META_OUT_DECL_TEMPLATE.format( + output_decl_code += SINGLE_GLOBAL_META_OUT_DECL_TEMPLATE.format( out_name, out_name ) if len(self.dense_output_args) == 1: @@ -451,10 +535,12 @@ def generate_infer_spmd_code(self) -> str: ) output_args_code = output_args_code[:-2] + if self.input_args_code != "": + input_args_code = self.input_args_code return ( output_decl_code + input_meta_code - + INFER_SPMD_TEMPLATE.format( + + INFER_GLOBAL_SHAPE_TEMPLATE.format( infer_meta_func_code, input_args_code, output_args_code ) ) @@ -465,7 +551,35 @@ def generate_kernel_selection_code(self) -> str: ) def generate_reshard_input_code(self) -> str: - return INPUT_RESHARD_TEMPLATE.format() + input_reshard_code = "" + if self.infer_meta['spmd_rule'] is not None: + input_names = self.inputs['names'] + + infer_meta = self.infer_meta + infer_meta_params = ( + infer_meta['param'] + if infer_meta['param'] is not None + else input_names + ) + for i, param in enumerate(infer_meta_params): + if param in input_names: + if self.inputs['input_info'][param] == "const Tensor&": + input_reshard_code += ( + SINGLE_INPUT_RESHARD_TEMPLATE.format( + arg=param, idx=i + ) + ) + else: + raise ValueError( + f"{self.api} : Param of reshard input error : {self.inputs['input_info'][param]} type is not supported." + ) + else: + # do nothing + pass + else: + # do nothingd + pass + return input_reshard_code def generate_single_dense_input( self, @@ -479,14 +593,18 @@ def generate_single_dense_input( if kernel_param is None: kernel_param = input_names + attr_names - input_tensor_code += SINGLE_PREPARE_DATA_TEMPLATE.format( - input_name, - input_name, - kernel_param.index(input_name), - trans_flag, - input_name, - input_name, - ) + if self.infer_meta['spmd_rule'] is not None: + input_tensor_code += SINGLE_PREPARE_DATA_TEMPLATE.format( + arg=input_name, + idx=kernel_param.index(input_name), + flag=trans_flag, + ) + else: + input_tensor_code += SINGLE_PREPARE_DATA_TEMPLATE_NO_RESHARD.format( + arg=input_name, + idx=kernel_param.index(input_name), + flag=trans_flag, + ) return input_tensor_code @@ -706,9 +824,6 @@ def generate_kernel_call_code(self) -> str: ", ".join(self.dense_output_args), ) - def generate_reshard_output_code(self) -> str: - return OUTPUT_RESHARD_TEMPLATE.format() - def generate_return_code(self) -> str: return self.gene_return_code() @@ -718,14 +833,14 @@ def generate_auto_paralel_branch(self) -> str: return "" return MAIN_DIST_BRANCH_TEMPLATE.format( self.generate_if_condition_code(), - self.generate_output_creation_code(), self.generate_infer_spmd_code(), + self.generate_output_creation_code(), + self.generate_infer_global_shape_code(), self.generate_kernel_selection_code(), self.generate_reshard_input_code(), self.generate_prepare_data_code(), self.generate_infer_meta_code(), self.generate_kernel_call_code(), - self.generate_reshard_output_code(), self.generate_return_code(), ) @@ -777,11 +892,14 @@ def gene_base_api_code(self, inplace_flag=False): # 3. doesn't support view api # 4. only for general forward and backward # 5. only support single tensor input and output + # 6. doesn't support double grad and triple grad dist_branch_code = "" if ( len(self.inputs['names']) > 0 and len(self.view_map) == 0 and self.check_argument_whether_support_auto_parallel() + and not self.api.endswith("_double_grad") + and not self.api.endswith("_triple_grad") ): dist_branch_code = self.generate_auto_paralel_branch() return API_IMPL_TEMPLATE.format( diff --git a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py index 487d6e3a25720..25944e3356966 100644 --- a/paddle/phi/api/yaml/generator/dist_bw_api_gen.py +++ b/paddle/phi/api/yaml/generator/dist_bw_api_gen.py @@ -22,10 +22,24 @@ # Code Gen Templates # ###################### +MAIN_DIST_BRANCH_TEMPLATE = """ + // Auto Parallel condition + if ({}) {{ + // 1. Create API Output & Prepare Dist and Dense Output{} + // 2. Infer DistTensor's Global Shape{} + // 3. Select Kernel{} + // 4. PrepareData (DataTransform & Prepare Dense Input){} + // 5. Infer Local DenseTensor Meta{} + // 6. DenseTensor Kernel Call{} + // 7. Return + {} + }} +""" + # 1. Create API Outputs SINGLE_OUT_CREATION_TEMPLATE = """ auto dist_out = SetKernelDistOutput({}); - auto dense_out = const_cast(&dist_out->value()); + auto dense_out = dist_out->unsafe_mutable_value(); """ VECTOR_OUT_CREATION_TEMPLATE = """ auto dist_out = SetKernelDistOutput({name}); @@ -39,7 +53,21 @@ """ MULTI_SINGLE_OUT_CREATION_TEMPLATE = """ auto dist_out_{} = SetKernelDistOutput({}); - auto dense_out_{} = const_cast(&dist_out_{}->value()); + auto dense_out_{} = dist_out_{}->unsafe_mutable_value(); +""" + +# 2. Infer Global Shape +SINGLE_DIST_META_IN_TEMPLATE = """MakeDistMetaTensor(*{}.impl()), """ +SINGLE_DIST_META_OUT_DECL_TEMPLATE = """ + phi::distributed::DistMetaTensor meta_{}({});""" +INFER_GLOBAL_SHAPE_TEMPLATE = """ + phi::{}({}{}); +""" + +# 4. PrepareData (DataTransform & Prepare Dist and Dense Input) +SINGLE_PREPARE_DATA_TEMPLATE = """ + auto dist_input_{arg} = PrepareDataForDistTensor({arg}, GetKernelInputArgDef(kernel.InputAt({idx}), kernel_backend), {flag}, kernel_result.is_stride_kernel); + auto input_{arg} = &dist_input_{}->value(); """ @@ -131,6 +159,21 @@ def gene_return_code(self): def gene_api_declaration(self) -> str: return BackwardAPI.gene_api_declaration(self) + def generate_auto_paralel_branch(self) -> str: + # if no tensor input, do not genetate auto parallel branch + if len(self.inputs['names']) == 0: + return "" + return MAIN_DIST_BRANCH_TEMPLATE.format( + self.generate_if_condition_code(), + self.generate_output_creation_code(), + self.generate_infer_global_shape_code(), + self.generate_kernel_selection_code(), + self.generate_prepare_data_code(), + self.generate_infer_meta_code(), + self.generate_kernel_call_code(), + self.generate_return_code(), + ) + def header_include(): return """ diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index fcf4b2f28bb1e..4c151374c6893 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -651,6 +651,7 @@ output : Tensor infer_meta : func : MatmulInferMeta + spmd_rule : MatmulSpmdInferForward kernel : func : matmul backward : matmul_grad diff --git a/paddle/phi/core/distributed/auto_parallel/dist_tensor.h b/paddle/phi/core/distributed/auto_parallel/dist_tensor.h index 7af036a9268a8..bc8b98d81a3ff 100644 --- a/paddle/phi/core/distributed/auto_parallel/dist_tensor.h +++ b/paddle/phi/core/distributed/auto_parallel/dist_tensor.h @@ -66,6 +66,15 @@ class DistTensor final /// \return The DenseTensor value's const reference const DenseTensor& value() const { return value_; } + /// \brief Returns the mutable dense tensor value in dist tensor. + /// \note If DenseTensor value is modified externally, the corresponding + /// relationship between it and the current tensor's global dims and + /// dist attr may be destroyed, which may introduce some subtle bugs, + /// so you need to make sure to consider it thoroughly when using + /// this method. + /// \return The mutable pointer of DenseTensor value + DenseTensor* unsafe_mutable_value() { return &value_; } + /// \brief Returns the global dims of the dist tensor. /// \return The global dims of the dist tensor. const DDim& local_dims() const; diff --git a/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc index 3b94dc017e5e7..531727b3ee8d1 100644 --- a/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc +++ b/paddle/phi/core/distributed/auto_parallel/inferspmd_utils.cc @@ -73,11 +73,6 @@ bool SpmdRuleFactory::ContainsSpmdRule(const std::string& kernel_name) const { } int SpmdRuleFactory::InsertSpmdRule(std::string kernel_name, SpmdRule rule) { - PADDLE_ENFORCE_NE( - ContainsSpmdRule(kernel_name), - true, - phi::errors::AlreadyExists( - "`%s` Kernel's Spmd rules has been registered.", kernel_name)); spmd_rule_map_.insert({std::move(kernel_name), std::move(rule)}); return 0; } diff --git a/paddle/phi/infermeta/spmd_rules/rules.h b/paddle/phi/infermeta/spmd_rules/rules.h index ad519ff287a33..5ec2f212ec65b 100644 --- a/paddle/phi/infermeta/spmd_rules/rules.h +++ b/paddle/phi/infermeta/spmd_rules/rules.h @@ -27,12 +27,12 @@ limitations under the License. */ * 2. Since the infer functions of Spmd forward and backward are closely related * and need to be registered together, we manage them together in one file. * - * 3. SPMD rules are much smaller than infermeta function, and we manage files - * in operator units. + * 3. SPMD rules are less than infermeta function, and we manage files by + * operator. * * 4. The previous registration used some compile-time regular matching methods, * which was less flexible, and the registration of SPMD rules here is declare - * directly in the header file + * directly in the header file. */ namespace phi { diff --git a/test/auto_parallel/CMakeLists.txt b/test/auto_parallel/CMakeLists.txt index aeb00a0fc7296..eae16e0245468 100644 --- a/test/auto_parallel/CMakeLists.txt +++ b/test/auto_parallel/CMakeLists.txt @@ -88,6 +88,10 @@ if(WITH_DISTRIBUTE AND WITH_GPU) py_test_modules(test_reshard_r_to_p MODULES test_reshard_r_to_p) set_tests_properties(test_reshard_r_to_p PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + py_test_modules(test_semi_auto_parallel_basic MODULES + test_semi_auto_parallel_basic) + set_tests_properties(test_semi_auto_parallel_basic + PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) # End of unittests WITH multi cards and timeout # NOTE(zyl): unittests WITH multi cards and WITHOUT timeout diff --git a/test/auto_parallel/semi_auto_parallel_for_matmul.py b/test/auto_parallel/semi_auto_parallel_for_matmul.py new file mode 100644 index 0000000000000..953c734e6dc21 --- /dev/null +++ b/test/auto_parallel/semi_auto_parallel_for_matmul.py @@ -0,0 +1,84 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import os + +import numpy as np + +import paddle +import paddle.distributed as dist + + +class TestMatmulApiForSemiAutoParallel: + def __init__(self): + self._dtype = os.getenv("dtype") + self._seeds = eval(os.getenv("seeds")) + self._backend = os.getenv("backend") + self._mesh = dist.ProcessMesh([0, 1], dim_names=["x"]) + + def test_body(self, x_specs, y_specs): + x_shape = [64, 32] + y_shape = [32, 48] + x = paddle.randn(x_shape, self._dtype) + y = paddle.randn(y_shape, self._dtype) + + x_dist_attr = dist.DistAttr(mesh=self._mesh, sharding_specs=x_specs) + y_dist_attr = dist.DistAttr(mesh=self._mesh, sharding_specs=y_specs) + + dist_x = dist.shard_tensor(x, dist_attr=x_dist_attr) + dist_y = dist.shard_tensor(y, dist_attr=y_dist_attr) + + dist_out = paddle.matmul(dist_x, dist_y) + + # verify global shape + out_shape = [64, 48] + np.testing.assert_equal(dist_out.shape, out_shape, verbose=True) + + return dist_out + + def test_case1(self): + # case1: mk[0,-1],kn[-1,-1] -> mk[0,-1],kn[-1,-1] = mn[0,-1] partial[] + dist_out = self.test_body(x_specs=['x', None], y_specs=[None, None]) + # verify local shape and dist attr + np.testing.assert_equal(dist_out._local_shape, [32, 48], verbose=True) + np.testing.assert_equal( + dist_out.dist_attr.dims_mapping, [0, -1], verbose=True + ) + assert dist_out.dist_attr._is_partial() is False + + def test_case2(self): + # case2: mk[-1, 0],kn[-1,-1] --> mk[-1, 0],kn[0, -1] = nm[-1, -1] partial[0] + dist_out = self.test_body(x_specs=[None, 'x'], y_specs=[None, None]) + # verify local shape + np.testing.assert_equal(dist_out._local_shape, [64, 48], verbose=True) + np.testing.assert_equal( + dist_out.dist_attr.dims_mapping, [-1, -1], verbose=True + ) + assert dist_out.dist_attr._is_partial() is True + assert dist_out.dist_attr._partial_dims() == {0} + + def run_test_case(self): + if self._backend == "cpu": + paddle.set_device("cpu") + elif self._backend == "gpu": + paddle.set_device("gpu:" + str(dist.get_rank())) + else: + raise ValueError("Only support cpu or gpu backend.") + + self.test_case1() + self.test_case2() + + +if __name__ == '__main__': + TestMatmulApiForSemiAutoParallel().run_test_case() diff --git a/test/auto_parallel/test_semi_auto_parallel_basic.py b/test/auto_parallel/test_semi_auto_parallel_basic.py new file mode 100644 index 0000000000000..a1ec1b18e9b33 --- /dev/null +++ b/test/auto_parallel/test_semi_auto_parallel_basic.py @@ -0,0 +1,41 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import unittest + +import collective.test_communication_api_base as test_base + + +class TestSemiAutoParallelMatmul(test_base.CommunicationTestDistBase): + def setUp(self): + super().setUp(num_of_devices=2, timeout=120) + self._default_envs = { + "dtype": "float32", + "seeds": str(self._seeds), + } + self._changeable_envs = {"backend": ["cpu", "gpu"]} + + def test_matmul_api(self): + envs_list = test_base.gen_product_envs_list( + self._default_envs, self._changeable_envs + ) + for envs in envs_list: + self.run_test_case( + "semi_auto_parallel_for_matmul.py", + user_defined_envs=envs, + ) + + +if __name__ == "__main__": + unittest.main() From c958ba740806a5a07e70851e3d778c4a88207f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=A3=9E=E7=8C=AA?= <106524776+ooooo-create@users.noreply.github.com> Date: Wed, 6 Sep 2023 20:54:01 +0800 Subject: [PATCH 163/194] [xdoctest][task 248-249,266-267,269] reformat example code with google style in `incubate/distributed/fleet/*`,`incubate/nn/layer/*` (#56772) * [Doctest]fix No.248-249,266-267,269, test=docs_preview * fix style * fix * add env:DISTRIBUTED --- .../incubate/distributed/fleet/fleet_util.py | 642 ++++++++++-------- .../parameter_server/ir/ps_dispatcher.py | 20 +- .../paddle/incubate/nn/layer/fused_ec_moe.py | 18 +- .../paddle/incubate/nn/layer/fused_linear.py | 16 +- python/paddle/incubate/nn/layer/io.py | 38 +- 5 files changed, 398 insertions(+), 336 deletions(-) diff --git a/python/paddle/incubate/distributed/fleet/fleet_util.py b/python/paddle/incubate/distributed/fleet/fleet_util.py index f809a536ca241..34dc040c45ab3 100644 --- a/python/paddle/incubate/distributed/fleet/fleet_util.py +++ b/python/paddle/incubate/distributed/fleet/fleet_util.py @@ -46,9 +46,10 @@ class FleetUtil: Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.rank0_print("my log") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.rank0_print("my log") """ @@ -81,9 +82,10 @@ def rank0_print(self, s): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.rank0_print("my log") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.rank0_print("my log") """ if fleet.worker_index() != 0: @@ -101,9 +103,10 @@ def rank0_info(self, s): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.rank0_info("my log info") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.rank0_info("my log info") """ if fleet.worker_index() != 0: @@ -120,9 +123,10 @@ def rank0_error(self, s): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.rank0_error("my log error") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.rank0_error("my log error") """ if fleet.worker_index() != 0: @@ -148,9 +152,11 @@ def set_zero( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.set_zero(myvar.name, myscope) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.set_zero(myvar.name, myscope) """ param = scope.var(var_name).get_tensor() @@ -176,23 +182,27 @@ def print_global_auc( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.print_global_auc(myscope, stat_pos=stat_pos.name, - stat_neg=stat_neg.name) - - # below is part of model - emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - similarity_norm = fluid.layers.sigmoid(paddle.clip(\ - emb, min=-15.0, max=15.0), name="similarity_norm")\ - binary_predict = fluid.layers.concat(input=[\ - paddle.subtract(\ - fluid.layers.ceil(similarity_norm), similarity_norm),\ - similarity_norm], axis=1) - auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ - stat_neg] = paddle.static.auc(input=binary_predict,\ - label=label, curve='ROC',\ - num_thresholds=4096) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.print_global_auc(myscope, stat_pos=stat_pos.name, + ... stat_neg=stat_neg.name) + + >>> # below is part of model + >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 + >>> similarity_norm = fluid.layers.sigmoid(paddle.clip( + ... emb, min=-15.0, max=15.0), name="similarity_norm") + >>> binary_predict = fluid.layers.concat(input=[ + ... paddle.subtract( + ... fluid.layers.ceil(similarity_norm), + ... similarity_norm), + ... similarity_norm], + ... axis=1) + >>> auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, + ... stat_neg] = paddle.static.auc(input=binary_predict, + ... label=label,curve='ROC', + ... num_thresholds=4096) """ auc_value = self.get_global_auc(scope, stat_pos, stat_neg) @@ -218,11 +228,13 @@ def get_global_auc( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - auc_value, _ = fleet_util.get_global_auc(myscope, - stat_pos=stat_pos, - stat_neg=stat_neg) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> auc_value, _ = fleet_util.get_global_auc(myscope, + ... stat_pos=stat_pos, + ... stat_neg=stat_neg) """ if scope.find_var(stat_pos) is None or scope.find_var(stat_neg) is None: @@ -288,9 +300,10 @@ def load_fleet_model_one_table(self, table_id, path): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.load_fleet_model("hdfs:/my/model/path", table_id=1) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.load_fleet_model_one_table(1, path="hdfs:/my/model/path") """ fleet.load_one_table(table_id, path) @@ -306,12 +319,13 @@ def load_fleet_model(self, path, mode=0): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() - fleet_util.load_fleet_model("hdfs:/my/model/path") + >>> fleet_util.load_fleet_model("hdfs:/my/model/path") - fleet_util.load_fleet_model("hdfs:/my/model/path", mode=0) + >>> fleet_util.load_fleet_model("hdfs:/my/model/path", mode=0) """ fleet.init_server(path, mode=mode) @@ -328,9 +342,10 @@ def save_fleet_model(self, path, mode=0): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_fleet_model("hdfs:/my/model/path") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_fleet_model("hdfs:/my/model/path") """ fleet.save_persistables(None, path, mode=mode) @@ -406,15 +421,15 @@ def write_model_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.write_model_donefile(output_path="hdfs:/my/output", - model_path="hdfs:/my/model", - day=20190723, - pass_id=66, - xbox_base_key=int(time.time()), - hadoop_fs_name="hdfs://xxx", - hadoop_fs_ugi="user,passwd") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.write_model_donefile(output_path="hdfs:/my/output", + ... day=20190723, + ... pass_id=66, + ... xbox_base_key=int(time.time()), + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd") """ day = str(day) @@ -508,19 +523,18 @@ def write_xbox_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.write_xbox_donefile( - output_path="hdfs:/my/output/", - model_path="hdfs:/my/output/20190722/01", - day=20190722, - pass_id=1, - xbox_base_key=int(time.time()), - data_path="hdfs:/my/data/", - hadoop_fs_name="hdfs://xxx", - hadoop_fs_ugi="user,passwd", - monitor_data={} - ) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.write_xbox_donefile( + ... output_path="hdfs:/my/output/", + ... day=20190722, + ... pass_id=1, + ... xbox_base_key=int(time.time()), + ... data_path="hdfs:/my/data/", + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd", + ... monitor_data={}) """ day = str(day) @@ -627,16 +641,16 @@ def write_cache_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.write_cache_donefile( - output_path="hdfs:/my/output/", - day=20190722, - pass_id=1, - key_num=123456, - hadoop_fs_name="hdfs://xxx", - hadoop_fs_ugi="user,passwd", - ) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.write_cache_donefile( + ... output_path="hdfs:/my/output/", + ... day=20190722, + ... pass_id=1, + ... key_num=123456, + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd") """ day = str(day) @@ -686,9 +700,10 @@ def load_model(self, output_path, day, pass_id): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.load_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.load_model("hdfs:/my/path", 20190722, 88) """ day = str(day) @@ -711,9 +726,10 @@ def save_model(self, output_path, day, pass_id): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_model("hdfs:/my/path", 20190722, 88) """ day = str(day) @@ -735,9 +751,10 @@ def save_batch_model(self, output_path, day): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_batch_model("hdfs:/my/path", 20190722) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_batch_model("hdfs:/my/path", 20190722) """ day = str(day) @@ -759,9 +776,10 @@ def save_delta_model(self, output_path, day, pass_id): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_batch_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_delta_model("hdfs:/my/path", 20190722, 88) """ day = str(day) @@ -783,9 +801,10 @@ def save_xbox_base_model(self, output_path, day): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_xbox_base_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_xbox_base_model("hdfs:/my/path", 20190722) """ day = str(day) @@ -813,9 +832,10 @@ def save_cache_model(self, output_path, day, pass_id, mode=1, **kwargs): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_cache_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_cache_model("hdfs:/my/path", 20190722, 88) """ day = str(day) @@ -848,9 +868,10 @@ def save_cache_base_model(self, output_path, day, **kwargs): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_cache_base_model("hdfs:/my/path", 20190722) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_cache_base_model("hdfs:/my/path", 20190722) """ day = str(day) @@ -875,9 +896,11 @@ def pull_all_dense_params(self, scope, program): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.pull_all_dense_params(my_scope, my_program) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.pull_all_dense_params(my_scope, my_program) """ fleet._role_maker._barrier_worker() @@ -950,18 +973,20 @@ def save_paddle_inference_model( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_paddle_inference_model(exe, - join_scope, - join_program, - feeded_vars, - target_vars, - "hdfs:/my/output/path/", - day=20190727, - pass_id=6, - hadoop_fs_name="xxx", - hadoop_fs_ugi="xxx,xxx") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_paddle_inference_model(exe, + ... join_scope, + ... join_program, + ... feeded_vars, + ... target_vars, + ... "hdfs:/my/output/path/", + ... day=20190727, + ... pass_id=6, + ... hadoop_fs_name="xxx", + ... hadoop_fs_ugi="xxx,xxx") """ day = str(day) pass_id = str(pass_id) @@ -1044,38 +1069,40 @@ def save_paddle_params( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.save_paddle_params(exe, - join_scope, - join_program, - "paddle_dense.model.0", - "hdfs:/my/output/path/", - day=20190727, - pass_id=6, - hadoop_fs_name="xxx", - hadoop_fs_ugi="xxx,xxx", - var_names=join_all_var_names) - fleet_util.save_paddle_params(exe, - join_scope, - join_program, - "paddle_dense.model.usr.0", - "hdfs:/my/output/path/", - day=20190727, - pass_id=6, - hadoop_fs_name="xxx", - hadoop_fs_ugi="xxx,xxx", - var_names=join_user_var_names) - fleet_util.save_paddle_params(exe, - join_scope, - join_program, - "paddle_dense.model.item.0", - "hdfs:/my/output/path/", - day=20190727, - pass_id=6, - hadoop_fs_name="xxx", - hadoop_fs_ugi="xxx,xxx", - var_names=join_user_item_names) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.save_paddle_params(exe, + ... join_scope, + ... join_program, + ... "paddle_dense.model.0", + ... "hdfs:/my/output/path/", + ... day=20190727, + ... pass_id=6, + ... hadoop_fs_name="xxx", + ... hadoop_fs_ugi="xxx,xxx", + ... var_names=join_all_var_names) + >>> fleet_util.save_paddle_params(exe, + ... join_scope, + ... join_program, + ... "paddle_dense.model.usr.0", + ... "hdfs:/my/output/path/", + ... day=20190727, + ... pass_id=6, + ... hadoop_fs_name="xxx", + ... hadoop_fs_ugi="xxx,xxx", + ... var_names=join_user_var_names) + >>> fleet_util.save_paddle_params(exe, + ... join_scope, + ... join_program, + ... "paddle_dense.model.item.0", + ... "hdfs:/my/output/path/", + ... day=20190727, + ... pass_id=6, + ... hadoop_fs_name="xxx", + ... hadoop_fs_ugi="xxx,xxx", + ... var_names=join_user_item_names) """ day = str(day) @@ -1139,11 +1166,13 @@ def get_last_save_xbox_base( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - last_save_day, last_path, xbox_base_key = \ - fleet_util.get_last_save_xbox_base("hdfs:/my/path", 20190722, - 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> last_save_day, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_xbox_base("hdfs:/my/path", + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd") """ donefile_path = output_path + "/xbox_base_done.txt" @@ -1187,10 +1216,13 @@ def get_last_save_xbox( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - last_save_day, last_save_pass, last_path, xbox_base_key = \ - fleet_util.get_last_save_xbox("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> last_save_day, last_save_pass, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_xbox("hdfs:/my/path", + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd") """ donefile_path = output_path + "/xbox_patch_done.txt" @@ -1235,10 +1267,13 @@ def get_last_save_model( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - last_save_day, last_save_pass, last_path, xbox_base_key = \ - fleet_util.get_last_save_model("hdfs:/my/path", 20190722, 88) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> last_save_day, last_save_pass, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_model("hdfs:/my/path", + ... hadoop_fs_name="hdfs://xxx", + ... hadoop_fs_ugi="user,passwd") """ last_save_day = -1 @@ -1279,14 +1314,15 @@ def get_online_pass_interval( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - online_pass_interval = fleet_util.get_online_pass_interval( - days="{20190720..20190729}", - hours="{0..23}", - split_interval=5, - split_per_pass=2, - is_data_hourly_placed=False) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> online_pass_interval = fleet_util.get_online_pass_interval( + ... days="{20190720..20190729}", + ... hours="{0..23}", + ... split_interval=5, + ... split_per_pass=2, + ... is_data_hourly_placed=False) """ days = os.popen("echo -n " + days).read().split(" ") @@ -1358,35 +1394,37 @@ def get_global_metrics( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - metric_list = fleet_util.get_global_metrics(myscope, - stat_pos.name, - stat_neg.name, - local_sqrerr.name, - local_abserr.name, - local_prob.name, - local_q.name, - local_pos_ins.name, - local_total_ins.name) - - # below is part of example model - label = paddle.static.data(name="click", shape=[-1, 1],\ - dtype="int64", lod_level=0) - emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - similarity_norm = fluid.layers.sigmoid(paddle.clip(\ - emb, min=-15.0, max=15.0), name="similarity_norm")\ - binary_predict = fluid.layers.concat(input=[\ - paddle.subtract(\ - fluid.layers.ceil(similarity_norm), similarity_norm),\ - similarity_norm], axis=1) - auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ - stat_neg] = paddle.static.auc(input=binary_predict,\ - label=label, curve='ROC',\ - num_thresholds=4096) - local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins,\ - local_total_ins = paddle.static.ctr_metric_bundle(\ - similarity_norm, label) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> metric_list = fleet_util.get_global_metrics(myscope, + ... stat_pos.name, + ... stat_neg.name, + ... local_sqrerr.name, + ... local_abserr.name, + ... local_prob.name, + ... local_q.name, + ... local_pos_ins.name, + ... local_total_ins.name) + + >>> # below is part of example model + >>> label = paddle.static.data(name="click", shape=[-1, 1],\ + ... dtype="int64", lod_level=0) + >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 + >>> similarity_norm = fluid.layers.sigmoid(paddle.clip(\ + ... emb, min=-15.0, max=15.0), name="similarity_norm")\ + >>> binary_predict = fluid.layers.concat(input=[\ + ... paddle.subtract(\ + ... fluid.layers.ceil(similarity_norm), similarity_norm),\ + ... similarity_norm], axis=1) + >>> auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ + ... stat_neg] = paddle.static.auc(input=binary_predict,\ + ... label=label, curve='ROC',\ + ... num_thresholds=4096) + >>> local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins,\ + ... local_total_ins = paddle.static.ctr_metric_bundle(\ + ... similarity_norm, label) """ if ( @@ -1558,35 +1596,37 @@ def print_global_metrics( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - fleet_util.print_global_metrics(myscope, - stat_pos.name, - stat_neg.name, - local_sqrerr.name, - local_abserr.name, - local_prob.name, - local_q.name, - local_pos_ins.name, - local_total_ins.name) - - # below is part of model - label = paddle.static.data(name="click", shape=[-1, 1],\ - dtype="int64", lod_level=0) - emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - similarity_norm = fluid.layers.sigmoid(paddle.clip(\ - emb, min=-15.0, max=15.0), name="similarity_norm")\ - binary_predict = fluid.layers.concat(input=[\ - paddle.subtract(\ - fluid.layers.ceil(similarity_norm), similarity_norm),\ - similarity_norm], axis=1) - auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ - stat_neg] = paddle.static.auc(input=binary_predict,\ - label=label, curve='ROC',\ - num_thresholds=4096) - local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins, \ - local_total_ins = paddle.static.ctr_metric_bundle(\ - similarity_norm, label) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> # doctest: +SKIP('dependency on custom variables') + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> fleet_util.print_global_metrics(myscope, + ... stat_pos.name, + ... stat_neg.name, + ... local_sqrerr.name, + ... local_abserr.name, + ... local_prob.name, + ... local_q.name, + ... local_pos_ins.name, + ... local_total_ins.name) + + >>> # below is part of model + >>> label = paddle.static.data(name="click", shape=[-1, 1],\ + ... dtype="int64", lod_level=0) + >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 + >>> similarity_norm = fluid.layers.sigmoid(paddle.clip(\ + ... emb, min=-15.0, max=15.0), name="similarity_norm")\ + >>> binary_predict = fluid.layers.concat(input=[\ + ... paddle.subtract(\ + ... fluid.layers.ceil(similarity_norm), similarity_norm),\ + ... similarity_norm], axis=1) + >>> auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ + ... stat_neg] = paddle.static.auc(input=binary_predict,\ + ... label=label, curve='ROC',\ + ... num_thresholds=4096) + >>> local_sqrerr, local_abserr, local_prob, local_q, local_pos_ins, \ + ... local_total_ins = paddle.static.ctr_metric_bundle(\ + ... similarity_norm, label) """ if ( @@ -1722,12 +1762,13 @@ def parse_program_proto(self, prog_path, is_text, output_dir): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import FleetUtil - fleet_util = FleetUtil() - program_path = "./program.pbtxt" - is_text = True - output_dir = "/tmp/" - fleet_util.parse_program_proto(program_path, is_text, output_dir) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import FleetUtil + >>> fleet_util = FleetUtil() + >>> program_path = "./program.pbtxt" + >>> is_text = True + >>> output_dir = "/tmp/" + >>> fleet_util.parse_program_proto(program_path, is_text, output_dir) """ program = self.load_program(prog_path, is_text) utils.parse_program(program, output_dir) @@ -1740,9 +1781,10 @@ class GPUPSUtil(FleetUtil): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - fleet_util = GPUPSUtil() - fleet_util.rank0_print("my log") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> fleet_util = GPUPSUtil() + >>> fleet_util.rank0_print("my log") """ def __init__(self, fs_client=None): @@ -1766,9 +1808,10 @@ def init(self, fs_name, fs_user, fs_passwd, fs_conf): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - fleet_util = GPUPSUtil() - fleet_util.init(20190722, 88, 88, "./afs.conf") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> fleet_util = GPUPSUtil() + >>> fleet_util.init(20190722, 88, 88, "./afs.conf") """ self._afs.init(fs_name, fs_user, fs_passwd, fs_conf) @@ -1785,11 +1828,12 @@ def set_fsclient(self, fs_client): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) """ self._afs = fs_client @@ -1809,13 +1853,14 @@ def get_last_save_xbox_base(self, output_path): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - last_save_day, last_path, xbox_base_key = \ - fleet_util.get_last_save_xbox_base("hdfs:/my/path") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> last_save_day, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_xbox_base("hdfs:/my/path") """ donefile_path = output_path + "/xbox_base_done.txt" @@ -1851,13 +1896,14 @@ def get_last_save_xbox(self, output_path): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - last_save_day, last_save_pass, last_path, xbox_base_key = \ - fleet_util.get_last_save_xbox("hdfs:/my/path") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> last_save_day, last_save_pass, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_xbox("hdfs:/my/path") """ donefile_path = output_path + "/xbox_patch_done.txt" @@ -1894,13 +1940,14 @@ def get_last_save_model(self, output_path): Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - last_save_day, last_save_pass, last_path, xbox_base_key = \ - fleet_util.get_last_save_model("hdfs:/my/path") + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> last_save_day, last_save_pass, last_path, xbox_base_key = \ + ... fleet_util.get_last_save_model("hdfs:/my/path") """ last_save_day = -1 @@ -1942,16 +1989,16 @@ def write_model_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - fleet_util.write_model_donefile(output_path="hdfs:/my/output", - model_path="hdfs:/my/model", - day=20190723, - pass_id=66, - xbox_base_key=int(time.time())) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> fleet_util.write_model_donefile(output_path="hdfs:/my/output", + ... day=20190723, + ... pass_id=66, + ... xbox_base_key=int(time.time())) """ day = str(day) @@ -2041,19 +2088,19 @@ def write_xbox_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - fleet_util.write_xbox_donefile( - output_path="hdfs:/my/output/", - model_path="hdfs:/my/output/20190722/01", - day=20190722, - pass_id=1, - xbox_base_key=int(time.time()), - data_path="hdfs:/my/data/", - monitor_data={}) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> fleet_util.write_xbox_donefile( + ... output_path="hdfs:/my/output/", + ... day=20190722, + ... pass_id=1, + ... xbox_base_key=int(time.time()), + ... data_path="hdfs:/my/data/", + ... monitor_data={}) """ day = str(day) @@ -2154,16 +2201,17 @@ def write_cache_donefile( Examples: .. code-block:: python - from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil - from paddle.distributed.fleet.utils.fs import AFSClient - hdfs_client = AFSClient() - fleet_util = GPUPSUtil() - fleet_util.set_fsclient(hdfs_client) - fleet_util.write_cache_donefile( - output_path="hdfs:/my/output/", - day=20190722, - pass_id=1, - key_num=123456) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.distributed.fleet.fleet_util import GPUPSUtil + >>> from paddle.distributed.fleet.utils.fs import AFSClient + >>> hdfs_client = AFSClient() + >>> fleet_util = GPUPSUtil() + >>> fleet_util.set_fsclient(hdfs_client) + >>> fleet_util.write_cache_donefile( + ... output_path="hdfs:/my/output/", + ... day=20190722, + ... pass_id=1, + ... key_num=123456) """ day = str(day) diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/ir/ps_dispatcher.py b/python/paddle/incubate/distributed/fleet/parameter_server/ir/ps_dispatcher.py index 4155413cb59f9..29f7fda393e7a 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/ir/ps_dispatcher.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/ir/ps_dispatcher.py @@ -55,11 +55,13 @@ class HashName(PSDispatcher): Examples: .. code-block:: python - pserver_endpoints = ["127.0.0.1:6007", "127.0.0.1:6008"] - vars = ["var1","var2","var3","var4","var5"] + >>> from paddle.incubate.distributed.fleet.parameter_server.ir.ps_dispatcher import RoundRobin - rr = RoundRobin(pserver_endpoints) - rr.dispatch(vars) + >>> pserver_endpoints = ["127.0.0.1:6007", "127.0.0.1:6008"] + >>> vars = ["var1","var2","var3","var4","var5"] + + >>> rr = HashName(pserver_endpoints) + >>> rr.dispatch(vars) """ @@ -95,11 +97,13 @@ class RoundRobin(PSDispatcher): Examples: .. code-block:: python - pserver_endpoints = ["127.0.0.1:6007", "127.0.0.1:6008"] - vars = ["var1","var2","var3","var4","var5"] + >>> from paddle.incubate.distributed.fleet.parameter_server.ir.ps_dispatcher import RoundRobin + + >>> pserver_endpoints = ["127.0.0.1:6007", "127.0.0.1:6008"] + >>> vars = ["var1","var2","var3","var4","var5"] - rr = RoundRobin(pserver_endpoints) - rr.dispatch(vars) + >>> rr = RoundRobin(pserver_endpoints) + >>> rr.dispatch(vars) """ diff --git a/python/paddle/incubate/nn/layer/fused_ec_moe.py b/python/paddle/incubate/nn/layer/fused_ec_moe.py index 407c8753519ce..1ae117fce4544 100644 --- a/python/paddle/incubate/nn/layer/fused_ec_moe.py +++ b/python/paddle/incubate/nn/layer/fused_ec_moe.py @@ -46,15 +46,17 @@ class FusedEcMoe(Layer): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn.layer.fused_ec_moe import FusedEcMoe + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> from paddle.incubate.nn.layer.fused_ec_moe import FusedEcMoe - x = paddle.randn([10, 128, 1024]) # [bsz, seq_len, d_model] - gate = paddle.randn([10, 128, 8]) # [bsz, seq_len, num_experts] - moe = FusedEcMoe(1024, 4096, 8, act_type="gelu") - y = moe(x, gate) - print(y.shape) # [10, 128, 1024] + >>> x = paddle.randn([10, 128, 1024]) # [bsz, seq_len, d_model] + >>> gate = paddle.randn([10, 128, 8]) # [bsz, seq_len, num_experts] + >>> moe = FusedEcMoe(1024, 4096, 8, act_type="gelu") + >>> y = moe(x, gate) + >>> print(y.shape) + [10, 128, 1024] """ def __init__( diff --git a/python/paddle/incubate/nn/layer/fused_linear.py b/python/paddle/incubate/nn/layer/fused_linear.py index 965aedaa391e9..14b159b9eb896 100644 --- a/python/paddle/incubate/nn/layer/fused_linear.py +++ b/python/paddle/incubate/nn/layer/fused_linear.py @@ -56,14 +56,16 @@ class FusedLinear(Layer): Examples: .. code-block:: python - # required: gpu - import paddle - from paddle.incubate.nn import FusedLinear + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> paddle.device.set_device('gpu') + >>> from paddle.incubate.nn import FusedLinear - x = paddle.randn([3, 4]) - linear = FusedLinear(4, 5) - y = linear(x) - print(y.shape) # [3, 5] + >>> x = paddle.randn([3, 4]) + >>> linear = FusedLinear(4, 5) + >>> y = linear(x) + >>> print(y.shape) + [3, 5] """ def __init__( diff --git a/python/paddle/incubate/nn/layer/io.py b/python/paddle/incubate/nn/layer/io.py index f464d93833b2f..3d8b8ec1fe2fd 100644 --- a/python/paddle/incubate/nn/layer/io.py +++ b/python/paddle/incubate/nn/layer/io.py @@ -55,21 +55,25 @@ class ListenAndServ: Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle - with fluid.program_guard(main): - serv = layers.ListenAndServ( - "127.0.0.1:6170", ["X"], optimizer_mode=False) - with serv.do(): - x = paddle.static.data( - shape=[32, 32], - dtype='float32', - name="X") - paddle.nn.initializer.Constant(value=1.0)(x, main.global_block()) - paddle.scale(x=x, scale=10.0, out=out_var) - - exe = fluid.Executor(place) - exe.run(main) + >>> # doctest: +REQUIRES(env:DISTRIBUTED) + >>> from paddle.incubate.nn.layer.io import ListenAndServ + >>> import paddle + >>> paddle.enable_static() + >>> place = paddle.CPUPlace() + >>> main = paddle.static.Program() + >>> with paddle.static.program_guard(main): + ... serv = ListenAndServ( + ... "127.0.0.1:6170", ["X"], optimizer_mode=False) + ... with serv.do(): + ... x = paddle.static.data( + ... shape=[32, 32], + ... dtype='float32', + ... name="X") + ... paddle.nn.initializer.Constant(value=1.0)(x, main.global_block()) + ... paddle.scale(x=x, scale=10.0) + + >>> exe = paddle.static.Executor(place) + >>> exe.run(main) """ def __init__(self, endpoint, inputs, fan_in=1, optimizer_mode=True): @@ -115,7 +119,9 @@ def parent_block(self): return parent_block def complete_op(self): - from paddle.incubate.fleet.parameter_server.mode import DistributedMode + from paddle.incubate.distributed.fleet.parameter_server.mode import ( + DistributedMode, + ) main_program = self.helper.main_program current_block = main_program.current_block() From 95983a6218e83538da96fe7a58a01aac22919e32 Mon Sep 17 00:00:00 2001 From: caozhou <48191911+Caozhou1995@users.noreply.github.com> Date: Thu, 7 Sep 2023 08:26:45 +0800 Subject: [PATCH 164/194] add auto tuner module (#56968) --- python/setup.py.in | 1 + setup.py | 1 + 2 files changed, 2 insertions(+) diff --git a/python/setup.py.in b/python/setup.py.in index a0e665526dc68..61ce98ffda09c 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -380,6 +380,7 @@ packages=['paddle', 'paddle.distributed.sharding', 'paddle.distributed.fleet', 'paddle.distributed.launch', + 'paddle.distributed.auto_tuner', 'paddle.distributed.launch.context', 'paddle.distributed.launch.controllers', 'paddle.distributed.launch.job', diff --git a/setup.py b/setup.py index f2b1df02d8c1e..962e9d70b002a 100644 --- a/setup.py +++ b/setup.py @@ -1377,6 +1377,7 @@ def get_setup_parameters(): 'paddle.distributed.utils', 'paddle.distributed.sharding', 'paddle.distributed.fleet', + 'paddle.distributed.auto_tuner', 'paddle.distributed.launch', 'paddle.distributed.launch.context', 'paddle.distributed.launch.controllers', From e7246bb09565196cb05df74756ff153818d7c6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=91=A8=E5=91=A8?= <39978853+zhoutianzi666@users.noreply.github.com> Date: Thu, 7 Sep 2023 09:40:22 +0800 Subject: [PATCH 165/194] [Paddle Inference] rename vars in subgraph (#56995) --- .../inference/analysis/ir_passes/subgraph_util.cc | 12 +++++++++--- .../inference/analysis/ir_passes/subgraph_util.h | 5 +++++ .../analysis/ir_passes/tensorrt_subgraph_pass.cc | 9 ++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/inference/analysis/ir_passes/subgraph_util.cc b/paddle/fluid/inference/analysis/ir_passes/subgraph_util.cc index 205bba131bf8c..fee6023db2c73 100644 --- a/paddle/fluid/inference/analysis/ir_passes/subgraph_util.cc +++ b/paddle/fluid/inference/analysis/ir_passes/subgraph_util.cc @@ -166,7 +166,7 @@ void RenameAndGetOutputs( for (int k = 0; k < in_var->arguments_size(); k++) { // all the arguments const std::string arg_value = in_var->arguments(k); const std::string arg_value_with_id = - arg_value + std::to_string(var2id[arg_value]); + RenameVarBeUnique(arg_value, std::to_string(var2id[arg_value])); if (input_names_with_id.count(arg_value_with_id)) { replaced_names.push_back(arg_value); if (graph_var_map.count(arg_value)) { @@ -199,7 +199,8 @@ void RenameAndGetOutputs( PADDLE_GET_CONST(std::vector, op_desc.GetAttr("paddings")); if (same_hierarchy_conv2d_num_map[input_var_name] > 0) { (*output_names_with_id) - .insert(out_var_name + std::to_string(var2id[out_var_name])); + .insert(RenameVarBeUnique(out_var_name, + std::to_string(var2id[out_var_name]))); (*output_names).insert(out_var_name); } else if (filter_shape[2] == 1 && filter_shape[3] == 1 && strides[0] == 1 && strides[1] == 1 && paddings[0] == 0 && @@ -214,7 +215,7 @@ void RenameAndGetOutputs( for (int k = 0; k < out_var->arguments_size(); k++) { const std::string arg_value = out_var->arguments(k); const std::string arg_value_with_id = - arg_value + std::to_string(var2id[arg_value]); + RenameVarBeUnique(arg_value, std::to_string(var2id[arg_value])); if (graph_var_map.count(arg_value)) { add_block_var(arg_value, arg_value_with_id); } @@ -231,6 +232,11 @@ void RenameAndGetOutputs( } } +std::string RenameVarBeUnique(std::string original_var_name, + std::string var_id) { + return original_var_name + "_subgraph_" + var_id; +} + } // namespace analysis } // namespace inference } // namespace paddle diff --git a/paddle/fluid/inference/analysis/ir_passes/subgraph_util.h b/paddle/fluid/inference/analysis/ir_passes/subgraph_util.h index 21bfe7582061a..ff196a7c03d49 100644 --- a/paddle/fluid/inference/analysis/ir_passes/subgraph_util.h +++ b/paddle/fluid/inference/analysis/ir_passes/subgraph_util.h @@ -62,6 +62,11 @@ void RenameAndGetOutputs( const std::unordered_map &graph_var_map, bool trt_and_not_int8 = false); +// When fuse some ops into one subgraph, we need to rename all vars within this +// subgraph (excluding the inputs and outputs of the subgraph) to a unique name. +std::string RenameVarBeUnique(std::string original_var_name, + std::string var_id); + } // namespace analysis } // namespace inference } // namespace paddle diff --git a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc index 1fc24afe4942a..99f0c74770cff 100644 --- a/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc +++ b/paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc @@ -327,7 +327,8 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp( // The node->inputs contains input tensors and parameters. for (auto *x : node->inputs) { input_names.insert(x->Name()); - input_names_with_id.insert(x->Name() + std::to_string(x->id())); + input_names_with_id.insert( + RenameVarBeUnique(x->Name(), std::to_string(x->id()))); if (std::count(graph_params.begin(), graph_params.end(), x->Name()) > 0) { parameters.push_back(x->Name()); } @@ -357,7 +358,8 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp( // https://github.com/PaddlePaddle/Paddle/pull/53184 for (auto *n : graph->Nodes()) { if (n->IsVar() && input_names.count(n->Name())) { - input_names_with_id.insert(n->Name() + std::to_string(n->id())); + input_names_with_id.insert( + RenameVarBeUnique(n->Name(), std::to_string(n->id()))); } } @@ -412,7 +414,8 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp( for (auto *x : node->outputs) { output_names.insert(x->Name()); - output_names_with_id.insert(x->Name() + std::to_string(x->id())); + output_names_with_id.insert( + RenameVarBeUnique(x->Name(), std::to_string(x->id()))); origin_name_output_rank[x->Name()] = x->Var()->GetShape().size(); trt_outputs.insert(x); map_origin_outputs_dtype[x->Name()] = From 093f6ecdc9890a725e2a7c94ef397b131c4193d2 Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:03:55 +0800 Subject: [PATCH 166/194] [xdoctest] reformat example code with google style in No.286-290 (#56797) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 8.30下午 # doctest: +REQUIRES(env:GPU)这个不清楚怎么改,还请帮忙看一下 * Update softmax_mask_fuse.py 修改了相关内容 * Update graph_reindex.py * Update graph_sample_neighbors.py 全都测了一遍,全都是successful * fix cases on cpu * fix gpu cases --------- Co-authored-by: SigureMo --- .../incubate/operators/graph_reindex.py | 68 +++++++++++-------- .../operators/graph_sample_neighbors.py | 29 ++++---- .../incubate/operators/graph_send_recv.py | 58 +++++++++------- .../incubate/operators/softmax_mask_fuse.py | 17 ++--- .../softmax_mask_fuse_upper_triangle.py | 33 ++++++--- 5 files changed, 123 insertions(+), 82 deletions(-) diff --git a/python/paddle/incubate/operators/graph_reindex.py b/python/paddle/incubate/operators/graph_reindex.py index 2594ed7ce056e..26f55b0fa2ec3 100644 --- a/python/paddle/incubate/operators/graph_reindex.py +++ b/python/paddle/incubate/operators/graph_reindex.py @@ -80,33 +80,47 @@ def graph_reindex( Examples: .. code-block:: python - import paddle - - x = [0, 1, 2] - neighbors_e1 = [8, 9, 0, 4, 7, 6, 7] - count_e1 = [2, 3, 2] - x = paddle.to_tensor(x, dtype="int64") - neighbors_e1 = paddle.to_tensor(neighbors_e1, dtype="int64") - count_e1 = paddle.to_tensor(count_e1, dtype="int32") - - reindex_src, reindex_dst, out_nodes = \ - paddle.incubate.graph_reindex(x, neighbors_e1, count_e1) - # reindex_src: [3, 4, 0, 5, 6, 7, 6] - # reindex_dst: [0, 0, 1, 1, 1, 2, 2] - # out_nodes: [0, 1, 2, 8, 9, 4, 7, 6] - - neighbors_e2 = [0, 2, 3, 5, 1] - count_e2 = [1, 3, 1] - neighbors_e2 = paddle.to_tensor(neighbors_e2, dtype="int64") - count_e2 = paddle.to_tensor(count_e2, dtype="int32") - - neighbors = paddle.concat([neighbors_e1, neighbors_e2]) - count = paddle.concat([count_e1, count_e2]) - reindex_src, reindex_dst, out_nodes = \ - paddle.incubate.graph_reindex(x, neighbors, count) - # reindex_src: [3, 4, 0, 5, 6, 7, 6, 0, 2, 8, 9, 1] - # reindex_dst: [0, 0, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2] - # out_nodes: [0, 1, 2, 8, 9, 4, 7, 6, 3, 5] + >>> import paddle + + >>> x = [0, 1, 2] + >>> neighbors_e1 = [8, 9, 0, 4, 7, 6, 7] + >>> count_e1 = [2, 3, 2] + >>> x = paddle.to_tensor(x, dtype="int64") + >>> neighbors_e1 = paddle.to_tensor(neighbors_e1, dtype="int64") + >>> count_e1 = paddle.to_tensor(count_e1, dtype="int32") + + >>> reindex_src, reindex_dst, out_nodes = paddle.incubate.graph_reindex( + ... x, + ... neighbors_e1, + ... count_e1, + ... ) + >>> print(reindex_src) + Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True, + [3, 4, 0, 5, 6, 7, 6]) + >>> print(reindex_dst) + Tensor(shape=[7], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 0, 1, 1, 1, 2, 2]) + >>> print(out_nodes) + Tensor(shape=[8], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 1, 2, 8, 9, 4, 7, 6]) + + >>> neighbors_e2 = [0, 2, 3, 5, 1] + >>> count_e2 = [1, 3, 1] + >>> neighbors_e2 = paddle.to_tensor(neighbors_e2, dtype="int64") + >>> count_e2 = paddle.to_tensor(count_e2, dtype="int32") + + >>> neighbors = paddle.concat([neighbors_e1, neighbors_e2]) + >>> count = paddle.concat([count_e1, count_e2]) + >>> reindex_src, reindex_dst, out_nodes = paddle.incubate.graph_reindex(x, neighbors, count) + >>> print(reindex_src) + Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True, + [3, 4, 0, 5, 6, 7, 6, 0, 2, 8, 9, 1]) + >>> print(reindex_dst) + Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 0, 1, 1, 1, 2, 2, 0, 1, 1, 1, 2]) + >>> print(out_nodes) + Tensor(shape=[10], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 1, 2, 8, 9, 4, 7, 6, 3, 5]) """ if flag_buffer_hashtable: diff --git a/python/paddle/incubate/operators/graph_sample_neighbors.py b/python/paddle/incubate/operators/graph_sample_neighbors.py index 169acca5fdc63..66e8d5f2fa5b7 100644 --- a/python/paddle/incubate/operators/graph_sample_neighbors.py +++ b/python/paddle/incubate/operators/graph_sample_neighbors.py @@ -80,19 +80,22 @@ def graph_sample_neighbors( Examples: .. code-block:: python - import paddle - # edges: (3, 0), (7, 0), (0, 1), (9, 1), (1, 2), (4, 3), (2, 4), - # (9, 5), (3, 5), (9, 6), (1, 6), (9, 8), (7, 8) - row = [3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7] - colptr = [0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13] - nodes = [0, 8, 1, 2] - sample_size = 2 - row = paddle.to_tensor(row, dtype="int64") - colptr = paddle.to_tensor(colptr, dtype="int64") - nodes = paddle.to_tensor(nodes, dtype="int64") - out_neighbors, out_count = \ - paddle.incubate.graph_sample_neighbors(row, colptr, nodes, - sample_size=sample_size) + >>> import paddle + >>> # edges: (3, 0), (7, 0), (0, 1), (9, 1), (1, 2), (4, 3), (2, 4), + >>> # (9, 5), (3, 5), (9, 6), (1, 6), (9, 8), (7, 8) + >>> row = [3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7] + >>> colptr = [0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13] + >>> nodes = [0, 8, 1, 2] + >>> sample_size = 2 + >>> row = paddle.to_tensor(row, dtype="int64") + >>> colptr = paddle.to_tensor(colptr, dtype="int64") + >>> nodes = paddle.to_tensor(nodes, dtype="int64") + >>> out_neighbors, out_count = paddle.incubate.graph_sample_neighbors( + ... row, + ... colptr, + ... nodes, + ... sample_size=sample_size + ... ) """ diff --git a/python/paddle/incubate/operators/graph_send_recv.py b/python/paddle/incubate/operators/graph_send_recv.py index 7a874f19249e1..84bc656f348a9 100644 --- a/python/paddle/incubate/operators/graph_send_recv.py +++ b/python/paddle/incubate/operators/graph_send_recv.py @@ -91,30 +91,40 @@ def graph_send_recv( .. code-block:: python - import paddle - - x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") - indexes = paddle.to_tensor([[0, 1], [1, 2], [2, 1], [0, 0]], dtype="int32") - src_index = indexes[:, 0] - dst_index = indexes[:, 1] - out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum") - # Outputs: [[0., 2., 3.], [2., 8., 10.], [1., 4., 5.]] - - x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") - indexes = paddle.to_tensor([[0, 1], [2, 1], [0, 0]], dtype="int32") - src_index = indexes[:, 0] - dst_index = indexes[:, 1] - out_size = paddle.max(dst_index) + 1 - out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum", out_size=out_size) - # Outputs: [[0., 2., 3.], [[2., 8., 10.]]] - - x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") - indexes = paddle.to_tensor([[0, 1], [2, 1], [0, 0]], dtype="int32") - src_index = indexes[:, 0] - dst_index = indexes[:, 1] - out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum") - # Outputs: [[0., 2., 3.], [2., 8., 10.], [0., 0., 0.]] - + >>> import paddle + + >>> x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") + >>> indexes = paddle.to_tensor([[0, 1], [1, 2], [2, 1], [0, 0]], dtype="int32") + >>> src_index = indexes[:, 0] + >>> dst_index = indexes[:, 1] + >>> out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum") + >>> print(out) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 2. , 3. ], + [2. , 8. , 10.], + [1. , 4. , 5. ]]) + + >>> x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") + >>> indexes = paddle.to_tensor([[0, 1], [2, 1], [0, 0]], dtype="int32") + >>> src_index = indexes[:, 0] + >>> dst_index = indexes[:, 1] + >>> out_size = paddle.max(dst_index) + 1 + >>> out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum", out_size=out_size) + >>> print(out) + Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 2. , 3. ], + [2. , 8. , 10.]]) + + >>> x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32") + >>> indexes = paddle.to_tensor([[0, 1], [2, 1], [0, 0]], dtype="int32") + >>> src_index = indexes[:, 0] + >>> dst_index = indexes[:, 1] + >>> out = paddle.incubate.graph_send_recv(x, src_index, dst_index, pool_type="sum") + >>> print(out) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0. , 2. , 3. ], + [2. , 8. , 10.], + [0. , 0. , 0. ]]) """ if pool_type not in ["sum", "mean", "max", "min"]: diff --git a/python/paddle/incubate/operators/softmax_mask_fuse.py b/python/paddle/incubate/operators/softmax_mask_fuse.py index 178cfd9a046ce..c9a3539ee09e0 100644 --- a/python/paddle/incubate/operators/softmax_mask_fuse.py +++ b/python/paddle/incubate/operators/softmax_mask_fuse.py @@ -40,20 +40,21 @@ def softmax_mask_fuse(x, mask, name=None): For more information, please refer to :ref:`api_guide_Name`. Returns: - 4-D Tensor. A location into which the result is stored. It’s dimension is 4D. Has same shape with x. + 4-D Tensor. A location into which the result is stored. It's dimension is 4D. Has same shape with x. Examples: .. code-block:: python - # required: gpu - import paddle - import paddle.incubate as incubate + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> import paddle.incubate as incubate - x = paddle.rand([2, 8, 8, 32]) - mask = paddle.rand([2, 1, 8, 32]) + >>> x = paddle.rand([2, 8, 8, 32]) + >>> mask = paddle.rand([2, 1, 8, 32]) - rst = incubate.softmax_mask_fuse(x, mask) - # [[[[0.02404429, 0.04658398, 0.02746007, ..., 0.01489375, 0.02397441, 0.02851614] ... ]]] + >>> rst = incubate.softmax_mask_fuse(x, mask) + >>> rst.shape + [2, 8, 8, 32] """ if in_dynamic_mode(): out = _legacy_C_ops.fused_softmax_mask(x, mask) diff --git a/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py b/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py index dd8e229a1e9c2..ae02603f27f83 100644 --- a/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py +++ b/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py @@ -38,22 +38,35 @@ def softmax_mask_fuse_upper_triangle(x): The third dimension of x must be same with the fourth dimension of x. Returns: - 4-D Tensor. A location into which the result is stored. It’s dimension is 4D. Has same dimension with x. + 4-D Tensor. A location into which the result is stored. It's dimension is 4D. Has same dimension with x. Examples: .. code-block:: python - # required: gpu - import paddle - import paddle.incubate as incubate + >>> # doctest: +REQUIRES(env:GPU) + >>> import paddle + >>> import paddle.incubate as incubate - x = paddle.rand((1, 1, 32, 32)) + >>> paddle.seed(1) + >>> paddle.set_device("gpu") + >>> x = paddle.rand((1, 1, 32, 32)) - rst = incubate.softmax_mask_fuse_upper_triangle(x) - # [[[[1. , 0. , 0. , ..., 0., 0., 0.], - # [0.45324376, 0.54675621, 0. , ..., 0., 0., 0.], - # [0.32674268, 0.28156221, 0.39169508, ..., 0., 0., 0.] - # ... ]]] + >>> rst = incubate.softmax_mask_fuse_upper_triangle(x) + >>> print(rst) + Tensor(shape=[1, 1, 32, 32], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[[[1. , 0. , 0. , ..., 0. , + 0. , 0. ], + [0.49575609, 0.50424391, 0. , ..., 0. , + 0. , 0. ], + [0.26035303, 0.25114325, 0.48850375, ..., 0. , + 0. , 0. ], + ..., + [0.04379999, 0.04194880, 0.05150032, ..., 0.02721255, + 0. , 0. ], + [0.02348574, 0.01959674, 0.02609110, ..., 0.04046615, + 0.02248267, 0. ], + [0.02280738, 0.03144657, 0.02892209, ..., 0.03885521, + 0.03342311, 0.02842640]]]]) """ if in_dynamic_mode(): out = _legacy_C_ops.fused_softmax_mask_upper_triangle(x) From 2857fdbbd10e2b8bee4df1ac4c0b02c9865f4828 Mon Sep 17 00:00:00 2001 From: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:12:27 +0800 Subject: [PATCH 167/194] [NewIR] Update send recv infermeta and add unittest (#56794) * [NewIR]Update send recv infermeta and add unittest * rm new ir flag * rm fluid api * skip runing startup prog * update flag name * update recv_v2 yaml * fix conflict * unittest only for pp * fix cmakelist * unittest check precision * control random * fix cmakelist --- .../ir/dialect/op_generator/python_c_gen.py | 3 +- .../ir/dialect/paddle_dialect/ir/pd_ops.yaml | 2 +- paddle/phi/infermeta/nullary.cc | 42 +++++ paddle/phi/infermeta/nullary.h | 7 + paddle/phi/infermeta/unary.cc | 57 ++----- paddle/phi/infermeta/unary.h | 9 +- .../auto_parallel/static/engine.py | 13 ++ .../distributed/auto_parallel/static/utils.py | 13 ++ test/auto_parallel/CMakeLists.txt | 13 +- test/auto_parallel/gpt_with_newir.py | 161 ++++++++++++++++++ test/auto_parallel/test_gpt_with_newir.py | 55 ++++++ 11 files changed, 317 insertions(+), 58 deletions(-) create mode 100644 test/auto_parallel/gpt_with_newir.py create mode 100644 test/auto_parallel/test_gpt_with_newir.py diff --git a/paddle/fluid/ir/dialect/op_generator/python_c_gen.py b/paddle/fluid/ir/dialect/op_generator/python_c_gen.py index 340aa9569818a..a890a8db5d249 100644 --- a/paddle/fluid/ir/dialect/op_generator/python_c_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/python_c_gen.py @@ -283,11 +283,12 @@ def _gen_cast_attrs(self, op_info, op_name, with_mutable): def _gen_one_impl(self, op_info, op_name): input_name_list = op_info.input_name_list + output_name_list = op_info.output_name_list attr_name_list = op_info.attribute_name_list mutable_attr_name_list = op_info.mutable_attribute_name_list no_mutable_attr_name_list = op_info.non_mutable_attribute_name_list - if op_name == "send_v2": + if len(output_name_list) == 0: ret = NO_OUTPUT_API_IMPL_TEMPLATE.format( api_name=op_name, inputs=self._gen_inputs(op_info, op_name), diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml index a5d2f42fc1ba1..da4c252af7217 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_ops.yaml @@ -92,7 +92,7 @@ output : Tensor(out) infer_meta: func: RecvV2InferMeta - param: [peer, dtype, out_shape] + param: [ring_id, dynamic_shape, peer, out_shape, dtype] kernel : func : recv_v2 param : [ring_id, dynamic_shape, peer, out_shape, dtype, use_calc_stream] diff --git a/paddle/phi/infermeta/nullary.cc b/paddle/phi/infermeta/nullary.cc index 1526ba2ec021b..d5da3a2f8bc87 100644 --- a/paddle/phi/infermeta/nullary.cc +++ b/paddle/phi/infermeta/nullary.cc @@ -181,6 +181,48 @@ void PRecvArrayInferMeta(int peer, out->set_dtype(dtype); } +void RecvV2InferMeta(const int ring_id, + const bool dynamic_shape, + const int peer, + const std::vector& out_shape, + DataType dtype, + MetaTensor* out) { + PADDLE_ENFORCE_GE( + peer, + 0, + errors::InvalidArgument( + "The peer (%d) for recv_v2 op must be non-negative.", peer)); + + PADDLE_ENFORCE_GE( + ring_id, + 0, + errors::InvalidArgument( + "The ring_id (%d) for recv_v2 op must be non-negative.", ring_id)); + + PADDLE_ENFORCE_GE(out_shape.size(), + 1, + errors::InvalidArgument( + "The size of the output shape must be greater than 0 " + "but the value given is %d.", + out_shape.size())); + + if (!dynamic_shape) { + for (size_t i = 0; i < out_shape.size(); ++i) { + PADDLE_ENFORCE_GE(out_shape[i], + 1, + errors::InvalidArgument( + "The shape attribute for recv_v2 must be set " + "explicitly, but the %dth element is %d which " + "is less than 1. Or dynamic_shape should be " + "set to True for both send_v2 and recv_v2.", + i, + out_shape[i])); + } + out->set_dims(phi::make_ddim(out_shape)); + } + out->set_dtype(dtype); +} + void TruncatedGaussianRandomInferMeta(const std::vector& shape, float mean, float std, diff --git a/paddle/phi/infermeta/nullary.h b/paddle/phi/infermeta/nullary.h index 775df0cc6ab47..bc73942c8ec1c 100644 --- a/paddle/phi/infermeta/nullary.h +++ b/paddle/phi/infermeta/nullary.h @@ -76,6 +76,13 @@ void PRecvArrayInferMeta(int peer, const std::vector& out_shape, MetaTensor* out); +void RecvV2InferMeta(const int ring_id, + const bool dynamic_shape, + const int peer, + const std::vector& out_shape, + DataType dtype, + MetaTensor* out); + void TruncatedGaussianRandomInferMeta(const std::vector& shape, float mean, float std, diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 9932f9612d70f..aa1b6526cd5f8 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -405,50 +405,6 @@ void CConcatInferMeta(const MetaTensor& x, int nranks, MetaTensor* out) { out->set_dtype(x.dtype()); } -void SendV2InferMeta(const int peer, const int ring_id) { - PADDLE_ENFORCE_GE( - peer, - 0, - errors::InvalidArgument( - "The peer (%d) for send_v2 op must be non-negative.", peer)); - PADDLE_ENFORCE_GE( - ring_id, - 0, - errors::InvalidArgument( - "The ring_id (%d) for send_v2 op must be non-negative.", ring_id)); -} - -void RecvV2InferMeta(int peer, - DataType dtype, - const std::vector& out_shape, - MetaTensor* out) { - PADDLE_ENFORCE_GE( - peer, - 0, - errors::InvalidArgument( - "The peer (%d) for p_recv op must be non-negative.", peer)); - - PADDLE_ENFORCE_GE(out_shape.size(), - 1, - errors::InvalidArgument( - "The size of the output shape must be greater than 0 " - "but the value given is %d.", - out_shape.size())); - - for (size_t i = 0; i < out_shape.size(); ++i) { - PADDLE_ENFORCE_GE( - out_shape[i], - 1, - errors::InvalidArgument("The shape attribute for recv must be set " - "explicitly, but the %dth element is %d which " - "is less than 1. Or dynamic_shape should be " - "set to True for both send_v2 and recv_v2.", - i, - out_shape[i])); - } - out->set_dtype(dtype); -} - void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out) { auto dims = x.dims(); auto rank = dims.size(); @@ -3045,6 +3001,19 @@ void PSendArrayInferMeta(const MetaTensor& x, int peer) { "The peer (%d) for p_send op must be non-negative.", peer)); } +void SendV2InferMeta(const int peer, const int ring_id) { + PADDLE_ENFORCE_GE( + peer, + 0, + errors::InvalidArgument( + "The peer (%d) for send_v2 op must be non-negative.", peer)); + PADDLE_ENFORCE_GE( + ring_id, + 0, + errors::InvalidArgument( + "The ring_id (%d) for send_v2 op must be non-negative.", ring_id)); +} + void PoolInferMeta(const MetaTensor& x, const std::vector& kernel_size, const std::vector& strides, diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 2bf90048d30d3..a3b7e87d86d0b 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -73,13 +73,6 @@ void CastInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out); void CConcatInferMeta(const MetaTensor& x, int nranks, MetaTensor* out); -void SendV2InferMeta(const int peer, const int ring_id); - -void RecvV2InferMeta(int peer, - DataType dtype, - const std::vector& out_shape, - MetaTensor* out); - void ChannelShuffleInferMeta(const MetaTensor& x, int groups, const std::string& data_format, @@ -448,6 +441,8 @@ void PSendInferMeta(const MetaTensor& x, int peer); void PSendArrayInferMeta(const MetaTensor& x, int peer); +void SendV2InferMeta(const int peer, const int ring_id); + void QrInferMeta(const MetaTensor& x, const std::string& mode, MetaTensor* q, diff --git a/python/paddle/distributed/auto_parallel/static/engine.py b/python/paddle/distributed/auto_parallel/static/engine.py index 07bebf3d0c5ae..ff0c3004cf605 100644 --- a/python/paddle/distributed/auto_parallel/static/engine.py +++ b/python/paddle/distributed/auto_parallel/static/engine.py @@ -833,6 +833,14 @@ def _initialize(self, mode): dist_main_program, self._place, dist_context ) + # NOTE(zhaoyinglia): Skip startup program when use new ir temporarily. + use_new_ir = False + if auto_utils.use_new_ir(): + use_new_ir = True + paddle.framework.set_flags( + {"FLAGS_enable_new_ir_in_executor": False} + ) + if self._executor is None: self._executor = paddle.static.Executor(self._place) uninitialized = [] @@ -860,6 +868,11 @@ def _initialize(self, mode): ] self._executor.run(dist_startup_prog) + if use_new_ir: + paddle.framework.set_flags( + {"FLAGS_enable_new_ir_in_executor": True} + ) + def fit( self, train_data, diff --git a/python/paddle/distributed/auto_parallel/static/utils.py b/python/paddle/distributed/auto_parallel/static/utils.py index 3441914518822..8ec0ba2e09f98 100644 --- a/python/paddle/distributed/auto_parallel/static/utils.py +++ b/python/paddle/distributed/auto_parallel/static/utils.py @@ -2423,6 +2423,19 @@ def use_new_executor(): ] +def use_new_ir(): + enable_new_ir_in_executor = os.environ.get( + 'FLAGS_enable_new_ir_in_executor', None + ) + return enable_new_ir_in_executor in [ + 1, + '1', + True, + 'True', + 'true', + ] + + def get_pp_stage(dist_context, rank): pp_idx = None for idx, process_mesh in enumerate(dist_context.process_meshes): diff --git a/test/auto_parallel/CMakeLists.txt b/test/auto_parallel/CMakeLists.txt index eae16e0245468..3af649a809cca 100644 --- a/test/auto_parallel/CMakeLists.txt +++ b/test/auto_parallel/CMakeLists.txt @@ -78,20 +78,23 @@ if(WITH_DISTRIBUTE AND WITH_GPU) PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) py_test_modules(test_pass_quantization MODULES test_pass_quantization) set_tests_properties(test_pass_quantization - PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 60) + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 60) py_test_modules(test_reshard_s_to_r MODULES test_reshard_s_to_r) set_tests_properties(test_reshard_s_to_r - PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) py_test_modules(test_reshard_r_to_s MODULES test_reshard_r_to_s) set_tests_properties(test_reshard_r_to_s - PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) py_test_modules(test_reshard_r_to_p MODULES test_reshard_r_to_p) set_tests_properties(test_reshard_r_to_p - PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) py_test_modules(test_semi_auto_parallel_basic MODULES test_semi_auto_parallel_basic) set_tests_properties(test_semi_auto_parallel_basic - PROPERTIES LABELS "RUN_TYPE=EXECLUSIVE" TIMEOUT 100) + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) + py_test_modules(test_gpt_with_newir MODULES test_gpt_with_newir) + set_tests_properties(test_gpt_with_newir + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE" TIMEOUT 100) # End of unittests WITH multi cards and timeout # NOTE(zyl): unittests WITH multi cards and WITHOUT timeout diff --git a/test/auto_parallel/gpt_with_newir.py b/test/auto_parallel/gpt_with_newir.py new file mode 100644 index 0000000000000..4ddfd5a76ffe0 --- /dev/null +++ b/test/auto_parallel/gpt_with_newir.py @@ -0,0 +1,161 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import os +import random +import unittest + +import numpy as np +from get_gpt_model import FakeDataset, generate_model + +import paddle +from paddle.distributed import ParallelEnv +from paddle.distributed.fleet import auto + +paddle.enable_static() + + +def apply_pass(): + strategy = auto.Strategy() + strategy.auto_mode = "semi" + strategy.reinit = True + return strategy + + +def reset_prog(): + paddle.framework.switch_main_program(paddle.static.Program()) + paddle.framework.switch_startup_program(paddle.static.Program()) + paddle.utils.unique_name.switch() + + +class TestNewIR(unittest.TestCase): + def setUp(self): + self.batch_size = 2 + self.batch_num = 5 + self.clip_norm = 0.2 + self.dataset = FakeDataset(self.batch_size * self.batch_num) + os.environ['FLAGS_new_executor_micro_batching'] = 'True' + paddle.set_flags({'FLAGS_embedding_deterministic': 1}) + paddle.set_flags({'FLAGS_cudnn_deterministic': 1}) + + def init(self, engine): + paddle.seed(2021) + np.random.seed(2021) + random.seed(2021) + paddle.distributed.fleet.init(is_collective=True) + place = paddle.CUDAPlace(ParallelEnv().dev_id) + engine._executor = paddle.static.Executor(place) + + def get_engine(self, mode): + reset_prog() + + strategy = apply_pass() + clip = paddle.nn.ClipGradByGlobalNorm(self.clip_norm) + opt = paddle.optimizer.AdamW(learning_rate=0.00001, grad_clip=None) + model, loss = generate_model(mode) + + engine = auto.Engine(model, loss, opt, strategy=strategy) + self.init(engine) + return engine + + def check_results(self, ref_losses, check_losses): + np.testing.assert_equal( + ref_losses, + check_losses, + err_msg='pass {} has wrong results!, \nu={}\nv={}\ndiff={}'.format( + __class__, ref_losses, check_losses, ref_losses - check_losses + ), + ) + + def enable_new_ir(self, flag): + paddle.set_flags({'FLAGS_enable_new_ir_in_executor': flag}) # for c++ + os.environ['FLAGS_enable_new_ir_in_executor'] = str(flag) # for python + + def test_dp(self): + self.enable_new_ir(False) + engine_dp_prog = self.get_engine("dp") + out_dp_prog = engine_dp_prog.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + self.enable_new_ir(True) + engine_dp_ir = self.get_engine("dp") + out_dp_ir = engine_dp_ir.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + self.check_results( + out_dp_prog.history["loss"][0], out_dp_ir.history["loss"][0] + ) + + def test_mp(self): + self.enable_new_ir(False) + engine_mp_prog = self.get_engine("mp") + out_mp_prog = engine_mp_prog.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + self.enable_new_ir(True) + engine_mp_ir = self.get_engine("mp") + out_mp_ir = engine_mp_ir.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + self.check_results( + out_mp_prog.history["loss"][0], out_mp_ir.history["loss"][0] + ) + + def test_pp(self): + # navie pipeline parallel without schedule + self.enable_new_ir(False) + engine_pp_prog = self.get_engine("pp") + out_pp_prog = engine_pp_prog.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + self.enable_new_ir(True) + # send_v2/recv_v2 dynamic_shape is True + engine_pp_ir = self.get_engine("pp") + out_pp_ir = engine_pp_ir.fit( + self.dataset, 3, batch_size=self.batch_size, log_freq=1 + ) + + if paddle.distributed.get_rank() == 1: + self.check_results( + out_pp_prog.history["loss"][0], out_pp_ir.history["loss"][0] + ) + + # send_v2/recv_v2 dynamic_shape is False + engine_pp_prog1 = self.get_engine("pp") + dataloader_pp_prog = engine_pp_prog1.dataloader( + self.dataset, + batch_size=self.batch_size, + sample_split=3, + mode="train", + ) + engine_pp_prog1.prepare(mode="train") + for op in engine_pp_prog1.main_program.global_block().ops: + if op.type in ["send_v2", "recv_v2"]: + op.desc._set_attr("dynamic_shape", False) + for data in dataloader_pp_prog: + out_pp_prog1 = engine_pp_prog1.run(data, mode="train") + + if paddle.distributed.get_rank() == 1: + self.check_results( + out_pp_prog1["loss"], out_pp_ir.history["loss"][0] + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/auto_parallel/test_gpt_with_newir.py b/test/auto_parallel/test_gpt_with_newir.py new file mode 100644 index 0000000000000..2f736d8a3b297 --- /dev/null +++ b/test/auto_parallel/test_gpt_with_newir.py @@ -0,0 +1,55 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import os +import subprocess +import sys +import tempfile +import unittest + + +class TestGPTNewIR(unittest.TestCase): + def test_gpt_newir(self): + file_dir = os.path.dirname(os.path.abspath(__file__)) + launch_model_path = os.path.join(file_dir, "gpt_with_newir.py") + + if os.environ.get("WITH_COVERAGE", "OFF") == "ON": + coverage_args = ["-m", "coverage", "run", "--branch", "-p"] + else: + coverage_args = [] + + tmp_dir = tempfile.TemporaryDirectory() + cmd = ( + [sys.executable, "-u"] + + coverage_args + + [ + "-m", + "paddle.distributed.launch", + "--devices", + "0,1", + "--log_dir", + tmp_dir.name, + launch_model_path, + ] + ) + + process = subprocess.Popen(cmd) + process.wait() + self.assertEqual(process.returncode, 0) + + tmp_dir.cleanup() + + +if __name__ == "__main__": + unittest.main() From 2e9a79f4541039d6d2d112f267c3c50bfc85b465 Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:23:14 +0800 Subject: [PATCH 168/194] [IR] Fix inplace bug of add_grad cpu kernel (#57042) * enable inplace in dy2st with newir * fix bug --------- Co-authored-by: kangguangli --- paddle/fluid/framework/CMakeLists.txt | 2 +- paddle/fluid/framework/executor_cache.cc | 12 ++++++++++++ paddle/fluid/ir/transforms/inplace_pass.cc | 19 +++++++++++++++++-- paddle/ir/pass/pass_registry.h | 4 ++-- .../transforms/dead_code_elimination_pass.cc | 2 +- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 055163ed6206b..fab0a8765bbf4 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -1034,7 +1034,7 @@ cc_library( cc_library( executor_cache SRCS executor_cache.cc - DEPS parallel_executor standalone_executor phi_kernel_adaptor + DEPS parallel_executor standalone_executor phi_kernel_adaptor pd_inplace_pass pd_op_to_kernel_pass ir) if(WITH_PSCORE) get_property(RPC_DEPS GLOBAL PROPERTY RPC_DEPS) diff --git a/paddle/fluid/framework/executor_cache.cc b/paddle/fluid/framework/executor_cache.cc index 959d4a4157ba3..7a97e67be90c5 100644 --- a/paddle/fluid/framework/executor_cache.cc +++ b/paddle/fluid/framework/executor_cache.cc @@ -13,12 +13,16 @@ // limitations under the License. #include "paddle/fluid/framework/executor_cache.h" + #include "paddle/fluid/framework/new_executor/interpretercore.h" #include "paddle/fluid/framework/op_info.h" +#include "paddle/fluid/ir/transforms/inplace_pass.h" #include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h" #include "paddle/fluid/ir_adaptor/translator/translate.h" #include "paddle/ir/core/program.h" #include "paddle/ir/core/value.h" +#include "paddle/ir/pass/pass.h" +#include "paddle/ir/pass/pass_manager.h" namespace paddle { namespace framework { @@ -440,6 +444,10 @@ std::unique_ptr<::ir::Program> ConstructFowardIrProgram( auto ir_res = paddle::dialect::PdOpLowerToKernelPass(program.get()); + ::ir::PassManager pm(::ir::IrContext::Instance(), 3); + pm.AddPass(::ir::CreateInplacePass()); + pm.Run(ir_res.get()); + return ir_res; } @@ -513,6 +521,10 @@ std::unique_ptr<::ir::Program> ConstructBackwardIrProgram( auto res = paddle::dialect::PdOpLowerToKernelPass(program.get()); + ::ir::PassManager pm(::ir::IrContext::Instance(), 3); + pm.AddPass(::ir::CreateInplacePass()); + pm.Run(res.get()); + return res; } diff --git a/paddle/fluid/ir/transforms/inplace_pass.cc b/paddle/fluid/ir/transforms/inplace_pass.cc index 28c733e0928ac..c445da1142087 100644 --- a/paddle/fluid/ir/transforms/inplace_pass.cc +++ b/paddle/fluid/ir/transforms/inplace_pass.cc @@ -18,6 +18,7 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/fluid/ir/dialect/paddle_dialect/trait/inplace.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_parser.h" +#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" #include "paddle/ir/core/builtin_op.h" @@ -199,6 +200,20 @@ static std::unordered_map GetInplaceOps( upper_op_attrs.at("op_name").dyn_cast<::ir::StrAttribute>().AsString(); VLOG(6) << "analyse op: " << upper_op_name; + // NOTE(zhangbo): add_grad cpu kernel can't do inplace, for the reason shown + // in the function: CommonElementwiseBroadcastBackward + // (paddle/phi/kernels/funcs/elementwise_grad_base.h) + if ((upper_op_name == "pd.add_grad") && + (upper_op_attrs.at("kernel_key") + .dyn_cast() + .data() + .backend() == phi::Backend::CPU)) { + for (size_t i = 0; i < op->num_results(); ++i) { + visited_values.insert(op->result(i)); + } + continue; + } + if (upper_op_attrs.count("is_inplace") != 0 && upper_op_attrs.at("is_inplace").dyn_cast().data()) { VLOG(6) << upper_op_name << " is already an inplace op."; @@ -291,7 +306,7 @@ class InplacePass : public ir::Pass { void Run(ir::Operation* op) override { auto module_op = op->dyn_cast(); - IR_ENFORCE(module_op, "DcePass should run on module op."); + IR_ENFORCE(module_op, "InplacePass should run on module op."); auto* block = module_op.block(); auto inplace_ops = details::GetInplaceOps(block); @@ -330,4 +345,4 @@ std::unique_ptr CreateInplacePass() { } // namespace ir -REGISTER_PASS(inplace, InplacePass); +REGISTER_IR_PASS(inplace, InplacePass); diff --git a/paddle/ir/pass/pass_registry.h b/paddle/ir/pass/pass_registry.h index c35dc0ba90a30..a7d52edca027f 100644 --- a/paddle/ir/pass/pass_registry.h +++ b/paddle/ir/pass/pass_registry.h @@ -80,10 +80,10 @@ class PassRegistrar { msg) // Register a new pass that can be applied on the IR. -#define REGISTER_PASS(pass_type, pass_class) \ +#define REGISTER_IR_PASS(pass_type, pass_class) \ STATIC_ASSERT_PASS_GLOBAL_NAMESPACE( \ __reg_pass__##pass_type, \ - "REGISTER_PASS must be called in global namespace"); \ + "REGISTER_IR_PASS must be called in global namespace"); \ static ::ir::PassRegistrar __pass_registrar_##pass_type##__( \ #pass_type); \ int TouchPassRegistrar_##pass_type() { \ diff --git a/paddle/ir/transforms/dead_code_elimination_pass.cc b/paddle/ir/transforms/dead_code_elimination_pass.cc index 461ab9c6708a2..c74d71ea34569 100644 --- a/paddle/ir/transforms/dead_code_elimination_pass.cc +++ b/paddle/ir/transforms/dead_code_elimination_pass.cc @@ -77,4 +77,4 @@ std::unique_ptr CreateDeadCodeEliminationPass() { } // namespace ir -REGISTER_PASS(dead_code_elimination, DeadCodeEliminationPass); +REGISTER_IR_PASS(dead_code_elimination, DeadCodeEliminationPass); From 12301bc5337fa3bc2d07050d240fbac3689fa9ce Mon Sep 17 00:00:00 2001 From: ronnywang Date: Thu, 7 Sep 2023 10:29:15 +0800 Subject: [PATCH 169/194] [CustomDevice] Allow registration of not ready kernels (#57038) --- paddle/phi/core/custom_kernel.cc | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/paddle/phi/core/custom_kernel.cc b/paddle/phi/core/custom_kernel.cc index 58c1a736725b5..bc737fa398baf 100644 --- a/paddle/phi/core/custom_kernel.cc +++ b/paddle/phi/core/custom_kernel.cc @@ -16,9 +16,6 @@ #include "glog/logging.h" -static std::vector gpu_exclusive_kernels({"sync_batch_norm", - "sync_batch_norm_grad"}); - namespace phi { void CustomKernelMap::RegisterCustomKernel(const std::string& name, @@ -44,16 +41,6 @@ void CustomKernelMap::RegisterCustomKernels() { } auto& kernels = KernelFactory::Instance().kernels(); for (auto& pair : kernels_) { - if (kernels.find(pair.first) == kernels.cend()) { - if (std::find(gpu_exclusive_kernels.cbegin(), - gpu_exclusive_kernels.cend(), - pair.first) == gpu_exclusive_kernels.cend()) { - PADDLE_THROW(phi::errors::InvalidArgument( - "The kernel %s is not ready for custom kernel registering.", - pair.first)); - } - } - for (auto& info_pair : pair.second) { PADDLE_ENFORCE_EQ( kernels[pair.first].find(info_pair.first), From 22e5ccb0d0a8899d42aadcfd2c6fffc7523e3b2c Mon Sep 17 00:00:00 2001 From: winter-wang <78149749+winter-wang@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:33:03 +0800 Subject: [PATCH 170/194] [IR] add control flow dialect (#56799) * [IR] add control flow dialect --- .../dialect/paddle_dialect/ir/pd_dialect.cc | 14 ++++- .../ir/dialect/paddle_dialect/ir/pd_dialect.h | 7 ++- .../dialect/paddle_dialect/ir/pd_manual_op.cc | 40 ++++++++++++ .../dialect/paddle_dialect/ir/pd_manual_op.h | 22 ++++++- paddle/ir/core/dialect.h | 2 +- paddle/ir/core/ir_printer.cc | 10 +-- paddle/ir/core/ir_printer.h | 2 +- paddle/ir/core/op_base.h | 4 ++ paddle/ir/core/operation.h | 7 ++- paddle/ir/dialect/CMakeLists.txt | 1 + paddle/ir/dialect/control_flow/CMakeLists.txt | 2 + .../ir/dialect/control_flow/ir/cf_dialect.cc | 20 ++++++ .../ir/dialect/control_flow/ir/cf_dialect.h | 33 ++++++++++ paddle/ir/dialect/control_flow/ir/cf_ops.cc | 26 ++++++++ paddle/ir/dialect/control_flow/ir/cf_ops.h | 35 +++++++++++ test/cpp/ir/CMakeLists.txt | 1 + .../ir/control_flow_dialect/CMakeLists.txt | 8 +++ .../cpp/ir/control_flow_dialect/if_op_test.cc | 61 +++++++++++++++++++ test/cpp/ir/core/ir_op_test.cc | 2 +- 19 files changed, 282 insertions(+), 15 deletions(-) create mode 100644 paddle/ir/dialect/control_flow/CMakeLists.txt create mode 100644 paddle/ir/dialect/control_flow/ir/cf_dialect.cc create mode 100644 paddle/ir/dialect/control_flow/ir/cf_dialect.h create mode 100644 paddle/ir/dialect/control_flow/ir/cf_ops.cc create mode 100644 paddle/ir/dialect/control_flow/ir/cf_ops.h create mode 100644 test/cpp/ir/control_flow_dialect/CMakeLists.txt create mode 100644 test/cpp/ir/control_flow_dialect/if_op_test.cc diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc index 9d24dcd277884..4b9dd25d67e00 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.cc @@ -20,6 +20,7 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type_storage.h" #include "paddle/fluid/ir/dialect/paddle_dialect/transforms/param_to_variable.h" +#include "paddle/ir/core/ir_printer.h" #include "paddle/ir/core/utils.h" namespace paddle { @@ -51,8 +52,8 @@ void PaddleDialect::initialize() { RegisterOps(); - + paddle::dialect::SplitGradOp, + paddle::dialect::IfOp>(); RegisterInterfaces(); } @@ -100,6 +101,15 @@ void PaddleDialect::PrintAttribute(ir::Attribute attr, std::ostream &os) const { } } +void PaddleDialect::PrintOperation(ir::Operation *op, + ir::IrPrinter &printer) const { + if (auto if_op = op->dyn_cast()) { + if_op.Print(printer); + } else { + printer.PrintGeneralOperation(op); + } +} + } // namespace dialect } // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h index 5a3d46afb4394..b9e9567e7908d 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h @@ -25,8 +25,11 @@ class PaddleDialect : public ir::Dialect { static const char* name() { return "pd"; } - void PrintType(ir::Type type, std::ostream& os) const; - void PrintAttribute(ir::Attribute type, std::ostream& os) const; + void PrintType(ir::Type type, std::ostream& os) const override; + void PrintAttribute(ir::Attribute type, std::ostream& os) const override; + + void PrintOperation(ir::Operation* op, + ir::IrPrinter& printer) const override; // NOLINT private: void initialize(); diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index 8e27526cebefa..45d29ce80b269 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -629,6 +629,45 @@ void SplitGradOp::InferMeta(phi::InferMetaContext *infer_meta) { fn(infer_meta); } +void IfOp::Build(ir::Builder &builder, // NOLINT + ir::OperationArgument &argument, // NOLINT + ir::OpResult cond, + std::vector &&output_types) { + argument.num_regions = 2; + argument.AddOperand(cond); + argument.output_types.swap(output_types); +} +ir::Block *IfOp::true_block() { + ir::Region &true_region = (*this)->region(0); + if (true_region.empty()) true_region.emplace_back(); + return true_region.front(); +} +ir::Block *IfOp::false_block() { + ir::Region &false_region = (*this)->region(1); + if (false_region.empty()) false_region.emplace_back(); + return false_region.front(); +} +void IfOp::Print(ir::IrPrinter &printer) { + auto &os = printer.os; + auto op = operation(); + printer.PrintOpResult(op); + os << " = pd.if"; + printer.PrintOpOperands(op); + os << " -> "; + printer.PrintOpReturnType(op); + os << "{"; + for (auto item : *true_block()) { + os << "\n "; + printer.PrintOperation(item); + } + os << "\n } else {"; + for (auto item : *false_block()) { + os << "\n "; + printer.PrintOperation(item); + } + os << "\n }"; +} +void IfOp::Verify() {} } // namespace dialect } // namespace paddle @@ -636,3 +675,4 @@ IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddNOp) IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::SplitGradOp) IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddN_Op) IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::AddNWithKernelOp) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::IfOp) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h index 8f0dbd86d1d80..6e120317cb461 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h @@ -14,7 +14,7 @@ #ifdef GET_MANUAL_OP_LIST #undef GET_MANUAL_OP_LIST -paddle::dialect::AddNOp, paddle::dialect::SplitGradOp +paddle::dialect::AddNOp, paddle::dialect::SplitGradOp, paddle::dialect::IfOp #else @@ -28,6 +28,7 @@ paddle::dialect::AddNOp, paddle::dialect::SplitGradOp #include "paddle/fluid/ir/dialect/paddle_dialect/utils/op_yaml_info_util.h" #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" #include "paddle/ir/core/builder.h" +#include "paddle/ir/core/ir_printer.h" #include "paddle/ir/core/op_base.h" #include "paddle/ir/core/operation_utils.h" #include "paddle/phi/core/infermeta_utils.h" @@ -116,6 +117,23 @@ class SplitGradOp : public ir::Op { static void InferMeta(phi::InferMetaContext *infer_meta); }; +class IfOp : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd.if"; } + static constexpr const char **attributes_name = nullptr; + static constexpr uint32_t attributes_num = 0; + static void Build(ir::Builder &builder, // NOLINT + ir::OperationArgument &argument, // NOLINT + ir::OpResult cond, + std::vector &&output_types); + ir::Value cond() { return operand_source(0); } + ir::Block *true_block(); + ir::Block *false_block(); + void Print(ir::IrPrinter &printer); // NOLINT + void Verify(); +}; + } // namespace dialect } // namespace paddle @@ -123,5 +141,5 @@ IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddNOp) IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::SplitGradOp) IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddN_Op) IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::AddNWithKernelOp) - +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::IfOp) #endif diff --git a/paddle/ir/core/dialect.h b/paddle/ir/core/dialect.h index c1cc54a257b76..be67898dd98f5 100644 --- a/paddle/ir/core/dialect.h +++ b/paddle/ir/core/dialect.h @@ -145,7 +145,7 @@ class IR_API Dialect { IR_THROW("dialect has no registered attribute printing hook"); } - virtual void PrintOperation(const Operation *op, + virtual void PrintOperation(Operation *op, IrPrinter &printer) const; // NOLINT private: diff --git a/paddle/ir/core/ir_printer.cc b/paddle/ir/core/ir_printer.cc index 25f23b31e2854..16d6568ecc4c3 100644 --- a/paddle/ir/core/ir_printer.cc +++ b/paddle/ir/core/ir_printer.cc @@ -125,7 +125,7 @@ void IrPrinter::PrintProgram(const Program* program) { } } -void IrPrinter::PrintOperation(const Operation* op) { +void IrPrinter::PrintOperation(Operation* op) { if (auto* dialect = op->dialect()) { dialect->PrintOperation(op, *this); return; @@ -156,7 +156,7 @@ void IrPrinter::PrintGeneralOperation(const Operation* op) { } void IrPrinter::PrintFullOperation(const Operation* op) { - PrintOperation(op); + PrintGeneralOperation(op); if (op->num_regions() > 0) { os << newline; } @@ -290,7 +290,7 @@ void IrPrinter::PrintOpReturnType(const Operation* op) { [this]() { this->os << ", "; }); } -void Dialect::PrintOperation(const Operation* op, IrPrinter& printer) const { +void Dialect::PrintOperation(Operation* op, IrPrinter& printer) const { printer.PrintGeneralOperation(op); } @@ -299,9 +299,9 @@ void Program::Print(std::ostream& os) const { printer.PrintProgram(this); } -void Operation::Print(std::ostream& os) const { +void Operation::Print(std::ostream& os) { IrPrinter printer(os); - printer.PrintFullOperation(this); + printer.PrintOperation(this); } void Type::Print(std::ostream& os) const { diff --git a/paddle/ir/core/ir_printer.h b/paddle/ir/core/ir_printer.h index d3f868946ddc2..c393d2dfbe90a 100644 --- a/paddle/ir/core/ir_printer.h +++ b/paddle/ir/core/ir_printer.h @@ -49,7 +49,7 @@ class IR_API IrPrinter : public BasicIrPrinter { void PrintProgram(const Program* program); /// @brief dispatch to custom printer function or PrintGeneralOperation - void PrintOperation(const Operation* op); + void PrintOperation(Operation* op); /// @brief print operation itself without its regions void PrintGeneralOperation(const Operation* op); /// @brief print operation and its regions diff --git a/paddle/ir/core/op_base.h b/paddle/ir/core/op_base.h index 1b4690f9099e9..0a491795d4eed 100644 --- a/paddle/ir/core/op_base.h +++ b/paddle/ir/core/op_base.h @@ -215,6 +215,10 @@ class Op : public OpBase { return ConcreteOp(nullptr); } + static bool classof(const Operation *op) { + return op && op->info().id() == TypeId::get(); + } + static std::vector GetInterfaceMap() { constexpr size_t interfaces_num = std::tuple_size::value; std::vector interfaces_map(interfaces_num); diff --git a/paddle/ir/core/operation.h b/paddle/ir/core/operation.h index dec0dfa6883ea..961e4a5fccc50 100644 --- a/paddle/ir/core/operation.h +++ b/paddle/ir/core/operation.h @@ -75,7 +75,7 @@ class IR_API alignas(8) Operation final { const Region ®ion(unsigned index) const; uint32_t num_regions() const { return num_regions_; } - void Print(std::ostream &os) const; + void Print(std::ostream &os); const AttributeMap &attributes() const { return attributes_; } @@ -109,6 +109,11 @@ class IR_API alignas(8) Operation final { return CastUtil::call(this); } + template + bool isa() const { + return T::classof(this); + } + template bool HasTrait() const { return info_.HasTrait(); diff --git a/paddle/ir/dialect/CMakeLists.txt b/paddle/ir/dialect/CMakeLists.txt index a87b0abfb2383..064d328fc53d6 100644 --- a/paddle/ir/dialect/CMakeLists.txt +++ b/paddle/ir/dialect/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(control_flow) add_subdirectory(shape) diff --git a/paddle/ir/dialect/control_flow/CMakeLists.txt b/paddle/ir/dialect/control_flow/CMakeLists.txt new file mode 100644 index 0000000000000..5a693ba156ccd --- /dev/null +++ b/paddle/ir/dialect/control_flow/CMakeLists.txt @@ -0,0 +1,2 @@ +file(GLOB_RECURSE CONTROL_FLOW_SRCS "*.cc") +ir_library(ir_control_flow SRCS ${CONTROL_FLOW_SRCS} DEPS ir_core) diff --git a/paddle/ir/dialect/control_flow/ir/cf_dialect.cc b/paddle/ir/dialect/control_flow/ir/cf_dialect.cc new file mode 100644 index 0000000000000..8d26f862b562b --- /dev/null +++ b/paddle/ir/dialect/control_flow/ir/cf_dialect.cc @@ -0,0 +1,20 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +#include "paddle/ir/dialect/control_flow/ir/cf_dialect.h" +#include "paddle/ir/dialect/control_flow/ir/cf_ops.h" + +namespace ir { +void ControlFlowDialect::initialize() { RegisterOps(); } +} // namespace ir +IR_DEFINE_EXPLICIT_TYPE_ID(ir::ControlFlowDialect) diff --git a/paddle/ir/dialect/control_flow/ir/cf_dialect.h b/paddle/ir/dialect/control_flow/ir/cf_dialect.h new file mode 100644 index 0000000000000..867290cdd5bab --- /dev/null +++ b/paddle/ir/dialect/control_flow/ir/cf_dialect.h @@ -0,0 +1,33 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/ir/core/dialect.h" + +namespace ir { +class ControlFlowDialect : public Dialect { + public: + explicit ControlFlowDialect(IrContext *context) + : Dialect(name(), context, TypeId::get()) { + initialize(); + } + static const char *name() { return "cf"; } + + private: + void initialize(); +}; + +} // namespace ir +IR_DECLARE_EXPLICIT_TYPE_ID(ir::ControlFlowDialect) diff --git a/paddle/ir/dialect/control_flow/ir/cf_ops.cc b/paddle/ir/dialect/control_flow/ir/cf_ops.cc new file mode 100644 index 0000000000000..dc5491d1ad5d3 --- /dev/null +++ b/paddle/ir/dialect/control_flow/ir/cf_ops.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#include "paddle/ir/dialect/control_flow/ir/cf_ops.h" + +namespace ir { + +void YieldOp::Build(Builder &builder, + OperationArgument &argument, + std::vector &&inputs) { + argument.AddOperands(inputs.begin(), inputs.end()); +} +} // namespace ir + +IR_DEFINE_EXPLICIT_TYPE_ID(ir::YieldOp) diff --git a/paddle/ir/dialect/control_flow/ir/cf_ops.h b/paddle/ir/dialect/control_flow/ir/cf_ops.h new file mode 100644 index 0000000000000..d58e717136ae2 --- /dev/null +++ b/paddle/ir/dialect/control_flow/ir/cf_ops.h @@ -0,0 +1,35 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#pragma once + +#include "paddle/ir/core/builder.h" +#include "paddle/ir/core/op_base.h" + +namespace ir { +class IR_API YieldOp : public Op { + public: + using Op::Op; + static const char *name() { return "cf.yield"; } + static constexpr uint32_t attributes_num = 0; + static constexpr const char **attributes_name = nullptr; + + static void Build(Builder &builder, // NOLINT + OperationArgument &argument, // NOLINT + std::vector &&inputs); + void Verify() {} +}; +} // namespace ir + +IR_EXPORT_DECLARE_EXPLICIT_TYPE_ID(ir::YieldOp); diff --git a/test/cpp/ir/CMakeLists.txt b/test/cpp/ir/CMakeLists.txt index 4eec7e8ef94c1..87c538633e6df 100644 --- a/test/cpp/ir/CMakeLists.txt +++ b/test/cpp/ir/CMakeLists.txt @@ -4,4 +4,5 @@ add_subdirectory(pass) add_subdirectory(pattern_rewrite) add_subdirectory(kernel_dialect) add_subdirectory(cinn) +add_subdirectory(control_flow_dialect) add_subdirectory(shape_dialect) diff --git a/test/cpp/ir/control_flow_dialect/CMakeLists.txt b/test/cpp/ir/control_flow_dialect/CMakeLists.txt new file mode 100644 index 0000000000000..5f2a864f9942e --- /dev/null +++ b/test/cpp/ir/control_flow_dialect/CMakeLists.txt @@ -0,0 +1,8 @@ +cc_test_old( + test_if_op + SRCS + if_op_test.cc + DEPS + ir + pd_dialect + gtest) diff --git a/test/cpp/ir/control_flow_dialect/if_op_test.cc b/test/cpp/ir/control_flow_dialect/if_op_test.cc new file mode 100644 index 0000000000000..8d0d962b5e791 --- /dev/null +++ b/test/cpp/ir/control_flow_dialect/if_op_test.cc @@ -0,0 +1,61 @@ +// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +#include +#include + +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.h" +#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.h" +#include "paddle/ir/core/builder.h" +#include "paddle/ir/core/builtin_op.h" +#include "paddle/ir/core/program.h" +#include "paddle/ir/dialect/control_flow/ir/cf_dialect.h" +#include "paddle/ir/dialect/control_flow/ir/cf_ops.h" + +TEST(if_op_test, base) { + ir::IrContext* ctx = ir::IrContext::Instance(); + ctx->GetOrRegisterDialect(); + ctx->GetOrRegisterDialect(); + + ir::Program program(ctx); + ir::Block* block = program.block(); + ir::Builder builder(ctx, block); + + auto full_op = builder.Build( + std::vector{1}, true, phi::DataType::BOOL); + + auto if_op = builder.Build( + full_op.out(), std::vector{builder.bool_type()}); + + ir::Block* true_block = if_op.true_block(); + + builder.SetInsertionPointToStart(true_block); + + auto full_op_1 = builder.Build( + std::vector{2}, true, phi::DataType::BOOL); + builder.Build(std::vector{full_op_1.out()}); + + ir::Block* false_block = if_op.false_block(); + + builder.SetInsertionPointToStart(false_block); + + auto full_op_2 = builder.Build( + std::vector{3}, true, phi::DataType::BOOL); + builder.Build(std::vector{full_op_2.out()}); + + std::stringstream ss; + program.Print(ss); + + LOG(INFO) << ss.str(); +} diff --git a/test/cpp/ir/core/ir_op_test.cc b/test/cpp/ir/core/ir_op_test.cc index 39880c4e5bdaa..48f54c63230e0 100644 --- a/test/cpp/ir/core/ir_op_test.cc +++ b/test/cpp/ir/core/ir_op_test.cc @@ -157,7 +157,7 @@ class TestDialect : public ir::Dialect { } static const char *name() { return "test"; } - void PrintOperation(const ir::Operation *op, + void PrintOperation(ir::Operation *op, ir::IrPrinter &printer) const override { printer.PrintOpResult(op); printer.os << " ="; From f6463eaa4a2fb6366202a32ea15a0c9d34b2c353 Mon Sep 17 00:00:00 2001 From: yangguohao <70266361+yangguohao@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:38:30 +0800 Subject: [PATCH 171/194] =?UTF-8?q?=E3=80=90Complex=20OP=E3=80=91No.28=20L?= =?UTF-8?q?ogSigmoid=20(#56852)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * complex op logsigmoid * fix 2023-08-31 --- paddle/phi/common/complex.h | 10 +++++ .../phi/kernels/cpu/activation_grad_kernel.cc | 3 +- paddle/phi/kernels/cpu/activation_kernel.cc | 2 +- paddle/phi/kernels/funcs/activation_functor.h | 41 +++++++++++++++++++ .../phi/kernels/gpu/activation_grad_kernel.cu | 3 +- paddle/phi/kernels/gpu/activation_kernel.cu | 2 +- python/paddle/nn/functional/activation.py | 7 +++- test/legacy_test/test_activation_op.py | 18 +++++++- 8 files changed, 79 insertions(+), 7 deletions(-) diff --git a/paddle/phi/common/complex.h b/paddle/phi/common/complex.h index 130047f850426..e0ff7f11ac542 100644 --- a/paddle/phi/common/complex.h +++ b/paddle/phi/common/complex.h @@ -476,6 +476,16 @@ HOSTDEVICE inline complex conj(const complex& a) { #endif } +template +HOSTDEVICE inline complex exp(const complex& a) { +#if defined(PADDLE_WITH_CUDA_OR_HIP_COMPLEX) && \ + (defined(__CUDA_ARCH__) || defined(__HIPCC__)) + return complex(thrust::exp(thrust::complex(a))); +#else + return complex(std::exp(std::complex(a))); +#endif +} + template HOSTDEVICE inline complex log(const complex& a) { #if defined(PADDLE_WITH_CUDA_OR_HIP_COMPLEX) && \ diff --git a/paddle/phi/kernels/cpu/activation_grad_kernel.cc b/paddle/phi/kernels/cpu/activation_grad_kernel.cc index 1216801e0eee0..68f3fce76a8dc 100644 --- a/paddle/phi/kernels/cpu/activation_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/activation_grad_kernel.cc @@ -423,7 +423,8 @@ PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_grad, SigmoidGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_double_grad, SigmoidDoubleGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_triple_grad, SigmoidTripleGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(hardsigmoid_grad, HardSigmoidGradKernel) -PD_REGISTER_ACTIVATION_GRAD_KERNEL(logsigmoid_grad, LogSigmoidGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL_WITH_COMPLEX(logsigmoid_grad, + LogSigmoidGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log_grad, LogGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log2_grad, Log2GradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log10_grad, Log10GradKernel) diff --git a/paddle/phi/kernels/cpu/activation_kernel.cc b/paddle/phi/kernels/cpu/activation_kernel.cc index 204947572ce2a..b3f86c7c908db 100644 --- a/paddle/phi/kernels/cpu/activation_kernel.cc +++ b/paddle/phi/kernels/cpu/activation_kernel.cc @@ -228,7 +228,7 @@ PD_REGISTER_KERNEL( square, CPU, ALL_LAYOUT, phi::SquareKernel, float, double, int, int64_t) {} PD_REGISTER_ACTIVATION_KERNEL(softsign, SoftsignKernel) PD_REGISTER_ACTIVATION_KERNEL(sigmoid, SigmoidKernel) -PD_REGISTER_ACTIVATION_KERNEL(logsigmoid, LogSigmoidKernel) +PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX(logsigmoid, LogSigmoidKernel) PD_REGISTER_ACTIVATION_KERNEL(hardsigmoid, HardSigmoidKernel) PD_REGISTER_ACTIVATION_KERNEL(swish, SwishKernel) PD_REGISTER_ACTIVATION_KERNEL(relu6, Relu6Kernel) diff --git a/paddle/phi/kernels/funcs/activation_functor.h b/paddle/phi/kernels/funcs/activation_functor.h index 926920dfa93dd..e0ee4ea7d7a2b 100644 --- a/paddle/phi/kernels/funcs/activation_functor.h +++ b/paddle/phi/kernels/funcs/activation_functor.h @@ -2051,6 +2051,25 @@ struct LogSigmoidGradFunctor : public BaseActivationFunctor { static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } }; +template +struct LogSigmoidGradFunctor> + : public BaseActivationFunctor> { + template + void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { + auto temp = + (-x).cwiseMax(static_cast>(0)); // temp = max(-x, 0) + dx.device(d) = + dout * ((-x - temp).exp() / ((-temp).exp() + (-x - temp).exp())) + .unaryExpr(Conj()); + } + + static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } +}; + template struct HardSigmoidFunctor : public BaseActivationFunctor { float slope; @@ -3862,6 +3881,28 @@ struct CudaLogSigmoidGradFunctor : public BaseActivationFunctor { static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } }; +template +struct CudaLogSigmoidGradFunctor> + : public BaseActivationFunctor> { + ComplexType zero = static_cast>(0.0f); + + // dx = dout * exp(-x) / (1 + exp(-x)) + // For numerical stability: + // dx = dout * exp(-x - max(-x, 0)) / (exp(-max(-x, 0)) + exp(-x - max(-x, + // 0))) + __device__ __forceinline__ ComplexType operator()( + const ComplexType arg_dout, const ComplexType arg_x) const { + ComplexType dout = static_cast>(arg_dout); + ComplexType x = static_cast>(arg_x); + ComplexType temp1 = x > zero ? zero : -x; + ComplexType temp2 = exp(-x - temp1); + return static_cast>(dout * + conj(temp2 / (exp(-temp1) + temp2))); + } + + static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } +}; + template struct CudaHardSigmoidFunctor : public BaseActivationFunctor { T zero = static_cast(0.0f); diff --git a/paddle/phi/kernels/gpu/activation_grad_kernel.cu b/paddle/phi/kernels/gpu/activation_grad_kernel.cu index ea4a88683c0bf..43460eb10a411 100644 --- a/paddle/phi/kernels/gpu/activation_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/activation_grad_kernel.cu @@ -495,7 +495,8 @@ PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_grad, SigmoidGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_double_grad, SigmoidDoubleGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(sigmoid_triple_grad, SigmoidTripleGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(hardsigmoid_grad, HardSigmoidGradKernel) -PD_REGISTER_ACTIVATION_GRAD_KERNEL(logsigmoid_grad, LogSigmoidGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL_WITH_COMPLEX(logsigmoid_grad, + LogSigmoidGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log_grad, LogGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log2_grad, Log2GradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(log10_grad, Log10GradKernel) diff --git a/paddle/phi/kernels/gpu/activation_kernel.cu b/paddle/phi/kernels/gpu/activation_kernel.cu index d29df57589315..aefe04385d7f7 100644 --- a/paddle/phi/kernels/gpu/activation_kernel.cu +++ b/paddle/phi/kernels/gpu/activation_kernel.cu @@ -290,7 +290,7 @@ PD_REGISTER_ACTIVATION_KERNEL(elu, EluKernel) PD_REGISTER_ACTIVATION_KERNEL(silu, SiluKernel) PD_REGISTER_ACTIVATION_KERNEL(softsign, SoftsignKernel) PD_REGISTER_ACTIVATION_KERNEL(sigmoid, SigmoidKernel) -PD_REGISTER_ACTIVATION_KERNEL(logsigmoid, LogSigmoidKernel) +PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX(logsigmoid, LogSigmoidKernel) PD_REGISTER_ACTIVATION_KERNEL(hardsigmoid, HardSigmoidKernel) PD_REGISTER_ACTIVATION_KERNEL(hardswish, HardSwishKernel) PD_REGISTER_ACTIVATION_KERNEL(swish, SwishKernel) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index aacc0232be2f3..131201b1bc60c 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -790,7 +790,7 @@ def log_sigmoid(x, name=None): log\_sigmoid(x) = log \frac{1}{1 + e^{-x}} Parameters: - x (Tensor): The input Tensor with data type float32, float64. + x (Tensor): The input Tensor with data type float32, float64, complex64, complex128. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: @@ -813,7 +813,10 @@ def log_sigmoid(x, name=None): return _C_ops.logsigmoid(x) else: check_variable_and_dtype( - x, 'x', ['float16', 'float32', 'float64'], 'log_sigmoid' + x, + 'x', + ['float16', 'float32', 'float64', 'complex64', 'complex128'], + 'log_sigmoid', ) helper = LayerHelper("log_sigmoid", **locals()) out = helper.create_variable_for_type_inference(x.dtype) diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index 144b7fdcaa4e5..a0bb12264d61d 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -464,7 +464,13 @@ def setUp(self): self.init_shape() np.random.seed(2048) - x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) + if self.dtype is np.complex64 or self.dtype is np.complex128: + x = ( + np.random.uniform(-1, 1, self.shape) + + 1j * np.random.uniform(-1, 1, self.shape) + ).astype(self.dtype) + else: + x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.log(1 / (1 + np.exp(-x))) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} self.outputs = {'Out': out} @@ -477,6 +483,16 @@ def test_check_grad(self): self.check_grad(['X'], 'Out', max_relative_error=0.008) +class TestLogSigmoidComplex64(TestLogSigmoid): + def init_dtype(self): + self.dtype = np.complex64 + + +class TestLogSigmoidComplex128(TestLogSigmoid): + def init_dtype(self): + self.dtype = np.complex128 + + class TestLogSigmoid_ZeroDim(TestLogSigmoid): def init_shape(self): self.shape = [] From 5ed7c8a01f0b007f2894ae64649e68c1cf5de30c Mon Sep 17 00:00:00 2001 From: zyfncg Date: Thu, 7 Sep 2023 10:39:15 +0800 Subject: [PATCH 172/194] Disable ConstantFoldingPass in pattern_rewrite_test (#57047) --- test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index 6dc24d09d78de..a86055523b521 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -1114,7 +1114,7 @@ TEST(pattern_rewrite, Patterns) { ir::PassManager pm(ctx); pm.AddPass(std::make_unique()); - pm.AddPass(ir::CreateConstantFoldingPass()); + // pm.AddPass(ir::CreateConstantFoldingPass()); pm.AddPass(ir::CreateDeadCodeEliminationPass()); pm.AddPass(ir::CreateReorderBlockOpsPass()); pm.EnablePassTiming(); From 1d4e938da987c91ece12197c7defc1c8ed2f1e6d Mon Sep 17 00:00:00 2001 From: Ruibin Cheung Date: Thu, 7 Sep 2023 10:58:55 +0800 Subject: [PATCH 173/194] [complex] add complex support for silu (#56903) --- paddle/phi/common/complex.h | 10 ++++++ .../phi/kernels/cpu/activation_grad_kernel.cc | 2 +- paddle/phi/kernels/cpu/activation_kernel.cc | 2 +- paddle/phi/kernels/funcs/activation_functor.h | 36 +++++++++++++++++++ .../phi/kernels/gpu/activation_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/activation_kernel.cu | 2 +- python/paddle/nn/functional/activation.py | 14 ++++++-- test/legacy_test/test_activation_op.py | 21 ++++++++++- 8 files changed, 82 insertions(+), 7 deletions(-) diff --git a/paddle/phi/common/complex.h b/paddle/phi/common/complex.h index e0ff7f11ac542..c50b0f5c9b43b 100644 --- a/paddle/phi/common/complex.h +++ b/paddle/phi/common/complex.h @@ -496,6 +496,16 @@ HOSTDEVICE inline complex log(const complex& a) { #endif } +template +HOSTDEVICE inline complex exp(const complex& a) { +#if defined(PADDLE_WITH_CUDA_OR_HIP_COMPLEX) && \ + (defined(__CUDA_ARCH__) || defined(__HIPCC__)) + return complex(thrust::exp(thrust::complex(a))); +#else + return complex(std::exp(std::complex(a))); +#endif +} + template inline std::ostream& operator<<(std::ostream& os, const complex& a) { os << "real:" << a.real << " imag:" << a.imag; diff --git a/paddle/phi/kernels/cpu/activation_grad_kernel.cc b/paddle/phi/kernels/cpu/activation_grad_kernel.cc index 68f3fce76a8dc..b3203332ec7d1 100644 --- a/paddle/phi/kernels/cpu/activation_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/activation_grad_kernel.cc @@ -301,7 +301,7 @@ PD_REGISTER_ACTIVATION_GRAD_KERNEL(softshrink_grad, SoftShrinkGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(hard_shrink_grad, HardShrinkGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(tanh_shrink_grad, TanhShrinkGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(elu_grad, EluGradKernel) -PD_REGISTER_ACTIVATION_GRAD_KERNEL(silu_grad, SiluGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL_WITH_COMPLEX(silu_grad, SiluGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(mish_grad, MishGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(stanh_grad, STanhGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(reciprocal_grad, ReciprocalGradKernel) diff --git a/paddle/phi/kernels/cpu/activation_kernel.cc b/paddle/phi/kernels/cpu/activation_kernel.cc index b3f86c7c908db..8a554470dea39 100644 --- a/paddle/phi/kernels/cpu/activation_kernel.cc +++ b/paddle/phi/kernels/cpu/activation_kernel.cc @@ -195,7 +195,7 @@ PD_REGISTER_ACTIVATION_KERNEL(hard_shrink, HardShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(softshrink, SoftShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(tanh_shrink, TanhShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(elu, EluKernel) -PD_REGISTER_ACTIVATION_KERNEL(silu, SiluKernel) +PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX(silu, SiluKernel) PD_REGISTER_ACTIVATION_KERNEL(mish, MishKernel) PD_REGISTER_ACTIVATION_KERNEL(stanh, STanhKernel) PD_REGISTER_ACTIVATION_KERNEL(reciprocal, ReciprocalKernel) diff --git a/paddle/phi/kernels/funcs/activation_functor.h b/paddle/phi/kernels/funcs/activation_functor.h index e0ee4ea7d7a2b..dd6dbde75ad69 100644 --- a/paddle/phi/kernels/funcs/activation_functor.h +++ b/paddle/phi/kernels/funcs/activation_functor.h @@ -1847,6 +1847,25 @@ struct SiluGradFunctor : public BaseActivationFunctor { static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } }; +template +struct SiluGradFunctor> + : public BaseActivationFunctor> { + template + void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { + auto temp1 = static_cast>(1) + (-x).exp(); // 1+e^(-x) + auto temp2 = x * (-x).exp(); // x*e^(-x) + dx.device(d) = dout * ((static_cast>(1) / temp1) * + (static_cast>(1) + (temp2 / temp1))) + .unaryExpr(Conj()); + } + + static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } +}; + template struct SoftsignFunctor : public BaseActivationFunctor { template @@ -3793,6 +3812,23 @@ struct CudaSiluGradFunctor : public BaseActivationFunctor { static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } }; +template +struct CudaSiluGradFunctor> + : public BaseActivationFunctor> { + ComplexType one = static_cast>(1.0f); + + // dx = dout * (1 + exp(-x) + x * exp(-x) / (1 + exp(-x))^2) + __device__ __forceinline__ ComplexType operator()( + const ComplexType arg_dout, const ComplexType arg_x) const { + ComplexType dout = static_cast>(arg_dout); + ComplexType x = static_cast>(arg_x); + ComplexType temp = one / (one + exp(-x)); + return dout * conj(temp * (one + x * (one - temp))); + } + + static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } +}; + template struct CudaSoftsignFunctor : public BaseActivationFunctor { T one = static_cast(1.0f); diff --git a/paddle/phi/kernels/gpu/activation_grad_kernel.cu b/paddle/phi/kernels/gpu/activation_grad_kernel.cu index 43460eb10a411..a0695935de1bc 100644 --- a/paddle/phi/kernels/gpu/activation_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/activation_grad_kernel.cu @@ -403,7 +403,7 @@ PD_REGISTER_KERNEL(exp_grad, PD_REGISTER_ACTIVATION_GRAD_KERNEL(softshrink_grad, SoftShrinkGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(hard_shrink_grad, HardShrinkGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(tanh_shrink_grad, TanhShrinkGradKernel) -PD_REGISTER_ACTIVATION_GRAD_KERNEL(silu_grad, SiluGradKernel) +PD_REGISTER_ACTIVATION_GRAD_KERNEL_WITH_COMPLEX(silu_grad, SiluGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(elu_grad, EluGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(elu_double_grad, EluDoubleGradKernel) PD_REGISTER_ACTIVATION_GRAD_KERNEL(logit_grad, LogitCUDAGradKernel) diff --git a/paddle/phi/kernels/gpu/activation_kernel.cu b/paddle/phi/kernels/gpu/activation_kernel.cu index aefe04385d7f7..061a02f531538 100644 --- a/paddle/phi/kernels/gpu/activation_kernel.cu +++ b/paddle/phi/kernels/gpu/activation_kernel.cu @@ -287,7 +287,7 @@ PD_REGISTER_ACTIVATION_KERNEL(hard_shrink, HardShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(softshrink, SoftShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(tanh_shrink, TanhShrinkKernel) PD_REGISTER_ACTIVATION_KERNEL(elu, EluKernel) -PD_REGISTER_ACTIVATION_KERNEL(silu, SiluKernel) +PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX(silu, SiluKernel) PD_REGISTER_ACTIVATION_KERNEL(softsign, SoftsignKernel) PD_REGISTER_ACTIVATION_KERNEL(sigmoid, SigmoidKernel) PD_REGISTER_ACTIVATION_KERNEL_WITH_COMPLEX(logsigmoid, LogSigmoidKernel) diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 131201b1bc60c..621a06ea8e1fc 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -1034,7 +1034,7 @@ def silu(x, name=None): Where :math:`x` is the input Tensor. Parameters: - x (Tensor): The input Tensor with data type bfloat16, float16, float32, float64. + x (Tensor): The input Tensor with data type bfloat16, float16, float32, float64, complex64, complex128. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: @@ -1057,7 +1057,17 @@ def silu(x, name=None): return _C_ops.silu(x) else: check_variable_and_dtype( - x, 'x', ['float16', 'uint16', 'float32', 'float64'], 'silu' + x, + 'x', + [ + 'float16', + 'uint16', + 'float32', + 'float64', + 'complex64', + 'complex128', + ], + 'silu', ) helper = LayerHelper("silu", **locals()) out = helper.create_variable_for_type_inference(x.dtype) diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index a0bb12264d61d..e9c6ef7b1985d 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -384,6 +384,11 @@ def setUp(self): np.random.seed(1024) x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) + if self.dtype == np.complex64 or self.dtype == np.complex128: + x = ( + np.random.uniform(-1, 1, self.shape) + + 1j * np.random.uniform(-1, 1, self.shape) + ).astype(self.dtype) out = x / (np.exp(-x) + 1) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} self.outputs = {'Out': out} @@ -397,7 +402,11 @@ def if_enable_cinn(self): pass def test_check_grad(self): - self.check_grad(['X'], 'Out', check_prim=True) + # TODO(BeingGod): set `check_prim=True` when `fill_constant` supports `complex` dtype + if self.dtype == np.complex64 or self.dtype == np.complex128: + self.check_grad(['X'], 'Out', check_prim=False) + else: + self.check_grad(['X'], 'Out', check_prim=True) class TestSilu_ZeroDim(TestSilu): @@ -405,6 +414,16 @@ def init_shape(self): self.shape = [] +class TestSilu_Complex64(TestSilu): + def init_dtype(self): + self.dtype = np.complex64 + + +class TestSilu_Complex128(TestSilu): + def init_dtype(self): + self.dtype = np.complex128 + + class TestSiluAPI(unittest.TestCase): # test paddle.nn.Silu, paddle.nn.functional.silu def setUp(self): From 3ce7eb0f3a0104bbcac4d8f55d8e0dc8c593fe69 Mon Sep 17 00:00:00 2001 From: Shaopeng Ling Date: Thu, 7 Sep 2023 11:57:43 +0800 Subject: [PATCH 174/194] fix doc typo and format (#57018) * fix doc typo * fix doc typo and missing part --- python/paddle/nn/layer/container.py | 2 +- python/paddle/nn/layer/norm.py | 7 +++++-- python/paddle/tensor/creation.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/paddle/nn/layer/container.py b/python/paddle/nn/layer/container.py index 750e65e349d74..8fa65aa8f340e 100644 --- a/python/paddle/nn/layer/container.py +++ b/python/paddle/nn/layer/container.py @@ -303,7 +303,7 @@ class ParameterList(Layer): This container acts like a Python list, but parameters it contains will be properly added. Parameters: - parameters (iterable, optional): Iterable Parameters to be added + parameters (iterable, optional): Iterable Parameters to be added. Examples: .. code-block:: python diff --git a/python/paddle/nn/layer/norm.py b/python/paddle/nn/layer/norm.py index e01e426a75ff2..bea68c61d79a9 100644 --- a/python/paddle/nn/layer/norm.py +++ b/python/paddle/nn/layer/norm.py @@ -894,8 +894,8 @@ class BatchNorm(Layer): - :math:`x` : mini-batch data - :math:`m` : the size of the mini-batch data - When use_global_stats = True, the :math:`\\mu_{\\beta}` - and :math:`\\sigma_{\\beta}^{2}` are not the statistics of one mini-batch. + When use_global_stats = True, the :math:`\mu_{\beta}` + and :math:`\sigma_{\beta}^{2}` are not the statistics of one mini-batch. They are global or running statistics (moving_mean and moving_variance). It usually got from the pre-trained model. Calculated as follows: @@ -1181,6 +1181,9 @@ class BatchNorm1D(_BatchNormBase): \sigma_{\beta}^{2} &\gets \frac{1}{m} \sum_{i=1}^{m}(x_i - \ \mu_{\beta})^2 \qquad &//\ mini-batch\ variance \\ + - :math:`x` : mini-batch data + - :math:`m` : the size of the mini-batch data + When use_global_stats = True, the :math:`\mu_{\beta}` and :math:`\sigma_{\beta}^{2}` are not the statistics of one mini-batch. They are global or running statistics (moving_mean and moving_variance). It usually got from the diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 528a3c40b9a4b..1ad7c82c39a37 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -1065,7 +1065,7 @@ def zeros(shape, dtype=None, name=None): If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list. dtype(np.dtype|str, optional): Data type of output Tensor, it supports - bool, float16, float32, float64, int32 and int64. Default: if None, the date type is float32. + bool, float16, float32, float64, int32 and int64. Default: if None, the data type is float32. name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. From d78cbee7bfbe08f1fa692c1f1f836d038a0ae76c Mon Sep 17 00:00:00 2001 From: Ruibin Cheung Date: Thu, 7 Sep 2023 12:57:21 +0800 Subject: [PATCH 175/194] =?UTF-8?q?=E3=80=90complex=20op=E3=80=91No.18=20a?= =?UTF-8?q?dd=20complex=20support=20for=20silu=20(#57058)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- paddle/phi/common/complex.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/paddle/phi/common/complex.h b/paddle/phi/common/complex.h index c50b0f5c9b43b..e0ff7f11ac542 100644 --- a/paddle/phi/common/complex.h +++ b/paddle/phi/common/complex.h @@ -496,16 +496,6 @@ HOSTDEVICE inline complex log(const complex& a) { #endif } -template -HOSTDEVICE inline complex exp(const complex& a) { -#if defined(PADDLE_WITH_CUDA_OR_HIP_COMPLEX) && \ - (defined(__CUDA_ARCH__) || defined(__HIPCC__)) - return complex(thrust::exp(thrust::complex(a))); -#else - return complex(std::exp(std::complex(a))); -#endif -} - template inline std::ostream& operator<<(std::ostream& os, const complex& a) { os << "real:" << a.real << " imag:" << a.imag; From 31c2b9dcc720f8e6377682f6e754646e091514fc Mon Sep 17 00:00:00 2001 From: Chen Zhiyang <1792266893@qq.com> Date: Thu, 7 Sep 2023 14:04:22 +0800 Subject: [PATCH 176/194] Vjp auto gen StrAttribute bug fixed (#56971) * fix StrAttribute vjp gen bug * add dropout to list * fix bug * fix bug --- .../ir/dialect/op_generator/op_interface_gen.py | 13 ++++++++++++- .../op_generator/vjp_interface_gen_op_list.py | 2 ++ paddle/fluid/primitive/codegen/gen.py | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py b/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py index 5f72a2efbc1d6..2490335f6c3fb 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_interface_gen.py @@ -45,7 +45,7 @@ }}""" OP_VJP_ATTRIBUTE_TEMPLATE = """ - {attr_type} {attr_name} = op->attribute("{attr_name}").dyn_cast<{attr_parse_type}>().data();""" + {attr_type} {attr_name} = op->attribute("{attr_name}").dyn_cast<{attr_parse_type}>().{func}();""" OP_VJP_ATTRIBUTE_DEFAULT_TEMPLATE = """ {attr_type} {attr_name} = {default_value};""" @@ -92,6 +92,10 @@ 'ir::VectorType': 'Tensor[]', } +attr_data_map = { + 'ir::StrAttribute': 'AsString', +} + def gen_op_vjp_str( op_class_name, @@ -155,10 +159,17 @@ def gen_op_vjp_str( ) ) else: + func = 'data' + if ( + op_grad_info.attribute_type_list[idx] + in attr_data_map.keys() + ): + func = attr_data_map[op_grad_info.attribute_type_list[idx]] attribute_code += OP_VJP_ATTRIBUTE_TEMPLATE.format( attr_type=op_grad_info.attribute_gen_arg_type_list[idx], attr_name=op_attribute_list[idx], attr_parse_type=op_grad_info.attribute_type_list[idx], + func=func, ) else: diff --git a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py index cb130ae0b2365..9707d6fb5f9a2 100644 --- a/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py +++ b/paddle/fluid/ir/dialect/op_generator/vjp_interface_gen_op_list.py @@ -37,6 +37,7 @@ "subtract", "pow", "rsqrt", + "dropout", ] vjp_interface_implementation_gen_op_list = [ "tanh", @@ -52,4 +53,5 @@ "subtract", "pow", "rsqrt", + "dropout", ] diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 89ba4fe53cdf0..722ed94953d19 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -61,6 +61,7 @@ 'rsqrt_grad', 'slice_grad', 'transpose_grad', + 'dropout_grad', ] VJP_COMPS = ['divide_grad', 'sum_grad'] BACKENDS = [ @@ -127,6 +128,7 @@ 'roll', 'scatter', 'scatter_nd_add', + 'dropout_grad', ] From 30c9dad15733ae1cc8950b319951d3f9b8fecbaf Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Thu, 7 Sep 2023 14:33:26 +0800 Subject: [PATCH 177/194] refine add_n (#57004) --- paddle/fluid/ir_adaptor/translator/op_translator.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 86f31fd92ae90..39a6acdd21b55 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -1112,8 +1112,7 @@ struct AddNOpTranscriber : public OpTranscriber { } const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name); if (!op_info) { - IR_THROW("Op assign_value should have corresponding OpInfo %s", - target_op_name); + IR_THROW("Op add_n should have corresponding OpInfo %s", target_op_name); } return op_info; From 8c75039cd75f1df0b02758a5a9c89fca7199550c Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Thu, 7 Sep 2023 14:35:51 +0800 Subject: [PATCH 178/194] refine stride flag (#57005) * refine stride flag --- paddle/phi/core/kernel_factory.cc | 36 ++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/paddle/phi/core/kernel_factory.cc b/paddle/phi/core/kernel_factory.cc index d58decadfadca..2e85d521c516f 100644 --- a/paddle/phi/core/kernel_factory.cc +++ b/paddle/phi/core/kernel_factory.cc @@ -14,6 +14,10 @@ #include "paddle/phi/core/kernel_factory.h" +#include +#include +#include + #include "glog/logging.h" #include "paddle/phi/core/enforce.h" #include "paddle/utils/flags.h" @@ -33,6 +37,10 @@ PHI_DEFINE_EXPORTED_bool(use_stride_kernel, true, "Whether to use strdie kernel if op support stride."); +PHI_DEFINE_EXPORTED_string(stride_kernel_blacklist, + "", + "It controls the strided kernel subset do not use."); + PD_DECLARE_int32(low_precision_op_list); PD_DECLARE_bool(enable_api_kernel_fallback); PD_DECLARE_bool(run_kp_kernel); @@ -226,14 +234,26 @@ KernelResult KernelFactory::SelectKernelOrThrowError( phi::errors::NotFound("The kernel `%s` is not registered.", kernel_name)); if (FLAGS_use_stride_kernel && use_strided_kernel) { - auto stride_kernel_iter = iter->second.find( - {const_kernel_key.backend() == paddle::experimental::Backend::GPUDNN - ? paddle::experimental::Backend::GPU - : const_kernel_key.backend(), - phi::DataLayout::STRIDED, - const_kernel_key.dtype()}); - if (stride_kernel_iter != iter->second.end()) { - return {stride_kernel_iter->second, false, true}; + std::regex reg(","); + std::unordered_set elems{ + std::sregex_token_iterator(FLAGS_stride_kernel_blacklist.begin(), + FLAGS_stride_kernel_blacklist.end(), + reg, + -1), + std::sregex_token_iterator()}; + elems.erase(""); + + if (!elems.count(kernel_name)) { + auto stride_kernel_iter = iter->second.find( + {const_kernel_key.backend() == paddle::experimental::Backend::GPUDNN + ? paddle::experimental::Backend::GPU + : const_kernel_key.backend(), + phi::DataLayout::STRIDED, + const_kernel_key.dtype()}); + if (stride_kernel_iter != iter->second.end()) { + VLOG(1) << "use strided kernel, kernel_name = " << kernel_name; + return {stride_kernel_iter->second, false, true}; + } } } From ca9045f7f05ef75ae77858ceb75f56cd01da4de7 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 7 Sep 2023 15:04:07 +0800 Subject: [PATCH 179/194] [NewIR] optimize GetNameById in check_gc (#56964) * optimize GetNameById in check_gc * remove debug code * fix * fix --- .../new_executor/interpreter/interpreter_util.cc | 10 ++++++++++ .../new_executor/interpreter/interpreter_util.h | 2 ++ .../framework/new_executor/new_ir_interpreter.cc | 12 +++++++----- .../framework/new_executor/new_ir_interpreter.h | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc index be87549ecbfbc..9d114ed0c19f7 100644 --- a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc +++ b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc @@ -1218,6 +1218,16 @@ std::unordered_set GetSpecialOpNames() { }; } +void BuildId2VarName(const std::map& var_name_2_id, + std::unordered_map* id_2_var_name) { + PADDLE_ENFORCE_NOT_NULL( + id_2_var_name, + phi::errors::InvalidArgument("id2_var_name can not be null")); + for (auto [var_name, id] : var_name_2_id) { + id_2_var_name->insert({id, var_name}); + } +} + } // namespace interpreter } // namespace framework } // namespace paddle diff --git a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h index 186f9459fbac7..33b89cac542d4 100644 --- a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h +++ b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.h @@ -116,6 +116,8 @@ void BuildOpFuncList( void BuildVariableScope(const framework::BlockDesc& block, const ExecutionConfig& execution_config, VariableScope* var_scope); +void BuildId2VarName(const std::map& var_name_2_id, + std::unordered_map* id_2_var_name); void LogDeviceMemoryStats(const platform::Place& place); diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index e026e914adb57..37cecdad1308e 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -194,11 +194,10 @@ std::string NewIRInterpreter::GetNameById(int id) const { // typically when the target variable is not existed in the original program // desc, but created by interpretercore. // For example, created and used by d2h_copy or h2d_copy operator. - auto it = std::find_if(var_name_2_id_.begin(), - var_name_2_id_.end(), - [id](const auto& pair) { return pair.second == id; }); - if (it != var_name_2_id_.end()) { - return it->first; + + auto it = id_2_var_name_.find(id); + if (it != id_2_var_name_.end()) { + return it->second; } return ""; } @@ -925,6 +924,9 @@ FetchList NewIRInterpreter::Run(const std::vector& feed_names, &variable_2_var_name_, &var_name_2_id_, &variable_list_); + + interpreter::BuildId2VarName(var_name_2_id_, &id_2_var_name_); + VLOG(4) << "Done BuildScope"; VLOG(4) << DebugValueInfo(); diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.h b/paddle/fluid/framework/new_executor/new_ir_interpreter.h index 841e9136a2ecc..b37b26d107560 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.h +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.h @@ -208,6 +208,7 @@ class NewIRInterpreter : public InterpreterBaseImpl { variable_2_var_name_; std::map var_name_2_id_; + std::unordered_map id_2_var_name_; std::vector variable_list_; From 0d845ada7e4d6fe5ca03eb5cf2a29392df32ab3b Mon Sep 17 00:00:00 2001 From: Lylinnnnn <94297888+Lylinnnnn@users.noreply.github.com> Date: Thu, 7 Sep 2023 15:06:05 +0800 Subject: [PATCH 180/194] update readme.md (#57062) --- README.md | 2 +- README_cn.md | 2 +- README_ja.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 69aeacd857f21..93dadc0cabe8e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ English | [简体中文](./README_cn.md) | [日本語](./README_ja.md) Welcome to the PaddlePaddle GitHub. PaddlePaddle, as the first independent R&D deep learning platform in China, has been officially open-sourced to professional communities since 2016. It is an industrial platform with advanced technologies and rich features that cover core deep learning frameworks, basic model libraries, end-to-end development kits, tools & components as well as service platforms. -PaddlePaddle is originated from industrial practices with dedication and commitments to industrialization. It has been widely adopted by a wide range of sectors including manufacturing, agriculture, enterprise service, and so on while serving more than 5.35 million developers, 200,000 companies and generating 670,000 models. With such advantages, PaddlePaddle has helped an increasing number of partners commercialize AI. +PaddlePaddle is originated from industrial practices with dedication and commitments to industrialization. It has been widely adopted by a wide range of sectors including manufacturing, agriculture, enterprise service, and so on while serving more than 8 million developers, 220,000 companies and generating 800,000 models. With such advantages, PaddlePaddle has helped an increasing number of partners commercialize AI. ## Installation diff --git a/README_cn.md b/README_cn.md index b469f7f00a3d9..1041faae39ddc 100644 --- a/README_cn.md +++ b/README_cn.md @@ -14,7 +14,7 @@ 欢迎来到 PaddlePaddle GitHub -飞桨(PaddlePaddle)以百度多年的深度学习技术研究和业务应用为基础,是中国首个自主研发、功能完备、 开源开放的产业级深度学习平台,集深度学习核心训练和推理框架、基础模型库、端到端开发套件和丰富的工具组件于一体。目前,飞桨累计开发者535万,服务企业20万家,基于飞桨开源深度学习平台产生了67万个模型。飞桨助力开发者快速实现AI想法,快速上线AI业务。帮助越来越多的行业完成AI赋能,实现产业智能化升级。 +飞桨(PaddlePaddle)以百度多年的深度学习技术研究和业务应用为基础,是中国首个自主研发、功能完备、 开源开放的产业级深度学习平台,集深度学习核心训练和推理框架、基础模型库、端到端开发套件和丰富的工具组件于一体。目前,飞桨累计开发者800万,服务企业22万家,基于飞桨开源深度学习平台产生了80万个模型。飞桨助力开发者快速实现AI想法,快速上线AI业务。帮助越来越多的行业完成AI赋能,实现产业智能化升级。 ## 安装 diff --git a/README_ja.md b/README_ja.md index fe0d7852e0b31..22c78a1a79bbd 100644 --- a/README_ja.md +++ b/README_ja.md @@ -15,7 +15,7 @@ PaddlePaddle GitHub へようこそ。 PaddlePaddle は中国初の独立系 R&D ディープラーニングプラットフォームとして、2016年からプロのコミュニティに正式にオープンソース化されました。コアとなる深層学習フレームワーク、基本モデルライブラリ、エンドツーエンドの開発キット、ツール&コンポーネント、さらにサービスプラットフォームを網羅する、高度な技術と豊富な機能を備えた産業プラットフォームです。 -PaddlePaddle は、工業化に対するコミットメントを持つ工業的実践から生まれたものです。製造業、農業、企業サービスなど幅広い分野で採用され、535万人以上の開発者、20万以上の企業、67万以上のモデルを生み出しています。それにより PaddlePaddle は、ますます多くのパートナーの AI 商用化を支援しています。 +PaddlePaddle は、工業化に対するコミットメントを持つ工業的実践から生まれたものです。製造業、農業、企業サービスなど幅広い分野で採用され、800万人以上の開発者、22万以上の企業、80万以上のモデルを生み出しています。それにより PaddlePaddle は、ますます多くのパートナーの AI 商用化を支援しています。 ## インストール From 2fbebfd87839a9512f8b82fe2e012aa756ab3012 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Thu, 7 Sep 2023 16:41:19 +0800 Subject: [PATCH 181/194] refine save numpy (#57040) * refine save numpy --- python/paddle/framework/io.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/python/paddle/framework/io.py b/python/paddle/framework/io.py index cc9ed4768ced9..4b8440956fadb 100644 --- a/python/paddle/framework/io.py +++ b/python/paddle/framework/io.py @@ -62,7 +62,7 @@ def _build_saved_state_dict(state_dict): raise ValueError( "The saved tensor is not initialized. If you used group sharded, please use save_group_sharded_model." ) - save_dict[key] = np.array(value) + save_dict[key] = np.array(value.cpu()) name_table[key] = value.name else: save_dict[key] = value @@ -91,7 +91,9 @@ def _load_state_dict_from_save_inference_model(model_path, config): # 3. construct state_dict load_param_dict = {} for var_name in persistable_var_dict: - load_param_dict[var_name] = np.array(persistable_var_dict[var_name]) + load_param_dict[var_name] = np.array( + persistable_var_dict[var_name].cpu() + ) # if *.info exists, we can recover structured_name var_info_filename = str(config.params_filename) + ".info" @@ -145,7 +147,7 @@ def _load_state_dict_from_save_params(model_path): # 3. construct state_dict load_param_dict = {} for var in load_var_list: - load_param_dict[var.name] = np.array(var) + load_param_dict[var.name] = np.array(var.cpu()) return load_param_dict @@ -290,13 +292,15 @@ def _pickle_save(obj, f, protocol): ) def reduce_varbase(self): - data = np.array(self) + data = np.array(self.cpu()) name = self.name return (tuple, ((name, data),)) def reduce_LoDTensor(self): - data = np.array(self) + p = core.Place() + p.set_place(paddle.CPUPlace()) + data = np.array(self._copy(p)) return (eval, ('data', {'data': data})) @@ -1108,7 +1112,9 @@ def load(path, **configs): try: tensor, _ = _load_lod_tensor(path) if config.return_numpy: - return np.array(tensor) + p = core.Place() + p.set_place(paddle.CPUPlace()) + return np.array(tensor._copy(p)) else: if in_dygraph_mode(): return _lod_tensor2varbase(tensor) From 98baedafded479d2d24fe6e53f4193b67cd5dab2 Mon Sep 17 00:00:00 2001 From: cyberslack_lee Date: Thu, 7 Sep 2023 16:43:17 +0800 Subject: [PATCH 182/194] [clang-tidy] NO.25 modernize-deprecated-headers(#56994) --- .clang-tidy | 2 +- paddle/fluid/framework/data_feed_factory.cc | 2 +- paddle/fluid/framework/device_worker_factory.cc | 2 +- .../ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc | 2 +- paddle/fluid/framework/lod_tensor.cc | 2 +- paddle/fluid/framework/pull_dense_worker.cc | 2 +- paddle/fluid/framework/section_worker.cc | 2 +- paddle/fluid/framework/trainer_factory.cc | 2 +- paddle/fluid/imperative/data_loader.cc | 2 +- paddle/fluid/memory/allocation/cpu_allocator.cc | 2 +- paddle/fluid/memory/allocation/cuda_ipc_allocator.cc | 2 +- paddle/fluid/memory/allocation/mmap_allocator.cc | 2 +- paddle/fluid/operators/detection/generate_mask_labels_op.cc | 2 +- paddle/fluid/operators/detection/mask_util.cc | 4 ++-- paddle/fluid/operators/save_op.cc | 2 +- paddle/fluid/platform/gen_comm_id_helper.cc | 2 +- paddle/fluid/platform/profiler/event_node.cc | 2 +- paddle/fluid/prim/api/manual_prim/static_prim_api.cc | 2 +- paddle/phi/kernels/cpu/lstsq_kernel.cc | 2 +- paddle/phi/kernels/cpu/trunc_kernel.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/adam.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/adamw.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/embseqpool.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/gru.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/lstm.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/matmul.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/sgd.cc | 2 +- 27 files changed, 28 insertions(+), 28 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 9721281764494..21c10cfeaab0d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -167,7 +167,7 @@ misc-unused-alias-decls, misc-unused-using-decls, modernize-avoid-bind, modernize-avoid-c-arrays, --modernize-deprecated-headers, +modernize-deprecated-headers, -modernize-deprecated-ios-base-aliases, modernize-loop-convert, modernize-make-shared, diff --git a/paddle/fluid/framework/data_feed_factory.cc b/paddle/fluid/framework/data_feed_factory.cc index e058b19469000..88afa021b7c1b 100644 --- a/paddle/fluid/framework/data_feed_factory.cc +++ b/paddle/fluid/framework/data_feed_factory.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/framework/data_feed_factory.h" -#include +#include #include #include diff --git a/paddle/fluid/framework/device_worker_factory.cc b/paddle/fluid/framework/device_worker_factory.cc index ae01f622effa8..5c920fa3e318f 100644 --- a/paddle/fluid/framework/device_worker_factory.cc +++ b/paddle/fluid/framework/device_worker_factory.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/framework/device_worker_factory.h" -#include +#include #include #include diff --git a/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc b/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc index 745e0ffa9cff8..eee24e01a328b 100644 --- a/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.cc @@ -14,7 +14,7 @@ #include "paddle/fluid/framework/ir/mkldnn/compute_propagate_scales_mkldnn_pass.h" -#include +#include #include diff --git a/paddle/fluid/framework/lod_tensor.cc b/paddle/fluid/framework/lod_tensor.cc index 9e75f94157244..38ba51095adac 100644 --- a/paddle/fluid/framework/lod_tensor.cc +++ b/paddle/fluid/framework/lod_tensor.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/framework/lod_tensor.h" -#include +#include #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/version.h" diff --git a/paddle/fluid/framework/pull_dense_worker.cc b/paddle/fluid/framework/pull_dense_worker.cc index a51d8c38b3342..db8506e9ec5c9 100644 --- a/paddle/fluid/framework/pull_dense_worker.cc +++ b/paddle/fluid/framework/pull_dense_worker.cc @@ -11,7 +11,7 @@ distributed under the License is distributed on an "AS IS" BASIS, 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. */ -#include +#include #include "paddle/fluid/framework/device_worker.h" diff --git a/paddle/fluid/framework/section_worker.cc b/paddle/fluid/framework/section_worker.cc index 7ee02b1e6d88f..f88dbc409d170 100644 --- a/paddle/fluid/framework/section_worker.cc +++ b/paddle/fluid/framework/section_worker.cc @@ -10,7 +10,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) -#include +#include #include "paddle/fluid/framework/device_worker.h" #include "paddle/fluid/framework/executor_gc_helper.h" diff --git a/paddle/fluid/framework/trainer_factory.cc b/paddle/fluid/framework/trainer_factory.cc index 16aa069a0c33a..ba5dac4830aa1 100644 --- a/paddle/fluid/framework/trainer_factory.cc +++ b/paddle/fluid/framework/trainer_factory.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/framework/trainer_factory.h" -#include +#include #include #include diff --git a/paddle/fluid/imperative/data_loader.cc b/paddle/fluid/imperative/data_loader.cc index f225b871b6860..3e2e96f143277 100644 --- a/paddle/fluid/imperative/data_loader.cc +++ b/paddle/fluid/imperative/data_loader.cc @@ -16,9 +16,9 @@ #include "paddle/fluid/imperative/data_loader.h" -#include #include #include +#include #include diff --git a/paddle/fluid/memory/allocation/cpu_allocator.cc b/paddle/fluid/memory/allocation/cpu_allocator.cc index d24db257494d1..102a27509d67a 100644 --- a/paddle/fluid/memory/allocation/cpu_allocator.cc +++ b/paddle/fluid/memory/allocation/cpu_allocator.cc @@ -14,7 +14,7 @@ #include "paddle/fluid/memory/allocation/cpu_allocator.h" -#include +#include #include "paddle/fluid/memory/stats.h" #include "paddle/fluid/platform/enforce.h" diff --git a/paddle/fluid/memory/allocation/cuda_ipc_allocator.cc b/paddle/fluid/memory/allocation/cuda_ipc_allocator.cc index dff93736a6e70..df62c112681b1 100644 --- a/paddle/fluid/memory/allocation/cuda_ipc_allocator.cc +++ b/paddle/fluid/memory/allocation/cuda_ipc_allocator.cc @@ -17,8 +17,8 @@ #include "paddle/fluid/memory/allocation/cuda_ipc_allocator.h" #include -#include #include +#include #include #include diff --git a/paddle/fluid/memory/allocation/mmap_allocator.cc b/paddle/fluid/memory/allocation/mmap_allocator.cc index e3acb1e8579ac..6be6436b4db7b 100644 --- a/paddle/fluid/memory/allocation/mmap_allocator.cc +++ b/paddle/fluid/memory/allocation/mmap_allocator.cc @@ -17,8 +17,8 @@ #include "paddle/fluid/memory/allocation/mmap_allocator.h" #include -#include #include +#include #include #include diff --git a/paddle/fluid/operators/detection/generate_mask_labels_op.cc b/paddle/fluid/operators/detection/generate_mask_labels_op.cc index e5cb72eed931e..7f3f61f48e4ec 100644 --- a/paddle/fluid/operators/detection/generate_mask_labels_op.cc +++ b/paddle/fluid/operators/detection/generate_mask_labels_op.cc @@ -9,7 +9,7 @@ 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. */ -#include +#include #include #include diff --git a/paddle/fluid/operators/detection/mask_util.cc b/paddle/fluid/operators/detection/mask_util.cc index 4a44f0eb401f1..83bb8cd8ccd69 100644 --- a/paddle/fluid/operators/detection/mask_util.cc +++ b/paddle/fluid/operators/detection/mask_util.cc @@ -14,8 +14,8 @@ limitations under the License. */ #include "paddle/fluid/operators/detection/mask_util.h" -#include -#include +#include +#include #include "paddle/fluid/memory/memory.h" diff --git a/paddle/fluid/operators/save_op.cc b/paddle/fluid/operators/save_op.cc index 2450d64542a78..f025d27807421 100644 --- a/paddle/fluid/operators/save_op.cc +++ b/paddle/fluid/operators/save_op.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/fluid/operators/save_op.h" -#include +#include #include #include diff --git a/paddle/fluid/platform/gen_comm_id_helper.cc b/paddle/fluid/platform/gen_comm_id_helper.cc index 215064b32fc36..3049e30f5ff1d 100644 --- a/paddle/fluid/platform/gen_comm_id_helper.cc +++ b/paddle/fluid/platform/gen_comm_id_helper.cc @@ -19,8 +19,8 @@ limitations under the License. */ #include #include #include -#include #include +#include #include #include diff --git a/paddle/fluid/platform/profiler/event_node.cc b/paddle/fluid/platform/profiler/event_node.cc index aa595869ea85f..1657810fbe666 100644 --- a/paddle/fluid/platform/profiler/event_node.cc +++ b/paddle/fluid/platform/profiler/event_node.cc @@ -11,7 +11,7 @@ limitations under the License. */ #include "paddle/fluid/platform/profiler/event_node.h" -#include +#include #include #include diff --git a/paddle/fluid/prim/api/manual_prim/static_prim_api.cc b/paddle/fluid/prim/api/manual_prim/static_prim_api.cc index 0db4497e6289b..c907be2d10256 100644 --- a/paddle/fluid/prim/api/manual_prim/static_prim_api.cc +++ b/paddle/fluid/prim/api/manual_prim/static_prim_api.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include #include #include #include diff --git a/paddle/phi/kernels/cpu/lstsq_kernel.cc b/paddle/phi/kernels/cpu/lstsq_kernel.cc index 033e2f3e5ce72..3e4782c1a9f0a 100644 --- a/paddle/phi/kernels/cpu/lstsq_kernel.cc +++ b/paddle/phi/kernels/cpu/lstsq_kernel.cc @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include +#include #include #include "paddle/phi/backends/cpu/cpu_context.h" diff --git a/paddle/phi/kernels/cpu/trunc_kernel.cc b/paddle/phi/kernels/cpu/trunc_kernel.cc index 5fe33ec6a4b2e..21fa84319fcf7 100644 --- a/paddle/phi/kernels/cpu/trunc_kernel.cc +++ b/paddle/phi/kernels/cpu/trunc_kernel.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/trunc_kernel.h" -#include +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/adam.cc b/paddle/phi/kernels/funcs/jit/gen/adam.cc index c5f9c37f7aecc..7efd63001f4cb 100644 --- a/paddle/phi/kernels/funcs/jit/gen/adam.cc +++ b/paddle/phi/kernels/funcs/jit/gen/adam.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/adam.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/registry.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/adamw.cc b/paddle/phi/kernels/funcs/jit/gen/adamw.cc index 3170ceb649d58..b5697f09c5681 100644 --- a/paddle/phi/kernels/funcs/jit/gen/adamw.cc +++ b/paddle/phi/kernels/funcs/jit/gen/adamw.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/adamw.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/registry.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/embseqpool.cc b/paddle/phi/kernels/funcs/jit/gen/embseqpool.cc index de5dbcaf60231..a98c9e51953ae 100644 --- a/paddle/phi/kernels/funcs/jit/gen/embseqpool.cc +++ b/paddle/phi/kernels/funcs/jit/gen/embseqpool.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/embseqpool.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/macro.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/gru.cc b/paddle/phi/kernels/funcs/jit/gen/gru.cc index 4b44b461eedb1..599564f431497 100644 --- a/paddle/phi/kernels/funcs/jit/gen/gru.cc +++ b/paddle/phi/kernels/funcs/jit/gen/gru.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/gru.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/macro.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/lstm.cc b/paddle/phi/kernels/funcs/jit/gen/lstm.cc index 09f17f563ca06..e22a5a2880dff 100644 --- a/paddle/phi/kernels/funcs/jit/gen/lstm.cc +++ b/paddle/phi/kernels/funcs/jit/gen/lstm.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/lstm.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/macro.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/matmul.cc b/paddle/phi/kernels/funcs/jit/gen/matmul.cc index 54ce57d257f41..666873b430e94 100644 --- a/paddle/phi/kernels/funcs/jit/gen/matmul.cc +++ b/paddle/phi/kernels/funcs/jit/gen/matmul.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/matmul.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/registry.h" diff --git a/paddle/phi/kernels/funcs/jit/gen/sgd.cc b/paddle/phi/kernels/funcs/jit/gen/sgd.cc index c09f934ae5b7d..930e7794cf94f 100644 --- a/paddle/phi/kernels/funcs/jit/gen/sgd.cc +++ b/paddle/phi/kernels/funcs/jit/gen/sgd.cc @@ -14,7 +14,7 @@ #include "paddle/phi/kernels/funcs/jit/gen/sgd.h" -#include // offsetof +#include // offsetof #include "paddle/phi/backends/cpu/cpu_info.h" #include "paddle/phi/kernels/funcs/jit/registry.h" From 54e447518b762f1e41193b90e22b0475174650eb Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:44:21 +0800 Subject: [PATCH 183/194] [clang-tidy] replenish cppcoreguidelines-narrowing-conversions (#56904) --- paddle/fluid/framework/block_desc.cc | 7 +-- paddle/fluid/framework/custom_operator.cc | 10 ++-- paddle/fluid/framework/data_feed.cc | 49 ++++++++++--------- paddle/fluid/framework/data_set.cc | 17 ++++--- .../details/fetch_async_op_handle.cc | 4 +- .../details/fused_all_reduce_op_handle.cc | 3 +- .../details/fused_broadcast_op_handle.cc | 5 +- .../framework/details/gather_op_handle.cc | 2 +- .../details/gather_op_handle_test.cc | 4 +- .../details/scale_loss_grad_op_handle.cc | 2 +- .../details/scope_buffered_monitor.cc | 5 +- .../scope_buffered_ssa_graph_executor.cc | 6 ++- paddle/fluid/framework/device_worker.cc | 2 +- paddle/fluid/framework/dist_multi_trainer.cc | 2 +- paddle/fluid/framework/downpour_worker.cc | 21 ++++---- paddle/fluid/framework/downpour_worker_opt.cc | 2 +- paddle/fluid/framework/executor.cc | 10 ++-- paddle/fluid/framework/executor_gc_helper.cc | 4 +- paddle/fluid/framework/hogwild_worker.cc | 9 ++-- paddle/fluid/framework/infershape_utils.cc | 14 +++--- .../fluid/framework/io/crypto/aes_cipher.cc | 2 +- .../fluid/framework/io/crypto/cipher_utils.cc | 2 +- paddle/fluid/framework/io/shell.cc | 2 +- .../framework/ir/attention_lstm_fuse_pass.cc | 6 +-- .../framework/ir/constant_folding_pass.cc | 2 +- .../ir/conv2d_fusion_layout_transfer_pass.cc | 4 +- ..._trans_filter_dilations_nxn_to_1x1_pass.cc | 33 +++++++------ .../fluid/framework/ir/conv_bn_fuse_pass.cc | 2 +- paddle/fluid/framework/ir/cost_model.cc | 8 +-- .../ir/delete_quant_dequant_filter_op_pass.cc | 4 +- .../ir/delete_quant_dequant_linear_op_pass.cc | 3 +- .../ir/embedding_fc_lstm_fuse_pass.cc | 6 +-- paddle/fluid/framework/ir/fc_fuse_pass.cc | 6 +-- .../fluid/framework/ir/fuse_adamw_op_pass.cc | 2 +- .../fused_multi_transformer_encoder_pass.cc | 18 +++---- paddle/fluid/framework/ir/generate_pass.cc | 2 +- paddle/fluid/framework/ir/graph.cc | 13 +++-- paddle/fluid/framework/ir/graph_helper.cc | 10 ++-- .../framework/ir/graph_pattern_detector.cc | 4 +- paddle/fluid/framework/ir/graph_viz_pass.cc | 9 ++-- .../framework/ir/layer_norm_fuse_pass.cc | 3 +- .../eager_deletion_pass.cc | 15 +++--- .../framework/ir/multi_batch_merge_pass.cc | 3 +- .../multi_devices_graph_pass.cc | 9 ++-- .../ir/multihead_matmul_fuse_pass.cc | 6 ++- .../ir/multihead_matmul_roformer_fuse_pass.cc | 3 +- paddle/fluid/framework/ir/pass_builder.cc | 4 +- paddle/fluid/framework/ir/pass_test_util.cc | 4 +- .../ir/quant_conv2d_dequant_fuse_pass.cc | 10 ++-- .../ir/repeated_fc_relu_fuse_pass.cc | 4 +- .../ir/shuffle_channel_detect_pass.cc | 18 +++---- .../fluid/framework/ir/subgraph_detector.cc | 6 +-- ...rt_delete_weight_dequant_linear_op_pass.cc | 6 +-- .../framework/ir/vit_attention_fuse_pass.cc | 2 +- paddle/fluid/framework/lod_tensor.cc | 18 ++++--- paddle/fluid/framework/multi_trainer.cc | 2 +- paddle/fluid/framework/naive_executor.cc | 2 +- .../event_garbage_collector.cc | 2 +- .../fast_garbage_collector.cc | 2 +- .../no_event_garbage_collector.cc | 2 +- .../interpreter/execution_config.cc | 6 +-- .../interpreter/interpreter_util.cc | 3 +- .../new_executor/interpreter/static_build.cc | 6 +-- .../new_executor/new_executor_defs.cc | 2 +- .../new_executor/new_ir_interpreter.cc | 27 +++++----- .../new_executor/program_interpreter.cc | 33 +++++++------ .../new_executor/workqueue/workqueue.cc | 4 +- paddle/fluid/framework/op_desc.cc | 19 ++++--- paddle/fluid/framework/operator.cc | 6 +-- paddle/fluid/framework/phi_utils.cc | 6 +-- paddle/fluid/framework/program_converter.cc | 4 +- paddle/fluid/framework/program_desc.cc | 10 ++-- paddle/fluid/framework/prune.cc | 4 +- paddle/fluid/framework/string_array.cc | 4 +- paddle/fluid/framework/tensor_util.cc | 6 +-- .../imperative/jit/program_desc_tracer.cc | 2 +- paddle/fluid/operators/affine_channel_op.cc | 14 +++--- paddle/fluid/operators/attention_lstm_op.cc | 28 +++++------ paddle/fluid/operators/batch_norm_op.cc | 8 +-- paddle/fluid/operators/conv_shift_op.cc | 9 ++-- paddle/fluid/operators/copy_cross_scope_op.cc | 2 +- paddle/fluid/operators/correlation_op.cc | 17 ++++--- paddle/fluid/operators/data_norm_op.cc | 30 ++++++------ .../fluid/operators/dequantize_abs_max_op.cc | 2 +- paddle/fluid/operators/dequantize_log_op.cc | 2 +- paddle/fluid/operators/expand_op.cc | 2 +- paddle/fluid/operators/fake_dequantize_op.cc | 4 +- paddle/fluid/operators/fake_quantize_op.cc | 4 +- paddle/fluid/operators/fill_constant_op.cc | 2 +- paddle/fluid/operators/flatten_op.cc | 4 +- paddle/fluid/operators/gru_op.cc | 22 ++++----- paddle/fluid/operators/gru_unit_op.cc | 26 +++++----- paddle/fluid/operators/inplace_abn_op.cc | 8 +-- paddle/fluid/operators/interpolate_op.cc | 24 ++++----- paddle/fluid/operators/lstm_op.cc | 2 +- paddle/fluid/operators/lstm_unit_op.cc | 4 +- paddle/fluid/operators/lstmp_op.cc | 2 +- paddle/fluid/operators/math/sampler.cc | 6 +-- paddle/fluid/operators/math/tree2col.cc | 9 ++-- paddle/fluid/operators/max_sequence_len_op.cc | 2 +- paddle/fluid/operators/merge_lod_tensor_op.cc | 8 +-- paddle/fluid/operators/nce_op.cc | 3 +- paddle/fluid/operators/pad2d_op.cc | 44 ++++++++--------- paddle/fluid/operators/partial_concat_op.cc | 6 +-- .../operators/prune_gate_by_capacity_op.cc | 2 +- paddle/fluid/operators/pyramid_hash_op.cc | 24 +++++---- paddle/fluid/operators/random_crop_op.cc | 3 +- paddle/fluid/operators/recurrent_op.cc | 43 +++++++++------- .../reorder_lod_tensor_by_rank_op.cc | 4 +- paddle/fluid/operators/reshape_op.cc | 12 ++--- paddle/fluid/operators/row_conv_op.cc | 20 ++++---- paddle/fluid/operators/sample_logits_op.cc | 2 +- .../fluid/operators/shrink_rnn_memory_op.cc | 10 ++-- paddle/fluid/operators/split_op.cc | 2 +- paddle/fluid/operators/spp_op.cc | 3 +- paddle/fluid/operators/stft_op.cc | 4 +- paddle/fluid/operators/sum_op.cc | 9 ++-- .../operators/tensor_array_to_tensor_op.cc | 4 +- 118 files changed, 542 insertions(+), 473 deletions(-) diff --git a/paddle/fluid/framework/block_desc.cc b/paddle/fluid/framework/block_desc.cc index 73d6b5fda50ed..8eb2f5dc590ef 100644 --- a/paddle/fluid/framework/block_desc.cc +++ b/paddle/fluid/framework/block_desc.cc @@ -143,18 +143,19 @@ void BlockDesc::PrependAllocatedOp(std::unique_ptr &&op_desc) { OpDesc *BlockDesc::InsertOp(size_t index) { need_update_ = true; - auto it = ops_.begin() + index; + auto it = ops_.begin() + index; // NOLINT std::unique_ptr new_op(new OpDesc(this)); it = ops_.insert(it, std::move(new_op)); return (*it).get(); } void BlockDesc::RemoveOp(size_t s, size_t e) { - if (ops_.begin() + s >= ops_.end() || ops_.begin() + e > ops_.end()) { + if (ops_.begin() + s >= ops_.end() || // NOLINT + ops_.begin() + e > ops_.end()) { // NOLINT return; } need_update_ = true; - ops_.erase(ops_.begin() + s, ops_.begin() + e); + ops_.erase(ops_.begin() + s, ops_.begin() + e); // NOLINT } void BlockDesc::RemoveOpInternal(const OpDesc *op_desc) { diff --git a/paddle/fluid/framework/custom_operator.cc b/paddle/fluid/framework/custom_operator.cc index a1e7d6477d915..c2dd1bf37dd19 100644 --- a/paddle/fluid/framework/custom_operator.cc +++ b/paddle/fluid/framework/custom_operator.cc @@ -653,8 +653,8 @@ static void RunDefaultInferDtypeFunc( if (detail::IsDuplicableVar(pair.first)) { size_t size = ctx->InputSize(pair.first); for (size_t i = 0; i < size; ++i) { - auto dtype = ctx->GetInputDataType(pair.first, i); - ctx->SetOutputDataType(pair.second, dtype, i); + auto dtype = ctx->GetInputDataType(pair.first, static_cast(i)); + ctx->SetOutputDataType(pair.second, dtype, static_cast(i)); } } else { auto dtype = ctx->GetInputDataType(pair.first); @@ -681,7 +681,7 @@ static void RunInferDtypeFunc( std::vector vec_custom_dtype; if (ctx->HasInput(in_name)) { // general inputs for (size_t i = 0; i < ctx->InputSize(in_name); ++i) { - auto dtype = ctx->GetInputDataType(in_name, i); + auto dtype = ctx->GetInputDataType(in_name, static_cast(i)); vec_custom_dtype.emplace_back( paddle::framework::TransToPhiDataType(dtype)); } @@ -799,8 +799,8 @@ static void RunInferDtypeFunc( if (ctx->HasOutput(out_name)) { size_t size = ctx->InputSize(in_name); for (size_t i = 0; i < size; ++i) { - auto dtype = ctx->GetInputDataType(in_name, i); - ctx->SetOutputDataType(out_name, dtype, i); + auto dtype = ctx->GetInputDataType(in_name, static_cast(i)); + ctx->SetOutputDataType(out_name, dtype, static_cast(i)); } } else { PADDLE_ENFORCE( diff --git a/paddle/fluid/framework/data_feed.cc b/paddle/fluid/framework/data_feed.cc index 05257d0845591..97a148ed42f5a 100644 --- a/paddle/fluid/framework/data_feed.cc +++ b/paddle/fluid/framework/data_feed.cc @@ -45,7 +45,9 @@ class BufferedLineFileReader { class FILEReader { public: explicit FILEReader(FILE* fp) : fp_(fp) {} - int read(char* buf, int len) { return fread(buf, sizeof(char), len, fp_); } + int read(char* buf, int len) { + return static_cast(fread(buf, sizeof(char), len, fp_)); + } private: FILE* fp_; @@ -644,10 +646,11 @@ void MultiSlotDataFeed::Init( use_slots_.clear(); use_slots_is_dense_.clear(); for (size_t i = 0; i < all_slot_num; ++i) { - const auto& slot = multi_slot_desc.slots(i); + const auto& slot = multi_slot_desc.slots(static_cast(i)); all_slots_[i] = slot.name(); all_slots_type_[i] = slot.type(); - use_slots_index_[i] = slot.is_used() ? use_slots_.size() : -1; + use_slots_index_[i] = + static_cast(slot.is_used() ? use_slots_.size() : -1); total_dims_without_inductive_[i] = 1; inductive_shape_index_[i] = -1; if (slot.is_used()) { @@ -1048,10 +1051,11 @@ void MultiSlotInMemoryDataFeed::Init( use_slots_is_dense_.clear(); slot_conf_.resize(all_slot_num); for (size_t i = 0; i < all_slot_num; ++i) { - const auto& slot = multi_slot_desc.slots(i); + const auto& slot = multi_slot_desc.slots(static_cast(i)); all_slots_[i] = slot.name(); all_slots_type_[i] = slot.type(); - use_slots_index_[i] = slot.is_used() ? use_slots_.size() : -1; + use_slots_index_[i] = + static_cast(slot.is_used() ? use_slots_.size() : -1); slot_conf_[i].name = slot.name(); slot_conf_[i].type = slot.type(); @@ -1839,21 +1843,21 @@ void PaddleBoxDataFeed::GetRankOffset(const std::vector& pv_vec, int max_rank = 3; // the value is setting int row = ins_number; int col = max_rank * 2 + 1; - int pv_num = pv_vec.size(); + int pv_num = static_cast(pv_vec.size()); std::vector rank_offset_mat(row * col, -1); rank_offset_mat.shrink_to_fit(); for (int i = 0; i < pv_num; i++) { auto pv_ins = pv_vec[i]; - int ad_num = pv_ins->ads.size(); + int ad_num = static_cast(pv_ins->ads.size()); int index_start = index; for (int j = 0; j < ad_num; ++j) { auto ins = pv_ins->ads[j]; int rank = -1; if ((ins->cmatch == 222 || ins->cmatch == 223) && ins->rank <= static_cast(max_rank) && ins->rank != 0) { - rank = ins->rank; + rank = static_cast(ins->rank); } rank_offset_mat[index * col] = rank; @@ -1864,12 +1868,13 @@ void PaddleBoxDataFeed::GetRankOffset(const std::vector& pv_vec, if ((cur_ins->cmatch == 222 || cur_ins->cmatch == 223) && cur_ins->rank <= static_cast(max_rank) && cur_ins->rank != 0) { - fast_rank = cur_ins->rank; + fast_rank = static_cast(cur_ins->rank); } if (fast_rank > 0) { int m = fast_rank - 1; - rank_offset_mat[index * col + 2 * m + 1] = cur_ins->rank; + rank_offset_mat[index * col + 2 * m + 1] = + static_cast(cur_ins->rank); rank_offset_mat[index * col + 2 * m + 2] = index_start + k; } } @@ -2035,7 +2040,7 @@ void SlotRecordInMemoryDataFeed::Init(const DataFeedDesc& data_feed_desc) { float_total_dims_size_ = 0; float_total_dims_without_inductives_.clear(); for (size_t i = 0; i < all_slot_num; ++i) { - const auto& slot = multi_slot_desc.slots(i); + const auto& slot = multi_slot_desc.slots(static_cast(i)); all_slots_[i] = slot.name(); AllSlotInfo& all_slot = all_slots_info_[i]; @@ -2046,7 +2051,7 @@ void SlotRecordInMemoryDataFeed::Init(const DataFeedDesc& data_feed_desc) { if (slot.is_used()) { UsedSlotInfo& info = used_slots_info_[use_slot_size_]; - info.idx = i; + info.idx = static_cast(i); info.slot = slot.name(); info.type = slot.type(); info.dense = slot.is_dense(); @@ -2423,20 +2428,20 @@ bool SlotRecordInMemoryDataFeed::ParseOneInstance(const std::string& line, slot_uint64_feasigns.resize(uint64_use_slot_size_); if (parse_ins_id_) { - int num = strtol(&str[pos], &endptr, 10); + int num = static_cast(strtol(&str[pos], &endptr, 10)); CHECK(num == 1); // NOLINT - pos = endptr - str + 1; + pos = static_cast(endptr - str + 1); size_t len = 0; while (str[pos + len] != ' ') { ++len; } rec->ins_id_ = std::string(str + pos, len); - pos += len + 1; + pos += static_cast(len + 1); } if (parse_logkey_) { - int num = strtol(&str[pos], &endptr, 10); + int num = static_cast(strtol(&str[pos], &endptr, 10)); CHECK(num == 1); // NOLINT - pos = endptr - str + 1; + pos = static_cast(endptr - str + 1); size_t len = 0; while (str[pos + len] != ' ') { ++len; @@ -2452,14 +2457,14 @@ bool SlotRecordInMemoryDataFeed::ParseOneInstance(const std::string& line, rec->search_id = search_id; rec->cmatch = cmatch; rec->rank = rank; - pos += len + 1; + pos += static_cast(len + 1); } int float_total_slot_num = 0; int uint64_total_slot_num = 0; for (auto& info : all_slots_info_) { - int num = strtol(&str[pos], &endptr, 10); + int num = static_cast(strtol(&str[pos], &endptr, 10)); PADDLE_ENFORCE(num, "The number of ids can not be zero, you need padding " "it in data generator; or if there is something wrong with " @@ -2488,7 +2493,7 @@ bool SlotRecordInMemoryDataFeed::ParseOneInstance(const std::string& line, ++uint64_total_slot_num; } } - pos = endptr - str; + pos = static_cast(endptr - str); } else { for (int j = 0; j <= num; ++j) { // pos = line.find_first_of(' ', pos + 1); @@ -2565,7 +2570,7 @@ void SlotRecordInMemoryDataFeed::PutToFeedVec(const SlotRecord* ins_vec, batch_fea.resize(total_instance + fea_num); memcpy( &batch_fea[total_instance], slot_values, sizeof(float) * fea_num); - total_instance += fea_num; + total_instance += static_cast(fea_num); slot_offset.push_back(total_instance); } @@ -2588,7 +2593,7 @@ void SlotRecordInMemoryDataFeed::PutToFeedVec(const SlotRecord* ins_vec, memcpy(&batch_fea[total_instance], slot_values, sizeof(uint64_t) * fea_num); - total_instance += fea_num; + total_instance += static_cast(fea_num); } if (fea_num == 0) { batch_fea.resize(total_instance + fea_num); diff --git a/paddle/fluid/framework/data_set.cc b/paddle/fluid/framework/data_set.cc index 4243ca6175f86..b28bfa5438099 100644 --- a/paddle/fluid/framework/data_set.cc +++ b/paddle/fluid/framework/data_set.cc @@ -295,7 +295,7 @@ static void compute_batch_num(const int64_t ins_num, int thread_batch_num = batch_size * thread_num; // less data if (static_cast(thread_batch_num) > ins_num) { - compute_left_batch_num(ins_num, thread_num, offset, 0); + compute_left_batch_num(static_cast(ins_num), thread_num, offset, 0); return; } @@ -1271,7 +1271,7 @@ void MultiSlotDataset::PreprocessInstance() { input_channel_->Close(); std::vector pv_data; input_channel_->ReadAll(input_records_); - int all_records_num = input_records_.size(); + int all_records_num = static_cast(input_records_.size()); std::vector all_records; all_records.reserve(all_records_num); for (int index = 0; index < all_records_num; ++index) { @@ -1333,7 +1333,7 @@ void MultiSlotDataset::GenerateLocalTablesUnlock(int table_id, local_map_tables = fleet_ptr_->GetLocalTable(); local_map_tables.resize(shard_num); // read thread - int channel_num = multi_output_channel_.size(); + int channel_num = static_cast(multi_output_channel_.size()); if (read_thread_num < channel_num) { read_thread_num = channel_num; } @@ -1361,7 +1361,8 @@ void MultiSlotDataset::GenerateLocalTablesUnlock(int table_id, this->multi_output_channel_[i]->ReadAll(vec_data); for (auto& item : vec_data) { for (auto& feature : item.uint64_feasigns_) { - int shard = feature.sign().uint64_feasign_ % shard_num; + int shard = + static_cast(feature.sign().uint64_feasign_ % shard_num); task_keys[shard].push_back(feature.sign().uint64_feasign_); } } @@ -1634,11 +1635,11 @@ void MultiSlotDataset::PreprocessChannel( int out_channel_size = 0; if (cur_channel_ == 0) { for (auto& item : multi_output_channel_) { - out_channel_size += item->Size(); + out_channel_size += static_cast(item->Size()); } } else { for (auto& item : multi_consume_channel_) { - out_channel_size += item->Size(); + out_channel_size += static_cast(item->Size()); } } VLOG(2) << "DatasetImpl::SlotsShuffle() begin with input channel size: " @@ -1724,14 +1725,14 @@ void MultiSlotDataset::PreprocessChannel( if (!item) { continue; } - end_size += item->Size(); + end_size += static_cast(item->Size()); } } else { for (auto& item : multi_consume_channel_) { if (!item) { continue; } - end_size += item->Size(); + end_size += static_cast(item->Size()); } } CHECK(input_channel_->Size() == 0) diff --git a/paddle/fluid/framework/details/fetch_async_op_handle.cc b/paddle/fluid/framework/details/fetch_async_op_handle.cc index 89617f0adf9fd..900f0ebc4f111 100644 --- a/paddle/fluid/framework/details/fetch_async_op_handle.cc +++ b/paddle/fluid/framework/details/fetch_async_op_handle.cc @@ -174,7 +174,7 @@ void FetchAsyncOpHandle::FetchMergedLodTensor( // for 0D tensor, can't concat eath tensor. So stack 0D and concat 1+D tensor if (rank == 0) { - int src_lodtensor_size = src_lodtensors.size(); + int src_lodtensor_size = static_cast(src_lodtensors.size()); new_dim = phi::make_ddim(std::vector({src_lodtensor_size})); } else { bool find_first_dims = false; @@ -211,7 +211,7 @@ void FetchAsyncOpHandle::FetchMergedLodTensor( if (rank == 0) { end = begin + 1; } else { - end = begin + src->dims()[0]; + end = static_cast(begin + src->dims()[0]); } if (end == begin) { diff --git a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc index a075b4702e946..53746482d58a8 100644 --- a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc +++ b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc @@ -401,7 +401,8 @@ void FusedAllReduceOpHandle::GetDTypeAndNumel( "The size of grad tensors of fused_all_reduce_op_handle " "must be > 0, but got %d.", len)); - *numel += phi::Alignment(len * size_of_dtype, places_[0]) / size_of_dtype; + *numel += static_cast( + phi::Alignment(len * size_of_dtype, places_[0]) / size_of_dtype); } } diff --git a/paddle/fluid/framework/details/fused_broadcast_op_handle.cc b/paddle/fluid/framework/details/fused_broadcast_op_handle.cc index d9e599067b083..c446d3502e4e4 100644 --- a/paddle/fluid/framework/details/fused_broadcast_op_handle.cc +++ b/paddle/fluid/framework/details/fused_broadcast_op_handle.cc @@ -49,8 +49,9 @@ void FusedBroadcastOpHandle::RunImpl() { for (size_t i = 0; i < in_var_handles.size(); ++i) { BroadcastOneVar( *in_var_handles[i], - std::vector(out_var_handles.begin() + i * place_num, - out_var_handles.begin() + (i + 1) * place_num), + std::vector( + out_var_handles.begin() + i * place_num, // NOLINT + out_var_handles.begin() + (i + 1) * place_num), // NOLINT local_exec_scopes_); } } diff --git a/paddle/fluid/framework/details/gather_op_handle.cc b/paddle/fluid/framework/details/gather_op_handle.cc index d94b97c770b67..79b43b1b501db 100644 --- a/paddle/fluid/framework/details/gather_op_handle.cc +++ b/paddle/fluid/framework/details/gather_op_handle.cc @@ -129,7 +129,7 @@ void GatherOpHandle::RunImpl() { out_var_handle->place(), [in_tensors, out_tensor, &dev_ctx, t_out_p] { int s = 0, e = 0; for (const auto &in_tensor : in_tensors) { - e += in_tensor.dims()[0]; + e += static_cast(in_tensor.dims()[0]); auto sub_out = out_tensor->Slice(s, e); paddle::framework::TensorCopy(in_tensor, t_out_p, *dev_ctx, &sub_out); s = e; diff --git a/paddle/fluid/framework/details/gather_op_handle_test.cc b/paddle/fluid/framework/details/gather_op_handle_test.cc index 65db0d8449e6d..12d84aef8a8ac 100644 --- a/paddle/fluid/framework/details/gather_op_handle_test.cc +++ b/paddle/fluid/framework/details/gather_op_handle_test.cc @@ -134,12 +134,12 @@ struct TestGatherOpHandle { } void TestGatherSelectedRows(size_t output_scope_idx) { - int height = kDims[0] * 2; + int height = static_cast(kDims[0] * 2); std::vector rows{0, 1, 2, 3, 3, 0, 14, 7, 3, 1, 2, 4, 6, 3, 1, 1, 1, 1, 3, 7}; std::vector send_vector(phi::product(kDims)); for (size_t k = 0; k < send_vector.size(); ++k) { - send_vector[k] = k; + send_vector[k] = static_cast(k); } for (size_t input_scope_idx = 0; input_scope_idx < gpu_list_.size(); diff --git a/paddle/fluid/framework/details/scale_loss_grad_op_handle.cc b/paddle/fluid/framework/details/scale_loss_grad_op_handle.cc index 98f2c01100d0d..6aac8b6fd51cd 100644 --- a/paddle/fluid/framework/details/scale_loss_grad_op_handle.cc +++ b/paddle/fluid/framework/details/scale_loss_grad_op_handle.cc @@ -33,7 +33,7 @@ ScaleLossGradOpHandle::ScaleLossGradOpHandle(ir::Node *node, platform::DeviceContext *dev_ctx, proto::VarType::Type dtype) : OpHandleBase(node), - coeff_(static_cast(1.0 / num_dev)), + coeff_(static_cast(1.0 / num_dev)), // NOLINT scope_(scope), place_(place), out_dtype_(dtype) { diff --git a/paddle/fluid/framework/details/scope_buffered_monitor.cc b/paddle/fluid/framework/details/scope_buffered_monitor.cc index bd53a5d4e19cf..3d5111f2662ca 100644 --- a/paddle/fluid/framework/details/scope_buffered_monitor.cc +++ b/paddle/fluid/framework/details/scope_buffered_monitor.cc @@ -169,14 +169,15 @@ void ScopeBufferedMonitor::Apply(const std::function &callback, if (VLOG_IS_ON(8)) { for (size_t idx = 0; idx < gpu_memory_size_per_gpu.size(); ++idx) { VLOG(8) << "history local exec scopes contains " - << string::HumanReadableSize(gpu_memory_size_per_gpu.at(idx)) + << string::HumanReadableSize( + gpu_memory_size_per_gpu.at(idx)) // NOLINT << " in " << places_.at(idx); } } if (FLAGS_local_exe_sub_scope_limit > 0) { for (size_t idx = 0; idx < gpu_memory_size_per_gpu.size(); ++idx) { - if (gpu_memory_size_per_gpu.at(idx) / kMB >= + if (gpu_memory_size_per_gpu.at(idx) / kMB >= // NOLINT FLAGS_local_exe_sub_scope_limit) { platform::DeviceContextPool::Instance().Get(places_.at(idx))->Wait(); local_exec_scopes_.at(idx)->DropKids(); diff --git a/paddle/fluid/framework/details/scope_buffered_ssa_graph_executor.cc b/paddle/fluid/framework/details/scope_buffered_ssa_graph_executor.cc index aa8b1718bbfeb..7ee7fa82250a9 100644 --- a/paddle/fluid/framework/details/scope_buffered_ssa_graph_executor.cc +++ b/paddle/fluid/framework/details/scope_buffered_ssa_graph_executor.cc @@ -104,7 +104,8 @@ FetchResultType ScopeBufferedSSAGraphExecutor::Run( if (VLOG_IS_ON(5)) { for (auto *scope : local_exec_scopes_) { VLOG(5) << "Left " - << string::HumanReadableSize(GetScopeVarMemorySize(scope)) + << string::HumanReadableSize( + GetScopeVarMemorySize(scope)) // NOLINT << " on scope " << scope << " before deleting"; } } @@ -118,7 +119,8 @@ FetchResultType ScopeBufferedSSAGraphExecutor::Run( if (VLOG_IS_ON(5)) { for (auto *scope : local_exec_scopes_) { VLOG(5) << "Left " - << string::HumanReadableSize(GetScopeVarMemorySize(scope)) + << string::HumanReadableSize( + GetScopeVarMemorySize(scope)) // NOLINT << " on scope " << scope << " after deleting"; } } diff --git a/paddle/fluid/framework/device_worker.cc b/paddle/fluid/framework/device_worker.cc index d2bc5b2b21f77..dbf7a6853b04d 100644 --- a/paddle/fluid/framework/device_worker.cc +++ b/paddle/fluid/framework/device_worker.cc @@ -448,7 +448,7 @@ void DeviceWorker::DumpField(const Scope& scope, if (!hit[i]) { continue; } - auto bound = GetTensorBound(tensor, i); + auto bound = GetTensorBound(tensor, static_cast(i)); ars[i] += "\t" + field + ":" + std::to_string(bound.second - bound.first) + ":"; ars[i] += PrintLodTensor(tensor, bound.first, bound.second); diff --git a/paddle/fluid/framework/dist_multi_trainer.cc b/paddle/fluid/framework/dist_multi_trainer.cc index 5e11de0ea9d1c..570f14323deec 100644 --- a/paddle/fluid/framework/dist_multi_trainer.cc +++ b/paddle/fluid/framework/dist_multi_trainer.cc @@ -36,7 +36,7 @@ void DistMultiTrainer::Initialize(const TrainerDesc &trainer_desc, const std::vector readers = dataset->GetReaders(); RegisterHeterCallback(); - thread_num_ = readers.size(); + thread_num_ = static_cast(readers.size()); workers_.resize(thread_num_); for (int i = 0; i < trainer_desc.downpour_param().stat_var_names_size(); i++) { diff --git a/paddle/fluid/framework/downpour_worker.cc b/paddle/fluid/framework/downpour_worker.cc index 7d16226bcfb58..8a0406864cde7 100644 --- a/paddle/fluid/framework/downpour_worker.cc +++ b/paddle/fluid/framework/downpour_worker.cc @@ -130,8 +130,9 @@ void DownpourWorker::CollectLabelInfo(size_t table_idx) { if (no_cvm_) { return; } - uint64_t table_id = static_cast( - param_.program_config(0).pull_sparse_table_id(table_idx)); + uint64_t table_id = + static_cast(param_.program_config(0).pull_sparse_table_id( + static_cast(table_idx))); TableParameter table; for (auto i : param_.sparse_table()) { @@ -185,8 +186,9 @@ void DownpourWorker::CollectLabelInfo(size_t table_idx) { } void DownpourWorker::FillSparseValue(size_t table_idx) { - uint64_t table_id = static_cast( - param_.program_config(0).pull_sparse_table_id(table_idx)); + uint64_t table_id = + static_cast(param_.program_config(0).pull_sparse_table_id( + static_cast(table_idx))); TableParameter table; for (auto i : param_.sparse_table()) { @@ -210,7 +212,7 @@ void DownpourWorker::FillSparseValue(size_t table_idx) { phi::DenseTensor* tensor = var->GetMutable(); CHECK(tensor != nullptr) << "tensor of var " << slot_name << " is null"; int64_t* ids = tensor->data(); - int len = tensor->numel(); + int len = static_cast(tensor->numel()); Variable* var_emb = thread_scope_->FindVar(emb_slot_name); if (var_emb == nullptr) { continue; @@ -354,8 +356,8 @@ void DownpourWorker::AdjustInsWeight() { void DownpourWorker::CopySparseTable() { for (auto& copy_sparse_table : copy_sparse_tables_) { - int64_t src_table = copy_sparse_table.first; - int64_t dest_table = copy_sparse_table.second; + int64_t src_table = static_cast(copy_sparse_table.first); + int64_t dest_table = static_cast(copy_sparse_table.second); int32_t feanum = 0; if (src_table == dest_table) { continue; @@ -768,7 +770,8 @@ void DownpourWorker::TrainFilesWithProfiler() { fprintf(stderr, "push dense time percent: %f\n", push_dense_time / total_time * 100); - fprintf(stderr, "%6.2f instances/s\n", total_inst / total_time); + fprintf( + stderr, "%6.2f instances/s\n", total_inst / total_time); // NOLINT } } timeline.Start(); @@ -984,7 +987,7 @@ void DownpourWorker::TrainFiles() { param_.program_config(0).push_dense_table_id(i)); if (condvalue_set_.find(tid) != condvalue_set_.end()) { // common dense table must push dense - if (cond2table_map_[cond_value_batch[0]] != tid) { + if (cond2table_map_[static_cast(cond_value_batch[0])] != tid) { // can't push dense continue; } diff --git a/paddle/fluid/framework/downpour_worker_opt.cc b/paddle/fluid/framework/downpour_worker_opt.cc index 7810ad32c98ac..68c774965aeab 100644 --- a/paddle/fluid/framework/downpour_worker_opt.cc +++ b/paddle/fluid/framework/downpour_worker_opt.cc @@ -197,7 +197,7 @@ void DownpourWorkerOpt::CreateThreadOperatorsWithRerank( auto& block = program.Block(0); std::vector ops = block.AllOps(); // check if Independent between losses if not skip for now - int loss_num = loss_names_.size(); + int loss_num = static_cast(loss_names_.size()); std::unordered_map> loss_input_map; std::unordered_map> diff --git a/paddle/fluid/framework/executor.cc b/paddle/fluid/framework/executor.cc index f18705ef099ab..c3e541b8a7163 100644 --- a/paddle/fluid/framework/executor.cc +++ b/paddle/fluid/framework/executor.cc @@ -51,11 +51,11 @@ void ExecutorPrepareContext::PrepareUnusedVars( // If gc is enabled and block size > 1 if (prog_.Size() > 1) { operators::PrepareSafeEagerDeletionOnConditionalOpAndConditionalGradOp( - prog_, block_id_, ops_); + prog_, static_cast(block_id_), ops_); operators::PrepareSafeEagerDeletionOnWhileOpAndWhileGradOp( - prog_, block_id_, ops_); + prog_, static_cast(block_id_), ops_); operators::PrepareSafeEagerDeletionOnRecurrentOpAndRecurrentGradOp( - prog_, block_id_, ops_); + prog_, static_cast(block_id_), ops_); } force_disable_gc_ = force_disable_gc; @@ -485,7 +485,7 @@ void Executor::RunPartialPreparedContext(ExecutorPrepareContext* ctx, if (create_local_scope) { local_scope = &scope->NewScope(); } - CreateVariables(ctx->prog_, local_scope, ctx->block_id_); + CreateVariables(ctx->prog_, local_scope, static_cast(ctx->block_id_)); } int64_t max_memory_size = GetEagerDeletionThreshold(); @@ -585,7 +585,7 @@ void Executor::RunPreparedContext(ExecutorPrepareContext* ctx, bool create_vars, bool keep_kids) { int64_t start_op_index = 0; - int64_t end_op_index = ctx->ops_.size(); + int64_t end_op_index = static_cast(ctx->ops_.size()); RunPartialPreparedContext(ctx, scope, start_op_index, diff --git a/paddle/fluid/framework/executor_gc_helper.cc b/paddle/fluid/framework/executor_gc_helper.cc index 39eeabe700c45..fa63b4bca16ea 100644 --- a/paddle/fluid/framework/executor_gc_helper.cc +++ b/paddle/fluid/framework/executor_gc_helper.cc @@ -196,7 +196,7 @@ static std::vector> CreateOpsFromBlock( size_t op_num = block.OpSize(); ops.reserve(op_num); for (size_t i = 0; i < op_num; ++i) { - auto *op_desc = block.Op(i); + auto *op_desc = block.Op(static_cast(i)); ops.push_back(OpRegistry::CreateOp(*op_desc)); } return ops; @@ -245,7 +245,7 @@ GetEagerDeletionCleanVarsForPartial(const ProgramDesc &origin_program, const auto &block = program.Block(i); size_t op_num = block.OpSize(); for (size_t j = 0; j < op_num; ++j) { - auto *op = block.Op(j); + auto *op = block.Op(static_cast(j)); if (!op->HasAttr(kSubBlock) || !op->HasAttr(kSkipEagerDeletionVars)) { continue; } diff --git a/paddle/fluid/framework/hogwild_worker.cc b/paddle/fluid/framework/hogwild_worker.cc index 199978a6c4f6c..cc2c70506a34c 100644 --- a/paddle/fluid/framework/hogwild_worker.cc +++ b/paddle/fluid/framework/hogwild_worker.cc @@ -86,9 +86,9 @@ void HogwildWorker::CreateThreadScope(const ProgramDesc &program) { InitializeVariable(ptr, var->GetType()); if (stat_var_name_map_.find(var->Name()) != stat_var_name_map_.end() && thread_id_ != 0) { - int tensor_dim = root_scope_->FindVar(var->Name()) - ->GetMutable() - ->numel(); + int tensor_dim = static_cast(root_scope_->FindVar(var->Name()) + ->GetMutable() + ->numel()); auto *ptr1 = thread_scope_->Var(var->Name()); InitializeVariable(ptr1, var->GetType()); phi::DenseTensor *thread_tensor = ptr1->GetMutable(); @@ -290,7 +290,8 @@ void HogwildWorker::TrainFilesWithProfiler() { } fprintf(stderr, "mean read time: %fs\n", read_time / batch_cnt); fprintf(stderr, "IO percent: %f\n", read_time / total_time * 100); - fprintf(stderr, "%6.2f instances/s\n", total_inst / total_time); + fprintf( + stderr, "%6.2f instances/s\n", total_inst / total_time); // NOLINT } } #endif diff --git a/paddle/fluid/framework/infershape_utils.cc b/paddle/fluid/framework/infershape_utils.cc index ff801d15136d6..38cc88f7ec936 100644 --- a/paddle/fluid/framework/infershape_utils.cc +++ b/paddle/fluid/framework/infershape_utils.cc @@ -171,7 +171,7 @@ int64_t CompatMetaTensor::numel() const { return var->Get().numel(); } else { auto* var = PADDLE_GET_CONST(VarDesc*, var_); - return var->ElementSize(); + return static_cast(var->ElementSize()); } } @@ -445,19 +445,19 @@ void CompatMetaTensor::share_meta(const MetaTensor& meta_tensor) { } void CompatInferMetaContext::EmplaceBackInput(CompatMetaTensor input) { - int index = compat_inputs_.size(); + int index = static_cast(compat_inputs_.size()); compat_inputs_.emplace_back(std::move(input)); input_range_.emplace_back(std::pair(index, index + 1)); } void CompatInferMetaContext::EmplaceBackOutput(CompatMetaTensor output) { - int index = compat_outputs_.size(); + int index = static_cast(compat_outputs_.size()); compat_outputs_.emplace_back(std::move(output)); output_range_.emplace_back(std::pair(index, index + 1)); } void CompatInferMetaContext::EmplaceBackInputs( paddle::small_vector inputs) { - int index = compat_inputs_.size(); + int index = static_cast(compat_inputs_.size()); input_range_.emplace_back(std::pair(index, index + inputs.size())); compat_inputs_.insert(compat_inputs_.end(), std::make_move_iterator(inputs.begin()), @@ -467,7 +467,7 @@ void CompatInferMetaContext::EmplaceBackInputs( void CompatInferMetaContext::EmplaceBackOutputs( paddle::small_vector outputs) { - int index = compat_outputs_.size(); + int index = static_cast(compat_outputs_.size()); output_range_.emplace_back( std::pair(index, index + outputs.size())); compat_outputs_.insert(compat_outputs_.end(), @@ -711,11 +711,11 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, } if (num_ele <= 0) { - num_ele = tensor_dims.size(); + num_ele = static_cast(tensor_dims.size()); } } else { - num_ele = vars.size(); + num_ele = static_cast(vars.size()); } phi::IntArray tensor_attr(std::vector(num_ele, -1)); tensor_attr.SetFromTensor(true); diff --git a/paddle/fluid/framework/io/crypto/aes_cipher.cc b/paddle/fluid/framework/io/crypto/aes_cipher.cc index 5d27af0301fcf..5a1258fa432f7 100644 --- a/paddle/fluid/framework/io/crypto/aes_cipher.cc +++ b/paddle/fluid/framework/io/crypto/aes_cipher.cc @@ -292,7 +292,7 @@ void AESCipher::EncryptToFile(const std::string& plaintext, const std::string& filename) { std::ofstream fout(filename, std::ios::binary); std::string ciphertext = this->Encrypt(plaintext, key); - fout.write(ciphertext.data(), ciphertext.size()); + fout.write(ciphertext.data(), ciphertext.size()); // NOLINT fout.close(); } diff --git a/paddle/fluid/framework/io/crypto/cipher_utils.cc b/paddle/fluid/framework/io/crypto/cipher_utils.cc index 0d5eb6fa732bd..c10da1ce6706c 100644 --- a/paddle/fluid/framework/io/crypto/cipher_utils.cc +++ b/paddle/fluid/framework/io/crypto/cipher_utils.cc @@ -49,7 +49,7 @@ std::string CipherUtils::GenKeyToFile(int length, const std::string& filename) { "Failed to open file : %s, " "make sure input filename is available.", filename)); - fout.write(rng.c_str(), rng.size()); + fout.write(rng.c_str(), rng.size()); // NOLINT fout.close(); return rng; } diff --git a/paddle/fluid/framework/io/shell.cc b/paddle/fluid/framework/io/shell.cc index b710289b09175..1e5ceb97b0c6e 100644 --- a/paddle/fluid/framework/io/shell.cc +++ b/paddle/fluid/framework/io/shell.cc @@ -153,7 +153,7 @@ static int shell_popen_fork_internal(const char* real_cmd, static int read_from_pipe(FILE* fp, std::string* output) { std::array buf; while (true) { - int n = fread(buf.data(), 1, 4096, fp); + int n = static_cast(fread(buf.data(), 1, 4096, fp)); if (n <= 0) { break; } diff --git a/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc b/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc index 1aa12ded2611b..9764f78744974 100644 --- a/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc +++ b/paddle/fluid/framework/ir/attention_lstm_fuse_pass.cc @@ -294,8 +294,8 @@ void PrepareLSTMWeight(const phi::DenseTensor& W_forget_w0, const phi::DenseTensor& W_cell_w0, const phi::DenseTensor& W_cell_w1, phi::DenseTensor* out) { - int D = W_forget_w0.dims()[0]; - int M = W_forget_w1.dims()[0]; + int D = static_cast(W_forget_w0.dims()[0]); + int M = static_cast(W_forget_w1.dims()[0]); out->Resize(phi::make_ddim({D + M, 4 * D})); VLOG(3) << "LSTMWeight resized to " << out->dims(); @@ -342,7 +342,7 @@ void PrepareLSTMBias(const phi::DenseTensor& B_forget, platform::errors::InvalidArgument( "phi::DenseTensor B forget dimension size(%d) must be 1.", B_forget.dims().size())); - int D = B_forget.dims()[0]; + int D = static_cast(B_forget.dims()[0]); out->Resize(phi::make_ddim({1, 4 * D})); auto* out_data = out->mutable_data(platform::CPUPlace()); for (size_t i = 0; i < tensors.size(); i++) { diff --git a/paddle/fluid/framework/ir/constant_folding_pass.cc b/paddle/fluid/framework/ir/constant_folding_pass.cc index 8f227929b395b..3b3f23933fb6d 100644 --- a/paddle/fluid/framework/ir/constant_folding_pass.cc +++ b/paddle/fluid/framework/ir/constant_folding_pass.cc @@ -143,7 +143,7 @@ void ConstantFoldingPass::ApplyImpl(ir::Graph *graph) const { local_scope->FindVar(out_name)->GetMutable(); std::vector out_shape; for (int64_t i = 0; i < local_out_tensor->dims().size(); i++) { - out_shape.push_back(local_out_tensor->dims()[i]); + out_shape.push_back(local_out_tensor->dims()[static_cast(i)]); } out_desc->SetShape(out_shape); out_desc->SetPersistable(true); diff --git a/paddle/fluid/framework/ir/conv2d_fusion_layout_transfer_pass.cc b/paddle/fluid/framework/ir/conv2d_fusion_layout_transfer_pass.cc index fba60cbf44585..249e968f07bc6 100644 --- a/paddle/fluid/framework/ir/conv2d_fusion_layout_transfer_pass.cc +++ b/paddle/fluid/framework/ir/conv2d_fusion_layout_transfer_pass.cc @@ -152,8 +152,8 @@ void Conv2dFusionLayoutTransferPass::ApplyImpl(ir::Graph *graph) const { auto *filter_var = scope->FindLocalVar(filter_name); const auto &filter_tensor = filter_var->Get(); CHECK_EQ(filter_tensor.dims().size() == 4UL, true); - int oc = filter_tensor.dims()[0]; - int ic = filter_tensor.dims()[1]; + int oc = static_cast(filter_tensor.dims()[0]); + int ic = static_cast(filter_tensor.dims()[1]); bool cudnn_can_support = oc % CUDNN_ALIGNMENT == 0 && ic % CUDNN_ALIGNMENT == 0; if (!cudnn_can_support) { diff --git a/paddle/fluid/framework/ir/conv2d_trans_filter_dilations_nxn_to_1x1_pass.cc b/paddle/fluid/framework/ir/conv2d_trans_filter_dilations_nxn_to_1x1_pass.cc index 0cad2736c1e05..cbb1af4615552 100644 --- a/paddle/fluid/framework/ir/conv2d_trans_filter_dilations_nxn_to_1x1_pass.cc +++ b/paddle/fluid/framework/ir/conv2d_trans_filter_dilations_nxn_to_1x1_pass.cc @@ -115,10 +115,10 @@ void Conv2dTransFilterDilationsNxNTo1x1Pass::conv2d_dilation_trans( auto* weights = scope->FindVar(weights_name)->GetMutable(); auto weights_shape = weights->dims(); - int kh = weights_shape[2]; - int kw = weights_shape[3]; - int new_kh = dilations[0] * (kh - 1) + 1; - int new_kw = dilations[1] * (kw - 1) + 1; + int kh = static_cast(weights_shape[2]); + int kw = static_cast(weights_shape[3]); + int new_kh = static_cast(dilations[0] * (kh - 1) + 1); + int new_kw = static_cast(dilations[1] * (kw - 1) + 1); // New weights auto new_weights_name = weights_name + "_dilation_trans"; auto* new_weights = @@ -131,8 +131,8 @@ void Conv2dTransFilterDilationsNxNTo1x1Pass::conv2d_dilation_trans( memset(new_weights_data, 0, new_weights->numel() * sizeof(float)); conv2d_dilation_trans_fn(weights_data, new_weights_data, - weights_shape[0], - weights_shape[1], + static_cast(weights_shape[0]), + static_cast(weights_shape[1]), kh, kw, new_kh, @@ -147,16 +147,17 @@ void Conv2dTransFilterDilationsNxNTo1x1Pass::conv2d_dilation_trans( memset(new_weights_data, 0, new_weights->numel() * sizeof(phi::dtype::float16)); - conv2d_dilation_trans_fn(weights_data, - new_weights_data, - weights_shape[0], - weights_shape[1], - kh, - kw, - new_kh, - new_kw, - dilations[0], - dilations[1]); + conv2d_dilation_trans_fn( + weights_data, + new_weights_data, + static_cast(weights_shape[0]), + static_cast(weights_shape[1]), + kh, + kw, + new_kh, + new_kw, + dilations[0], + dilations[1]); } else { VLOG(3) << "Transfilter only support float32/float16 dtype of weights -- do " diff --git a/paddle/fluid/framework/ir/conv_bn_fuse_pass.cc b/paddle/fluid/framework/ir/conv_bn_fuse_pass.cc index 256de39c24cd0..6b21bfa5defc9 100644 --- a/paddle/fluid/framework/ir/conv_bn_fuse_pass.cc +++ b/paddle/fluid/framework/ir/conv_bn_fuse_pass.cc @@ -158,7 +158,7 @@ void recompute_bias_and_weights(const Scope* scope, // ConvTranspose weights are in IOHW format if (conv_type == "conv2d_transpose") { - int kernel_size = weights_shape[2] * weights_shape[3]; + int kernel_size = static_cast(weights_shape[2] * weights_shape[3]); for (int i = 0; i < weights->numel();) { for (int j = 0; j < weights_shape[1]; ++j) { for (int k = 0; k < kernel_size; ++k, ++i) { diff --git a/paddle/fluid/framework/ir/cost_model.cc b/paddle/fluid/framework/ir/cost_model.cc index 0f75697caefcf..a54138060283b 100644 --- a/paddle/fluid/framework/ir/cost_model.cc +++ b/paddle/fluid/framework/ir/cost_model.cc @@ -87,7 +87,7 @@ bool CostData::SetCostData(const ProgramDesc& program, bool event_to_cost_success = true; size_t event_index = 0; for (size_t i = 0; i < op_size; ++i) { - const OpDesc* op_desc = global_block.Op(i); + const OpDesc* op_desc = global_block.Op(static_cast(i)); std::string op_type = op_desc->Type(); while (event_index < main_thread_events.size()) { @@ -133,7 +133,7 @@ bool CostData::SetCostData(const ProgramDesc& program, main_thread_events[op_pop_index]); #endif double time_ms = gpu_time_ms + cpu_time_ms; - op_time_ms_[i] = time_ms; + op_time_ms_[static_cast(i)] = time_ms; } event_index = 0; @@ -141,9 +141,9 @@ bool CostData::SetCostData(const ProgramDesc& program, int stop_profiler_idx = -1; while (event_index < main_thread_events.size()) { if (main_thread_events[event_index].name() == "_start_profiler_") { - start_profiler_idx = event_index; + start_profiler_idx = static_cast(event_index); } else if (main_thread_events[event_index].name() == "_stop_profiler_") { - stop_profiler_idx = event_index; + stop_profiler_idx = static_cast(event_index); break; } ++event_index; diff --git a/paddle/fluid/framework/ir/delete_quant_dequant_filter_op_pass.cc b/paddle/fluid/framework/ir/delete_quant_dequant_filter_op_pass.cc index 6d22db029238e..cfe644a61ea51 100644 --- a/paddle/fluid/framework/ir/delete_quant_dequant_filter_op_pass.cc +++ b/paddle/fluid/framework/ir/delete_quant_dequant_filter_op_pass.cc @@ -192,7 +192,7 @@ void DeleteQuantDequantFilterOpPass::ApplyImpl(ir::Graph* graph) const { 0, platform::errors::InvalidArgument( "Weight scale should be nonzero, but get zero.")); - weight_scale[i] = weight_scale[i] / range; + weight_scale[i] = weight_scale[i] / static_cast(range); } } else if (dequant_type == "fake_quantize_dequantize_abs_max") { // Implement quantize_dequantize_abs_max quantization algorithm @@ -205,7 +205,7 @@ void DeleteQuantDequantFilterOpPass::ApplyImpl(ir::Graph* graph) const { 0, platform::errors::InvalidArgument( "Weight scale should be nonzero, but get zero")); - weight_scale.push_back(abs_max_weight / range); + weight_scale.push_back(abs_max_weight / static_cast(range)); } else { PADDLE_THROW(platform::errors::InvalidArgument( "Unsupported quantize_dequantize op type: %s", dequant_type)); diff --git a/paddle/fluid/framework/ir/delete_quant_dequant_linear_op_pass.cc b/paddle/fluid/framework/ir/delete_quant_dequant_linear_op_pass.cc index 3d56469dc9280..cb6a6e1d5d9dc 100644 --- a/paddle/fluid/framework/ir/delete_quant_dequant_linear_op_pass.cc +++ b/paddle/fluid/framework/ir/delete_quant_dequant_linear_op_pass.cc @@ -135,7 +135,8 @@ void DeleteQuantDequantLinearOpPass::ApplyImpl(ir::Graph* graph) const { input_scale_tensor.dtype())); } - int nums_any_ops = dequantize_linear_op_out->outputs.size(); + int nums_any_ops = + static_cast(dequantize_linear_op_out->outputs.size()); for (int i = 0; i < nums_any_ops; ++i) { auto* any_op_desc = dequantize_linear_op_out->outputs[i]->Op(); any_op_desc->SetAttr("Input_scale_" + quantize_linear_op_x->Var()->Name(), diff --git a/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc b/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc index 68992abad3c89..fdbb1fa29d585 100644 --- a/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc +++ b/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc @@ -108,9 +108,9 @@ static int BuildFusion(Graph* graph, auto alpha = 1.0f; auto beta = 1.0f; - int m = embedding_tensor.dims()[0]; - int n = weightx_tensor.dims()[1]; - int k = embedding_tensor.dims()[1]; + int m = static_cast(embedding_tensor.dims()[0]); + int n = static_cast(weightx_tensor.dims()[1]); + int k = static_cast(embedding_tensor.dims()[1]); // Copy only gate biases values (only actual bias data, not peephole // weights) diff --git a/paddle/fluid/framework/ir/fc_fuse_pass.cc b/paddle/fluid/framework/ir/fc_fuse_pass.cc index cbbac28144320..a037793e59190 100644 --- a/paddle/fluid/framework/ir/fc_fuse_pass.cc +++ b/paddle/fluid/framework/ir/fc_fuse_pass.cc @@ -198,9 +198,9 @@ int FCFusePass::ApplyFCPattern(Graph* graph, bool with_relu) const { auto* weight = scope->FindVar(w->Name())->GetMutable(); auto* weight_data = weight->data(); auto weight_dims = weight->dims(); - int weight_num = product(weight_dims); - int w_h = weight_dims[0]; - int w_w = weight_dims[1]; + int weight_num = static_cast(product(weight_dims)); + int w_h = static_cast(weight_dims[0]); + int w_w = static_cast(weight_dims[1]); if (w_h % 128 == 0 && w_w % 128 == 0) { auto* w_var = scope->Var(w_name); auto* w_tensor = w_var->GetMutable(); diff --git a/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc b/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc index c2d16d182cb9c..9bbf2409aff2d 100644 --- a/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc +++ b/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc @@ -306,7 +306,7 @@ ir::Graph *FuseAdamWPass::FuseAdamWFun(ir::Graph *graph, VLOG(4) << "replace adamw with fuse_adamw"; - AddStatis(found_adamw_count); + AddStatis(static_cast(found_adamw_count)); return graph; } diff --git a/paddle/fluid/framework/ir/fused_multi_transformer_encoder_pass.cc b/paddle/fluid/framework/ir/fused_multi_transformer_encoder_pass.cc index e596e1a1b8489..99bb9c5920685 100644 --- a/paddle/fluid/framework/ir/fused_multi_transformer_encoder_pass.cc +++ b/paddle/fluid/framework/ir/fused_multi_transformer_encoder_pass.cc @@ -1698,8 +1698,8 @@ inline void QKVWeightsBiasProcessFuseQKV(phi::DenseTensor* qkv_w_tensor, inline void TransposeWeights(phi::DenseTensor* weight_tensor) { auto* dev_ctx = static_cast( platform::DeviceContextPool::Instance().Get(platform::CPUPlace())); - int m = weight_tensor->dims()[0]; - int n = weight_tensor->dims()[1]; + int m = static_cast(weight_tensor->dims()[0]); + int n = static_cast(weight_tensor->dims()[1]); phi::DenseTensor tmp_weight_tensor; tmp_weight_tensor.Resize({n, m}); dev_ctx->Alloc(&tmp_weight_tensor); @@ -1816,8 +1816,8 @@ int FusedMultiTransformerEncoderPass::BuildFusion(Graph* graph, .at(3); auto* layer_norm_bias_tensor = scope->FindVar(layer_norm_bias->Name())->GetMutable(); - int dim_embed = layer_norm_bias_tensor->dims()[0]; - int num_head = wq_tensor->dims()[1] / dim_head; + int dim_embed = static_cast(layer_norm_bias_tensor->dims()[0]); + int num_head = static_cast(wq_tensor->dims()[1] / dim_head); QKVWeightsBiasProcess(wq_tensor, wk_tensor, @@ -2631,8 +2631,8 @@ int FusedMultiTransformerEncoderFuseQKVPass::BuildFusion( 3; // 3 for qkv auto* layer_norm_bias_tensor = scope->FindVar(layer_norm_bias->Name())->GetMutable(); - int dim_embed = layer_norm_bias_tensor->dims()[0]; - int num_head = qkv_w_tensor->dims()[1] / 3 / dim_head; + int dim_embed = static_cast(layer_norm_bias_tensor->dims()[0]); + int num_head = static_cast(qkv_w_tensor->dims()[1] / 3 / dim_head); QKVWeightsBiasProcessFuseQKV( qkv_w_tensor, qkv_b_tensor, num_head, dim_head, dim_embed); @@ -3464,7 +3464,7 @@ int MultiDevicesFusedMultiTransformerEncoderPass::BuildFusion( auto* bv_tensor = scope->FindVar(eltadd2_b->Name())->GetMutable(); - int dim_embed = wq_tensor->dims()[0]; + int dim_embed = static_cast(wq_tensor->dims()[0]); QKVWeightsBiasProcess(wq_tensor, wk_tensor, @@ -4242,13 +4242,13 @@ int MultiDevicesFusedMultiTransformerEncoderFuseQKVPass::BuildFusion( // 2. calculate num_head according to wqkv_tensor.shape[1]/3 and dim_head auto* layer_norm_bias_tensor = scope->FindVar(layer_norm_bias->Name())->GetMutable(); - int dim_embed = layer_norm_bias_tensor->dims()[0]; + int dim_embed = static_cast(layer_norm_bias_tensor->dims()[0]); auto reshape_desc = reshape2_0->Op(); int dim_head = PADDLE_GET_CONST(std::vector, reshape_desc->GetAttr("shape")) .at(3) / 3; // 3 for qkv - int num_head = qkv_w_tensor->dims()[1] / 3 / dim_head; + int num_head = static_cast(qkv_w_tensor->dims()[1] / 3 / dim_head); QKVWeightsBiasProcessFuseQKV( qkv_w_tensor, qkv_b_tensor, num_head, dim_head, dim_embed); diff --git a/paddle/fluid/framework/ir/generate_pass.cc b/paddle/fluid/framework/ir/generate_pass.cc index 7e8f06c45f153..17d2bdda56cb9 100644 --- a/paddle/fluid/framework/ir/generate_pass.cc +++ b/paddle/fluid/framework/ir/generate_pass.cc @@ -90,7 +90,7 @@ Attribute GetVarAttrValue(const VarDesc* desc, } else if (attr.has_element_index()) { int element_index = attr.element_index(); if (attr.element_index() < 0) { - element_index += shape.size(); + element_index += static_cast(shape.size()); } if (element_index >= 0 && static_cast(element_index) < shape.size()) { diff --git a/paddle/fluid/framework/ir/graph.cc b/paddle/fluid/framework/ir/graph.cc index caaba441d1ff3..bb08e129981d6 100644 --- a/paddle/fluid/framework/ir/graph.cc +++ b/paddle/fluid/framework/ir/graph.cc @@ -27,7 +27,8 @@ namespace framework { namespace ir { Graph::Graph(const ProgramDesc &program) - : Graph(program, 0, program.Block(0).AllOps().size()) {} + : Graph( + program, 0, static_cast(program.Block(0).AllOps().size())) {} Graph::Graph(const ProgramDesc &program, const int64_t start_op_index, @@ -52,7 +53,8 @@ Graph::Graph(const ProgramDesc &program, platform::errors::InvalidArgument("Can't construct a graph from this " "program, it doesn't have a block")); - const int64_t block_op_size = program_.Block(0).AllOps().size(); + const int64_t block_op_size = + static_cast(program_.Block(0).AllOps().size()); PADDLE_ENFORCE_LE(end_op_index, block_op_size, platform::errors::InvalidArgument( @@ -70,7 +72,7 @@ Graph::Graph(const ProgramDesc &program, for (size_t idx = 1; idx < program_.Size(); ++idx) { std::unique_ptr sub_graph = std::make_unique(program_.Block(idx), this); - sub_graph->block_id_ = idx; + sub_graph->block_id_ = static_cast(idx); sub_graphs_.push_back(std::move(sub_graph)); } } else { @@ -79,7 +81,8 @@ Graph::Graph(const ProgramDesc &program, } Graph::Graph(const BlockDesc &block, const Graph *main_graph) - : Graph(block, main_graph, 0, block.AllOps().size()) {} + : Graph(block, main_graph, 0, static_cast(block.AllOps().size())) { +} Graph::Graph(const BlockDesc &block, const Graph *main_graph, @@ -359,7 +362,7 @@ std::unique_ptr Graph::CloneSubGraph(const size_t idx) { std::make_unique(this->program_.Block(idx), this); cloned_sub_graph->ReleaseNodes(); cloned_sub_graph->num_node_created_ = 0; - cloned_sub_graph->block_id_ = idx; + cloned_sub_graph->block_id_ = static_cast(idx); std::unordered_map origin_to_cloned; for (auto *n : this->sub_graphs_.at(idx)->Nodes()) { PADDLE_ENFORCE_NOT_NULL( diff --git a/paddle/fluid/framework/ir/graph_helper.cc b/paddle/fluid/framework/ir/graph_helper.cc index 87a710cd036af..3e744e18bf6c8 100644 --- a/paddle/fluid/framework/ir/graph_helper.cc +++ b/paddle/fluid/framework/ir/graph_helper.cc @@ -275,7 +275,7 @@ std::vector TopologyDfsSortOperations(const Graph &graph) { // build in_degree for (auto *node : graph.Nodes()) { if (node->IsOp()) { - in_degree[node] += node->inputs.size(); + in_degree[node] += static_cast(node->inputs.size()); } else if (node->IsVar() && node->inputs.empty()) { // put all the inputs of the whole graph ready. set_out_ops_ready(node); @@ -291,7 +291,7 @@ std::vector TopologyDfsSortOperations(const Graph &graph) { } // traverse the graph - int num_ops = op_queue.size(); + int num_ops = static_cast(op_queue.size()); while (num_ops) { for (auto cur_op : op_queue) { if (!cur_op || in_degree[cur_op] > 0) continue; @@ -636,7 +636,7 @@ void UpdateControlOpSkipEagerDeletionVars(const Node &node, auto origin_program = graph.OriginProgram(); auto &block = origin_program.Block(graph_idx); for (size_t j = 0; j < block.OpSize(); ++j) { - auto *op = block.Op(j); + auto *op = block.Op(static_cast(j)); if (op->Type() == control_type && op->HasAttr("skip_eager_deletion_vars")) { if (op->InputArgumentNames() == node.Op()->InputArgumentNames() && @@ -815,10 +815,10 @@ void GraphToProgram(const Graph &graph, if (idx == kRootBlockIndex) continue; if (static_cast(idx) < program_pb.blocks_size()) { - block = program_pb.mutable_blocks(idx); + block = program_pb.mutable_blocks(idx); // NOLINT } else { block = program_pb.add_blocks(); - block->set_idx(idx); + block->set_idx(idx); // NOLINT block->set_parent_idx(kRootBlockIndex); } diff --git a/paddle/fluid/framework/ir/graph_pattern_detector.cc b/paddle/fluid/framework/ir/graph_pattern_detector.cc index cca14f229b85e..06107bdc733ce 100644 --- a/paddle/fluid/framework/ir/graph_pattern_detector.cc +++ b/paddle/fluid/framework/ir/graph_pattern_detector.cc @@ -2876,10 +2876,10 @@ PDNode *patterns::MultipleQuantize::operator()() { // find nodes that are inputs to quantize operators prev_out->assert_more([&](Node *node) { - int counter = std::count_if( + int counter = static_cast(std::count_if( node->outputs.begin(), node->outputs.end(), [&](Node const *iter) { return iter && iter->IsOp() && iter->Op()->Type() == "quantize"; - }); + })); return (counter > 1); }); diff --git a/paddle/fluid/framework/ir/graph_viz_pass.cc b/paddle/fluid/framework/ir/graph_viz_pass.cc index 8403d9075e3bf..4f430ba4041d6 100644 --- a/paddle/fluid/framework/ir/graph_viz_pass.cc +++ b/paddle/fluid/framework/ir/graph_viz_pass.cc @@ -70,8 +70,11 @@ void GraphVizPass::ApplyImpl(ir::Graph* graph) const { // TODO(wilber): GraphToProgram seems have bugs. for (size_t i = 0; i < program_desc.Size(); ++i) { for (size_t j = 0; j < program_desc.Block(i).OpSize(); ++j) { - if (program_desc.Block(i).Op(j)->Type() == "tensorrt_engine") { - program_desc.Block(i).Op(j)->RemoveAttr("sub_block"); + if (program_desc.Block(i).Op(static_cast(j))->Type() == + "tensorrt_engine") { + program_desc.Block(i) + .Op(static_cast(j)) + ->RemoveAttr("sub_block"); } } } @@ -83,7 +86,7 @@ void GraphVizPass::ApplyImpl(ir::Graph* graph) const { program_path = optim_cache_dir + "/" + program_path; } std::ofstream file(program_path.c_str(), std::ios::binary); - file.write(program_bytes.c_str(), program_bytes.size()); + file.write(program_bytes.c_str(), program_bytes.size()); // NOLINT file.close(); VLOG(3) << "serialize program to " << program_path; } diff --git a/paddle/fluid/framework/ir/layer_norm_fuse_pass.cc b/paddle/fluid/framework/ir/layer_norm_fuse_pass.cc index 9020c4871ee08..fc894c07e0966 100644 --- a/paddle/fluid/framework/ir/layer_norm_fuse_pass.cc +++ b/paddle/fluid/framework/ir/layer_norm_fuse_pass.cc @@ -306,7 +306,8 @@ void LayerNormFusePass::ApplyImpl(Graph* graph) const { } int begin_norm_axis = mean_dim.front(); - if (begin_norm_axis < 0) begin_norm_axis += x_shape.size(); + if (begin_norm_axis < 0) + begin_norm_axis += static_cast(x_shape.size()); const auto& gamma_shape = gamma->Var()->GetShape(); const auto& beta_shape = beta->Var()->GetShape(); diff --git a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc index b85a88eb7e0b6..b0729abfcf883 100644 --- a/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc +++ b/paddle/fluid/framework/ir/memory_optimize_pass/eager_deletion_pass.cc @@ -66,11 +66,12 @@ static int64_t GetMemorySize( platform::errors::InvalidArgument( "Var(%s) must be phi::DenseTensor.", var_name)); auto dims = var_desc->GetShape(); - return SizeOfType(var_desc->GetDataType()) * - std::accumulate(dims.begin(), - dims.end(), - static_cast(1), - std::multiplies()); // NOLINT + return static_cast( + SizeOfType(var_desc->GetDataType()) * + std::accumulate(dims.begin(), + dims.end(), + static_cast(1), + std::multiplies())); // NOLINT } // Split all variables in the graph into phi::DenseTensor and @@ -176,8 +177,8 @@ static OpToVarNameSetMap ShrinkGCVars( }); int64_t accumulated_size = 0; - int64_t size_threshold = - static_cast(fraction_of_memory_size * place_to_size[place]); + int64_t size_threshold = static_cast( + fraction_of_memory_size * place_to_size[place]); // NOLINT for (size_t i = 0; i < gc_vars.size() && accumulated_size < size_threshold; ++i) { partial_vars[gc_vars[i].op_].insert(gc_vars[i].name_); diff --git a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc index 3332d5128f805..d5d3804e75ca3 100644 --- a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc +++ b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc @@ -278,7 +278,8 @@ void BatchMergePass::ApplyImpl(ir::Graph* graph) const { scale_op.SetInput("X", {sum_out_var_node->Var()->Name()}); // NOTE: inplace scale. scale_op.SetOutput("Out", {sum_out_var_node->Var()->Name()}); - scale_op.SetAttr("scale", static_cast(1.0f / num_repeats)); + scale_op.SetAttr( + "scale", static_cast(1.0f / static_cast(num_repeats))); scale_op.SetAttr(OpProtoAndCheckerMaker::OpRoleAttrName(), static_cast(OpRole::kBackward)); diff --git a/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc b/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc index cadf613f798c3..35f4e4830d882 100644 --- a/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc +++ b/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc @@ -1151,7 +1151,8 @@ int DistSSAGraphBuilder::CreateRPCOp(ir::Graph *result, ir::Node *node) const { "Parameter and Parameter@Grad.", node->Name(), OpProtoAndCheckerMaker::OpRoleVarAttrName())); - op_dev_id = GetAppropriateDeviceID({send_param_grad[1]}); + op_dev_id = + static_cast(GetAppropriateDeviceID({send_param_grad[1]})); VLOG(10) << "send grad " << input_var_names[0] << " origin " << send_param_grad[1] << " place: " << op_dev_id; for (auto &varname : input_var_names) { @@ -1173,7 +1174,7 @@ int DistSSAGraphBuilder::CreateRPCOp(ir::Graph *result, ir::Node *node) const { << " get grad place: " << recv_param_grad[1] << " place: " << op_dev_id; } else { - op_dev_id = GetAppropriateDeviceID(output_var_names); + op_dev_id = static_cast(GetAppropriateDeviceID(output_var_names)); } for (auto &varname : output_var_names) { sharded_var_device_.emplace(varname, op_dev_id); @@ -1213,7 +1214,7 @@ int DistSSAGraphBuilder::CreateRPCOp(ir::Graph *result, ir::Node *node) const { op_handle->SetDeviceContext(p, platform::DeviceContextPool::Instance().Get(p)); - SetOpInputsAllPlaces(result, node, places_.size()); + SetOpInputsAllPlaces(result, node, static_cast(places_.size())); for (ir::Node *output : node->outputs) { int outvar_dev_id = op_dev_id; if (node->Op()->Type() == "fetch_barrier") { @@ -1258,7 +1259,7 @@ int DistSSAGraphBuilder::CreateDistTrainOp(ir::Graph *result, op_dev_id = GetVarDeviceID(input_var_names[0]); if (strategy_.reduce_ == details::BuildStrategy::ReduceStrategy::kAllReduce) { - op_dev_id = GetAppropriateDeviceID(input_var_names); + op_dev_id = static_cast(GetAppropriateDeviceID(input_var_names)); for (auto &varname : input_var_names) { sharded_var_device_.emplace(varname, op_dev_id); } diff --git a/paddle/fluid/framework/ir/multihead_matmul_fuse_pass.cc b/paddle/fluid/framework/ir/multihead_matmul_fuse_pass.cc index a48cfde23853d..a950ec191a4bf 100644 --- a/paddle/fluid/framework/ir/multihead_matmul_fuse_pass.cc +++ b/paddle/fluid/framework/ir/multihead_matmul_fuse_pass.cc @@ -662,7 +662,8 @@ inline void QKVWeightsProcess(phi::DenseTensor* wq_tensor, tmp_combined_w_tensor.mutable_data(platform::CPUPlace()); std::vector w_vec = {wq_data, wk_data, wv_data}; - int dims_h = combined_w_dims[0], dims_w = combined_w_dims[2]; + int dims_h = static_cast(combined_w_dims[0]), + dims_w = static_cast(combined_w_dims[2]); // Combine the three fc weights together. for (int i = 0; i < dims_h; i++) { for (int j = 0; j < 3; j++) { @@ -1379,7 +1380,8 @@ int MultiHeadMatmulV3FusePass::BuildFusionV3(Graph* graph, tmp_combined_w_tensor.mutable_data(platform::CPUPlace()); std::vector w_vec = {wq_data, wk_data, wv_data}; - int dims_h = combined_w_dims[0], dims_w = combined_w_dims[2]; + int dims_h = static_cast(combined_w_dims[0]), + dims_w = static_cast(combined_w_dims[2]); // Combine the three fc weights together. for (int i = 0; i < dims_h; i++) { for (int j = 0; j < 3; j++) { diff --git a/paddle/fluid/framework/ir/multihead_matmul_roformer_fuse_pass.cc b/paddle/fluid/framework/ir/multihead_matmul_roformer_fuse_pass.cc index 7a28236a8a203..c974f5fafd68b 100644 --- a/paddle/fluid/framework/ir/multihead_matmul_roformer_fuse_pass.cc +++ b/paddle/fluid/framework/ir/multihead_matmul_roformer_fuse_pass.cc @@ -467,7 +467,8 @@ int MultiHeadMatmulRoformerFusePass::BuildFusion(Graph* graph, tmp_combined_w_tensor.mutable_data(platform::CPUPlace()); std::vector w_vec = {wq_data, wk_data, wv_data}; - int dims_h = combined_w_dims[0], dims_w = combined_w_dims[2]; + int dims_h = static_cast(combined_w_dims[0]), + dims_w = static_cast(combined_w_dims[2]); // Combine the three fc weights together. for (int i = 0; i < dims_h; i++) { for (int j = 0; j < 3; j++) { diff --git a/paddle/fluid/framework/ir/pass_builder.cc b/paddle/fluid/framework/ir/pass_builder.cc index daab1eb1cc493..d9f8eaf6fc4f8 100644 --- a/paddle/fluid/framework/ir/pass_builder.cc +++ b/paddle/fluid/framework/ir/pass_builder.cc @@ -38,7 +38,7 @@ void PassBuilder::RemovePass(size_t idx) { idx, platform::errors::InvalidArgument( "Passes size is %d, %d is not a valid index.", passes_.size(), idx)); - passes_.erase(passes_.begin() + idx); + passes_.erase(passes_.begin() + idx); // NOLINT } std::shared_ptr PassBuilder::InsertPass(size_t idx, @@ -50,7 +50,7 @@ std::shared_ptr PassBuilder::InsertPass(size_t idx, "Passes size is %d, %d is not a valid index.", passes_.size(), idx)); std::shared_ptr pass( ir::PassRegistry::Instance().Get(pass_type).release()); - passes_.insert(passes_.begin() + idx, std::move(pass)); + passes_.insert(passes_.begin() + idx, std::move(pass)); // NOLINT return passes_[idx]; } diff --git a/paddle/fluid/framework/ir/pass_test_util.cc b/paddle/fluid/framework/ir/pass_test_util.cc index 8a092d5797d66..ea3532135fafb 100644 --- a/paddle/fluid/framework/ir/pass_test_util.cc +++ b/paddle/fluid/framework/ir/pass_test_util.cc @@ -167,10 +167,10 @@ bool RunPassAndAssert(Graph* graph, int added_nodes_count) { if (!TestIsReachable(*graph, from, to)) return false; - int original_nodes_num = graph->Nodes().size(); + int original_nodes_num = static_cast(graph->Nodes().size()); auto pass = PassRegistry::Instance().Get(pass_name); pass->Apply(graph); - int current_nodes_num = graph->Nodes().size(); + int current_nodes_num = static_cast(graph->Nodes().size()); if (!TestIsReachable(*graph, from, to)) return false; diff --git a/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc b/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc index 0dd8a79ac6a5b..cdb10e4a16805 100644 --- a/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc +++ b/paddle/fluid/framework/ir/quant_conv2d_dequant_fuse_pass.cc @@ -419,13 +419,15 @@ void QuantDequantFusePass::FuseDequant(ir::Graph* graph, "Channel scale tensor's place should be CPU.")); const float* channel_scale_data = channel_scale_tensor.data(); for (int i = 0; i < channel_scale_tensor.numel(); i++) { - weight_scale.push_back(channel_scale_data[i] / range); + weight_scale.push_back(channel_scale_data[i] / + static_cast(range)); } nodes2rm.insert(dequant_channel_scale_node); } else { float max_range = PADDLE_GET_CONST(float, dequant_op_node->Op()->GetAttr("max_range")); - weight_scale.push_back((range * range) / max_range / range); + weight_scale.push_back(static_cast(range * range) / max_range / + static_cast(range)); } // Convert weight to fp32 range @@ -522,7 +524,7 @@ void QuantDequantFusePass::FuseDequant(ir::Graph* graph, static_cast(w_dims[0]), weight_scale.size())); for (int j = 0; j < weight_tensor->numel(); j++) { - int inner_size = w_dims[1] * w_dims[2] * w_dims[3]; + int inner_size = static_cast(w_dims[1] * w_dims[2] * w_dims[3]); quantized_weight_data[j] *= weight_scale[j / inner_size]; } } else if (quantized_op_type == "conv2d_transpose") { @@ -553,7 +555,7 @@ void QuantDequantFusePass::FuseDequant(ir::Graph* graph, static_cast(w_dims[1]), weight_scale.size())); for (int j = 0; j < weight_tensor->numel(); j++) { - int inner_size = w_dims[2] * w_dims[3]; + int inner_size = static_cast(w_dims[2] * w_dims[3]); quantized_weight_data[j] *= weight_scale[(j / inner_size) % w_dims[1]]; } diff --git a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc index 72d1a4b323d54..ee15aebc426ab 100644 --- a/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc +++ b/paddle/fluid/framework/ir/repeated_fc_relu_fuse_pass.cc @@ -106,7 +106,7 @@ static int FindFCIdx(Node* x, const std::string& act_type = "relu") { for (size_t k = 0; k < x->outputs.size(); ++k) { auto* out_op = x->outputs[k]; if (IsFCWithAct(out_op, act_type) && out_op->outputs.size() == 1U) { - return k; + return static_cast(k); } } return -1; @@ -120,7 +120,7 @@ static int FindInputIdx(Node* n, } for (size_t i = 0; i < n->inputs.size(); ++i) { if (n->inputs[i]->Name() == n->Op()->Input(name)[0]) { - return i; + return static_cast(i); } } return -1; diff --git a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc index 5d38a9c536ad1..3674f463e74aa 100644 --- a/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc +++ b/paddle/fluid/framework/ir/shuffle_channel_detect_pass.cc @@ -100,11 +100,11 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { std::string input_name = input_node->Name(); std::string output_name = reshape2_out->Name(); - auto reshape1_shape = + std::vector reshape1_shape = PADDLE_GET_CONST(std::vector, reshape1_desc->GetAttr("shape")); - auto reshape2_shape = + std::vector reshape2_shape = PADDLE_GET_CONST(std::vector, reshape2_desc->GetAttr("shape")); - auto trans_axis = + std::vector trans_axis = PADDLE_GET_CONST(std::vector, trans_desc->GetAttr("axis")); auto* block1 = reshape1_desc->Block(); auto* block2 = reshape2_desc->Block(); @@ -125,12 +125,12 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { constexpr int64_t copy_dim_val = 0; for (size_t i = 0; i < reshape1_shape.size(); i++) { if (reshape1_shape[i] == copy_dim_val) { - reshape1_shape[i] = x_shape1[i]; + reshape1_shape[i] = static_cast(x_shape1[i]); } } for (size_t i = 0; i < reshape2_shape.size(); i++) { if (reshape2_shape[i] == copy_dim_val) { - reshape2_shape[i] = x_shape2[i]; + reshape2_shape[i] = static_cast(x_shape2[i]); } } constexpr int64_t unk_dim_idx = -1; @@ -141,7 +141,7 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape1_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape1_shape[i] = + reshape1_shape[i] = static_cast( std::accumulate(x_shape1.begin(), x_shape1.end(), static_cast(1), @@ -149,7 +149,7 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { std::accumulate(reshape1_shape.begin(), reshape1_shape.end(), static_cast(-1), - std::multiplies()); // NOLINT + std::multiplies())); // NOLINT break; } } @@ -161,7 +161,7 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { if ((reshape2_shape[i] == unk_dim_idx) && (i != 0)) { // there is no sufficient info if (!all_positive) return; - reshape2_shape[i] = + reshape2_shape[i] = static_cast( std::accumulate(x_shape2.begin(), x_shape2.end(), static_cast(1), @@ -169,7 +169,7 @@ void ShuffleChannelDetectPass::ApplyImpl(ir::Graph* graph) const { std::accumulate(reshape2_shape.begin(), reshape2_shape.end(), static_cast(-1), - std::multiplies()); // NOLINT + std::multiplies())); // NOLINT break; } } diff --git a/paddle/fluid/framework/ir/subgraph_detector.cc b/paddle/fluid/framework/ir/subgraph_detector.cc index cce72ae161b51..a657620323592 100644 --- a/paddle/fluid/framework/ir/subgraph_detector.cc +++ b/paddle/fluid/framework/ir/subgraph_detector.cc @@ -122,7 +122,7 @@ void SubgraphDetector::MarkNodesInsideSubGraph() { using node_map_t = std::map; // Find the ancestor id of a node. int UnionFindGetAncestor(const node_map_t &node_map, size_t id) { - int tmp = id; + int tmp = static_cast(id); do { tmp = Agent(node_map.at(tmp)).union_find_parent(); } while (Agent(node_map.at(tmp)).union_find_parent() != tmp); @@ -134,8 +134,8 @@ void UnionFindCombine(const node_map_t &node_map, size_t a, size_t b) { int a_ancestor = UnionFindGetAncestor(node_map, a); int b_ancestor = UnionFindGetAncestor(node_map, b); Agent(node_map.at(b_ancestor)).set_union_find_parent(a_ancestor); - Agent(node_map.at(a)).set_union_find_parent(a_ancestor); - Agent(node_map.at(b)).set_union_find_parent(a_ancestor); + Agent(node_map.at(a)).set_union_find_parent(a_ancestor); // NOLINT + Agent(node_map.at(b)).set_union_find_parent(a_ancestor); // NOLINT } // This is a simple representation of a graph. diff --git a/paddle/fluid/framework/ir/trt_delete_weight_dequant_linear_op_pass.cc b/paddle/fluid/framework/ir/trt_delete_weight_dequant_linear_op_pass.cc index 9c51254029c58..6c6174b926701 100644 --- a/paddle/fluid/framework/ir/trt_delete_weight_dequant_linear_op_pass.cc +++ b/paddle/fluid/framework/ir/trt_delete_weight_dequant_linear_op_pass.cc @@ -255,7 +255,7 @@ void TrtDeleteWeightQuantDequantLinearOpPass::ApplyImpl( auto weight_scale_nums = weight_scale_tensor->numel(); for (int i = 0; i < weight_scale_nums; i++) { - weight_scale.push_back(weight_scale_data[i] / range); + weight_scale.push_back(weight_scale_data[i] / static_cast(range)); } // dequant weight @@ -292,7 +292,7 @@ void TrtDeleteWeightQuantDequantLinearOpPass::ApplyImpl( "conv2d_fusion)'s weight dims should be 4.")); for (int i = 0; i < weight_tensor->numel(); i++) { - int inner_size = w_dims[1] * w_dims[2] * w_dims[3]; + int inner_size = static_cast(w_dims[1] * w_dims[2] * w_dims[3]); weight_data_tmp[i] = static_cast(quantized_weight_data[i]) * weight_scale[i / inner_size]; } @@ -313,7 +313,7 @@ void TrtDeleteWeightQuantDequantLinearOpPass::ApplyImpl( "When quant_axis == 1 means use per_channel quant_dequant, " "only conv2d_transpose weight dims equal 4.")); for (int i = 0; i < weight_tensor->numel(); i++) { - int inner_size = w_dims[2] * w_dims[3]; + int inner_size = static_cast(w_dims[2] * w_dims[3]); weight_data_tmp[i] = static_cast(quantized_weight_data[i]) * weight_scale[(i / inner_size) % w_dims[1]]; } diff --git a/paddle/fluid/framework/ir/vit_attention_fuse_pass.cc b/paddle/fluid/framework/ir/vit_attention_fuse_pass.cc index c27c7430c2af6..23db75266310c 100644 --- a/paddle/fluid/framework/ir/vit_attention_fuse_pass.cc +++ b/paddle/fluid/framework/ir/vit_attention_fuse_pass.cc @@ -62,7 +62,7 @@ bool HasScale(OpDesc* const op_ptr, name->clear(); std::unordered_map attr_map = op_ptr->GetAttrMap(); std::unordered_map::iterator iter; - int len = regexp.size(); + int len = static_cast(regexp.size()); for (iter = attr_map.begin(); iter != attr_map.end(); iter++) { if (regexp == iter->first.substr(0, len)) { *name = iter->first; diff --git a/paddle/fluid/framework/lod_tensor.cc b/paddle/fluid/framework/lod_tensor.cc index 38ba51095adac..32675d5fa09c1 100644 --- a/paddle/fluid/framework/lod_tensor.cc +++ b/paddle/fluid/framework/lod_tensor.cc @@ -60,14 +60,14 @@ LoD SliceInLevel(const LoD &in, LoD res; res.resize(in.size() - level); // copy the first level - res[0].assign(in[level].begin() + elem_begin, - in[level].begin() + elem_end + 1); + res[0].assign(in[level].begin() + elem_begin, // NOLINT + in[level].begin() + elem_end + 1); // NOLINT for (size_t lvl = 1; lvl < res.size(); lvl++) { const auto &in_level = in[level + lvl]; const auto &above_level = res[lvl - 1]; auto &out_level = res[lvl]; - out_level.assign(in_level.begin() + above_level.front(), - in_level.begin() + above_level.back() + 1); + out_level.assign(in_level.begin() + above_level.front(), // NOLINT + in_level.begin() + above_level.back() + 1); // NOLINT } for (auto &item : res) { // to make the first offset equals 0, all the elements minus the first @@ -160,7 +160,7 @@ bool CheckAbsLoD(const LoD &in, int tensor_height) { // the same(the height of underlying tensor). if (level.front() != 0) return false; if (tensor_height < 0) { - tensor_height = level.back(); + tensor_height = static_cast(level.back()); } else if (static_cast(tensor_height) != level.back()) { return false; } @@ -392,7 +392,8 @@ std::vector SplitLoDTensor( phi::DenseTensor dst; if (src.lod().empty()) { - auto sliced_src = src.Slice(begin, end); + auto sliced_src = + src.Slice(static_cast(begin), static_cast(end)); auto &dst_place = places[i]; framework::TensorCopy(sliced_src, dst_place, &dst); } else { @@ -400,7 +401,8 @@ std::vector SplitLoDTensor( GetSubLoDAndAbsoluteOffset(src.lod(), begin, end, 0); auto &offset = lod_and_offset.second; - auto sliced_src = src.Slice(offset.first, offset.second); + auto sliced_src = src.Slice(static_cast(offset.first), + static_cast(offset.second)); auto &dst_place = places[i]; framework::TensorCopy(sliced_src, dst_place, &dst); @@ -508,7 +510,7 @@ void MergeLoDTensor(phi::DenseTensor *target, int begin = 0; for (auto *src : lod_tensors) { - int end = begin + src->dims()[0]; + int end = static_cast(begin + src->dims()[0]); if (end == begin) { continue; } diff --git a/paddle/fluid/framework/multi_trainer.cc b/paddle/fluid/framework/multi_trainer.cc index c1ffe1f46e877..b7eb7b234c9d7 100644 --- a/paddle/fluid/framework/multi_trainer.cc +++ b/paddle/fluid/framework/multi_trainer.cc @@ -54,7 +54,7 @@ void MultiTrainer::Initialize(const TrainerDesc& trainer_desc, dataset->GetReaders(); VLOG(3) << "readers num: " << readers.size(); // change thread num to readers num - thread_num_ = readers.size(); + thread_num_ = static_cast(readers.size()); VLOG(3) << "worker thread num: " << thread_num_; workers_.resize(thread_num_); diff --git a/paddle/fluid/framework/naive_executor.cc b/paddle/fluid/framework/naive_executor.cc index 066b2150d4084..9f8e9ed80ca46 100644 --- a/paddle/fluid/framework/naive_executor.cc +++ b/paddle/fluid/framework/naive_executor.cc @@ -215,7 +215,7 @@ void NaiveExecutor::MakeReusePlan( const auto &reuse_name = reuse_table.at(name); auto it = std::find(cluster_names.begin(), cluster_names.end(), reuse_name); - int idx = it - cluster_names.begin(); + int idx = static_cast(it - cluster_names.begin()); auto *var = scope_->FindVar(name); auto *reuse_var = scope_->FindVar(reuse_name); if (var && reuse_var && var->IsType() && diff --git a/paddle/fluid/framework/new_executor/garbage_collector/event_garbage_collector.cc b/paddle/fluid/framework/new_executor/garbage_collector/event_garbage_collector.cc index 3c837e7ac6edc..e826c94712568 100644 --- a/paddle/fluid/framework/new_executor/garbage_collector/event_garbage_collector.cc +++ b/paddle/fluid/framework/new_executor/garbage_collector/event_garbage_collector.cc @@ -132,7 +132,7 @@ void InterpreterCoreEventGarbageCollector::Add( } else { { // lock guard std::lock_guard guard(spinlock_); - cur_memory_size_ += garbage->size(); + cur_memory_size_ += static_cast(garbage->size()); garbages_->push_back(std::move(garbage)); events_[ctx] = event; diff --git a/paddle/fluid/framework/new_executor/garbage_collector/fast_garbage_collector.cc b/paddle/fluid/framework/new_executor/garbage_collector/fast_garbage_collector.cc index 09b004e65b411..4bc8b298012ab 100644 --- a/paddle/fluid/framework/new_executor/garbage_collector/fast_garbage_collector.cc +++ b/paddle/fluid/framework/new_executor/garbage_collector/fast_garbage_collector.cc @@ -72,7 +72,7 @@ void InterpreterCoreFastGarbageCollector::Add(Garbage garbage) { std::unique_ptr pending_delete_garbages; { // lock guard std::lock_guard guard(spinlock_); - cur_memory_size_ += garbage->size(); + cur_memory_size_ += static_cast(garbage->size()); garbages_->push_back(std::move(garbage)); if (cur_memory_size_ >= max_memory_size_) { diff --git a/paddle/fluid/framework/new_executor/garbage_collector/no_event_garbage_collector.cc b/paddle/fluid/framework/new_executor/garbage_collector/no_event_garbage_collector.cc index 08eea9f67d031..3b7ebc18f36da 100644 --- a/paddle/fluid/framework/new_executor/garbage_collector/no_event_garbage_collector.cc +++ b/paddle/fluid/framework/new_executor/garbage_collector/no_event_garbage_collector.cc @@ -89,7 +89,7 @@ void InterpreterCoreNoEventGarbageCollector::Add( } else { // lock guard std::lock_guard guard(spinlock_); - cur_memory_size_ += garbage->size(); + cur_memory_size_ += static_cast(garbage->size()); garbages_->emplace_back(std::move(garbage)); ctxs_.insert(ctx); diff --git a/paddle/fluid/framework/new_executor/interpreter/execution_config.cc b/paddle/fluid/framework/new_executor/interpreter/execution_config.cc index cf3195bb8c296..8383b1fdd1790 100644 --- a/paddle/fluid/framework/new_executor/interpreter/execution_config.cc +++ b/paddle/fluid/framework/new_executor/interpreter/execution_config.cc @@ -50,7 +50,7 @@ inline std::tuple GetThreadPoolConfig(const phi::Place& place, num_device_threads = 0; num_host_threads = 4; } else { - processor_count = std::thread::hardware_concurrency(); + processor_count = static_cast(std::thread::hardware_concurrency()); if (processor_count) { if (platform::is_gpu_place(place)) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -91,8 +91,8 @@ inline std::tuple GetThreadPoolConfig(const phi::Place& place, if (device_count) { auto num = processor_count / device_count / 2 - (kNumGcThreads + num_device_threads); - num_host_threads = - num > 0 ? (num > kHostNumThreads ? kHostNumThreads : num) : 1; + num_host_threads = static_cast( + num > 0 ? (num > kHostNumThreads ? kHostNumThreads : num) : 1); } } } diff --git a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc index 9d114ed0c19f7..a717a3ed09531 100644 --- a/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc +++ b/paddle/fluid/framework/new_executor/interpreter/interpreter_util.cc @@ -648,7 +648,8 @@ void BuildOpFuncList(const platform::Place& place, op_func_node.input_index = ins_name2id; op_func_node.output_index = outs_name2id; - const OperatorDistAttr* dist_attr = block.Op(i)->DistAttr(); + const OperatorDistAttr* dist_attr = + block.Op(static_cast(i))->DistAttr(); if (dist_attr) { if (dist_attr->execution_stream() != distributed::auto_parallel::kDefault) { diff --git a/paddle/fluid/framework/new_executor/interpreter/static_build.cc b/paddle/fluid/framework/new_executor/interpreter/static_build.cc index bf9b257c01c83..69b4920050925 100644 --- a/paddle/fluid/framework/new_executor/interpreter/static_build.cc +++ b/paddle/fluid/framework/new_executor/interpreter/static_build.cc @@ -80,9 +80,9 @@ bool BlockCanBeStaticBuilt(const framework::BlockDesc& block) { bool has_structured_kernel = phi::KernelFactory::Instance().HasStructuredKernel(op_type); - KernelCode kernel_code = (in_black_list << 7) + (is_operator_base << 6) + - (is_custom_op << 5) + (use_mkldnn << 4) + - (has_structured_kernel << 2); + KernelCode kernel_code = static_cast( + (in_black_list << 7) + (is_operator_base << 6) + (is_custom_op << 5) + + (use_mkldnn << 4) + (has_structured_kernel << 2)); if (!OpsCanSkipedFakeAllocInStaticBuild.count(op_type)) { if (in_black_list || (is_operator_base && diff --git a/paddle/fluid/framework/new_executor/new_executor_defs.cc b/paddle/fluid/framework/new_executor/new_executor_defs.cc index 007cd16e7a607..f4ca2f20d01ae 100644 --- a/paddle/fluid/framework/new_executor/new_executor_defs.cc +++ b/paddle/fluid/framework/new_executor/new_executor_defs.cc @@ -92,7 +92,7 @@ void VariableScope::AddVar(const std::string& name, framework::VarDesc* var_desc) { if (!HasVar(name)) { auto id = VarSize(); - name2id_[name] = id; + name2id_[name] = static_cast(id); vec_meta_info_.emplace_back(0, var_desc); if (local_scope_ != nullptr) { var_list_.push_back(local_scope_->FindVar(name)); diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index 37cecdad1308e..b3b668e05ffc4 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -174,7 +174,7 @@ void NewIRInterpreter::reset_scope(Scope* new_scope) { var_scope_.SetScope(new_scope); scope_ = new_scope; for (size_t i = 0; i < variable_list_.size(); i++) { - const auto& var_name = GetNameById(i); + const auto& var_name = GetNameById(static_cast(i)); variable_list_[i] = new_scope->FindVar(var_name); } // The index should be assured valid, cause the InterpreterCore may not be @@ -758,17 +758,19 @@ void NewIRInterpreter::CheckGC(InstructionBase* instr) { #endif for (auto var_id : instr->GCCheckVars()) { - VLOG(4) << "GC:" << GetNameById(var_id) << ", id:" << var_id - << ", ref:" << refs_[var_id]->DynamicRef(); + VLOG(4) << "GC:" << GetNameById(static_cast(var_id)) + << ", id:" << var_id << ", ref:" << refs_[var_id]->DynamicRef(); bool is_ready = refs_[var_id]->CheckAndDecrease(); // ignore all persistable var while GCphi - if (parameter_var_names_.count(GetNameById(var_id))) { - VLOG(4) << GetNameById(var_id) << " is a parameter, skip gc"; + if (parameter_var_names_.count(GetNameById(static_cast(var_id)))) { + VLOG(4) << GetNameById(static_cast(var_id)) + << " is a parameter, skip gc"; continue; } if (is_ready) { - VLOG(6) << "Async delete variable with name : " << GetNameById(var_id); + VLOG(6) << "Async delete variable with name : " + << GetNameById(static_cast(var_id)); gc_->Add(refs_[var_id]->Var(), instr); } } @@ -804,13 +806,13 @@ void NewIRInterpreter::CalculateLastLiveOps() { for (auto var_id : gc_check_vars) { Scope* inner_scope = InnerScope(); paddle::framework::Variable* var = - inner_scope->FindVar(GetNameById(var_id)); + inner_scope->FindVar(GetNameById(static_cast(var_id))); if (var->IsType() || var->IsType() || var->IsType()) { last_live_ops_[var_id].insert(op_idx); } else { - VLOG(4) << "not clear " << GetNameById(var_id) << " after " - << instr->Name() << " because its type is " + VLOG(4) << "not clear " << GetNameById(static_cast(var_id)) + << " after " << instr->Name() << " because its type is " << framework::ToTypeName(var->Type()); } } @@ -854,14 +856,15 @@ void NewIRInterpreter::CalculateLastLiveOps() { } } if (not_before_any) { - VLOG(6) << "last live op of var " << i << " " << GetNameById(i) << " : " - << item << " " << vec_instruction_base_[item]->Name(); + VLOG(6) << "last live op of var " << i << " " + << GetNameById(static_cast(i)) << " : " << item << " " + << vec_instruction_base_[item]->Name(); minumum_last_live_ops.insert(item); vec_instruction_base_[item]->AddGCCheckVar(i); } } last_live_ops_[i] = minumum_last_live_ops; - var_ref_count_[i] = last_live_ops_[i].size(); + var_ref_count_[i] = static_cast(last_live_ops_[i].size()); } for (auto& dep : *dependecy_count_) { diff --git a/paddle/fluid/framework/new_executor/program_interpreter.cc b/paddle/fluid/framework/new_executor/program_interpreter.cc index 29998e61f0111..42842ddddc2dc 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.cc +++ b/paddle/fluid/framework/new_executor/program_interpreter.cc @@ -266,7 +266,7 @@ void ProgramInterpreter::reset_scope(Scope* new_scope) { var_scope_.SetScope(new_scope); auto& var_list = var_scope_.MutableVarList(); for (size_t i = 0; i < var_list.size(); i++) { - const auto& var_name = var_scope_.GetNameById(i); + const auto& var_name = var_scope_.GetNameById(static_cast(i)); var_list[i] = new_scope->FindVar(var_name); } // The index should be assured valid, cause the InterpreterCore may not be @@ -307,14 +307,15 @@ void ProgramInterpreter::ShareBuildResultsFrom(const InterpreterBaseImpl& src) { bool ProgramInterpreter::BuildInplaceCheckVarIsOnlyInput( const std::vector>& input_var2op, size_t var_index) { - if (!var_scope_.VarDesc(var_index)) { + if (!var_scope_.VarDesc(static_cast(var_index))) { return input_var2op.at(var_index).size() == 1; } else { int is_input_cnt = 0; for (auto inst_id : input_var2op.at(var_index)) { OpInOutInfo info; info.Build(vec_instruction_.at(inst_id).OpBase()); - if (info.IsInArgBufferNeeded(var_scope_.VarDesc(var_index)->Name())) { + if (info.IsInArgBufferNeeded( + var_scope_.VarDesc(static_cast(var_index))->Name())) { is_input_cnt++; } } @@ -723,13 +724,14 @@ void ProgramInterpreter::Convert( for (auto var_id : gc_check_vars) { Scope* inner_scope = HasLocalScope() ? local_scope_ : var_scope_.GetMutableScope(); - paddle::framework::Variable* var = - inner_scope->FindVar(var_scope_.GetNameById(var_id)); + paddle::framework::Variable* var = inner_scope->FindVar( + var_scope_.GetNameById(static_cast(var_id))); if (var->IsType() || var->IsType() || var->IsType()) { last_live_ops_[var_id].insert(op_idx); } else { - VLOG(4) << "not clear " << var_scope_.GetNameById(var_id) << " after " + VLOG(4) << "not clear " + << var_scope_.GetNameById(static_cast(var_id)) << " after " << instr.OpBase()->Type() << " because its type is " << framework::ToTypeName(var->Type()); } @@ -767,14 +769,15 @@ void ProgramInterpreter::Convert( } if (not_before_any) { VLOG(8) << "last live op of var " << i << " " - << var_scope_.GetNameById(i) << " : " << item << " " - << vec_instruction_[item].OpBase()->Type(); + << var_scope_.GetNameById(static_cast(i)) << " : " << item + << " " << vec_instruction_[item].OpBase()->Type(); minumum_last_live_ops.insert(item); vec_instruction_[item].AddGCCheckVar(i); } } last_live_ops_[i] = minumum_last_live_ops; - vec_meta_info[i].var_ref_count_ = last_live_ops_[i].size(); + vec_meta_info[i].var_ref_count_ = + static_cast(last_live_ops_[i].size()); } for (auto& ins : vec_instruction_) { @@ -799,7 +802,8 @@ void ProgramInterpreter::Convert( } for (size_t i = 0; i < vec_meta_info.size(); ++i) { refs_.emplace_back(std::make_shared( - vec_meta_info[i].var_ref_count_, var_scope_.VarRef(i))); + vec_meta_info[i].var_ref_count_, + var_scope_.VarRef(static_cast(i)))); } AnalyseExecuteOrderForTrace(); @@ -1298,16 +1302,17 @@ void ProgramInterpreter::CheckGC(const Instruction& instr) { auto& var_scope = var_scope_; for (auto var_id : instr.GCCheckVars()) { - VLOG(4) << "GC:" << var_scope_.GetNameById(var_id) << ", id:" << var_id - << ", ref:" << refs_[var_id]->DynamicRef(); + VLOG(4) << "GC:" << var_scope_.GetNameById(static_cast(var_id)) + << ", id:" << var_id << ", ref:" << refs_[var_id]->DynamicRef(); bool is_ready = refs_[var_id]->CheckAndDecrease(); // ignore all persistable var while GC - if (var_scope.VarDesc(var_id) && var_scope.VarDesc(var_id)->Persistable()) { + if (var_scope.VarDesc(static_cast(var_id)) && + var_scope.VarDesc(static_cast(var_id))->Persistable()) { continue; } if (is_ready) { VLOG(6) << "Async delete variable with name : " - << var_scope.GetNameById(var_id); + << var_scope.GetNameById(static_cast(var_id)); gc_->Add(refs_[var_id]->Var(), instr); } } diff --git a/paddle/fluid/framework/new_executor/workqueue/workqueue.cc b/paddle/fluid/framework/new_executor/workqueue/workqueue.cc index 95fc97c3cf043..a7945a612c1fb 100644 --- a/paddle/fluid/framework/new_executor/workqueue/workqueue.cc +++ b/paddle/fluid/framework/new_executor/workqueue/workqueue.cc @@ -48,7 +48,7 @@ class WorkQueueImpl : public WorkQueue { options.events_waiter->RegisterEvent(kQueueDestructEvent); } queue_ = new NonblockingThreadPool(options_.name, - options_.num_threads, + static_cast(options_.num_threads), options_.allow_spinning, options_.always_spinning); } @@ -141,7 +141,7 @@ WorkQueueGroupImpl::WorkQueueGroupImpl( } queues_[idx] = new (&queues_storage_[idx]) NonblockingThreadPool(options.name, - options.num_threads, + static_cast(options.num_threads), options.allow_spinning, options.always_spinning); } diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index 3fbf15c943816..cdc06e3ea0ba3 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -62,7 +62,7 @@ class CompileTimeInferShapeContext : public InferShapeContext { op_.Type(), idx, op_proto->inputs().size())); - return op_proto->inputs()[idx].name(); + return op_proto->inputs()[static_cast(idx)].name(); } std::string GetOutputNameByIdx(size_t idx) const override { @@ -77,7 +77,7 @@ class CompileTimeInferShapeContext : public InferShapeContext { op_.Type(), idx, op_proto->outputs().size())); - return op_proto->outputs()[idx].name(); + return op_proto->outputs()[static_cast(idx)].name(); } void ShareDim(const std::string &in, @@ -346,14 +346,13 @@ class CompileTimeInferShapeContext : public InferShapeContext { const std::vector &names) const { std::vector retv; retv.resize(names.size()); - std::transform( - names.begin(), - names.end(), - retv.begin(), - std::bind( - std::mem_fn(&CompileTimeInferShapeContext::GetVarType), // NOLINT - this, - std::placeholders::_1)); + std::transform(names.begin(), + names.end(), + retv.begin(), + std::bind( // NOLINT + std::mem_fn(&CompileTimeInferShapeContext::GetVarType), + this, + std::placeholders::_1)); return retv; } diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index c095ba849a4f2..9b9979bc70f4c 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -170,7 +170,7 @@ static int GetRowSize(const Scope& scope, const std::string& name) { } if (var->IsType()) { - return var->Get().rows().size(); + return static_cast(var->Get().rows().size()); } return -1; @@ -309,7 +309,7 @@ std::string RuntimeInferShapeContext::GetInputNameByIdx(size_t idx) const { op_.Type(), idx, op_proto->inputs().size())); - return op_proto->inputs()[idx].name(); + return op_proto->inputs()[static_cast(idx)].name(); } std::string RuntimeInferShapeContext::GetOutputNameByIdx(size_t idx) const { @@ -323,7 +323,7 @@ std::string RuntimeInferShapeContext::GetOutputNameByIdx(size_t idx) const { op_.Type(), idx, op_proto->outputs().size())); - return op_proto->outputs()[idx].name(); + return op_proto->outputs()[static_cast(idx)].name(); } void RuntimeInferShapeContext::ShareDim(const std::string& in, diff --git a/paddle/fluid/framework/phi_utils.cc b/paddle/fluid/framework/phi_utils.cc index 98e13b6e092fb..cc5cf54724dab 100644 --- a/paddle/fluid/framework/phi_utils.cc +++ b/paddle/fluid/framework/phi_utils.cc @@ -251,9 +251,9 @@ void InitDefaultKernelSignatureMap() { static void SetAllocationForUninitializedDenseTensor( phi::DenseTensor* dense_tensor, const platform::Place& place) { - int dtype_size = dense_tensor->dtype() == DataType::UNDEFINED - ? 0 - : phi::SizeOf(dense_tensor->dtype()); + int dtype_size = static_cast(dense_tensor->dtype() == DataType::UNDEFINED + ? 0 + : phi::SizeOf(dense_tensor->dtype())); int64_t numels = product(dense_tensor->dims()); numels = numels < 0 ? 0 : numels; auto tmp_allocation_ptr = memory::Alloc(place, numels * dtype_size); diff --git a/paddle/fluid/framework/program_converter.cc b/paddle/fluid/framework/program_converter.cc index b13aab38cbc05..fc60a0abf676e 100644 --- a/paddle/fluid/framework/program_converter.cc +++ b/paddle/fluid/framework/program_converter.cc @@ -139,7 +139,7 @@ void ConvertProgram(ProgramDesc* program) { BlockDesc* block = program->MutableBlock(i); const size_t num_ops = block->OpSize(); for (size_t j = 0; j < num_ops; j++) { - OpDesc* op = block->Op(j); + OpDesc* op = block->Op(static_cast(j)); const std::string op_type = op->Type(); if (op_type == "set_value" || op_type == "set_value_grad") { ConvertSetValueOp(op); @@ -230,7 +230,7 @@ void ConvertProgram(ProgramDesc* program) { BlockDesc* block = program->MutableBlock(i); const size_t num_ops = block->OpSize(); for (size_t j = 0; j < num_ops; j++) { - OpDesc* op = block->Op(j); + OpDesc* op = block->Op(static_cast(j)); const std::string op_type = op->Type(); if (!legacy_op_versions.count(op_type)) { continue; diff --git a/paddle/fluid/framework/program_desc.cc b/paddle/fluid/framework/program_desc.cc index 272ff43e6b299..baf50d275c89f 100644 --- a/paddle/fluid/framework/program_desc.cc +++ b/paddle/fluid/framework/program_desc.cc @@ -92,8 +92,9 @@ ProgramDesc::ProgramDesc(const ProgramDesc &o) { block_desc) != old_block_desc.end()) { // The block is owned by the origin program. Just use id to get // the corresponding block. - int sub_block_id = - o.Block(block_id).Op(op_id)->GetBlockAttrId(attr_name); + int sub_block_id = o.Block(block_id) + .Op(static_cast(op_id)) + ->GetBlockAttrId(attr_name); op->SetBlockAttr(attr_name, MutableBlock(sub_block_id)); } else { // The block is not owned by the origin program. Should copy @@ -102,8 +103,9 @@ ProgramDesc::ProgramDesc(const ProgramDesc &o) { op->SetBlockAttr(attr_name, block_desc); } } else if (op->GetAttrType(attr_name) == proto::AttrType::BLOCKS) { - std::vector sub_block_ids = - o.Block(block_id).Op(op_id)->GetBlocksAttrIds(attr_name); + std::vector sub_block_ids = o.Block(block_id) + .Op(static_cast(op_id)) + ->GetBlocksAttrIds(attr_name); std::vector block_descs; for (int block_id : sub_block_ids) { block_descs.push_back(MutableBlock(block_id)); diff --git a/paddle/fluid/framework/prune.cc b/paddle/fluid/framework/prune.cc index bf55d0c312ebe..d4c2021d5f6e1 100644 --- a/paddle/fluid/framework/prune.cc +++ b/paddle/fluid/framework/prune.cc @@ -288,7 +288,7 @@ void prune_impl(const proto::ProgramDesc& input, for (size_t i = 0; i < should_run.size(); ++i) { if (should_run[i]) { auto* op = op_field->Add(); - *op = input.blocks(block_id).ops(i); + *op = input.blocks(block_id).ops(static_cast(i)); if (HasSubBlock(*op)) { VLOG(2) << "Pruning op which has sub block: " << op->type(); // create sub_block_dependent_vars here to help prune the sub block @@ -504,7 +504,7 @@ std::tuple> PruneBackward( // Step 2. Prune backward for each block. for (size_t i = 0; i < origin_clone.Size(); i++) { auto pruned = proto::BlockDesc(); - auto origin = origin_clone.Proto()->mutable_blocks(i); + auto origin = origin_clone.Proto()->mutable_blocks(static_cast(i)); PruneBackwardImpl(origin, &pruned); // If pruned block contains no operator, it means the block is a diff --git a/paddle/fluid/framework/string_array.cc b/paddle/fluid/framework/string_array.cc index c15ff8748c8ce..c073090e3d129 100644 --- a/paddle/fluid/framework/string_array.cc +++ b/paddle/fluid/framework/string_array.cc @@ -69,7 +69,7 @@ void StringMapToStream(std::ostream& os, // write the token size_t length = token.size(); os.write(reinterpret_cast(&length), sizeof(length)); - os.write(token.c_str(), length); + os.write(token.c_str(), length); // NOLINT // write the token_id os.write(reinterpret_cast(&token_id), sizeof(token_id)); } @@ -90,7 +90,7 @@ void StringMapFromStream(std::istream& is, size_t token_length; is.read(reinterpret_cast(&token_length), sizeof(token_length)); char* tmp = new char[token_length]; - is.read(tmp, token_length); + is.read(tmp, token_length); // NOLINT std::string token(tmp, tmp + token_length); delete[] tmp; // read the token_id diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 5e2b11c1559c4..6fe75d1a90dab 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -608,7 +608,7 @@ void TensorFromStream(std::istream& is, { // read tensor tensor->Resize(phi::make_ddim(shape)); size_t seekg = seek * framework::SizeOfType(desc.data_type()); - is.seekg(seekg, is.cur); + is.seekg(seekg, is.cur); // NOLINT void* buf; phi::CPUContext ctx; @@ -642,7 +642,7 @@ void TensorFromStream(std::istream& is, framework::VisitDataType( desc.data_type(), DeserializedDataFunctor(&buf, tensor, ctx.GetPlace())); - is.read(static_cast(buf), size); + is.read(static_cast(buf), size); // NOLINT } } } @@ -718,7 +718,7 @@ void TensorFromStream(std::istream& is, framework::VisitDataType( desc.data_type(), DeserializedDataFunctor(&buf, tensor, ctx.GetPlace())); - is.read(static_cast(buf), size); + is.read(static_cast(buf), size); // NOLINT } } } diff --git a/paddle/fluid/imperative/jit/program_desc_tracer.cc b/paddle/fluid/imperative/jit/program_desc_tracer.cc index 467480a6d0eec..757668f12ddc7 100644 --- a/paddle/fluid/imperative/jit/program_desc_tracer.cc +++ b/paddle/fluid/imperative/jit/program_desc_tracer.cc @@ -265,7 +265,7 @@ void ProgramDescTracer::InsertVarIfNotExist( const auto &tensor = inner_var.Get(); new_var_desc->SetType(framework::proto::VarType::LOD_TENSOR); new_var_desc->SetShape(phi::vectorize(tensor.dims())); - new_var_desc->SetLoDLevel(tensor.lod().size()); + new_var_desc->SetLoDLevel(static_cast(tensor.lod().size())); if (tensor.IsInitialized()) { new_var_desc->SetDataType(framework::TransToProtoVarType(tensor.dtype())); } else { diff --git a/paddle/fluid/operators/affine_channel_op.cc b/paddle/fluid/operators/affine_channel_op.cc index 565054c265151..137249a30d455 100644 --- a/paddle/fluid/operators/affine_channel_op.cc +++ b/paddle/fluid/operators/affine_channel_op.cc @@ -199,9 +199,10 @@ class AffineChannelKernel : public framework::OpKernel { phi::StringToDataLayout(ctx.Attr("data_layout")); auto dims = x->dims(); - int N = dims[0]; - int C = layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]; - int HxW = x->numel() / N / C; + int N = static_cast(dims[0]); + int C = static_cast( + layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]); + int HxW = static_cast(x->numel() / N / C); auto* scale_d = scale->data(); auto* bias_d = bias->data(); @@ -245,9 +246,10 @@ class AffineChannelGradKernel : public framework::OpKernel { phi::StringToDataLayout(ctx.Attr("data_layout")); auto dims = x->dims(); - int N = dims[0]; - int C = layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]; - int HxW = x->numel() / N / C; + int N = static_cast(dims[0]); + int C = static_cast( + layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]); + int HxW = static_cast(x->numel() / N / C); auto* dy_d = dy->data(); auto* scale_d = scale->data(); diff --git a/paddle/fluid/operators/attention_lstm_op.cc b/paddle/fluid/operators/attention_lstm_op.cc index 6b9e3b7c296f0..4ec16e62f2ffa 100644 --- a/paddle/fluid/operators/attention_lstm_op.cc +++ b/paddle/fluid/operators/attention_lstm_op.cc @@ -51,7 +51,7 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const { ctx->HasOutput("LSTMOUT"), "Output", "LSTMOUT", "AttentionLstm"); auto x_dims = ctx->GetInputDim("X"); - const int M = x_dims[1]; + const int M = static_cast(x_dims[1]); PADDLE_ENFORCE_EQ(x_dims.size(), 2, platform::errors::InvalidArgument( @@ -59,7 +59,7 @@ void AttentionLSTMOp::InferShape(framework::InferShapeContext* ctx) const { x_dims.size())); auto w_dims = ctx->GetInputDim("LSTMWeight"); - const int D = w_dims[1] / 4; + const int D = static_cast(w_dims[1] / 4); PADDLE_ENFORCE_EQ( w_dims.size(), 2, @@ -364,18 +364,18 @@ class AttentionLSTMKernel : public framework::OpKernel { // some shape should be reshape here since infershape can not get lod info auto x_lod = x->lod(); - const int N = x_lod[0].size() - 1; // batch size - auto x_dims = x->dims(); // T x M - auto w_dims = lstm_w->dims(); // (D+M) x 4D - const int total_T = x_dims[0]; - const int M = x_dims[1]; // x frame size - const int D = w_dims[1] / 4; // gate frame size - const int D2 = D * 2; - const int D3 = D * 3; - const int D4 = w_dims[1]; - int max_seq_len = x_lod[0][1]; + const int N = static_cast(x_lod[0].size() - 1); // batch size + auto x_dims = x->dims(); // T x M + auto w_dims = lstm_w->dims(); // (D+M) x 4D + const int total_T = static_cast(x_dims[0]); + const int M = static_cast(x_dims[1]); // x frame size + const int D = static_cast(w_dims[1] / 4); // gate frame size + const int D2 = static_cast(D * 2); + const int D3 = static_cast(D * 3); + const int D4 = static_cast(w_dims[1]); + int max_seq_len = static_cast(x_lod[0][1]); for (int i = 1; i < N; ++i) { - int len = x_lod[0][i + 1] - x_lod[0][i]; + int len = static_cast(x_lod[0][i + 1] - x_lod[0][i]); max_seq_len = max_seq_len < len ? len : max_seq_len; } PADDLE_ENFORCE_EQ( @@ -443,7 +443,7 @@ class AttentionLSTMKernel : public framework::OpKernel { T* cur_cell_out_data = cell_out_data; T* cur_hidden_out_data = hidden_out_data; for (int i = 0; i < N; ++i) { - int seq_len = x_lod[0][i + 1] - x_lod[0][i]; + int seq_len = static_cast(x_lod[0][i + 1] - x_lod[0][i]); prev_cell_data = c0_data + i * D; prev_hidden_data = h0_data ? h0_data + i * D : NULL; for (int step = 0; step < seq_len; ++step) { diff --git a/paddle/fluid/operators/batch_norm_op.cc b/paddle/fluid/operators/batch_norm_op.cc index d21842ca48911..4f1c7ab3857d7 100644 --- a/paddle/fluid/operators/batch_norm_op.cc +++ b/paddle/fluid/operators/batch_norm_op.cc @@ -361,10 +361,10 @@ void BatchNormGradOp::InferShape(framework::InferShapeContext *ctx) const { const DataLayout data_layout = phi::StringToDataLayout(ctx->Attrs().Get("data_layout")); - const int C = + const int C = static_cast( ((ctx->IsRunMKLDNNKernel() == true) || (data_layout == DataLayout::kNCHW) ? x_dims[1] - : x_dims[x_dims.size() - 1]); + : x_dims[x_dims.size() - 1])); // has_scale_grad == has_bias_grad, judge has_scale_grad is enough if (has_scale_grad) { @@ -504,10 +504,10 @@ void BatchNormDoubleGradOp::InferShape( const auto x_dims = ctx->GetInputDim("X"); const DataLayout data_layout = phi::StringToDataLayout(ctx->Attrs().Get("data_layout")); - const int C = + const int C = static_cast( ((ctx->IsRunMKLDNNKernel() == true) || (data_layout == DataLayout::kNCHW) ? x_dims[1] - : x_dims[x_dims.size() - 1]); + : x_dims[x_dims.size() - 1])); if (ctx->HasOutput("DX")) { ctx->SetOutputDim("DX", x_dims); diff --git a/paddle/fluid/operators/conv_shift_op.cc b/paddle/fluid/operators/conv_shift_op.cc index d018d4b78ef2a..d2d8f56587cfd 100644 --- a/paddle/fluid/operators/conv_shift_op.cc +++ b/paddle/fluid/operators/conv_shift_op.cc @@ -173,7 +173,8 @@ class ConvShiftKernel : public framework::OpKernel { for (size_t k = 0; k < batch_size; ++k) { for (size_t i = 0; i < x_width; ++i) { for (size_t j = 0; j < y_width; ++j) { - int index = (i + j - y_half_width + x_width) % x_width; + int index = + static_cast((i + j - y_half_width + x_width) % x_width); out(k, i) += x(k, index) * y(k, j); } } @@ -211,7 +212,8 @@ class ConvShiftGradKernel : public framework::OpKernel { for (size_t k = 0; k < batch_size; ++k) { for (size_t i = 0; i < x_width; ++i) { for (size_t j = 0; j < y_width; ++j) { - int index = (i + j - y_half_width + x_width) % x_width; + int index = + static_cast((i + j - y_half_width + x_width) % x_width); dx(k, index) += dout(k, i) * y(k, j); } } @@ -225,7 +227,8 @@ class ConvShiftGradKernel : public framework::OpKernel { for (size_t k = 0; k < batch_size; ++k) { for (size_t i = 0; i < x_width; ++i) { for (size_t j = 0; j < y_width; ++j) { - int index = (i + j - y_half_width + x_width) % x_width; + int index = + static_cast((i + j - y_half_width + x_width) % x_width); dy(k, j) += x(k, index) * dout(k, i); } } diff --git a/paddle/fluid/operators/copy_cross_scope_op.cc b/paddle/fluid/operators/copy_cross_scope_op.cc index 56f334b66571d..ed433518068b4 100644 --- a/paddle/fluid/operators/copy_cross_scope_op.cc +++ b/paddle/fluid/operators/copy_cross_scope_op.cc @@ -46,7 +46,7 @@ class CopyCrossScopeOp : public framework::OperatorBase { private: void RunImpl(const framework::Scope& scope, const platform::Place& dev_place) const override { - int num_micro_scopes = scope.kids().size(); + int num_micro_scopes = static_cast(scope.kids().size()); int num_micro_batches = Attr("num_micro_batches"); bool ToM = Attr("to_main_scope"); PADDLE_ENFORCE_EQ(num_micro_scopes, diff --git a/paddle/fluid/operators/correlation_op.cc b/paddle/fluid/operators/correlation_op.cc index 19dc3fca2542e..bedadbd18746c 100644 --- a/paddle/fluid/operators/correlation_op.cc +++ b/paddle/fluid/operators/correlation_op.cc @@ -97,14 +97,15 @@ class CorrelationOp : public framework::OperatorWithKernel { "Input(Y) of CorrelationOp must be 4 dims." "But received dims is %d.", in2_dims.size())); - std::vector output_shape = CorrelationOutputSize(in_dims[0], - in_dims[2], - in_dims[3], - stride1, - stride2, - kernel_size, - pad_size, - max_displacement); + std::vector output_shape = + CorrelationOutputSize(static_cast(in_dims[0]), + static_cast(in_dims[2]), + static_cast(in_dims[3]), + stride1, + stride2, + kernel_size, + pad_size, + max_displacement); ctx->SetOutputDim("Output", phi::make_ddim(output_shape)); } diff --git a/paddle/fluid/operators/data_norm_op.cc b/paddle/fluid/operators/data_norm_op.cc index b25303e41d6ca..493351654d5eb 100644 --- a/paddle/fluid/operators/data_norm_op.cc +++ b/paddle/fluid/operators/data_norm_op.cc @@ -280,10 +280,10 @@ class DataNormKernel : public framework::OpKernel { x_dims.size(), 2, platform::errors::InvalidArgument("The Input dim size should be 2")); - const int N = x_dims[0]; - const int C = - (data_layout == DataLayout::kNCHW ? x_dims[1] - : x_dims[x_dims.size() - 1]); + const int N = static_cast(x_dims[0]); + const int C = static_cast(data_layout == DataLayout::kNCHW + ? x_dims[1] + : x_dims[x_dims.size() - 1]); PADDLE_ENFORCE_LT(0, N, @@ -327,7 +327,7 @@ class DataNormKernel : public framework::OpKernel { // to check if show number is zero, if so, skip normalization. if (slot_dim > 0 && N > 0 && (!ctx.Attr("enable_scale_and_shift"))) { - const int item_size = x->numel() / N; + const int item_size = static_cast(x->numel() / N); // location of show number in one embedding int offset = 0; for (int k = 0; k < N; ++k) { @@ -371,7 +371,7 @@ class DataNormKernel : public framework::OpKernel { new_bias; } else { - const int item_size = x->numel() / N; + const int item_size = static_cast(x->numel() / N); const auto *scale_w = ctx.Input("scale_w"); const auto *bias = ctx.Input("bias"); const T *scale_w_data = scale_w->data(); @@ -454,9 +454,9 @@ class DataNormGradOp : public framework::OperatorWithKernel { const auto x_dims = ctx->GetInputDim("X"); const DataLayout data_layout = phi::StringToDataLayout(ctx->Attrs().Get("data_layout")); - const int C = - (data_layout == DataLayout::kNCHW ? x_dims[1] - : x_dims[x_dims.size() - 1]); + const int C = static_cast(data_layout == DataLayout::kNCHW + ? x_dims[1] + : x_dims[x_dims.size() - 1]); if (ctx->HasOutput(framework::GradVarName("X"))) { ctx->SetOutputDim(framework::GradVarName("X"), x_dims); @@ -527,10 +527,10 @@ class DataNormGradKernel : public framework::OpKernel { x_dims.size(), 2, platform::errors::InvalidArgument("The Input dim size should be 2")); - const int N = x_dims[0]; - const int C = - (data_layout == DataLayout::kNCHW ? x_dims[1] - : x_dims[x_dims.size() - 1]); + const int N = static_cast(x_dims[0]); + const int C = static_cast(data_layout == DataLayout::kNCHW + ? x_dims[1] + : x_dims[x_dims.size() - 1]); // init output phi::DenseTensor *d_x = nullptr; if (ctx.HasOutput(framework::GradVarName("X"))) { @@ -627,7 +627,7 @@ class DataNormGradKernel : public framework::OpKernel { } } else { int offset = 0; - const int item_size = x->numel() / N; + const int item_size = static_cast(x->numel() / N); T *d_x_data = d_x->mutable_data(ctx.GetPlace()); T *d_scale_data = d_scale->mutable_data(ctx.GetPlace()); T *d_bias_data = d_bias->mutable_data(ctx.GetPlace()); @@ -663,7 +663,7 @@ class DataNormGradKernel : public framework::OpKernel { // if slot_dim is set and batch size is larger than zero, we choose // to check if show number is zero, if so, skip update statistics. int offset = 0; - const int item_size = x->numel() / N; + const int item_size = static_cast(x->numel() / N); for (int k = 0; k < N; ++k) { for (int i = 0; i < item_size; i += slot_dim) { if (!(x_data[offset + i] > -min_precision && diff --git a/paddle/fluid/operators/dequantize_abs_max_op.cc b/paddle/fluid/operators/dequantize_abs_max_op.cc index 8cfc1805d44af..b9d5f0bb29200 100644 --- a/paddle/fluid/operators/dequantize_abs_max_op.cc +++ b/paddle/fluid/operators/dequantize_abs_max_op.cc @@ -42,7 +42,7 @@ struct DequantizeFunctor { const float* scale_factor = scale->data(); const T* input_data = in->data(); float* output_data = out->mutable_data(dev_ctx.GetPlace()); - int ind = in->numel(); + int ind = static_cast(in->numel()); for (size_t i = 0; i < (unsigned)ind; i++) { output_data[i] = scale_factor[0] * input_data[i] / max_range; } diff --git a/paddle/fluid/operators/dequantize_log_op.cc b/paddle/fluid/operators/dequantize_log_op.cc index 55e3e4819895e..03ede45695148 100644 --- a/paddle/fluid/operators/dequantize_log_op.cc +++ b/paddle/fluid/operators/dequantize_log_op.cc @@ -40,7 +40,7 @@ struct DequantizeFunctor { const float* dict_data = dict->data(); const T* input_data = in->data(); float* output_data = out->mutable_data(dev_ctx.GetPlace()); - int ind = in->numel(); + int ind = static_cast(in->numel()); for (size_t i = 0; i < (unsigned)ind; i++) { if (input_data[i] < 0) { output_data[i] = -dict_data[input_data[i] + 128]; diff --git a/paddle/fluid/operators/expand_op.cc b/paddle/fluid/operators/expand_op.cc index 1c1e3f76619dc..c6e750f4fe0ec 100644 --- a/paddle/fluid/operators/expand_op.cc +++ b/paddle/fluid/operators/expand_op.cc @@ -51,7 +51,7 @@ class ExpandOp : public framework::OperatorWithKernel { x_dims.size())); std::vector out_shape(x_dims.size()); - for (size_t i = 0; i < expand_times.size(); ++i) { + for (int i = 0; i < static_cast(expand_times.size()); ++i) { if (x_dims[i] == -1 || expand_times[i] == -1) { out_shape[i] = -1; } else { diff --git a/paddle/fluid/operators/fake_dequantize_op.cc b/paddle/fluid/operators/fake_dequantize_op.cc index c25be0680c6b9..a941fb8171de3 100644 --- a/paddle/fluid/operators/fake_dequantize_op.cc +++ b/paddle/fluid/operators/fake_dequantize_op.cc @@ -115,8 +115,8 @@ struct ChannelDequantizeFunctor { } } } else { - int batch_size = in->dims()[0]; - int channel = in->dims()[1]; + int batch_size = static_cast(in->dims()[0]); + int channel = static_cast(in->dims()[1]); const T* scale_one = scales[0]->data(); const T* scale_two = scales[1]->data(); for (int i = 0; i < batch_size; i++) { diff --git a/paddle/fluid/operators/fake_quantize_op.cc b/paddle/fluid/operators/fake_quantize_op.cc index c5740f0f853ab..1eca5834d5212 100644 --- a/paddle/fluid/operators/fake_quantize_op.cc +++ b/paddle/fluid/operators/fake_quantize_op.cc @@ -336,7 +336,7 @@ struct FindRangeAbsMaxFunctor { phi::DenseTensor *out_scale) { T *scale_arr = scales_arr->mutable_data(ctx.GetPlace()); int64_t it = iter.data()[0]; - int idx = it % window_size; + int idx = static_cast(it % window_size); T removed = scale_arr[idx]; T cur = cur_scale.data()[0]; scale_arr[idx] = cur; @@ -345,7 +345,7 @@ struct FindRangeAbsMaxFunctor { if (max < cur) { max = cur; } else if (fabs(removed - max) < 1e-6) { - int size = (it > window_size) ? window_size : it; + int size = static_cast((it > window_size) ? window_size : it); FindAbsMaxFunctor()(ctx, scale_arr, size, &max); } out_scale->mutable_data(ctx.GetPlace())[0] = max; diff --git a/paddle/fluid/operators/fill_constant_op.cc b/paddle/fluid/operators/fill_constant_op.cc index 8af460259a37b..a398698f40cab 100644 --- a/paddle/fluid/operators/fill_constant_op.cc +++ b/paddle/fluid/operators/fill_constant_op.cc @@ -45,7 +45,7 @@ class FillConstantOp : public framework::OperatorWithKernel { auto shape_dims = ctx->GetInputDim("ShapeTensor"); int num_ele = 1; for (int i = 0; i < shape_dims.size(); ++i) { - num_ele *= shape_dims[i]; + num_ele *= static_cast(shape_dims[i]); } auto vec_dims = std::vector(num_ele, -1); ctx->SetOutputDim("Out", phi::make_ddim(vec_dims)); diff --git a/paddle/fluid/operators/flatten_op.cc b/paddle/fluid/operators/flatten_op.cc index 530b3560bb878..7bacc5f9de3e0 100644 --- a/paddle/fluid/operators/flatten_op.cc +++ b/paddle/fluid/operators/flatten_op.cc @@ -81,8 +81,8 @@ class FlattenOp : public framework::OperatorWithKernel { } } std::vector out_shape(2); - out_shape[0] = outer; - out_shape[1] = inner; + out_shape[0] = static_cast(outer); + out_shape[1] = static_cast(inner); return out_shape; } diff --git a/paddle/fluid/operators/gru_op.cc b/paddle/fluid/operators/gru_op.cc index 6c3294ac5e261..20dfa2d91df19 100644 --- a/paddle/fluid/operators/gru_op.cc +++ b/paddle/fluid/operators/gru_op.cc @@ -47,8 +47,8 @@ class GRUOp : public framework::OperatorWithKernel { } auto input_dims = ctx->GetInputDim("Input"); auto weight_dims = ctx->GetInputDim("Weight"); - int input_size = input_dims[1]; - int frame_size = weight_dims[0]; + int input_size = static_cast(input_dims[1]); + int frame_size = static_cast(weight_dims[0]); if (ctx->IsRuntime()) { PADDLE_ENFORCE_EQ(input_size, frame_size * 3, @@ -82,8 +82,8 @@ class GRUOp : public framework::OperatorWithKernel { } if (ctx->HasInput("Bias")) { auto bias_dims = ctx->GetInputDim("Bias"); - int bias_height = bias_dims[0]; - int bias_width = bias_dims[1]; + int bias_height = static_cast(bias_dims[0]); + int bias_width = static_cast(bias_dims[1]); PADDLE_ENFORCE_EQ( bias_height, 1, @@ -226,10 +226,10 @@ class GRUGradOp : public framework::OperatorWithKernel { auto input_dims = ctx->GetInputDim("Input"); auto weight_dims = ctx->GetInputDim("Weight"); - int input_size = input_dims[1]; - int frame_size = weight_dims[0]; - int weight_height = weight_dims[0]; - int weight_width = weight_dims[1]; + int input_size = static_cast(input_dims[1]); + int frame_size = static_cast(weight_dims[0]); + int weight_height = static_cast(weight_dims[0]); + int weight_width = static_cast(weight_dims[1]); PADDLE_ENFORCE_EQ( input_size, frame_size * 3, @@ -274,8 +274,8 @@ class GRUGradOp : public framework::OperatorWithKernel { } if (ctx->HasInput("Bias")) { auto bias_dims = ctx->GetInputDim("Bias"); - int bias_height = bias_dims[0]; - int bias_width = bias_dims[1]; + int bias_height = static_cast(bias_dims[0]); + int bias_width = static_cast(bias_dims[1]); PADDLE_ENFORCE_EQ( bias_height, 1, @@ -365,7 +365,7 @@ class GRUCPUKernel : public framework::OpKernel { add_bias(dev_ctx, *batch_gate, *bias, batch_gate); } - int frame_size = hidden_dims[1]; + int frame_size = static_cast(hidden_dims[1]); phi::funcs::GRUMetaValue gru_value; gru_value.gate_weight = const_cast(weight_data); gru_value.state_weight = diff --git a/paddle/fluid/operators/gru_unit_op.cc b/paddle/fluid/operators/gru_unit_op.cc index 3c8b2947e5fce..b217d58e6d5da 100644 --- a/paddle/fluid/operators/gru_unit_op.cc +++ b/paddle/fluid/operators/gru_unit_op.cc @@ -37,11 +37,11 @@ class GRUUnitOp : public framework::OperatorWithKernel { auto input_dims = ctx->GetInputDim("Input"); auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev"); auto weight_dims = ctx->GetInputDim("Weight"); - int batch_size = input_dims[0]; - int input_size = input_dims[1]; - int frame_size = hidden_prev_dims[1]; - int weight_height = weight_dims[0]; - int weight_width = weight_dims[1]; + int batch_size = static_cast(input_dims[0]); + int input_size = static_cast(input_dims[1]); + int frame_size = static_cast(hidden_prev_dims[1]); + int weight_height = static_cast(weight_dims[0]); + int weight_width = static_cast(weight_dims[1]); if (ctx->IsRuntime() || input_size >= 0) { PADDLE_ENFORCE_EQ(input_size, frame_size * 3, @@ -77,8 +77,8 @@ class GRUUnitOp : public framework::OperatorWithKernel { if (ctx->HasInput("Bias")) { auto bias_dims = ctx->GetInputDim("Bias"); - int bias_height = bias_dims[0]; - int bias_width = bias_dims[1]; + int bias_height = static_cast(bias_dims[0]); + int bias_width = static_cast(bias_dims[1]); PADDLE_ENFORCE_EQ( bias_height, 1, @@ -195,10 +195,10 @@ class GRUUnitGradOp : public framework::OperatorWithKernel { auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev"); auto weight_dims = ctx->GetInputDim("Weight"); // int batch_size = input_dims[0]; - int input_size = input_dims[1]; - int frame_size = hidden_prev_dims[1]; - int weight_height = weight_dims[0]; - int weight_width = weight_dims[1]; + int input_size = static_cast(input_dims[1]); + int frame_size = static_cast(hidden_prev_dims[1]); + int weight_height = static_cast(weight_dims[0]); + int weight_width = static_cast(weight_dims[1]); if (ctx->IsRuntime() || input_size >= 0) { PADDLE_ENFORCE_EQ( input_size, @@ -234,8 +234,8 @@ class GRUUnitGradOp : public framework::OperatorWithKernel { frame_size * 3)); if (ctx->HasInput("Bias")) { auto bias_dims = ctx->GetInputDim("Bias"); - int bias_height = bias_dims[0]; - int bias_width = bias_dims[1]; + int bias_height = static_cast(bias_dims[0]); + int bias_width = static_cast(bias_dims[1]); PADDLE_ENFORCE_EQ( bias_height, diff --git a/paddle/fluid/operators/inplace_abn_op.cc b/paddle/fluid/operators/inplace_abn_op.cc index 26b765abe79d3..eee0f1f304bc3 100644 --- a/paddle/fluid/operators/inplace_abn_op.cc +++ b/paddle/fluid/operators/inplace_abn_op.cc @@ -258,10 +258,10 @@ class InplaceABNGradOp : public framework::OperatorWithKernel { const DataLayout data_layout = phi::StringToDataLayout(ctx->Attrs().Get("data_layout")); - const int C = ((ctx->IsRunMKLDNNKernel() == true) || - (data_layout == DataLayout::kNCHW) - ? y_dims[1] - : y_dims[y_dims.size() - 1]); + const int C = static_cast((ctx->IsRunMKLDNNKernel() == true) || + (data_layout == DataLayout::kNCHW) + ? y_dims[1] + : y_dims[y_dims.size() - 1]); ctx->SetOutputDim(framework::GradVarName("X"), y_dims); // has_scale_grad == has_bias_grad, judge has_scale_grad is enough diff --git a/paddle/fluid/operators/interpolate_op.cc b/paddle/fluid/operators/interpolate_op.cc index ddd4835e380b1..2bb9bf633f0c2 100644 --- a/paddle/fluid/operators/interpolate_op.cc +++ b/paddle/fluid/operators/interpolate_op.cc @@ -76,8 +76,8 @@ static void Interpolate1DInferShapeCheck(framework::InferShapeContext* ctx) { if (scale > 0) { // round down out_w = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale) - : static_cast(dim_x[1] * scale)); + ? static_cast(dim_x[2] * scale) // NOLINT + : static_cast(dim_x[1] * scale)); // NOLINT // protect when input shape is -1 out_w = out_w > 0 ? out_w : -1; } else { @@ -167,11 +167,11 @@ static void Interpolate2DInferShapeCheck(framework::InferShapeContext* ctx) { if (scale > 0) { // round down out_h = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale) - : static_cast(dim_x[1] * scale)); + ? static_cast(dim_x[2] * scale) // NOLINT + : static_cast(dim_x[1] * scale)); // NOLINT out_w = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[3] * scale) - : static_cast(dim_x[2] * scale)); + ? static_cast(dim_x[3] * scale) // NOLINT + : static_cast(dim_x[2] * scale)); // NOLINT // protect when input shape is -1 out_h = out_h > 0 ? out_h : -1; out_w = out_w > 0 ? out_w : -1; @@ -264,14 +264,14 @@ static void Interpolate3DInferShapeCheck(framework::InferShapeContext* ctx) { if (scale > 0) { // round down out_d = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[2] * scale) - : static_cast(dim_x[1] * scale)); + ? static_cast(dim_x[2] * scale) // NOLINT + : static_cast(dim_x[1] * scale)); // NOLINT out_h = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[3] * scale) - : static_cast(dim_x[2] * scale)); + ? static_cast(dim_x[3] * scale) // NOLINT + : static_cast(dim_x[2] * scale)); // NOLINT out_w = (data_layout == DataLayout::kNCHW - ? static_cast(dim_x[4] * scale) - : static_cast(dim_x[3] * scale)); + ? static_cast(dim_x[4] * scale) // NOLINT + : static_cast(dim_x[3] * scale)); // NOLINT // protect when input shape is -1 out_d = out_d > 0 ? out_d : -1; out_h = out_h > 0 ? out_h : -1; diff --git a/paddle/fluid/operators/lstm_op.cc b/paddle/fluid/operators/lstm_op.cc index d7734e57ee492..8bdb455375bee 100644 --- a/paddle/fluid/operators/lstm_op.cc +++ b/paddle/fluid/operators/lstm_op.cc @@ -66,7 +66,7 @@ class LSTMOp : public framework::OperatorWithKernel { c_dims)); } - int frame_size = in_dims[1] / 4; + int frame_size = static_cast(in_dims[1] / 4); auto w_dims = ctx->GetInputDim("Weight"); PADDLE_ENFORCE_EQ( w_dims.size(), diff --git a/paddle/fluid/operators/lstm_unit_op.cc b/paddle/fluid/operators/lstm_unit_op.cc index bbe5504b98e39..25503ee32e9bf 100644 --- a/paddle/fluid/operators/lstm_unit_op.cc +++ b/paddle/fluid/operators/lstm_unit_op.cc @@ -56,8 +56,8 @@ class LstmUnitOp : public framework::OperatorWithKernel { c_prev_dims[1] * 4)); } - int b_size = c_prev_dims[0]; // batch size - int s_dim = c_prev_dims[1]; // state dim + int b_size = static_cast(c_prev_dims[0]); // batch size + int s_dim = static_cast(c_prev_dims[1]); // state dim ctx->SetOutputDim("C", {b_size, s_dim}); ctx->SetOutputDim("H", {b_size, s_dim}); } diff --git a/paddle/fluid/operators/lstmp_op.cc b/paddle/fluid/operators/lstmp_op.cc index 44e9a698beee7..7af04a237de4c 100644 --- a/paddle/fluid/operators/lstmp_op.cc +++ b/paddle/fluid/operators/lstmp_op.cc @@ -50,7 +50,7 @@ class LSTMPOp : public framework::OperatorWithKernel { "Input(X)'s rank of LSTMP operator must be 2, but received %d.", in_dims.size())); - int frame_size = in_dims[1] / 4; + int frame_size = static_cast(in_dims[1] / 4); auto w_dims = ctx->GetInputDim("Weight"); auto proj_dims = ctx->GetInputDim("ProjWeight"); PADDLE_ENFORCE_EQ( diff --git a/paddle/fluid/operators/math/sampler.cc b/paddle/fluid/operators/math/sampler.cc index fb6931836d764..0ea4336e92ec0 100644 --- a/paddle/fluid/operators/math/sampler.cc +++ b/paddle/fluid/operators/math/sampler.cc @@ -25,7 +25,7 @@ namespace math { Sampler::~Sampler() = default; UniformSampler::UniformSampler(int64_t range, unsigned int seed) - : Sampler(range, seed), inv_range_(1.0 / (range + 1)) { + : Sampler(range, seed), inv_range_(1.0f / (range + 1)) { // NOLINT random_engine_ = phi::GetCPURandomEngine(seed_); dist_ = std::make_shared>(0, range); } @@ -35,7 +35,7 @@ int64_t UniformSampler::Sample() const { return (*dist_)(*random_engine_); } float UniformSampler::Probability(int64_t value) const { return inv_range_; } LogUniformSampler::LogUniformSampler(int64_t range, unsigned int seed) - : Sampler(range, seed), log_range_(log(range + 1)) { + : Sampler(range, seed), log_range_(log(range + 1)) { // NOLINT random_engine_ = phi::GetCPURandomEngine(seed_); dist_ = std::make_shared>(0, 1); } @@ -57,7 +57,7 @@ float LogUniformSampler::Probability(int64_t value) const { // The value's probability is integral of f(x) from value to (value + 1) // More details: // https://wanghaoshuang.github.io/2017/11/Log-uniform-distribution-sampler - return (log((value + 2.0) / (value + 1.0))) / log_range_; + return (log((value + 2.0) / (value + 1.0))) / log_range_; // NOLINT } CustomSampler::CustomSampler(int64_t range, diff --git a/paddle/fluid/operators/math/tree2col.cc b/paddle/fluid/operators/math/tree2col.cc index 77eb7f68ec1c2..21eeb52fd311a 100644 --- a/paddle/fluid/operators/math/tree2col.cc +++ b/paddle/fluid/operators/math/tree2col.cc @@ -28,17 +28,18 @@ std::vector Tree2ColUtil::construct_patch( stack.emplace(root, 1, 1, 0); patch.emplace_back(root, 1, 1, 0); - visited[root] = true; + visited[static_cast(root)] = true; while (!stack.empty()) { TreeNode &u = stack.top(); bool end = true; size_t node = u.get_node(), sz = tr[node].size(); - visited[node] = true; + visited[static_cast(node)] = true; for (size_t i = 0; i < sz; i++) { size_t v = tr[node][i]; - if (!visited[v] && static_cast(u.get_depth()) + 1 < max_depth) { - visited[v] = true; + if (!visited[static_cast(v)] && + static_cast(u.get_depth()) + 1 < max_depth) { + visited[static_cast(v)] = true; stack.emplace(v, i, sz, u.get_depth() + 1); patch.emplace_back(v, i + 1, sz, u.get_depth() + 1); end = false; diff --git a/paddle/fluid/operators/max_sequence_len_op.cc b/paddle/fluid/operators/max_sequence_len_op.cc index d248c5c0c628c..813b1901760b9 100644 --- a/paddle/fluid/operators/max_sequence_len_op.cc +++ b/paddle/fluid/operators/max_sequence_len_op.cc @@ -46,7 +46,7 @@ class MaxSeqenceLenOp : public framework::OperatorBase { scope.FindVar(Input("RankTable"))->Get(); auto *out = scope.FindVar(Output("Out"))->GetMutable(); int64_t *out_ptr = out->mutable_data({1}, platform::CPUPlace()); - *out_ptr = rank_table.items()[0].length; + *out_ptr = rank_table.items()[0].length; // NOLINT } }; diff --git a/paddle/fluid/operators/merge_lod_tensor_op.cc b/paddle/fluid/operators/merge_lod_tensor_op.cc index 007f853f3243f..ae00939d07844 100644 --- a/paddle/fluid/operators/merge_lod_tensor_op.cc +++ b/paddle/fluid/operators/merge_lod_tensor_op.cc @@ -138,9 +138,11 @@ class MergeLoDTensorOp : public framework::OperatorBase { if (len == 0) { continue; } - auto slice = out->Slice(out_offset, out_offset + len); - framework::TensorCopy( - input->Slice(start_offset, end_offset), place, dev_ctx, &slice); + auto slice = out->Slice(out_offset, out_offset + len); // NOLINT + framework::TensorCopy(input->Slice(start_offset, end_offset), // NOLINT + place, + dev_ctx, + &slice); out_offset += len; (*in_idx) += 1; } diff --git a/paddle/fluid/operators/nce_op.cc b/paddle/fluid/operators/nce_op.cc index 9c9055d1987e1..477d5aea7e883 100644 --- a/paddle/fluid/operators/nce_op.cc +++ b/paddle/fluid/operators/nce_op.cc @@ -55,7 +55,8 @@ class NCEOp : public framework::OperatorWithKernel { label_dims, label_dims[0])); } - int num_true_classes = label_dims.size() == 2 ? label_dims[1] : 1; + int num_true_classes = + static_cast(label_dims.size() == 2 ? label_dims[1] : 1); if (ctx->HasInput("Bias")) { PADDLE_ENFORCE_EQ( ctx->GetInputDim("Weight")[0], diff --git a/paddle/fluid/operators/pad2d_op.cc b/paddle/fluid/operators/pad2d_op.cc index c2aeaebb51820..337a4e3178956 100644 --- a/paddle/fluid/operators/pad2d_op.cc +++ b/paddle/fluid/operators/pad2d_op.cc @@ -434,13 +434,13 @@ class Pad2dCPUKernel : public framework::OpKernel { const int pad_top = pads[0]; const int pad_left = pads[2]; - const int num = in_dims[0]; + const int num = static_cast(in_dims[0]); if (data_format == "NCHW") { - const int channels = in_dims[1]; - const int in_height = in_dims[2]; - const int in_width = in_dims[3]; - const int out_height = out_dims[2]; - const int out_width = out_dims[3]; + const int channels = static_cast(in_dims[1]); + const int in_height = static_cast(in_dims[2]); + const int in_width = static_cast(in_dims[3]); + const int out_height = static_cast(out_dims[2]); + const int out_width = static_cast(out_dims[3]); if (mode == "reflect") { Pad2DReflectNCHW(in_data, num, @@ -477,11 +477,11 @@ class Pad2dCPUKernel : public framework::OpKernel { out_data); } } else { - const int channels = in_dims[3]; - const int in_height = in_dims[1]; - const int in_width = in_dims[2]; - const int out_height = out_dims[1]; - const int out_width = out_dims[2]; + const int channels = static_cast(in_dims[3]); + const int in_height = static_cast(in_dims[1]); + const int in_width = static_cast(in_dims[2]); + const int out_height = static_cast(out_dims[1]); + const int out_width = static_cast(out_dims[2]); if (mode == "reflect") { Pad2DReflectNHWC(in_data, num, @@ -542,13 +542,13 @@ class Pad2dGradCPUKernel : public framework::OpKernel { static_cast(0)); const int pad_top = pads[0]; const int pad_left = pads[2]; - const int num = d_in_dims[0]; + const int num = static_cast(d_in_dims[0]); if (data_format == "NCHW") { - const int channels = d_in_dims[1]; - const int in_height = d_in_dims[2]; - const int in_width = d_in_dims[3]; - const int out_height = d_out_dims[2]; - const int out_width = d_out_dims[3]; + const int channels = static_cast(d_in_dims[1]); + const int in_height = static_cast(d_in_dims[2]); + const int in_width = static_cast(d_in_dims[3]); + const int out_height = static_cast(d_out_dims[2]); + const int out_width = static_cast(d_out_dims[3]); if (mode == "reflect") { Pad2DGradReflectNCHW(d_in_data, num, @@ -584,11 +584,11 @@ class Pad2dGradCPUKernel : public framework::OpKernel { d_out_data); } } else { - const int channels = d_in_dims[3]; - const int in_height = d_in_dims[1]; - const int in_width = d_in_dims[2]; - const int out_height = d_out_dims[1]; - const int out_width = d_out_dims[2]; + const int channels = static_cast(d_in_dims[3]); + const int in_height = static_cast(d_in_dims[1]); + const int in_width = static_cast(d_in_dims[2]); + const int out_height = static_cast(d_out_dims[1]); + const int out_width = static_cast(d_out_dims[2]); if (mode == "reflect") { Pad2DGradReflectNHWC(d_in_data, num, diff --git a/paddle/fluid/operators/partial_concat_op.cc b/paddle/fluid/operators/partial_concat_op.cc index d5b0f5630b911..3132a7bf27260 100644 --- a/paddle/fluid/operators/partial_concat_op.cc +++ b/paddle/fluid/operators/partial_concat_op.cc @@ -74,12 +74,12 @@ class PartialConcatOp : public framework::OperatorWithKernel { } } - int start_index = ComputeStartIndex( + int start_index = static_cast(ComputeStartIndex( static_cast(ctx->Attrs().Get("start_index")), - inputs_dims[0][1]); + inputs_dims[0][1])); int partial_len = ctx->Attrs().Get("length"); if (partial_len < 0) { - partial_len = inputs_dims[0][1] - start_index; + partial_len = static_cast(inputs_dims[0][1] - start_index); } ctx->SetOutputDim( diff --git a/paddle/fluid/operators/prune_gate_by_capacity_op.cc b/paddle/fluid/operators/prune_gate_by_capacity_op.cc index ed5bdaa010b52..365342fa7ea5f 100644 --- a/paddle/fluid/operators/prune_gate_by_capacity_op.cc +++ b/paddle/fluid/operators/prune_gate_by_capacity_op.cc @@ -44,7 +44,7 @@ class PruneGateByCapacityOp : public framework::OperatorWithKernel { int64_t n_worker = ctx->Attrs().Get("n_worker"); int64_t expert_count_num_ele = 1; - for (int64_t i = 0; i < expert_count_dims.size(); i++) { + for (int i = 0; i < static_cast(expert_count_dims.size()); i++) { expert_count_num_ele *= expert_count_dims[i]; } diff --git a/paddle/fluid/operators/pyramid_hash_op.cc b/paddle/fluid/operators/pyramid_hash_op.cc index db28f927c5eb0..787797544ef84 100644 --- a/paddle/fluid/operators/pyramid_hash_op.cc +++ b/paddle/fluid/operators/pyramid_hash_op.cc @@ -298,7 +298,7 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { buff->Resize(phi::make_ddim({bottom->dims()[0], bottom->dims()[1]})); float* bottom_data = buff->mutable_data(ctx.GetPlace()); for (int i = 0; i < bottom->dims()[0]; i++) { - bottom_data[i] = bottom_data_ori[i]; + bottom_data[i] = bottom_data_ori[i]; // NOLINT } const auto* weights = _blobs_0->data(); @@ -341,7 +341,7 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { int* iter_end = iter; for (size_t i = 0; i < top_offset.size() - 1; ++i) { - int w = offset[i + 1] - offset[i]; + int w = static_cast(offset[i + 1] - offset[i]); int nsentense_with_pyramid = 0; if (w < 2) { nsentense_with_pyramid = 0; @@ -354,7 +354,8 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { ilayer + 1)) { if (_is_training != 0) { unsigned int rand_val = rand_r(&_seed); - float rate = static_cast(rand_val) / (RAND_MAX); + float rate = + static_cast(rand_val) / (RAND_MAX); // NOLINT *(iter_end++) = (rate < _drop_out_percent ? 0 : 1); } else { *(iter_end++) = 1; @@ -364,7 +365,8 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { } } } - nsentense_with_pyramid = std::count(iter, iter_end, 1); + nsentense_with_pyramid = + static_cast(std::count(iter, iter_end, 1)); iter = iter_end; } drop_pos_offset[i + 1] = drop_pos_offset[i] + nsentense_with_pyramid; @@ -373,7 +375,7 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { (nsentense_with_pyramid == 0 ? 1 : nsentense_with_pyramid); } - int top_l = top_offset[top_offset.size() - 1]; + int top_l = static_cast(top_offset[top_offset.size() - 1]); framework::LoD top_lod; top_lod.push_back(top_offset); @@ -388,8 +390,9 @@ class CPUPyramidHashOPKernel : public framework::OpKernel { iter = drop_pos->mutable_data(ctx.GetPlace()); int top_counter = 0; for (size_t i = 0; i < offset.size() - 1; ++i) { - int w_drop = drop_pos_offset[i + 1] - drop_pos_offset[i]; - int w = offset[i + 1] - offset[i]; + int w_drop = + static_cast(drop_pos_offset[i + 1] - drop_pos_offset[i]); + int w = static_cast(offset[i + 1] - offset[i]); if (w_drop == 0) { if (w >= 2) { for (int ilayer = 1; ilayer < _pyramid_layer && ilayer < w; @@ -524,7 +527,7 @@ class CPUPyramidHashOPGradKernel : public framework::OpKernel { auto* buff = ctx.Input("X_Temp_Out"); auto* bottom_data = buff->data(); - int _slot_len = bottom->dims()[0]; + int _slot_len = static_cast(bottom->dims()[0]); if (static_cast(_slot_len) == bottom->lod()[0].size() - 1 && std::count(bottom_data, bottom_data + _slot_len, -1) == _slot_len) { return; @@ -541,8 +544,9 @@ class CPUPyramidHashOPGradKernel : public framework::OpKernel { const int* iter = drop_pos->data(); int top_counter = 0; for (size_t i = 0; i < offset.size() - 1; ++i) { - int w = offset[i + 1] - offset[i]; - int w_drop = drop_pos_offset[i + 1] - drop_pos_offset[i]; + int w = static_cast(offset[i + 1] - offset[i]); + int w_drop = + static_cast(drop_pos_offset[i + 1] - drop_pos_offset[i]); if (w_drop == 0) { top_counter++; } diff --git a/paddle/fluid/operators/random_crop_op.cc b/paddle/fluid/operators/random_crop_op.cc index 196e5db055220..62e805e323f84 100644 --- a/paddle/fluid/operators/random_crop_op.cc +++ b/paddle/fluid/operators/random_crop_op.cc @@ -38,7 +38,8 @@ class RandomCropOp : public framework::OperatorWithKernel { for (size_t i = 1; i <= shape.size(); ++i) { size_t x_i = x_dim.size() - i; size_t shape_i = shape.size() - i; - if (ctx->IsRuntime() || (x_dim[x_i] > 0 && shape[shape_i] > 0)) { + if (ctx->IsRuntime() || + (x_dim[static_cast(x_i)] > 0 && shape[shape_i] > 0)) { PADDLE_ENFORCE_GE( x_dim[x_i], shape[shape_i], diff --git a/paddle/fluid/operators/recurrent_op.cc b/paddle/fluid/operators/recurrent_op.cc index 321c7ad54a5cb..0362e618c2bc5 100644 --- a/paddle/fluid/operators/recurrent_op.cc +++ b/paddle/fluid/operators/recurrent_op.cc @@ -245,18 +245,18 @@ void RecurrentOp::RunImpl(const framework::Scope &scope, // Link outside::input --> inside::input // inside::input = outside::input[seq_offset: seq_offset+1] - LinkTensorWithCallback( - scope, - Inputs(kInputs), - &cur_scope, - Inputs(kInputs), - [&seq_offset](const phi::DenseTensor &outside, - phi::DenseTensor *inside) { - inside->ShareDataWith(outside.Slice(seq_offset, seq_offset + 1)); - auto dims = phi::vectorize(inside->dims()); - dims.erase(dims.begin()); - inside->Resize(phi::make_ddim(dims)); - }); + LinkTensorWithCallback(scope, + Inputs(kInputs), + &cur_scope, + Inputs(kInputs), + [&seq_offset](const phi::DenseTensor &outside, + phi::DenseTensor *inside) { + inside->ShareDataWith(outside.Slice( + seq_offset, seq_offset + 1)); // NOLINT + auto dims = phi::vectorize(inside->dims()); + dims.erase(dims.begin()); + inside->Resize(phi::make_ddim(dims)); + }); if (has_state) { if (i == 0) { @@ -277,7 +277,8 @@ void RecurrentOp::RunImpl(const framework::Scope &scope, // Link inside::output -> outside::output // outside::output[seq_offset: seq_offset + 1] = inside::output - executor.CreateVariables(ctx->prog_, &cur_scope, ctx->block_id_); + executor.CreateVariables( + ctx->prog_, &cur_scope, static_cast(ctx->block_id_)); // Linked now, execute! executor.RunPreparedContext(ctx.get(), @@ -297,7 +298,8 @@ void RecurrentOp::RunImpl(const framework::Scope &scope, dst_tensor->Resize(PrependDims(seq_len, src_tensor.dims())); dst_tensor->mutable_data(place, src_tensor.dtype()); - auto dst_out = dst_tensor->Slice(seq_offset, seq_offset + 1); + auto dst_out = + dst_tensor->Slice(seq_offset, seq_offset + 1); // NOLINT // Explicit copy output since the local RNN scope can be destroyed // early. framework::TensorCopy(src_tensor, place, dev_ctx, &dst_out); @@ -310,7 +312,8 @@ void RecurrentOp::RunImpl(const framework::Scope &scope, Outputs(kOutputs), [&](const phi::DenseTensor &src_tensor, phi::DenseTensor *dst_tensor) { - auto dst_out = dst_tensor->Slice(seq_offset, seq_offset + 1); + auto dst_out = + dst_tensor->Slice(seq_offset, seq_offset + 1); // NOLINT framework::TensorCopy(src_tensor, place, dev_ctx, &dst_out); }); } @@ -378,7 +381,8 @@ void RecurrentGradOp::RunImpl(const framework::Scope &scope, &cur_scope, Inputs(kOutputGrads), [&](const phi::DenseTensor &outside, phi::DenseTensor *inside) { - inside->ShareDataWith(outside.Slice(seq_offset, seq_offset + 1)); + inside->ShareDataWith( + outside.Slice(seq_offset, seq_offset + 1)); // NOLINT auto dims = phi::vectorize(inside->dims()); dims.erase(dims.begin()); inside->Resize(phi::make_ddim(dims)); @@ -429,7 +433,8 @@ void RecurrentGradOp::RunImpl(const framework::Scope &scope, // Link inside::output -> outside::output // outside::output[seq_offset: seq_offset + 1] = inside::output - executor.CreateVariables(ctx->prog_, &cur_scope, ctx->block_id_); + executor.CreateVariables( + ctx->prog_, &cur_scope, static_cast(ctx->block_id_)); if (step_id > 0) { LinkTensorWithCallback( scope, @@ -443,7 +448,7 @@ void RecurrentGradOp::RunImpl(const framework::Scope &scope, return; } phi::DenseTensor src_slice = - src_tensor.Slice(seq_offset, seq_offset + 1); + src_tensor.Slice(seq_offset, seq_offset + 1); // NOLINT dst_tensor->ShareDataWith(src_slice); }, true /*is_backward*/); @@ -532,7 +537,7 @@ void RecurrentGradOp::RunImpl(const framework::Scope &scope, outside->Resize(PrependDims(seq_len, inside.dims())); outside->mutable_data(place, inside.dtype()); - auto dst = outside->Slice(seq_offset, seq_offset + 1); + auto dst = outside->Slice(seq_offset, seq_offset + 1); // NOLINT framework::TensorCopy(inside, place, dev_ctx, &dst); }, true /*is_backward*/); diff --git a/paddle/fluid/operators/reorder_lod_tensor_by_rank_op.cc b/paddle/fluid/operators/reorder_lod_tensor_by_rank_op.cc index dbd424b1fa0e5..319fad9b39231 100644 --- a/paddle/fluid/operators/reorder_lod_tensor_by_rank_op.cc +++ b/paddle/fluid/operators/reorder_lod_tensor_by_rank_op.cc @@ -182,8 +182,8 @@ class ReorderLoDTensorByRankTableBase : public framework::OperatorBase { } } - auto x_sliced = x.Slice(x_offset, x_offset + len); - auto out_sliced = out->Slice(out_offset, out_offset + len); + auto x_sliced = x.Slice(x_offset, x_offset + len); // NOLINT + auto out_sliced = out->Slice(out_offset, out_offset + len); // NOLINT platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); auto &dev_ctx = *pool.Get(place); diff --git a/paddle/fluid/operators/reshape_op.cc b/paddle/fluid/operators/reshape_op.cc index f1e8134ad5014..26db1962a4c56 100644 --- a/paddle/fluid/operators/reshape_op.cc +++ b/paddle/fluid/operators/reshape_op.cc @@ -96,7 +96,7 @@ class ReshapeOp : public framework::OperatorWithKernel { i, in_dims.size(), in_dims)); - infer_shape[i] = in_dims[i]; + infer_shape[i] = static_cast(in_dims[static_cast(i)]); } } auto infer_out_dims = phi::make_ddim(infer_shape); @@ -109,7 +109,7 @@ class ReshapeOp : public framework::OperatorWithKernel { auto shape_dims = ctx->GetInputDim("Shape"); int num_ele = 1; for (int i = 0; i < shape_dims.size(); ++i) { - num_ele *= shape_dims[i]; + num_ele *= static_cast(shape_dims[i]); } auto vec_dims = std::vector(num_ele, -1); auto out_dims = phi::make_ddim(vec_dims); @@ -160,7 +160,7 @@ class ReshapeOp : public framework::OperatorWithKernel { "be -1. But received shape = [%s], shape[%d] is also -1.", phi::make_ddim(shape), i)); - unk_dim_idx = i; + unk_dim_idx = static_cast(i); } else if (shape[i] == copy_dim_val) { PADDLE_ENFORCE_LT( static_cast(i), @@ -189,9 +189,9 @@ class ReshapeOp : public framework::OperatorWithKernel { // NOTE all non-zero values will be converted to True (include negative // value) - capacity *= (shape[i] ? shape[i] : in_dims[i]); - output_shape[i] = - (shape[i] ? static_cast(shape[i]) : in_dims[i]); + capacity *= (shape[i] ? shape[i] : in_dims[static_cast(i)]); + output_shape[i] = (shape[i] ? static_cast(shape[i]) + : in_dims[static_cast(i)]); } if (unk_dim_idx != -1) { diff --git a/paddle/fluid/operators/row_conv_op.cc b/paddle/fluid/operators/row_conv_op.cc index 1c0ecbd5f49b4..04633c9e8e5dd 100644 --- a/paddle/fluid/operators/row_conv_op.cc +++ b/paddle/fluid/operators/row_conv_op.cc @@ -147,9 +147,9 @@ class RowConvKernel : public framework::OpKernel { bool is_tensor = x->lod().empty(); int batch_size = 0; if (is_tensor) { - batch_size = x->dims()[0]; + batch_size = static_cast(x->dims()[0]); } else { - batch_size = x->lod()[0].size() - 1; + batch_size = static_cast(x->lod()[0].size() - 1); } phi::Vector batch_indices(batch_size + 1); int input_dim = 0; @@ -158,11 +158,11 @@ class RowConvKernel : public framework::OpKernel { for (int i = 0; i < batch_size + 1; i++) { batch_indices[i] = i; } - input_dim = x->dims()[2]; - timesteps = x->dims()[1]; + input_dim = static_cast(x->dims()[2]); + timesteps = static_cast(x->dims()[1]); } else { batch_indices = x->lod()[0]; - input_dim = x->dims()[1]; + input_dim = static_cast(x->dims()[1]); } size_t num_sequence = batch_indices.size() - 1; @@ -225,9 +225,9 @@ class RowConvGradKernel : public framework::OpKernel { bool is_tensor = x_lod.empty(); int batch_size = 0; if (is_tensor) { - batch_size = x->dims()[0]; + batch_size = static_cast(x->dims()[0]); } else { - batch_size = x->lod()[0].size() - 1; + batch_size = static_cast(x->lod()[0].size() - 1); } phi::Vector batch_indices(batch_size + 1); int timesteps = 0; @@ -236,11 +236,11 @@ class RowConvGradKernel : public framework::OpKernel { for (int i = 0; i < batch_size + 1; i++) { batch_indices[i] = i; } - input_dim = x->dims()[2]; - timesteps = x->dims()[1]; + input_dim = static_cast(x->dims()[2]); + timesteps = static_cast(x->dims()[1]); } else { batch_indices = x->lod()[0]; - input_dim = x->dims()[1]; + input_dim = static_cast(x->dims()[1]); } size_t num_sequence = batch_indices.size() - 1; diff --git a/paddle/fluid/operators/sample_logits_op.cc b/paddle/fluid/operators/sample_logits_op.cc index 469f91b16d1e8..1e2e27f460871 100644 --- a/paddle/fluid/operators/sample_logits_op.cc +++ b/paddle/fluid/operators/sample_logits_op.cc @@ -157,7 +157,7 @@ class SampleLogitsOp : public framework::OperatorWithKernel { labels_dims.size())); const int num_samples = ctx->Attrs().Get("num_samples"); - int num_sampled_classes = labels_dims[1] + num_samples; + int num_sampled_classes = static_cast(labels_dims[1] + num_samples); if ((!ctx->IsRuntime()) && labels_dims[1] <= 0) { num_sampled_classes = -1; } diff --git a/paddle/fluid/operators/shrink_rnn_memory_op.cc b/paddle/fluid/operators/shrink_rnn_memory_op.cc index deeee2a582e8e..e883ba8e83092 100644 --- a/paddle/fluid/operators/shrink_rnn_memory_op.cc +++ b/paddle/fluid/operators/shrink_rnn_memory_op.cc @@ -53,13 +53,13 @@ class ShrinkRNNMemoryOp : public ArrayOp { auto &rank_table = rank_table_var->Get(); auto &rank_items = rank_table.items(); - int dst_num_rows = + int dst_num_rows = static_cast( std::lower_bound(rank_items.begin(), rank_items.end(), offset, [](const framework::LoDRankTable::TableItem &a, size_t b) { return a.length > b; }) - - rank_items.begin(); + rank_items.begin()); auto *out_var = scope.FindVar(Output("Out")); PADDLE_ENFORCE_NOT_NULL( @@ -83,8 +83,10 @@ class ShrinkRNNMemoryOp : public ArrayOp { if (dst_num_rows != 0) { out_tensor.mutable_data(place, x_tensor.dtype()); auto dev_ctx = platform::DeviceContextPool::Instance().Get(place); - framework::TensorCopy( - x_tensor.Slice(0, height), place, *dev_ctx, &out_tensor); + framework::TensorCopy(x_tensor.Slice(0, static_cast(height)), + place, + *dev_ctx, + &out_tensor); } } }; diff --git a/paddle/fluid/operators/split_op.cc b/paddle/fluid/operators/split_op.cc index 4b1c44bd34277..246e9368a7d2f 100644 --- a/paddle/fluid/operators/split_op.cc +++ b/paddle/fluid/operators/split_op.cc @@ -76,7 +76,7 @@ class SplitOp : public framework::OperatorWithKernel { // Construct sections_final if (ctx->IsRuntime() && ctx->HasInputs("SectionsTensorList")) { int sections_tensor_list_size = - ctx->GetInputVarPtrs("SectionsTensorList").size(); + static_cast(ctx->GetInputVarPtrs("SectionsTensorList").size()); const paddle::small_vector §ions_varptr_list = ctx->GetInputVarPtrs("SectionsTensorList"); diff --git a/paddle/fluid/operators/spp_op.cc b/paddle/fluid/operators/spp_op.cc index 249fc63a300f9..9fe04caf3e100 100644 --- a/paddle/fluid/operators/spp_op.cc +++ b/paddle/fluid/operators/spp_op.cc @@ -77,7 +77,8 @@ class SppOp : public framework::OperatorWithKernel { 4, platform::errors::InvalidArgument( "Spping intput must be of 4-dimensional.")); - int outlen = ((std::pow(4, pyramid_height) - 1) / (4 - 1)) * in_x_dims[1]; + int outlen = + ((std::pow(4, pyramid_height) - 1) / (4 - 1)) * in_x_dims[1]; // NOLINT std::vector output_shape({in_x_dims[0], outlen}); ctx->SetOutputDim("Out", phi::make_ddim(output_shape)); } diff --git a/paddle/fluid/operators/stft_op.cc b/paddle/fluid/operators/stft_op.cc index f85b856098dc5..14c1af81c1610 100644 --- a/paddle/fluid/operators/stft_op.cc +++ b/paddle/fluid/operators/stft_op.cc @@ -30,7 +30,7 @@ class StftOp : public framework::OperatorWithKernel { const auto x_dims = ctx->GetInputDim("X"); const int x_rank = x_dims.size(); const auto window_dims = ctx->GetInputDim("Window"); - const int window_size = window_dims[0]; + const int window_size = static_cast(window_dims[0]); const bool onesided = ctx->Attrs().Get("onesided"); PADDLE_ENFORCE_EQ( @@ -55,7 +55,7 @@ class StftOp : public framework::OperatorWithKernel { n_fft, window_size)); - int seq_length = x_dims[x_rank - 1]; + int seq_length = static_cast(x_dims[x_rank - 1]); int n_frames = 1 + (seq_length - n_fft) / hop_length; PADDLE_ENFORCE_LE(n_fft, diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index 2c6833a60ce37..5cf9fba9f2681 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -157,8 +157,8 @@ class SumOpVarTypeInference : public framework::VarTypeInference { auto var_type = framework::proto::VarType::SELECTED_ROWS; if (VLOG_IS_ON(10)) { for (size_t ind = 0; ind < ctx->InputSize("X"); ++ind) { - VLOG(10) << ctx->InputVarName("X", ind) << " " - << ctx->GetInputType("X", ind); + VLOG(10) << ctx->InputVarName("X", static_cast(ind)) << " " + << ctx->GetInputType("X", static_cast(ind)); } } @@ -168,8 +168,9 @@ class SumOpVarTypeInference : public framework::VarTypeInference { framework::proto::VarType::LOD_TENSOR_ARRAY)) { std::ostringstream os; for (size_t ind = 0; ind < ctx->InputSize("X"); ++ind) { - os << " " << ctx->InputVarName("X", ind) << " type is " - << ctx->GetInputType("X", ind) << "\n"; + os << " " << ctx->InputVarName("X", static_cast(ind)) + << " type is " << ctx->GetInputType("X", static_cast(ind)) + << "\n"; } PADDLE_THROW(platform::errors::InvalidArgument( "Not all inputs are tensor array:\n%s", os.str())); diff --git a/paddle/fluid/operators/tensor_array_to_tensor_op.cc b/paddle/fluid/operators/tensor_array_to_tensor_op.cc index 56a02f0d0bc43..6188106f64bfa 100644 --- a/paddle/fluid/operators/tensor_array_to_tensor_op.cc +++ b/paddle/fluid/operators/tensor_array_to_tensor_op.cc @@ -105,12 +105,12 @@ class LoDTensorArray2TensorOp : public framework::OperatorBase { for (size_t i = 1; i < n; i++) { for (size_t j = 0; j < in_zero_dims_size; j++) { if (j == static_cast(axis)) { - out_dims[axis] += inx[i].dims()[j]; + out_dims[axis] += inx[i].dims()[static_cast(j)]; } } } auto vec = phi::vectorize(out_dims); - vec.insert(vec.begin() + axis, inx.size()); + vec.insert(vec.begin() + axis, inx.size()); // NOLINT out.Resize(phi::make_ddim(vec)); LodTensorArray2LodTensorVector(scope, base_name, Input("X"), &names); From 25f78de0e6ac617e09c596e774dde3dd062e0e6a Mon Sep 17 00:00:00 2001 From: Zhenghai Zhang <65210872+ccsuzzh@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:44:57 +0800 Subject: [PATCH 184/194] [clang-tidy] NO.33,64 enable `bugprone-signed-char-misuse`,`clang-analyzer-optin.portability.UnixAPI` check (#56744) * enable bugprone-signed-char-misuse&clang-analyzer-optin.portability.UnixAPI check * fix bugprone-signed-char-misuse * fix bug --- .clang-tidy | 4 ++-- paddle/fluid/framework/details/all_reduce_op_handle.cc | 2 +- paddle/fluid/framework/details/broadcast_op_handle.cc | 4 ++-- paddle/fluid/framework/details/op_handle_base.cc | 6 +++--- .../fluid/framework/details/parallel_ssa_graph_executor.cc | 2 +- paddle/fluid/framework/details/reduce_op_handle.cc | 4 ++-- paddle/fluid/framework/dlpack_tensor.cc | 2 +- paddle/fluid/imperative/nccl_context.cc | 4 ++-- paddle/fluid/imperative/xccl_context.cc | 4 ++-- paddle/fluid/memory/allocation/cuda_managed_allocator.cc | 2 +- .../fluid/memory/allocation/cuda_virtual_mem_allocator.cc | 2 +- paddle/fluid/operators/reader/buffered_reader.cc | 2 +- paddle/fluid/platform/device_event_custom_device.cc | 2 +- paddle/fluid/platform/device_event_gpu.cc | 2 +- paddle/fluid/pybind/pybind.cc | 2 +- paddle/phi/backends/custom/custom_device.cc | 2 +- paddle/phi/kernels/cpu/cross_entropy_grad_kernel.cc | 4 ++-- paddle/phi/kernels/cpu/multiclass_nms3_kernel.cc | 4 ++-- paddle/phi/kernels/funcs/cross_entropy.cc | 2 +- 19 files changed, 28 insertions(+), 28 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 21c10cfeaab0d..c1bd5d211ffe0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -24,7 +24,7 @@ bugprone-misplaced-widening-cast, -bugprone-not-null-terminated-result, -bugprone-parent-virtual-call, -bugprone-posix-return, --bugprone-signed-char-misuse, +bugprone-signed-char-misuse, -bugprone-sizeof-container, -bugprone-sizeof-expression, -bugprone-string-constructor, @@ -91,7 +91,7 @@ clang-analyzer-optin.cplusplus.UninitializedObject, -clang-analyzer-optin.osx.cocoa.localizability.NonLocalizedStringChecker, -clang-analyzer-optin.performance.GCDAntipattern, -clang-analyzer-optin.performance.Padding, --clang-analyzer-optin.portability.UnixAPI, +clang-analyzer-optin.portability.UnixAPI, -clang-analyzer-osx.API, -clang-analyzer-osx.MIG, -clang-analyzer-osx.NSOrCFErrorDerefChecker, diff --git a/paddle/fluid/framework/details/all_reduce_op_handle.cc b/paddle/fluid/framework/details/all_reduce_op_handle.cc index 78b8b2e078773..b064a2aded0bc 100644 --- a/paddle/fluid/framework/details/all_reduce_op_handle.cc +++ b/paddle/fluid/framework/details/all_reduce_op_handle.cc @@ -321,7 +321,7 @@ void AllReduceOpHandle::NCCLAllReduceFunc( void AllReduceOpHandle::SyncNCCLAllReduce() { if (FLAGS_sync_nccl_allreduce) { for (auto &p : places_) { - int dev_id = p.device; + int dev_id = p.device; // NOLINT auto *nccl_ctxs = nccl_ctxs_->GetRunEnvNCCLCtx(run_order_, use_hierarchical_allreduce_); auto &nccl_ctx = nccl_ctxs->at(dev_id); diff --git a/paddle/fluid/framework/details/broadcast_op_handle.cc b/paddle/fluid/framework/details/broadcast_op_handle.cc index d28f81f3556cc..b79eff24ee87d 100644 --- a/paddle/fluid/framework/details/broadcast_op_handle.cc +++ b/paddle/fluid/framework/details/broadcast_op_handle.cc @@ -90,7 +90,7 @@ void BroadcastOpHandle::BroadcastOneVar( } else if (platform::is_gpu_place(in_tensor.place())) { #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) VarHandle *out_handle = nullptr; - int root_id = in_tensor.place().device; + int root_id = in_tensor.place().device; // NOLINT std::vector> broadcast_calls; int type = platform::ToNCCLDataType( @@ -101,7 +101,7 @@ void BroadcastOpHandle::BroadcastOneVar( Variable *out_var = var_scopes.at(out_var_handle->scope_idx()) ->FindVar(out_var_handle->name()); - int dst_id = out_var_handle->place().device; + int dst_id = out_var_handle->place().device; // NOLINT auto &nccl_ctx = nccl_ctxs_->at(dst_id); diff --git a/paddle/fluid/framework/details/op_handle_base.cc b/paddle/fluid/framework/details/op_handle_base.cc index 36015052062ea..ee87141a9d541 100644 --- a/paddle/fluid/framework/details/op_handle_base.cc +++ b/paddle/fluid/framework/details/op_handle_base.cc @@ -47,7 +47,7 @@ OpHandleBase::~OpHandleBase() PADDLE_MAY_THROW { // NOLINT void OpHandleBase::InitCUDA() { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) for (auto &p : dev_ctxes_) { - int dev_id = p.first.device; + int dev_id = p.first.device; // NOLINT platform::SetDeviceId(dev_id); #ifdef PADDLE_WITH_HIP PADDLE_ENFORCE_GPU_SUCCESS( @@ -61,7 +61,7 @@ void OpHandleBase::InitCUDA() { for (auto &out_var : outputs_) { auto *out_var_handle = dynamic_cast(out_var); if (out_var_handle) { - int dev_id = out_var_handle->place().device; + int dev_id = out_var_handle->place().device; // NOLINT out_var_handle->SetGenerateEvent(events_.at(dev_id)); } } @@ -74,7 +74,7 @@ void OpHandleBase::InitCUDA() { Name(), dev_ctxes_.size())); auto &place = dev_ctxes_.begin()->first; - int dev_id = place.device; + int dev_id = place.device; // NOLINT for (auto &out_var : outputs_) { auto *out_var_handle = dynamic_cast(out_var); if (out_var_handle) { diff --git a/paddle/fluid/framework/details/parallel_ssa_graph_executor.cc b/paddle/fluid/framework/details/parallel_ssa_graph_executor.cc index e581342947831..42f97e975ed3c 100644 --- a/paddle/fluid/framework/details/parallel_ssa_graph_executor.cc +++ b/paddle/fluid/framework/details/parallel_ssa_graph_executor.cc @@ -45,7 +45,7 @@ static std::vector> SeparateMultiDevicesGraph( for (auto &op : op_handles) { auto &dev_ctx = op->DeviceContext(); auto &p = dev_ctx.begin()->first; - int dev_id = p.device; + int dev_id = p.device; // NOLINT auto &dev_dummys = graphs[dev_id]->Get(kGraphDepVars); graphs[dev_id]->AddNode(graph->RemoveNode(op->Node()).release()); diff --git a/paddle/fluid/framework/details/reduce_op_handle.cc b/paddle/fluid/framework/details/reduce_op_handle.cc index e17f3cbcf6c56..7acf425fd77f3 100644 --- a/paddle/fluid/framework/details/reduce_op_handle.cc +++ b/paddle/fluid/framework/details/reduce_op_handle.cc @@ -189,13 +189,13 @@ void ReduceOpHandle::RunImpl() { out_var_handle->place(), pre_in.dtype()); auto out_p = out_var_handle->place(); - int root_id = out_p.device; + int root_id = out_p.device; // NOLINT std::vector> all_reduce_calls; for (size_t i = 0; i < var_scopes.size(); ++i) { auto &p = in_places[i]; auto &lod_tensor = *lod_tensors[i]; - int dev_id = p.device; + int dev_id = p.device; // NOLINT auto &nccl_ctx = nccl_ctxs_->at(dev_id); void *buffer = const_cast(lod_tensor.data()); diff --git a/paddle/fluid/framework/dlpack_tensor.cc b/paddle/fluid/framework/dlpack_tensor.cc index 46b917cda740a..1e1a02f944f65 100644 --- a/paddle/fluid/framework/dlpack_tensor.cc +++ b/paddle/fluid/framework/dlpack_tensor.cc @@ -99,7 +99,7 @@ struct DLDeviceVisitor { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) ::DLDevice device; device.device_type = kDLGPU; - device.device_id = place.device; + device.device_id = place.device; // NOLINT return device; #else PADDLE_THROW(platform::errors::Unavailable( diff --git a/paddle/fluid/imperative/nccl_context.cc b/paddle/fluid/imperative/nccl_context.cc index 6293a765ad4d1..d70d40808f915 100644 --- a/paddle/fluid/imperative/nccl_context.cc +++ b/paddle/fluid/imperative/nccl_context.cc @@ -80,7 +80,7 @@ void NCCLParallelContext::Init() { } BcastNCCLId(nccl_ids, 0, server_fd); - int gpu_id = place_.device; + int gpu_id = place_.device; // NOLINT for (int ring_id = 0; ring_id < strategy_.nrings_; ring_id++) { VLOG(0) << "init nccl context nranks: " << strategy_.nranks_ << " local rank: " << strategy_.local_rank_ << " gpu id: " << gpu_id @@ -115,7 +115,7 @@ void NCCLParallelContext::InitWithRingID(int ring_id) { } BcastNCCLId(nccl_ids, 0, server_fd); - int gpu_id = place_.device; + int gpu_id = place_.device; // NOLINT VLOG(0) << "init nccl context nranks: " << strategy_.nranks_ << " local rank: " << strategy_.local_rank_ << " gpu id: " << gpu_id << " ring id: " << ring_id; diff --git a/paddle/fluid/imperative/xccl_context.cc b/paddle/fluid/imperative/xccl_context.cc index dc7a4b939b7bd..1ed821d09c346 100644 --- a/paddle/fluid/imperative/xccl_context.cc +++ b/paddle/fluid/imperative/xccl_context.cc @@ -99,7 +99,7 @@ void XCCLParallelContext::Init() { } BcastXCCLId(xccl_ids, 0, server_fd); - int dev_id = place_.device; + int dev_id = place_.device; // NOLINT for (int ring_id = 0; ring_id < strategy_.nrings_; ring_id++) { VLOG(0) << "init nccl context nranks: " << strategy_.nranks_ << " local rank: " << strategy_.local_rank_ << " dev id: " << dev_id @@ -136,7 +136,7 @@ void XCCLParallelContext::InitWithRingID(int ring_id) { } BcastXCCLId(xccl_ids, 0, server_fd); - int dev_id = place_.device; + int dev_id = place_.device; // NOLINT VLOG(0) << "init xccl context nranks: " << strategy_.nranks_ << " local rank: " << strategy_.local_rank_ << " dev id: " << dev_id << " ring id: " << ring_id; diff --git a/paddle/fluid/memory/allocation/cuda_managed_allocator.cc b/paddle/fluid/memory/allocation/cuda_managed_allocator.cc index 06e9fbe88827b..77ca495cacbc7 100644 --- a/paddle/fluid/memory/allocation/cuda_managed_allocator.cc +++ b/paddle/fluid/memory/allocation/cuda_managed_allocator.cc @@ -48,7 +48,7 @@ void CUDAManagedAllocator::FreeImpl(phi::Allocation* allocation) { phi::Allocation* CUDAManagedAllocator::AllocateImpl(size_t size) { std::call_once(once_flag_, [this] { platform::SetDeviceId(place_.device); }); - int dev_id = place_.device; + int dev_id = place_.device; // NOLINT void* ptr; auto result = platform::RecordedGpuMalloc(&ptr, size, diff --git a/paddle/fluid/memory/allocation/cuda_virtual_mem_allocator.cc b/paddle/fluid/memory/allocation/cuda_virtual_mem_allocator.cc index fa20c0fe76698..ccdd18bf3ffa9 100644 --- a/paddle/fluid/memory/allocation/cuda_virtual_mem_allocator.cc +++ b/paddle/fluid/memory/allocation/cuda_virtual_mem_allocator.cc @@ -45,7 +45,7 @@ CUDAVirtualMemAllocator::CUDAVirtualMemAllocator( // case, the allocation will be pinnded device memory local to a given device. prop.type = CU_MEM_ALLOCATION_TYPE_PINNED; prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE; - prop.location.id = place.device; + prop.location.id = place.device; // NOLINT prop_ = prop; // Prepare the access descriptor array indicating where and how the backings diff --git a/paddle/fluid/operators/reader/buffered_reader.cc b/paddle/fluid/operators/reader/buffered_reader.cc index a0ad7e3939a02..2e24caa91c6bb 100644 --- a/paddle/fluid/operators/reader/buffered_reader.cc +++ b/paddle/fluid/operators/reader/buffered_reader.cc @@ -50,7 +50,7 @@ BufferedReader::BufferedReader( VLOG(1) << "BufferedReader"; #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) if (platform::is_gpu_place(place_) && !pin_memory) { - int dev_idx = place_.device; + int dev_idx = place_.device; // NOLINT compute_stream_ = ((phi::GPUContext *)(platform::DeviceContextPool::Instance().Get( place_))) diff --git a/paddle/fluid/platform/device_event_custom_device.cc b/paddle/fluid/platform/device_event_custom_device.cc index 6d284d657818a..cd2242d5a69e1 100644 --- a/paddle/fluid/platform/device_event_custom_device.cc +++ b/paddle/fluid/platform/device_event_custom_device.cc @@ -27,7 +27,7 @@ struct CustomDeviceEventWrapper { platform::errors::PreconditionNotMet( "Required device shall be CustomPlace, but received %d. ", place)); - device_id_ = place.device; + device_id_ = place.device; // NOLINT PADDLE_ENFORCE_GT( device_id_, -1, diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index 37da8daf7fd69..d64b062cda0ac 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -27,7 +27,7 @@ struct CUDADeviceEventWrapper { platform::errors::PreconditionNotMet( "Required device shall be CUDAPlace, but received %d. ", place)); - device_id_ = place.device; + device_id_ = place.device; // NOLINT PADDLE_ENFORCE_GT( device_id_, -1, diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index a3eab23650845..d204b2e919812 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -420,7 +420,7 @@ struct iinfo { dtype = "int64"; break; case framework::proto::VarType::INT8: - min = std::numeric_limits::min(); + min = std::numeric_limits::min(); // NOLINT max = std::numeric_limits::max(); bits = 8; dtype = "int8"; diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc index ef91c0c9f65d3..48bedd1bd939e 100644 --- a/paddle/phi/backends/custom/custom_device.cc +++ b/paddle/phi/backends/custom/custom_device.cc @@ -935,7 +935,7 @@ class CustomDevice : public DeviceInterface { private: inline int PlaceToIdNoCheck(const Place& place) { - int dev_id = place.GetDeviceId(); + int dev_id = place.GetDeviceId(); // NOLINT return dev_id; } diff --git a/paddle/phi/kernels/cpu/cross_entropy_grad_kernel.cc b/paddle/phi/kernels/cpu/cross_entropy_grad_kernel.cc index 23a25fe75d8cc..cc28c27fde276 100644 --- a/paddle/phi/kernels/cpu/cross_entropy_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/cross_entropy_grad_kernel.cc @@ -92,7 +92,7 @@ void CrossEntropyWithSoftmaxGradCPUKernel(const CPUContext& dev_ctx, for (int j = 0; j < remain; j++) { // for each sample_other_dims int idx = i * remain + j; // this sample's label_idx. for 1d case, // remain=1 and j=0, so, idx = i - auto lbl = static_cast(label_data[idx]); + auto lbl = static_cast(label_data[idx]); // NOLINT if (lbl == ignore_index) { for (int k = 0; k < axis_dim; ++k) { // for each class id's label logit_grad_data[i * d + k * remain + j] = 0; @@ -144,7 +144,7 @@ void CrossEntropyWithSoftmaxGradCPUKernel(const CPUContext& dev_ctx, for (int j = 0; j < remain; j++) { // for each sample_other_dims int idx = i * remain + j; // this sample's label_idx. for 1d case, // remain=1 and j=0, so, idx = i - auto lbl = static_cast(label_data[idx]); + auto lbl = static_cast(label_data[idx]); // NOLINT if (lbl == ignore_index) { for (int k = 0; k < axis_dim; ++k) { // for each class id's label logit_grad_data[i * d + k * remain + j] = 0; diff --git a/paddle/phi/kernels/cpu/multiclass_nms3_kernel.cc b/paddle/phi/kernels/cpu/multiclass_nms3_kernel.cc index a03b2370f536f..c4627caa0adb8 100644 --- a/paddle/phi/kernels/cpu/multiclass_nms3_kernel.cc +++ b/paddle/phi/kernels/cpu/multiclass_nms3_kernel.cc @@ -66,8 +66,8 @@ void Array2Poly(const T* box, (*poly).contour = (phi::funcs::gpc_vertex_list*)malloc(sizeof(phi::funcs::gpc_vertex_list)); (*poly).contour->num_vertices = static_cast(pts_num); - (*poly).contour->vertex = - (phi::funcs::gpc_vertex*)malloc(sizeof(phi::funcs::gpc_vertex) * pts_num); + (*poly).contour->vertex = (phi::funcs::gpc_vertex*)malloc( + sizeof(phi::funcs::gpc_vertex) * pts_num); // NOLINT for (size_t i = 0; i < pts_num; ++i) { (*poly).contour->vertex[i].x = box[2 * i]; (*poly).contour->vertex[i].y = box[2 * i + 1]; diff --git a/paddle/phi/kernels/funcs/cross_entropy.cc b/paddle/phi/kernels/funcs/cross_entropy.cc index 9edab1b5890cd..cf53e9ea65efc 100644 --- a/paddle/phi/kernels/funcs/cross_entropy.cc +++ b/paddle/phi/kernels/funcs/cross_entropy.cc @@ -51,7 +51,7 @@ struct HardLabelCrossEntropyCPUFunctorImpl { const auto* label_data = labels_->template data(); for (int i = 0; i < batch_size; ++i) { for (int j = 0; j < num_remain; j++) { - int lbl = static_cast(label_data[i * num_remain + j]); + int lbl = static_cast(label_data[i * num_remain + j]); // NOLINT if (lbl != ignore_index_) { PADDLE_ENFORCE_GE( lbl, From 0e9cc5512424c950177aef224f4605bb97d42eca Mon Sep 17 00:00:00 2001 From: cyberslack_lee Date: Thu, 7 Sep 2023 16:48:54 +0800 Subject: [PATCH 185/194] [clang-tidy] NO.20 clang-analyzer-core.CallAndMessage (#56954) --- .clang-tidy | 2 +- paddle/fluid/eager/grad_node_info.cc | 2 +- .../fluid/framework/ir/delete_cast_op_pass.cc | 2 +- .../ir/embedding_fc_lstm_fuse_pass.cc | 2 +- .../fluid/framework/ir/fuse_adamw_op_pass.cc | 3 +- .../framework/ir/graph_pattern_detector.cc | 10 ++-- .../framework/ir/transfer_layout_elim_pass.cc | 2 +- .../new_executor/program_interpreter.cc | 5 +- paddle/fluid/framework/op_desc.cc | 2 +- paddle/fluid/imperative/layer.cc | 2 +- paddle/fluid/operators/gru_op.cc | 15 ++--- paddle/fluid/operators/gru_op.cu.cc | 2 +- .../operators/mkldnn/matmul_mkldnn_op.cc | 2 +- .../sequence_softmax_cudnn_op.cu.cc | 2 +- paddle/phi/kernels/cpu/rnn_functor.h | 2 +- paddle/phi/kernels/cpu/rnn_grad_kernel.cc | 8 +-- .../kernels/cpu/send_u_recv_grad_kernel.cc | 3 +- paddle/phi/kernels/funcs/activation_functor.h | 55 ++++++++++--------- paddle/phi/kernels/funcs/gpc.cc | 4 +- paddle/phi/kernels/gpu/rnn_functor.h | 2 +- .../sparse/cpu/elementwise_grad_kernel.cc | 4 +- 21 files changed, 70 insertions(+), 61 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c1bd5d211ffe0..27b3a808dd5cf 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -50,7 +50,7 @@ bugprone-use-after-move, -clang-analyzer-apiModeling.google.GTest, -clang-analyzer-apiModeling.llvm.CastValue, -clang-analyzer-apiModeling.llvm.ReturnValue, --clang-analyzer-core.CallAndMessage, +clang-analyzer-core.CallAndMessage, -clang-analyzer-core.DivideZero, -clang-analyzer-core.DynamicTypePropagation, clang-analyzer-core.NonNullParamChecker, diff --git a/paddle/fluid/eager/grad_node_info.cc b/paddle/fluid/eager/grad_node_info.cc index 6aba5f31b6d3f..059e2ff6669af 100644 --- a/paddle/fluid/eager/grad_node_info.cc +++ b/paddle/fluid/eager/grad_node_info.cc @@ -126,7 +126,7 @@ void GradNodeBase::SetGradInMeta(const paddle::Tensor& fwd_out, } else if (phi::distributed::DistTensor::classof(fwd_out.impl().get())) { // TODO(chenweihang): DistTensor contains global and local meta, here // only set the local meta now, we should set global meta later - dense_tensor = + dense_tensor = // NOLINT &(static_cast(fwd_out.impl().get()) ->value()); } else { diff --git a/paddle/fluid/framework/ir/delete_cast_op_pass.cc b/paddle/fluid/framework/ir/delete_cast_op_pass.cc index 9db286927362b..6d4224982f79b 100644 --- a/paddle/fluid/framework/ir/delete_cast_op_pass.cc +++ b/paddle/fluid/framework/ir/delete_cast_op_pass.cc @@ -180,7 +180,7 @@ int DeleteCastOpPass::ApplyCastWriteReadPass(ir::Graph* graph) const { std::string cast_out_name = write_to_array_0_x_name + "_fp16"; VarDesc cast_out_desc(cast_out_name); - cast_out_desc.SetShape(write_to_array_0_x->Var()->GetShape()); + cast_out_desc.SetShape(write_to_array_0_x->Var()->GetShape()); // NOLINT cast_out_desc.SetDataType(proto::VarType::Type::VarType_Type_FP16); auto* cast_out = graph0->CreateVarNode(&cast_out_desc); diff --git a/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc b/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc index fdbb1fa29d585..08d93630eadcd 100644 --- a/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc +++ b/paddle/fluid/framework/ir/embedding_fc_lstm_fuse_pass.cc @@ -121,7 +121,7 @@ static int BuildFusion(Graph* graph, if (with_fc_bias) { // Add FC-bias with LSTM-bias (into GEMM result to be) - auto* fc_bias_var = scope->FindVar(fc_bias->Name()); + auto* fc_bias_var = scope->FindVar(fc_bias->Name()); // NOLINT const auto& fc_bias_tensor = fc_bias_var->Get(); for (int i = 0; i < fc_bias_tensor.numel(); i++) { combined_biases[i] += fc_bias_tensor.data()[i]; diff --git a/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc b/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc index 9bbf2409aff2d..a694bd1fa45e0 100644 --- a/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc +++ b/paddle/fluid/framework/ir/fuse_adamw_op_pass.cc @@ -102,7 +102,8 @@ void InsertOpToGraph(const std::vector> &inout_node_vectors, i++; } - fuse_adamw_op_desc.SetInput("LearningRate", {config.first_lr->Name()}); + fuse_adamw_op_desc.SetInput("LearningRate", + {config.first_lr->Name()}); // NOLINT if (config.use_skip_update) { fuse_adamw_op_desc.SetInput("SkipUpdate", {config.first_skip_update->Name()}); diff --git a/paddle/fluid/framework/ir/graph_pattern_detector.cc b/paddle/fluid/framework/ir/graph_pattern_detector.cc index 06107bdc733ce..a5f35533e8a98 100644 --- a/paddle/fluid/framework/ir/graph_pattern_detector.cc +++ b/paddle/fluid/framework/ir/graph_pattern_detector.cc @@ -72,10 +72,12 @@ PDNode *PDPattern::RetrieveNode(const std::string &id) const { } void PDPattern::AddEdge(PDNode *a, PDNode *b) { - PADDLE_ENFORCE_NOT_NULL( - a, platform::errors::NotFound("PDNode %s is not found.", a->name())); - PADDLE_ENFORCE_NOT_NULL( - b, platform::errors::NotFound("PDNode %s is not found.", b->name())); + PADDLE_ENFORCE_NOT_NULL(a, + platform::errors::NotFound("PDNode %s is not found.", + a->name())); // NOLINT + PADDLE_ENFORCE_NOT_NULL(b, + platform::errors::NotFound("PDNode %s is not found.", + b->name())); // NOLINT PADDLE_ENFORCE_NE(a, b, platform::errors::PermissionDenied( diff --git a/paddle/fluid/framework/ir/transfer_layout_elim_pass.cc b/paddle/fluid/framework/ir/transfer_layout_elim_pass.cc index 4a5ba86e8e032..df4ea068a342e 100644 --- a/paddle/fluid/framework/ir/transfer_layout_elim_pass.cc +++ b/paddle/fluid/framework/ir/transfer_layout_elim_pass.cc @@ -49,7 +49,7 @@ void TransferLayoutElimPass::PutTranferlayoutAfterOp( std::unordered_set remove_nodes; // Ensure op_node has only one output! int op_node_useful_output = 0; - Node *var2; + Node *var2 = nullptr; for (auto ele : op_node->outputs) { if (!ele->outputs.empty()) { op_node_useful_output++; diff --git a/paddle/fluid/framework/new_executor/program_interpreter.cc b/paddle/fluid/framework/new_executor/program_interpreter.cc index 42842ddddc2dc..f59d5812273c3 100644 --- a/paddle/fluid/framework/new_executor/program_interpreter.cc +++ b/paddle/fluid/framework/new_executor/program_interpreter.cc @@ -909,7 +909,8 @@ void ProgramInterpreter::RunOperator(const Instruction& instr_node) { } } - VLOG(4) << "End run " << place << " " << op->DebugStringEx(local_scope); + VLOG(4) << "End run " << place << " " + << op->DebugStringEx(local_scope); // NOLINT if (!instr_node.InplaceBackMap().empty()) { platform::RecordEvent inplaceback_event( @@ -933,7 +934,7 @@ void ProgramInterpreter::RunOperator(const Instruction& instr_node) { instr_node.DeviceContext().Wait(); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PADDLE_ENFORCE_GPU_SUCCESS(platform::GpuGetLastError()); - VLOG(4) << "Operator(" << op->Type() + VLOG(4) << "Operator(" << op->Type() // NOLINT << "): context wait and get last error"; #endif } diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index cdc06e3ea0ba3..ab74b2691b062 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -1200,7 +1200,7 @@ VarDesc *OpDesc::FindVarRecursive(const std::string &name) { PADDLE_THROW(platform::errors::NotFound( "Not found Var(%s) from Block(%d) back into global Block.", name, - block_->ID())); + block_->ID())); // NOLINT } CompileTimeInferShapeContext::CompileTimeInferShapeContext( diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index c2062b899c34e..3528fc87b6ab1 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -387,7 +387,7 @@ void VarBase::CopyFrom(const VarBase& src, const bool blocking) { src.Name())); place = Place(); } else { - dst_tensor->set_lod(src_tensor.lod()); + dst_tensor->set_lod(src_tensor.lod()); // NOLINT dst_tensor->Resize(src_tensor.dims()); } framework::TensorCopy(src_tensor, place, dst_tensor); diff --git a/paddle/fluid/operators/gru_op.cc b/paddle/fluid/operators/gru_op.cc index 20dfa2d91df19..315bd22580972 100644 --- a/paddle/fluid/operators/gru_op.cc +++ b/paddle/fluid/operators/gru_op.cc @@ -513,13 +513,14 @@ class GRUCPUKernel : public framework::OpKernel { gru_value.gate_value = gate_t.data(); gru_value.reset_output_value = reset_hidden_prev_t.data(); - phi::funcs::GRUUnitFunctor::compute(dev_ctx, - gru_value, - frame_size, - cur_batch_size, - active_node, - active_gate, - origin_mode); + phi::funcs::GRUUnitFunctor::compute( + dev_ctx, // NOLINT + gru_value, + frame_size, + cur_batch_size, + active_node, + active_gate, + origin_mode); gru_value.prev_out_value = gru_value.output_value; } diff --git a/paddle/fluid/operators/gru_op.cu.cc b/paddle/fluid/operators/gru_op.cu.cc index f89400fca5373..f7b4317832b0e 100644 --- a/paddle/fluid/operators/gru_op.cu.cc +++ b/paddle/fluid/operators/gru_op.cu.cc @@ -109,7 +109,7 @@ class GRUKernel : public framework::OpKernel { gru_value.output_value = hidden_t.data(); gru_value.gate_value = gate_t.data(); gru_value.reset_output_value = reset_hidden_prev_t.data(); - phi::funcs::GRUUnitFunctor::compute(dev_ctx, + phi::funcs::GRUUnitFunctor::compute(dev_ctx, // NOLINT gru_value, frame_size, cur_batch_size, diff --git a/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc index 49184ac573bc5..a1dad17392a22 100644 --- a/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc @@ -600,7 +600,7 @@ class MatMulGradMKLDNNKernel : public paddle::framework::OpKernel { phi::DenseTensor *out) const { // gradient is calculated in a different way when broadcasting is used bool need_combine = (x->dims().size() == 3 || y->dims().size() == 3) && - out->dims().size() == 2; + out->dims().size() == 2; // NOLINT phi::DenseTensor x_combined, y_combined; if (need_combine) { diff --git a/paddle/fluid/operators/sequence_ops/sequence_softmax_cudnn_op.cu.cc b/paddle/fluid/operators/sequence_ops/sequence_softmax_cudnn_op.cu.cc index 4cd521fdb81bf..3b2583bbe4299 100644 --- a/paddle/fluid/operators/sequence_ops/sequence_softmax_cudnn_op.cu.cc +++ b/paddle/fluid/operators/sequence_ops/sequence_softmax_cudnn_op.cu.cc @@ -81,7 +81,7 @@ class SequenceSoftmaxGradCUDNNKernel : public framework::OpKernel { auto& lod = x->lod(); const size_t level = lod.size() - 1; - x_grad->mutable_data(ctx.GetPlace()); + x_grad->mutable_data(ctx.GetPlace()); // NOLINT for (int i = 0; i < static_cast(lod[level].size()) - 1; ++i) { int start_pos = static_cast(lod[level][i]); int end_pos = static_cast(lod[level][i + 1]); diff --git a/paddle/phi/kernels/cpu/rnn_functor.h b/paddle/phi/kernels/cpu/rnn_functor.h index 87b533799ba90..4adb754174dac 100644 --- a/paddle/phi/kernels/cpu/rnn_functor.h +++ b/paddle/phi/kernels/cpu/rnn_functor.h @@ -295,7 +295,7 @@ void RnnFunc(const Context& dev_ctx, num_layers, init_h_dims[0])); if (is_lstm(cell_type)) { - const auto& init_c_dims = init_c->dims(); + const auto& init_c_dims = init_c->dims(); // NOLINT PADDLE_ENFORCE_EQ(init_c_dims[0], num_layers * direction_num, phi::errors::InvalidArgument( diff --git a/paddle/phi/kernels/cpu/rnn_grad_kernel.cc b/paddle/phi/kernels/cpu/rnn_grad_kernel.cc index bccfa0a95211a..48d6ea98c16de 100644 --- a/paddle/phi/kernels/cpu/rnn_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/rnn_grad_kernel.cc @@ -32,7 +32,7 @@ template void BackupTensor(const CPUContext& dev_ctx, DenseTensor* dst, DenseTensor* src) { - dst->Resize(src->dims()); + dst->Resize(src->dims()); // NOLINT dev_ctx.Alloc(dst); Copy(dev_ctx, *src, dev_ctx.GetPlace(), false, dst); } @@ -250,7 +250,7 @@ struct GRUGradCell : GradCell { gru_value.gate_weight = weight_hh->data(); gru_grad.gate_grad = grad_gate->data(); - gru_grad.reset_output_grad = grad_state->data(); + gru_grad.reset_output_grad = grad_state->data(); // NOLINT gru_grad.prev_out_grad = grad_pre_hidden->data(); gru_grad.output_grad = grad_hidden->data(); gru_grad.gate_weight_grad = grad_weight_hh->data(); @@ -314,9 +314,9 @@ struct LSTMGradCell : GradCell { lstm_value.gate_value = gate_tensor->data(); lstm_value.state_value = state_tensor->data(); lstm_value.state_active_value = act_state_tensor->data(); - lstm_value.prev_state_value = pre_state->data(); + lstm_value.prev_state_value = pre_state->data(); // NOLINT - lstm_grad.state_grad = grad_state->data(); + lstm_grad.state_grad = grad_state->data(); // NOLINT lstm_grad.gate_grad = grad_gate->data(); lstm_grad.output_grad = grad_hidden->data(); lstm_grad.prev_state_grad = grad_pre_state->data(); diff --git a/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc b/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc index 5e48b19488ab9..ae76f01da8e1b 100644 --- a/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/send_u_recv_grad_kernel.cc @@ -55,7 +55,8 @@ void GraphSendRecvCpuGradLoop(const int& index_size, const IndexT& forward_src_idx = d_index[i]; const IndexT& forward_dst_idx = s_index[i]; auto input_slice = input.Slice(forward_src_idx, forward_src_idx + 1); - auto output_slice = output->Slice(forward_dst_idx, forward_dst_idx + 1); + auto output_slice = + output->Slice(forward_dst_idx, forward_dst_idx + 1); // NOLINT auto eigen_input = phi::EigenVector::Flatten(input_slice); auto eigen_output = phi::EigenVector::Flatten(output_slice); diff --git a/paddle/phi/kernels/funcs/activation_functor.h b/paddle/phi/kernels/funcs/activation_functor.h index dd6dbde75ad69..6295ca14aa3ad 100644 --- a/paddle/phi/kernels/funcs/activation_functor.h +++ b/paddle/phi/kernels/funcs/activation_functor.h @@ -528,7 +528,7 @@ struct MishFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - auto sp = (x > static_cast(threshold)) + auto sp = (x > static_cast(threshold)) // NOLINT .select(x, (static_cast(1) + x.exp()).log()); out.device(d) = x * sp.tanh(); } @@ -551,7 +551,7 @@ struct MishGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { - auto sp = (x > static_cast(threshold)) + auto sp = (x > static_cast(threshold)) // NOLINT .select(x, (static_cast(1) + x.exp()).log()); auto gsp = static_cast(1) - (-sp).exp(); auto tsp = sp.tanh(); @@ -571,8 +571,8 @@ struct STanhFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = - static_cast(scale_b) * (static_cast(scale_a) * x).tanh(); + out.device(d) = static_cast(scale_b) * + (static_cast(scale_a) * x).tanh(); // NOLINT } }; @@ -738,7 +738,7 @@ struct SoftplusFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - auto x_beta = static_cast(beta) * x; + auto x_beta = static_cast(beta) * x; // NOLINT out.device(d) = (x_beta > static_cast(threshold)) .select(x, (static_cast(1) + x_beta.exp()).log() / @@ -764,7 +764,7 @@ struct SoftplusGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { - auto x_beta = static_cast(beta) * x; + auto x_beta = static_cast(beta) * x; // NOLINT dx.device(d) = (x_beta > static_cast(threshold)) .select(dout, dout / (static_cast(1) + (-x_beta).exp())); @@ -790,7 +790,7 @@ struct SoftplusDoubleGradFunctor : public BaseActivationFunctor { auto* d = dev.eigen_device(); auto x = EigenVector::Flatten( GET_DATA_SAFELY(X, "Input", "X", "SoftplusDoubleGrad")); - auto x_beta = static_cast(beta) * x; + auto x_beta = static_cast(beta) * x; // NOLINT auto ddx = EigenVector::Flatten( GET_DATA_SAFELY(ddX, "Input", "DDX", "SoftplusDoubleGrad")); @@ -1453,8 +1453,8 @@ struct HardTanhFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = - x.cwiseMax(static_cast(t_min)).cwiseMin(static_cast(t_max)); + out.device(d) = x.cwiseMax(static_cast(t_min)) + .cwiseMin(static_cast(t_max)); // NOLINT } }; @@ -1471,9 +1471,9 @@ struct HardTanhGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { - dx.device(d) = - dout * ((x > static_cast(t_min)) * (x < static_cast(t_max))) - .template cast(); + dx.device(d) = dout * ((x > static_cast(t_min)) * + (x < static_cast(t_max))) // NOLINT + .template cast(); } static constexpr ActBwdOpFwdDeps FwdDeps() { return ActBwdOpFwdDeps::kDepX; } @@ -1508,8 +1508,8 @@ struct LeakyReluGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { - auto temp1 = - static_cast(alpha) * (x < static_cast(0)).template cast(); + auto temp1 = static_cast(alpha) * + (x < static_cast(0)).template cast(); // NOLINT auto temp2 = (x >= static_cast(0)).template cast(); dx.device(d) = dout * (temp1 + temp2).template cast(); } @@ -1593,8 +1593,8 @@ struct Relu6Functor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = - x.cwiseMax(static_cast(0)).cwiseMin(static_cast(threshold)); + out.device(d) = x.cwiseMax(static_cast(0)) + .cwiseMin(static_cast(threshold)); // NOLINT } }; @@ -1731,7 +1731,8 @@ struct ELUFunctor : public BaseActivationFunctor { void operator()(Device d, X x, Out out) const { out.device(d) = (x < static_cast(0)) - .select(static_cast(alpha) * (x.exp() - static_cast(1)), x); + .select(static_cast(alpha) * (x.exp() - static_cast(1)), + x); // NOLINT } }; @@ -2099,7 +2100,7 @@ struct HardSigmoidFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - auto temp = x * static_cast(slope) + static_cast(offset); + auto temp = x * static_cast(slope) + static_cast(offset); // NOLINT out.device(d) = temp.cwiseMax(static_cast(0)).cwiseMin(static_cast(1)); } @@ -2118,7 +2119,7 @@ struct HardSigmoidGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x UNUSED, Out out, dOut dout, dX dx) const { - dx.device(d) = dout * + dx.device(d) = dout * // NOLINT ((out > static_cast(0)) * (out < static_cast(1))) .template cast() * static_cast(slope); @@ -2353,7 +2354,7 @@ struct HardSwishFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = (x + static_cast(offset)) + out.device(d) = (x + static_cast(offset)) // NOLINT .cwiseMax(static_cast(0)) .cwiseMin(static_cast(threshold)) * x / static_cast(scale); @@ -2375,8 +2376,9 @@ struct HardSwishGradFunctor : public BaseActivationFunctor { typename dOut, typename dX> void operator()(Device d, X x, Out out UNUSED, dOut dout, dX dx) const { - auto tmp = ((x + static_cast(offset)) < static_cast(threshold)) - .template cast(); + auto tmp = + ((x + static_cast(offset)) < static_cast(threshold)) // NOLINT + .template cast(); dx.device(d) = dout * (((x + static_cast(offset)) > static_cast(0)).template cast() * @@ -2397,7 +2399,8 @@ struct SwishFunctor : public BaseActivationFunctor { template void operator()(Device d, X x, Out out) const { - out.device(d) = x / (static_cast(1) + (static_cast(-beta) * x).exp()); + out.device(d) = + x / (static_cast(1) + (static_cast(-beta) * x).exp()); // NOLINT } }; @@ -2431,7 +2434,7 @@ struct PowFunctor : public BaseActivationFunctor { } template void operator()(Device d, X x, Out out) const { - out.device(d) = x.pow(static_cast(factor)); + out.device(d) = x.pow(static_cast(factor)); // NOLINT } }; @@ -2585,8 +2588,8 @@ struct CELUFunctor : public BaseActivationFunctor { void operator()(Device d, X x, Out out) const { out.device(d) = (x < static_cast(0)) - .select(static_cast(alpha) * - ((x / static_cast(alpha)).exp() - static_cast(1)), + .select(static_cast(alpha) * ((x / static_cast(alpha)).exp() - + static_cast(1)), // NOLINT x); } }; diff --git a/paddle/phi/kernels/funcs/gpc.cc b/paddle/phi/kernels/funcs/gpc.cc index 671ebf26613b0..9dea9fc4c9be3 100644 --- a/paddle/phi/kernels/funcs/gpc.cc +++ b/paddle/phi/kernels/funcs/gpc.cc @@ -893,7 +893,7 @@ void gpc_add_contour(gpc_polygon *p, gpc_vertex_list *new_contour, int hole) { /* Copy the old contour and hole data into the extended arrays */ for (c = 0; c < p->num_contours; c++) { extended_hole[c] = p->hole[c]; - extended_contour[c] = p->contour[c]; + extended_contour[c] = p->contour[c]; // NOLINT } /* Copy the new contour and hole onto the end of the extended arrays */ @@ -905,7 +905,7 @@ void gpc_add_contour(gpc_polygon *p, gpc_vertex_list *new_contour, int hole) { new_contour->num_vertices * static_cast(sizeof(gpc_vertex)), const_cast("contour addition")); for (v = 0; v < new_contour->num_vertices; v++) { - extended_contour[c].vertex[v] = new_contour->vertex[v]; + extended_contour[c].vertex[v] = new_contour->vertex[v]; // NOLINT } /* Dispose of the old contour */ diff --git a/paddle/phi/kernels/gpu/rnn_functor.h b/paddle/phi/kernels/gpu/rnn_functor.h index fc27258981d39..bdbcd05e65772 100644 --- a/paddle/phi/kernels/gpu/rnn_functor.h +++ b/paddle/phi/kernels/gpu/rnn_functor.h @@ -110,7 +110,7 @@ class RNNDescriptors { dropout_state->Resize({static_cast(state_size)}); dev_ctx.template Alloc(dropout_state); } - dropout_desc_.descriptor(handle, + dropout_desc_.descriptor(handle, // NOLINT dev_ctx.GetPlace(), is_initialized, dropout_prob_, diff --git a/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc b/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc index 776f6611fd590..88a01e1135b7b 100644 --- a/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/elementwise_grad_kernel.cc @@ -40,7 +40,7 @@ void AllocCsrPtr(const Context& dev_ctx, DenseTensor dx_crows = phi::EmptyLike(dev_ctx, x.crows()); DenseTensor dx_cols = phi::EmptyLike(dev_ctx, x.cols()); DenseTensor dx_values = phi::EmptyLike(dev_ctx, x.values()); - dx->set_meta(x.meta()); + dx->set_meta(x.meta()); // NOLINT dx->SetMember(dx_crows, dx_cols, dx_values, x.dims()); } @@ -50,7 +50,7 @@ void AllocCooPtr(const Context& dev_ctx, SparseCooTensor* dx) { DenseTensor dx_indices = phi::EmptyLike(dev_ctx, x.indices()); DenseTensor dx_values = phi::EmptyLike(dev_ctx, x.values()); - dx->set_meta(x.meta()); + dx->set_meta(x.meta()); // NOLINT dx->SetMember(dx_indices, dx_values, x.dims(), x.coalesced()); } From e53afe54f319f5085f9fbe409df95f5d3fb58dcc Mon Sep 17 00:00:00 2001 From: Difer <707065510@qq.com> Date: Thu, 7 Sep 2023 17:26:19 +0800 Subject: [PATCH 186/194] =?UTF-8?q?=E3=80=90Fluid=20Clean=E3=80=91=20Renam?= =?UTF-8?q?e=20paddle.fluid=20to=20paddle.base=20(#56576)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * rename fluid to base * fix setup and rename paddle.fluid * fix protobuf generated paddle.fluid * fix some errors * fix cmake depends * fix some error * try to fix cache error * fix codestyle && some doc error * remove build change * fix some error --------- Co-authored-by: XiangGao --- .flake8 | 2 +- .gitignore | 2 +- cmake/generic.cmake | 16 +- cmake/replace_string.py | 31 + paddle/CMakeLists.txt | 2 +- paddle/cinn/hlir/op/transform.cc | 2 +- paddle/fluid/API.spec | 6 +- paddle/fluid/framework/CMakeLists.txt | 8 +- .../framework/ir/auto_mixed_precision_pass.cc | 2 +- paddle/fluid/pybind/imperative.cc | 6 +- paddle/fluid/pybind/place.cc | 4 +- paddle/fluid/pybind/pybind.cc | 15 +- paddle/fluid/pybind/tensor.cc | 46 +- paddle/phi/api/profiler/CMakeLists.txt | 2 +- paddle/scripts/paddle_build.sh | 5 +- pyproject.toml | 4 +- python/CMakeLists.txt | 4 +- python/paddle/_C_ops.py | 2 +- python/paddle/__init__.py | 2 +- python/paddle/_ir_ops.py | 2 +- python/paddle/_legacy_C_ops.py | 2 +- python/paddle/amp/__init__.py | 4 +- python/paddle/amp/auto_cast.py | 6 +- python/paddle/amp/debugging.py | 20 +- python/paddle/amp/grad_scaler.py | 8 +- python/paddle/autograd/__init__.py | 10 +- python/paddle/autograd/autograd.py | 4 +- python/paddle/autograd/backward_mode.py | 4 +- python/paddle/autograd/py_layer.py | 4 +- python/paddle/autograd/saved_tensors_hooks.py | 2 +- python/paddle/{fluid => base}/.gitignore | 0 python/paddle/{fluid => base}/__init__.py | 6 +- python/paddle/{fluid => base}/backward.py | 26 +- python/paddle/{fluid => base}/compiler.py | 6 +- python/paddle/{fluid => base}/core.py | 0 .../paddle/{fluid => base}/data_feed_desc.py | 30 +- python/paddle/{fluid => base}/data_feeder.py | 28 +- python/paddle/{fluid => base}/dataset.py | 220 +++---- .../{fluid => base}/default_scope_funcs.py | 6 +- .../paddle/{fluid => base}/device_worker.py | 0 .../{fluid => base}/dygraph/__init__.py | 0 python/paddle/{fluid => base}/dygraph/base.py | 78 +-- .../{fluid => base}/dygraph/math_op_patch.py | 0 .../dygraph/tensor_patch_methods.py | 20 +- .../paddle/{fluid => base}/dygraph/tracer.py | 4 +- .../paddle/{fluid => base}/dygraph_utils.py | 0 python/paddle/{fluid => base}/executor.py | 42 +- python/paddle/{fluid => base}/framework.py | 154 ++--- .../{fluid => base}/incubate/__init__.py | 2 +- .../incubate/checkpoint/__init__.py | 0 .../incubate/checkpoint/auto_checkpoint.py | 4 +- .../incubate/checkpoint/checkpoint_saver.py | 0 python/paddle/{fluid => base}/initializer.py | 2 +- python/paddle/{fluid => base}/io.py | 12 +- python/paddle/{fluid => base}/layer_helper.py | 0 .../{fluid => base}/layer_helper_base.py | 6 +- .../paddle/{fluid => base}/layers/__init__.py | 0 python/paddle/{fluid => base}/layers/io.py | 0 .../layers/layer_function_generator.py | 2 +- .../{fluid => base}/layers/math_op_patch.py | 18 +- python/paddle/{fluid => base}/lod_tensor.py | 12 +- python/paddle/{fluid => base}/log_helper.py | 0 .../{fluid => base}/multiprocess_utils.py | 0 python/paddle/{fluid => base}/param_attr.py | 2 +- python/paddle/{fluid => base}/reader.py | 94 +-- python/paddle/{fluid => base}/trainer_desc.py | 0 .../paddle/{fluid => base}/trainer_factory.py | 2 +- python/paddle/{fluid => base}/unique_name.py | 0 .../paddle/{fluid => base}/variable_index.py | 22 +- .../{fluid => base}/wrapped_decorator.py | 0 python/paddle/common_ops_import.py | 16 +- python/paddle/cost_model/cost_model.py | 2 +- python/paddle/decomposition/decomp.py | 2 +- python/paddle/device/__init__.py | 16 +- python/paddle/device/cuda/__init__.py | 4 +- python/paddle/device/cuda/graphs.py | 8 +- python/paddle/device/cuda/streams.py | 4 +- python/paddle/device/xpu/__init__.py | 2 +- .../auto_parallel/static/auto_align_tool.py | 4 +- .../auto_parallel/static/cluster.py | 2 +- .../auto_parallel/static/completion.py | 2 +- .../auto_parallel/static/cost/base_cost.py | 4 +- .../auto_parallel/static/cost_model.py | 2 +- .../auto_parallel/static/dist_attribute.py | 6 +- .../auto_parallel/static/dist_loader.py | 2 +- .../auto_parallel/static/engine.py | 2 +- .../static/tuner/rule_based_tuner.py | 6 +- .../distributed/auto_parallel/static/utils.py | 8 +- python/paddle/distributed/collective.py | 2 +- .../communication/stream/all_gather.py | 2 +- .../communication/stream/all_reduce.py | 2 +- .../communication/stream/all_to_all.py | 2 +- .../communication/stream/broadcast.py | 2 +- .../distributed/communication/stream/recv.py | 2 +- .../communication/stream/reduce.py | 2 +- .../communication/stream/reduce_scatter.py | 2 +- .../communication/stream/scatter.py | 2 +- .../distributed/communication/stream/send.py | 2 +- .../fleet/base/distributed_strategy.py | 4 +- .../distributed/fleet/base/role_maker.py | 2 +- .../paddle/distributed/fleet/base/topology.py | 2 +- .../distributed/fleet/base/util_factory.py | 8 +- .../distributed/fleet/dataset/dataset.py | 4 +- .../fleet/dataset/index_dataset.py | 2 +- python/paddle/distributed/fleet/fleet.py | 8 +- .../distributed/fleet/layers/mpu/mp_layers.py | 2 +- .../distributed/fleet/layers/mpu/mp_ops.py | 2 +- .../distributed/fleet/layers/mpu/random.py | 4 +- .../fleet/meta_optimizers/dgc_optimizer.py | 6 +- .../meta_optimizers/raw_program_optimizer.py | 2 +- .../fleet/meta_optimizers/sharding/utils.py | 2 +- .../meta_optimizers/sharding_optimizer.py | 2 +- .../fleet/meta_parallel/pipeline_parallel.py | 2 +- .../sharding/group_sharded_stage3.py | 2 +- .../sharding/group_sharded_utils.py | 4 +- .../distributed/fleet/recompute/recompute.py | 2 +- .../fleet/recompute/recompute_hybrid.py | 2 +- .../distributed/fleet/runtime/the_one_ps.py | 28 +- python/paddle/distributed/fleet/scaler.py | 2 +- python/paddle/distributed/fleet/utils/fs.py | 2 +- .../fleet/utils/hybrid_parallel_inference.py | 37 +- .../fleet/utils/hybrid_parallel_util.py | 8 +- .../fleet/utils/mix_precision_utils.py | 6 +- .../fleet/utils/sequence_parallel_utils.py | 2 +- .../fleet/utils/tensor_parallel_utils.py | 2 +- python/paddle/distributed/io.py | 38 +- .../distributed/launch/context/device.py | 5 +- python/paddle/distributed/parallel.py | 2 +- .../paddle/distributed/parallel_with_gloo.py | 7 +- .../distributed/passes/auto_parallel_amp.py | 2 +- .../distributed/passes/auto_parallel_fp16.py | 4 +- .../passes/auto_parallel_pipeline.py | 4 +- .../passes/auto_parallel_recompute.py | 4 +- .../paddle/distributed/passes/pass_utils.py | 6 +- .../distributed/passes/pipeline_pass_base.py | 2 +- .../passes/pipeline_scheduler_pass.py | 2 +- .../distributed/passes/ps_trainer_pass.py | 2 +- .../ps/utils/collective_transpiler.py | 2 +- .../ps/utils/ps_program_builder.py | 18 +- python/paddle/distributed/ps/utils/public.py | 14 +- python/paddle/distributed/rpc/rpc.py | 2 +- python/paddle/distributed/spawn.py | 7 +- .../distributed/transpiler/collective.py | 2 +- .../transpiler/details/vars_distributed.py | 4 +- .../transpiler/distribute_transpiler.py | 10 +- python/paddle/distributed/utils/nccl_utils.py | 2 +- python/paddle/distribution/bernoulli.py | 4 +- python/paddle/distribution/categorical.py | 4 +- python/paddle/distribution/cauchy.py | 2 +- python/paddle/distribution/dirichlet.py | 4 +- python/paddle/distribution/distribution.py | 4 +- python/paddle/distribution/geometric.py | 2 +- python/paddle/distribution/gumbel.py | 2 +- python/paddle/distribution/laplace.py | 2 +- python/paddle/distribution/normal.py | 4 +- python/paddle/distribution/transform.py | 16 +- python/paddle/distribution/uniform.py | 4 +- python/paddle/fft.py | 4 +- python/paddle/framework/__init__.py | 76 +-- python/paddle/framework/dtype.py | 6 +- python/paddle/framework/framework.py | 2 +- python/paddle/framework/io.py | 28 +- python/paddle/framework/io_utils.py | 34 +- python/paddle/framework/ir.py | 2 +- python/paddle/framework/random.py | 16 +- python/paddle/geometric/math.py | 4 +- .../geometric/message_passing/send_recv.py | 6 +- .../paddle/geometric/message_passing/utils.py | 4 +- python/paddle/geometric/reindex.py | 6 +- python/paddle/geometric/sampling/neighbors.py | 4 +- python/paddle/hapi/model.py | 90 ++- python/paddle/hapi/model_summary.py | 6 +- python/paddle/hapi/static_flops.py | 2 +- python/paddle/incubate/__init__.py | 2 +- python/paddle/incubate/asp/asp.py | 8 +- .../incubate/asp/supported_layer_list.py | 2 +- .../incubate/autograd/composite_rules.py | 26 +- python/paddle/incubate/autograd/functional.py | 4 +- python/paddle/incubate/autograd/primapi.py | 8 +- python/paddle/incubate/autograd/primops.py | 4 +- python/paddle/incubate/autograd/primreg.py | 8 +- python/paddle/incubate/autograd/primx.py | 22 +- python/paddle/incubate/autograd/utils.py | 4 +- python/paddle/incubate/autotune.py | 2 +- python/paddle/incubate/checkpoint/__init__.py | 2 +- .../paddle/incubate/distributed/fleet/base.py | 12 +- .../incubate/distributed/fleet/collective.py | 22 +- .../incubate/distributed/fleet/fleet_util.py | 66 +-- .../distribute_transpiler/__init__.py | 6 +- .../distributed_strategy.py | 12 +- .../fleet/parameter_server/ir/public.py | 2 +- .../parameter_server/ir/vars_metatools.py | 4 +- .../fleet/parameter_server/pslib/__init__.py | 12 +- .../incubate/distributed/fleet/role_maker.py | 22 +- .../incubate/distributed/fleet/utils.py | 38 +- .../distributed/utils/io/dist_load.py | 2 +- .../distributed/utils/io/dist_save.py | 2 +- .../distributed/utils/io/save_for_auto.py | 2 +- python/paddle/incubate/layers/nn.py | 12 +- .../incubate/multiprocessing/reductions.py | 16 +- .../nn/functional/fused_dropout_add.py | 2 +- .../incubate/nn/functional/fused_ec_moe.py | 2 +- .../nn/functional/fused_matmul_bias.py | 2 +- .../nn/functional/fused_transformer.py | 8 +- .../incubate/nn/layer/fused_transformer.py | 10 +- python/paddle/incubate/nn/layer/io.py | 2 +- python/paddle/incubate/nn/loss.py | 4 +- .../incubate/nn/memory_efficient_attention.py | 2 +- .../incubate/operators/graph_khop_sampler.py | 4 +- .../incubate/operators/graph_reindex.py | 4 +- .../operators/graph_sample_neighbors.py | 4 +- .../incubate/operators/graph_send_recv.py | 6 +- .../paddle/incubate/operators/resnet_unit.py | 12 +- .../incubate/operators/softmax_mask_fuse.py | 2 +- .../softmax_mask_fuse_upper_triangle.py | 2 +- python/paddle/incubate/operators/unzip.py | 4 +- .../optimizer/distributed_fused_lamb.py | 8 +- .../incubate/optimizer/functional/utils.py | 4 +- .../incubate/optimizer/gradient_merge.py | 14 +- .../incubate/optimizer/lars_momentum.py | 20 +- python/paddle/incubate/optimizer/lbfgs.py | 8 +- python/paddle/incubate/optimizer/lookahead.py | 12 +- .../paddle/incubate/optimizer/modelaverage.py | 14 +- python/paddle/incubate/optimizer/pipeline.py | 26 +- python/paddle/incubate/optimizer/recompute.py | 26 +- python/paddle/incubate/passes/ir.py | 16 +- python/paddle/incubate/tensor/manipulation.py | 4 +- python/paddle/incubate/tensor/math.py | 4 +- python/paddle/incubate/xpu/resnet_block.py | 14 +- python/paddle/inference/__init__.py | 2 +- .../inference/contrib/utils/__init__.py | 2 +- python/paddle/inference/wrapper.py | 6 +- .../paddle/io/dataloader/dataloader_iter.py | 6 +- python/paddle/io/dataloader/flat.py | 4 +- python/paddle/io/dataloader/worker.py | 2 +- python/paddle/io/reader.py | 4 +- python/paddle/ir/__init__.py | 4 +- python/paddle/ir/core.py | 8 +- python/paddle/jit/api.py | 34 +- .../paddle/jit/dy2static/base_transformer.py | 2 +- .../jit/dy2static/basic_api_transformer.py | 2 +- .../dy2static/break_continue_transformer.py | 2 +- .../paddle/jit/dy2static/convert_operators.py | 11 +- python/paddle/jit/dy2static/function_spec.py | 10 +- .../jit/dy2static/ifelse_transformer.py | 2 +- python/paddle/jit/dy2static/logging_utils.py | 2 +- .../paddle/jit/dy2static/loop_transformer.py | 2 +- python/paddle/jit/dy2static/origin_info.py | 4 +- .../paddle/jit/dy2static/partial_program.py | 20 +- .../jit/dy2static/program_translator.py | 14 +- .../jit/dy2static/return_transformer.py | 2 +- python/paddle/jit/dy2static/utils.py | 14 +- python/paddle/jit/dy2static/utils_helper.py | 10 +- .../jit/dy2static/variable_trans_func.py | 2 +- python/paddle/jit/layer.py | 4 +- python/paddle/jit/translated_layer.py | 8 +- python/paddle/metric/metrics.py | 20 +- python/paddle/new_ir_utils.py | 16 +- python/paddle/nn/clip.py | 70 +-- python/paddle/nn/decode.py | 2 +- python/paddle/nn/functional/activation.py | 6 +- python/paddle/nn/functional/common.py | 4 +- python/paddle/nn/functional/conv.py | 6 +- python/paddle/nn/functional/distance.py | 6 +- python/paddle/nn/functional/extension.py | 6 +- .../paddle/nn/functional/flash_attention.py | 4 +- python/paddle/nn/functional/input.py | 4 +- python/paddle/nn/functional/loss.py | 38 +- python/paddle/nn/functional/norm.py | 14 +- python/paddle/nn/functional/pooling.py | 6 +- .../paddle/nn/functional/sparse_attention.py | 2 +- python/paddle/nn/functional/vision.py | 6 +- python/paddle/nn/initializer/Bilinear.py | 4 +- python/paddle/nn/initializer/__init__.py | 2 +- python/paddle/nn/initializer/assign.py | 6 +- python/paddle/nn/initializer/constant.py | 4 +- python/paddle/nn/initializer/dirac.py | 20 +- python/paddle/nn/initializer/initializer.py | 2 +- python/paddle/nn/initializer/kaiming.py | 4 +- python/paddle/nn/initializer/lazy_init.py | 2 +- python/paddle/nn/initializer/normal.py | 6 +- python/paddle/nn/initializer/orthogonal.py | 6 +- python/paddle/nn/initializer/uniform.py | 6 +- python/paddle/nn/initializer/xavier.py | 6 +- python/paddle/nn/layer/container.py | 4 +- python/paddle/nn/layer/layers.py | 26 +- python/paddle/nn/layer/loss.py | 8 +- python/paddle/nn/layer/norm.py | 10 +- python/paddle/nn/layer/rnn.py | 24 +- python/paddle/nn/layer/transformer.py | 4 +- python/paddle/nn/quant/quant_layers.py | 6 +- .../paddle/nn/utils/transform_parameters.py | 10 +- python/paddle/nn/utils/weight_norm_hook.py | 4 +- python/paddle/optimizer/adadelta.py | 12 +- python/paddle/optimizer/adagrad.py | 4 +- python/paddle/optimizer/adam.py | 16 +- python/paddle/optimizer/adamax.py | 14 +- python/paddle/optimizer/adamw.py | 12 +- python/paddle/optimizer/lamb.py | 12 +- python/paddle/optimizer/lbfgs.py | 16 +- python/paddle/optimizer/lr.py | 8 +- python/paddle/optimizer/momentum.py | 10 +- python/paddle/optimizer/optimizer.py | 38 +- python/paddle/optimizer/rmsprop.py | 12 +- python/paddle/optimizer/sgd.py | 14 +- python/paddle/profiler/profiler.py | 2 +- python/paddle/profiler/profiler_statistic.py | 2 +- python/paddle/profiler/utils.py | 4 +- python/paddle/quantization/imperative/qat.py | 2 +- .../paddle/quantization/quanters/abs_max.py | 4 +- python/paddle/reader/decorator.py | 22 +- python/paddle/regularizer.py | 4 +- python/paddle/signal.py | 4 +- python/paddle/sparse/binary.py | 4 +- python/paddle/sparse/creation.py | 6 +- python/paddle/sparse/multiary.py | 2 +- .../paddle/sparse/nn/functional/activation.py | 4 +- python/paddle/sparse/nn/functional/conv.py | 2 +- .../sparse/nn/functional/transformer.py | 2 +- python/paddle/sparse/nn/layer/norm.py | 2 +- python/paddle/sparse/unary.py | 8 +- python/paddle/static/__init__.py | 52 +- python/paddle/static/amp/amp_nn.py | 8 +- python/paddle/static/amp/bf16/amp_lists.py | 2 +- python/paddle/static/amp/bf16/amp_utils.py | 10 +- python/paddle/static/amp/bf16/decorator.py | 2 +- python/paddle/static/amp/debugging.py | 16 +- python/paddle/static/amp/decorator.py | 4 +- python/paddle/static/amp/fp16_lists.py | 4 +- python/paddle/static/amp/fp16_utils.py | 10 +- python/paddle/static/amp/function_overload.py | 2 +- python/paddle/static/input.py | 10 +- python/paddle/static/io.py | 66 +-- python/paddle/static/nn/common.py | 34 +- python/paddle/static/nn/control_flow.py | 18 +- python/paddle/static/nn/loss.py | 14 +- python/paddle/static/nn/metric.py | 6 +- python/paddle/static/nn/sequence_lod.py | 44 +- python/paddle/static/nn/static_pylayer.py | 8 +- .../post_training_quantization.py | 2 +- .../quantization/quant2_int8_mkldnn_pass.py | 2 +- .../quantization/quant_int8_mkldnn_pass.py | 2 +- python/paddle/static/quantization/quanter.py | 2 +- .../static/quantization/quantization_pass.py | 14 +- python/paddle/static/quantization/utils.py | 2 +- python/paddle/tensor/array.py | 2 +- python/paddle/tensor/attribute.py | 4 +- python/paddle/tensor/creation.py | 6 +- python/paddle/tensor/einsum.py | 6 +- .../paddle/tensor/layer_function_generator.py | 4 +- python/paddle/tensor/linalg.py | 6 +- python/paddle/tensor/logic.py | 6 +- python/paddle/tensor/manipulation.py | 14 +- python/paddle/tensor/math.py | 4 +- python/paddle/tensor/ops.py | 2 +- python/paddle/tensor/random.py | 6 +- python/paddle/tensor/search.py | 6 +- python/paddle/tensor/stat.py | 2 +- python/paddle/tensor/to_string.py | 2 +- python/paddle/text/viterbi_decode.py | 6 +- python/paddle/utils/__init__.py | 2 +- .../utils/cpp_extension/cpp_extension.py | 2 +- .../utils/cpp_extension/extension_utils.py | 34 +- python/paddle/utils/dlpack.py | 12 +- python/paddle/utils/inplace_utils.py | 4 +- python/paddle/utils/layers_utils.py | 6 +- python/paddle/utils/op_version.py | 2 +- python/paddle/utils/unique_name.py | 8 +- python/paddle/vision/models/alexnet.py | 2 +- python/paddle/vision/models/densenet.py | 2 +- python/paddle/vision/models/googlenet.py | 2 +- python/paddle/vision/models/inceptionv3.py | 2 +- python/paddle/vision/models/squeezenet.py | 2 +- python/paddle/vision/ops.py | 8 +- python/paddle/vision/transforms/functional.py | 2 +- .../vision/transforms/functional_cv2.py | 2 +- .../vision/transforms/functional_pil.py | 2 +- .../vision/transforms/functional_tensor.py | 2 +- python/setup.py.in | 36 +- r/README.md | 2 +- r/README_cn.md | 2 +- r/example/mobilenet.py | 2 +- r/example/mobilenet.r | 2 +- security/advisory/pdsa-2022-001.md | 2 +- security/advisory/pdsa-2022-001_cn.md | 2 +- setup.py | 44 +- test/amp/amp_base_models.py | 2 +- test/amp/test_amp_api.py | 4 +- test/amp/test_amp_decorate.py | 2 +- test/amp/test_amp_list.py | 2 +- test/amp/test_amp_master_grad.py | 4 +- test/amp/test_amp_promote.py | 4 +- test/amp/test_collect_operator_stats.py | 2 +- test/amp/test_compare_accuracy_api.py | 4 +- test/amp/test_layer_convert_dtype.py | 2 +- test/amp/test_model_cast_to_bf16.py | 22 +- test/asp/asp_pruning_base.py | 18 +- test/asp/test_asp_customized_pruning.py | 20 +- test/asp/test_asp_optimize_dynamic.py | 2 +- test/asp/test_asp_optimize_static.py | 30 +- test/asp/test_asp_pruning_dynamic.py | 2 +- test/asp/test_asp_pruning_static.py | 18 +- test/asp/test_asp_save_load.py | 16 +- test/asp/test_fleet_with_asp_dynamic.py | 2 +- test/asp/test_fleet_with_asp_sharding.py | 20 +- test/asp/test_fleet_with_asp_static.py | 54 +- test/auto_parallel/1F1B_pass_unittest.py | 6 +- test/auto_parallel/amp_o2_pass.py | 6 +- test/auto_parallel/amp_pass_unittest.py | 6 +- .../auto_parallel_relaunch_model.py | 2 +- .../auto_parallel/clip_grad_by_global_norm.py | 10 +- test/auto_parallel/engine_api.py | 12 +- .../generation_pipeline_pass_unittest.py | 2 +- .../gradient_merge_pass_unittest.py | 6 +- .../pipeline_scheduler_unittest.py | 6 +- test/auto_parallel/random_control_unittest.py | 6 +- test/auto_parallel/recompute_pass_unittest.py | 6 +- test/auto_parallel/reshard_r_to_s.py | 2 +- test/auto_parallel/reshard_s_to_r.py | 2 +- test/auto_parallel/sharding_newexe.py | 6 +- test/auto_parallel/sharding_pass_unittest.py | 4 +- test/auto_parallel/test_align_tool.py | 10 +- test/auto_parallel/test_dist_assign.py | 4 +- test/auto_parallel/test_dist_attr_v2.py | 4 +- test/auto_parallel/test_dist_context.py | 2 +- test/auto_parallel/test_dist_embedding.py | 6 +- test/auto_parallel/test_dist_matmul.py | 12 +- test/auto_parallel/test_dist_op_cost.py | 4 +- test/auto_parallel/test_dist_pnorm.py | 20 +- test/auto_parallel/test_dist_reshape.py | 4 +- test/auto_parallel/test_dist_scale.py | 4 +- test/auto_parallel/test_dist_shape.py | 4 +- test/auto_parallel/test_dist_slice.py | 8 +- test/auto_parallel/test_dist_split.py | 4 +- test/auto_parallel/test_fp16_assign.py | 4 +- test/auto_parallel/test_fused_linear_pass.py | 6 +- test/auto_parallel/test_interface.py | 2 +- test/auto_parallel/test_pass_base_list.py | 6 +- test/auto_parallel/test_pass_bf16.py | 8 +- test/auto_parallel/test_prim_dist_op.py | 2 +- test/auto_parallel/test_process_mesh.py | 6 +- .../auto_parallel/test_selective_recompute.py | 6 +- test/auto_parallel/test_serialization.py | 6 +- test/auto_parallel/test_shard_tensor_api.py | 2 +- .../auto_parallel/test_while_op_completion.py | 2 +- test/auto_parallel/test_while_op_partition.py | 6 +- .../test_autograd_functional_dynamic.py | 2 +- .../test_autograd_functional_static.py | 58 +- test/autograd/test_jvp_and_transpose.py | 2 +- test/autograd/test_orig2prim.py | 2 +- test/autograd/test_prim2orig.py | 2 +- test/autograd/test_primapi.py | 10 +- test/autograd/utils.py | 4 +- test/book/notest_understand_sentiment.py | 34 +- test/book/test_fit_a_line.py | 38 +- test/book/test_image_classification.py | 34 +- test/book/test_recognize_digits.py | 36 +- test/book/test_recommender_system.py | 38 +- test/book/test_word2vec_book.py | 60 +- test/cinn/op_mappers/op_mapper_test.py | 6 +- test/cinn/ops/test_conv2d_op.py | 2 +- test/cinn/test_computation.py | 8 +- test/cinn/test_efficientnet.py | 8 +- test/cinn/test_facedet.py | 8 +- test/cinn/test_frontend.py | 14 +- test/cinn/test_mobilenetv1.py | 8 +- test/cinn/test_mobilenetv2.py | 8 +- test/cinn/test_resnet.py | 8 +- test/cinn/test_resnet18.py | 8 +- test/cinn/test_resnet50.py | 8 +- test/cinn/test_squeezenet.py | 8 +- test/collective/collective_allgather_api.py | 22 +- .../collective_allgather_api_dygraph.py | 4 +- ...collective_allgather_object_api_dygraph.py | 4 +- test/collective/collective_allreduce_api.py | 8 +- .../collective_allreduce_api_dygraph.py | 4 +- .../collective_allreduce_new_group_api.py | 4 +- test/collective/collective_allreduce_op.py | 6 +- .../collective_allreduce_op_wait.py | 6 +- test/collective/collective_alltoall_api.py | 8 +- .../collective_alltoall_api_dygraph.py | 4 +- .../collective_alltoall_single_api_dygraph.py | 4 +- test/collective/collective_barrier_api.py | 4 +- test/collective/collective_broadcast_api.py | 8 +- .../collective_broadcast_api_dygraph.py | 4 +- ...ctive_broadcast_object_list_api_dygraph.py | 4 +- test/collective/collective_broadcast_op.py | 6 +- test/collective/collective_concat_api.py | 6 +- test/collective/collective_concat_op.py | 6 +- .../collective_gather_api_dygraph.py | 4 +- test/collective/collective_global_gather.py | 22 +- .../collective_global_gather_dygraph.py | 4 +- test/collective/collective_global_scatter.py | 18 +- .../collective_global_scatter_dygraph.py | 4 +- test/collective/collective_identity_op.py | 6 +- .../collective_isend_irecv_api_dygraph.py | 4 +- test/collective/collective_reduce_api.py | 8 +- .../collective_reduce_api_dygraph.py | 4 +- test/collective/collective_reduce_op.py | 6 +- .../collective_reduce_op_calc_stream.py | 6 +- .../collective_reduce_scatter_api.py | 4 +- .../collective_reduce_scatter_api_dygraph.py | 4 +- test/collective/collective_scatter_api.py | 4 +- .../collective_scatter_api_dygraph.py | 4 +- ...lective_scatter_object_list_api_dygraph.py | 4 +- test/collective/collective_scatter_op.py | 6 +- test/collective/collective_sendrecv_api.py | 10 +- .../collective_sendrecv_api_dygraph.py | 4 +- test/collective/collective_sendrecv_op.py | 4 +- .../collective_sendrecv_op_array.py | 4 +- .../collective_sendrecv_op_dynamic_shape.py | 4 +- test/collective/collective_split_op.py | 6 +- test/collective/column_parallel_linear_api.py | 8 +- .../fleet/auto_parallel_parallelizer.py | 2 +- test/collective/fleet/c_comm_init_op.py | 12 +- .../fleet/dist_mnist_gradient_merge.py | 8 +- ...dist_mnist_gradient_merge_raw_optimizer.py | 4 +- .../fleet/dygraph_group_sharded_stage3.py | 2 +- .../dygraph_group_sharded_stage3_offload.py | 2 +- .../fleet/fused_attention_pass_with_mp.py | 10 +- .../fleet/hybrid_parallel_inference_helper.py | 16 +- test/collective/fleet/hybrid_parallel_qat.py | 10 +- .../parallel_dygraph_control_flow_same.py | 2 +- .../fleet/parallel_dygraph_no_sync.py | 12 +- .../fleet/parallel_dygraph_se_resnext.py | 8 +- .../fleet/parallel_dygraph_sync_batch_norm.py | 2 +- .../fleet/parallel_dygraph_transformer.py | 12 +- test/collective/fleet/pipeline_mnist.py | 26 +- .../fleet/pipeline_mnist_multi_device.py | 26 +- .../fleet/pipeline_mnist_one_device.py | 22 +- .../fleet/static_model_parallel_by_col.py | 8 +- .../fleet/static_model_parallel_by_row.py | 8 +- .../fleet/static_model_parallel_embedding.py | 8 +- test/collective/fleet/test_auto_checkpoint.py | 4 +- .../fleet/test_auto_checkpoint_dist_basic.py | 6 +- .../fleet/test_auto_checkpoint_multiple.py | 2 +- .../fleet/test_communicator_half_async.py | 10 +- test/collective/fleet/test_dgc_momentum_op.py | 6 +- test/collective/fleet/test_dgc_op.py | 6 +- test/collective/fleet/test_dgc_optimizer.py | 2 +- .../fleet/test_dist_mnist_dgc_nccl.py | 8 +- .../fleet/test_dist_mnist_gradient_merge.py | 8 +- .../fleet/test_dist_se_resnext_dgc.py | 4 +- .../fleet/test_distributed_strategy.py | 10 +- test/collective/fleet/test_fleet_amp_init.py | 6 +- .../fleet/test_fleet_amp_meta_optimizer.py | 24 +- .../collective/fleet/test_fleet_checkpoint.py | 14 +- .../fleet/test_fleet_dgc_meta_optimizer.py | 20 +- .../fleet/test_fleet_distributed_strategy.py | 8 +- ...est_fleet_fp16_allreduce_meta_optimizer.py | 8 +- ...est_fleet_gradient_merge_meta_optimizer.py | 20 +- .../fleet/test_fleet_lamb_meta_optimizer.py | 18 +- .../fleet/test_fleet_lars_meta_optimizer.py | 18 +- .../test_fleet_localsgd_meta_optimizer.py | 10 +- .../fleet/test_fleet_meta_optimizer_base.py | 8 +- .../test_fleet_pipeline_meta_optimizer.py | 6 +- ..._pipeline_meta_optimizer_with_recompute.py | 4 +- .../fleet/test_fleet_qat_meta_optimizer.py | 22 +- .../test_fleet_recompute_meta_optimizer.py | 18 +- .../test_fleet_sharding_meta_optimizer.py | 96 ++-- .../test_fleet_tensor_parallel_extra_sync.py | 2 +- test/collective/fleet/test_hdfs1.py | 4 +- ...perative_auto_mixed_precision_for_eager.py | 122 ++-- .../test_parallel_dygraph_control_flow.py | 6 +- .../fleet/test_parallel_dygraph_mnist.py | 12 +- .../fleet/test_parallel_dygraph_no_sync.py | 14 +- .../fleet/test_parallel_dygraph_qat.py | 6 +- .../fleet/test_parallel_dygraph_se_resnext.py | 6 +- .../test_parallel_dygraph_sparse_embedding.py | 8 +- ...el_dygraph_sparse_embedding_over_height.py | 6 +- .../test_parallel_dygraph_sync_batch_norm.py | 4 +- .../test_parallel_dygraph_transformer.py | 6 +- .../test_parallel_dygraph_unused_variables.py | 10 +- test/collective/fleet/test_pipeline.py | 8 +- test/collective/fleet/test_recv_save_op.py | 34 +- .../fleet/test_static_model_parallel.py | 12 +- test/collective/fleet/test_tcp_store.py | 2 +- test/collective/parallel_embedding_api.py | 8 +- test/collective/process_group_gloo.py | 6 +- test/collective/process_group_mpi.py | 4 +- test/collective/row_parallel_linear_api.py | 8 +- .../test_collective_allreduce_api.py | 4 +- .../test_collective_cpu_barrier_with_gloo.py | 12 +- test/collective/test_collective_reduce_api.py | 6 +- .../test_collective_sendrecv_api.py | 4 +- test/collective/test_gen_nccl_id_op.py | 2 +- test/contrib/test_bf16_utils.py | 14 +- test/contrib/test_correlation.py | 16 +- test/contrib/test_fp16_utils.py | 8 +- .../contrib/test_image_classification_fp16.py | 36 +- .../test_multi_precision_fp16_train.py | 36 +- .../custom_kernel_dot_c_setup.py | 2 +- test/custom_kernel/custom_kernel_dot_setup.py | 2 +- .../custom_raw_op_kernel_op_setup.py | 2 +- test/custom_op/test_custom_concat.py | 4 +- test/custom_op/test_custom_conj.py | 4 +- test/custom_runtime/process_group_xccl.py | 24 +- test/custom_runtime/test_custom_op_setup.py | 2 +- .../auto_parallel_pass_test_base.py | 6 +- .../distributed_passes/dist_pass_test_base.py | 2 +- .../test_auto_parallel_gradient_merge_pass.py | 2 +- .../test_dist_inplace_addto_pass.py | 2 +- test/distribution/test_dirichlet_op.py | 2 +- .../test_distribution_bernoulli.py | 28 +- test/distribution/test_distribution_beta.py | 10 +- .../test_distribution_categorical.py | 26 +- test/distribution/test_distribution_cauchy.py | 32 +- .../test_distribution_dirichlet.py | 10 +- .../test_distribution_geometric.py | 16 +- .../test_distribution_lognormal.py | 4 +- test/distribution/test_distribution_normal.py | 32 +- .../distribution/test_distribution_uniform.py | 34 +- test/distribution/test_kl.py | 4 +- test/dygraph_to_static/bert_dygraph_model.py | 22 +- test/dygraph_to_static/darknet.py | 2 +- .../dygraph_to_static_util.py | 2 +- test/dygraph_to_static/ifelse_simple_func.py | 4 +- test/dygraph_to_static/predictor_utils.py | 8 +- .../seq2seq_dygraph_model.py | 14 +- .../dygraph_to_static/simnet_dygraph_model.py | 10 +- test/dygraph_to_static/test_assert.py | 8 +- test/dygraph_to_static/test_ast_util.py | 12 +- .../test_basic_api_transformation.py | 136 ++--- test/dygraph_to_static/test_bert.py | 22 +- test/dygraph_to_static/test_bmn.py | 26 +- test/dygraph_to_static/test_break_continue.py | 32 +- test/dygraph_to_static/test_build_strategy.py | 8 +- test/dygraph_to_static/test_cache_program.py | 23 +- test/dygraph_to_static/test_cast.py | 18 +- test/dygraph_to_static/test_cinn_prim.py | 2 +- test/dygraph_to_static/test_cinn_prim_gelu.py | 2 +- .../test_cinn_prim_layer_norm.py | 2 +- test/dygraph_to_static/test_cinn_prim_mean.py | 2 +- test/dygraph_to_static/test_convert_call.py | 24 +- test/dygraph_to_static/test_cycle_gan.py | 38 +- test/dygraph_to_static/test_declarative.py | 18 +- test/dygraph_to_static/test_dict.py | 16 +- .../test_duplicate_output.py | 8 +- test/dygraph_to_static/test_error.py | 14 +- test/dygraph_to_static/test_eval_frame.py | 4 +- test/dygraph_to_static/test_fetch_feed.py | 6 +- test/dygraph_to_static/test_for_enumerate.py | 42 +- .../dygraph_to_static/test_full_name_usage.py | 8 +- test/dygraph_to_static/test_ifelse.py | 26 +- test/dygraph_to_static/test_lac.py | 42 +- test/dygraph_to_static/test_lambda.py | 22 +- test/dygraph_to_static/test_len.py | 32 +- test/dygraph_to_static/test_list.py | 38 +- test/dygraph_to_static/test_logical.py | 6 +- test/dygraph_to_static/test_loop.py | 36 +- test/dygraph_to_static/test_mnist.py | 28 +- test/dygraph_to_static/test_mnist_amp.py | 4 +- .../dygraph_to_static/test_mnist_pure_fp16.py | 6 +- test/dygraph_to_static/test_mobile_net.py | 22 +- .../dygraph_to_static/test_partial_program.py | 22 +- .../test_partial_program_hook.py | 2 +- test/dygraph_to_static/test_print.py | 4 +- .../test_program_translator.py | 18 +- test/dygraph_to_static/test_ptb_lm.py | 22 +- .../test_reinforcement_learning.py | 12 +- test/dygraph_to_static/test_resnet.py | 30 +- test/dygraph_to_static/test_resnet_amp.py | 14 +- .../test_resnet_pure_fp16.py | 12 +- test/dygraph_to_static/test_resnet_v2.py | 12 +- test/dygraph_to_static/test_return.py | 44 +- .../test_save_inference_model.py | 22 +- test/dygraph_to_static/test_save_load.py | 20 +- test/dygraph_to_static/test_se_resnet.py | 30 +- test/dygraph_to_static/test_sentiment.py | 20 +- test/dygraph_to_static/test_seq2seq.py | 18 +- test/dygraph_to_static/test_simnet.py | 14 +- test/dygraph_to_static/test_simnet_v2.py | 2 +- .../dygraph_to_static/test_static_analysis.py | 8 +- .../test_tensor_memcpy_on_cpu.py | 10 +- .../test_tensor_memcpy_on_gpu.py | 12 +- test/dygraph_to_static/test_tensor_shape.py | 46 +- test/dygraph_to_static/test_to_tensor.py | 4 +- test/dygraph_to_static/test_transformer.py | 26 +- test/dygraph_to_static/test_tsm.py | 20 +- test/dygraph_to_static/test_typehint.py | 12 +- .../dygraph_to_static/test_unuseful_inputs.py | 2 +- test/dygraph_to_static/test_word2vec.py | 22 +- test/dygraph_to_static/test_yolov3.py | 12 +- .../transformer_dygraph_model.py | 22 +- test/dygraph_to_static/yolov3.py | 12 +- test/fft/test_fft.py | 70 +-- test/indexing/test_getitem.py | 2 +- test/indexing/test_setitem.py | 2 +- .../custom_ops/test_custom_leaky_relu_ipu.py | 4 +- .../distributed/test_dist_pod128_sample.py | 4 +- test/ipu/distributed/test_dist_sample.py | 4 +- test/ipu/op_test_ipu.py | 6 +- test/ipu/test_dy2static_fp16_ipu.py | 2 +- test/ipu/test_dy2static_ipu.py | 2 +- test/ipu/test_greater_op_ipu.py | 10 +- test/ipu/test_identity_loss_ipu.py | 12 +- test/ipu/test_inference_model_io_ipu.py | 4 +- test/ipu/test_one_hot_v2_op_ipu.py | 2 +- test/ipu/test_save_load_ipu.py | 4 +- test/ipu/test_weight_sharing_ipu.py | 4 +- test/ir/inference/auto_scan_test.py | 2 +- test/ir/inference/inference_pass_test.py | 24 +- test/ir/inference/program_config.py | 18 +- test/ir/inference/quant_dequant_test.py | 50 +- test/ir/inference/test_inplace_op_pass.py | 2 +- .../test_mkldnn_cpu_bfloat16_pass.py | 6 +- .../test_mkldnn_elt_act_fuse_pass.py | 6 +- .../test_mkldnn_matmul_op_output_fuse_pass.py | 8 +- ...n_reshape_transpose_matmul_v2_fuse_pass.py | 6 +- .../test_trt_c_allreduce_infer_script.py | 2 +- test/ir/inference/test_trt_conv3d_op.py | 10 +- .../inference/test_trt_conv3d_transpose_op.py | 10 +- test/ir/inference/test_trt_conv_pass.py | 12 +- .../test_trt_conv_quant_dequant_pass.py | 30 +- .../test_trt_convert_preln_residual_bias.py | 2 +- ...test_trt_convert_preln_residual_no_bias.py | 2 +- test/ir/inference/test_trt_deformable_conv.py | 8 +- test/ir/inference/test_trt_dynamic_shape.py | 8 +- test/ir/inference/test_trt_elementwise_op.py | 8 +- test/ir/inference/test_trt_fc_fuse_pass.py | 24 +- .../test_trt_fc_fuse_quant_dequant_pass.py | 30 +- test/ir/inference/test_trt_flatten_op.py | 10 +- test/ir/inference/test_trt_gather_nd_op.py | 10 +- test/ir/inference/test_trt_gather_op.py | 10 +- test/ir/inference/test_trt_inspector.py | 8 +- .../ir/inference/test_trt_instance_norm_op.py | 8 +- test/ir/inference/test_trt_matmul.py | 14 +- .../test_trt_matmul_quant_dequant.py | 30 +- .../inference/test_trt_multiclass_nms3_op.py | 10 +- .../inference/test_trt_nearest_interp_op.py | 8 +- .../test_trt_nearest_interp_v2_op.py | 8 +- test/ir/inference/test_trt_pad_op.py | 8 +- test/ir/inference/test_trt_pool3d_op.py | 12 +- test/ir/inference/test_trt_pool_op.py | 8 +- test/ir/inference/test_trt_reduce_sum_op.py | 10 +- test/ir/inference/test_trt_reshape_op.py | 14 +- test/ir/inference/test_trt_scale_op.py | 10 +- .../test_trt_shuffle_channel_detect_pass.py | 6 +- .../test_trt_skip_layernorm_fuse_pass.py | 14 +- .../test_trt_slice_dynamic_plugin.py | 8 +- test/ir/inference/test_trt_slice_plugin.py | 12 +- test/ir/inference/test_trt_subgraph_pass.py | 30 +- test/ir/inference/test_trt_tile_op.py | 14 +- ..._trt_transpose_flatten_concat_fuse_pass.py | 8 +- .../inference/test_trt_tuned_dynamic_shape.py | 12 +- test/ir/inference/test_trt_yolo_box_op.py | 10 +- test/ir/inference/test_yolo_box_post.py | 2 +- test/ir/new_ir/test_data_op.py | 2 +- test/ir/new_ir/test_ir_pybind.py | 6 +- test/ir/new_ir/test_ir_vjp.py | 2 +- test/ir/new_ir/test_pass_manager.py | 2 +- test/ir/new_ir/test_special_op_translator.py | 2 +- test/ir/pass_test.py | 28 +- test/ir/test_fuse_resnet_unit.py | 4 +- ...r_embedding_eltwise_layernorm_fuse_pass.py | 8 +- test/ir/test_ir_fc_fuse_pass.py | 8 +- test/ir/test_ir_fusion_group_pass.py | 28 +- test/ir/test_ir_generate_pass.py | 10 +- test/ir/test_ir_graph_to_program_pass.py | 8 +- test/ir/test_ir_skip_layernorm_pass.py | 8 +- test/ir/test_ir_subgraph_python_interface.py | 10 +- test/ir/test_ir_yolo_box_pass.py | 6 +- test/legacy_test/auto_checkpoint_utils.py | 24 +- test/legacy_test/auto_parallel_gpt_model.py | 2 +- test/legacy_test/check_nan_inf_base.py | 18 +- test/legacy_test/collective_allgather_op.py | 6 +- .../collective_reducescatter_op.py | 6 +- test/legacy_test/decorator_helper.py | 12 +- test/legacy_test/detected_gpu.py | 11 +- test/legacy_test/detected_xpu.py | 8 +- test/legacy_test/dist_allreduce_op.py | 14 +- test/legacy_test/dist_ctr.py | 14 +- test/legacy_test/dist_ctr_reader.py | 2 +- test/legacy_test/dist_fleet_ctr.py | 40 +- test/legacy_test/dist_fleet_ctr_ps_gpu.py | 18 +- test/legacy_test/dist_fleet_debug_gloo.py | 2 +- .../dist_fleet_heter_pipeline_ctr.py | 40 +- .../dist_fleet_raw_program_optimizer.py | 14 +- ...et_raw_program_optimizer_fuse_allreduce.py | 14 +- test/legacy_test/dist_fleet_simnet_bow.py | 34 +- .../dist_fleet_sparse_embedding_ctr.py | 18 +- .../legacy_test/dist_fleet_sync_batch_norm.py | 18 +- test/legacy_test/dist_hapi_mnist_dynamic.py | 4 +- test/legacy_test/dist_hapi_mnist_static.py | 4 +- .../legacy_test/dist_hapi_pure_fp16_static.py | 6 +- test/legacy_test/dist_mnist.py | 14 +- test/legacy_test/dist_mnist_batch_merge.py | 4 +- test/legacy_test/dist_mnist_dgc.py | 14 +- test/legacy_test/dist_mnist_fp16_allreduce.py | 8 +- test/legacy_test/dist_mnist_lars.py | 8 +- test/legacy_test/dist_se_resnext.py | 16 +- test/legacy_test/dist_sharding_save.py | 18 +- test/legacy_test/dist_text_classification.py | 12 +- test/legacy_test/dist_word2vec.py | 20 +- .../distributed_fused_lamb_test_base.py | 4 +- test/legacy_test/eager_op_test.py | 102 ++-- test/legacy_test/feed_data_reader.py | 6 +- test/legacy_test/fleet_heter_ps_training.py | 24 +- test/legacy_test/fleet_meta_optimizer_base.py | 22 +- test/legacy_test/fleet_ps_training.py | 8 +- test/legacy_test/gradient_checker.py | 106 ++-- .../ir_memory_optimize_net_base.py | 24 +- test/legacy_test/jit_load_rename_var.py | 4 +- test/legacy_test/nets.py | 36 +- test/legacy_test/nproc_process.py | 4 +- test/legacy_test/op.py | 4 +- test/legacy_test/parallel_dygraph_mnist.py | 2 +- .../parallel_dygraph_shared_unused_var.py | 2 +- .../parallel_dygraph_sparse_embedding.py | 10 +- .../parallel_executor_test_base.py | 44 +- test/legacy_test/prim_op_test.py | 48 +- test/legacy_test/seresnext_net.py | 6 +- test/legacy_test/seresnext_test_base.py | 2 +- test/legacy_test/simple_nets.py | 8 +- .../static_model_parallel_fused_attention.py | 4 +- ...static_model_parallel_fused_feedforward.py | 4 +- ..._model_parallel_fused_multi_transformer.py | 4 +- test/legacy_test/test_accuracy_op.py | 12 +- test/legacy_test/test_activation_nn_grad.py | 100 ++-- test/legacy_test/test_activation_op.py | 366 ++++++------ test/legacy_test/test_activation_sparse_op.py | 2 +- test/legacy_test/test_adadelta_op.py | 14 +- test/legacy_test/test_adagrad_op.py | 2 +- test/legacy_test/test_adam_op.py | 20 +- .../test_adam_optimizer_fp32_fp64.py | 22 +- test/legacy_test/test_adamax_api.py | 14 +- test/legacy_test/test_adamw_op.py | 26 +- test/legacy_test/test_adaptive_avg_pool1d.py | 18 +- test/legacy_test/test_adaptive_avg_pool2d.py | 8 +- test/legacy_test/test_adaptive_avg_pool3d.py | 8 +- test/legacy_test/test_adaptive_max_pool1d.py | 18 +- test/legacy_test/test_adaptive_max_pool2d.py | 8 +- test/legacy_test/test_adaptive_max_pool3d.py | 8 +- .../test_add_position_encoding_op.py | 2 +- .../legacy_test/test_add_reader_dependency.py | 24 +- test/legacy_test/test_addmm_op.py | 24 +- test/legacy_test/test_affine_grid_function.py | 38 +- test/legacy_test/test_affine_grid_op.py | 2 +- test/legacy_test/test_allclose_layer.py | 46 +- test/legacy_test/test_allclose_op.py | 2 +- test/legacy_test/test_angle_op.py | 2 +- .../legacy_test/test_apply_pass_to_program.py | 6 +- test/legacy_test/test_arange.py | 2 +- test/legacy_test/test_arg_min_max_op.py | 2 +- test/legacy_test/test_arg_min_max_v2_op.py | 6 +- test/legacy_test/test_argsort_op.py | 26 +- test/legacy_test/test_array_read_write_op.py | 18 +- test/legacy_test/test_assert_op.py | 10 +- test/legacy_test/test_assign_op.py | 36 +- test/legacy_test/test_assign_pos_op.py | 2 +- test/legacy_test/test_assign_value_op.py | 24 +- test/legacy_test/test_async_read_write.py | 2 +- test/legacy_test/test_atan2_op.py | 2 +- test/legacy_test/test_attribute_var.py | 2 +- test/legacy_test/test_auc_op.py | 4 +- .../test_auto_growth_allocator_gpu.py | 16 +- .../test_auto_parallel_cost_model.py | 2 +- test/legacy_test/test_auto_parallel_mapper.py | 10 +- test/legacy_test/test_auto_search_dist_op.py | 2 +- .../test_avoid_twice_initialization.py | 4 +- test/legacy_test/test_backward.py | 96 ++-- ...test_backward_infer_var_data_type_shape.py | 6 +- test/legacy_test/test_base_layer.py | 84 ++- test/legacy_test/test_batch_fc_op.py | 2 +- test/legacy_test/test_batch_norm_op.py | 48 +- .../test_batch_norm_op_prim_nchw.py | 2 +- .../test_batch_norm_op_prim_nhwc.py | 2 +- test/legacy_test/test_batch_norm_op_v2.py | 78 +-- test/legacy_test/test_bce_loss.py | 16 +- test/legacy_test/test_bce_with_logits_loss.py | 22 +- .../legacy_test/test_beam_search_decode_op.py | 2 +- test/legacy_test/test_beam_search_op.py | 2 +- test/legacy_test/test_bernoulli_op.py | 2 +- test/legacy_test/test_bicubic_interp_op.py | 38 +- test/legacy_test/test_bicubic_interp_v2_op.py | 40 +- test/legacy_test/test_bilateral_slice_op.py | 12 +- test/legacy_test/test_bilinear_api.py | 12 +- test/legacy_test/test_bilinear_interp_op.py | 2 +- .../legacy_test/test_bilinear_interp_v2_op.py | 22 +- .../test_bilinear_tensor_product_op.py | 10 +- test/legacy_test/test_bincount_op.py | 26 +- test/legacy_test/test_bmm_op.py | 16 +- test/legacy_test/test_boxps.py | 20 +- test/legacy_test/test_broadcast_error.py | 2 +- test/legacy_test/test_broadcast_tensors_op.py | 2 +- test/legacy_test/test_broadcast_to_op.py | 16 +- test/legacy_test/test_bucketize_api.py | 2 +- .../test_buffer_shared_memory_reuse_pass.py | 36 +- .../test_build_strategy_fusion_group_pass.py | 8 +- test/legacy_test/test_c_comm_init_all_op.py | 12 +- test/legacy_test/test_calc_gradient.py | 56 +- test/legacy_test/test_case.py | 50 +- test/legacy_test/test_cast_op.py | 18 +- test/legacy_test/test_channel_shuffle.py | 28 +- test/legacy_test/test_checkpoint_saver.py | 2 +- test/legacy_test/test_cholesky_op.py | 28 +- test/legacy_test/test_cholesky_solve_op.py | 18 +- test/legacy_test/test_chunk_op.py | 22 +- .../test_class_center_sample_op.py | 26 +- test/legacy_test/test_clip_by_norm_op.py | 2 +- test/legacy_test/test_clip_op.py | 22 +- test/legacy_test/test_coalesce_tensor_op.py | 14 +- test/legacy_test/test_collective_api_base.py | 18 +- test/legacy_test/test_collective_base.py | 16 +- test/legacy_test/test_communicator_geo.py | 14 +- test/legacy_test/test_communicator_ps_gpu.py | 4 +- test/legacy_test/test_compare_op.py | 32 +- test/legacy_test/test_compiled_program.py | 38 +- test/legacy_test/test_complex_abs.py | 8 +- .../test_complex_elementwise_layers.py | 6 +- test/legacy_test/test_complex_getitem.py | 10 +- .../test_complex_grad_accumulated.py | 2 +- test/legacy_test/test_complex_kron.py | 6 +- test/legacy_test/test_complex_matmul.py | 10 +- test/legacy_test/test_complex_op.py | 2 +- test/legacy_test/test_complex_reshape.py | 6 +- test/legacy_test/test_complex_simplenet.py | 2 +- test/legacy_test/test_complex_sum_layer.py | 6 +- test/legacy_test/test_complex_trace_layer.py | 10 +- test/legacy_test/test_complex_transpose.py | 6 +- test/legacy_test/test_complex_variable.py | 8 +- test/legacy_test/test_complex_view_op.py | 2 +- test/legacy_test/test_concat_op.py | 60 +- test/legacy_test/test_cond.py | 94 +-- test/legacy_test/test_conditional_block.py | 16 +- test/legacy_test/test_conj_op.py | 6 +- test/legacy_test/test_const_value.py | 2 +- test/legacy_test/test_context_manager.py | 10 +- test/legacy_test/test_conv1d_layer.py | 24 +- .../test_conv1d_transpose_layer.py | 22 +- test/legacy_test/test_conv2d_api.py | 20 +- test/legacy_test/test_conv2d_fusion_op.py | 10 +- test/legacy_test/test_conv2d_layer.py | 38 +- test/legacy_test/test_conv2d_op.py | 20 +- .../test_conv2d_op_depthwise_conv.py | 2 +- .../test_conv2d_transpose_layer.py | 38 +- test/legacy_test/test_conv2d_transpose_op.py | 10 +- test/legacy_test/test_conv3d_layer.py | 38 +- test/legacy_test/test_conv3d_op.py | 14 +- .../test_conv3d_transpose_layer.py | 38 +- test/legacy_test/test_conv3d_transpose_op.py | 2 +- .../test_conv3d_transpose_part2_op.py | 10 +- test/legacy_test/test_conv_nn_grad.py | 138 ++--- .../test_conv_transpose_nn_grad.py | 16 +- test/legacy_test/test_corr.py | 14 +- .../legacy_test/test_cosine_similarity_api.py | 2 +- test/legacy_test/test_cost_model.py | 2 +- test/legacy_test/test_count_nonzero_api.py | 2 +- test/legacy_test/test_cov.py | 38 +- test/legacy_test/test_create_global_var.py | 2 +- test/legacy_test/test_create_parameter.py | 2 +- test/legacy_test/test_cross_entropy_loss.py | 536 +++++++++--------- test/legacy_test/test_cross_entropy_op.py | 12 +- test/legacy_test/test_cross_op.py | 20 +- test/legacy_test/test_crypto.py | 2 +- .../test_cuda_graph_static_mode.py | 2 +- .../test_cuda_graph_static_mode_error.py | 2 +- .../test_cuda_max_memory_allocated.py | 2 +- .../test_cuda_max_memory_reserved.py | 2 +- .../legacy_test/test_cuda_memory_allocated.py | 2 +- test/legacy_test/test_cuda_memory_reserved.py | 2 +- test/legacy_test/test_cuda_random_seed.py | 24 +- test/legacy_test/test_cummax_op.py | 20 +- test/legacy_test/test_cummin_op.py | 20 +- test/legacy_test/test_cumprod_op.py | 2 +- test/legacy_test/test_cumsum_op.py | 22 +- test/legacy_test/test_custom_grad_input.py | 2 +- test/legacy_test/test_data.py | 2 +- test/legacy_test/test_data_feeder.py | 8 +- test/legacy_test/test_data_norm_op.py | 26 +- .../test_dataloader_early_reset.py | 20 +- .../legacy_test/test_dataloader_keep_order.py | 24 +- .../test_dataloader_unkeep_order.py | 26 +- test/legacy_test/test_dataset.py | 204 ++++--- .../test_dataset_consistency_inspection.py | 6 +- test/legacy_test/test_dataset_dataloader.py | 36 +- test/legacy_test/test_debugger.py | 4 +- test/legacy_test/test_decoupled_py_reader.py | 32 +- .../test_decoupled_py_reader_data_check.py | 22 +- test/legacy_test/test_default_scope_funcs.py | 2 +- test/legacy_test/test_deformable_conv_op.py | 8 +- .../legacy_test/test_deformable_conv_v1_op.py | 6 +- test/legacy_test/test_deg2rad.py | 18 +- test/legacy_test/test_deprecated_decorator.py | 4 +- ...t_deprecated_memory_optimize_interfaces.py | 10 +- test/legacy_test/test_desc_clone.py | 32 +- test/legacy_test/test_detach.py | 18 +- test/legacy_test/test_detection.py | 50 +- test/legacy_test/test_device.py | 12 +- test/legacy_test/test_device_guard.py | 2 +- test/legacy_test/test_diag.py | 2 +- test/legacy_test/test_diag_embed.py | 8 +- test/legacy_test/test_diag_v2.py | 20 +- test/legacy_test/test_diagflat.py | 2 +- test/legacy_test/test_diagonal_op.py | 2 +- test/legacy_test/test_diff_op.py | 16 +- test/legacy_test/test_digamma_op.py | 8 +- test/legacy_test/test_dist_allreduce_op.py | 4 +- test/legacy_test/test_dist_base.py | 120 ++-- .../test_dist_fleet_a_sync_optimizer_async.py | 18 +- .../test_dist_fleet_a_sync_optimizer_auto.py | 8 +- ..._dist_fleet_a_sync_optimizer_auto_async.py | 10 +- ...st_dist_fleet_a_sync_optimizer_auto_geo.py | 8 +- .../test_dist_fleet_a_sync_optimizer_geo.py | 18 +- .../test_dist_fleet_a_sync_optimizer_sync.py | 2 +- test/legacy_test/test_dist_fleet_base.py | 12 +- .../test_dist_fleet_heter_program.py | 22 +- test/legacy_test/test_dist_fleet_minimize.py | 18 +- test/legacy_test/test_dist_fleet_ps.py | 18 +- test/legacy_test/test_dist_fleet_ps11.py | 18 +- test/legacy_test/test_dist_fleet_ps12.py | 18 +- test/legacy_test/test_dist_fleet_ps13.py | 18 +- test/legacy_test/test_dist_fleet_ps2.py | 18 +- test/legacy_test/test_dist_fleet_ps3.py | 18 +- test/legacy_test/test_dist_fleet_ps4.py | 18 +- test/legacy_test/test_dist_fleet_ps5.py | 18 +- test/legacy_test/test_dist_fleet_ps6.py | 18 +- .../test_dist_fleet_raw_program_optimizer.py | 8 +- ...et_raw_program_optimizer_fuse_allreduce.py | 4 +- .../test_dist_fleet_sparse_embedding_ctr.py | 16 +- test/legacy_test/test_dist_fleet_spmt.py | 26 +- test/legacy_test/test_dist_hapi_model.py | 4 +- .../test_dist_lookup_sparse_table_fuse_ops.py | 26 +- .../test_dist_mnist_backward_deps.py | 4 +- .../legacy_test/test_dist_mnist_fleet_save.py | 40 +- test/legacy_test/test_dist_mnist_fleetapi.py | 8 +- .../test_dist_mnist_fp16_allreduce.py | 4 +- .../legacy_test/test_dist_mnist_hallreduce.py | 4 +- .../legacy_test/test_dist_mnist_multi_comm.py | 4 +- test/legacy_test/test_dist_mnist_pg.py | 4 +- .../test_dist_mnist_ring_allreduce.py | 4 +- .../test_dist_mnist_with_program.py | 8 +- test/legacy_test/test_dist_op.py | 18 +- test/legacy_test/test_dist_se_resnext_nccl.py | 8 +- test/legacy_test/test_dist_sharding_save.py | 4 +- test/legacy_test/test_dist_sparse_load_ps0.py | 22 +- test/legacy_test/test_dist_sparse_load_ps1.py | 8 +- .../test_dist_sparse_tensor_load_adagrad.py | 6 +- .../test_dist_sparse_tensor_load_adam.py | 6 +- .../test_dist_sparse_tensor_load_ftrl.py | 6 +- .../test_dist_sparse_tensor_load_momentum.py | 6 +- .../test_dist_sparse_tensor_load_rmsprop.py | 6 +- .../test_dist_sparse_tensor_load_sgd.py | 18 +- test/legacy_test/test_dist_train.py | 26 +- test/legacy_test/test_dist_transpiler.py | 80 +-- test/legacy_test/test_dlpack.py | 18 +- test/legacy_test/test_dot_op.py | 22 +- test/legacy_test/test_downpoursgd.py | 22 +- test/legacy_test/test_dropout_nd_op.py | 16 +- test/legacy_test/test_dropout_op.py | 152 ++--- test/legacy_test/test_dygraph_mnist_fp16.py | 10 +- .../legacy_test/test_dygraph_multi_forward.py | 18 +- test/legacy_test/test_dygraph_weight_norm.py | 8 +- .../test_dynamic_rnn_stop_gradient.py | 18 +- .../test_eager_deletion_conditional_block.py | 4 +- .../test_eager_deletion_delete_vars.py | 42 +- .../test_eager_deletion_dynamic_rnn_base.py | 22 +- test/legacy_test/test_eager_deletion_mnist.py | 4 +- .../test_eager_deletion_padding_rnn.py | 20 +- .../test_eager_deletion_recurrent_op.py | 4 +- .../test_eager_deletion_while_op.py | 18 +- test/legacy_test/test_eager_run_program.py | 10 +- test/legacy_test/test_eager_trace_op.py | 6 +- test/legacy_test/test_egr_python_api.py | 40 +- .../legacy_test/test_egr_string_tensor_api.py | 2 +- test/legacy_test/test_eig_op.py | 16 +- test/legacy_test/test_eigvals_op.py | 2 +- test/legacy_test/test_einsum.py | 28 +- test/legacy_test/test_einsum_op.py | 2 +- test/legacy_test/test_einsum_v2.py | 28 +- test/legacy_test/test_elementwise_add_op.py | 40 +- test/legacy_test/test_elementwise_div_op.py | 16 +- .../test_elementwise_floordiv_op.py | 14 +- .../test_elementwise_floormod_op.py | 4 +- .../test_elementwise_gradient_op.py | 10 +- .../test_elementwise_heaviside_op.py | 2 +- test/legacy_test/test_elementwise_max_op.py | 2 +- test/legacy_test/test_elementwise_min_op.py | 8 +- test/legacy_test/test_elementwise_mod_op.py | 20 +- test/legacy_test/test_elementwise_mul_op.py | 30 +- test/legacy_test/test_elementwise_nn_grad.py | 52 +- test/legacy_test/test_elementwise_pow_op.py | 16 +- test/legacy_test/test_elementwise_sub_op.py | 24 +- test/legacy_test/test_ema.py | 26 +- .../test_embedding_id_stop_gradient.py | 20 +- test/legacy_test/test_empty_like_op.py | 8 +- test/legacy_test/test_empty_op.py | 8 +- test/legacy_test/test_entry_attr.py | 12 +- test/legacy_test/test_entry_attr2.py | 12 +- test/legacy_test/test_erf_op.py | 26 +- test/legacy_test/test_erfinv_op.py | 2 +- test/legacy_test/test_error_clip.py | 18 +- test/legacy_test/test_exception.py | 20 +- test/legacy_test/test_executor_and_mul.py | 2 +- .../test_executor_and_use_program_cache.py | 14 +- test/legacy_test/test_executor_check_feed.py | 32 +- .../test_executor_feed_non_tensor.py | 116 ++-- ..._executor_return_tensor_not_overwriting.py | 30 +- test/legacy_test/test_expand_as_v2_op.py | 10 +- test/legacy_test/test_expand_op.py | 8 +- test/legacy_test/test_expand_v2_op.py | 24 +- test/legacy_test/test_exponential_op.py | 2 +- test/legacy_test/test_eye_op.py | 8 +- test/legacy_test/test_fake_dequantize_op.py | 6 +- test/legacy_test/test_fake_init_op.py | 2 +- test/legacy_test/test_fc_op.py | 10 +- .../test_feed_data_check_shape_type.py | 22 +- test/legacy_test/test_feed_fetch_method.py | 2 +- test/legacy_test/test_fetch_handler.py | 16 +- .../test_fetch_lod_tensor_array.py | 20 +- test/legacy_test/test_fetch_var.py | 8 +- test/legacy_test/test_fill_any_like_op.py | 2 +- test/legacy_test/test_fill_any_op.py | 6 +- test/legacy_test/test_fill_constant_op.py | 22 +- .../test_fill_diagonal_tensor_op.py | 2 +- test/legacy_test/test_fill_op.py | 2 +- test/legacy_test/test_fill_zeros_like2_op.py | 2 +- test/legacy_test/test_flash_attention.py | 8 +- .../test_flatten_contiguous_range_op.py | 2 +- test/legacy_test/test_flatten_op.py | 2 +- test/legacy_test/test_fleet.py | 16 +- test/legacy_test/test_fleet_base.py | 8 +- test/legacy_test/test_fleet_base_2.py | 10 +- test/legacy_test/test_fleet_base_single.py | 16 +- .../test_fleet_exe_dist_model_run.py | 2 +- .../test_fleet_exe_dist_model_tensor.py | 2 +- test/legacy_test/test_fleet_executor.py | 8 +- .../test_fleet_executor_cond_interceptor.py | 4 +- .../test_fleet_executor_multi_devices.py | 6 +- .../test_fleet_executor_origin_scheduler.py | 8 +- .../test_fleet_executor_task_node.py | 2 +- .../test_fleet_executor_with_task_nodes.py | 8 +- test/legacy_test/test_fleet_gradient_scale.py | 6 +- test/legacy_test/test_fleet_metric.py | 18 +- test/legacy_test/test_fleet_nocvm_1.py | 16 +- test/legacy_test/test_fleet_ps.py | 2 +- test/legacy_test/test_fleet_pyramid_hash.py | 6 +- test/legacy_test/test_fleet_rolemaker.py | 14 +- test/legacy_test/test_fleet_rolemaker_2.py | 14 +- test/legacy_test/test_fleet_rolemaker_3.py | 14 +- test/legacy_test/test_fleet_unitaccessor.py | 16 +- test/legacy_test/test_flip.py | 30 +- test/legacy_test/test_fmax_op.py | 2 +- test/legacy_test/test_fmin_op.py | 2 +- test/legacy_test/test_fold_op.py | 14 +- test/legacy_test/test_frac_api.py | 12 +- test/legacy_test/test_frame_op.py | 2 +- test/legacy_test/test_framework_debug_str.py | 2 +- test/legacy_test/test_frexp_api.py | 2 +- test/legacy_test/test_ftrl_op.py | 2 +- test/legacy_test/test_full_like_op.py | 6 +- test/legacy_test/test_full_op.py | 10 +- test/legacy_test/test_functional_conv1d.py | 2 +- .../test_functional_conv1d_transpose.py | 2 +- test/legacy_test/test_functional_conv2d.py | 48 +- .../test_functional_conv2d_transpose.py | 48 +- test/legacy_test/test_functional_conv3d.py | 48 +- .../test_functional_conv3d_transpose.py | 48 +- test/legacy_test/test_fuse_all_reduce_pass.py | 8 +- test/legacy_test/test_fuse_bn_act_pass.py | 38 +- test/legacy_test/test_fuse_bn_add_act_pass.py | 58 +- .../test_fuse_elewise_add_act_pass.py | 28 +- .../test_fuse_gemm_epilogue_pass.py | 2 +- test/legacy_test/test_fuse_optimizer_pass.py | 8 +- .../test_fuse_relu_depthwise_conv_pass.py | 2 +- .../test_fused_attention_no_dropout.py | 2 +- test/legacy_test/test_fused_attention_op.py | 2 +- test/legacy_test/test_fused_attention_pass.py | 2 +- test/legacy_test/test_fused_bias_act_op.py | 2 +- ...sed_bias_dropout_residual_layer_norm_op.py | 2 +- test/legacy_test/test_fused_dropout_add_op.py | 6 +- test/legacy_test/test_fused_ec_moe_op.py | 2 +- .../test_fused_elemwise_activation_op.py | 6 +- .../legacy_test/test_fused_emb_seq_pool_op.py | 10 +- .../test_fused_fc_elementwise_layernorm_op.py | 2 +- test/legacy_test/test_fused_feedforward_op.py | 2 +- .../test_fused_feedforward_pass.py | 2 +- .../test_fused_gate_attention_op.py | 2 +- .../test_fused_gemm_epilogue_grad_op.py | 2 +- .../test_fused_gemm_epilogue_op.py | 2 +- test/legacy_test/test_fused_layernorm_op.py | 14 +- test/legacy_test/test_fused_matmul_bias.py | 2 +- .../test_fused_multi_transformer_int8_op.py | 4 +- .../test_fused_multi_transformer_op.py | 8 +- .../test_fused_multihead_matmul_op.py | 2 +- .../test_fused_rotary_position_embedding.py | 2 +- ...t_fused_scale_bias_relu_conv_bnstats_op.py | 2 +- .../test_fused_transformer_encoder_layer.py | 2 +- ...test_fusion_transpose_flatten_concat_op.py | 2 +- test/legacy_test/test_gather_nd_op.py | 16 +- test/legacy_test/test_gather_op.py | 14 +- test/legacy_test/test_gather_tree_op.py | 2 +- test/legacy_test/test_gaussian_nll_loss.py | 2 +- test/legacy_test/test_gaussian_random_op.py | 24 +- test/legacy_test/test_gcd.py | 18 +- test/legacy_test/test_gelu_op.py | 10 +- test/legacy_test/test_generator_dataloader.py | 34 +- .../test_get_all_op_or_kernel_names.py | 9 +- .../legacy_test/test_get_device_properties.py | 2 +- test/legacy_test/test_get_set_flags.py | 16 +- .../test_get_tensor_from_selected_rows_op.py | 2 +- .../test_global_var_getter_setter.py | 4 +- test/legacy_test/test_glu.py | 10 +- .../test_gpu_package_without_gpu_device.py | 2 +- test/legacy_test/test_grad_clip_minimize.py | 10 +- test/legacy_test/test_gradient_clip.py | 88 +-- test/legacy_test/test_graph_khop_sampler.py | 14 +- .../test_graph_sample_neighbors.py | 6 +- .../legacy_test/test_graph_send_ue_recv_op.py | 2 +- test/legacy_test/test_greater_equal_op.py | 2 +- test/legacy_test/test_grid_sample_function.py | 22 +- test/legacy_test/test_grid_sampler_op.py | 2 +- test/legacy_test/test_group_norm_op.py | 48 +- test/legacy_test/test_group_norm_op_v2.py | 12 +- test/legacy_test/test_gru_rnn_op.py | 2 +- test/legacy_test/test_gru_unit_op.py | 8 +- test/legacy_test/test_gumbel_softmax_op.py | 14 +- test/legacy_test/test_hapi_amp.py | 10 +- test/legacy_test/test_histogram_op.py | 30 +- test/legacy_test/test_host_memory_stats.py | 2 +- test/legacy_test/test_hsigmoid_op.py | 26 +- test/legacy_test/test_huber_loss_op.py | 2 +- test/legacy_test/test_i0_op.py | 2 +- test/legacy_test/test_i0e_op.py | 2 +- test/legacy_test/test_i1_op.py | 2 +- test/legacy_test/test_i1e_op.py | 2 +- test/legacy_test/test_identity_loss_op.py | 6 +- .../test_image_classification_layer.py | 12 +- .../legacy_test/test_imperative_auto_prune.py | 102 ++-- test/legacy_test/test_imperative_base.py | 14 +- .../test_imperative_container_layerlist.py | 8 +- ...test_imperative_container_parameterlist.py | 12 +- .../test_imperative_container_sequential.py | 10 +- .../test_imperative_data_loader_base.py | 26 +- .../test_imperative_data_loader_exception.py | 26 +- .../test_imperative_data_loader_exit_func.py | 2 +- .../test_imperative_data_loader_fds_clear.py | 14 +- .../test_imperative_data_loader_process.py | 16 +- .../test_imperative_data_parallel.py | 10 +- test/legacy_test/test_imperative_decorator.py | 12 +- test/legacy_test/test_imperative_deepcf.py | 32 +- .../test_imperative_double_grad.py | 42 +- test/legacy_test/test_imperative_framework.py | 6 +- test/legacy_test/test_imperative_gan.py | 28 +- test/legacy_test/test_imperative_gnn.py | 22 +- test/legacy_test/test_imperative_group.py | 4 +- .../test_imperative_hook_for_layer.py | 40 +- .../test_imperative_layer_apply.py | 4 +- .../test_imperative_layer_children.py | 4 +- .../test_imperative_layer_trainable.py | 6 +- .../test_imperative_load_static_param.py | 16 +- ..._imperative_lod_tensor_to_selected_rows.py | 24 +- test/legacy_test/test_imperative_mnist.py | 28 +- .../test_imperative_mnist_sorted_gradient.py | 28 +- .../test_imperative_named_members.py | 12 +- .../test_imperative_numpy_bridge.py | 10 +- .../test_imperative_ocr_attention_model.py | 36 +- test/legacy_test/test_imperative_optimizer.py | 46 +- .../test_imperative_optimizer_v2.py | 50 +- ...test_imperative_parallel_coalesce_split.py | 10 +- .../test_imperative_partitial_backward.py | 6 +- test/legacy_test/test_imperative_ptb_rnn.py | 26 +- ...test_imperative_ptb_rnn_sorted_gradient.py | 18 +- .../test_imperative_recurrent_usage.py | 34 +- .../test_imperative_reinforcement.py | 24 +- test/legacy_test/test_imperative_resnet.py | 26 +- .../test_imperative_resnet_sorted_gradient.py | 24 +- .../test_imperative_save_load_v2.py | 64 +-- .../legacy_test/test_imperative_se_resnext.py | 26 +- .../test_imperative_selected_rows.py | 20 +- ..._imperative_selected_rows_to_lod_tensor.py | 24 +- .../test_imperative_signal_handler.py | 2 +- ...perative_star_gan_with_gradient_penalty.py | 72 +-- .../test_imperative_tensor_clear_gradient.py | 14 +- ...imperative_trace_non_persistable_inputs.py | 12 +- ..._imperative_transformer_sorted_gradient.py | 30 +- .../test_imperative_triple_grad.py | 18 +- .../test_imperative_using_non_zero_gpu.py | 10 +- test/legacy_test/test_increment.py | 14 +- test/legacy_test/test_index_add_op.py | 2 +- test/legacy_test/test_index_put_op.py | 2 +- test/legacy_test/test_index_sample_op.py | 12 +- test/legacy_test/test_index_select_op.py | 24 +- .../test_infer_no_need_buffer_slots.py | 10 +- test/legacy_test/test_infer_shape.py | 2 +- test/legacy_test/test_inference_api.py | 28 +- test/legacy_test/test_inference_model_io.py | 20 +- test/legacy_test/test_initializer.py | 18 +- test/legacy_test/test_initializer_nn.py | 10 +- test/legacy_test/test_inplace.py | 52 +- test/legacy_test/test_inplace_abn_op.py | 18 +- .../test_inplace_addto_strategy.py | 26 +- ...test_inplace_softmax_with_cross_entropy.py | 22 +- test/legacy_test/test_input_spec.py | 4 +- test/legacy_test/test_instance_norm_op.py | 30 +- test/legacy_test/test_instance_norm_op_v2.py | 42 +- test/legacy_test/test_inverse_op.py | 32 +- test/legacy_test/test_io_save_load.py | 18 +- test/legacy_test/test_ir_graph.py | 34 +- test/legacy_test/test_ir_inplace_pass.py | 6 +- .../test_ir_memory_optimize_pass.py | 2 +- .../test_ir_memory_optimize_transformer.py | 2 +- test/legacy_test/test_isclose_op.py | 14 +- test/legacy_test/test_isfinite_op.py | 2 +- test/legacy_test/test_isfinite_v2_op.py | 20 +- test/legacy_test/test_jit_layer.py | 2 +- test/legacy_test/test_jit_save_load.py | 70 +-- test/legacy_test/test_kldiv_loss_op.py | 4 +- test/legacy_test/test_kron_op.py | 24 +- test/legacy_test/test_kthvalue_op.py | 8 +- test/legacy_test/test_l1_loss.py | 34 +- .../test_label_smooth_functional.py | 38 +- test/legacy_test/test_label_smooth_op.py | 2 +- test/legacy_test/test_lamb_op.py | 2 +- test/legacy_test/test_lambv2_op.py | 28 +- test/legacy_test/test_layer_norm_op.py | 14 +- test/legacy_test/test_layer_norm_op_v2.py | 44 +- test/legacy_test/test_layers.py | 268 ++++----- test/legacy_test/test_layout_autotune.py | 12 +- test/legacy_test/test_lazy_init.py | 2 +- test/legacy_test/test_lcm.py | 18 +- test/legacy_test/test_ldexp.py | 2 +- .../test_learning_rate_scheduler.py | 96 ++-- test/legacy_test/test_lerp_op.py | 2 +- test/legacy_test/test_less_equal_op.py | 2 +- test/legacy_test/test_less_than_op.py | 2 +- test/legacy_test/test_lgamma_op.py | 2 +- test/legacy_test/test_limit_by_capacity_op.py | 2 +- test/legacy_test/test_linalg_lstsq_op.py | 12 +- test/legacy_test/test_linalg_pinv_op.py | 14 +- test/legacy_test/test_linear.py | 8 +- test/legacy_test/test_linear_interp_op.py | 8 +- test/legacy_test/test_linear_interp_v2_op.py | 10 +- test/legacy_test/test_linspace.py | 12 +- test/legacy_test/test_listen_and_serv_op.py | 10 +- .../test_load_state_dict_from_old_format.py | 20 +- test/legacy_test/test_lod_array_length_op.py | 4 +- test/legacy_test/test_lod_tensor.py | 50 +- test/legacy_test/test_lod_tensor_array.py | 2 +- test/legacy_test/test_log_softmax.py | 6 +- test/legacy_test/test_logaddexp.py | 2 +- test/legacy_test/test_logcumsumexp_op.py | 28 +- test/legacy_test/test_logit_op.py | 4 +- test/legacy_test/test_logspace.py | 2 +- test/legacy_test/test_logsumexp.py | 10 +- test/legacy_test/test_lookahead.py | 14 +- test/legacy_test/test_lookup_table_bf16_op.py | 16 +- test/legacy_test/test_lookup_table_op.py | 2 +- .../test_lookup_table_v2_bf16_op.py | 16 +- test/legacy_test/test_lookup_table_v2_op.py | 22 +- test/legacy_test/test_lr_scheduler.py | 2 +- test/legacy_test/test_lrn_op.py | 44 +- test/legacy_test/test_lstm_cudnn_op.py | 2 +- test/legacy_test/test_lu_op.py | 20 +- test/legacy_test/test_lu_unpack_op.py | 34 +- test/legacy_test/test_manual_seed.py | 6 +- .../test_margin_cross_entropy_op.py | 18 +- test/legacy_test/test_margin_rank_loss_op.py | 18 +- test/legacy_test/test_masked_select_op.py | 2 +- test/legacy_test/test_math_op_patch.py | 126 ++-- .../test_math_op_patch_var_base.py | 156 ++--- test/legacy_test/test_matmul_int8_op.py | 2 +- test/legacy_test/test_matmul_op.py | 28 +- test/legacy_test/test_matmul_v2_op.py | 46 +- test/legacy_test/test_matrix_nms_op.py | 2 +- test/legacy_test/test_matrix_power_op.py | 30 +- test/legacy_test/test_matrix_rank_op.py | 26 +- test/legacy_test/test_max_min_amax_amin_op.py | 18 +- test/legacy_test/test_max_op.py | 2 +- test/legacy_test/test_maximum_op.py | 2 +- test/legacy_test/test_maxout_op.py | 2 +- test/legacy_test/test_mean_op.py | 26 +- test/legacy_test/test_memcpy_op.py | 22 +- .../test_memory_efficient_attention.py | 2 +- .../test_memory_reuse_exclude_feed_var.py | 26 +- .../legacy_test/test_merge_selectedrows_op.py | 2 +- test/legacy_test/test_merged_adam_op.py | 18 +- test/legacy_test/test_merged_momentum_op.py | 2 +- test/legacy_test/test_meshgrid_op.py | 44 +- test/legacy_test/test_metrics.py | 14 +- test/legacy_test/test_min_op.py | 6 +- test/legacy_test/test_minimum_op.py | 2 +- .../test_mix_precision_all_reduce_fuse.py | 2 +- test/legacy_test/test_mode_op.py | 8 +- test/legacy_test/test_model.py | 84 +-- test/legacy_test/test_modelaverage.py | 16 +- test/legacy_test/test_momentum_op.py | 42 +- test/legacy_test/test_monitor.py | 16 +- test/legacy_test/test_mse_loss.py | 70 +-- test/legacy_test/test_mul_nn_grad.py | 8 +- test/legacy_test/test_mul_op.py | 2 +- test/legacy_test/test_multi_dot_op.py | 2 +- .../test_multi_label_soft_margin_loss.py | 2 +- test/legacy_test/test_multiclass_nms_op.py | 4 +- test/legacy_test/test_multinomial_op.py | 18 +- test/legacy_test/test_multiplex_op.py | 6 +- .../test_multiprocess_dataloader_dataset.py | 24 +- .../test_multiprocess_dataloader_dynamic.py | 14 +- .../test_multiprocess_dataloader_exception.py | 16 +- ...ess_dataloader_iterable_dataset_dynamic.py | 14 +- ...ocess_dataloader_iterable_dataset_split.py | 10 +- ...cess_dataloader_iterable_dataset_static.py | 34 +- .../test_multiprocess_dataloader_static.py | 44 +- .../test_multiprocess_reader_exception.py | 26 +- .../test_naive_best_fit_gpu_memory_limit.py | 20 +- test/legacy_test/test_name_scope.py | 14 +- test/legacy_test/test_nan_inf.py | 10 +- test/legacy_test/test_nan_inf_dir.py | 2 +- test/legacy_test/test_nan_to_num_op.py | 4 +- test/legacy_test/test_nanmean_api.py | 2 +- test/legacy_test/test_nanmedian.py | 4 +- test/legacy_test/test_nansum_api.py | 22 +- test/legacy_test/test_nce.py | 44 +- test/legacy_test/test_nearest_interp_op.py | 2 +- test/legacy_test/test_nearest_interp_v2_op.py | 12 +- test/legacy_test/test_neg_op.py | 2 +- test/legacy_test/test_network_with_dtype.py | 18 +- test/legacy_test/test_nll_loss.py | 296 +++++----- .../test_nn_functional_embedding_static.py | 14 +- test/legacy_test/test_nn_functional_hot_op.py | 16 +- test/legacy_test/test_nn_grad.py | 72 +-- test/legacy_test/test_nn_margin_rank_loss.py | 6 +- test/legacy_test/test_nn_matmul_v2_grad.py | 68 +-- test/legacy_test/test_nn_sigmoid_op.py | 16 +- test/legacy_test/test_noamdecay_op.py | 2 +- test/legacy_test/test_nonzero_api.py | 16 +- test/legacy_test/test_norm_all.py | 24 +- test/legacy_test/test_norm_nn_grad.py | 40 +- test/legacy_test/test_norm_op.py | 12 +- test/legacy_test/test_normal.py | 2 +- .../legacy_test/test_normalization_wrapper.py | 12 +- test/legacy_test/test_normalize.py | 20 +- test/legacy_test/test_npair_loss_op.py | 10 +- test/legacy_test/test_number_count_op.py | 2 +- test/legacy_test/test_numel_op.py | 16 +- test/legacy_test/test_one_hot_v2_op.py | 16 +- test/legacy_test/test_ones_like.py | 16 +- test/legacy_test/test_ones_op.py | 2 +- .../legacy_test/test_op_function_generator.py | 24 +- test/legacy_test/test_op_name_conflict.py | 14 +- test/legacy_test/test_op_support_gpu.py | 2 +- test/legacy_test/test_op_version.py | 10 +- test/legacy_test/test_operator.py | 2 +- test/legacy_test/test_operator_desc.py | 4 +- test/legacy_test/test_optimizer.py | 36 +- .../legacy_test/test_optimizer_for_varbase.py | 8 +- test/legacy_test/test_optimizer_grad.py | 26 +- .../test_optimizer_in_control_flow.py | 30 +- test/legacy_test/test_overlap_add_op.py | 2 +- test/legacy_test/test_pad3d_op.py | 2 +- test/legacy_test/test_pad_op.py | 8 +- .../test_paddle_imperative_double_grad.py | 10 +- .../test_paddle_multiprocessing.py | 2 +- test/legacy_test/test_paddle_save_load.py | 86 ++- .../test_paddle_save_load_binary.py | 54 +- test/legacy_test/test_paddlescience.py | 4 +- test/legacy_test/test_pairwise_distance.py | 8 +- .../test_parallel_dygraph_dataparallel.py | 6 +- ...t_parallel_dygraph_dataparallel_cpuonly.py | 4 +- .../test_parallel_dygraph_transformer_gloo.py | 4 +- ...utor_seresnext_with_fuse_all_reduce_cpu.py | 6 +- ...utor_seresnext_with_fuse_all_reduce_gpu.py | 6 +- ...llel_executor_seresnext_with_reduce_cpu.py | 2 +- .../test_parallel_executor_transformer.py | 2 +- test/legacy_test/test_parameter.py | 8 +- ...test_partial_eager_deletion_transformer.py | 4 +- test/legacy_test/test_pass_builder.py | 16 +- test/legacy_test/test_pixel_shuffle_op.py | 26 +- test/legacy_test/test_pixel_unshuffle.py | 28 +- test/legacy_test/test_poisson_nll_loss.py | 2 +- test/legacy_test/test_poisson_op.py | 4 +- test/legacy_test/test_polar.py | 2 +- test/legacy_test/test_polygamma_op.py | 2 +- test/legacy_test/test_pool1d_api.py | 84 +-- test/legacy_test/test_pool2d_api.py | 120 ++-- test/legacy_test/test_pool2d_op.py | 4 +- test/legacy_test/test_pool3d_api.py | 118 ++-- test/legacy_test/test_pool3d_op.py | 4 +- test/legacy_test/test_pool_max_op.py | 2 +- test/legacy_test/test_pow.py | 2 +- test/legacy_test/test_prelu_op.py | 16 +- test/legacy_test/test_print_op.py | 12 +- test/legacy_test/test_prod_op.py | 2 +- test/legacy_test/test_program.py | 12 +- test/legacy_test/test_program_code.py | 6 +- test/legacy_test/test_program_converter.py | 2 +- .../test_program_prune_backward.py | 46 +- test/legacy_test/test_program_to_string.py | 4 +- test/legacy_test/test_protobuf.py | 2 +- test/legacy_test/test_protobuf_descs.py | 4 +- test/legacy_test/test_prune.py | 172 +++--- .../test_prune_gate_by_capacity_op.py | 2 +- test/legacy_test/test_psroi_pool_op.py | 8 +- test/legacy_test/test_pull_gpups_sparse_op.py | 16 +- test/legacy_test/test_py_func_op.py | 40 +- .../legacy_test/test_py_reader_combination.py | 14 +- .../legacy_test/test_py_reader_return_list.py | 18 +- .../test_py_reader_sample_generator.py | 16 +- test/legacy_test/test_pybind_interface.py | 2 +- test/legacy_test/test_pyramid_hash_op.py | 14 +- .../test_python_operator_overriding.py | 20 +- test/legacy_test/test_qr_op.py | 22 +- test/legacy_test/test_query_op.py | 2 +- test/legacy_test/test_queue.py | 16 +- test/legacy_test/test_rad2deg.py | 18 +- test/legacy_test/test_rand_op.py | 28 +- test/legacy_test/test_randint_op.py | 8 +- test/legacy_test/test_randn_op.py | 2 +- test/legacy_test/test_random_routing_op.py | 2 +- test/legacy_test/test_random_seed.py | 88 +-- test/legacy_test/test_randperm_op.py | 2 +- test/legacy_test/test_rank_attention_op.py | 2 +- .../legacy_test/test_raw_program_optimizer.py | 6 +- test/legacy_test/test_reader_reset.py | 20 +- test/legacy_test/test_real_imag_op.py | 6 +- test/legacy_test/test_reduce_op.py | 74 +-- .../test_registered_phi_kernels.py | 2 +- test/legacy_test/test_registry.py | 6 +- test/legacy_test/test_regularizer.py | 48 +- test/legacy_test/test_regularizer_api.py | 46 +- test/legacy_test/test_renorm_op.py | 10 +- test/legacy_test/test_repeat_interleave_op.py | 46 +- test/legacy_test/test_require_version.py | 114 ++-- test/legacy_test/test_reshape_op.py | 24 +- test/legacy_test/test_retain_graph.py | 4 +- test/legacy_test/test_rms_norm_op.py | 10 +- test/legacy_test/test_rmsprop_op.py | 24 +- test/legacy_test/test_rnn_cell_api.py | 10 +- test/legacy_test/test_rnn_decode_api.py | 22 +- test/legacy_test/test_rnn_memory_helper_op.py | 6 +- test/legacy_test/test_rnn_op.py | 2 +- test/legacy_test/test_roll_op.py | 24 +- test/legacy_test/test_rot90_op.py | 118 ++-- test/legacy_test/test_row_conv_op.py | 14 +- test/legacy_test/test_rrelu_op.py | 38 +- ...est_run_fluid_by_module_or_command_line.py | 4 +- test/legacy_test/test_run_program_op.py | 58 +- .../test_save_model_without_var.py | 12 +- test/legacy_test/test_scalar.py | 2 +- test/legacy_test/test_scale_op.py | 12 +- test/legacy_test/test_scatter_nd_op.py | 32 +- test/legacy_test/test_scatter_op.py | 26 +- test/legacy_test/test_scope.py | 14 +- test/legacy_test/test_searchsorted_op.py | 2 +- test/legacy_test/test_segment_ops.py | 10 +- .../test_select_input_output_op.py | 14 +- test/legacy_test/test_selected_rows.py | 2 +- test/legacy_test/test_selu_op.py | 10 +- test/legacy_test/test_set_bool_attr.py | 8 +- test/legacy_test/test_set_value_op.py | 16 +- test/legacy_test/test_sgd_op.py | 14 +- test/legacy_test/test_sgd_op_bf16.py | 20 +- test/legacy_test/test_shape_op.py | 2 +- test/legacy_test/test_share_data_op.py | 2 +- test/legacy_test/test_shuffle_batch_op.py | 4 +- ...st_sigmoid_cross_entropy_with_logits_op.py | 12 +- test/legacy_test/test_sigmoid_focal_loss.py | 8 +- test/legacy_test/test_sign_op.py | 14 +- test/legacy_test/test_simple_rnn_op.py | 2 +- test/legacy_test/test_size_op.py | 14 +- test/legacy_test/test_slice_op.py | 60 +- test/legacy_test/test_slice_var.py | 4 +- test/legacy_test/test_smooth_l1_loss.py | 82 +-- test/legacy_test/test_softmax2d.py | 2 +- test/legacy_test/test_softmax_mask_fuse_op.py | 16 +- ...est_softmax_mask_fuse_upper_triangle_op.py | 14 +- test/legacy_test/test_softmax_op.py | 26 +- .../test_softmax_with_cross_entropy_op.py | 2 +- test/legacy_test/test_solve_op.py | 54 +- test/legacy_test/test_sort_op.py | 12 +- test/legacy_test/test_space_to_depth_op.py | 14 +- test/legacy_test/test_sparse_attention_op.py | 6 +- test/legacy_test/test_sparse_conv_op.py | 2 +- test/legacy_test/test_sparse_embedding_op.py | 2 +- .../test_sparse_fused_attention_op.py | 2 +- test/legacy_test/test_sparse_norm_op.py | 6 +- test/legacy_test/test_sparse_unary_op.py | 2 +- test/legacy_test/test_sparse_utils_op.py | 2 +- .../test_spawn_and_init_parallel_env.py | 2 +- test/legacy_test/test_spectral_norm_op.py | 2 +- test/legacy_test/test_split_op.py | 60 +- test/legacy_test/test_splits_api.py | 2 +- test/legacy_test/test_square_error_cost.py | 10 +- test/legacy_test/test_squared_l2_norm_op.py | 2 +- test/legacy_test/test_squeeze2_op.py | 4 +- test/legacy_test/test_squeeze_op.py | 14 +- test/legacy_test/test_stack_op.py | 52 +- ...t_static_model_parallel_fused_attention.py | 4 +- ...static_model_parallel_fused_feedforward.py | 4 +- ..._model_parallel_fused_multi_transformer.py | 4 +- test/legacy_test/test_static_pylayer.py | 44 +- test/legacy_test/test_static_pylayer_block.py | 10 +- test/legacy_test/test_static_save_load.py | 274 ++++----- .../legacy_test/test_static_save_load_bf16.py | 22 +- .../test_static_save_load_large.py | 18 +- test/legacy_test/test_std_layer.py | 4 +- test/legacy_test/test_stride.py | 2 +- test/legacy_test/test_strided_slice_op.py | 12 +- test/legacy_test/test_subtract_op.py | 2 +- test/legacy_test/test_sum_op.py | 40 +- test/legacy_test/test_svd_op.py | 16 +- test/legacy_test/test_switch.py | 6 +- test/legacy_test/test_switch_autotune.py | 6 +- test/legacy_test/test_switch_case.py | 44 +- test/legacy_test/test_sync_batch_norm_op.py | 34 +- test/legacy_test/test_take.py | 20 +- test/legacy_test/test_tdm_child_op.py | 10 +- test/legacy_test/test_tdm_sampler_op.py | 14 +- test/legacy_test/test_temporal_shift_op.py | 6 +- test/legacy_test/test_tensor.py | 18 +- .../test_tensor_array_to_tensor.py | 26 +- test/legacy_test/test_tensor_copy_from.py | 2 +- test/legacy_test/test_tensor_fill_.py | 18 +- .../legacy_test/test_tensor_fill_diagonal_.py | 38 +- .../test_tensor_fill_diagonal_tensor.py | 8 +- .../test_tensor_fill_diagonal_tensor_.py | 8 +- test/legacy_test/test_tensor_register_hook.py | 6 +- test/legacy_test/test_tensor_to_list.py | 10 +- test/legacy_test/test_tensor_to_numpy.py | 12 +- test/legacy_test/test_tensor_uva.py | 8 +- test/legacy_test/test_tensor_zero_.py | 10 +- test/legacy_test/test_tensordot.py | 6 +- test/legacy_test/test_tf32_cublas.py | 10 +- test/legacy_test/test_tf32_cudnn.py | 2 +- test/legacy_test/test_tile_op.py | 18 +- test/legacy_test/test_top_k_v2_op.py | 6 +- test/legacy_test/test_trace_op.py | 8 +- test/legacy_test/test_traced_layer_err_msg.py | 64 +-- test/legacy_test/test_trainable.py | 16 +- test/legacy_test/test_trainer_desc.py | 6 +- test/legacy_test/test_trans_layout_op.py | 12 +- test/legacy_test/test_transfer_dtype_op.py | 2 +- test/legacy_test/test_transfer_layout_op.py | 14 +- test/legacy_test/test_transformer_api.py | 40 +- test/legacy_test/test_transpose_op.py | 72 +-- test/legacy_test/test_trapezoid.py | 4 +- test/legacy_test/test_triangular_solve_op.py | 18 +- test/legacy_test/test_tril_indices_op.py | 14 +- test/legacy_test/test_tril_triu_op.py | 32 +- test/legacy_test/test_trilinear_interp_op.py | 2 +- .../test_trilinear_interp_v2_op.py | 8 +- test/legacy_test/test_triu_indices_op.py | 14 +- test/legacy_test/test_trunc_op.py | 2 +- .../test_truncated_gaussian_random_op.py | 18 +- test/legacy_test/test_unbind_op.py | 16 +- test/legacy_test/test_unfold_op.py | 12 +- .../test_uniform_random_bf16_op.py | 24 +- .../test_uniform_random_inplace_op.py | 10 +- test/legacy_test/test_uniform_random_op.py | 100 ++-- test/legacy_test/test_unique.py | 2 +- .../legacy_test/test_unique_consecutive_op.py | 40 +- test/legacy_test/test_unique_name.py | 30 +- test/legacy_test/test_unique_with_counts.py | 2 +- test/legacy_test/test_unpool1d_op.py | 12 +- test/legacy_test/test_unpool3d_op.py | 14 +- test/legacy_test/test_unpool_op.py | 30 +- test/legacy_test/test_unsqueeze_op.py | 12 +- test/legacy_test/test_unstack_op.py | 6 +- test/legacy_test/test_unzip_op.py | 10 +- .../test_update_loss_scaling_op.py | 60 +- test/legacy_test/test_vander.py | 2 +- test/legacy_test/test_var_base.py | 142 ++--- test/legacy_test/test_variable.py | 70 +-- ...iable_length_memory_efficient_attention.py | 4 +- test/legacy_test/test_variance_layer.py | 2 +- test/legacy_test/test_version.py | 28 +- .../test_view_op_reuse_allocation.py | 2 +- test/legacy_test/test_viterbi_decode_op.py | 12 +- test/legacy_test/test_warpctc_op.py | 2 +- test/legacy_test/test_warprnnt_op.py | 2 +- test/legacy_test/test_weight_decay.py | 42 +- test/legacy_test/test_weight_normalization.py | 16 +- test/legacy_test/test_where_op.py | 68 ++- test/legacy_test/test_while_loop_op.py | 70 +-- test/legacy_test/test_while_op.py | 36 +- test/legacy_test/test_yolov3_loss_op.py | 2 +- test/legacy_test/test_zero_dim_tensor.py | 2 +- test/legacy_test/test_zeros_like_op.py | 20 +- test/legacy_test/test_zeros_op.py | 10 +- test/legacy_test/testsuite.py | 2 +- test/legacy_test/transformer_model.py | 4 +- test/legacy_test/utils.py | 6 +- test/mkldnn/check_flags_mkldnn_ops_on_off.py | 18 +- test/mkldnn/check_flags_use_mkldnn.py | 16 +- test/mkldnn/mkldnn_op_test.py | 16 +- test/mkldnn/test_activation_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_activation_mkldnn_op.py | 24 +- test/mkldnn/test_batch_norm_mkldnn_op.py | 2 +- test/mkldnn/test_cast_mkldnn_op.py | 2 +- test/mkldnn/test_clip_mkldnn_op.py | 2 +- test/mkldnn/test_concat_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_concat_mkldnn_op.py | 2 +- test/mkldnn/test_conv2d_bf16_mkldnn_op.py | 6 +- test/mkldnn/test_conv2d_int8_mkldnn_op.py | 8 +- test/mkldnn/test_conv2d_mkldnn_op.py | 4 +- .../test_conv2d_transpose_bf16_mkldnn_op.py | 6 +- .../mkldnn/test_conv2d_transpose_mkldnn_op.py | 2 +- test/mkldnn/test_dequantize_mkldnn_op.py | 4 +- .../test_elementwise_add_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_elementwise_div_mkldnn_op.py | 8 +- .../test_elementwise_mul_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_elementwise_sub_onednn_op.py | 8 +- test/mkldnn/test_expand_v2_mkldnn_op.py | 2 +- test/mkldnn/test_fc_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_flatten_mkldnn_op.py | 2 +- test/mkldnn/test_fused_vit_attention.py | 4 +- test/mkldnn/test_fusion_gru_bf16_mkldnn_op.py | 2 +- .../mkldnn/test_fusion_lstm_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_layer_norm_bf16_mkldnn_op.py | 10 +- test/mkldnn/test_layer_norm_mkldnn_op.py | 10 +- test/mkldnn/test_log_softmax_mkldnn_op.py | 2 +- test/mkldnn/test_matmul_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_matmul_v2_mkldnn_op.py | 2 +- test/mkldnn/test_mul_int8_mkldnn_op.py | 2 +- test/mkldnn/test_mul_mkldnn_op.py | 2 +- ...st_onnx_format_quantization_mobilenetv1.py | 6 +- test/mkldnn/test_pool2d_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_pool2d_int8_mkldnn_op.py | 4 +- test/mkldnn/test_prelu_mkldnn_op.py | 2 +- test/mkldnn/test_quantize_mkldnn_op.py | 2 +- test/mkldnn/test_reduce_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_requantize_mkldnn_op.py | 12 +- test/mkldnn/test_reshape_bf16_op.py | 2 +- test/mkldnn/test_reshape_mkldnn_op.py | 2 +- test/mkldnn/test_scale_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_shape_mkldnn_op.py | 2 +- test/mkldnn/test_shuffle_channel_mkldnn_op.py | 2 +- test/mkldnn/test_slice_mkldnn_op.py | 2 +- test/mkldnn/test_softmax_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_softmax_mkldnn_op.py | 4 +- test/mkldnn/test_split_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_squeeze2_mkldnn_op.py | 2 +- test/mkldnn/test_stack_mkldnn_op.py | 2 +- test/mkldnn/test_sum_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_sum_mkldnn_op.py | 2 +- test/mkldnn/test_transpose_bf16_mkldnn_op.py | 2 +- test/mkldnn/test_transpose_int8_mkldnn_op.py | 2 +- .../test_composite_batch_norm.py | 2 +- .../test_composite_batch_norm_grad.py | 2 +- .../composite_ops/test_composite_dropout.py | 2 +- .../prim/composite_ops/test_composite_gelu.py | 2 +- .../composite_ops/test_composite_gelu_grad.py | 2 +- .../test_composite_layer_norm.py | 6 +- .../test_composite_layer_norm_grad.py | 2 +- .../prim/composite_ops/test_composite_mean.py | 2 +- .../composite_ops/test_composite_mean_grad.py | 2 +- .../test_composite_relu_custom_vjp.py | 2 +- .../composite_ops/test_composite_softmax.py | 2 +- .../test_composite_softmax_custom_vjp.py | 2 +- .../test_composite_softmax_grad.py | 2 +- test/prim/model/bert.py | 2 +- test/prim/model/test_bert_cinn.py | 6 +- test/prim/model/test_bert_prim.py | 6 +- test/prim/model/test_bert_prim_cinn.py | 6 +- test/prim/model/test_comp_model_simple_net.py | 2 +- test/prim/model/test_prim_simplenet_cinn.py | 2 +- test/prim/model/test_resnet_cinn.py | 8 +- test/prim/model/test_resnet_prim.py | 8 +- test/prim/model/test_resnet_prim_cinn.py | 8 +- test/prim/new_ir_prim/test_vjp_prim.py | 2 +- test/prim/prim/flags/test_prim_flags.py | 2 +- test/prim/prim/flags/test_prim_flags_case.py | 2 +- .../vjp/eager/test_comp_eager_add_grad.py | 2 +- .../eager/test_comp_eager_batch_norm_grad.py | 2 +- .../vjp/eager/test_comp_eager_cast_grad.py | 4 +- .../vjp/eager/test_comp_eager_div_grad.py | 2 +- .../vjp/eager/test_comp_eager_exp_grad.py | 2 +- .../vjp/eager/test_comp_eager_expand_grad.py | 2 +- .../vjp/eager/test_comp_eager_gather_grad.py | 2 +- .../test_comp_eager_matmul_double_grad.py | 2 +- .../eager/test_comp_eager_multiply_grad.py | 2 +- .../vjp/eager/test_comp_eager_reshape_grad.py | 2 +- .../vjp/eager/test_comp_eager_sigmoid_grad.py | 2 +- .../vjp/eager/test_comp_eager_sqrt_grad.py | 2 +- .../vjp/eager/test_comp_eager_sub_grad.py | 2 +- .../vjp/eager/test_comp_eager_sum_grad.py | 2 +- .../vjp/eager/test_comp_eager_tanh_grad.py | 2 +- .../eager/test_comp_eager_transpose_grad.py | 2 +- .../prim/vjp/static/test_comp_add_grad.py | 2 +- .../vjp/static/test_comp_add_tanh_grad.py | 2 +- .../vjp/static/test_comp_batch_norm_grad.py | 2 +- .../prim/vjp/static/test_comp_cast_grad.py | 2 +- .../prim/vjp/static/test_comp_div_grad.py | 2 +- .../prim/vjp/static/test_comp_exp_grad.py | 2 +- .../prim/vjp/static/test_comp_expand_grad.py | 2 +- .../prim/vjp/static/test_comp_gather_grad.py | 2 +- .../static/test_comp_matmul_double_grad.py | 2 +- .../vjp/static/test_comp_multiply_grad.py | 2 +- .../prim/vjp/static/test_comp_reshape_grad.py | 2 +- .../prim/vjp/static/test_comp_sigmoid_grad.py | 2 +- .../prim/vjp/static/test_comp_sqrt_grad.py | 2 +- .../prim/vjp/static/test_comp_sub_grad.py | 2 +- .../prim/vjp/static/test_comp_sum_grad.py | 2 +- .../prim/vjp/static/test_comp_tanh_grad.py | 2 +- .../vjp/static/test_comp_transpose_grad.py | 2 +- test/prim/prim/vjp/test_comp_high_grad.py | 36 +- test/prim/process/test_check_inputs.py | 2 +- test/prim/process/test_copy_op.py | 2 +- test/prim/process/test_prim_amp.py | 2 +- test/prim/test_comp_custom_vjp.py | 2 +- test/prim/test_comp_dispensable.py | 6 +- ...est_comp_get_grad_op_desc_prim_disabled.py | 4 +- ...test_comp_get_grad_op_desc_prim_enabled.py | 4 +- test/prim/test_comp_skip_op_set.py | 2 +- test/ps/fl_ps_trainer.py | 26 +- test/ps/ps_dnn_model.py | 12 +- test/quantization/README.md | 2 +- test/quantization/convert_model2dot.py | 2 +- ...t2_int8_image_classification_comparison.py | 2 +- test/quantization/quant2_int8_lstm_model.py | 4 +- .../quant2_int8_nlp_comparison.py | 4 +- ...nt_int8_image_classification_comparison.py | 2 +- test/quantization/test_graph.py | 4 +- .../quantization/test_imperative_out_scale.py | 6 +- test/quantization/test_imperative_qat.py | 4 +- test/quantization/test_imperative_qat_amp.py | 4 +- test/quantization/test_llm_int8_linear.py | 16 +- ...t_post_training_quantization_lstm_model.py | 6 +- .../test_quant2_int8_mkldnn_pass.py | 2 +- test/quantization/test_quant_aware.py | 4 +- .../test_quant_aware_user_defined.py | 2 +- .../test_quantization_mkldnn_pass.py | 4 +- test/quantization/test_quantization_pass.py | 8 +- .../test_quantization_scale_pass.py | 4 +- .../test_user_defined_quantization.py | 4 +- test/quantization/test_weight_only_linear.py | 16 +- test/rnn/test_rnn_api.py | 28 +- test/rnn/test_rnn_cells.py | 4 +- test/rnn/test_rnn_cells_static.py | 28 +- test/rnn/test_rnn_cudnn_params_packing.py | 6 +- test/rnn/test_rnn_nets.py | 4 +- test/rnn/test_rnn_nets_static.py | 34 +- test/rnn/test_wrappers.py | 4 +- test/sequence/test_sequence_conv.py | 10 +- test/sequence/test_sequence_expand_as.py | 2 +- test/sequence/test_sequence_first_step.py | 2 +- test/sequence/test_sequence_last_step.py | 2 +- test/sequence/test_sequence_mask.py | 2 +- test/sequence/test_sequence_pad_op.py | 2 +- test/sequence/test_sequence_softmax_op.py | 2 +- .../test_standalone_controlflow.py | 4 +- .../test_standalone_cross_step_overlap.py | 2 +- .../test_standalone_custom_event.py | 4 +- .../test_standalone_custom_stream.py | 2 +- .../test_standalone_executor.py | 18 +- ...t_standalone_executor_aot_choose_kernel.py | 4 +- ...t_standalone_executor_multi_micro_batch.py | 8 +- .../test_standalone_executor_plan.py | 2 +- .../test_standalone_multiply_write.py | 2 +- test/tokenizer/test_faster_tokenizer_op.py | 4 +- test/xpu/collective_allgather_op_xpu.py | 6 +- test/xpu/collective_allreduce_op_xpu.py | 6 +- test/xpu/collective_broadcast_op_xpu.py | 6 +- test/xpu/collective_concat_op.py | 6 +- test/xpu/collective_identity_op_xpu.py | 6 +- test/xpu/collective_split_op.py | 6 +- test/xpu/get_test_cover_info.py | 2 +- test/xpu/op_test_xpu.py | 10 +- test/xpu/test_activation_op_xpu.py | 24 +- test/xpu/test_adadelta_op_xpu.py | 14 +- test/xpu/test_adam_op_xpu.py | 2 +- test/xpu/test_adamw_op_xpu.py | 26 +- test/xpu/test_affine_channel_op_xpu.py | 2 +- test/xpu/test_assign_value_op_xpu.py | 14 +- test/xpu/test_batch_norm_op_xpu.py | 8 +- test/xpu/test_bilinear_interp_op_xpu.py | 16 +- test/xpu/test_bitwise_op_xpu.py | 14 +- test/xpu/test_c_concat.py | 2 +- test/xpu/test_c_split.py | 2 +- test/xpu/test_cast_op_xpu.py | 8 +- test/xpu/test_clip_op_xpu.py | 20 +- test/xpu/test_coalesce_tensor_op_xpu.py | 8 +- test/xpu/test_collective_allgather_xpu.py | 2 +- test/xpu/test_collective_allreduce_xpu.py | 2 +- test/xpu/test_collective_base_xpu.py | 14 +- test/xpu/test_collective_broadcast_xpu.py | 2 +- test/xpu/test_collective_identity_xpu.py | 2 +- test/xpu/test_conv2d_op_xpu.py | 10 +- test/xpu/test_conv3d_op_xpu.py | 8 +- test/xpu/test_deformable_conv_op_xpu.py | 10 +- test/xpu/test_device_guard_xpu.py | 2 +- test/xpu/test_diag_v2_op_xpu.py | 14 +- test/xpu/test_dropout_op_xpu.py | 26 +- test/xpu/test_elementwise_add_op_xpu.py | 20 +- test/xpu/test_elementwise_add_op_xpu_kp.py | 20 +- test/xpu/test_elementwise_div_op_xpu.py | 6 +- test/xpu/test_elementwise_floordiv_op_xpu.py | 4 +- test/xpu/test_elementwise_mod_op_xpu.py | 8 +- test/xpu/test_elementwise_mul_op_xpu.py | 4 +- test/xpu/test_empty_op_xpu.py | 2 +- test/xpu/test_expand_as_v2_op_xpu.py | 6 +- test/xpu/test_expand_v2_op_xpu.py | 10 +- test/xpu/test_fill_any_op_xpu.py | 6 +- test/xpu/test_fill_op_xpu.py | 2 +- test/xpu/test_fleet_exe_dist_model_run_xpu.py | 2 +- test/xpu/test_fused_attention_op_xpu.py | 2 +- test/xpu/test_fused_feedforward_op_xpu.py | 2 +- .../test_fused_gemm_epilogue_grad_op_xpu.py | 2 +- test/xpu/test_fused_gemm_epilogue_op_xpu.py | 2 +- .../test_fused_resnet_basic_block_op_xpu.py | 42 +- test/xpu/test_gaussian_random_op_xpu.py | 18 +- test/xpu/test_gen_bkcl_id_op.py | 2 +- test/xpu/test_generate_proposals_v2_op_xpu.py | 2 +- test/xpu/test_group_norm_op_xpu.py | 6 +- test/xpu/test_huber_loss_op_xpu.py | 4 +- test/xpu/test_index_sample_op_xpu.py | 10 +- test/xpu/test_index_select_op_xpu.py | 20 +- test/xpu/test_instance_norm_op_xpu.py | 8 +- test/xpu/test_inverse_op_xpu.py | 14 +- test/xpu/test_kldiv_loss_op_xpu.py | 4 +- test/xpu/test_logical_op_xpu.py | 14 +- test/xpu/test_matmul_op_xpu.py | 28 +- test/xpu/test_mean_op_xpu.py | 2 +- test/xpu/test_merged_momentum_op_xpu.py | 2 +- test/xpu/test_merged_momentum_op_xpu_base.py | 4 +- test/xpu/test_momentum_op_xpu.py | 2 +- test/xpu/test_nearest_interp_op_xpu.py | 6 +- test/xpu/test_one_hot_op_xpu.py | 2 +- test/xpu/test_one_hot_v2_op_xpu.py | 16 +- test/xpu/test_pad3d_op_xpu.py | 2 +- test/xpu/test_pad_op_xpu.py | 8 +- .../xpu/test_parallel_dygraph_dataparallel.py | 6 +- test/xpu/test_pool2d_op_xpu.py | 2 +- test/xpu/test_pool3d_op_xpu.py | 2 +- test/xpu/test_prelu_op_xpu.py | 12 +- test/xpu/test_randperm_op_xpu.py | 2 +- test/xpu/test_refactor_op_xpu.py | 6 +- test/xpu/test_rmsprop_op_xpu.py | 8 +- test/xpu/test_rnn_op_xpu.py | 2 +- test/xpu/test_roi_align_op_xpu.py | 2 +- test/xpu/test_scale_op_xpu.py | 2 +- test/xpu/test_sequence_conv_op_xpu.py | 10 +- test/xpu/test_sequence_unpad_op_xpu.py | 8 +- test/xpu/test_set_value_op_xpu.py | 8 +- test/xpu/test_sgd_op_xpu.py | 10 +- test/xpu/test_shape_op_xpu.py | 4 +- test/xpu/test_squeeze_op_xpu.py | 6 +- test/xpu/test_sum_op_xpu.py | 18 +- test/xpu/test_tile_op_xpu.py | 4 +- .../test_truncated_gaussian_random_op_xpu.py | 8 +- test/xpu/test_unbind_op_xpu.py | 14 +- test/xpu/test_unfold_op_xpu.py | 6 +- test/xpu/test_unique_op_xpu.py | 4 +- test/xpu/test_update_loss_scaling_op_xpu.py | 38 +- test/xpu/test_warpctc_op_xpu.py | 2 +- test/xpu/test_where_index_xpu.py | 10 +- test/xpu/test_where_op_xpu.py | 28 +- test/xpu/test_while_op_xpu.py | 20 +- test/xpu/test_xpu_place.py | 8 +- tools/CrossStackProfiler/ProfileFileReader.py | 2 +- tools/check_file_diff_approvals.sh | 66 +-- tools/check_op_desc.py | 2 +- tools/check_op_register_type.py | 4 +- .../paddle_benchmark/paddle_test_benchmark.py | 2 +- .../cinn/paddle_benchmark/test_paddle_ops.py | 2 +- tools/count_api_without_core_ops.py | 8 +- tools/diff_api.py | 2 +- tools/diff_use_default_grad_op_maker.py | 4 +- tools/parse_kernel_info.py | 6 +- tools/print_op_desc.py | 2 +- tools/print_signatures.py | 6 +- tools/summary_env.py | 2 +- tools/test_check_api_compatible.py | 2 +- tools/test_runner.py | 16 +- tools/timeline.py | 2 +- 1965 files changed, 13864 insertions(+), 13959 deletions(-) create mode 100644 cmake/replace_string.py rename python/paddle/{fluid => base}/.gitignore (100%) rename python/paddle/{fluid => base}/__init__.py (97%) rename python/paddle/{fluid => base}/backward.py (99%) rename python/paddle/{fluid => base}/compiler.py (99%) rename python/paddle/{fluid => base}/core.py (100%) rename python/paddle/{fluid => base}/data_feed_desc.py (91%) rename python/paddle/{fluid => base}/data_feeder.py (95%) rename python/paddle/{fluid => base}/dataset.py (85%) rename python/paddle/{fluid => base}/default_scope_funcs.py (94%) rename python/paddle/{fluid => base}/device_worker.py (100%) rename python/paddle/{fluid => base}/dygraph/__init__.py (100%) rename python/paddle/{fluid => base}/dygraph/base.py (93%) rename python/paddle/{fluid => base}/dygraph/math_op_patch.py (100%) rename python/paddle/{fluid => base}/dygraph/tensor_patch_methods.py (98%) rename python/paddle/{fluid => base}/dygraph/tracer.py (99%) rename python/paddle/{fluid => base}/dygraph_utils.py (100%) rename python/paddle/{fluid => base}/executor.py (98%) rename python/paddle/{fluid => base}/framework.py (98%) rename python/paddle/{fluid => base}/incubate/__init__.py (94%) rename python/paddle/{fluid => base}/incubate/checkpoint/__init__.py (100%) rename python/paddle/{fluid => base}/incubate/checkpoint/auto_checkpoint.py (99%) rename python/paddle/{fluid => base}/incubate/checkpoint/checkpoint_saver.py (100%) rename python/paddle/{fluid => base}/initializer.py (97%) rename python/paddle/{fluid => base}/io.py (84%) rename python/paddle/{fluid => base}/layer_helper.py (100%) rename python/paddle/{fluid => base}/layer_helper_base.py (99%) rename python/paddle/{fluid => base}/layers/__init__.py (100%) rename python/paddle/{fluid => base}/layers/io.py (100%) rename python/paddle/{fluid => base}/layers/layer_function_generator.py (99%) rename python/paddle/{fluid => base}/layers/math_op_patch.py (98%) rename python/paddle/{fluid => base}/lod_tensor.py (93%) rename python/paddle/{fluid => base}/log_helper.py (100%) rename python/paddle/{fluid => base}/multiprocess_utils.py (100%) rename python/paddle/{fluid => base}/param_attr.py (99%) rename python/paddle/{fluid => base}/reader.py (95%) rename python/paddle/{fluid => base}/trainer_desc.py (100%) rename python/paddle/{fluid => base}/trainer_factory.py (99%) rename python/paddle/{fluid => base}/unique_name.py (100%) rename python/paddle/{fluid => base}/variable_index.py (98%) rename python/paddle/{fluid => base}/wrapped_decorator.py (100%) diff --git a/.flake8 b/.flake8 index ed87df8fb1b6f..9a569f7a6a56e 100644 --- a/.flake8 +++ b/.flake8 @@ -3,7 +3,7 @@ select = C,E,W exclude = ./build, # Exclude fluid directory - ./python/paddle/fluid/**, + ./python/paddle/base/**, # Exclude third-party libraries ./third_party/**, ./python/paddle/utils/gast/**, diff --git a/.gitignore b/.gitignore index 6abc4d02a4392..46b8e3a47c618 100644 --- a/.gitignore +++ b/.gitignore @@ -95,7 +95,7 @@ paddle/fluid/framework/__init__.py paddle/phi/api/profiler/__init__.py python/paddle/incubate/fleet/parameter_server/pslib/ps_pb2.py paddle/phi/kernels/fusion/cutlass/conv2d/generated/* -python/paddle/fluid/incubate/fleet/parameter_server/pslib/ps_pb2.py +python/paddle/base/incubate/fleet/parameter_server/pslib/ps_pb2.py paddle/fluid/ir_adaptor/translator/op_compat_info.cc paddle/fluid/pybind/static_op_function.* paddle/fluid/pybind/ops_api.cc diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 077db75fde206..32840a68330c2 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -1088,7 +1088,21 @@ function(py_proto_compile TARGET_NAME) "${multiValueArgs}" ${ARGN}) set(py_srcs) protobuf_generate_python(py_srcs ${py_proto_compile_SRCS}) - add_custom_target(${TARGET_NAME} ALL DEPENDS ${py_srcs} protobuf) + + add_custom_target(${TARGET_NAME}_replace DEPENDS ${py_srcs}) + + foreach(py_src ${py_srcs}) + add_custom_command( + TARGET ${TARGET_NAME}_replace + POST_BUILD + COMMAND ${PYTHON_EXECUTABLE} ${PADDLE_SOURCE_DIR}/cmake/replace_string.py + ${py_src} + COMMENT + "Replacing 'paddle.fluid' with 'paddle.base' generated by protobuf" + COMMENT "Replace ${py_src}") + endforeach() + + add_custom_target(${TARGET_NAME} ALL DEPENDS protobuf ${TARGET_NAME}_replace) endfunction() function(py_test TARGET_NAME) diff --git a/cmake/replace_string.py b/cmake/replace_string.py new file mode 100644 index 0000000000000..b7e10b5c9f946 --- /dev/null +++ b/cmake/replace_string.py @@ -0,0 +1,31 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +import sys + + +def main(): + src = sys.argv[1] + + with open(src, 'r') as file: + content = file.read() + + new_content = content.replace('paddle.fluid', 'paddle.base') + + with open(src, 'w') as file: + file.write(new_content) + + +if __name__ == "__main__": + main() diff --git a/paddle/CMakeLists.txt b/paddle/CMakeLists.txt index 22eac537766c4..92e302eb15acc 100644 --- a/paddle/CMakeLists.txt +++ b/paddle/CMakeLists.txt @@ -1,5 +1,5 @@ set(PYTHON_TESTS_DIR - ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests + ${PADDLE_BINARY_DIR}/python/paddle/base/tests CACHE INTERNAL "python tests directory") add_subdirectory(utils) diff --git a/paddle/cinn/hlir/op/transform.cc b/paddle/cinn/hlir/op/transform.cc index 05cb9adb9778b..a668d3518df30 100644 --- a/paddle/cinn/hlir/op/transform.cc +++ b/paddle/cinn/hlir/op/transform.cc @@ -2044,7 +2044,7 @@ CINN_REGISTER_HELPER(transform_ops) { // pointers, the code generated by operator fusion will have out-of-bounds // access. It should not fuse with any other injective operators, though // scatter_add is injective. turn KNonFusible to kInjective will fail - // /Paddle/python/paddle/fluid/tests/unittests/test_index_select_op.py + // /Paddle/python/paddle/base/tests/unittests/test_index_select_op.py .set_attr( "OpPattern", cinn::hlir::framework::OpPatternKind::kNonFusible) .set_support_level(4); diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 5771a0abd75b6..d10ff999f6eb2 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -1,6 +1,6 @@ -paddle.fluid.optimizer.PipelineOptimizer (paddle.fluid.optimizer.PipelineOptimizer, ('document', '2e55a29dbeb874934f7a1a1af3a22b8c')) -paddle.fluid.optimizer.PipelineOptimizer.__init__ (ArgSpec(args=['self', 'optimizer', 'num_microbatches', 'start_cpu_core_id'], varargs=None, keywords=None, defaults=(1, 0)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) -paddle.fluid.optimizer.PipelineOptimizer.minimize (ArgSpec(args=['self', 'loss', 'startup_program', 'parameter_list', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) +paddle.incubate.optimizer.PipelineOptimizer (paddle.incubate.optimizer.PipelineOptimizer, ('document', '2e55a29dbeb874934f7a1a1af3a22b8c')) +paddle.incubate.optimizer.PipelineOptimizer.__init__ (ArgSpec(args=['self', 'optimizer', 'num_microbatches', 'start_cpu_core_id'], varargs=None, keywords=None, defaults=(1, 0)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) +paddle.incubate.optimizer.PipelineOptimizer.minimize (ArgSpec(args=['self', 'loss', 'startup_program', 'parameter_list', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.audio.features (ArgSpec(), ('document', 'd41d8cd98f00b204e9800998ecf8427e')) paddle.audio.features.layers.LogMelSpectrogram (ArgSpec(), ('document', 'c38b53606aa89215c4f00d3833e158b8')) paddle.audio.features.layers.LogMelSpectrogram.forward (ArgSpec(args=['self', 'x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': , 'x': }), ('document', '6c14f6f78dc697a6981cf90412e2f1ea')) diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index fab0a8765bbf4..3befea7d0fd2b 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -600,8 +600,8 @@ if(WITH_PYTHON) TARGET framework_py_proto POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory - ${PADDLE_BINARY_DIR}/python/paddle/fluid/proto - COMMAND cp *.py ${PADDLE_BINARY_DIR}/python/paddle/fluid/proto/ + ${PADDLE_BINARY_DIR}/python/paddle/base/proto + COMMAND cp *.py ${PADDLE_BINARY_DIR}/python/paddle/base/proto/ COMMAND cp distributed_strategy_*.py ${PADDLE_BINARY_DIR}/python/paddle/distributed/fleet/proto COMMENT "Copy generated python proto into directory paddle/fluid/proto." @@ -620,7 +620,7 @@ if(WITH_PYTHON) endif() else() string(REPLACE "/" "\\" proto_dstpath - "${PADDLE_BINARY_DIR}/python/paddle/fluid/proto/") + "${PADDLE_BINARY_DIR}/python/paddle/base/proto/") string( REPLACE "/" "\\" fleet_proto_dstpath "${PADDLE_BINARY_DIR}/python/paddle/distributed/fleet/proto/") @@ -628,7 +628,7 @@ if(WITH_PYTHON) TARGET framework_py_proto POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory - ${PADDLE_BINARY_DIR}/python/paddle/fluid/proto + ${PADDLE_BINARY_DIR}/python/paddle/base/proto COMMAND copy /Y *.py ${proto_dstpath} COMMAND copy /Y distributed_strategy_*.py ${fleet_proto_dstpath} COMMENT "Copy generated python proto into directory paddle/fluid/proto." diff --git a/paddle/fluid/framework/ir/auto_mixed_precision_pass.cc b/paddle/fluid/framework/ir/auto_mixed_precision_pass.cc index 37f90289c4b53..698de5d90c256 100644 --- a/paddle/fluid/framework/ir/auto_mixed_precision_pass.cc +++ b/paddle/fluid/framework/ir/auto_mixed_precision_pass.cc @@ -175,7 +175,7 @@ bool OpSupportPrecision(const std::string& op_type, // The set of ops that support fp16 calculation and are considered // numerically-dangerous, slower and whose effects may also be observed in // downstream ops. -// ref to python/paddle/fluid/contrib/mixed_precision/fp16_lists.py +// ref to python/paddle/base/contrib/mixed_precision/fp16_lists.py void AutoMixedPrecisionPass::SetDefaultBlacklist() const { black_list_.insert({ // numerically-dangerous diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 111e8ebdfdf86..295dffd53af42 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -1034,7 +1034,7 @@ void BindImperative(py::module *m_ptr) { import paddle data = np.random.randint(10, size=(3, 4)) - tensor = paddle.fluid.core.to_uva_tensor(data) + tensor = paddle.base.core.to_uva_tensor(data) print(tensor) )DOC"); @@ -1165,7 +1165,7 @@ void BindImperative(py::module *m_ptr) { import numpy as np import paddle - from paddle.fluid import core + from paddle.base import core from paddle.device import cuda if core.is_compiled_with_cuda(): @@ -1397,7 +1397,7 @@ void BindImperative(py::module *m_ptr) { import numpy as np import paddle - from paddle.fluid import core + from paddle.base import core from paddle.device import cuda if core.is_compiled_with_cuda(): diff --git a/paddle/fluid/pybind/place.cc b/paddle/fluid/pybind/place.cc index 1f78b0994ae98..55a73cac1b665 100644 --- a/paddle/fluid/pybind/place.cc +++ b/paddle/fluid/pybind/place.cc @@ -394,8 +394,8 @@ void BindPlace(pybind11::module &m) { // NOLINT **Note**: Examples: .. code-block:: python - import paddle.fluid as fluid - xpu_place = fluid.XPUPlace(0) + import paddle.base as base + xpu_place = base.XPUPlace(0) )DOC"); g_xpuplace_pytype = reinterpret_cast(xpuplace.ptr()); xpuplace diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index d204b2e919812..056c4b0daadfc 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -1103,7 +1103,7 @@ PYBIND11_MODULE(libpaddle, m) { }); // NOTE(zjl): ctest would load environment variables at the beginning even - // though we have not `import paddle.fluid as fluid`. So we add this API + // though we have not `import paddle.base as base`. So we add this API // to enable eager deletion mode in unittest. m.def("_set_eager_deletion_mode", &paddle::framework::SetEagerDeletionMode); @@ -1239,7 +1239,6 @@ All parameter, weight, gradient are variables in Paddle. Examples: .. code-block:: python - import paddle.fluid as fluid # create tensor from a scope and set value to it. param = scope.var('Param').get_tensor() param_array = np.full((height, row_numel), 5.0).astype("float32") @@ -2149,9 +2148,9 @@ All parameter, weight, gradient are variables in Paddle. Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - arr = fluid.LoDTensorArray() + arr = base.LoDTensorArray() )DOC"); g_framework_lodtensorarray_pytype = reinterpret_cast(pylodtensorarray.ptr()); @@ -2193,12 +2192,12 @@ All parameter, weight, gradient are variables in Paddle. Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - arr = fluid.LoDTensorArray() - t = fluid.LoDTensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + arr = base.LoDTensorArray() + t = base.LoDTensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) arr.append(t) )DOC") .def( diff --git a/paddle/fluid/pybind/tensor.cc b/paddle/fluid/pybind/tensor.cc index 0bc22b373668f..b3edc9575223d 100644 --- a/paddle/fluid/pybind/tensor.cc +++ b/paddle/fluid/pybind/tensor.cc @@ -393,11 +393,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) )DOC") .def( @@ -413,11 +413,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) print(t.shape()) # [5, 30] )DOC") .def("_to_dlpack", @@ -517,11 +517,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) t.set_lod([[0, 2, 5]]) print(t.lod()) # [[0, 2, 5]] )DOC") @@ -566,11 +566,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) t.set_recursive_sequence_lengths([[2, 3]]) print(t.recursive_sequence_lengths()) # [[2, 3]] print(t.lod()) # [[0, 2, 5]] @@ -594,11 +594,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) t.set_lod([[0, 2, 5]]) print(t.lod()) # [[0, 2, 5]] )DOC") @@ -623,11 +623,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) t.set_recursive_sequence_lengths([[2, 3]]) print(t.recursive_sequence_lengths()) # [[2, 3]] )DOC") @@ -647,11 +647,11 @@ void BindTensor(pybind11::module &m) { // NOLINT Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.Tensor() - t.set(np.ndarray([5, 30]), fluid.CPUPlace()) + t = base.Tensor() + t.set(np.ndarray([5, 30]), base.CPUPlace()) t.set_recursive_sequence_lengths([[2, 3]]) print(t.has_valid_recursive_sequence_lengths()) # True )DOC") @@ -824,7 +824,7 @@ void BindTensor(pybind11::module &m) { // NOLINT import paddle tensor = paddle.ones([3,3]) metainfo = tensor.value().get_tensor()._share_cuda() - tensor_from_shared = paddle.to_tensor(paddle.fluid.core.LoDTensor._new_shared_cuda(metainfo)) + tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_cuda(metainfo)) )DOC") #endif @@ -945,7 +945,7 @@ void BindTensor(pybind11::module &m) { // NOLINT import paddle tensor = paddle.ones([3,3]) metainfo = tensor.value().get_tensor()._share_filename() - tensor_from_shared = paddle.to_tensor(paddle.fluid.core.LoDTensor._new_shared_filename(metainfo)) + tensor_from_shared = paddle.to_tensor(paddle.base.core.LoDTensor._new_shared_filename(metainfo)) )DOC") .def("_shared_incref", diff --git a/paddle/phi/api/profiler/CMakeLists.txt b/paddle/phi/api/profiler/CMakeLists.txt index ec569fe9fbc85..af1aacae63bd5 100644 --- a/paddle/phi/api/profiler/CMakeLists.txt +++ b/paddle/phi/api/profiler/CMakeLists.txt @@ -1,7 +1,7 @@ proto_library(phi_profiler_proto SRCS profiler.proto) if(WITH_PYTHON AND EXISTS ${PADDLE_BINARY_DIR}) - set(FLUID_PATH ${PADDLE_BINARY_DIR}/python/paddle/fluid) + set(FLUID_PATH ${PADDLE_BINARY_DIR}/python/paddle/base) py_proto_compile(profiler_py_proto SRCS profiler.proto) file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/__init__.py) if(NOT WIN32) diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index f83028c0ffb4f..6a6208334e0e4 100644 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -2574,7 +2574,7 @@ set -ex function parallel_test_base_ipu() { mkdir -p ${PADDLE_ROOT}/build - cd ${PADDLE_ROOT}/build/python/paddle/fluid/tests/unittests/ipu + cd ${PADDLE_ROOT}/build/python/paddle/base/tests/unittests/ipu if [ ${WITH_TESTING:-ON} == "ON" ] ; then cat < 0 ): diff --git a/python/paddle/autograd/backward_mode.py b/python/paddle/autograd/backward_mode.py index 4419a60598abc..0e3382dcac700 100644 --- a/python/paddle/autograd/backward_mode.py +++ b/python/paddle/autograd/backward_mode.py @@ -13,8 +13,8 @@ # limitations under the License. import paddle -from paddle.fluid import core, framework -from paddle.fluid.backward import gradients_with_optimizer # noqa: F401 +from paddle.base import core, framework +from paddle.base.backward import gradients_with_optimizer # noqa: F401 __all__ = [] diff --git a/python/paddle/autograd/py_layer.py b/python/paddle/autograd/py_layer.py index fa9243804faf0..5ddf610bb032b 100644 --- a/python/paddle/autograd/py_layer.py +++ b/python/paddle/autograd/py_layer.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle.fluid import core +from paddle.base import core __all__ = [] @@ -404,7 +404,7 @@ def backward(ctx, *args): def once_differentiable(backward): def wrapper(ctx, *args): - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): outputs = backward(ctx, *args) return outputs diff --git a/python/paddle/autograd/saved_tensors_hooks.py b/python/paddle/autograd/saved_tensors_hooks.py index 709c646325ed0..5d68a90e78c89 100644 --- a/python/paddle/autograd/saved_tensors_hooks.py +++ b/python/paddle/autograd/saved_tensors_hooks.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import core +from paddle.base import core __all__ = [] diff --git a/python/paddle/fluid/.gitignore b/python/paddle/base/.gitignore similarity index 100% rename from python/paddle/fluid/.gitignore rename to python/paddle/base/.gitignore diff --git a/python/paddle/fluid/__init__.py b/python/paddle/base/__init__.py similarity index 97% rename from python/paddle/fluid/__init__.py rename to python/paddle/base/__init__.py index 833576a9c7f8a..07ca385e88bac 100644 --- a/python/paddle/fluid/__init__.py +++ b/python/paddle/base/__init__.py @@ -32,11 +32,11 @@ except Exception as e: raise e -# import all class inside framework into fluid module +# import all class inside framework into base module from . import framework from .framework import * -# import all class inside executor into fluid module +# import all class inside executor into base module from . import executor from .executor import * @@ -73,7 +73,7 @@ from . import unique_name from . import compiler from .compiler import * -from paddle.fluid.layers.math_op_patch import monkey_patch_variable +from paddle.base.layers.math_op_patch import monkey_patch_variable from .dygraph.base import enable_dygraph, disable_dygraph from .dygraph.tensor_patch_methods import monkey_patch_tensor from .core import _cuda_synchronize diff --git a/python/paddle/fluid/backward.py b/python/paddle/base/backward.py similarity index 99% rename from python/paddle/fluid/backward.py rename to python/paddle/base/backward.py index 9b09ec11cd3ab..f468f3a6a74a7 100755 --- a/python/paddle/fluid/backward.py +++ b/python/paddle/base/backward.py @@ -14,15 +14,15 @@ from .proto import framework_pb2 -from paddle.fluid import framework as framework -from paddle.fluid import program_guard +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.fluid +import paddle.base from .data_feeder import check_type import warnings @@ -1393,7 +1393,7 @@ def update_distop_context( grad_sub_block_list.append(grad_sub_block.desc) # In primitive mode, raw phi GradOp will be split into multiple small # primitive operators, and the split rules are defined in c++ level, - # see details: paddle/fluid/prim/api/manual/backward/composite_backward_api.h + # see details: paddle/base/prim/api/manual/backward/composite_backward_api.h # It means that the output's shape and dtype of previous operators which # maybe used as the input of next operators must be known. Therefore, # we infer shape and dtype in a sandbox block(named composite_block) for @@ -1462,10 +1462,10 @@ def find_op_index(block_desc, cur_op_desc): # Rename internal gradient variables in multiple backward # so that they have different names with previous backward. # For example: - # y = x * x, grad = fluid.gradients(fluid.gradients(y, x) + y * y, x) + # y = x * x, grad = base.gradients(base.gradients(y, x) + y * y, x) # In second-time backward, gradient variable names of partial # forward network (y * y) may be have same names with first-time - # fluid.gradients(y, x). + # base.gradients(y, x). # So rename here before _addup_repetitive_outputs_. if program._appending_grad_times > 1: for op_desc in grad_op_desc: @@ -1796,7 +1796,7 @@ def infershape_for_composite(block, grad_op_desc): grad_op_desc.copy_from(op_desc) if not framework.OpProtoHolder.instance().has_op_proto(grad_op_desc.type()): - # NOTE: Some raw fluid grad operators which hadn't been decomposed may not + # NOTE: Some raw base grad operators which hadn't been decomposed may not # implement InferVarType method, such as elementwise_xx_grad, and it will # cause the dtype or shape of corresponding cotangent incorrect. This # patch set the cotangent dtype and shape same with corresponding @@ -1869,7 +1869,7 @@ def _get_no_grad_set_name(no_grad_set): no_grad_set_name.add(no_grad_var) else: raise TypeError( - "The type of no_grad_set's member must be paddle.fluid.Variable or str, but received %s." + "The type of no_grad_set's member must be paddle.base.Variable or str, but received %s." % (type(no_grad_var)) ) else: @@ -2169,7 +2169,7 @@ def append_backward( parameter_list, 'parameter_list', (list, tuple, set), - 'fluid.backward.append_backward', + 'base.backward.append_backward', ) parameters = [] for i, param in enumerate(parameter_list): @@ -2177,7 +2177,7 @@ def append_backward( param, 'parameter_list[%s]' % i, (framework.Variable, str), - 'fluid.backward.append_backward', + 'base.backward.append_backward', ) if isinstance(param, framework.Variable): parameters.append(param.name) @@ -2710,14 +2710,14 @@ def gradients_with_optimizer(program, optimizer, inputs=None, outputs=None): img = static.data(name='image', shape=[None, 784]) pred = static.nn.fc(x=img, size=10, activation='relu') loss = paddle.mean(pred) - opt_ops, pram_grads = paddle.fluid.backward.gradients_with_optimizer(static.default_main_program(), opt) + opt_ops, pram_grads = paddle.base.backward.gradients_with_optimizer(static.default_main_program(), opt) print(opt_ops) """ check_type( program, 'program', - paddle.fluid.Program, + paddle.base.Program, 'paddle.static.gradients_with_optimizer', ) check_type( @@ -2747,7 +2747,7 @@ def gradients_with_optimizer(program, optimizer, inputs=None, outputs=None): pram_grads = [ (pram, grad) for pram, grad in zip(inputs, grads) - if isinstance(pram, paddle.fluid.framework.Parameter) + if isinstance(pram, paddle.base.framework.Parameter) and grad is not None ] diff --git a/python/paddle/fluid/compiler.py b/python/paddle/base/compiler.py similarity index 99% rename from python/paddle/fluid/compiler.py rename to python/paddle/base/compiler.py index 505b7ad566dc6..b1944ac343b8b 100644 --- a/python/paddle/fluid/compiler.py +++ b/python/paddle/base/compiler.py @@ -402,9 +402,9 @@ def convert_concrete_program( """ Convert the ConcreteProgram to IPUConcreteProgram. """ - from ..fluid.dygraph.base import switch_to_static_graph - from ..fluid import backward - from ..fluid.framework import device_guard + from ..base.dygraph.base import switch_to_static_graph + from ..base import backward + from ..base.framework import device_guard import paddle inputs = concrete_program.inputs diff --git a/python/paddle/fluid/core.py b/python/paddle/base/core.py similarity index 100% rename from python/paddle/fluid/core.py rename to python/paddle/base/core.py diff --git a/python/paddle/fluid/data_feed_desc.py b/python/paddle/base/data_feed_desc.py similarity index 91% rename from python/paddle/fluid/data_feed_desc.py rename to python/paddle/base/data_feed_desc.py index 9e6257d96ef6a..3987440ecc6da 100644 --- a/python/paddle/fluid/data_feed_desc.py +++ b/python/paddle/base/data_feed_desc.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.proto import data_feed_pb2 +from paddle.base.proto import data_feed_pb2 from google.protobuf import text_format __all__ = ['DataFeedDesc'] @@ -28,12 +28,12 @@ class DataFeedDesc: DataFeedDesc shall be initialized from a valid protobuf message from disk. - See :code:`paddle/fluid/framework/data_feed.proto` for message definition. + See :code:`paddle/base/framework/data_feed.proto` for message definition. A typical message might look like: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base f = open("data.proto", "w") print >> f, 'name: "MultiSlotDataFeed"' print >> f, 'batch_size: 2' @@ -52,7 +52,7 @@ class DataFeedDesc: print >> f, ' }' print >> f, '}' f.close() - data_feed = fluid.DataFeedDesc('data.proto') + data_feed = base.DataFeedDesc('data.proto') However, users usually shouldn't care about the message format; instead, they are encouraged to use :code:`Data Generator` as a tool to generate a @@ -64,8 +64,8 @@ class DataFeedDesc: .. code-block:: python - import paddle.fluid as fluid - data_feed = fluid.DataFeedDesc('data.proto') + import paddle.base as base + data_feed = base.DataFeedDesc('data.proto') data_feed.set_batch_size(128) data_feed.set_dense_slots('wd') # The slot named 'wd' will be dense data_feed.set_use_slots('wd') # The slot named 'wd' will be used @@ -94,12 +94,12 @@ def __init__(self, proto_file): def set_batch_size(self, batch_size): """ - Set :attr:`batch_size` in :ref:`api_fluid_DataFeedDesc` . :attr:`batch_size` can be changed during training. + Set :attr:`batch_size` in :ref:`api_base_DataFeedDesc` . :attr:`batch_size` can be changed during training. Example: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base f = open("data.proto", "w") print >> f, 'name: "MultiSlotDataFeed"' print >> f, 'batch_size: 2' @@ -118,7 +118,7 @@ def set_batch_size(self, batch_size): print >> f, ' }' print >> f, '}' f.close() - data_feed = fluid.DataFeedDesc('data.proto') + data_feed = base.DataFeedDesc('data.proto') data_feed.set_batch_size(128) Args: @@ -140,7 +140,7 @@ def set_dense_slots(self, dense_slots_name): Example: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base f = open("data.proto", "w") print >> f, 'name: "MultiSlotDataFeed"' print >> f, 'batch_size: 2' @@ -159,7 +159,7 @@ def set_dense_slots(self, dense_slots_name): print >> f, ' }' print >> f, '}' f.close() - data_feed = fluid.DataFeedDesc('data.proto') + data_feed = base.DataFeedDesc('data.proto') data_feed.set_dense_slots(['words']) Args: @@ -187,7 +187,7 @@ def set_use_slots(self, use_slots_name): Example: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base f = open("data.proto", "w") print >> f, 'name: "MultiSlotDataFeed"' print >> f, 'batch_size: 2' @@ -206,7 +206,7 @@ def set_use_slots(self, use_slots_name): print >> f, ' }' print >> f, '}' f.close() - data_feed = fluid.DataFeedDesc('data.proto') + data_feed = base.DataFeedDesc('data.proto') data_feed.set_use_slots(['words']) Args: @@ -231,7 +231,7 @@ def desc(self): Example: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base f = open("data.proto", "w") print >> f, 'name: "MultiSlotDataFeed"' print >> f, 'batch_size: 2' @@ -250,7 +250,7 @@ def desc(self): print >> f, ' }' print >> f, '}' f.close() - data_feed = fluid.DataFeedDesc('data.proto') + data_feed = base.DataFeedDesc('data.proto') print(data_feed.desc()) Returns: diff --git a/python/paddle/fluid/data_feeder.py b/python/paddle/base/data_feeder.py similarity index 95% rename from python/paddle/fluid/data_feeder.py rename to python/paddle/base/data_feeder.py index 687504e3670e8..d4800aac174f2 100644 --- a/python/paddle/fluid/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -155,7 +155,7 @@ def check_type(input, input_name, expected_type, op_name, extra_message=''): expected_type += (core.eager.Tensor,) elif isinstance(input, core.eager.Tensor): raise TypeError( - "Please use `with fluid.dygraph.guard()` as context or `fluid.enable_dygraph()` to switch to imperative mode firstly. " + "Please use `with base.dygraph.guard()` as context or `base.enable_dygraph()` to switch to imperative mode firstly. " "Because received '{}' in {} is a imperative Variable.".format( input_name, op_name ) @@ -337,12 +337,12 @@ class DataFeeder: Parameters: feed_list (list): Variables or names of Variables that need to feed. - place (:ref:`api_fluid_CPUPlace` | :ref:`api_fluid_CUDAPlace` ): + place (:ref:`api_base_CPUPlace` | :ref:`api_base_CUDAPlace` ): place indicates the device (CPU | GPU) the data will be fed into, if - you want to feed data into GPU, please using :code:`fluid.CUDAPlace(i)` + you want to feed data into GPU, please using :code:`base.CUDAPlace(i)` (:code:`i` represents the GPU id), or if you want to feed data into CPU, - please using :code:`fluid.CPUPlace()`. - program (:ref:`api_fluid_Program` , optional): The Program that will + please using :code:`base.CPUPlace()`. + program (:ref:`api_base_Program` , optional): The Program that will feed data into, if program is None, it will use default_main_program(). Default None. @@ -354,24 +354,24 @@ class DataFeeder: import numpy as np import paddle - import paddle.fluid as fluid + import paddle.base as base - place = fluid.CPUPlace() + place = base.CPUPlace() def reader(): for _ in range(4): yield np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32'), - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): data_1 = paddle.static.data(name='data_1', shape=[None, 2, 2], dtype='float32') data_2 = paddle.static.data(name='data_2', shape=[None, 1, 3], dtype='float32') out = paddle.static.nn.fc(x=[data_1, data_2], size=2) # ... - feeder = fluid.DataFeeder([data_1, data_2], place) + feeder = base.DataFeeder([data_1, data_2], place) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) feed_data = feeder.feed(reader()) @@ -426,7 +426,7 @@ def feed(self, iterable): # result['data_1'] a LoD-Tensor with shape of [5, 2, 1, 3]. 5 is batch size, and [2, 1, 3] is the real shape of data_1. # result['data_2'], result['data_3'] are similar. import numpy as np - import paddle.fluid as fluid + import paddle.base as base def reader(limit=5): for i in range(1, limit + 1): @@ -435,7 +435,7 @@ def reader(limit=5): data_1 = paddle.static.data(name='data_1', shape=[None, 2, 1, 3]) data_2 = paddle.static.data(name='data_2', shape=[None, 1], dtype='int64') data_3 = paddle.static.data(name='data_3', shape=[None, 3, 3], dtype='float32') - feeder = fluid.DataFeeder(['data_1','data_2', 'data_3'], fluid.CPUPlace()) + feeder = base.DataFeeder(['data_1','data_2', 'data_3'], base.CPUPlace()) result = feeder.feed(reader()) diff --git a/python/paddle/fluid/dataset.py b/python/paddle/base/dataset.py similarity index 85% rename from python/paddle/fluid/dataset.py rename to python/paddle/base/dataset.py index 6f1ef89c504bd..099dba1e6d755 100644 --- a/python/paddle/fluid/dataset.py +++ b/python/paddle/base/dataset.py @@ -13,7 +13,7 @@ # limitations under the License. """This is definition of dataset class, which is high performance IO.""" -from paddle.fluid.proto import data_feed_pb2 +from paddle.base.proto import data_feed_pb2 from google.protobuf import text_format from . import core from ..utils import deprecated @@ -30,8 +30,8 @@ class DatasetFactory: Example: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") """ @@ -51,8 +51,8 @@ def create_dataset(self, datafeed_class="QueueDataset"): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() """ try: @@ -87,8 +87,8 @@ def set_pipe_command(self, pipe_command): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_pipe_command("python my_script.py") Args: @@ -104,8 +104,8 @@ def set_so_parser_name(self, so_parser_name): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_so_parser_name("./abc.so") Args: @@ -121,8 +121,8 @@ def set_rank_offset(self, rank_offset): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_rank_offset("rank_offset") Args: @@ -145,8 +145,8 @@ def set_fea_eval(self, record_candidate_size, fea_eval=True): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_fea_eval(1000000, True) """ @@ -166,8 +166,8 @@ def slots_shuffle(self, slots): slots(list[string]): the set of slots(string) to do slots shuffle. Examples: - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_merge_by_lineid() #suppose there is a slot 0 dataset.slots_shuffle(['0']) @@ -183,8 +183,8 @@ def set_batch_size(self, batch_size): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_batch_size(128) Args: @@ -200,8 +200,8 @@ def set_pv_batch_size(self, pv_batch_size): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_pv_batch(128) Args: pv_batch_size(int): pv batch size @@ -216,8 +216,8 @@ def set_thread(self, thread_num): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_thread(12) Args: @@ -233,8 +233,8 @@ def set_filelist(self, filelist): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_filelist(['a.txt', 'b.txt']) Args: @@ -253,8 +253,8 @@ def set_use_var(self, var_list): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_use_var([data, label]) Args: @@ -276,7 +276,7 @@ def set_use_var(self, var_list): slot_var.type = "uint32" else: raise ValueError( - "Currently, fluid.dataset only supports dtype=float32, dtype=int32 and dtype=int64" + "Currently, base.dataset only supports dtype=float32, dtype=int32 and dtype=int64" ) def set_hdfs_config(self, fs_name, fs_ugi): @@ -286,8 +286,8 @@ def set_hdfs_config(self, fs_name, fs_ugi): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_hdfs_config("my_fs_name", "my_fs_ugi") Args: @@ -303,8 +303,8 @@ def set_download_cmd(self, download_cmd): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() dataset.set_download_cmd("./read_from_afs") Args: @@ -347,8 +347,8 @@ def desc(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset() + import paddle.base as base + dataset = base.DatasetFactory().create_dataset() print(dataset.desc()) Returns: @@ -370,7 +370,7 @@ class InMemoryDataset(DatasetBase): This class should be created by DatasetFactory Example: - dataset = paddle.fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = paddle.base.DatasetFactory().create_dataset("InMemoryDataset") """ @deprecated(since="2.0.0", update_to="paddle.distributed.InMemoryDataset") @@ -465,8 +465,8 @@ def set_queue_num(self, queue_num): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_queue_num(12) """ @@ -487,8 +487,8 @@ def set_parse_ins_id(self, parse_ins_id): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_parse_ins_id(True) """ @@ -508,8 +508,8 @@ def set_parse_content(self, parse_content): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_parse_content(True) """ @@ -525,8 +525,8 @@ def set_parse_logkey(self, parse_logkey): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_parse_logkey(True) """ @@ -542,8 +542,8 @@ def _set_trainer_num(self, trainer_num): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset._set_trainer_num(1) """ @@ -563,8 +563,8 @@ def set_merge_by_sid(self, merge_by_sid): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_merge_by_sid(True) """ @@ -580,8 +580,8 @@ def set_enable_pv_merge(self, enable_pv_merge): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_enable_pv_merge(True) """ @@ -595,8 +595,8 @@ def preprocess_instance(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -613,8 +613,8 @@ def set_current_phase(self, current_phase): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -630,8 +630,8 @@ def postprocess_instance(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -656,8 +656,8 @@ def set_fleet_send_batch_size(self, fleet_send_batch_size=1024): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_fleet_send_batch_size(800) """ @@ -677,8 +677,8 @@ def set_fleet_send_sleep_seconds(self, fleet_send_sleep_seconds=0): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_fleet_send_sleep_seconds(2) """ @@ -699,8 +699,8 @@ def set_merge_by_lineid(self, merge_size=2): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_merge_by_lineid() """ @@ -740,9 +740,9 @@ def set_date(self, date): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_date("20211111") """ year = int(date[:4]) @@ -766,8 +766,8 @@ def load_into_memory(self, is_shuffle=False): .. code-block:: python # required: skiptest - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -794,8 +794,8 @@ def preload_into_memory(self, thread_num=None): .. code-block:: python # required: skiptest - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.preload_into_memory() @@ -820,8 +820,8 @@ def wait_preload_done(self): .. code-block:: python # required: skiptest - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.preload_into_memory() @@ -842,8 +842,8 @@ def local_shuffle(self): .. code-block:: python # required: skiptest - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -866,9 +866,9 @@ def global_shuffle(self, fleet=None, thread_num=12): .. code-block:: python # required: skiptest - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -928,16 +928,16 @@ def release_memory(self): .. code-block:: python # required: skiptest - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() dataset.global_shuffle(fleet) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) - exe.train_from_dataset(fluid.default_main_program(), dataset) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) + exe.train_from_dataset(base.default_main_program(), dataset) dataset.release_memory() """ @@ -957,8 +957,8 @@ def get_pv_data_size(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -992,9 +992,9 @@ def get_memory_data_size(self, fleet=None): .. code-block:: python # required: skiptest - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -1036,9 +1036,9 @@ def get_shuffle_data_size(self, fleet=None): .. code-block:: python # required: skiptest - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -1083,9 +1083,9 @@ def set_graph_config(self, config): .. code-block:: python # required: skiptest - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") graph_config = {"walk_len": 24, "walk_degree": 10, "once_sample_startid_len": 80000, @@ -1145,9 +1145,9 @@ def set_pass_id(self, pass_id): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base pass_id = 0 - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_pass_id(pass_id) """ self.pass_id = pass_id @@ -1163,8 +1163,8 @@ def get_pass_id(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") pass_id = dataset.get_pass_id() """ return self.pass_id @@ -1183,8 +1183,8 @@ class QueueDataset(DatasetBase): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("QueueDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("QueueDataset") """ @@ -1224,8 +1224,8 @@ def local_shuffle(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("QueueDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("QueueDataset") dataset.local_shuffle() Raises: @@ -1250,9 +1250,9 @@ def global_shuffle(self, fleet=None): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base from paddle.incubate.distributed.fleet.parameter_server.pslib import fleet - dataset = fluid.DatasetFactory().create_dataset("QueueDataset") + dataset = base.DatasetFactory().create_dataset("QueueDataset") #dataset.global_shuffle(fleet) Raises: @@ -1272,8 +1272,8 @@ class FileInstantDataset(DatasetBase): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory.create_dataset("FileInstantDataset") + import paddle.base as base + dataset = base.DatasetFactory.create_dataset("FileInstantDataset") """ def __init__(self): @@ -1312,8 +1312,8 @@ class BoxPSDataset(InMemoryDataset): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") """ def __init__(self): @@ -1342,8 +1342,8 @@ def begin_pass(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") dataset.begin_pass() """ self.boxps.begin_pass() @@ -1355,8 +1355,8 @@ def end_pass(self, need_save_delta): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") dataset.end_pass(True) """ self.boxps.end_pass(need_save_delta) @@ -1368,8 +1368,8 @@ def wait_preload_done(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.preload_into_memory() @@ -1383,8 +1383,8 @@ def load_into_memory(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.load_into_memory() @@ -1398,8 +1398,8 @@ def preload_into_memory(self): Examples: .. code-block:: python - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("BoxPSDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("BoxPSDataset") filelist = ["a.txt", "b.txt"] dataset.set_filelist(filelist) dataset.preload_into_memory() @@ -1427,8 +1427,8 @@ def slots_shuffle(self, slots): slots(list[string]): the set of slots(string) to do slots shuffle. Examples: - import paddle.fluid as fluid - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + import paddle.base as base + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_merge_by_lineid() #suppose there is a slot 0 dataset.slots_shuffle(['0']) diff --git a/python/paddle/fluid/default_scope_funcs.py b/python/paddle/base/default_scope_funcs.py similarity index 94% rename from python/paddle/fluid/default_scope_funcs.py rename to python/paddle/base/default_scope_funcs.py index f8faf69425246..80cfe40db57ad 100644 --- a/python/paddle/fluid/default_scope_funcs.py +++ b/python/paddle/base/default_scope_funcs.py @@ -26,7 +26,7 @@ invoked in a new local scope. """ -import paddle.fluid.core +import paddle.base.core import threading __tl_scope__ = threading.local() @@ -44,13 +44,13 @@ def get_cur_scope(): """ Get current scope. - :rtype: paddle.fluid.core.Scope + :rtype: paddle.base.core.Scope """ cur_scope_stack = getattr(__tl_scope__, 'cur_scope', None) if cur_scope_stack is None: __tl_scope__.cur_scope = list() if len(__tl_scope__.cur_scope) == 0: - __tl_scope__.cur_scope.append(paddle.fluid.core.Scope()) + __tl_scope__.cur_scope.append(paddle.base.core.Scope()) return __tl_scope__.cur_scope[-1] diff --git a/python/paddle/fluid/device_worker.py b/python/paddle/base/device_worker.py similarity index 100% rename from python/paddle/fluid/device_worker.py rename to python/paddle/base/device_worker.py diff --git a/python/paddle/fluid/dygraph/__init__.py b/python/paddle/base/dygraph/__init__.py similarity index 100% rename from python/paddle/fluid/dygraph/__init__.py rename to python/paddle/base/dygraph/__init__.py diff --git a/python/paddle/fluid/dygraph/base.py b/python/paddle/base/dygraph/base.py similarity index 93% rename from python/paddle/fluid/dygraph/base.py rename to python/paddle/base/dygraph/base.py index 5999d323b955c..06922ef9bd281 100644 --- a/python/paddle/fluid/dygraph/base.py +++ b/python/paddle/base/dygraph/base.py @@ -18,10 +18,10 @@ import inspect import sys import numpy as np -from paddle.fluid import core -from paddle.fluid import framework -from paddle.fluid.framework import global_var -from paddle.fluid.multiprocess_utils import CleanupFuncRegistrar +from paddle.base import core +from paddle.base import framework +from paddle.base.framework import global_var +from paddle.base.multiprocess_utils import CleanupFuncRegistrar from .tracer import Tracer import logging from ..data_feeder import convert_dtype @@ -166,13 +166,13 @@ def _convert_into_variable(tensor): def enabled(): """ This function checks whether the program runs in dynamic graph mode or not. - You can enter dynamic graph mode with :ref:`api_fluid_dygraph_guard` api, - or enable and disable dynamic graph mode with :ref:`api_fluid_dygraph_enable_dygraph` - and :ref:`api_fluid_dygraph_disable_dygraph` api . + You can enter dynamic graph mode with :ref:`api_base_dygraph_guard` api, + or enable and disable dynamic graph mode with :ref:`api_base_dygraph_enable_dygraph` + and :ref:`api_base_dygraph_disable_dygraph` api . **Note**: - ``fluid.dygraph.enabled`` is the alias of ``fluid.in_dygraph_mode``, and - ``fluid.in_dygraph_mode`` is recommended to use for now. + ``base.dygraph.enabled`` is the alias of ``base.in_dygraph_mode``, and + ``base.in_dygraph_mode`` is recommended to use for now. Returns: bool: Whether the program is running in dynamic graph mode. @@ -180,12 +180,12 @@ def enabled(): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - fluid.enable_dygraph() # Now we are in dygragh mode - print(fluid.dygraph.enabled()) # True - fluid.disable_dygraph() - print(fluid.dygraph.enabled()) # False + base.enable_dygraph() # Now we are in dygragh mode + print(base.dygraph.enabled()) # True + base.disable_dygraph() + print(base.dygraph.enabled()) # False """ # TODO(jiabin): Make this check as in_dygraph_mode when we support default eager mode. return framework.in_dygraph_mode() @@ -289,18 +289,18 @@ def no_grad(func=None): .. code-block:: python import numpy as np - import paddle.fluid as fluid + import paddle.base as base # use as generator data = np.array([[2, 3], [4, 5]]).astype('float32') - with fluid.dygraph.guard(): - l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None - l1 = fluid.Linear(2, 2) - with fluid.dygraph.no_grad(): + with base.dygraph.guard(): + l0 = base.Linear(2, 2) # l0.weight.gradient() is None + l1 = base.Linear(2, 2) + with base.dygraph.no_grad(): # l1.weight.stop_gradient is False tmp = l1.weight * 2 # tmp.stop_gradient is True - x = fluid.dygraph.to_variable(data) + x = base.dygraph.to_variable(data) y = l0(x) + tmp o = l1(y) o.backward() @@ -309,13 +309,13 @@ def no_grad(func=None): # use as decorator - @fluid.dygraph.no_grad + @base.dygraph.no_grad def test_layer(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): inp = np.ones([3, 1024], dtype='float32') - t = fluid.dygraph.base.to_variable(inp) - linear1 = fluid.Linear(1024, 4, bias_attr=False) - linear2 = fluid.Linear(4, 4) + t = base.dygraph.base.to_variable(inp) + linear1 = base.Linear(1024, 4, bias_attr=False) + linear2 = base.Linear(4, 4) ret = linear1(t) dy_ret = linear2(ret) @@ -555,7 +555,7 @@ def guard(place=None): This context will create a dygraph context for dygraph to run, using python ``with`` statement. Parameters: - place(fluid.CPUPlace| fluid.CUDAPlace|str, optional): Place to execute dygraph. + place(base.CPUPlace| base.CUDAPlace|str, optional): Place to execute dygraph. If None, the running place will be determined according to the way of paddle compilation. If ``place`` is string, It can be ``cpu``, ``gpu:x`` and ``xpu:x``, where ``x`` is the index of the GPUs or XPUs. Default: None @@ -568,13 +568,13 @@ def guard(place=None): .. code-block:: python import numpy as np - import paddle.fluid as fluid + import paddle.base as base - with fluid.dygraph.guard(): + with base.dygraph.guard(): inp = np.ones([3, 1024], dtype='float32') - t = fluid.dygraph.base.to_variable(inp) - linear1 = fluid.Linear(1024, 4, bias_attr=False) - linear2 = fluid.Linear(4, 4) + t = base.dygraph.base.to_variable(inp) + linear1 = base.Linear(1024, 4, bias_attr=False) + linear2 = base.Linear(4, 4) ret = linear1(t) dy_ret = linear2(ret) @@ -859,25 +859,25 @@ def to_variable(value, name=None, zero_copy=None, dtype=None): .. code-block:: python import numpy as np - import paddle.fluid as fluid + import paddle.base as base - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x = np.ones([2, 2], np.float32) - y = fluid.dygraph.to_variable(x, zero_copy=False) + y = base.dygraph.to_variable(x, zero_copy=False) x[0][0] = -1 y[0][0].numpy() # array([1.], dtype=float32) - y = fluid.dygraph.to_variable(x) + y = base.dygraph.to_variable(x) x[0][0] = 0 y[0][0].numpy() # array([0.], dtype=float32) c = np.array([2+1j, 2]) - z = fluid.dygraph.to_variable(c) + z = base.dygraph.to_variable(c) z.numpy() # array([2.+1.j, 2.+0.j]) z.dtype # 'complex128' - y = fluid.dygraph.to_variable([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]]) + y = base.dygraph.to_variable([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]]) y.shape # [3L, 2L] - y = fluid.dygraph.to_variable(((0.1, 1.2), (2.2, 3.1), (4.9, 5.2)), dtype='int32') + y = base.dygraph.to_variable(((0.1, 1.2), (2.2, 3.1), (4.9, 5.2)), dtype='int32') y.shape # [3L, 2L] """ @@ -892,7 +892,7 @@ def to_variable(value, name=None, zero_copy=None, dtype=None): ) if not isinstance(value, support_type): raise TypeError( - "The type of 'value' in fluid.dygraph.to_variable must be %s, but received %s." + "The type of 'value' in base.dygraph.to_variable must be %s, but received %s." % (support_type, type(value)) ) if isinstance(value, (core.eager.Tensor, framework.Variable)): diff --git a/python/paddle/fluid/dygraph/math_op_patch.py b/python/paddle/base/dygraph/math_op_patch.py similarity index 100% rename from python/paddle/fluid/dygraph/math_op_patch.py rename to python/paddle/base/dygraph/math_op_patch.py diff --git a/python/paddle/fluid/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py similarity index 98% rename from python/paddle/fluid/dygraph/tensor_patch_methods.py rename to python/paddle/base/dygraph/tensor_patch_methods.py index 0ba734237ffc8..2db290e1e1d4e 100644 --- a/python/paddle/fluid/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -34,7 +34,7 @@ ) from .base import switch_to_static_graph from .math_op_patch import monkey_patch_math_tensor -from paddle.fluid.data_feeder import ( +from paddle.base.data_feeder import ( convert_uint16_to_float, _PADDLE_DTYPE_2_NUMPY_DTYPE, ) @@ -43,7 +43,7 @@ from paddle.profiler.utils import in_profiler_mode from paddle import _C_ops, _legacy_C_ops from paddle.device import get_all_custom_device_type -from paddle.fluid.framework import _global_flags +from paddle.base.framework import _global_flags _grad_scalar = None @@ -100,12 +100,12 @@ def _to_static_var(self, to_parameter=False, **kwargs): Examples: .. code-block:: python - import paddle.fluid as fluid - from paddle.fluid.dygraph.base import to_variable + import paddle.base as base + from paddle.base.dygraph.base import to_variable import numpy as np data = np.ones([3, 1024], dtype='float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): tensor = to_variable(data) static_var = tensor._to_static_var() @@ -172,13 +172,13 @@ def set_value(self, value): Examples: .. code-block:: python - import paddle.fluid as fluid - from paddle.fluid.dygraph.base import to_variable + import paddle.base as base + from paddle.base.dygraph.base import to_variable from paddle.nn import Linear import numpy as np data = np.ones([3, 1024], dtype='float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear = Linear(1024, 4) t = to_variable(data) linear(t) # call with default weight @@ -480,7 +480,7 @@ def transform(t, device, dtype, blocking): # 2. cast Tensor to dtype if dtype is not None and dtype != t_used.dtype: - with paddle.fluid.framework._dygraph_place_guard( + with paddle.base.framework._dygraph_place_guard( place=t_used.place ): t_casted = t_used.cast(dtype=dtype) @@ -999,7 +999,7 @@ def dtype_str(dtype): prefix = 'paddle.' return prefix + numpy_dtype else: - # for example, paddle.fluid.core.VarDesc.VarType.LOD_TENSOR + # for example, paddle.base.core.VarDesc.VarType.LOD_TENSOR return origin(dtype) setattr(core.VarDesc.VarType, "__str__", dtype_str) diff --git a/python/paddle/fluid/dygraph/tracer.py b/python/paddle/base/dygraph/tracer.py similarity index 99% rename from python/paddle/fluid/dygraph/tracer.py rename to python/paddle/base/dygraph/tracer.py index f079d1946124f..34493a0ffa90a 100644 --- a/python/paddle/fluid/dygraph/tracer.py +++ b/python/paddle/base/dygraph/tracer.py @@ -16,8 +16,8 @@ import numpy as np -from paddle.fluid import core -from paddle.fluid import framework +from paddle.base import core +from paddle.base import framework from paddle import _C_ops, _legacy_C_ops name_mapping = { diff --git a/python/paddle/fluid/dygraph_utils.py b/python/paddle/base/dygraph_utils.py similarity index 100% rename from python/paddle/fluid/dygraph_utils.py rename to python/paddle/base/dygraph_utils.py diff --git a/python/paddle/fluid/executor.py b/python/paddle/base/executor.py similarity index 98% rename from python/paddle/fluid/executor.py rename to python/paddle/base/executor.py index 8ab4c13f40542..830590d3394c7 100755 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/base/executor.py @@ -131,14 +131,14 @@ def as_numpy(tensor, copy=False): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy - new_scope = fluid.Scope() - with fluid.scope_guard(new_scope): - fluid.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), fluid.CPUPlace()) + new_scope = base.Scope() + with base.scope_guard(new_scope): + base.global_scope().var("data").get_tensor().set(numpy.ones((2, 2)), base.CPUPlace()) tensor = new_scope.find_var("data").get_tensor() - fluid.executor.as_numpy(tensor) # or numpy.array(new_scope.find_var("data").get_tensor()) + base.executor.as_numpy(tensor) # or numpy.array(new_scope.find_var("data").get_tensor()) Args: tensor(Variable): a instance of Tensor @@ -679,11 +679,11 @@ def _as_lodtensor(data, place, dtype=None): For higher dimensional sequence data, please use LoDTensor directly. Examples: - >>> import paddle.fluid as fluid - >>> place = fluid.CPUPlace() - >>> exe = fluid.executor(place) + >>> import paddle.base as base + >>> place = base.CPUPlace() + >>> exe = base.executor(place) >>> data = np.array(size=(100, 200, 300)) - >>> np_outs = map(lambda x: fluid.executor._as_lodtensor(x, place), data) + >>> np_outs = map(lambda x: base.executor._as_lodtensor(x, place), data) >>> ... Args: @@ -712,7 +712,7 @@ def _as_lodtensor(data, place, dtype=None): raise TypeError( "\n\tFaild to convert input data to a regular ndarray :\n\t* Usually " "this means the input data contains nested lists with different lengths. " - "Please consider using 'fluid.create_lod_tensor' to convert it to a LoD-Tensor." + "Please consider using 'base.create_lod_tensor' to convert it to a LoD-Tensor." ) data = data.astype(dtype) else: @@ -1724,7 +1724,7 @@ def _run_impl( else: error_info = ( "There are no operators in the program to be executed. " - "If you pass Program manually, please use fluid.program_guard " + "If you pass Program manually, please use base.program_guard " "to ensure the current Program is being used." ) warnings.warn(error_info) @@ -1915,7 +1915,7 @@ def _run_new_ir_impl( else: error_info = ( "There are no operators in the program to be executed. " - "If you pass Program manually, please use fluid.program_guard " + "If you pass Program manually, please use base.program_guard " "to ensure the current Program is being used." ) warnings.warn(error_info) @@ -2245,7 +2245,7 @@ def _run_from_dataset( for var in program.global_block().vars.values(): if var.is_data: data_vars.append(var) - dataset = paddle.fluid.DatasetFactory().create_dataset( + dataset = paddle.base.DatasetFactory().create_dataset( 'FileInstantDataset' ) dataset.set_batch_size(1) @@ -2270,7 +2270,7 @@ def _run_from_dataset( for var in program.global_block().vars.values(): if var.is_data: data_vars.append(var) - dataset = paddle.fluid.DatasetFactory().create_dataset( + dataset = paddle.base.DatasetFactory().create_dataset( 'InMemoryDataset' ) dataset.set_batch_size(1) @@ -2417,7 +2417,7 @@ def _get_dataset(): for var in program.global_block().vars.values(): if var.is_data: data_vars.append(var) - dataset = paddle.fluid.DatasetFactory().create_dataset( + dataset = paddle.base.DatasetFactory().create_dataset( 'FileInstantDataset' ) dataset.set_batch_size(1) @@ -2897,7 +2897,7 @@ def infer_from_dataset( fetch_handler=None, ): """ - Infer from a pre-defined Dataset. Dataset is defined in paddle.fluid.dataset. + Infer from a pre-defined Dataset. Dataset is defined in paddle.base.dataset. Given a program, either a program or compiled program, infer_from_dataset will consume all data samples in dataset. Input scope can be given by users. By default, scope is global_scope(). The total number of thread run in training is `thread`. @@ -2912,7 +2912,7 @@ def infer_from_dataset( Args: program(Program|CompiledProgram): the program that needs to be run, if not provided, then default_main_program (not compiled) will be used. - dataset(paddle.fluid.Dataset): dataset created outside this function, + dataset(paddle.base.Dataset): dataset created outside this function, a user should provide a well-defined dataset before calling this function. Please check the document of Dataset if needed. default is None scope(Scope): the scope used to run this program, you can switch it to different scope @@ -2940,7 +2940,7 @@ def infer_from_dataset( exe = paddle.static.Executor(place) x = paddle.static.data(name="x", shape=[None, 10, 10], dtype="int64") y = paddle.static.data(name="y", shape=[None, 1], dtype="int64", lod_level=1) - dataset = paddle.fluid.DatasetFactory().create_dataset() + dataset = paddle.base.DatasetFactory().create_dataset() dataset.set_use_var([x, y]) dataset.set_thread(1) # you should set your own filelist, e.g. filelist = ["dataA.txt"] @@ -3021,7 +3021,7 @@ def train_from_dataset( fetch_handler=None, ): """ - Train from a pre-defined Dataset. Dataset is defined in paddle.fluid.dataset. + Train from a pre-defined Dataset. Dataset is defined in paddle.base.dataset. Given a program, either a program or compiled program, train_from_dataset will consume all data samples in dataset. Input scope can be given by users. By default, scope is global_scope(). The total number of thread run in training is `thread`. @@ -3034,7 +3034,7 @@ def train_from_dataset( Args: program(Program|CompiledProgram): the program that needs to be run, if not provided, then default_main_program (not compiled) will be used. - dataset(paddle.fluid.Dataset): dataset created outside this function, + dataset(paddle.base.Dataset): dataset created outside this function, a user should provide a well-defined dataset before calling this function. Please check the document of Dataset if needed. scope(Scope): the scope used to run this program, you can switch it to different scope @@ -3063,7 +3063,7 @@ def train_from_dataset( exe = paddle.static.Executor(place) x = paddle.static.data(name="x", shape=[None, 10, 10], dtype="int64") y = paddle.static.data(name="y", shape=[None, 1], dtype="int64", lod_level=1) - dataset = paddle.fluid.DatasetFactory().create_dataset() + dataset = paddle.base.DatasetFactory().create_dataset() dataset.set_use_var([x, y]) dataset.set_thread(1) # you should set your own filelist, e.g. filelist = ["dataA.txt"] diff --git a/python/paddle/fluid/framework.py b/python/paddle/base/framework.py similarity index 98% rename from python/paddle/fluid/framework.py rename to python/paddle/base/framework.py index b4442bcedf923..2fe4b5ce4a104 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/base/framework.py @@ -35,7 +35,7 @@ from . import core from . import unique_name from .. import ir -from paddle.fluid.libpaddle import DataType +from paddle.base.libpaddle import DataType import paddle.version as fluid_version import warnings import functools @@ -409,13 +409,13 @@ def require_version(min_version, max_version=None): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base # any version >= 0.1.0 is acceptable. - fluid.require_version('0.1.0') + base.require_version('0.1.0') # if 0.1.0 <= version <= 10.0.0, it is acceptable. - fluid.require_version(min_version='0.1.0', max_version='10.0.0') + base.require_version(min_version='0.1.0', max_version='10.0.0') """ if not isinstance(min_version, str): raise TypeError( @@ -576,7 +576,7 @@ def __impl__(*args, **kwargs): # NOTE(chenweihang): There is argument name typo (stat_dict, correct name is state_dict) -# in fluid api Layer.set_dict, Optimizer.load, in order to correct the argument without +# in base api Layer.set_dict, Optimizer.load, in order to correct the argument without # introducing compatibility issues, add this decorator # NOTE(chenweihang): not using `wrap_decorator` here is because `wrap_decorator` will # move kwargs to args, which doesn't work in this decorate case @@ -722,8 +722,8 @@ def is_compiled_with_xpu(): Examples: .. code-block:: python - import paddle.fluid as fluid - support_xpu = fluid.is_compiled_with_xpu() + import paddle.base as base + support_xpu = base.is_compiled_with_xpu() """ return core.is_compiled_with_xpu() @@ -925,7 +925,7 @@ def cpu_places(device_count=None): def cuda_pinned_places(device_count=None): """ - This function creates a list of :code:`fluid.CUDAPinnedPlace` objects. + This function creates a list of :code:`base.CUDAPinnedPlace` objects. If :code:`device_count` is None, the device count would be determined by environment variable :code:`CPU_NUM`. @@ -938,15 +938,15 @@ def cuda_pinned_places(device_count=None): device_count (int, optional): device number. Default: None. Returns: - list of fluid.CUDAPinnedPlace: Created list of CUDA pinned places. + list of base.CUDAPinnedPlace: Created list of CUDA pinned places. Examples: .. code-block:: python - import paddle.fluid as fluid - cuda_pinned_places_cpu_num = fluid.cuda_pinned_places() + import paddle.base as base + cuda_pinned_places_cpu_num = base.cuda_pinned_places() # or - cuda_pinned_places = fluid.cuda_pinned_places(1) + cuda_pinned_places = base.cuda_pinned_places(1) """ assert core.is_compiled_with_cuda(), "Not compiled with CUDA" @@ -1318,7 +1318,7 @@ class Variable(metaclass=VariableMetaClass): In Static Graph Mode: Please use ** `Block.create_var` ** to create a Static variable which has no data until being feed. - In Dygraph Mode: Please use ** :ref:`api_fluid_dygraph_to_variable` ** to create a dygraph variable with real data. + In Dygraph Mode: Please use ** :ref:`api_base_dygraph_to_variable` ** to create a dygraph variable with real data. In Fluid, every input and output of an OP is a variable. In most cases, variables are used for holding different kinds of data or training @@ -1326,7 +1326,7 @@ class Variable(metaclass=VariableMetaClass): two variables in different :ref:`api_guide_Block_en` could have the same name. There are many kinds of variables. Each kind of them has its own attributes - and usages. Please refer to the `framework.proto `_ for details. + and usages. Please refer to the `framework.proto `_ for details. Most of a Variable's member variables can be set to be None. It mean it is not available or will be specified later. @@ -1337,8 +1337,8 @@ class Variable(metaclass=VariableMetaClass): .. code-block:: python :name: code-example-1 - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -1349,11 +1349,11 @@ class Variable(metaclass=VariableMetaClass): .. code-block:: python :name: code-example-2 - import paddle.fluid as fluid + import paddle.base as base import numpy as np - with fluid.dygraph.guard(): - new_variable = fluid.dygraph.to_variable(np.arange(10)) + with base.dygraph.guard(): + new_variable = base.dygraph.to_variable(np.arange(10)) """ @@ -1535,13 +1535,13 @@ def numpy(self): Examples: .. code-block:: python - import paddle.fluid as fluid - from paddle.fluid.dygraph.base import to_variable - from paddle.fluid.dygraph import Linear + import paddle.base as base + from paddle.base.dygraph.base import to_variable + from paddle.base.dygraph import Linear import numpy as np data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear = Linear(32, 64) data = to_variable(data) x = linear(data) @@ -1614,15 +1614,15 @@ def gradient(self): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np # example1: return ndarray x = np.ones([2, 2], np.float32) - with fluid.dygraph.guard(): + with base.dygraph.guard(): inputs2 = [] for _ in range(10): - tmp = fluid.dygraph.base.to_variable(x) + tmp = base.dygraph.base.to_variable(x) tmp.stop_gradient=False inputs2.append(tmp) ret2 = paddle.add_n(inputs2) @@ -1631,7 +1631,7 @@ def gradient(self): print(loss2.gradient()) # example2: return tuple of ndarray - with fluid.dygraph.guard(): + with base.dygraph.guard(): embedding = paddle.nn.Embedding( 20, 32, @@ -1639,7 +1639,7 @@ def gradient(self): sparse=True) x_data = np.arange(12).reshape(4, 3).astype('int64') x_data = x_data.reshape((-1, 3, 1)) - x = fluid.dygraph.base.to_variable(x_data) + x = base.dygraph.base.to_variable(x_data) out = embedding(x) out.backward() print(embedding.weight.gradient()) @@ -1663,14 +1663,14 @@ def clear_gradient(self): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np x = np.ones([2, 2], np.float32) - with fluid.dygraph.guard(): + with base.dygraph.guard(): inputs2 = [] for _ in range(10): - tmp = fluid.dygraph.base.to_variable(x) + tmp = base.dygraph.base.to_variable(x) tmp.stop_gradient=False inputs2.append(tmp) ret2 = paddle.add_n(inputs2) @@ -1788,11 +1788,11 @@ def to_string(self, throw_on_error, with_details=False): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -1853,22 +1853,22 @@ def stop_gradient(self): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") - linear = fluid.Linear(13, 5, dtype="float32") - linear2 = fluid.Linear(3, 3, dtype="float32") - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + linear = base.Linear(13, 5, dtype="float32") + linear2 = base.Linear(3, 3, dtype="float32") + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) out2 = linear2(b) out1.stop_gradient = True - out = fluid.layers.concat(input=[out1, out2, c], axis=1) + out = base.layers.concat(input=[out1, out2, c], axis=1) out.backward() assert linear.weight.gradient() is None @@ -1895,8 +1895,8 @@ def persistable(self): Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -1944,8 +1944,8 @@ def name(self): Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -1988,8 +1988,8 @@ def shape(self): Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -2010,8 +2010,8 @@ def dtype(self): Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -2023,7 +2023,7 @@ def dtype(self): @property def lod_level(self): """ - Indicating ``LoD`` info of current Variable, please refer to :ref:`api_fluid_LoDTensor_en` to check the meaning + Indicating ``LoD`` info of current Variable, please refer to :ref:`api_base_LoDTensor_en` to check the meaning of ``LoD`` **Notes**: @@ -2036,10 +2036,10 @@ def lod_level(self): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -2062,8 +2062,8 @@ def type(self): Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() new_variable = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -2767,8 +2767,8 @@ class Operator: Examples: .. code-block:: python - import paddle.fluid as fluid - cur_program = fluid.Program() + import paddle.base as base + cur_program = base.Program() cur_block = cur_program.current_block() # var1 += var2 + var3 cur_block.append_op(type="sum", @@ -3122,9 +3122,9 @@ def _to_readable_code(self, skip_op_callstack=True): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() var = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -3653,7 +3653,7 @@ def check_if_to_static_diff_with_dygraph(op_type, inplace_map, outputs): and inplace_map.get("Input", None) == "Out" ): raise ValueError( - 'Sorry about what\'s happend. In to_static mode, %s\'s output variable %s is a viewed Tensor in dygraph. This will result in inconsistent calculation behavior between dynamic and static graphs. If you are sure it is safe, you can call with paddle.fluid.framework._stride_in_no_check_dy2st_diff() in your safe code block.' + 'Sorry about what\'s happend. In to_static mode, %s\'s output variable %s is a viewed Tensor in dygraph. This will result in inconsistent calculation behavior between dynamic and static graphs. If you are sure it is safe, you can call with paddle.base.framework._stride_in_no_check_dy2st_diff() in your safe code block.' % (op_type, k) ) elif isinstance(v, list): @@ -3664,7 +3664,7 @@ def check_if_to_static_diff_with_dygraph(op_type, inplace_map, outputs): and inplace_map.get("Input", None) == "Out" ): raise ValueError( - 'Sorry about what\'s happend. In to_static mode, %s\'s output variable %s is a viewed Tensor in dygraph. This will result in inconsistent calculation behavior between dynamic and static graphs. If you are sure it is safe, you can call with paddle.fluid.framework._stride_in_no_check_dy2st_diff() in your safe code block.' + 'Sorry about what\'s happend. In to_static mode, %s\'s output variable %s is a viewed Tensor in dygraph. This will result in inconsistent calculation behavior between dynamic and static graphs. If you are sure it is safe, you can call with paddle.base.framework._stride_in_no_check_dy2st_diff() in your safe code block.' % (op_type, k) ) @@ -3872,9 +3872,9 @@ class Block: Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() var = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -3911,9 +3911,9 @@ def _to_readable_code(self, skip_op_callstack=True): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() new_var = cur_block.create_var(name="X", shape=[-1, 23, 48], @@ -4292,7 +4292,7 @@ def append_op(self, *args, **kwargs): inplace_map, ) else: - from paddle.fluid.dygraph.base import param_guard + from paddle.base.dygraph.base import param_guard from paddle.utils import flatten def pass_stop_gradient(ins, outs): @@ -5534,11 +5534,11 @@ def _update_desc_attr(self, desc, name, val): class Program: """ Create Python Program. It has at least one :ref:`api_guide_Block_en`, when the - control flow op like conditional_block, while :ref:`api_paddle_fluid_layers_While` is included, + control flow op like conditional_block, while :ref:`api_paddle_base_layers_While` is included, it will contain nested block. Please reference the - `framework.proto `_ + `framework.proto `_ for details. A set of Program usually contains startup program and main program. @@ -5551,9 +5551,9 @@ class Program: backward ops and vars. **Notes**: - **we have** :ref:`api_paddle_fluid_framework_default_startup_program` **and** :ref:`api_paddle_fluid_framework_default_main_program` - **by default, a pair of them will shared the parameters. The** :ref:`api_paddle_fluid_framework_default_startup_program` **only run once to initialize parameters,** - :ref:`api_paddle_fluid_framework_default_main_program` **run in every mini batch and adjust the weights.** + **we have** :ref:`api_paddle_base_framework_default_startup_program` **and** :ref:`api_paddle_base_framework_default_main_program` + **by default, a pair of them will shared the parameters. The** :ref:`api_paddle_base_framework_default_startup_program` **only run once to initialize parameters,** + :ref:`api_paddle_base_framework_default_main_program` **run in every mini batch and adjust the weights.** Returns: Program: An empty Program. @@ -5857,7 +5857,7 @@ def _optimized_guard(self, param_and_grads): Examples: - >>> import paddle.fluid as fluid + >>> import paddle.base as base >>> p, g = backward(...) >>> with program._optimized_guard([p,g]): >>> p = p - 0.001 * g @@ -5893,7 +5893,7 @@ def _lr_schedule_guard(self, is_with_opt=False): Examples: - >>> import paddle.fluid as fluid + >>> import paddle.base as base >>> p, g = backward(...) >>> with program.lr_schedule_guard(): >>> lr = lr * decay @@ -6062,7 +6062,7 @@ def clone(self, for_test=False): Create a new Program with forward content of original one when ``for_test=True``. Create a new Program as same as the original one when ``for_test=False``. - Some operators, e.g., :ref:`api_paddle_fluid_layers_batch_norm` , behave differently between + Some operators, e.g., :ref:`api_paddle_base_layers_batch_norm` , behave differently between training and testing. They have an attribute, :code:`is_test`, to control this behaviour. This method will change the :code:`is_test` attribute of them to :code:`True` when :code:`for_test=True`. @@ -7252,10 +7252,10 @@ def to_string(self, throw_on_error, with_details=False): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle - prog = fluid.default_main_program() + prog = base.default_main_program() rlt = paddle.static.data("fake_data", shape=[-1,1,1], dtype='float32') debug_str = prog.to_string(throw_on_error=True, with_details=False) print(debug_str) @@ -7466,7 +7466,7 @@ def default_startup_program(): The :code:`startup_program` will initialize the parameters by the OPs. This method will return the default or the current startup program. Users can use - :ref:`api_paddle_fluid_framework_program_guard` to switch :ref:`api_paddle_fluid_framework_Program` . + :ref:`api_paddle_base_framework_program_guard` to switch :ref:`api_paddle_base_framework_Program` . Returns: Program: current default startup program. @@ -7499,7 +7499,7 @@ def default_main_program(): a lot of APIs. For example, the :code:`Executor.run()` will execute the :code:`default_main_program` when the program is not specified. - If you want to switch the ``default main program``, you can use :ref:`api_paddle_fluid_framework_program_guard` . + If you want to switch the ``default main program``, you can use :ref:`api_paddle_base_framework_program_guard` . Returns: Program: A ``Program`` which holding the descriptions of OPs and tensors in the network. diff --git a/python/paddle/fluid/incubate/__init__.py b/python/paddle/base/incubate/__init__.py similarity index 94% rename from python/paddle/fluid/incubate/__init__.py rename to python/paddle/base/incubate/__init__.py index 76c5c6391fde3..af8ad3be6630f 100644 --- a/python/paddle/fluid/incubate/__init__.py +++ b/python/paddle/base/incubate/__init__.py @@ -13,5 +13,5 @@ # incubate directory is mainly for internal use # after we have tested incubate APIs in industrial application for a period -# we will move stable functions into fluid +# we will move stable functions into base __version__ = '0.1.0' diff --git a/python/paddle/fluid/incubate/checkpoint/__init__.py b/python/paddle/base/incubate/checkpoint/__init__.py similarity index 100% rename from python/paddle/fluid/incubate/checkpoint/__init__.py rename to python/paddle/base/incubate/checkpoint/__init__.py diff --git a/python/paddle/fluid/incubate/checkpoint/auto_checkpoint.py b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py similarity index 99% rename from python/paddle/fluid/incubate/checkpoint/auto_checkpoint.py rename to python/paddle/base/incubate/checkpoint/auto_checkpoint.py index 58387d37064eb..7d07c4594acb4 100644 --- a/python/paddle/fluid/incubate/checkpoint/auto_checkpoint.py +++ b/python/paddle/base/incubate/checkpoint/auto_checkpoint.py @@ -22,9 +22,9 @@ from threading import Thread, current_thread from contextlib import contextmanager -from paddle.fluid import unique_name, compiler +from paddle.base import unique_name, compiler from .checkpoint_saver import SerializableBase, CheckpointSaver, PaddleModel -from paddle.fluid.framework import in_dygraph_mode, Program +from paddle.base.framework import in_dygraph_mode, Program g_train_epoch_range = None g_checker = None diff --git a/python/paddle/fluid/incubate/checkpoint/checkpoint_saver.py b/python/paddle/base/incubate/checkpoint/checkpoint_saver.py similarity index 100% rename from python/paddle/fluid/incubate/checkpoint/checkpoint_saver.py rename to python/paddle/base/incubate/checkpoint/checkpoint_saver.py diff --git a/python/paddle/fluid/initializer.py b/python/paddle/base/initializer.py similarity index 97% rename from python/paddle/fluid/initializer.py rename to python/paddle/base/initializer.py index 5eead87a995c9..c9d197a4f6840 100644 --- a/python/paddle/fluid/initializer.py +++ b/python/paddle/base/initializer.py @@ -58,7 +58,7 @@ def set_global_initializer(weight_init, bias_init=None): The model parameters include ``weight`` and ``bias`` . In the framework, they correspond to ``paddle.ParamAttr`` , which is inherited from ``paddle.Tensor`` , and is a persistable Variable. This API only takes effect for model parameters, not for variables created through apis such as - :ref:`api_fluid_layers_create_global_var` , :ref:`api_fluid_layers_create_tensor`. + :ref:`api_base_layers_create_global_var` , :ref:`api_base_layers_create_tensor`. If the initializer is also set up by ``param_attr`` or ``bias_attr`` when creating a network layer, the global initializer setting here will not take effect because it has a lower priority. diff --git a/python/paddle/fluid/io.py b/python/paddle/base/io.py similarity index 84% rename from python/paddle/fluid/io.py rename to python/paddle/base/io.py index 027e1ad7c438e..80855333b9165 100644 --- a/python/paddle/fluid/io.py +++ b/python/paddle/base/io.py @@ -25,9 +25,9 @@ import numpy as np import math import paddle -from paddle.fluid import layers -from paddle.fluid.executor import Executor, global_scope -from paddle.fluid.framework import ( +from paddle.base import layers +from paddle.base.executor import Executor, global_scope +from paddle.base.framework import ( Program, Parameter, default_main_program, @@ -50,14 +50,14 @@ multiprocess_reader, ) from .wrapped_decorator import signature_safe_contextmanager -from paddle.fluid.compiler import CompiledProgram -from paddle.fluid.log_helper import get_logger +from paddle.base.compiler import CompiledProgram +from paddle.base.log_helper import get_logger from . import reader from . import unique_name from .reader import * from . import core from paddle.utils import deprecated -from paddle.fluid.framework import static_only +from paddle.base.framework import static_only __all__ = reader.__all__ diff --git a/python/paddle/fluid/layer_helper.py b/python/paddle/base/layer_helper.py similarity index 100% rename from python/paddle/fluid/layer_helper.py rename to python/paddle/base/layer_helper.py diff --git a/python/paddle/fluid/layer_helper_base.py b/python/paddle/base/layer_helper_base.py similarity index 99% rename from python/paddle/fluid/layer_helper_base.py rename to python/paddle/base/layer_helper_base.py index 042e33a108eca..02419f474e102 100644 --- a/python/paddle/fluid/layer_helper_base.py +++ b/python/paddle/base/layer_helper_base.py @@ -79,11 +79,11 @@ def to_variable(self, value, name=None): .. code-block:: python import numpy as np - import paddle.fluid as fluid + import paddle.base as base - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = np.ones([2, 2], np.float32) - y = fluid.dygraph.to_variable(x) + y = base.dygraph.to_variable(x) """ if isinstance(value, np.ndarray): diff --git a/python/paddle/fluid/layers/__init__.py b/python/paddle/base/layers/__init__.py similarity index 100% rename from python/paddle/fluid/layers/__init__.py rename to python/paddle/base/layers/__init__.py diff --git a/python/paddle/fluid/layers/io.py b/python/paddle/base/layers/io.py similarity index 100% rename from python/paddle/fluid/layers/io.py rename to python/paddle/base/layers/io.py diff --git a/python/paddle/fluid/layers/layer_function_generator.py b/python/paddle/base/layers/layer_function_generator.py similarity index 99% rename from python/paddle/fluid/layers/layer_function_generator.py rename to python/paddle/base/layers/layer_function_generator.py index f44f00f41a46e..17cf2820f85d2 100644 --- a/python/paddle/fluid/layers/layer_function_generator.py +++ b/python/paddle/base/layers/layer_function_generator.py @@ -349,7 +349,7 @@ def func(x, name=None): func.__name__ = inplace_op_type func.__doc__ = """ Inplace version of ``{0}`` API, the output Tensor will be inplaced with input ``x``. -Please refer to :ref:`api_fluid_layers_{1}`. +Please refer to :ref:`api_base_layers_{1}`. """.format( origin_op_type, origin_op_type ) diff --git a/python/paddle/fluid/layers/math_op_patch.py b/python/paddle/base/layers/math_op_patch.py similarity index 98% rename from python/paddle/fluid/layers/math_op_patch.py rename to python/paddle/base/layers/math_op_patch.py index e95fbacf2f66a..d2858430a438d 100644 --- a/python/paddle/fluid/layers/math_op_patch.py +++ b/python/paddle/base/layers/math_op_patch.py @@ -18,7 +18,7 @@ from .. import core from ..framework import Variable, unique_name, static_only from .layer_function_generator import OpProtoHolder -from paddle.fluid.dygraph.base import in_declarative_mode +from paddle.base.dygraph.base import in_declarative_mode _supported_int_dtype_ = [ core.VarDesc.VarType.BOOL, @@ -241,7 +241,7 @@ def place(self): def astype(self, dtype): """ **Notes**: - **The variable must be a** :ref:`api_fluid_Tensor` + **The variable must be a** :ref:`api_base_Tensor` Cast a variable to a specified data type. @@ -259,11 +259,11 @@ def astype(self, dtype): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() - startup_prog = fluid.Program() - main_prog = fluid.Program() - with fluid.program_guard(startup_prog, main_prog): + startup_prog = base.Program() + main_prog = base.Program() + with base.program_guard(startup_prog, main_prog): original_variable = paddle.static.data(name = "new_variable", shape=[2,2], dtype='float32') new_variable = original_variable.astype('int64') print("new var's dtype is: {}".format(new_variable.dtype)) @@ -272,12 +272,12 @@ def astype(self, dtype): .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np x = np.ones([2, 2], np.float32) - with fluid.dygraph.guard(): - original_variable = fluid.dygraph.to_variable(x) + with base.dygraph.guard(): + original_variable = base.dygraph.to_variable(x) print("original var's dtype is: {}, numpy dtype is {}".format(original_variable.dtype, original_variable.numpy().dtype)) new_variable = original_variable.astype('int64') print("new var's dtype is: {}, numpy dtype is {}".format(new_variable.dtype, new_variable.numpy().dtype)) diff --git a/python/paddle/fluid/lod_tensor.py b/python/paddle/base/lod_tensor.py similarity index 93% rename from python/paddle/fluid/lod_tensor.py rename to python/paddle/base/lod_tensor.py index d099536fcd784..02058c0f576dc 100644 --- a/python/paddle/fluid/lod_tensor.py +++ b/python/paddle/base/lod_tensor.py @@ -62,10 +62,10 @@ def create_lod_tensor(data, recursive_seq_lens, place): .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import numpy as np - t = fluid.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], fluid.CPUPlace()) + t = base.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], base.CPUPlace()) """ if isinstance(data, core.LoDTensor): return create_lod_tensor(np.array(data), recursive_seq_lens, place) @@ -128,7 +128,7 @@ def create_random_int_lodtensor( :code:`base_shape` . 2. Create a numpy array of random integers, and parse the created numpy - array as parameter :code:`data` of :ref:`api_fluid_create_lod_tensor` to + array as parameter :code:`data` of :ref:`api_base_create_lod_tensor` to create the output LoDTensor. Suppose we want to create a LoDTensor to hold data for 2 sequences, where @@ -156,10 +156,10 @@ def create_random_int_lodtensor( Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base - t = fluid.create_random_int_lodtensor(recursive_seq_lens=[[2, 3]], - base_shape=[30], place=fluid.CPUPlace(), low=0, high=10) + t = base.create_random_int_lodtensor(recursive_seq_lens=[[2, 3]], + base_shape=[30], place=base.CPUPlace(), low=0, high=10) print(t.shape()) # [5, 30] """ assert isinstance(base_shape, list), "base_shape should be a list" diff --git a/python/paddle/fluid/log_helper.py b/python/paddle/base/log_helper.py similarity index 100% rename from python/paddle/fluid/log_helper.py rename to python/paddle/base/log_helper.py diff --git a/python/paddle/fluid/multiprocess_utils.py b/python/paddle/base/multiprocess_utils.py similarity index 100% rename from python/paddle/fluid/multiprocess_utils.py rename to python/paddle/base/multiprocess_utils.py diff --git a/python/paddle/fluid/param_attr.py b/python/paddle/base/param_attr.py similarity index 99% rename from python/paddle/fluid/param_attr.py rename to python/paddle/base/param_attr.py index 10f0523f19b60..15f2f89b12016 100644 --- a/python/paddle/fluid/param_attr.py +++ b/python/paddle/base/param_attr.py @@ -14,7 +14,7 @@ import paddle from paddle.regularizer import WeightDecayRegularizer -from paddle.fluid.data_feeder import check_type +from paddle.base.data_feeder import check_type __all__ = [ 'ParamAttr', diff --git a/python/paddle/fluid/reader.py b/python/paddle/base/reader.py similarity index 95% rename from python/paddle/fluid/reader.py rename to python/paddle/base/reader.py index c08fdedba3bcb..001c96bd7df26 100644 --- a/python/paddle/fluid/reader.py +++ b/python/paddle/base/reader.py @@ -46,7 +46,7 @@ ) from .unique_name import UniqueNameGenerator from .framework import _get_paddle_place, _get_paddle_place_list -from paddle.fluid.framework import _set_expected_place, _current_expected_place +from paddle.base.framework import _set_expected_place, _current_expected_place import logging import warnings @@ -147,7 +147,7 @@ def _check_input_array(cls, item): "this means the input data contains nested lists with different lengths. " "\n\t* Check the reader function passed to 'decorate_batch_generator'" " to locate the data causes this issue.\n\t* Please consider using " - "'fluid.create_lod_tensor' to convert it to a LoD-Tensor." + "'base.create_lod_tensor' to convert it to a LoD-Tensor." ) return arr @@ -327,7 +327,7 @@ def set_data_source(loader, places): label = static.data(name='label', shape=[None, 1], dtype='int64') # Define DataLoader - loader = paddle.fluid.io.DataLoader.from_generator(feed_list=[image, label], capacity=16, iterable=ITERABLE) + loader = paddle.base.io.DataLoader.from_generator(feed_list=[image, label], capacity=16, iterable=ITERABLE) # Define network loss = simple_net(image, label) @@ -401,7 +401,7 @@ def forward(self, x): adam = opt.Adam(learning_rate=0.001, parameters=dp_layer.parameters()) # create data loader - loader = paddle.fluid.io.DataLoader.from_generator(capacity=5) + loader = paddle.base.io.DataLoader.from_generator(capacity=5) loader.set_batch_generator(random_batch_reader()) for epoch_id in range(EPOCH_NUM): @@ -478,7 +478,7 @@ def from_dataset(dataset, places, drop_last=True): use_var=[image, label]) dataset.set_filelist(['a.txt', 'b.txt', 'c.txt']) - loader = paddle.fluid.io.DataLoader.from_dataset(dataset, static.cpu_places()) + loader = paddle.base.io.DataLoader.from_dataset(dataset, static.cpu_places()) """ return DatasetLoader(dataset, places, drop_last) @@ -1151,9 +1151,9 @@ class PyReader(DataLoaderBase): Examples: 1. If iterable = False, the created PyReader object is almost the - same as :code:`fluid.layers.py_reader()`. Operators would be + same as :code:`base.layers.py_reader()`. Operators would be inserted into the program. User should call :code:`start()` - before each epoch and catch :code:`fluid.core.EOFException` + before each epoch and catch :code:`base.core.EOFException` thrown by :code:`Executor.run()` when epoch ends. Once the exception is caught, user should call :code:`reset()` to reset the reader manually. @@ -1161,7 +1161,7 @@ class PyReader(DataLoaderBase): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -1191,7 +1191,7 @@ def reader(): image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = fluid.io.PyReader(feed_list=[image, label], + reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=False) @@ -1199,14 +1199,14 @@ def reader(): reader.decorate_sample_list_generator( paddle.batch(user_defined_reader, batch_size=BATCH_SIZE)) loss = network(image, label) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for i in range(EPOCH_NUM): reader.start() while True: try: executor.run(feed=None) - except fluid.core.EOFException: + except base.core.EOFException: reader.reset() break @@ -1220,7 +1220,7 @@ def reader(): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -1247,16 +1247,16 @@ def reader(): image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True, return_list=False) + reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True, return_list=False) user_defined_reader = reader_creator_random_image(784, 784) reader.decorate_sample_list_generator( paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), - fluid.core.CPUPlace()) + base.core.CPUPlace()) loss = network(image, label) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for _ in range(EPOCH_NUM): for data in reader(): @@ -1269,7 +1269,7 @@ def reader(): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np ITER_NUM = 5 @@ -1282,9 +1282,9 @@ def reader(): np.random.random_integers(low=0, high=9, size=[1]) return reader - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): - py_reader = fluid.io.PyReader(capacity=2, return_list=True) + place = base.CPUPlace() + with base.dygraph.guard(place): + py_reader = base.io.PyReader(capacity=2, return_list=True) user_defined_reader = reader_creator_random_image(784, 784) py_reader.decorate_sample_list_generator( paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), @@ -1328,7 +1328,7 @@ def start(self): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np BATCH_SIZE = 10 @@ -1338,18 +1338,18 @@ def generator(): yield np.random.uniform(low=0, high=255, size=[784, 784]), image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=False) + reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) reader.decorate_sample_list_generator( paddle.batch(generator, batch_size=BATCH_SIZE)) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for i in range(3): reader.start() while True: try: executor.run(feed=None) - except fluid.core.EOFException: + except base.core.EOFException: reader.reset() break @@ -1358,14 +1358,14 @@ def generator(): def reset(self): ''' - Reset the reader object when :code:`fluid.core.EOFException` raises. + Reset the reader object when :code:`base.core.EOFException` raises. Can only call when the reader object is not iterable. Example: .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np BATCH_SIZE = 10 @@ -1375,18 +1375,18 @@ def generator(): yield np.random.uniform(low=0, high=255, size=[784, 784]), image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - reader = fluid.io.PyReader(feed_list=[image], capacity=4, iterable=False) + reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) reader.decorate_sample_list_generator( paddle.batch(generator, batch_size=BATCH_SIZE)) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for i in range(3): reader.start() while True: try: executor.run(feed=None) - except fluid.core.EOFException: + except base.core.EOFException: reader.reset() break @@ -1420,7 +1420,7 @@ def decorate_sample_generator( .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np EPOCH_NUM = 3 @@ -1447,15 +1447,15 @@ def generator(): image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) user_defined_generator = random_image_and_label_generator(784, 784) reader.decorate_sample_generator(user_defined_generator, batch_size=BATCH_SIZE, - places=[fluid.CPUPlace()]) + places=[base.CPUPlace()]) loss = network(image, label) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for _ in range(EPOCH_NUM): for data in reader(): @@ -1485,7 +1485,7 @@ def decorate_sample_list_generator(self, reader, places=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -1514,16 +1514,16 @@ def generator(): image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) user_defined_generator = random_image_and_label_generator(784, 784) reader.decorate_sample_list_generator( paddle.batch(user_defined_generator, batch_size=BATCH_SIZE), - fluid.core.CPUPlace()) + base.core.CPUPlace()) loss = network(image, label) - executor = fluid.Executor(fluid.core.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.core.CPUPlace()) + executor.run(base.default_startup_program()) for _ in range(EPOCH_NUM): for data in reader(): @@ -1551,7 +1551,7 @@ def decorate_batch_generator(self, reader, places=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -1582,14 +1582,14 @@ def generator(): image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = fluid.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) user_defined_generator = random_image_and_label_generator(784, 784) - reader.decorate_batch_generator(user_defined_generator, fluid.CPUPlace()) + reader.decorate_batch_generator(user_defined_generator, base.CPUPlace()) loss = network(image, label) - executor = fluid.Executor(fluid.CPUPlace()) - executor.run(fluid.default_startup_program()) + executor = base.Executor(base.CPUPlace()) + executor.run(base.default_startup_program()) for _ in range(EPOCH_NUM): for data in reader(): diff --git a/python/paddle/fluid/trainer_desc.py b/python/paddle/base/trainer_desc.py similarity index 100% rename from python/paddle/fluid/trainer_desc.py rename to python/paddle/base/trainer_desc.py diff --git a/python/paddle/fluid/trainer_factory.py b/python/paddle/base/trainer_factory.py similarity index 99% rename from python/paddle/fluid/trainer_factory.py rename to python/paddle/base/trainer_factory.py index e06b48a079af9..859d8c27f71d2 100644 --- a/python/paddle/fluid/trainer_factory.py +++ b/python/paddle/base/trainer_factory.py @@ -17,7 +17,7 @@ import time import logging import numpy as np -from paddle.fluid.log_helper import get_logger +from paddle.base.log_helper import get_logger local_logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' diff --git a/python/paddle/fluid/unique_name.py b/python/paddle/base/unique_name.py similarity index 100% rename from python/paddle/fluid/unique_name.py rename to python/paddle/base/unique_name.py diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/base/variable_index.py similarity index 98% rename from python/paddle/fluid/variable_index.py rename to python/paddle/base/variable_index.py index bdc3d74050912..2efe308df0b83 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/base/variable_index.py @@ -73,10 +73,10 @@ def __init__(self): def update(self, index): if is_list_tuple(index, int) or isinstance( - index, (paddle.fluid.Variable, np.ndarray) + index, (paddle.base.Variable, np.ndarray) ): # convert index to Tensor - if not isinstance(index, paddle.fluid.Variable): + if not isinstance(index, paddle.base.Variable): index = paddle.assign(index) if self.dtype is None: @@ -123,7 +123,7 @@ def numel(self, shape): def get_offset_stride(self, tensor_shape): for index in self.indexes: - if not isinstance(index, paddle.fluid.Variable): + if not isinstance(index, paddle.base.Variable): raise ValueError( "only support list/tensor index, but received {}.".format( type(index) @@ -152,7 +152,7 @@ def get_item(self, tensor): return paddle.gather_nd(tensor, index) def set_item(self, tensor_origin, value): - if not isinstance(value, paddle.fluid.Variable): + if not isinstance(value, paddle.base.Variable): value = paddle.assign(value) tensor_type = None @@ -398,7 +398,7 @@ def _setitem_for_tensor_array(var, item, value): def _setitem_impl_(var, item, value): - from paddle.fluid import core + from paddle.base import core from .framework import default_main_program, Variable if var.type == core.VarDesc.VarType.LOD_TENSOR_ARRAY: @@ -565,7 +565,7 @@ def _setitem_impl_(var, item, value): var._bump_inplace_version() output = var else: - helper = paddle.fluid.layer_helper.LayerHelper('set_value', **locals()) + helper = paddle.base.layer_helper.LayerHelper('set_value', **locals()) if helper.main_program.current_block_idx != 0: # not in global block, we should create a global variable. output = helper._create_global_variable_for_type_inference( @@ -786,7 +786,7 @@ def parse_index(x, indices): has_advanced_index = True estimated_dim += 1 - elif isinstance(slice_item, paddle.fluid.Variable): + elif isinstance(slice_item, paddle.base.Variable): # In this case, the Variable is not 0-dim Tensor and will be treated as advanced-indexing. if slice_item.dtype == paddle.bool: if slice_item.ndim == 0: @@ -816,7 +816,7 @@ def parse_index(x, indices): axes.append(dim) use_strided_slice = ( True - if (isinstance(step, paddle.fluid.Variable) or step != 1) + if (isinstance(step, paddle.base.Variable) or step != 1) else use_strided_slice ) return ( @@ -844,7 +844,7 @@ def _setitem_static(x, indices, values): """ from .framework import default_main_program, Variable - if x.type == paddle.fluid.core.VarDesc.VarType.LOD_TENSOR_ARRAY: + if x.type == paddle.base.core.VarDesc.VarType.LOD_TENSOR_ARRAY: return _setitem_for_tensor_array(x, indices, values) # step1: parsing the index and recording them @@ -912,7 +912,7 @@ def _setitem_static(x, indices, values): x._bump_inplace_version() output = x else: - helper = paddle.fluid.layer_helper.LayerHelper( + helper = paddle.base.layer_helper.LayerHelper( 'set_value', **locals() ) if helper.main_program.current_block_idx != 0: @@ -986,7 +986,7 @@ def _setitem_static(x, indices, values): x._bump_inplace_version() output = x else: - helper = paddle.fluid.layer_helper.LayerHelper( + helper = paddle.base.layer_helper.LayerHelper( 'set_value', **locals() ) if helper.main_program.current_block_idx != 0: diff --git a/python/paddle/fluid/wrapped_decorator.py b/python/paddle/base/wrapped_decorator.py similarity index 100% rename from python/paddle/fluid/wrapped_decorator.py rename to python/paddle/base/wrapped_decorator.py diff --git a/python/paddle/common_ops_import.py b/python/paddle/common_ops_import.py index 042934d66f775..3605d71c39e62 100644 --- a/python/paddle/common_ops_import.py +++ b/python/paddle/common_ops_import.py @@ -11,16 +11,16 @@ # 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 import 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 +from paddle import base # noqa: F401 +from paddle.base import core, dygraph_utils # noqa: F401 +from paddle.base.core import VarDesc # noqa: F401 +from paddle.base.data_feeder import ( # noqa: F401 check_dtype, check_type, check_variable_and_dtype, convert_dtype, ) -from paddle.fluid.framework import ( # noqa: F401 +from paddle.base.framework import ( # noqa: F401 OpProtoHolder, Variable, _create_tensor, @@ -31,8 +31,8 @@ dygraph_only, in_dygraph_mode, ) -from paddle.fluid.layer_helper import LayerHelper # noqa: F401 -from paddle.fluid.layers.layer_function_generator import ( # noqa: F401 +from paddle.base.layer_helper import LayerHelper # noqa: F401 +from paddle.base.layers.layer_function_generator import ( # noqa: F401 templatedoc, ) -from paddle.fluid.param_attr import ParamAttr # noqa: F401 +from paddle.base.param_attr import ParamAttr # noqa: F401 diff --git a/python/paddle/cost_model/cost_model.py b/python/paddle/cost_model/cost_model.py index 20f98e906e031..f976bed4704d3 100644 --- a/python/paddle/cost_model/cost_model.py +++ b/python/paddle/cost_model/cost_model.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import core +from paddle.base import core class CostModel: diff --git a/python/paddle/decomposition/decomp.py b/python/paddle/decomposition/decomp.py index c166c131d10cb..273b11f24c9e5 100644 --- a/python/paddle/decomposition/decomp.py +++ b/python/paddle/decomposition/decomp.py @@ -16,7 +16,7 @@ import typing from paddle import ir -from paddle.fluid.libpaddle.ir import Block, Program +from paddle.base.libpaddle.ir import Block, Program from paddle.framework import core from . import register diff --git a/python/paddle/device/__init__.py b/python/paddle/device/__init__.py index a7b7dd1be052f..764c84c325572 100644 --- a/python/paddle/device/__init__.py +++ b/python/paddle/device/__init__.py @@ -17,11 +17,11 @@ import os import ctypes import paddle -from paddle.fluid import core -from paddle.fluid import framework -from paddle.fluid.framework import is_compiled_with_cinn # noqa: F401 -from paddle.fluid.framework import is_compiled_with_cuda # noqa: F401 -from paddle.fluid.framework import is_compiled_with_rocm # noqa: F401 +from paddle.base import core +from paddle.base import framework +from paddle.base.framework import is_compiled_with_cinn # noqa: F401 +from paddle.base.framework import is_compiled_with_cuda # noqa: F401 +from paddle.base.framework import is_compiled_with_rocm # noqa: F401 from . import cuda from . import xpu @@ -854,8 +854,8 @@ def __enter__(self): self.src_prev_stream = current_stream(cur_stream.device) if self.src_prev_stream.device != cur_stream.device: - self.tmp_place = paddle.fluid.framework._current_expected_place() - paddle.fluid.framework._set_expected_place(cur_stream.device) + self.tmp_place = paddle.base.framework._current_expected_place() + paddle.base.framework._set_expected_place(cur_stream.device) self.dst_prev_stream = current_stream(cur_stream.device) set_stream(cur_stream) else: @@ -868,7 +868,7 @@ def __exit__(self, *args): if self.src_prev_stream.device != cur_stream.device: set_stream(self.dst_prev_stream) - paddle.fluid.framework._set_expected_place(self.tmp_place) + paddle.base.framework._set_expected_place(self.tmp_place) set_stream(self.src_prev_stream) else: set_stream(self.src_prev_stream) diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 7f3ea571fb2a5..d2d7d0e23477a 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -13,8 +13,8 @@ # limitations under the License. import paddle -from paddle.fluid import core -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base import core +from paddle.base.wrapped_decorator import signature_safe_contextmanager from paddle.utils import deprecated from .streams import Stream # noqa: F401 diff --git a/python/paddle/device/cuda/graphs.py b/python/paddle/device/cuda/graphs.py index 2983897381a85..598bf64a10387 100644 --- a/python/paddle/device/cuda/graphs.py +++ b/python/paddle/device/cuda/graphs.py @@ -16,15 +16,15 @@ import warnings import paddle -from paddle.fluid import core -from paddle.fluid.core import ( +from paddle.base import core +from paddle.base.core import ( CUDAPlace, is_compiled_with_cuda, is_compiled_with_rocm, ) if is_compiled_with_cuda() and not is_compiled_with_rocm(): - from paddle.fluid.core import CUDAGraph as CoreCUDAGraph + from paddle.base.core import CUDAGraph as CoreCUDAGraph def is_cuda_graph_supported(): return True @@ -82,7 +82,7 @@ def wrap_cuda_graph(function, mode="thread_local", memory_pool="default"): assert mode in ALL_MODES if not paddle.in_dynamic_mode(): # static graph mode - from paddle.fluid.framework import _cuda_graph_guard + from paddle.base.framework import _cuda_graph_guard global cuda_graph_id graph_id = str(cuda_graph_id) diff --git a/python/paddle/device/cuda/streams.py b/python/paddle/device/cuda/streams.py index 6d716fd9016c5..cdfa8cfe0230b 100644 --- a/python/paddle/device/cuda/streams.py +++ b/python/paddle/device/cuda/streams.py @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.core import CUDAEvent as Event # noqa: F401 -from paddle.fluid.core import CUDAStream as Stream # noqa: F401 +from paddle.base.core import CUDAEvent as Event # noqa: F401 +from paddle.base.core import CUDAStream as Stream # noqa: F401 diff --git a/python/paddle/device/xpu/__init__.py b/python/paddle/device/xpu/__init__.py index 9d0ff4b3ea63f..1567f0e6658a7 100644 --- a/python/paddle/device/xpu/__init__.py +++ b/python/paddle/device/xpu/__init__.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle.fluid import core +from paddle.base import core from paddle.utils import deprecated __all__ = [ diff --git a/python/paddle/distributed/auto_parallel/static/auto_align_tool.py b/python/paddle/distributed/auto_parallel/static/auto_align_tool.py index ad345497167cd..d7d98f75d80f1 100644 --- a/python/paddle/distributed/auto_parallel/static/auto_align_tool.py +++ b/python/paddle/distributed/auto_parallel/static/auto_align_tool.py @@ -21,6 +21,8 @@ import paddle import paddle.distributed as dist +from paddle.base import core +from paddle.base.framework import Program from paddle.distributed.auto_parallel.static.converter import Converter from paddle.distributed.auto_parallel.static.dist_context import ( get_default_distributed_context, @@ -30,8 +32,6 @@ is_forward_op, is_loss_op, ) -from paddle.fluid import core -from paddle.fluid.framework import Program from paddle.static.io import deserialize_program _valid_types = [ diff --git a/python/paddle/distributed/auto_parallel/static/cluster.py b/python/paddle/distributed/auto_parallel/static/cluster.py index 3145153893f47..47d9711367765 100644 --- a/python/paddle/distributed/auto_parallel/static/cluster.py +++ b/python/paddle/distributed/auto_parallel/static/cluster.py @@ -884,7 +884,7 @@ def is_by_json_config(json_config): gpu_name = os.getenv("PADDLE_XCCL_BACKEND", None) gpu_model = gpu_name memory = int( - paddle.fluid.core.libpaddle._get_device_total_memory(gpu_name) + paddle.base.core.libpaddle._get_device_total_memory(gpu_name) ) // (1000**3) else: gpu_info = paddle.device.cuda.get_device_properties() diff --git a/python/paddle/distributed/auto_parallel/static/completion.py b/python/paddle/distributed/auto_parallel/static/completion.py index acc7f512e7f31..2e7396dd596af 100644 --- a/python/paddle/distributed/auto_parallel/static/completion.py +++ b/python/paddle/distributed/auto_parallel/static/completion.py @@ -15,8 +15,8 @@ import copy import logging +from paddle.base.core import get_spmd_rule # noqa: F401 from paddle.distributed.fleet.meta_optimizers.common import OpRole -from paddle.fluid.core import get_spmd_rule # noqa: F401 from paddle.framework import core from ..process_mesh import ProcessMesh, compute_compatible_process_mesh diff --git a/python/paddle/distributed/auto_parallel/static/cost/base_cost.py b/python/paddle/distributed/auto_parallel/static/cost/base_cost.py index 197656d6ea845..58ab301ad99f8 100644 --- a/python/paddle/distributed/auto_parallel/static/cost/base_cost.py +++ b/python/paddle/distributed/auto_parallel/static/cost/base_cost.py @@ -928,9 +928,9 @@ def calc_time_by_modeling(op=None, desc=None, cluster=None): def calc_time_by_cost_model(op, cluster=None): """Calc op time by cost model and the unit is microsecond.""" - if not isinstance(op, paddle.fluid.framework.Operator): + if not isinstance(op, paddle.base.framework.Operator): raise TypeError( - "OP must be paddle.fluid.framework.Operator, but got {}.".format( + "OP must be paddle.base.framework.Operator, but got {}.".format( type(op) ) ) diff --git a/python/paddle/distributed/auto_parallel/static/cost_model.py b/python/paddle/distributed/auto_parallel/static/cost_model.py index 7a9ccd691d644..b3e5bce8ef58f 100644 --- a/python/paddle/distributed/auto_parallel/static/cost_model.py +++ b/python/paddle/distributed/auto_parallel/static/cost_model.py @@ -164,7 +164,7 @@ def __init__(self, node, node_type, id=None, is_bwd=False, is_optim=False): self.is_optim = is_optim def init_comp_cost(self, cost_data): - # TODO: improve fluid.CostModel for more specific cost_data + # TODO: improve base.CostModel for more specific cost_data op_id = self.node.desc.id() if op_id in cost_data.keys(): self.cost = cost_data[op_id] diff --git a/python/paddle/distributed/auto_parallel/static/dist_attribute.py b/python/paddle/distributed/auto_parallel/static/dist_attribute.py index d31df134d6b6a..a8ee0e313669a 100644 --- a/python/paddle/distributed/auto_parallel/static/dist_attribute.py +++ b/python/paddle/distributed/auto_parallel/static/dist_attribute.py @@ -12,6 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License -from paddle.fluid.core import DistTensorSpec # noqa: F401 -from paddle.fluid.core import OperatorDistAttr # noqa: F401 -from paddle.fluid.core import TensorDistAttr # noqa: F401 +from paddle.base.core import DistTensorSpec # noqa: F401 +from paddle.base.core import OperatorDistAttr # noqa: F401 +from paddle.base.core import TensorDistAttr # noqa: F401 diff --git a/python/paddle/distributed/auto_parallel/static/dist_loader.py b/python/paddle/distributed/auto_parallel/static/dist_loader.py index 8db3d21e3ad47..1069fa92f830f 100644 --- a/python/paddle/distributed/auto_parallel/static/dist_loader.py +++ b/python/paddle/distributed/auto_parallel/static/dist_loader.py @@ -199,7 +199,7 @@ def data_generator(): yield partial_data - dataloader = paddle.fluid.io.DataLoader.from_generator( + dataloader = paddle.base.io.DataLoader.from_generator( feed_list=self.feed_list, capacity=self.capacity, use_double_buffer=self.use_double_buffer, diff --git a/python/paddle/distributed/auto_parallel/static/engine.py b/python/paddle/distributed/auto_parallel/static/engine.py index ff0c3004cf605..3d7096441dac5 100644 --- a/python/paddle/distributed/auto_parallel/static/engine.py +++ b/python/paddle/distributed/auto_parallel/static/engine.py @@ -24,8 +24,8 @@ import paddle import paddle.distributed.auto_parallel.static.utils as auto_utils from paddle import static, utils +from paddle.base.executor import _to_name_str from paddle.distributed import fleet -from paddle.fluid.executor import _to_name_str from paddle.framework import IrGraph from paddle.framework import _current_expected_place as _get_device from paddle.framework import core, in_dynamic_mode diff --git a/python/paddle/distributed/auto_parallel/static/tuner/rule_based_tuner.py b/python/paddle/distributed/auto_parallel/static/tuner/rule_based_tuner.py index 79d3b7ef974e0..8e0c3855e477e 100644 --- a/python/paddle/distributed/auto_parallel/static/tuner/rule_based_tuner.py +++ b/python/paddle/distributed/auto_parallel/static/tuner/rule_based_tuner.py @@ -26,6 +26,9 @@ import numpy as np import paddle +from paddle.base import program_guard +from paddle.base.backward import append_backward +from paddle.base.framework import Parameter, unique_name from paddle.distributed.auto_parallel.process_mesh import ProcessMesh from paddle.distributed.auto_parallel.static.cluster_v2 import DeviceMesh from paddle.distributed.auto_parallel.static.completion import Completer @@ -48,9 +51,6 @@ print_program_with_dist_attr, ) from paddle.distributed.fleet.meta_optimizers.common import OpRole -from paddle.fluid import program_guard -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Parameter, unique_name from ....utils.log_utils import get_logger from ..graph import Graph diff --git a/python/paddle/distributed/auto_parallel/static/utils.py b/python/paddle/distributed/auto_parallel/static/utils.py index 8ec0ba2e09f98..79597fd693218 100644 --- a/python/paddle/distributed/auto_parallel/static/utils.py +++ b/python/paddle/distributed/auto_parallel/static/utils.py @@ -22,7 +22,7 @@ import numpy as np import paddle -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle.base.wrapped_decorator import wrap_decorator from paddle.framework import core from paddle.framework.io_utils import is_belong_to_optimizer, is_parameter from paddle.static import Variable @@ -546,7 +546,7 @@ def _check_param_dict(param_dict): "The type of key of 'param_dict' should be 'str', " "but got '{}'.".format(str(type(name))) ) - if not isinstance(value, paddle.fluid.LoDTensor): + if not isinstance(value, paddle.base.LoDTensor): raise TypeError( "The type of value of 'param_dict' should be 'LoDTensor', " "but got '{}'.".format(str(type(value))) @@ -988,7 +988,7 @@ def _merge_parameter_with_dist_attr(param_list, dist_attr): def _slice_parameter_with_dist_attr(param, dist_attr): """Slice parameter with distributed attribute""" param = ( - np.array(param) if isinstance(param, paddle.fluid.LoDTensor) else param + np.array(param) if isinstance(param, paddle.base.LoDTensor) else param ) dims_mapping = dist_attr["dims_mapping"] process_shape = dist_attr["process_shape"] @@ -2401,7 +2401,7 @@ def __impl__(*args, **kwargs): if paddle.framework.in_dynamic_mode(): return func(*args, **kwargs) else: - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): return func(*args, **kwargs) return __impl__ diff --git a/python/paddle/distributed/collective.py b/python/paddle/distributed/collective.py index 144be980a7599..5128e2657c605 100644 --- a/python/paddle/distributed/collective.py +++ b/python/paddle/distributed/collective.py @@ -17,7 +17,7 @@ import paddle # (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core +from paddle.base import core from paddle.framework import in_dynamic_mode from .communication.group import Group, _add_new_group, is_initialized diff --git a/python/paddle/distributed/communication/stream/all_gather.py b/python/paddle/distributed/communication/stream/all_gather.py index fc814956be998..165bf9690b6f2 100644 --- a/python/paddle/distributed/communication/stream/all_gather.py +++ b/python/paddle/distributed/communication/stream/all_gather.py @@ -15,8 +15,8 @@ import paddle import paddle.distributed as dist from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import _get_global_group -from paddle.fluid import data_feeder def _all_gather_into_tensor_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/all_reduce.py b/python/paddle/distributed/communication/stream/all_reduce.py index 970b060abd805..61ea16b769a7a 100644 --- a/python/paddle/distributed/communication/stream/all_reduce.py +++ b/python/paddle/distributed/communication/stream/all_reduce.py @@ -13,12 +13,12 @@ # limitations under the License. from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _warn_cur_rank_not_in_group, ) from paddle.distributed.communication.reduce import ReduceOp, _get_reduce_op -from paddle.fluid import data_feeder def _all_reduce_in_dygraph(tensor, op, group, sync_op, use_calc_stream): diff --git a/python/paddle/distributed/communication/stream/all_to_all.py b/python/paddle/distributed/communication/stream/all_to_all.py index ae1fad0109d30..656986270805c 100644 --- a/python/paddle/distributed/communication/stream/all_to_all.py +++ b/python/paddle/distributed/communication/stream/all_to_all.py @@ -15,11 +15,11 @@ import paddle import paddle.distributed as dist from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _warn_cur_rank_not_in_group, ) -from paddle.fluid import data_feeder def _all_to_all_tensor_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/broadcast.py b/python/paddle/distributed/communication/stream/broadcast.py index 573fd9368ffe5..2f02012098ec9 100644 --- a/python/paddle/distributed/communication/stream/broadcast.py +++ b/python/paddle/distributed/communication/stream/broadcast.py @@ -13,12 +13,12 @@ # limitations under the License. from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _get_or_throw_group_rank, _warn_cur_rank_not_in_group, ) -from paddle.fluid import data_feeder def _broadcast_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/recv.py b/python/paddle/distributed/communication/stream/recv.py index c34e2d790b21a..cb93ce8a47155 100644 --- a/python/paddle/distributed/communication/stream/recv.py +++ b/python/paddle/distributed/communication/stream/recv.py @@ -13,12 +13,12 @@ # limitations under the License. from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _get_or_throw_group_rank, _warn_cur_rank_not_in_group, ) -from paddle.fluid import data_feeder def _recv_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/reduce.py b/python/paddle/distributed/communication/stream/reduce.py index 5f50cdd45b503..93c69a701879e 100644 --- a/python/paddle/distributed/communication/stream/reduce.py +++ b/python/paddle/distributed/communication/stream/reduce.py @@ -13,13 +13,13 @@ # limitations under the License. from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _get_or_throw_group_rank, _warn_cur_rank_not_in_group, ) from paddle.distributed.communication.reduce import ReduceOp, _get_reduce_op -from paddle.fluid import data_feeder def _reduce_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/reduce_scatter.py b/python/paddle/distributed/communication/stream/reduce_scatter.py index 52f9d86f7a298..6b9d669469e8e 100644 --- a/python/paddle/distributed/communication/stream/reduce_scatter.py +++ b/python/paddle/distributed/communication/stream/reduce_scatter.py @@ -15,12 +15,12 @@ import paddle import paddle.distributed as dist from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _warn_cur_rank_not_in_group, ) from paddle.distributed.communication.reduce import ReduceOp, _get_reduce_op -from paddle.fluid import data_feeder def _reduce_scatter_tensor_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/scatter.py b/python/paddle/distributed/communication/stream/scatter.py index 2d6c4282cb586..84a03f44bdf3e 100644 --- a/python/paddle/distributed/communication/stream/scatter.py +++ b/python/paddle/distributed/communication/stream/scatter.py @@ -17,12 +17,12 @@ import paddle import paddle.distributed as dist from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _get_or_throw_group_rank, _warn_cur_rank_not_in_group, ) -from paddle.fluid import data_feeder def _scatter_tensor_in_dygraph( diff --git a/python/paddle/distributed/communication/stream/send.py b/python/paddle/distributed/communication/stream/send.py index 298cd76bbefea..d90d180783c85 100644 --- a/python/paddle/distributed/communication/stream/send.py +++ b/python/paddle/distributed/communication/stream/send.py @@ -13,12 +13,12 @@ # limitations under the License. from paddle import framework +from paddle.base import data_feeder from paddle.distributed.communication.group import ( _get_global_group, _get_or_throw_group_rank, _warn_cur_rank_not_in_group, ) -from paddle.fluid import data_feeder def _send_in_dygraph( diff --git a/python/paddle/distributed/fleet/base/distributed_strategy.py b/python/paddle/distributed/fleet/base/distributed_strategy.py index 5cb9480eca8b6..09e2ef82162bc 100755 --- a/python/paddle/distributed/fleet/base/distributed_strategy.py +++ b/python/paddle/distributed/fleet/base/distributed_strategy.py @@ -19,10 +19,10 @@ import google.protobuf.text_format import paddle +from paddle.base.framework import _global_flags +from paddle.base.wrapped_decorator import wrap_decorator from paddle.distributed.fleet.proto import distributed_strategy_pb2 from paddle.distributed.fleet.utils.log_util import logger -from paddle.fluid.framework import _global_flags -from paddle.fluid.wrapped_decorator import wrap_decorator __all__ = [] diff --git a/python/paddle/distributed/fleet/base/role_maker.py b/python/paddle/distributed/fleet/base/role_maker.py index 0b09fd082ba41..870f818d177a9 100755 --- a/python/paddle/distributed/fleet/base/role_maker.py +++ b/python/paddle/distributed/fleet/base/role_maker.py @@ -20,10 +20,10 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) -from paddle.fluid import core from ...backup_env import getenv_or_backup diff --git a/python/paddle/distributed/fleet/base/topology.py b/python/paddle/distributed/fleet/base/topology.py index bec592e6bb534..1bacdfb8df02e 100644 --- a/python/paddle/distributed/fleet/base/topology.py +++ b/python/paddle/distributed/fleet/base/topology.py @@ -26,7 +26,7 @@ _HYBRID_PARALLEL_GROUP = None _use_four_directions = os.environ.get( - 'PADDLE_USE_FOUR_DIRECTIONS_P2P', paddle.fluid.core.is_compiled_with_xpu() + 'PADDLE_USE_FOUR_DIRECTIONS_P2P', paddle.base.core.is_compiled_with_xpu() ) diff --git a/python/paddle/distributed/fleet/base/util_factory.py b/python/paddle/distributed/fleet/base/util_factory.py index 6415f95793ca1..0f7d37aba8a6b 100755 --- a/python/paddle/distributed/fleet/base/util_factory.py +++ b/python/paddle/distributed/fleet/base/util_factory.py @@ -26,8 +26,8 @@ import paddle from paddle import framework -from paddle.fluid import core -from paddle.fluid.proto import framework_pb2 +from paddle.base import core +from paddle.base.proto import framework_pb2 from paddle.static import Program from ..utils.fs import FS @@ -644,7 +644,7 @@ def check_not_expected_ops(prog, not_expected_op_types): dtype=feed_config.feeded_vars_types[i], ) feed_tensors.append( - paddle.fluid.create_lod_tensor( + paddle.base.create_lod_tensor( t, [[1] * config.batch_size], place ) ) @@ -673,7 +673,7 @@ def check_not_expected_ops(prog, not_expected_op_types): ) for i in range(len(feed_config.feeded_vars_names)) ] - feeder = paddle.fluid.DataFeeder( + feeder = paddle.base.DataFeeder( feed_list=feed_vars, place=place ) batch_feed = feed_gen( diff --git a/python/paddle/distributed/fleet/dataset/dataset.py b/python/paddle/distributed/fleet/dataset/dataset.py index 8cdd57a34248e..bdaa8cbb5ea08 100755 --- a/python/paddle/distributed/fleet/dataset/dataset.py +++ b/python/paddle/distributed/fleet/dataset/dataset.py @@ -15,8 +15,8 @@ from google.protobuf import text_format -from paddle.fluid import core -from paddle.fluid.proto import data_feed_pb2 +from paddle.base import core +from paddle.base.proto import data_feed_pb2 __all__ = [] diff --git a/python/paddle/distributed/fleet/dataset/index_dataset.py b/python/paddle/distributed/fleet/dataset/index_dataset.py index 7df2931b5d87d..4c02cb8d82c7a 100644 --- a/python/paddle/distributed/fleet/dataset/index_dataset.py +++ b/python/paddle/distributed/fleet/dataset/index_dataset.py @@ -11,7 +11,7 @@ # 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 import core +from paddle.base import core __all__ = [] diff --git a/python/paddle/distributed/fleet/fleet.py b/python/paddle/distributed/fleet/fleet.py index df0c39ee119ab..0b873cde8e6d9 100755 --- a/python/paddle/distributed/fleet/fleet.py +++ b/python/paddle/distributed/fleet/fleet.py @@ -16,8 +16,8 @@ import os import paddle -from paddle.fluid import compiler -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle.base import compiler +from paddle.base.wrapped_decorator import wrap_decorator from paddle.framework import _global_flags, in_dynamic_mode from paddle.framework.ir import apply_build_strategy @@ -1252,9 +1252,9 @@ def minimize( Args: loss (Tensor): A ``Tensor`` containing the value to minimize. - startup_program (Program, optional): :ref:`api_fluid_Program` for + startup_program (Program, optional): :ref:`api_base_Program` for initializing parameters in ``parameter_list``. The default value - is None, at this time :ref:`api_fluid_default_startup_program` will be used. + is None, at this time :ref:`api_base_default_startup_program` will be used. parameter_list (Iterable, optional): Iterable of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. diff --git a/python/paddle/distributed/fleet/layers/mpu/mp_layers.py b/python/paddle/distributed/fleet/layers/mpu/mp_layers.py index 61ccbbcc448b9..c24062c1f392b 100644 --- a/python/paddle/distributed/fleet/layers/mpu/mp_layers.py +++ b/python/paddle/distributed/fleet/layers/mpu/mp_layers.py @@ -14,8 +14,8 @@ import paddle from paddle.autograd import PyLayer +from paddle.base import core from paddle.distributed import fleet -from paddle.fluid import core from paddle.nn import functional as F from ....communication.reduce import ReduceOp, _get_reduce_op diff --git a/python/paddle/distributed/fleet/layers/mpu/mp_ops.py b/python/paddle/distributed/fleet/layers/mpu/mp_ops.py index ccfba7acafffd..27d8a32f0693f 100644 --- a/python/paddle/distributed/fleet/layers/mpu/mp_ops.py +++ b/python/paddle/distributed/fleet/layers/mpu/mp_ops.py @@ -15,8 +15,8 @@ import paddle from paddle import _legacy_C_ops from paddle.autograd import PyLayer +from paddle.base.data_feeder import check_dtype, check_variable_and_dtype from paddle.distributed import collective -from paddle.fluid.data_feeder import check_dtype, check_variable_and_dtype from paddle.framework import LayerHelper, _create_tensor, in_dynamic_mode from paddle.nn import Layer from paddle.nn.utils import dygraph_utils diff --git a/python/paddle/distributed/fleet/layers/mpu/random.py b/python/paddle/distributed/fleet/layers/mpu/random.py index c0d4f6320f4b6..5b43ef951cfff 100644 --- a/python/paddle/distributed/fleet/layers/mpu/random.py +++ b/python/paddle/distributed/fleet/layers/mpu/random.py @@ -18,9 +18,9 @@ import paddle from paddle import _legacy_C_ops +from paddle.base import core +from paddle.base.data_feeder import check_variable_and_dtype from paddle.common_ops_import import Variable -from paddle.fluid import core -from paddle.fluid.data_feeder import check_variable_and_dtype from paddle.framework import LayerHelper, in_dynamic_mode __all__ = [] diff --git a/python/paddle/distributed/fleet/meta_optimizers/dgc_optimizer.py b/python/paddle/distributed/fleet/meta_optimizers/dgc_optimizer.py index 4fa85994269a7..cee43657c7527 100644 --- a/python/paddle/distributed/fleet/meta_optimizers/dgc_optimizer.py +++ b/python/paddle/distributed/fleet/meta_optimizers/dgc_optimizer.py @@ -19,9 +19,9 @@ __all__ = [] import paddle +from paddle.base import framework +from paddle.base.dygraph import base as imperative_base from paddle.common_ops_import import LayerHelper -from paddle.fluid import framework -from paddle.fluid.dygraph import base as imperative_base from paddle.framework import core, in_dynamic_mode from paddle.nn.clip import ClipGradByNorm, append_gradient_clip_ops from paddle.optimizer import Momentum, Optimizer @@ -313,7 +313,7 @@ def _clip_by_norm(self, x, max_norm, name=None): helper = LayerHelper("dgc_clip_by_norm_op", **args) if name is None: - name = paddle.fluid.unique_name.generate_with_ignorable_key( + name = paddle.base.unique_name.generate_with_ignorable_key( ".".join([helper.name, 'tmp']) ) diff --git a/python/paddle/distributed/fleet/meta_optimizers/raw_program_optimizer.py b/python/paddle/distributed/fleet/meta_optimizers/raw_program_optimizer.py index a1ab474723527..36061659c9cef 100755 --- a/python/paddle/distributed/fleet/meta_optimizers/raw_program_optimizer.py +++ b/python/paddle/distributed/fleet/meta_optimizers/raw_program_optimizer.py @@ -14,7 +14,7 @@ import os from paddle import static -from paddle.fluid import core +from paddle.base import core from paddle.framework.ir import apply_build_strategy from paddle.utils import unique_name diff --git a/python/paddle/distributed/fleet/meta_optimizers/sharding/utils.py b/python/paddle/distributed/fleet/meta_optimizers/sharding/utils.py index c15803e583771..1f7a497b3dfea 100755 --- a/python/paddle/distributed/fleet/meta_optimizers/sharding/utils.py +++ b/python/paddle/distributed/fleet/meta_optimizers/sharding/utils.py @@ -1034,7 +1034,7 @@ def is_gradient_merge_vars(var): def is_trainable(var): return ( - isinstance(var, paddle.fluid.framework.Parameter) and var.trainable + isinstance(var, paddle.base.framework.Parameter) and var.trainable ) def sharding_predicate(var): diff --git a/python/paddle/distributed/fleet/meta_optimizers/sharding_optimizer.py b/python/paddle/distributed/fleet/meta_optimizers/sharding_optimizer.py index ad126d5348633..884cd16ecb5da 100755 --- a/python/paddle/distributed/fleet/meta_optimizers/sharding_optimizer.py +++ b/python/paddle/distributed/fleet/meta_optimizers/sharding_optimizer.py @@ -14,7 +14,7 @@ import os -from paddle.fluid import core +from paddle.base import core from paddle.incubate.optimizer import PipelineOptimizer from paddle.static import ( create_global_var, diff --git a/python/paddle/distributed/fleet/meta_parallel/pipeline_parallel.py b/python/paddle/distributed/fleet/meta_parallel/pipeline_parallel.py index c194ab8d4e5e2..367169aead956 100755 --- a/python/paddle/distributed/fleet/meta_parallel/pipeline_parallel.py +++ b/python/paddle/distributed/fleet/meta_parallel/pipeline_parallel.py @@ -31,7 +31,7 @@ from .parallel_layers.pp_layers import PipelineLayer _use_four_directions = os.environ.get( - 'PADDLE_USE_FOUR_DIRECTIONS_P2P', paddle.fluid.core.is_compiled_with_xpu() + 'PADDLE_USE_FOUR_DIRECTIONS_P2P', paddle.base.core.is_compiled_with_xpu() ) if _use_four_directions: from .pp_utils import four_directions_p2p_communication as p2p diff --git a/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_stage3.py b/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_stage3.py index b9ca53aeef0a1..3b342778442c2 100644 --- a/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_stage3.py +++ b/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_stage3.py @@ -22,8 +22,8 @@ import paddle.distributed as dist from paddle import framework, nn from paddle.autograd import PyLayer +from paddle.base.framework import EagerParamBase from paddle.distributed import collective -from paddle.fluid.framework import EagerParamBase from paddle.framework import core from paddle.nn import ClipGradByGlobalNorm diff --git a/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_utils.py b/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_utils.py index 723d3a177124e..adfad3ec77e31 100644 --- a/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_utils.py +++ b/python/paddle/distributed/fleet/meta_parallel/sharding/group_sharded_utils.py @@ -20,9 +20,9 @@ import paddle from paddle import _C_ops, _legacy_C_ops +from paddle.base import core +from paddle.base.dygraph import to_variable from paddle.common_ops_import import dygraph_only -from paddle.fluid import core -from paddle.fluid.dygraph import to_variable from paddle.nn import clip diff --git a/python/paddle/distributed/fleet/recompute/recompute.py b/python/paddle/distributed/fleet/recompute/recompute.py index ba22372e79147..43e4dde69440a 100755 --- a/python/paddle/distributed/fleet/recompute/recompute.py +++ b/python/paddle/distributed/fleet/recompute/recompute.py @@ -170,7 +170,7 @@ def forward(ctx, run_function, preserve_rng_state, *args, **kwargs): @staticmethod def backward(ctx, *args): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # TODO need to check the recompute calling is vaild or not # Restore inputs diff --git a/python/paddle/distributed/fleet/recompute/recompute_hybrid.py b/python/paddle/distributed/fleet/recompute/recompute_hybrid.py index 48dd678a22a9d..bdbdc797b2b7b 100644 --- a/python/paddle/distributed/fleet/recompute/recompute_hybrid.py +++ b/python/paddle/distributed/fleet/recompute/recompute_hybrid.py @@ -175,7 +175,7 @@ def forward( @staticmethod def backward(ctx, *args): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # Restore inputs inputs = list(ctx.inputs) tensor_indices = ctx.tensor_indices diff --git a/python/paddle/distributed/fleet/runtime/the_one_ps.py b/python/paddle/distributed/fleet/runtime/the_one_ps.py index 8bc7ea5fbf5d3..43440ba37fc3a 100644 --- a/python/paddle/distributed/fleet/runtime/the_one_ps.py +++ b/python/paddle/distributed/fleet/runtime/the_one_ps.py @@ -15,11 +15,11 @@ import os import warnings -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.compiler import CompiledProgram -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program +from paddle import base +from paddle.base import core +from paddle.base.compiler import CompiledProgram +from paddle.base.executor import Executor +from paddle.base.framework import Program from ..base.private_helper_function import wait_server_ready from .runtime_base import RuntimeBase @@ -676,7 +676,7 @@ def __init__(self): super().__init__() self._communicator = None self._server = None - self._worker = fluid.core.DistFleetWrapper() + self._worker = base.core.DistFleetWrapper() self._server_sub_program = [] self._heter_client = None @@ -772,7 +772,7 @@ def sync_strategy_envs(): string_hosts = [] for idx, ep in enumerate(endpoints): host, port = ep.split(":") - pshost = fluid.core.PSHost(host, int(port), idx) + pshost = base.core.PSHost(host, int(port), idx) string_hosts.append(pshost.serialize_to_string()) dense_map = self.compiled_strategy.get_the_one_recv_context( @@ -816,7 +816,7 @@ def sync_strategy_envs(): trainer_config.mode, kwargs, trainer_config.get_communicator_flags() ) self._communicator.init_with_ctx( - send_ctx, dense_map, proto_txt, string_hosts, fluid.global_scope() + send_ctx, dense_map, proto_txt, string_hosts, base.global_scope() ) from paddle.distributed import fleet @@ -887,12 +887,12 @@ def sync_strategy_envs(): ) def _push_sparse_param( - self, var_name, table_id=-1, scope=fluid.global_scope() + self, var_name, table_id=-1, scope=base.global_scope() ): self._communicator.push_sparse_param(var_name, table_id, scope) def _get_executor(self): - executor = fluid.Executor(fluid.CPUPlace()) + executor = base.Executor(base.CPUPlace()) if self.role_maker._is_heter_parameter_server_mode: if self.role_maker._is_heter_worker(): heter_device_type = self.role_maker._heter_device_type().upper() @@ -904,13 +904,13 @@ def _get_executor(self): ) if heter_device_type == "GPU": executor = Executor( - fluid.CUDAPlace( + base.CUDAPlace( int(os.getenv("FLAGS_selected_gpus", "0")) ) ) elif heter_device_type == "XPU": executor = Executor( - fluid.XPUPlace( + base.XPUPlace( int(os.getenv("FLAGS_selected_xpus", "0")) ) ) @@ -1182,10 +1182,10 @@ def _init_server(self, dirname=None, var_names=None, **kwargs): string_hosts = [] for idx, ep in enumerate(endpoints): host, port = ep.split(":") - pshost = fluid.core.PSHost(host, int(port), idx) + pshost = base.core.PSHost(host, int(port), idx) string_hosts.append(pshost.serialize_to_string()) - self._server = fluid.core.DistFleetWrapper() + self._server = base.core.DistFleetWrapper() self._server.init_server( proto_txt, string_hosts, role_id, trainers, self._server_sub_program ) diff --git a/python/paddle/distributed/fleet/scaler.py b/python/paddle/distributed/fleet/scaler.py index 9423c95e99390..bf0d7363b0525 100755 --- a/python/paddle/distributed/fleet/scaler.py +++ b/python/paddle/distributed/fleet/scaler.py @@ -18,8 +18,8 @@ import paddle from paddle import _C_ops, _legacy_C_ops +from paddle.base.dygraph import to_variable from paddle.distributed import fleet -from paddle.fluid.dygraph import to_variable from paddle.framework import core from .base.topology import ParallelMode diff --git a/python/paddle/distributed/fleet/utils/fs.py b/python/paddle/distributed/fleet/utils/fs.py index a4f95340cb88e..770cef9c551e6 100644 --- a/python/paddle/distributed/fleet/utils/fs.py +++ b/python/paddle/distributed/fleet/utils/fs.py @@ -21,7 +21,7 @@ import time # (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core +from paddle.base import core from .log_util import logger diff --git a/python/paddle/distributed/fleet/utils/hybrid_parallel_inference.py b/python/paddle/distributed/fleet/utils/hybrid_parallel_inference.py index 2c49cbd46619f..a5723f856e661 100644 --- a/python/paddle/distributed/fleet/utils/hybrid_parallel_inference.py +++ b/python/paddle/distributed/fleet/utils/hybrid_parallel_inference.py @@ -16,10 +16,9 @@ import numpy as np -from paddle.distributed import fleet - # (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core +from paddle.base import core +from paddle.distributed import fleet from paddle.framework import Block, Program, in_dynamic_mode @@ -47,7 +46,7 @@ class HybridParallelInferenceHelper: :name: bash-example1 # while op pattern - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # init global cond max_len = paddle.full(shape=[1], dtype="int64", fill_value=10) step_idx = paddle.full(shape=[1], dtype="int64", fill_value=0) @@ -59,7 +58,7 @@ class HybridParallelInferenceHelper: arr = paddle.tensor.array_write(data, step_idx) with while_op.block(): - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # read data from global lod_tensor_array element_in_arr = paddle.tensor.array_read(array=arr, i=step_idx) # write placehold data to global lod_tensor_array, @@ -67,13 +66,13 @@ class HybridParallelInferenceHelper: paddle.increment(x=step_idx, value=1.0) paddle.tensor.array_write(element_in_arr, i=step_idx, array=arr) - with paddle.fluid.device_guard(f'{device}:0'): + with paddle.base.device_guard(f'{device}:0'): ... some code - with paddle.fluid.device_guard(f'{device}:1'): + with paddle.base.device_guard(f'{device}:1'): ... some code - with paddle.fluid.device_guard(f'{device}:{num_pp-1}'): + with paddle.base.device_guard(f'{device}:{num_pp-1}'): # generate some data in while block and write to global lod_tensor_array # that they are read in next while step. # we will using send_v2 to send global lod_tensor_array to other pipeline and sync @@ -82,11 +81,11 @@ class HybridParallelInferenceHelper: # update cond and assign to cond_int, we will sync cond_int layers.assign(layers.cast(cond, dtype="int32"), cond_int) - with paddle.fluid.device_guard(f'{model._device}:all'): + with paddle.base.device_guard(f'{model._device}:all'): # the code below must at end of while block and exists in device:all layers.assign(layers.cast(cond_int, dtype='bool'), cond) - with paddle.fluid.device_guard(f'{model._device}:all'): + with paddle.base.device_guard(f'{model._device}:all'): # use a empty lod_tensor_array to clear lod_tensor_array layers.assign(layers.create_array(data.dtype), arr) @@ -100,7 +99,7 @@ class HybridParallelInferenceHelper: import os import numpy as np import paddle - import paddle.fluid.layers as layers + import paddle.base.layers as layers import paddle.distributed.fleet as fleet paddle.enable_static() @@ -119,10 +118,10 @@ class HybridParallelInferenceHelper: device = "gpu" with paddle.static.program_guard(main_program, startup_program): - with paddle.fluid.device_guard(f'{device}:0'): + with paddle.base.device_guard(f'{device}:0'): X = paddle.static.data(name='X', shape=[None, 2], dtype='float32') - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): max_len = paddle.full( shape=[1], dtype="int64", fill_value=5, name="n") step_idx = paddle.full( @@ -135,18 +134,18 @@ class HybridParallelInferenceHelper: while_op = layers.While(cond, is_test=True) with while_op.block(): - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): input = paddle.tensor.array_read(array=data, i=step_idx) paddle.increment(x=step_idx, value=1.0) paddle.tensor.array_write(input, i=step_idx, array=data) - with paddle.fluid.device_guard(f'{device}:0'): + with paddle.base.device_guard(f'{device}:0'): param_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(1.0)) weight1 = paddle.static.create_parameter( shape=[2, 5], dtype='float32', attr=param_attr, is_bias=False) hidden1 = paddle.matmul(input, weight1) - with paddle.fluid.device_guard(f'{device}:1'): + with paddle.base.device_guard(f'{device}:1'): param_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Constant(2.0)) weight2 = paddle.static.create_parameter( shape=[5, 2], dtype='float32', attr=param_attr, is_bias=False) @@ -158,15 +157,15 @@ class HybridParallelInferenceHelper: paddle.assign(paddle.less_than(x=step_idx, y=max_len), cond) layers.assign(layers.cast(cond, dtype="int32"), cond_int) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # the code below must at end of while block and exists in device:all layers.assign(layers.cast(cond_int, dtype='bool'), cond) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): out = layers.create_array(data.dtype) layers.assign(data, out) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # use a empty lod_tensor_array to clear lod_tensor_array layers.assign(layers.create_array(data.dtype), data) diff --git a/python/paddle/distributed/fleet/utils/hybrid_parallel_util.py b/python/paddle/distributed/fleet/utils/hybrid_parallel_util.py index 340ace6ed7b80..edeb585384b03 100644 --- a/python/paddle/distributed/fleet/utils/hybrid_parallel_util.py +++ b/python/paddle/distributed/fleet/utils/hybrid_parallel_util.py @@ -14,6 +14,9 @@ import paddle from paddle import framework + +# (TODO: GhostScreaming) It will be removed later. +from paddle.base import core from paddle.distributed.parallel import ( _split_tensors, build_groups, @@ -21,9 +24,6 @@ sync_params_buffers, ) -# (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core - from .log_util import logger __all__ = [] @@ -73,7 +73,7 @@ def _apply_collective_grads(parameters, comm_group, bucket_size, scale=None): # need to div nranks if scale is not None: div_factor = paddle.to_tensor(scale, dtype=coalesced_grad.dtype) - paddle.fluid.framework._dygraph_tracer().trace_op( + paddle.base.framework._dygraph_tracer().trace_op( type="elementwise_div", inputs={'X': coalesced_grad, 'Y': div_factor}, outputs={'Out': coalesced_grad}, diff --git a/python/paddle/distributed/fleet/utils/mix_precision_utils.py b/python/paddle/distributed/fleet/utils/mix_precision_utils.py index 991a1fafc7b0f..ead12379bb5a8 100644 --- a/python/paddle/distributed/fleet/utils/mix_precision_utils.py +++ b/python/paddle/distributed/fleet/utils/mix_precision_utils.py @@ -20,13 +20,13 @@ import paddle from paddle import _legacy_C_ops, nn +from paddle.base import framework +from paddle.base.dygraph import base as imperative_base +from paddle.base.dygraph import to_variable from paddle.distributed import fleet from paddle.distributed.fleet.utils.hybrid_parallel_util import ( obtain_optimizer_parameters_list, ) -from paddle.fluid import framework -from paddle.fluid.dygraph import base as imperative_base -from paddle.fluid.dygraph import to_variable from paddle.framework import core diff --git a/python/paddle/distributed/fleet/utils/sequence_parallel_utils.py b/python/paddle/distributed/fleet/utils/sequence_parallel_utils.py index 1e7f5e93785ad..ae5dec21b4e8f 100644 --- a/python/paddle/distributed/fleet/utils/sequence_parallel_utils.py +++ b/python/paddle/distributed/fleet/utils/sequence_parallel_utils.py @@ -17,12 +17,12 @@ import paddle from paddle import distributed as dist from paddle.autograd import PyLayer +from paddle.base import core from paddle.distributed import fleet from paddle.distributed.fleet.meta_parallel import get_rng_state_tracker from paddle.distributed.fleet.utils.hybrid_parallel_util import ( fused_allreduce_gradients_with_group, ) -from paddle.fluid import core from paddle.nn import Layer from paddle.nn import functional as F diff --git a/python/paddle/distributed/fleet/utils/tensor_parallel_utils.py b/python/paddle/distributed/fleet/utils/tensor_parallel_utils.py index f3026a84f1dd0..e370042927434 100644 --- a/python/paddle/distributed/fleet/utils/tensor_parallel_utils.py +++ b/python/paddle/distributed/fleet/utils/tensor_parallel_utils.py @@ -22,8 +22,8 @@ ch.setFormatter(formatter) logger.addHandler(ch) +from paddle.base import core from paddle.distributed.fleet.meta_optimizers.common import OP_ROLE_KEY -from paddle.fluid import core from paddle.static import Parameter _supported_optimizer_type = [ diff --git a/python/paddle/distributed/io.py b/python/paddle/distributed/io.py index bc125cb242a29..69f6e42bf1954 100644 --- a/python/paddle/distributed/io.py +++ b/python/paddle/distributed/io.py @@ -15,7 +15,7 @@ import os import paddle -from paddle.fluid.framework import Program, static_only +from paddle.base.framework import Program, static_only from paddle.framework import core, dygraph_not_support @@ -38,10 +38,10 @@ def _load_distributed_persistables(executor, dirname, main_program=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) param_path = "./my_paddle_model" t = paddle.distributed.transpiler.DistributeTranspiler() t.transpile(...) @@ -161,12 +161,12 @@ def load_persistables(executor, dirname, main_program=None, filename=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) param_path = "./my_paddle_model" - prog = fluid.default_main_program() + prog = base.default_main_program() paddle.distributed.io.load_persistables(executor=exe, dirname=param_path, main_program=None) """ @@ -367,11 +367,11 @@ def is_persistable(var): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() - param = fluid.default_main_program().global_block().var('fc.b') - res = fluid.io.is_persistable(param) + param = base.default_main_program().global_block().var('fc.b') + res = base.io.is_persistable(param) """ if ( var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH @@ -465,7 +465,7 @@ def load_inference_model_distributed( """ Load the inference model from a given directory. By this API, you can get the model structure(Inference Program) and model parameters. If you just want to load - parameters of the pre-trained model, please use the :ref:`api_fluid_io_load_params` API. + parameters of the pre-trained model, please use the :ref:`api_base_io_load_params` API. You can refer to :ref:`api_guide_model_save_reader_en` for more details. Args: @@ -505,26 +505,26 @@ def load_inference_model_distributed( .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() # Build the model - main_prog = fluid.Program() - startup_prog = fluid.Program() - with fluid.program_guard(main_prog, startup_prog): - data = fluid.layers.data(name="img", shape=[64, 784], append_batch_size=False) + main_prog = base.Program() + startup_prog = base.Program() + with base.program_guard(main_prog, startup_prog): + data = base.layers.data(name="img", shape=[64, 784], append_batch_size=False) w = paddle.create_parameter(shape=[784, 200], dtype='float32') b = paddle.create_parameter(shape=[200], dtype='float32') hidden_w = paddle.matmul(x=data, y=w) - hidden_b = fluid.layers.elementwise_add(hidden_w, b) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + hidden_b = base.layers.elementwise_add(hidden_w, b) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) # Save the inference model path = "./infer_model" - fluid.io.save_inference_model(dirname=path, feeded_var_names=['img'], + base.io.save_inference_model(dirname=path, feeded_var_names=['img'], target_vars=[hidden_b], executor=exe, main_program=main_prog) # Demo one. Not need to set the distributed look up table, because the diff --git a/python/paddle/distributed/launch/context/device.py b/python/paddle/distributed/launch/context/device.py index 7ca3c5f41bff5..5126f687ccb0a 100644 --- a/python/paddle/distributed/launch/context/device.py +++ b/python/paddle/distributed/launch/context/device.py @@ -14,10 +14,9 @@ import os -from paddle.device import get_available_custom_device - # (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core +from paddle.base import core +from paddle.device import get_available_custom_device class DeviceType: diff --git a/python/paddle/distributed/parallel.py b/python/paddle/distributed/parallel.py index 67452c1a4e1b2..843c9eb5d9c0e 100644 --- a/python/paddle/distributed/parallel.py +++ b/python/paddle/distributed/parallel.py @@ -185,7 +185,7 @@ def sync_params_buffers( for coalesced_var, origin_vars, var_shapes in coalesced_vars: var_len = [np.prod(v_shape) for v_shape in var_shapes] - paddle.fluid.framework._dygraph_tracer().trace_op( + paddle.base.framework._dygraph_tracer().trace_op( type='split', inputs={'X': coalesced_var}, outputs={'Out': origin_vars}, diff --git a/python/paddle/distributed/parallel_with_gloo.py b/python/paddle/distributed/parallel_with_gloo.py index f205a5d261235..9183139becd95 100755 --- a/python/paddle/distributed/parallel_with_gloo.py +++ b/python/paddle/distributed/parallel_with_gloo.py @@ -15,14 +15,13 @@ import time from multiprocessing import Manager, Process +# deprecated module import +# (TODO: GhostScreaming) It will be removed later. +from paddle.base import core from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) -# deprecated module import -# (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core - __all__ = [] _global_gloo_ctx = None diff --git a/python/paddle/distributed/passes/auto_parallel_amp.py b/python/paddle/distributed/passes/auto_parallel_amp.py index 34ab1c29534a9..322adfb5da310 100644 --- a/python/paddle/distributed/passes/auto_parallel_amp.py +++ b/python/paddle/distributed/passes/auto_parallel_amp.py @@ -13,6 +13,7 @@ # limitations under the License. import paddle +from paddle.base.data_feeder import check_type, check_variable_and_dtype from paddle.distributed.auto_parallel.static.dist_attribute import ( OperatorDistAttr, ) @@ -24,7 +25,6 @@ set_var_dist_attr, ) from paddle.distributed.fleet.meta_optimizers.common import OP_ROLE_KEY, OpRole -from paddle.fluid.data_feeder import check_type, check_variable_and_dtype from paddle.framework import core from paddle.static.amp.bf16.amp_utils import ( AutoMixedPrecisionListsBF16, diff --git a/python/paddle/distributed/passes/auto_parallel_fp16.py b/python/paddle/distributed/passes/auto_parallel_fp16.py index c9a0f772db5ce..8f9927ae37c28 100644 --- a/python/paddle/distributed/passes/auto_parallel_fp16.py +++ b/python/paddle/distributed/passes/auto_parallel_fp16.py @@ -839,7 +839,7 @@ def _apply_single_impl(self, main_program, startup_program, context): with main_program._optimized_guard([]): block = main_program.global_block() - # all_infs = paddle.fluid.layers.concat(found_infs) + # all_infs = paddle.base.layers.concat(found_infs) all_infs = block.create_var( name=paddle.utils.unique_name.generate_with_ignorable_key( ".".join(['concat', 'tmp']) @@ -870,7 +870,7 @@ def _apply_single_impl(self, main_program, startup_program, context): self.dist_context, ) - # found_inf = paddle.fluid.layers.reduce_any(all_infs) + # found_inf = paddle.base.layers.reduce_any(all_infs) found_inf = block.create_var( name=paddle.utils.unique_name.generate_with_ignorable_key( ".".join(['find_infinite_scale', 'tmp']) diff --git a/python/paddle/distributed/passes/auto_parallel_pipeline.py b/python/paddle/distributed/passes/auto_parallel_pipeline.py index acc99636a53d9..9e2a06778854a 100644 --- a/python/paddle/distributed/passes/auto_parallel_pipeline.py +++ b/python/paddle/distributed/passes/auto_parallel_pipeline.py @@ -14,6 +14,8 @@ import os +from paddle.base import core +from paddle.base.framework import Program from paddle.distributed.auto_parallel.static.process_group import ( remove_process_group, ) @@ -24,8 +26,6 @@ is_optimize_op, ) from paddle.distributed.fleet.fleet_executor_utils import TaskNode -from paddle.fluid import core -from paddle.fluid.framework import Program from .pass_base import PassBase, register_pass from .pass_utils import _create_program, _insert_sync_for_fthenb_1f1b diff --git a/python/paddle/distributed/passes/auto_parallel_recompute.py b/python/paddle/distributed/passes/auto_parallel_recompute.py index d69bdb2a44c10..0fd008ff5a701 100644 --- a/python/paddle/distributed/passes/auto_parallel_recompute.py +++ b/python/paddle/distributed/passes/auto_parallel_recompute.py @@ -15,14 +15,14 @@ import logging import paddle -from paddle.distributed.fleet.meta_optimizers.common import OP_ROLE_KEY, OpRole -from paddle.fluid.backward import ( +from paddle.base.backward import ( ProgramStats, _append_grad_suffix_, _find_op_path_, _get_no_grad_set_name, _rename_arg_, ) +from paddle.distributed.fleet.meta_optimizers.common import OP_ROLE_KEY, OpRole from paddle.framework import core from paddle.utils import unique_name diff --git a/python/paddle/distributed/passes/pass_utils.py b/python/paddle/distributed/passes/pass_utils.py index 89c9c65ba1761..7c34023ebd6cc 100644 --- a/python/paddle/distributed/passes/pass_utils.py +++ b/python/paddle/distributed/passes/pass_utils.py @@ -15,6 +15,8 @@ from collections import OrderedDict from typing import List +from paddle.base import core +from paddle.base.framework import Parameter, Program from paddle.distributed.auto_parallel.static.utils import ( is_backward_op, is_forward_op, @@ -22,8 +24,6 @@ is_optimize_op, ) from paddle.distributed.fleet.meta_optimizers.common import OpRole -from paddle.fluid import core -from paddle.fluid.framework import Parameter, Program __not_shape_var_type__ = [ core.VarDesc.VarType.READER, @@ -332,7 +332,7 @@ def _create_program(src_block, dst_block, src_op, force_create=False): def _insert_sync_for_fthenb_1f1b(program): """ - This implementation refers to lots of Paddle/python/paddle/fluid/optimizer.py. + This implementation refers to lots of Paddle/python/paddle/base/optimizer.py. The difference between this function with 'PipelineOptimizer' is that 'send_v2' op and 'recv_v2' op have been inserted in program by 'reshard'. """ diff --git a/python/paddle/distributed/passes/pipeline_pass_base.py b/python/paddle/distributed/passes/pipeline_pass_base.py index c18a215012e86..07235ecc05a0f 100644 --- a/python/paddle/distributed/passes/pipeline_pass_base.py +++ b/python/paddle/distributed/passes/pipeline_pass_base.py @@ -14,8 +14,8 @@ import logging +from paddle.base import core from paddle.distributed.auto_parallel.static.utils import get_logger -from paddle.fluid import core from .pass_base import PassBase from .pass_utils import get_skip_gc_vars diff --git a/python/paddle/distributed/passes/pipeline_scheduler_pass.py b/python/paddle/distributed/passes/pipeline_scheduler_pass.py index dc45e73f77c4d..509836585a99d 100644 --- a/python/paddle/distributed/passes/pipeline_scheduler_pass.py +++ b/python/paddle/distributed/passes/pipeline_scheduler_pass.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import core +from paddle.base import core from .pass_base import PassContext, new_pass, register_pass from .pass_utils import _program_for_fthenb_and_1f1b diff --git a/python/paddle/distributed/passes/ps_trainer_pass.py b/python/paddle/distributed/passes/ps_trainer_pass.py index a1eeffa9abb00..434d32891d1d0 100755 --- a/python/paddle/distributed/passes/ps_trainer_pass.py +++ b/python/paddle/distributed/passes/ps_trainer_pass.py @@ -17,8 +17,8 @@ from _collections import defaultdict import paddle +from paddle.base import framework from paddle.distributed.passes.pass_base import PassBase, register_pass -from paddle.fluid import framework from paddle.framework import core from paddle.static import Parameter, Program diff --git a/python/paddle/distributed/ps/utils/collective_transpiler.py b/python/paddle/distributed/ps/utils/collective_transpiler.py index f5278d05367fd..99bb76a3b315b 100644 --- a/python/paddle/distributed/ps/utils/collective_transpiler.py +++ b/python/paddle/distributed/ps/utils/collective_transpiler.py @@ -15,10 +15,10 @@ import os import paddle +from paddle.base import unique_name from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) -from paddle.fluid import unique_name from paddle.framework import core from paddle.static import default_main_program, default_startup_program diff --git a/python/paddle/distributed/ps/utils/ps_program_builder.py b/python/paddle/distributed/ps/utils/ps_program_builder.py index ec2409028f91d..8ba98a3707d99 100755 --- a/python/paddle/distributed/ps/utils/ps_program_builder.py +++ b/python/paddle/distributed/ps/utils/ps_program_builder.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) @@ -78,7 +78,7 @@ def _build_pserver_programs(self): def _build_programs(self): if self.attrs['is_worker']: self._build_trainer_programs() - fluid.framework.switch_startup_program(self.cloned_startup) + base.framework.switch_startup_program(self.cloned_startup) print( "paddle.static.default_startup_program: {}".format( paddle.static.default_startup_program @@ -97,9 +97,7 @@ def _build_programs(self): elif self.attrs['is_server']: self._build_pserver_programs() self.loss.block.program = self.attrs['_main_server'] - fluid.framework.switch_startup_program( - self.attrs['_startup_server'] - ) + base.framework.switch_startup_program(self.attrs['_startup_server']) class GeoPsProgramBuilder(PsProgramBuilder): # 仅 CPU 模式 @@ -372,9 +370,7 @@ def _build_programs(self): elif self.attrs['is_server']: self._build_pserver_programs() self.loss.block.program = self.attrs['_main_server'] - fluid.framework.switch_startup_program( - self.attrs['_startup_server'] - ) + base.framework.switch_startup_program(self.attrs['_startup_server']) class FlPsProgramBuilder(HeterAsyncPsProgramBuilder): @@ -470,7 +466,7 @@ def _build_pserver_programs(self): def _build_programs(self): if not self.is_server: self._build_trainer_programs() - fluid.framework.switch_startup_program(self.cloned_startup) + base.framework.switch_startup_program(self.cloned_startup) paddle.framework.switch_main_program(self.cloned_main) print( "paddle.static.default_startup_program: {}".format( @@ -479,7 +475,5 @@ def _build_programs(self): ) else: self._build_pserver_programs() - fluid.framework.switch_startup_program( - self.attrs['_startup_server'] - ) + base.framework.switch_startup_program(self.attrs['_startup_server']) paddle.framework.switch_main_program(self.attrs['_main_server']) diff --git a/python/paddle/distributed/ps/utils/public.py b/python/paddle/distributed/ps/utils/public.py index 5537f6b4739f9..865de4c828308 100755 --- a/python/paddle/distributed/ps/utils/public.py +++ b/python/paddle/distributed/ps/utils/public.py @@ -18,8 +18,8 @@ import warnings from functools import reduce +from paddle.base.framework import generate_control_dev_var_name from paddle.distributed.io import is_persistable -from paddle.fluid.framework import generate_control_dev_var_name from paddle.framework import core # logging.basicConfig( @@ -391,7 +391,7 @@ def get_dense_send_context( aggregate = True # print("public get_dense_send_context dense_table:", grad_name, # var_numel, origin_varnames) - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext dense_ctx = CommContext( grad_name, @@ -427,7 +427,7 @@ def get_dense_send_context( aggregate = True # print("public get_dense_send_context data_norm table:", grad_name, # var_numel, origin_varnames) - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext data_norm_ctx = CommContext( grad_name, @@ -455,7 +455,7 @@ def get_dense_send_context( var_numel = reduce(lambda x, y: x * y, var.shape, 1) grad_name = origin_varname aggregate = True - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext dense_ctx = CommContext( grad_name, @@ -504,7 +504,7 @@ def get_geo_trainer_send_context(attrs): ) var = program.global_block().vars[grad.merged_var.name] var_numel = reduce(lambda x, y: x * y, var.shape[1:], 1) - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext print( "public get_the_geo_send_context sparse: ", grad_name, var_numel @@ -544,7 +544,7 @@ def _step_ctx(idx, role_maker): endpoints = get_ps_endpoints(role_maker) sections = [1] * len(endpoints) names = [name] * len(endpoints) - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext ctx = CommContext( name, @@ -602,7 +602,7 @@ def get_the_one_send_context(attrs, split_dense_table=False, ep_list=None): if grad_name in send_ctx: continue - from paddle.fluid.core import CommContext + from paddle.base.core import CommContext print( "public get_the_one_send_context sparse: ", diff --git a/python/paddle/distributed/rpc/rpc.py b/python/paddle/distributed/rpc/rpc.py index e1e2de7c7b04a..ebe6bc54623d6 100644 --- a/python/paddle/distributed/rpc/rpc.py +++ b/python/paddle/distributed/rpc/rpc.py @@ -18,10 +18,10 @@ import time from collections import namedtuple +from paddle.base import core from paddle.distributed.launch.context import Node from paddle.distributed.rpc.internal import PythonFunc, _serialize from paddle.distributed.utils.launch_utils import logger -from paddle.fluid import core WorkerInfo = namedtuple("WorkerInfo", ["name", "rank", "ip", "port"]) diff --git a/python/paddle/distributed/spawn.py b/python/paddle/distributed/spawn.py index 0bc68c34c2086..91039b3b3bac3 100644 --- a/python/paddle/distributed/spawn.py +++ b/python/paddle/distributed/spawn.py @@ -18,6 +18,9 @@ import sys import warnings +# deprecated module import +# (TODO: GhostScreaming) It will be removed later. +from paddle.base import core from paddle.device import get_device from paddle.distributed.cloud_utils import ( _get_trainers_num, @@ -35,10 +38,6 @@ _print_arguments, get_host_name_ip, ) - -# deprecated module import -# (TODO: GhostScreaming) It will be removed later. -from paddle.fluid import core from paddle.framework import set_flags __all__ = [] diff --git a/python/paddle/distributed/transpiler/collective.py b/python/paddle/distributed/transpiler/collective.py index 1fb1cf474a701..e76238e02af43 100644 --- a/python/paddle/distributed/transpiler/collective.py +++ b/python/paddle/distributed/transpiler/collective.py @@ -15,10 +15,10 @@ import os import paddle +from paddle.base import unique_name from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) -from paddle.fluid import unique_name from paddle.framework import core from paddle.static import default_main_program, default_startup_program diff --git a/python/paddle/distributed/transpiler/details/vars_distributed.py b/python/paddle/distributed/transpiler/details/vars_distributed.py index e6dc457c5266c..25324598d2547 100644 --- a/python/paddle/distributed/transpiler/details/vars_distributed.py +++ b/python/paddle/distributed/transpiler/details/vars_distributed.py @@ -116,7 +116,7 @@ def equal(var1, var2): def __str__(self): origin_var_str = ( - "{name} : fluid.{type}.shape{shape}.astype({dtype})".format( + "{name} : base.{type}.shape{shape}.astype({dtype})".format( name=self.origin.name, type=self.origin.type, shape=self.origin.shape, @@ -125,7 +125,7 @@ def __str__(self): ) slice_var_str = ( - "{name} : fluid.{type}.shape{shape}.astype({dtype})" + "{name} : base.{type}.shape{shape}.astype({dtype})" ".slice({is_slice}).block({block_id}).offset({offset})".format( name=self.slice.name, type=self.slice.type, diff --git a/python/paddle/distributed/transpiler/distribute_transpiler.py b/python/paddle/distributed/transpiler/distribute_transpiler.py index 38892c1a6e92e..d1059c08cb975 100644 --- a/python/paddle/distributed/transpiler/distribute_transpiler.py +++ b/python/paddle/distributed/transpiler/distribute_transpiler.py @@ -38,7 +38,7 @@ import numpy as np from paddle import framework -from paddle.fluid.framework import grad_var_name +from paddle.base.framework import grad_var_name from paddle.framework import Block, Program, core from paddle.incubate.distributed.fleet.parameter_server.ir.ps_dispatcher import ( PSDispatcher, @@ -157,8 +157,8 @@ class DistributeTranspilerConfig: .. py:attribute:: split_method (PSDispatcher) Methods of dispatching parameters for server, - :ref:`api_fluid_transpiler_RoundRobin` or - :ref:`api_fluid_transpiler_HashName` can be used and default is RoundRobin. + :ref:`api_base_transpiler_RoundRobin` or + :ref:`api_base_transpiler_HashName` can be used and default is RoundRobin. Try to choose the best method to balance loads for parameter servers. .. py:attribute:: min_block_size (int) @@ -266,7 +266,7 @@ class DistributeTranspiler: **DistributeTranspiler** - Convert the fluid program to distributed data-parallelism programs. + Convert the base program to distributed data-parallelism programs. Supports two modes: parameter server(pserver) mode and nccl2 mode. In pserver mode, the main_program will be transformed to use a remote @@ -283,7 +283,7 @@ class DistributeTranspiler: .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import paddle.distributed.transpiler as transpiler paddle.enable_static() diff --git a/python/paddle/distributed/utils/nccl_utils.py b/python/paddle/distributed/utils/nccl_utils.py index f9a1b99f91b17..2910bdd0a8d2e 100644 --- a/python/paddle/distributed/utils/nccl_utils.py +++ b/python/paddle/distributed/utils/nccl_utils.py @@ -13,7 +13,7 @@ # limitations under the License. -from paddle.fluid import core +from paddle.base import core def get_nccl_version_str(ver): diff --git a/python/paddle/distribution/bernoulli.py b/python/paddle/distribution/bernoulli.py index 1b365bbcd313c..7d4849fab48e7 100644 --- a/python/paddle/distribution/bernoulli.py +++ b/python/paddle/distribution/bernoulli.py @@ -16,9 +16,9 @@ import numpy as np import paddle +from paddle.base.data_feeder import check_type, convert_dtype +from paddle.base.framework import Variable from paddle.distribution import exponential_family -from paddle.fluid.data_feeder import check_type, convert_dtype -from paddle.fluid.framework import Variable from paddle.framework import in_dynamic_mode from paddle.nn.functional import ( binary_cross_entropy_with_logits, diff --git a/python/paddle/distribution/categorical.py b/python/paddle/distribution/categorical.py index 1af187c2cfed7..b6484e3f21d56 100644 --- a/python/paddle/distribution/categorical.py +++ b/python/paddle/distribution/categorical.py @@ -15,9 +15,9 @@ import numpy as np import paddle +from paddle.base.data_feeder import check_type, convert_dtype +from paddle.base.framework import Variable from paddle.distribution import distribution -from paddle.fluid.data_feeder import check_type, convert_dtype -from paddle.fluid.framework import Variable from paddle.framework import in_dynamic_mode from paddle.tensor import multinomial diff --git a/python/paddle/distribution/cauchy.py b/python/paddle/distribution/cauchy.py index 818c9d0491d2f..63e0a5f252638 100644 --- a/python/paddle/distribution/cauchy.py +++ b/python/paddle/distribution/cauchy.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import framework from paddle.distribution import distribution -from paddle.fluid import framework class Cauchy(distribution.Distribution): diff --git a/python/paddle/distribution/dirichlet.py b/python/paddle/distribution/dirichlet.py index 4e4b002533eb9..cf578c9d0dd5c 100644 --- a/python/paddle/distribution/dirichlet.py +++ b/python/paddle/distribution/dirichlet.py @@ -13,9 +13,9 @@ # limitations under the License. import paddle +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.distribution import exponential_family -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/python/paddle/distribution/distribution.py b/python/paddle/distribution/distribution.py index 68d468accee55..0bc1a70a4c854 100644 --- a/python/paddle/distribution/distribution.py +++ b/python/paddle/distribution/distribution.py @@ -25,8 +25,8 @@ import paddle from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype, convert_dtype -from paddle.fluid.framework import Variable +from paddle.base.data_feeder import check_variable_and_dtype, convert_dtype +from paddle.base.framework import Variable from paddle.framework import in_dynamic_mode diff --git a/python/paddle/distribution/geometric.py b/python/paddle/distribution/geometric.py index ae531cf50a873..bfcd734bb1785 100644 --- a/python/paddle/distribution/geometric.py +++ b/python/paddle/distribution/geometric.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import framework from paddle.distribution import distribution, uniform -from paddle.fluid import framework class Geometric(distribution.Distribution): diff --git a/python/paddle/distribution/gumbel.py b/python/paddle/distribution/gumbel.py index 5844548255c29..005801ae6b7cc 100644 --- a/python/paddle/distribution/gumbel.py +++ b/python/paddle/distribution/gumbel.py @@ -18,8 +18,8 @@ import numpy as np import paddle +from paddle.base import framework from paddle.distribution.transformed_distribution import TransformedDistribution -from paddle.fluid import framework class Gumbel(TransformedDistribution): diff --git a/python/paddle/distribution/laplace.py b/python/paddle/distribution/laplace.py index d4af1203fcd35..fc4b57eeba79c 100644 --- a/python/paddle/distribution/laplace.py +++ b/python/paddle/distribution/laplace.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import framework from paddle.distribution import distribution -from paddle.fluid import framework class Laplace(distribution.Distribution): diff --git a/python/paddle/distribution/normal.py b/python/paddle/distribution/normal.py index b1cec30d2d89e..0564bb2b5397b 100644 --- a/python/paddle/distribution/normal.py +++ b/python/paddle/distribution/normal.py @@ -18,9 +18,9 @@ import numpy as np import paddle +from paddle.base.data_feeder import check_type, convert_dtype +from paddle.base.framework import Variable from paddle.distribution import distribution -from paddle.fluid.data_feeder import check_type, convert_dtype -from paddle.fluid.framework import Variable from paddle.framework import in_dynamic_mode from paddle.tensor import random diff --git a/python/paddle/distribution/transform.py b/python/paddle/distribution/transform.py index 06735d23b1bdf..92313c9bec58a 100644 --- a/python/paddle/distribution/transform.py +++ b/python/paddle/distribution/transform.py @@ -166,7 +166,7 @@ def forward(self, x): Returns: Tensor: Outcome of forward transformation. """ - if not isinstance(x, paddle.fluid.framework.Variable): + if not isinstance(x, paddle.base.framework.Variable): raise TypeError( f"Expected 'x' is a Tensor or Real, but got {type(x)}." ) @@ -187,7 +187,7 @@ def inverse(self, y): Returns: Tensor: Outcome of inverse transform. """ - if not isinstance(y, paddle.fluid.framework.Variable): + if not isinstance(y, paddle.base.framework.Variable): raise TypeError( f"Expected 'y' is a Tensor or Real, but got {type(y)}." ) @@ -209,12 +209,12 @@ def forward_log_det_jacobian(self, x): Returns: Tensor: The log of the absolute value of Jacobian determinant. """ - if not isinstance(x, paddle.fluid.framework.Variable): + if not isinstance(x, paddle.base.framework.Variable): raise TypeError( f"Expected 'y' is a Tensor or Real, but got {type(x)}." ) if ( - isinstance(x, paddle.fluid.framework.Variable) + isinstance(x, paddle.base.framework.Variable) and x.dim() < self._domain.event_rank ): raise ValueError( @@ -241,7 +241,7 @@ def inverse_log_det_jacobian(self, y): Returns: Tensor: The value of :math:`log|det J_{f^{-1}}(y)|`. """ - if not isinstance(y, paddle.fluid.framework.Variable): + if not isinstance(y, paddle.base.framework.Variable): raise TypeError(f"Expected 'y' is a Tensor, but got {type(y)}.") if y.dim() < self._codomain.event_rank: raise ValueError( @@ -441,9 +441,9 @@ class AffineTransform(Transform): _type = Type.BIJECTION def __init__(self, loc, scale): - if not isinstance(loc, paddle.fluid.framework.Variable): + if not isinstance(loc, paddle.base.framework.Variable): raise TypeError(f"Expected 'loc' is a Tensor, but got {type(loc)}") - if not isinstance(scale, paddle.fluid.framework.Variable): + if not isinstance(scale, paddle.base.framework.Variable): raise TypeError( f"Expected scale is a Tensor, but got {type(scale)}" ) @@ -791,7 +791,7 @@ class PowerTransform(Transform): _type = Type.BIJECTION def __init__(self, power): - if not isinstance(power, paddle.fluid.framework.Variable): + if not isinstance(power, paddle.base.framework.Variable): raise TypeError( f"Expected 'power' is a tensor, but got {type(power)}" ) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index dbd27fd14728f..4d8a0f97d910d 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -16,9 +16,9 @@ import paddle from paddle import _C_ops +from paddle.base.data_feeder import check_type, convert_dtype +from paddle.base.framework import Variable from paddle.distribution import distribution -from paddle.fluid.data_feeder import check_type, convert_dtype -from paddle.fluid.framework import Variable from paddle.framework import in_dynamic_mode from paddle.tensor import random diff --git a/python/paddle/fft.py b/python/paddle/fft.py index 704bc56823ae1..df9cc318830d4 100644 --- a/python/paddle/fft.py +++ b/python/paddle/fft.py @@ -19,8 +19,8 @@ import paddle from . import _C_ops -from .fluid.data_feeder import check_variable_and_dtype -from .fluid.layer_helper import LayerHelper +from .base.data_feeder import check_variable_and_dtype +from .base.layer_helper import LayerHelper from .framework import in_dynamic_mode from .tensor.attribute import is_floating_point, is_integer from .tensor.creation import _complex_to_real_dtype, _real_to_complex_dtype diff --git a/python/paddle/framework/__init__.py b/python/paddle/framework/__init__.py index 302acc6b4c4db..110f8cd39b585 100755 --- a/python/paddle/framework/__init__.py +++ b/python/paddle/framework/__init__.py @@ -19,18 +19,18 @@ from .framework import get_default_dtype # noqa: F401 from .framework import set_default_dtype # noqa: F401 -from ..fluid.param_attr import ParamAttr # noqa: F401 -from ..fluid.core import CPUPlace # noqa: F401 -from ..fluid.core import IPUPlace # noqa: F401 -from ..fluid.core import CUDAPlace # noqa: F401 -from ..fluid.core import CUDAPinnedPlace # noqa: F401 -from ..fluid.core import CustomPlace # noqa: F401 -from ..fluid.core import XPUPlace # noqa: F401 +from ..base.param_attr import ParamAttr # noqa: F401 +from ..base.core import CPUPlace # noqa: F401 +from ..base.core import IPUPlace # noqa: F401 +from ..base.core import CUDAPlace # noqa: F401 +from ..base.core import CUDAPinnedPlace # noqa: F401 +from ..base.core import CustomPlace # noqa: F401 +from ..base.core import XPUPlace # noqa: F401 -from ..fluid import core # noqa: F401 -from ..fluid.dygraph import base, to_variable -from ..fluid.dygraph.base import no_grad_ as no_grad # noqa: F401 -from ..fluid.dygraph.base import grad # noqa: F401 +from ..base import core # noqa: F401 +from ..base.dygraph import base, to_variable +from ..base.dygraph.base import no_grad_ as no_grad # noqa: F401 +from ..base.dygraph.base import grad # noqa: F401 from .io import save # noqa: F401 from .io import load # noqa: F401 @@ -47,41 +47,41 @@ # Do the *DUPLICATED* monkey-patch for the tensor object. # We need remove the duplicated code here once we fix # the illogical implement in the monkey-patch methods later. -from ..fluid.layers.math_op_patch import monkey_patch_variable -from ..fluid.dygraph.math_op_patch import monkey_patch_math_tensor -from ..fluid.framework import disable_signal_handler # noqa: F401 -from ..fluid.framework import get_flags # noqa: F401 -from ..fluid.framework import set_flags # noqa: F401 -from ..fluid.framework import Parameter -from ..fluid.dygraph.base import enable_dygraph as disable_static # noqa: F401 -from ..fluid.dygraph.base import disable_dygraph as enable_static # noqa: F401 -from ..fluid.framework import in_dygraph_mode as in_dynamic_mode # noqa: F401 -from ..fluid.framework import in_new_ir_mode # noqa: F401 -from ..fluid.framework import in_dynamic_or_new_ir_mode # noqa: F401 -from ..fluid.framework import ( +from ..base.layers.math_op_patch import monkey_patch_variable +from ..base.dygraph.math_op_patch import monkey_patch_math_tensor +from ..base.framework import disable_signal_handler # noqa: F401 +from ..base.framework import get_flags # noqa: F401 +from ..base.framework import set_flags # noqa: F401 +from ..base.framework import Parameter +from ..base.dygraph.base import enable_dygraph as disable_static # noqa: F401 +from ..base.dygraph.base import disable_dygraph as enable_static # noqa: F401 +from ..base.framework import in_dygraph_mode as in_dynamic_mode # noqa: F401 +from ..base.framework import in_new_ir_mode # noqa: F401 +from ..base.framework import in_dynamic_or_new_ir_mode # noqa: F401 +from ..base.framework import ( _current_expected_place, _get_paddle_place, ) # noqa: F401 -from ..fluid.framework import dygraph_only # noqa: F401 -from ..fluid.framework import dygraph_not_support # noqa: F401 -from ..fluid.framework import ( +from ..base.framework import dygraph_only # noqa: F401 +from ..base.framework import dygraph_not_support # noqa: F401 +from ..base.framework import ( convert_np_dtype_to_dtype_, _create_tensor, OpProtoHolder, ) # noqa: F401 -from ..fluid.framework import _dygraph_tracer # noqa: F401 -from ..fluid.framework import generate_control_dev_var_name # noqa: F401 +from ..base.framework import _dygraph_tracer # noqa: F401 +from ..base.framework import generate_control_dev_var_name # noqa: F401 -from ..fluid.layer_helper import LayerHelper # noqa: F401 -from ..fluid.framework import _global_flags # noqa: F401 -from ..fluid.framework import _apply_pass # noqa: F401 -from ..fluid.framework import switch_main_program -from ..fluid.framework import switch_startup_program -from ..fluid.framework import _set_expected_place # noqa: F401 -from ..fluid.framework import Block, Program # noqa: F401 -from ..fluid.framework import IrGraph # noqa: F401 -from ..fluid.framework import deprecate_stat_dict -from ..fluid.framework import ( +from ..base.layer_helper import LayerHelper # noqa: F401 +from ..base.framework import _global_flags # noqa: F401 +from ..base.framework import _apply_pass # noqa: F401 +from ..base.framework import switch_main_program +from ..base.framework import switch_startup_program +from ..base.framework import _set_expected_place # noqa: F401 +from ..base.framework import Block, Program # noqa: F401 +from ..base.framework import IrGraph # noqa: F401 +from ..base.framework import deprecate_stat_dict +from ..base.framework import ( _stride_in_no_check_dy2st_diff as _no_check_dy2st_diff, ) # noqa: F401 diff --git a/python/paddle/framework/dtype.py b/python/paddle/framework/dtype.py index 6640407084785..57a3cb81d00fe 100644 --- a/python/paddle/framework/dtype.py +++ b/python/paddle/framework/dtype.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..fluid.core import VarDesc -from ..fluid.core import finfo as core_finfo -from ..fluid.core import iinfo as core_iinfo +from ..base.core import VarDesc +from ..base.core import finfo as core_finfo +from ..base.core import iinfo as core_iinfo dtype = VarDesc.VarType dtype.__qualname__ = "dtype" diff --git a/python/paddle/framework/framework.py b/python/paddle/framework/framework.py index 6f8c7c7555bf0..812c42d956093 100644 --- a/python/paddle/framework/framework.py +++ b/python/paddle/framework/framework.py @@ -16,7 +16,7 @@ import numpy as np # TODO: define framework api -from paddle.fluid.layer_helper_base import LayerHelperBase +from paddle.base.layer_helper_base import LayerHelperBase __all__ = [] diff --git a/python/paddle/framework/io.py b/python/paddle/framework/io.py index 4b8440956fadb..b1401759c1aad 100644 --- a/python/paddle/framework/io.py +++ b/python/paddle/framework/io.py @@ -25,9 +25,9 @@ import paddle # deprecated module import -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle import base +from paddle.base import core +from paddle.base.framework import ( EagerParamBase, Program, Variable, @@ -83,7 +83,7 @@ def _load_state_dict_from_save_inference_model(model_path, config): programs = _construct_program_holders(model_path, config.model_filename) # 2. load layer parameters & buffers - with fluid.dygraph.guard(): + with base.dygraph.guard(): persistable_var_dict = _construct_params_and_buffers( model_path, programs, config.params_filename, append_suffix=False ) @@ -133,7 +133,7 @@ def _load_state_dict_from_save_params(model_path): var_name_list.append(var_name) # 2. create and load Tensor - with fluid.dygraph.guard(): + with base.dygraph.guard(): for name in var_name_list: new_var = _create_tensor(name=name, persistable=True) _dygraph_tracer().trace_op( @@ -162,8 +162,8 @@ def _load_state_dict_from_save_params(model_path): # - paddle.jit.save # - paddle.static.save_inference_model # - need [directory] when loading [compatible for paddle 1.x] -# - paddle.fluid.io.save_inference_model -# - paddle.fluid.io.save_params/save_persistable +# - paddle.base.io.save_inference_model +# - paddle.base.io.save_params/save_persistable # 2. Error cases: # - no error case def _build_load_path_and_config(path, config): @@ -189,7 +189,7 @@ def _build_load_path_and_config(path, config): error_msg = "The ``path`` (%s) to load model not exists." # if current path is a prefix, and the path.pdparams or path.pdopt # is exist, users may want use `paddle.load` load the result of - # `fluid.save_dygraph`, we raise error here for users + # `base.save_dygraph`, we raise error here for users params_file_path = path + ".pdparams" opti_file_path = path + ".pdopt" if os.path.exists(params_file_path) or os.path.exists(opti_file_path): @@ -565,15 +565,15 @@ def _save_lod_tensor(tensor, file_name): def _load_lod_tensor(file_name): - temp_t = paddle.fluid.core.LoDTensor() + temp_t = paddle.base.core.LoDTensor() if _is_file_path(file_name): # '_seek' is the end position of this tensor in the file. - _seek = paddle.fluid.core.load_lod_tensor(temp_t, file_name) + _seek = paddle.base.core.load_lod_tensor(temp_t, file_name) elif _is_memory_buffer(file_name): with _open_file_buffer(file_name, 'rb') as f: tensor_bytes = f.read() - paddle.fluid.core.load_lod_tensor_from_memory(temp_t, tensor_bytes) + paddle.base.core.load_lod_tensor_from_memory(temp_t, tensor_bytes) _seek = f.tell() else: @@ -616,7 +616,7 @@ def _load_selected_rows(file_name): elif _is_memory_buffer(file_name): with _open_file_buffer(file_name, 'rb') as f: selected_rows_bytes = f.read() - paddle.fluid.core.load_selected_rows_from_memory( + paddle.base.core.load_selected_rows_from_memory( temp_sr, selected_rows_bytes ) _seek = f.tell() @@ -909,8 +909,8 @@ def load(path, **configs): or ``paddle.Model().save(training=False)`` , ``path`` need to be a file prefix, such as ``model/mnist``, and ``paddle.load`` will get information from ``mnist.pdmodel`` and ``mnist.pdiparams`` ; - 3. loading from paddle 1.x APIs ``paddle.fluid.io.save_inference_model`` or - ``paddle.fluid.io.save_params/save_persistables`` , ``path`` need to be a + 3. loading from paddle 1.x APIs ``paddle.base.io.save_inference_model`` or + ``paddle.base.io.save_params/save_persistables`` , ``path`` need to be a directory, such as ``model`` and model is a directory. Note: diff --git a/python/paddle/framework/io_utils.py b/python/paddle/framework/io_utils.py index 1c72bc2852d0c..a5c46778605cc 100644 --- a/python/paddle/framework/io_utils.py +++ b/python/paddle/framework/io_utils.py @@ -22,10 +22,10 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.framework import Parameter, Variable, static_only -from paddle.fluid.log_helper import get_logger -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base import core +from paddle.base.framework import Parameter, Variable, static_only +from paddle.base.log_helper import get_logger +from paddle.base.wrapped_decorator import signature_safe_contextmanager _logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' @@ -96,11 +96,11 @@ def is_persistable(var): >>> # doctest: +SKIP('ValueError: var fc.b not in this block') >>> import paddle - >>> import paddle.fluid as fluid + >>> import paddle.base as base >>> paddle.enable_static() - >>> param = fluid.default_main_program().global_block().var('fc.b') - >>> res = fluid.io.is_persistable(param) + >>> param = base.default_main_program().global_block().var('fc.b') + >>> res = base.io.is_persistable(param) """ if ( var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH @@ -127,11 +127,11 @@ def is_parameter(var): >>> # doctest: +SKIP('ValueError: var fc.w not in this block') >>> import paddle - >>> import paddle.fluid as fluid + >>> import paddle.base as base >>> paddle.enable_static() - >>> param = fluid.default_main_program().global_block().var('fc.w') - >>> res = fluid.io.is_parameter(param) + >>> param = base.default_main_program().global_block().var('fc.w') + >>> res = base.io.is_parameter(param) """ return isinstance(var, Parameter) @@ -166,13 +166,13 @@ def _clone_var_in_block_(block, var): @signature_safe_contextmanager def _load_program_scope(main=None, startup=None, scope=None): - prog = main if main else paddle.fluid.Program() - startup_prog = startup if startup else paddle.fluid.Program() - scope = scope if scope else paddle.fluid.core.Scope() - with paddle.fluid.scope_guard(scope): - with paddle.fluid.program_guard(prog, startup_prog): - with paddle.fluid.unique_name.guard(): - with paddle.fluid.framework._dygraph_guard(None): + prog = main if main else paddle.base.Program() + startup_prog = startup if startup else paddle.base.Program() + scope = scope if scope else paddle.base.core.Scope() + with paddle.base.scope_guard(scope): + with paddle.base.program_guard(prog, startup_prog): + with paddle.base.unique_name.guard(): + with paddle.base.framework._dygraph_guard(None): yield diff --git a/python/paddle/framework/ir.py b/python/paddle/framework/ir.py index 544eff024d750..dff055a1575b1 100644 --- a/python/paddle/framework/ir.py +++ b/python/paddle/framework/ir.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..fluid.framework import _apply_pass +from ..base.framework import _apply_pass from . import core diff --git a/python/paddle/framework/random.py b/python/paddle/framework/random.py index 9670e79b457bd..8d72d0215e93c 100644 --- a/python/paddle/framework/random.py +++ b/python/paddle/framework/random.py @@ -14,8 +14,8 @@ # TODO: define random api import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core __all__ = [] @@ -49,7 +49,7 @@ def seed(seed): elif core.is_compiled_with_xpu(): for i in range(core.get_xpu_device_count()): core.default_xpu_generator(i).manual_seed(seed) - place = fluid.framework._current_expected_place() + place = base.framework._current_expected_place() if isinstance(place, core.CustomPlace): dev_cnt = sum( [ @@ -80,7 +80,7 @@ def get_rng_state(device=None): """ state_list = [] if device is None: - place = fluid.framework._current_expected_place() + place = base.framework._current_expected_place() else: place = paddle.device._convert_to_place(device) @@ -164,7 +164,7 @@ def set_rng_state(state_list, device=None): """ if device is None: - place = fluid.framework._current_expected_place() + place = base.framework._current_expected_place() else: place = device._convert_to_place(device) @@ -250,9 +250,9 @@ def _manual_program_seed(seed): Returns: None """ - fluid.default_main_program().random_seed = seed - fluid.default_startup_program().random_seed = seed - program = fluid.Program() + base.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + program = base.Program() program.global_seed(seed) diff --git a/python/paddle/geometric/math.py b/python/paddle/geometric/math.py index fd8e2dda24484..4ba60f5d63a54 100644 --- a/python/paddle/geometric/math.py +++ b/python/paddle/geometric/math.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/geometric/message_passing/send_recv.py b/python/paddle/geometric/message_passing/send_recv.py index 9ad69b7cf76b4..e0ec592d7010b 100644 --- a/python/paddle/geometric/message_passing/send_recv.py +++ b/python/paddle/geometric/message_passing/send_recv.py @@ -15,13 +15,13 @@ import numpy as np from paddle import _C_ops -from paddle.fluid.data_feeder import ( +from paddle.base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, ) -from paddle.fluid.framework import Variable -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.framework import Variable +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from .utils import ( diff --git a/python/paddle/geometric/message_passing/utils.py b/python/paddle/geometric/message_passing/utils.py index 5122a4f110ffe..cbcd8478ab3aa 100644 --- a/python/paddle/geometric/message_passing/utils.py +++ b/python/paddle/geometric/message_passing/utils.py @@ -15,8 +15,8 @@ import numpy as np import paddle -from paddle.fluid.data_feeder import check_dtype, convert_dtype -from paddle.fluid.framework import Variable +from paddle.base.data_feeder import check_dtype, convert_dtype +from paddle.base.framework import Variable def convert_out_size_to_list(out_size): diff --git a/python/paddle/geometric/reindex.py b/python/paddle/geometric/reindex.py index 7a38f2cfdca46..164a5af2cbb9f 100644 --- a/python/paddle/geometric/reindex.py +++ b/python/paddle/geometric/reindex.py @@ -14,9 +14,9 @@ import paddle from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.framework import Variable -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.framework import Variable +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/geometric/sampling/neighbors.py b/python/paddle/geometric/sampling/neighbors.py index f3242d02adfdf..8e771f7d081fc 100644 --- a/python/paddle/geometric/sampling/neighbors.py +++ b/python/paddle/geometric/sampling/neighbors.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops, _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/hapi/model.py b/python/paddle/hapi/model.py index 425ea07ab45c9..8ff1a557673af 100644 --- a/python/paddle/hapi/model.py +++ b/python/paddle/hapi/model.py @@ -24,16 +24,16 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base from paddle.autograd import no_grad +from paddle.base import core +from paddle.base.dygraph.base import to_variable +from paddle.base.executor import global_scope +from paddle.base.framework import Variable +from paddle.base.framework import _current_expected_place as _get_device +from paddle.base.framework import _get_paddle_place from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable -from paddle.fluid.executor import global_scope -from paddle.fluid.framework import Variable -from paddle.fluid.framework import _current_expected_place as _get_device -from paddle.fluid.framework import _get_paddle_place from paddle.framework import in_dynamic_mode from paddle.framework.io_utils import is_belong_to_optimizer from paddle.io import DataLoader, Dataset, DistributedBatchSampler @@ -58,10 +58,8 @@ def to_list(value): def to_numpy(var): - assert isinstance( - var, (Variable, fluid.core.eager.Tensor) - ), "not a variable" - if isinstance(var, fluid.core.eager.Tensor): + assert isinstance(var, (Variable, base.core.eager.Tensor)), "not a variable" + if isinstance(var, base.core.eager.Tensor): return np.array(var) t = global_scope().find_var(var.name).get_tensor() return np.array(t) @@ -131,9 +129,9 @@ def init_communicator( wait_server_ready(other_endpoints) if core.is_compiled_with_cuda(): nccl_id_var = block.create_var( - name=fluid.unique_name.generate('nccl_id'), + name=base.unique_name.generate('nccl_id'), persistable=True, - type=fluid.core.VarDesc.VarType.RAW, + type=base.core.VarDesc.VarType.RAW, ) block.append_op( @@ -159,9 +157,9 @@ def init_communicator( ) elif core.is_compiled_with_xpu(): bkcl_id_var = block.create_var( - name=fluid.unique_name.generate('bkcl_id'), + name=base.unique_name.generate('bkcl_id'), persistable=True, - type=fluid.core.VarDesc.VarType.RAW, + type=base.core.VarDesc.VarType.RAW, ) block.append_op( @@ -190,9 +188,9 @@ def init_communicator( in paddle.device.get_all_custom_device_type() ): xccl_id_var = block.create_var( - name=fluid.unique_name.generate('xccl_id'), + name=base.unique_name.generate('xccl_id'), persistable=True, - type=fluid.core.VarDesc.VarType.RAW, + type=base.core.VarDesc.VarType.RAW, ) block.append_op( @@ -221,9 +219,9 @@ def init_communicator( def prepare_distributed_context(place=None): if place is None: place = ( - fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) if paddle.distributed.ParallelEnv().nranks > 1 - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) place = _get_paddle_place(place) @@ -242,10 +240,10 @@ def prepare_distributed_context(place=None): global _parallel_context_initialized - if not _parallel_context_initialized and isinstance(place, fluid.CUDAPlace): + if not _parallel_context_initialized and isinstance(place, base.CUDAPlace): def _init_context(): - communicator_prog = fluid.Program() + communicator_prog = base.Program() init_communicator( communicator_prog, strategy.local_rank, @@ -254,13 +252,13 @@ def _init_context(): strategy.current_endpoint, strategy.trainer_endpoints, ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(communicator_prog) if in_dynamic_mode(): - fluid.disable_dygraph() + base.disable_dygraph() _init_context() - fluid.enable_dygraph(place) + base.enable_dygraph(place) else: assert "Only support CUDAPlace for now." @@ -299,8 +297,8 @@ def __init__(self, model): self.model = model # with `_build_once` gone, parameters are now created in `__init__` # so we need to keep track of the parameters already created - self._startup_prog = fluid.default_startup_program() - self._orig_prog = fluid.default_main_program() + self._startup_prog = base.default_startup_program() + self._orig_prog = base.default_main_program() self._label_vars = {} # label variables self._input_vars = {} # label variables @@ -388,12 +386,12 @@ def _save(state, path): # TODO: Support save/load scaler state in static graph def load(self, param_state_pairs, optim_state): if self._executor is None: - executor = fluid.Executor(fluid.CPUPlace())._default_executor + executor = base.Executor(base.CPUPlace())._default_executor else: executor = self._executor._default_executor # restore parameter states - fluid.core._create_loaded_parameter( + base.core._create_loaded_parameter( [param for param, state in param_state_pairs], global_scope(), executor, @@ -413,7 +411,7 @@ def _load_optimizer(self, state, executor): if not optim: return - fluid.core._create_loaded_parameter(optim, global_scope(), executor) + base.core._create_loaded_parameter(optim, global_scope(), executor) converted_state = dict(state) for var in optim: @@ -506,13 +504,13 @@ def _set_var(self, var, ndarray): t = global_scope().find_var(var.name).get_tensor() p = t._place() if p.is_cpu_place(): - place = fluid.CPUPlace() + place = base.CPUPlace() elif p.is_cuda_pinned_place(): - place = fluid.CUDAPinnedPlace() + place = base.CUDAPinnedPlace() else: - p = fluid.core.Place() + p = base.core.Place() p.set_place(t._place()) - place = fluid.CUDAPlace(p.gpu_device_id()) + place = base.CUDAPlace(p.gpu_device_id()) t.set(ndarray, place) @@ -651,7 +649,7 @@ def _make_program(self, mode): losses = [] metrics = [] - with fluid.program_guard(prog, self._startup_prog): + with base.program_guard(prog, self._startup_prog): inputs = self.model._inputs labels = self.model._labels if self.model._labels else [] inputs = [k._create_feed_layer() for k in to_list(inputs)] @@ -732,11 +730,11 @@ def _compile_and_initialize(self, prog, mode): # even if `forward()` may run different code path for different mode # therefore startup program only needs to run once if self._executor is None: - self._executor = fluid.Executor(place) + self._executor = base.Executor(place) # XXX incremental initialization uninitialized = [] for var_py in self._startup_prog.list_vars(): - var = fluid.global_scope().find_var(var_py.name) + var = base.global_scope().find_var(var_py.name) if ( not var_py.name.startswith('nccl_id') and var @@ -766,7 +764,7 @@ def _compile_and_initialize(self, prog, mode): self.model._optimizer.amp_init(place) if self._nranks < 2: - compiled_prog = fluid.CompiledProgram(prog) + compiled_prog = base.CompiledProgram(prog) else: compiled_prog = prog @@ -935,7 +933,7 @@ def predict_batch(self, inputs): inputs = [to_variable(x) for x in to_list(inputs)] self._input_info = _update_input_info(inputs) outputs = self.model.network(*inputs) - if self._nranks > 1 and isinstance(self.model._place, fluid.CUDAPlace): + if self._nranks > 1 and isinstance(self.model._place, base.CUDAPlace): outputs = [_all_gather(o) for o in to_list(outputs)] return [to_numpy(o) for o in to_list(outputs)] @@ -1026,7 +1024,7 @@ def load(self, param_state_pairs, optim_state, scaler_state=None): if not hasattr(self.model._optimizer, 'set_state_dict'): warnings.warn( - "paddle.fluid.optimizer is deprecated in API 2.0, please use paddle.optimizer instead." + "paddle.base.optimizer is deprecated in API 2.0, please use paddle.optimizer instead." ) self.model._optimizer.set_dict(converted_state) else: @@ -1708,23 +1706,23 @@ def prepare( """ self._place = _get_device() - if isinstance(self._place, fluid.CUDAPlace): + if isinstance(self._place, base.CUDAPlace): global _parallel_context_initialized if ( paddle.distributed.ParallelEnv().nranks > 1 and not _parallel_context_initialized ): if in_dynamic_mode(): - main_prog_seed = fluid.default_main_program().random_seed + main_prog_seed = base.default_main_program().random_seed startup_prog_seed = ( - fluid.default_startup_program().random_seed + base.default_startup_program().random_seed ) - fluid.disable_dygraph() + base.disable_dygraph() paddle.disable_static(self._place) # enable_dygraph would create and switch to a new program, # thus also copy seed to the new program - fluid.default_main_program().random_seed = main_prog_seed - fluid.default_startup_program().random_seed = ( + base.default_main_program().random_seed = main_prog_seed + base.default_startup_program().random_seed = ( startup_prog_seed ) else: @@ -2233,7 +2231,7 @@ def _save_inference_model(self, path): """ if in_dynamic_mode(): - with fluid.framework._dygraph_guard(None): + with base.framework._dygraph_guard(None): layer = self.network if self._input_info is None: # No provided or inferred raise RuntimeError( diff --git a/python/paddle/hapi/model_summary.py b/python/paddle/hapi/model_summary.py index 262547444d8d2..267e938a6e298 100644 --- a/python/paddle/hapi/model_summary.py +++ b/python/paddle/hapi/model_summary.py @@ -240,7 +240,7 @@ def summary(net, input_size=None, dtypes=None, input=None): input_size = [] for key in input.keys(): input_size.append(tuple(input[key].shape)) - elif isinstance(input, paddle.fluid.framework.Variable): + elif isinstance(input, paddle.base.framework.Variable): input_size = tuple(input.shape) else: raise ValueError( @@ -353,9 +353,7 @@ def _build_dtypes(input_size, dtype): depth = len(list(model.sublayers())) def _get_shape_from_tensor(x): - if isinstance( - x, (paddle.fluid.Variable, paddle.fluid.core.eager.Tensor) - ): + if isinstance(x, (paddle.base.Variable, paddle.base.core.eager.Tensor)): return list(x.shape) elif isinstance(x, (list, tuple)): return [_get_shape_from_tensor(xx) for xx in x] diff --git a/python/paddle/hapi/static_flops.py b/python/paddle/hapi/static_flops.py index c6d099d8b8872..21a4359743685 100644 --- a/python/paddle/hapi/static_flops.py +++ b/python/paddle/hapi/static_flops.py @@ -73,7 +73,7 @@ def outputs(self, name): class GraphWrapper: """ - It is a wrapper of paddle.fluid.framework.IrGraph with some special functions + It is a wrapper of paddle.base.framework.IrGraph with some special functions for paddle slim framework. Args: diff --git a/python/paddle/incubate/__init__.py b/python/paddle/incubate/__init__.py index 81f46340d250a..f6eb4377f6033 100644 --- a/python/paddle/incubate/__init__.py +++ b/python/paddle/incubate/__init__.py @@ -16,7 +16,7 @@ from .optimizer import ModelAverage # noqa: F401 from .optimizer import DistributedFusedLamb # noqa: F401 from .checkpoint import auto_checkpoint # noqa: F401 -from ..fluid.layer_helper import LayerHelper # noqa: F401 +from ..base.layer_helper import LayerHelper # noqa: F401 from .operators import softmax_mask_fuse_upper_triangle # noqa: F401 from .operators import softmax_mask_fuse # noqa: F401 from .operators import graph_send_recv diff --git a/python/paddle/incubate/asp/asp.py b/python/paddle/incubate/asp/asp.py index 8ba4966973fed..671bc0251c1e8 100644 --- a/python/paddle/incubate/asp/asp.py +++ b/python/paddle/incubate/asp/asp.py @@ -22,8 +22,8 @@ import numpy as np import paddle -from paddle.fluid import core, global_scope, program_guard -from paddle.fluid.framework import dygraph_only +from paddle.base import core, global_scope, program_guard +from paddle.base.framework import dygraph_only from paddle.incubate import asp from .supported_layer_list import ( @@ -453,7 +453,7 @@ def prune_model(model, n=2, m=4, mask_algo='mask_1d', with_mask=True): if ( hasattr(model, "distributed_info_") and model.distributed_info_["sharding_degree"] > 1 - and paddle.fluid.is_compiled_with_cuda() + and paddle.base.is_compiled_with_cuda() ): gpu_id = int(os.environ.get('FLAGS_selected_gpus', 0)) place = paddle.CUDAPlace(gpu_id) @@ -857,7 +857,7 @@ def _step(cls, optimizer): """ optimizer.step() main_prog = paddle.static.default_main_program() - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): ASPHelper._insert_sparse_mask_ops( main_prog, optimizer._parameter_list ) diff --git a/python/paddle/incubate/asp/supported_layer_list.py b/python/paddle/incubate/asp/supported_layer_list.py index 432adebacc620..b0d420fa36b03 100644 --- a/python/paddle/incubate/asp/supported_layer_list.py +++ b/python/paddle/incubate/asp/supported_layer_list.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle.fluid.log_helper import get_logger +from paddle.base.log_helper import get_logger from paddle.incubate import asp __all__ = [] diff --git a/python/paddle/incubate/autograd/composite_rules.py b/python/paddle/incubate/autograd/composite_rules.py index 1ca8ab62a8363..9123b98ac2054 100644 --- a/python/paddle/incubate/autograd/composite_rules.py +++ b/python/paddle/incubate/autograd/composite_rules.py @@ -20,7 +20,7 @@ import functools import operator -from paddle.fluid import core +from paddle.base import core from .primitives import * # noqa: F403 from .primreg import REGISTER_COMPOSITE, lookup_composite @@ -35,7 +35,7 @@ def _composite(op, *args): def softmax_composite(x, axis): """define composite rule of op softmax""" is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype # Softmax need fp32 compute since it has sum op in dtype = convert_dtype(x.dtype) @@ -78,7 +78,7 @@ def composite_batchnorm( """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -152,7 +152,7 @@ def layernorm_composite(x, scale, bias, epsilon, begin_norm_axis): var = mean((x-mean(x))^2) """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -194,7 +194,7 @@ def instancenorm_composite(x, scale, bias, epsilon): var = mean((x-mean(x))^2) """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -259,7 +259,7 @@ def gelu_composite(x, approximate): def mean_composite(x, axis, keepdim): """define composite rule of op mean""" is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -436,7 +436,7 @@ def dropout_composite(x, seed_tensor, p, is_test, mode, seed, fix_seed): def bernoulli(shape, dtype, p, seed=0): - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype # TODO(jiabin) Fix uniform doesn't support float16 error in CINN new_dtype = ( @@ -493,7 +493,7 @@ def sigmoid_composite(x): res = 1 / (1 + exp(-x)) """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -512,7 +512,7 @@ def silu_composite(x): res = x / (1 + exp(-x)) """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -592,7 +592,7 @@ def sqrt_composite(x): res = pow(x, 0.5) """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -611,7 +611,7 @@ def pow_composite(x, y): res = x^y """ is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -661,7 +661,7 @@ def rsqrt_composite(x): """define composite rule of op rsqrt.""" # rsqrt(x) = x^(-0.5) is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) if dtype in ["float16", "uint16"]: @@ -684,7 +684,7 @@ def group_norm_composite(x, scale, bias, epsilon, groups, data_layout): N, C, H, W = x.shape is_amp = False - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype dtype = convert_dtype(x.dtype) # when inputs are float16 or bfloat16, convert to float32 in computing diff --git a/python/paddle/incubate/autograd/functional.py b/python/paddle/incubate/autograd/functional.py index 116f4e2936fb7..fbf054b05808a 100644 --- a/python/paddle/incubate/autograd/functional.py +++ b/python/paddle/incubate/autograd/functional.py @@ -15,7 +15,7 @@ import typing import paddle -from paddle.fluid import framework +from paddle.base import framework from paddle.incubate.autograd import primapi, utils @@ -582,7 +582,7 @@ def _grad(ys, xs, v=None): # xs_grad when the xs is a signle Tensor. xs_grad = paddle.grad(ys, xs, v, create_graph=True, allow_unused=True) if ( - isinstance(xs, paddle.fluid.framework.Variable) + isinstance(xs, paddle.base.framework.Variable) and isinstance(xs_grad, typing.Sequence) and len(xs_grad) > 0 ): diff --git a/python/paddle/incubate/autograd/primapi.py b/python/paddle/incubate/autograd/primapi.py index ee72cc3bd8f18..cc57f930de4a7 100644 --- a/python/paddle/incubate/autograd/primapi.py +++ b/python/paddle/incubate/autograd/primapi.py @@ -16,8 +16,8 @@ import typing import paddle -from paddle.fluid import backward, core, framework -from paddle.fluid.core import prim_config +from paddle.base import backward, core, framework +from paddle.base.core import prim_config from paddle.incubate.autograd import primx, utils @@ -240,12 +240,12 @@ def to_prim( """ if not core._is_fwd_prim_enabled(): return - if isinstance(blocks, paddle.fluid.framework.Block): + if isinstance(blocks, paddle.base.framework.Block): logging.info("Atomize composite op to primitive ops begin.") main_program = blocks.program elif isinstance(blocks, typing.Sequence): for item in blocks: - if not isinstance(item, paddle.fluid.framework.Block): + if not isinstance(item, paddle.base.framework.Block): raise TypeError( f"Expect block or sequence of blocks, but sequence contains {type(item)}." ) diff --git a/python/paddle/incubate/autograd/primops.py b/python/paddle/incubate/autograd/primops.py index 29a17dda1d458..3b628d410f525 100644 --- a/python/paddle/incubate/autograd/primops.py +++ b/python/paddle/incubate/autograd/primops.py @@ -16,7 +16,7 @@ import operator import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from .primreg import REGISTER_FN @@ -307,7 +307,7 @@ def split(x, num_or_sections, axis=0, outs=None): @REGISTER_FN('concat_p', 'XS', 'Y') def concat(xs, axis=0, out=None): - if isinstance(xs, paddle.fluid.framework.Variable): + if isinstance(xs, paddle.base.framework.Variable): xs = [xs] attrs = {'axis': axis} helper = LayerHelper('concat_p', **locals()) diff --git a/python/paddle/incubate/autograd/primreg.py b/python/paddle/incubate/autograd/primreg.py index 5e94c220acb47..5cedac46320dd 100644 --- a/python/paddle/incubate/autograd/primreg.py +++ b/python/paddle/incubate/autograd/primreg.py @@ -79,7 +79,7 @@ def op_position_inputs(op): .. code-block:: python >>> from paddle.incubate.autograd.primops import _simple_binop - >>> from paddle.fluid.layer_helper import LayerHelper + >>> from paddle.base.layer_helper import LayerHelper >>> from paddle.incubate.autograd.primreg import REGISTER_FN >>> # doctest: +SKIP('Depends on external code.') @@ -126,7 +126,7 @@ def op_position_output(op): >>> # doctest: +SKIP('Depends on external code.') >>> from paddle.incubate.autograd.primops import _simple_binop - >>> from paddle.fluid.layer_helper import LayerHelper + >>> from paddle.base.layer_helper import LayerHelper >>> from paddle.incubate.autograd.primreg import REGISTER_FN >>> @REGISTER_FN('div_p', 'X', 'Y', 'Z') @@ -169,7 +169,7 @@ def REGISTER_FN(op_type, *position_argnames): >>> # doctest: +SKIP('Depends on external code.') >>> from paddle.incubate.autograd.primops import _simple_binop - >>> from paddle.fluid.layer_helper import LayerHelper + >>> from paddle.base.layer_helper import LayerHelper >>> from paddle.incubate.autograd.primreg import REGISTER_FN >>> @REGISTER_FN('tanh_p', 'X', 'Y') @@ -204,7 +204,7 @@ def REGISTER_ORIG2PRIM(op_type): .. code-block:: python >>> # doctest: +SKIP('Depends on external code.') - >>> from paddle.fluid.layer_helper import LayerHelper + >>> from paddle.base.layer_helper import LayerHelper >>> from paddle.incubate.autograd.utils import get_input_var_list >>> from paddle.incubate.autograd import primops >>> from paddle.incubate.autograd.primreg import REGISTER_ORIG2PRIM diff --git a/python/paddle/incubate/autograd/primx.py b/python/paddle/incubate/autograd/primx.py index b61076ed0df22..49c6876640a71 100644 --- a/python/paddle/incubate/autograd/primx.py +++ b/python/paddle/incubate/autograd/primx.py @@ -17,9 +17,9 @@ from collections import OrderedDict import paddle -from paddle.fluid import framework -from paddle.fluid.core import ops_contain_none, prim_config -from paddle.fluid.framework import Operator, default_main_program +from paddle.base import framework +from paddle.base.core import ops_contain_none, prim_config +from paddle.base.framework import Operator, default_main_program from paddle.incubate.autograd.utils import as_tensors from .composite_rules import _composite @@ -141,8 +141,8 @@ def add(self, key_var, value_var): def add_rec(self, key_vars, value_vars): if value_vars is None: return - if isinstance(key_vars, paddle.fluid.framework.Variable): - if not isinstance(value_vars, paddle.fluid.framework.Variable): + if isinstance(key_vars, paddle.base.framework.Variable): + if not isinstance(value_vars, paddle.base.framework.Variable): raise TypeError( f'value_vars must be Variable, but got {type(value_vars)}' ) @@ -212,7 +212,7 @@ def add_vars(self, new_vars): def add_vars_rec(self, new_vars): if new_vars is None: return - if isinstance(new_vars, paddle.fluid.framework.Variable): + if isinstance(new_vars, paddle.base.framework.Variable): self.vars.update({id(new_vars): new_vars}) return if not isinstance(new_vars, list): @@ -246,7 +246,7 @@ def erase_dots(self, vars_to_erase): def var2dot_rec(self, vars): """Lookup var2dot recursively.""" - if isinstance(vars, paddle.fluid.framework.Variable): + if isinstance(vars, paddle.base.framework.Variable): dot = self.var2dot.lookup(vars) return dot @@ -254,7 +254,7 @@ def var2dot_rec(self, vars): return dots def dot2bar_rec(self, dots): - if isinstance(dots, paddle.fluid.framework.Variable): + if isinstance(dots, paddle.base.framework.Variable): bar = self.dot2bar.lookup(dots) assert bar is not None, 'bar must be not None' return bar @@ -509,7 +509,7 @@ def expand_nested_list(xs): attrs = {} for name in sorted(op.attr_names): attrs[name] = op.attr(name) - from paddle.fluid.dygraph.base import param_guard + from paddle.base.dygraph.base import param_guard new_op_desc = block.desc.append_op() with param_guard(inputs), param_guard(outputs): @@ -561,7 +561,7 @@ def bind(args, to_bind, value_table): for i in range(len(args)): if isinstance(args[i], list): bind(args[i], to_bind, value_table) - if not isinstance(args[i], paddle.fluid.framework.Variable): + if not isinstance(args[i], paddle.base.framework.Variable): continue elif args[i] is not None and args[i].name in to_bind: args[i] = value_table[to_bind[args[i].name]] @@ -584,7 +584,7 @@ def expand_nested_list(xs): return_list.append(x) return return_list - if isinstance(block, paddle.fluid.framework.Block): + if isinstance(block, paddle.base.framework.Block): logging.info("Atomize composite op to primitive ops begin.") # Step1: Do some preparatory work for lower diff --git a/python/paddle/incubate/autograd/utils.py b/python/paddle/incubate/autograd/utils.py index 79667cafeaf3e..1aa4912a89bf6 100644 --- a/python/paddle/incubate/autograd/utils.py +++ b/python/paddle/incubate/autograd/utils.py @@ -16,7 +16,7 @@ import paddle import paddle.framework.dtype as dtypes -from paddle.fluid import framework +from paddle.base import framework from .phi_ops_map import op_info, op_map @@ -309,7 +309,7 @@ def map_output_for_composite(op): def flatten(inp): - if inp is None or isinstance(inp, paddle.fluid.framework.Variable): + if inp is None or isinstance(inp, paddle.base.framework.Variable): return [inp] flattened = [] for part in inp: diff --git a/python/paddle/incubate/autotune.py b/python/paddle/incubate/autotune.py index a4b9cbe7006bd..745ac9fc69c07 100644 --- a/python/paddle/incubate/autotune.py +++ b/python/paddle/incubate/autotune.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle.fluid import core +from paddle.base import core __all__ = ['set_config'] diff --git a/python/paddle/incubate/checkpoint/__init__.py b/python/paddle/incubate/checkpoint/__init__.py index 79e6259de0275..115cc0a255a41 100644 --- a/python/paddle/incubate/checkpoint/__init__.py +++ b/python/paddle/incubate/checkpoint/__init__.py @@ -12,6 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ...fluid.incubate.checkpoint import auto_checkpoint # noqa: F401 +from ...base.incubate.checkpoint import auto_checkpoint # noqa: F401 __all__ = [] diff --git a/python/paddle/incubate/distributed/fleet/base.py b/python/paddle/incubate/distributed/fleet/base.py index 81d071bf98226..87af4c7fd4e82 100644 --- a/python/paddle/incubate/distributed/fleet/base.py +++ b/python/paddle/incubate/distributed/fleet/base.py @@ -14,9 +14,9 @@ import abc -from paddle import fluid +from paddle import base +from paddle.base.executor import Executor from paddle.distributed.fleet.base.role_maker import RoleMakerBase -from paddle.fluid.executor import Executor from paddle.optimizer import SGD from paddle.static.amp.decorator import OptimizerWithMixedPrecision @@ -200,7 +200,7 @@ def init(self, role_maker=None): Returns: None """ - self._executor = Executor(fluid.CPUPlace()) + self._executor = Executor(base.CPUPlace()) if role_maker and not isinstance(role_maker, RoleMakerBase): from paddle.incubate.distributed.fleet.role_maker import ( @@ -272,8 +272,8 @@ def save_persistables(self, executor, dirname, main_program=None): class DistributedOptimizer(metaclass=abc.ABCMeta): """ - DistributedOptimizer is a wrapper for paddle.fluid.optimizer - A user should pass a paddle.fluid.optimizer to DistributedOptimizer + DistributedOptimizer is a wrapper for paddle.base.optimizer + A user should pass a paddle.base.optimizer to DistributedOptimizer minimize() function is implemented. DistributedOptimizer is the starting point for a user who wants to run distributed training. The optimized information will be stored in @@ -345,7 +345,7 @@ def apply_gradients(self, params_grads): >>> # doctest: +SKIP('The network is not defined.') >>> loss = network() - >>> optimizer = fluid.optimizer.SGD(learning_rate=0.1) + >>> optimizer = base.optimizer.SGD(learning_rate=0.1) >>> params_grads = optimizer.backward(loss) >>> # you may append operations for params_grads here >>> # ... diff --git a/python/paddle/incubate/distributed/fleet/collective.py b/python/paddle/incubate/distributed/fleet/collective.py index 6b32ef85de3e5..0a63ddb71dffb 100644 --- a/python/paddle/incubate/distributed/fleet/collective.py +++ b/python/paddle/incubate/distributed/fleet/collective.py @@ -16,15 +16,15 @@ import paddle import paddle.distributed.transpiler.distribute_transpiler as dist_transpiler -from paddle import fluid -from paddle.distributed.fleet.meta_optimizers import RawProgramOptimizer -from paddle.fluid.compiler import CompiledProgram -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program -from paddle.fluid.incubate.checkpoint.checkpoint_saver import ( +from paddle import base +from paddle.base.compiler import CompiledProgram +from paddle.base.executor import Executor +from paddle.base.framework import Program +from paddle.base.incubate.checkpoint.checkpoint_saver import ( CheckpointSaver, PaddleModel, ) +from paddle.distributed.fleet.meta_optimizers import RawProgramOptimizer from paddle.incubate.distributed.fleet.base import ( DistributedOptimizer, Fleet, @@ -203,7 +203,7 @@ def load_checkpoint( fleet = Collective() -class DistributedStrategy(fluid.BuildStrategy): +class DistributedStrategy(base.BuildStrategy): """ Init function of DistributedStrategy """ @@ -222,7 +222,7 @@ def __init__(self): self.use_amp = False # use mixed precision optimizer self.amp_loss_scaling = 2**15 - self.exec_strategy = fluid.ExecutionStrategy() + self.exec_strategy = base.ExecutionStrategy() # configurations below are used for unit test self._ut4grad_allreduce = False @@ -258,8 +258,8 @@ def apply_gradients(self, params_grads): class CollectiveOptimizer(DistributedOptimizer): """ - DistributedOptimizer is a wrapper for paddle.fluid.optimizer - A user should pass a paddle.fluid.optimizer to DistributedOptimizer + DistributedOptimizer is a wrapper for paddle.base.optimizer + A user should pass a paddle.base.optimizer to DistributedOptimizer minimize() function is implemented. DistributedOptimizer is the starting point for a user who wants to run distributed training. The optimized information will be stored in @@ -550,7 +550,7 @@ def minimize( main_program = loss.block.program if startup_program is None: - startup_program = fluid.default_startup_program() + startup_program = base.default_startup_program() fleet.startup_program = startup_program self._loss = loss diff --git a/python/paddle/incubate/distributed/fleet/fleet_util.py b/python/paddle/incubate/distributed/fleet/fleet_util.py index 34dc040c45ab3..4b232ea8d2c87 100644 --- a/python/paddle/incubate/distributed/fleet/fleet_util.py +++ b/python/paddle/incubate/distributed/fleet/fleet_util.py @@ -24,9 +24,9 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base +from paddle.base.log_helper import get_logger from paddle.distributed.fleet.utils.fs import HDFSClient -from paddle.fluid.log_helper import get_logger from . import utils @@ -136,8 +136,8 @@ def rank0_error(self, s): def set_zero( self, var_name, - scope=fluid.global_scope(), - place=fluid.CPUPlace(), + scope=base.global_scope(), + place=base.CPUPlace(), param_type="int64", ): """ @@ -145,8 +145,8 @@ def set_zero( Args: var_name(str): name of Variable - scope(Scope): Scope object, default is fluid.global_scope() - place(Place): Place object, default is fluid.CPUPlace() + scope(Scope): Scope object, default is base.global_scope() + place(Place): Place object, default is base.CPUPlace() param_type(str): param data type, default is int64 Examples: @@ -165,7 +165,7 @@ def set_zero( def print_global_auc( self, - scope=fluid.global_scope(), + scope=base.global_scope(), stat_pos="_generated_var_2", stat_neg="_generated_var_3", print_prefix="", @@ -174,7 +174,7 @@ def print_global_auc( Print global auc of all distributed workers. Args: - scope(Scope): Scope object, default is fluid.global_scope() + scope(Scope): Scope object, default is base.global_scope() stat_pos(str): name of auc pos bucket Variable stat_neg(str): name of auc neg bucket Variable print_prefix(str): prefix of print auc @@ -191,11 +191,11 @@ def print_global_auc( >>> # below is part of model >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - >>> similarity_norm = fluid.layers.sigmoid(paddle.clip( + >>> similarity_norm = paddle.nn.functional.sigmoid(paddle.clip( ... emb, min=-15.0, max=15.0), name="similarity_norm") - >>> binary_predict = fluid.layers.concat(input=[ + >>> binary_predict = paddle.concat(input=[ ... paddle.subtract( - ... fluid.layers.ceil(similarity_norm), + ... paddle.ceil(similarity_norm), ... similarity_norm), ... similarity_norm], ... axis=1) @@ -210,7 +210,7 @@ def print_global_auc( def get_global_auc( self, - scope=fluid.global_scope(), + scope=base.global_scope(), stat_pos="_generated_var_2", stat_neg="_generated_var_3", ): @@ -218,7 +218,7 @@ def get_global_auc( Get global auc of all distributed workers. Args: - scope(Scope): Scope object, default is fluid.global_scope() + scope(Scope): Scope object, default is base.global_scope() stat_pos(str): name of auc pos bucket Variable stat_neg(str): name of auc neg bucket Variable @@ -890,8 +890,8 @@ def pull_all_dense_params(self, scope, program): pull all dense params in trainer of rank 0 Args: - scope(Scope): fluid Scope - program(Program): fluid Program + scope(Scope): base Scope + program(Program): base Program Examples: .. code-block:: python @@ -956,9 +956,9 @@ def save_paddle_inference_model( save paddle inference model, and upload to hdfs dnn_plugin path Args: - executor(Executor): fluid Executor - scope(Scope): fluid Scope - program(Program): fluid Program + executor(Executor): base Executor + scope(Scope): base Scope + program(Program): base Program feeded_vars(list[Variable]): feed vars target_vars(list[variable]): fetch vars output_path(str): hdfs/afs output path @@ -994,7 +994,7 @@ def save_paddle_inference_model( # pull dense before save self.pull_all_dense_params(scope, program) if fleet.worker_index() == 0: - with fluid.scope_guard(scope): + with base.scope_guard(scope): if save_combine: paddle.static.io.save_inference_model( model_name, @@ -1052,9 +1052,9 @@ def save_paddle_params( save paddle model, and upload to hdfs dnn_plugin path Args: - executor(Executor): fluid Executor - scope(Scope): fluid Scope - program(Program): fluid Program + executor(Executor): base Executor + scope(Scope): base Scope + program(Program): base Program model_name(str): save model local dir or filename output_path(str): hdfs/afs output path day(str|int): training day @@ -1111,7 +1111,7 @@ def save_paddle_params( self.pull_all_dense_params(scope, program) if fleet.worker_index() == 0: vars = [program.global_block().var(i) for i in var_names] - with fluid.scope_guard(scope): + with base.scope_guard(scope): if save_combine: paddle.static.io.save_vars( executor, "./", program, vars=vars, filename=model_name @@ -1362,7 +1362,7 @@ def get_online_pass_interval( def get_global_metrics( self, - scope=fluid.global_scope(), + scope=base.global_scope(), stat_pos_name="_generated_var_2", stat_neg_name="_generated_var_3", sqrerr_name="sqrerr", @@ -1377,7 +1377,7 @@ def get_global_metrics( actual_ctr, predicted_ctr, copc, mean_predict_qvalue, total_ins_num. Args: - scope(Scope): Scope object, default is fluid.global_scope() + scope(Scope): Scope object, default is base.global_scope() stat_pos_name(str): name of auc pos bucket Variable stat_neg_name(str): name of auc neg bucket Variable sqrerr_name(str): name of sqrerr Variable @@ -1412,11 +1412,11 @@ def get_global_metrics( >>> label = paddle.static.data(name="click", shape=[-1, 1],\ ... dtype="int64", lod_level=0) >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - >>> similarity_norm = fluid.layers.sigmoid(paddle.clip(\ + >>> similarity_norm = paddle.nn.functional.sigmoid(paddle.clip(\ ... emb, min=-15.0, max=15.0), name="similarity_norm")\ - >>> binary_predict = fluid.layers.concat(input=[\ + >>> binary_predict = paddle.concat(input=[\ ... paddle.subtract(\ - ... fluid.layers.ceil(similarity_norm), similarity_norm),\ + ... paddle.ceil(similarity_norm), similarity_norm),\ ... similarity_norm], axis=1) >>> auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ ... stat_neg] = paddle.static.auc(input=binary_predict,\ @@ -1566,7 +1566,7 @@ def get_metric(name): def print_global_metrics( self, - scope=fluid.global_scope(), + scope=base.global_scope(), stat_pos_name="_generated_var_2", stat_neg_name="_generated_var_3", sqrerr_name="sqrerr", @@ -1582,7 +1582,7 @@ def print_global_metrics( actual_ctr, predicted_ctr, copc, mean_predict_qvalue, total_ins_num. Args: - scope(Scope): Scope object, default is fluid.global_scope() + scope(Scope): Scope object, default is base.global_scope() stat_pos_name(str): name of auc pos bucket Variable stat_neg_name(str): name of auc neg bucket Variable sqrerr_name(str): name of sqrerr Variable @@ -1614,11 +1614,11 @@ def print_global_metrics( >>> label = paddle.static.data(name="click", shape=[-1, 1],\ ... dtype="int64", lod_level=0) >>> emb = my_slot_net(slots, label) # emb can be fc layer of size 1 - >>> similarity_norm = fluid.layers.sigmoid(paddle.clip(\ + >>> similarity_norm = paddle.nn.functional.sigmoid(paddle.clip(\ ... emb, min=-15.0, max=15.0), name="similarity_norm")\ - >>> binary_predict = fluid.layers.concat(input=[\ + >>> binary_predict = paddle.concat(input=[\ ... paddle.subtract(\ - ... fluid.layers.ceil(similarity_norm), similarity_norm),\ + ... paddle.ceil(similarity_norm), similarity_norm),\ ... similarity_norm], axis=1) >>> auc, batch_auc, [batch_stat_pos, batch_stat_neg, stat_pos, \ ... stat_neg] = paddle.static.auc(input=binary_predict,\ diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/__init__.py b/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/__init__.py index b3adb88271a75..d590fe145ebc3 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/__init__.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/__init__.py @@ -27,7 +27,7 @@ Program, Executor, ) -from paddle.fluid.compiler import CompiledProgram +from paddle.base.compiler import CompiledProgram from paddle.distributed.transpiler.distribute_transpiler import ( DistributeTranspilerConfig, @@ -812,8 +812,8 @@ def is_valid(var): class ParameterServerOptimizer(DistributedOptimizer): """ - DistributedOptimizer is a wrapper for paddle.fluid.optimizer - A user should pass a paddle.fluid.optimizer to DistributedOptimizer + DistributedOptimizer is a wrapper for paddle.base.optimizer + A user should pass a paddle.base.optimizer to DistributedOptimizer minimize() function is implemented. DistributedOptimizer is the starting point for a user who wants to run distributed training. The optimized information will be stored in diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/distributed_strategy.py b/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/distributed_strategy.py index 87cb5fa1d3b11..b7158f41e9892 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/distributed_strategy.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/distribute_transpiler/distributed_strategy.py @@ -16,7 +16,7 @@ import os -from paddle import fluid +from paddle import base from paddle.distributed.transpiler.distribute_transpiler import ( DistributeTranspilerConfig, ServerRuntimeConfig, @@ -171,13 +171,13 @@ def __init__(self): self._server_runtime_config = ServerRuntimeConfig() num_threads = int(os.getenv("CPU_NUM", "1")) - self._execute_strategy = fluid.ExecutionStrategy() - self._build_strategy = fluid.BuildStrategy() + self._execute_strategy = base.ExecutionStrategy() + self._build_strategy = base.BuildStrategy() self._execute_strategy.num_threads = num_threads if num_threads > 1: self._build_strategy.reduce_strategy = ( - fluid.BuildStrategy.ReduceStrategy.Reduce + base.BuildStrategy.ReduceStrategy.Reduce ) self.debug_opt = None self.use_ps_gpu = False @@ -287,7 +287,7 @@ def get_execute_strategy(self): return self._execute_strategy def set_execute_strategy(self, config): - if isinstance(config, fluid.ExecutionStrategy): + if isinstance(config, base.ExecutionStrategy): self._execute_strategy = config elif isinstance(config, dict): for key in config: @@ -312,7 +312,7 @@ def get_build_strategy(self): return self._build_strategy def set_build_strategy(self, config): - if isinstance(config, fluid.BuildStrategy): + if isinstance(config, base.BuildStrategy): self._build_strategy = config elif isinstance(config, dict): for key in config: diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/ir/public.py b/python/paddle/incubate/distributed/fleet/parameter_server/ir/public.py index 75d65dc079e09..628d4f580bbad 100755 --- a/python/paddle/incubate/distributed/fleet/parameter_server/ir/public.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/ir/public.py @@ -148,7 +148,7 @@ def __init__(self, main_program, startup_program, strategy, role_maker): self.is_heter_ps_mode = role_maker._is_heter_parameter_server_mode except: warnings.warn( - "Using paddle.distributed.fleet instead of paddle.fluid.incubate.fleet" + "Using paddle.distributed.fleet instead of paddle.base.incubate.fleet" ) self.is_heter_ps_mode = False diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/ir/vars_metatools.py b/python/paddle/incubate/distributed/fleet/parameter_server/ir/vars_metatools.py index 20914fda33705..99f753373ef2f 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/ir/vars_metatools.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/ir/vars_metatools.py @@ -157,7 +157,7 @@ def equal(var1, var2): def __str__(self): origin_var_str = ( - "{name} : fluid.{type}.shape{shape}.astype({dtype})".format( + "{name} : base.{type}.shape{shape}.astype({dtype})".format( name=self.origin.name, type=self.origin.type, shape=self.origin.shape, @@ -166,7 +166,7 @@ def __str__(self): ) slice_var_str = ( - "{name} : fluid.{type}.shape{shape}.astype({dtype})" + "{name} : base.{type}.shape{shape}.astype({dtype})" ".slice({is_slice}).block({block_id}).offset({offset})".format( name=self.slice.name, type=self.slice.type, diff --git a/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py b/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py index 3f36562b2adc2..5dded6c9b8183 100644 --- a/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py +++ b/python/paddle/incubate/distributed/fleet/parameter_server/pslib/__init__.py @@ -592,7 +592,7 @@ def shrink_dense_table(self, decay, emb_dim=11, scope=None, table_id=None): Args: decay (float): The decay rate, usually range in (0, 1). emb_dim (int, optional): One element's length in datanorm layer. Default is 11. - scope (Scope, optional): Scope object, default is fluid.global_scope(). Default is None. + scope (Scope, optional): Scope object, default is base.global_scope(). Default is None. table_id (int, optional): Table id of shrinking dense table. None means shrink all, you should specify it when using multiple scopes, default is None. @@ -692,7 +692,7 @@ def load_pslib_whitelist(self, table_id, model_path, **kwargs): # below is how to save proto binary file with open("my_program.bin", "wb") as fout: - my_program = fluid.default_main_program() + my_program = base.default_main_program() fout.write(my_program.desc.serialize_to_string()) """ @@ -736,7 +736,7 @@ def load_one_table(self, table_id, model_path, **kwargs): load_combine = False) # below is how to save proto binary file with open("my_program.bin", "wb") as fout: - my_program = fluid.default_main_program() + my_program = base.default_main_program() fout.write(my_program.desc.serialize_to_string()) """ @@ -1228,7 +1228,7 @@ class fleet_embedding: size=[-1, 11], is_sparse=True, is_distributed=True, - param_attr=fluid.ParamAttr(name="embedding")) + param_attr=base.ParamAttr(name="embedding")) """ def __init__(self, click_name, scale_sparse_grad=True): @@ -1257,8 +1257,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): class DownpourOptimizer(DistributedOptimizer): """ - DistributedOptimizer is a wrapper for paddle.fluid.optimizer - A user should pass a paddle.fluid.optimizer to DistributedOptimizer + DistributedOptimizer is a wrapper for paddle.base.optimizer + A user should pass a paddle.base.optimizer to DistributedOptimizer minimize() function is implemented. DistributedOptimizer is the starting point for a user who wants to run distributed training. The optimized information will be stored in diff --git a/python/paddle/incubate/distributed/fleet/role_maker.py b/python/paddle/incubate/distributed/fleet/role_maker.py index f15a5b6598ef9..920f04ac3fa73 100644 --- a/python/paddle/incubate/distributed/fleet/role_maker.py +++ b/python/paddle/incubate/distributed/fleet/role_maker.py @@ -17,7 +17,7 @@ import time from multiprocessing import Manager, Process -from paddle import fluid +from paddle import base __all__ = [] @@ -664,7 +664,7 @@ def generate_role(self): self._node_type = 1 self._cur_endpoint = worker_endpoints[current_id] if self._is_barrier_all: - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() gloo.set_rank(current_id) gloo.set_size(len(worker_endpoints)) gloo.set_prefix(self._prefix) @@ -687,7 +687,7 @@ def generate_role(self): gloo.init() self._node_type_comm = gloo if self._use_ps_gpu or self._use_metric: - Gloo_strategy = fluid.core.GlooParallelStrategy() + Gloo_strategy = base.core.GlooParallelStrategy() Gloo_strategy.rank = current_id Gloo_strategy.rank_num = len(worker_endpoints) Gloo_strategy.ip_address = self._http_ip_port[0] @@ -698,7 +698,7 @@ def generate_role(self): Default_init_timeout_seconds ) Gloo_strategy.run_seconds = Default_run_timeout_seconds - Gloo = fluid.core.GlooParallelContext(Gloo_strategy) + Gloo = base.core.GlooParallelContext(Gloo_strategy) Gloo.init() else: self._all_comm = MockBarrier() @@ -715,7 +715,7 @@ def generate_role(self): current_id = eplist.index(cur_endpoint) self._node_type = 0 self._cur_endpoint = cur_endpoint - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() gloo.set_rank(current_id) gloo.set_size(len(eplist)) gloo.set_prefix(self._prefix) @@ -738,7 +738,7 @@ def generate_role(self): gloo.init() self._node_type_comm = gloo - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() all_list = worker_endpoints + eplist gloo.set_rank(all_list.index(self._cur_endpoint)) gloo.set_size(len(all_list)) @@ -1071,7 +1071,7 @@ def generate_role(self): current_id = int(os.environ["PADDLE_TRAINER_ID"]) self._node_type = 1 self._cur_endpoint = worker_endpoints[current_id] - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() gloo.set_rank(current_id) gloo.set_size(len(worker_endpoints)) @@ -1092,7 +1092,7 @@ def generate_role(self): current_id = int(os.environ["PADDLE_XPU_ID"]) self._node_type = 2 self._cur_endpoint = xpu_endpoints[current_id] - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() gloo.set_rank(current_id) gloo.set_size(len(xpu_endpoints)) @@ -1121,7 +1121,7 @@ def generate_role(self): current_id = eplist.index(cur_endpoint) self._node_type = 0 self._cur_endpoint = cur_endpoint - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() gloo.set_rank(current_id) gloo.set_size(len(eplist)) gloo.set_prefix(self._prefix) @@ -1138,7 +1138,7 @@ def generate_role(self): self._node_type_comm = gloo if training_role == "TRAINER" or training_role == "XPU": - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() heter_list = worker_endpoints + xpu_endpoints gloo.set_rank(heter_list.index(self._cur_endpoint)) @@ -1156,7 +1156,7 @@ def generate_role(self): gloo.init() self._heter_comm = gloo - gloo = fluid.core.Gloo() + gloo = base.core.Gloo() all_list = worker_endpoints + eplist + xpu_endpoints gloo.set_rank(all_list.index(self._cur_endpoint)) diff --git a/python/paddle/incubate/distributed/fleet/utils.py b/python/paddle/incubate/distributed/fleet/utils.py index ca30f1caff128..2be2ac7161071 100644 --- a/python/paddle/incubate/distributed/fleet/utils.py +++ b/python/paddle/incubate/distributed/fleet/utils.py @@ -21,11 +21,11 @@ from google.protobuf import text_format import paddle -from paddle import fluid +from paddle import base +from paddle.base import core +from paddle.base.framework import Program +from paddle.base.proto import framework_pb2 from paddle.distributed.fleet.base.util_factory import draw_block_graphviz -from paddle.fluid import core -from paddle.fluid.framework import Program -from paddle.fluid.proto import framework_pb2 from paddle.framework import io_utils __all__ = [ @@ -168,21 +168,21 @@ def append_load_op(block, var, path): def save_var(np_array, var_name, shape_list, dtype, save_path): - program = fluid.Program() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + program = base.Program() + place = base.CPUPlace() + exe = base.Executor(place) shape = list(shape_list) - with fluid.program_guard(program): + with base.program_guard(program): d0_data = paddle.static.data(var_name, shape=shape, dtype=dtype) append_save_op(program.global_block(), d0_data, save_path) exe.run(feed={var_name: np_array}, fetch_list=[]) def load_var(var_name, shape_list, dtype, save_path): - program = fluid.Program() - place = fluid.CPUPlace() - exe = fluid.Executor(place) - with fluid.program_guard(program): + program = base.Program() + place = base.CPUPlace() + exe = base.Executor(place) + with base.program_guard(program): d0_data = paddle.static.data(var_name, shape=shape_list, dtype=dtype) append_load_op(program.global_block(), d0_data, save_path) outs = exe.run(feed={}, fetch_list=[d0_data]) @@ -230,10 +230,10 @@ def try_load_model_vars( save_filename, saved_params, ): - place = fluid.CPUPlace() - exe = fluid.Executor(place) - scope = fluid.core.Scope() - with fluid.scope_guard(scope): + place = base.CPUPlace() + exe = base.Executor(place) + scope = base.core.Scope() + with base.scope_guard(scope): if is_text_dump_program: dump_prog_fn = program_type_trans( dump_dir, dump_prog_fn, is_text_dump_program @@ -256,7 +256,7 @@ def try_load_model_vars( for each_var in saved_params } for each_var in saved_params: - var_temp = fluid.global_scope().find_var(each_var.name) + var_temp = base.global_scope().find_var(each_var.name) assert var_temp is not None, "can't not find var: " + each_var.name new_shape = (np.array(var_temp.get_tensor())).shape assert each_var.name in orig_para_shape, ( @@ -381,7 +381,7 @@ def try_load_model_vars( dtype=feed_config.feeded_vars_types[i], ) feed_tensors.append( - fluid.create_lod_tensor(t, [[1] * batch_size], place) + base.create_lod_tensor(t, [[1] * batch_size], place) ) else: raise RuntimeError( @@ -408,7 +408,7 @@ def try_load_model_vars( ) for i in range(len(feed_config.feeded_vars_names)) ] - feeder = fluid.DataFeeder(feed_list=feed_vars, place=place) + feeder = base.DataFeeder(feed_list=feed_vars, place=place) batch_feed = feed_gen( batch_size, feed_config.feeded_vars_dims, diff --git a/python/paddle/incubate/distributed/utils/io/dist_load.py b/python/paddle/incubate/distributed/utils/io/dist_load.py index 5815b0237baef..4d1ce2a40ff6e 100644 --- a/python/paddle/incubate/distributed/utils/io/dist_load.py +++ b/python/paddle/incubate/distributed/utils/io/dist_load.py @@ -17,8 +17,8 @@ import paddle import paddle.distributed as dist +from paddle.base.framework import dygraph_only from paddle.distributed import fleet -from paddle.fluid.framework import dygraph_only @dygraph_only diff --git a/python/paddle/incubate/distributed/utils/io/dist_save.py b/python/paddle/incubate/distributed/utils/io/dist_save.py index ce0d2d05042d9..94b07ed728cd2 100644 --- a/python/paddle/incubate/distributed/utils/io/dist_save.py +++ b/python/paddle/incubate/distributed/utils/io/dist_save.py @@ -18,9 +18,9 @@ import paddle import paddle.distributed as dist +from paddle.base.framework import dygraph_only from paddle.distributed import fleet from paddle.distributed.fleet.utils.log_util import logger -from paddle.fluid.framework import dygraph_only from .save_for_auto import save_for_auto_inference diff --git a/python/paddle/incubate/distributed/utils/io/save_for_auto.py b/python/paddle/incubate/distributed/utils/io/save_for_auto.py index 2a7749947995d..5dd12d41218a6 100644 --- a/python/paddle/incubate/distributed/utils/io/save_for_auto.py +++ b/python/paddle/incubate/distributed/utils/io/save_for_auto.py @@ -21,12 +21,12 @@ import paddle import paddle.distributed as dist +from paddle.base.framework import dygraph_only from paddle.distributed import fleet from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage3 import ( GroupShardedStage3, ) from paddle.distributed.fleet.utils.log_util import logger -from paddle.fluid.framework import dygraph_only __all__ = ["save_for_auto_inference"] diff --git a/python/paddle/incubate/layers/nn.py b/python/paddle/incubate/layers/nn.py index 1777aaa9a4b24..167b52cde2693 100644 --- a/python/paddle/incubate/layers/nn.py +++ b/python/paddle/incubate/layers/nn.py @@ -21,15 +21,15 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid import core, unique_name -from paddle.fluid.data_feeder import ( +from paddle.base import core, unique_name +from paddle.base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, ) -from paddle.fluid.framework import Variable, convert_np_dtype_to_dtype_ -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.param_attr import ParamAttr +from paddle.base.framework import Variable, convert_np_dtype_to_dtype_ +from paddle.base.layer_helper import LayerHelper +from paddle.base.param_attr import ParamAttr __all__ = [] @@ -1134,7 +1134,7 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): """ :alias_main: paddle.nn.functional.bilateral_slice :alias: paddle.nn.functional.bilateral_slice,paddle.nn.functional.vision.bilateral_slice - :old_api: paddle.fluid.layers.bilateral_slice + :old_api: paddle.base.layers.bilateral_slice This operation implements bilateral slicing on the input according to the guide map. For more information of bilateral slicing, please refer to Deep Bilateral Learning for Real-Time Image Enhancement _ diff --git a/python/paddle/incubate/multiprocessing/reductions.py b/python/paddle/incubate/multiprocessing/reductions.py index 5bc1e21186212..520aa0a2b24ee 100644 --- a/python/paddle/incubate/multiprocessing/reductions.py +++ b/python/paddle/incubate/multiprocessing/reductions.py @@ -75,14 +75,14 @@ def _cuda_from_cache(key): def _rebuild_tensor(cls, lodtensor, metadata): - if cls == paddle.fluid.framework.EagerParamBase: - tensor = paddle.fluid.framework.EagerParamBase( + if cls == paddle.base.framework.EagerParamBase: + tensor = paddle.base.framework.EagerParamBase( lodtensor.shape(), lodtensor._dtype(), **metadata ) tensor.value().get_tensor()._share_data_with(lodtensor) else: size, stop_gradient = metadata - tensor = paddle.fluid.core.eager.Tensor() + tensor = paddle.base.core.eager.Tensor() if lodtensor._is_initialized(): tensor.value().get_tensor()._share_data_with(lodtensor) else: @@ -104,7 +104,7 @@ def _reduce_tensor(tensor): or tensor.place.is_gpu_place() or tensor.place.is_cuda_pinned_place() ): - if type(tensor) == paddle.fluid.framework.EagerParamBase: + if type(tensor) == paddle.base.framework.EagerParamBase: metadata = copy.deepcopy(tensor.__dict__) else: metadata = (tensor.size, tensor.stop_gradient) @@ -138,7 +138,7 @@ def _rebuild_cuda_tensor( # you should manualy maintian the lifecycle of ipc tensor shared_cache[(handle, offset_bytes)] = lodtensor else: - lodtensor = paddle.fluid.core.LoDTensor() + lodtensor = paddle.base.core.LoDTensor() lodtensor._share_buffer_with( cache_tensor, (size, type_idx, dims, lod, device_idx) ) @@ -184,8 +184,8 @@ def init_reductions(): return ForkingPickler.register(paddle.Tensor, _reduce_tensor) - ForkingPickler.register(paddle.fluid.core.eager.Tensor, _reduce_tensor) + ForkingPickler.register(paddle.base.core.eager.Tensor, _reduce_tensor) ForkingPickler.register( - paddle.fluid.framework.EagerParamBase, _reduce_tensor + paddle.base.framework.EagerParamBase, _reduce_tensor ) - ForkingPickler.register(paddle.fluid.core.LoDTensor, _reduce_lodtensor) + ForkingPickler.register(paddle.base.core.LoDTensor, _reduce_lodtensor) diff --git a/python/paddle/incubate/nn/functional/fused_dropout_add.py b/python/paddle/incubate/nn/functional/fused_dropout_add.py index 4c47a2302ef6c..d191f1682fdda 100644 --- a/python/paddle/incubate/nn/functional/fused_dropout_add.py +++ b/python/paddle/incubate/nn/functional/fused_dropout_add.py @@ -14,8 +14,8 @@ from paddle import _C_ops +from paddle.base import core from paddle.common_ops_import import default_main_program -from paddle.fluid import core from paddle.framework import LayerHelper, in_dynamic_mode diff --git a/python/paddle/incubate/nn/functional/fused_ec_moe.py b/python/paddle/incubate/nn/functional/fused_ec_moe.py index 9f067acbb0de0..3b4b85029fadd 100644 --- a/python/paddle/incubate/nn/functional/fused_ec_moe.py +++ b/python/paddle/incubate/nn/functional/fused_ec_moe.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper def fused_ec_moe( diff --git a/python/paddle/incubate/nn/functional/fused_matmul_bias.py b/python/paddle/incubate/nn/functional/fused_matmul_bias.py index 526f8a3fec05f..83d3b5a91d4ba 100644 --- a/python/paddle/incubate/nn/functional/fused_matmul_bias.py +++ b/python/paddle/incubate/nn/functional/fused_matmul_bias.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.tensor.linalg import matmul diff --git a/python/paddle/incubate/nn/functional/fused_transformer.py b/python/paddle/incubate/nn/functional/fused_transformer.py index 469aea26cc600..355b5916b5ddb 100644 --- a/python/paddle/incubate/nn/functional/fused_transformer.py +++ b/python/paddle/incubate/nn/functional/fused_transformer.py @@ -13,10 +13,10 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid import core -from paddle.fluid.data_feeder import check_dtype, check_variable_and_dtype -from paddle.fluid.framework import default_main_program -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core +from paddle.base.data_feeder import check_dtype, check_variable_and_dtype +from paddle.base.framework import default_main_program +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/incubate/nn/layer/fused_transformer.py b/python/paddle/incubate/nn/layer/fused_transformer.py index a3f3ea1d83af4..a09cc521a0e79 100644 --- a/python/paddle/incubate/nn/layer/fused_transformer.py +++ b/python/paddle/incubate/nn/layer/fused_transformer.py @@ -14,10 +14,10 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.core import VarDesc -from paddle.fluid.dygraph import no_grad -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base import core +from paddle.base.core import VarDesc +from paddle.base.dygraph import no_grad +from paddle.base.framework import convert_np_dtype_to_dtype_ from paddle.framework import in_dynamic_mode from paddle.incubate.nn import functional as incubate_f from paddle.nn import Layer @@ -66,7 +66,7 @@ def _to_dtype(t, dtype): t_used = t if dtype is not None and dtype != t_used.dtype: - with paddle.fluid.framework._dygraph_place_guard(place=t_used.place): + with paddle.base.framework._dygraph_place_guard(place=t_used.place): t_casted = t_used.cast(dtype=dtype) else: t_casted = t_used diff --git a/python/paddle/incubate/nn/layer/io.py b/python/paddle/incubate/nn/layer/io.py index 3d8b8ec1fe2fd..7eef4832c5b17 100644 --- a/python/paddle/incubate/nn/layer/io.py +++ b/python/paddle/incubate/nn/layer/io.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from ....fluid.framework import Variable +from ....base.framework import Variable from ....framework import LayerHelper, core diff --git a/python/paddle/incubate/nn/loss.py b/python/paddle/incubate/nn/loss.py index d31fe41d8ce3f..0a8043b445b82 100644 --- a/python/paddle/incubate/nn/loss.py +++ b/python/paddle/incubate/nn/loss.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/python/paddle/incubate/nn/memory_efficient_attention.py b/python/paddle/incubate/nn/memory_efficient_attention.py index 565809cf7df48..eff81e25ec454 100644 --- a/python/paddle/incubate/nn/memory_efficient_attention.py +++ b/python/paddle/incubate/nn/memory_efficient_attention.py @@ -21,7 +21,7 @@ import paddle from paddle import _C_ops -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from .attn_bias import ( diff --git a/python/paddle/incubate/operators/graph_khop_sampler.py b/python/paddle/incubate/operators/graph_khop_sampler.py index 9cade59f1fff3..45555786eb02d 100644 --- a/python/paddle/incubate/operators/graph_khop_sampler.py +++ b/python/paddle/incubate/operators/graph_khop_sampler.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/python/paddle/incubate/operators/graph_reindex.py b/python/paddle/incubate/operators/graph_reindex.py index 26f55b0fa2ec3..abba95b97ca03 100644 --- a/python/paddle/incubate/operators/graph_reindex.py +++ b/python/paddle/incubate/operators/graph_reindex.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.utils import deprecated diff --git a/python/paddle/incubate/operators/graph_sample_neighbors.py b/python/paddle/incubate/operators/graph_sample_neighbors.py index 66e8d5f2fa5b7..53f93287083cb 100644 --- a/python/paddle/incubate/operators/graph_sample_neighbors.py +++ b/python/paddle/incubate/operators/graph_sample_neighbors.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.utils import deprecated diff --git a/python/paddle/incubate/operators/graph_send_recv.py b/python/paddle/incubate/operators/graph_send_recv.py index 84bc656f348a9..871d3a28b5ca7 100644 --- a/python/paddle/incubate/operators/graph_send_recv.py +++ b/python/paddle/incubate/operators/graph_send_recv.py @@ -16,14 +16,14 @@ import paddle from paddle import _C_ops -from paddle.fluid.data_feeder import ( +from paddle.base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, convert_dtype, ) -from paddle.fluid.framework import Variable -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.framework import Variable +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.utils import deprecated diff --git a/python/paddle/incubate/operators/resnet_unit.py b/python/paddle/incubate/operators/resnet_unit.py index a02201180c762..54d07d3292506 100644 --- a/python/paddle/incubate/operators/resnet_unit.py +++ b/python/paddle/incubate/operators/resnet_unit.py @@ -15,9 +15,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.param_attr import ParamAttr +from paddle import base +from paddle.base.layer_helper import LayerHelper +from paddle.base.param_attr import ParamAttr from paddle.nn import Layer from paddle.nn import initializer as I @@ -50,8 +50,8 @@ def resnet_unit( act, ): helper = LayerHelper('resnet_unit', **locals()) - bn_param_dtype = fluid.core.VarDesc.VarType.FP32 - bit_mask_dtype = fluid.core.VarDesc.VarType.INT32 + bn_param_dtype = base.core.VarDesc.VarType.FP32 + bit_mask_dtype = base.core.VarDesc.VarType.INT32 out = helper.create_variable_for_type_inference(x.dtype) bit_mask = helper.create_variable_for_type_inference( dtype=bit_mask_dtype, stop_gradient=True @@ -213,7 +213,7 @@ def _get_default_param_initializer(channels): is_nchw = data_format == 'NCHW' # initial filter - bn_param_dtype = fluid.core.VarDesc.VarType.FP32 + bn_param_dtype = base.core.VarDesc.VarType.FP32 if not is_nchw: bn_param_shape = [1, 1, 1, num_filters] filter_x_shape = [ diff --git a/python/paddle/incubate/operators/softmax_mask_fuse.py b/python/paddle/incubate/operators/softmax_mask_fuse.py index c9a3539ee09e0..4a4e01d816272 100644 --- a/python/paddle/incubate/operators/softmax_mask_fuse.py +++ b/python/paddle/incubate/operators/softmax_mask_fuse.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py b/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py index ae02603f27f83..9a0388614b061 100644 --- a/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py +++ b/python/paddle/incubate/operators/softmax_mask_fuse_upper_triangle.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle import _legacy_C_ops -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/python/paddle/incubate/operators/unzip.py b/python/paddle/incubate/operators/unzip.py index 68a491300c5d2..2edf3a392025d 100644 --- a/python/paddle/incubate/operators/unzip.py +++ b/python/paddle/incubate/operators/unzip.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper def unzip(input, lod): diff --git a/python/paddle/incubate/optimizer/distributed_fused_lamb.py b/python/paddle/incubate/optimizer/distributed_fused_lamb.py index 9f2873fe81b09..fe6442756e3c7 100644 --- a/python/paddle/incubate/optimizer/distributed_fused_lamb.py +++ b/python/paddle/incubate/optimizer/distributed_fused_lamb.py @@ -15,10 +15,10 @@ import os import paddle -from paddle.fluid import core, unique_name -from paddle.fluid.executor import global_scope -from paddle.fluid.framework import Variable, name_scope -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core, unique_name +from paddle.base.executor import global_scope +from paddle.base.framework import Variable, name_scope +from paddle.base.layer_helper import LayerHelper from paddle.nn import ClipGradByGlobalNorm from paddle.optimizer import Optimizer diff --git a/python/paddle/incubate/optimizer/functional/utils.py b/python/paddle/incubate/optimizer/functional/utils.py index 674c56c0530fb..52edef7177ae9 100644 --- a/python/paddle/incubate/optimizer/functional/utils.py +++ b/python/paddle/incubate/optimizer/functional/utils.py @@ -13,8 +13,8 @@ # limitations under the License. import paddle -from paddle.fluid.data_feeder import check_type -from paddle.fluid.framework import Variable +from paddle.base.data_feeder import check_type +from paddle.base.framework import Variable def check_input_type(input, name, op_name): diff --git a/python/paddle/incubate/optimizer/gradient_merge.py b/python/paddle/incubate/optimizer/gradient_merge.py index bd99b1aebb779..c407780f50945 100644 --- a/python/paddle/incubate/optimizer/gradient_merge.py +++ b/python/paddle/incubate/optimizer/gradient_merge.py @@ -14,8 +14,8 @@ import paddle -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle.base import core +from paddle.base.framework import ( Variable, default_main_program, default_startup_program, @@ -51,7 +51,7 @@ class GradientMergeOptimizer: .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np def gen_data(batch_size): @@ -75,13 +75,13 @@ def mlp(input_x, input_y, hid_dim=128, label_dim=2): sgd = paddle.incubate.optimizer.GradientMergeOptimizer(sgd, k_steps=4, avg=True) sgd.minimize(cost) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) for i in range(10): cost_val = exe.run(feed=gen_data(32), - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[cost.name]) print("step=%d, cost=%f" % (i, cost_val[0])) """ diff --git a/python/paddle/incubate/optimizer/lars_momentum.py b/python/paddle/incubate/optimizer/lars_momentum.py index 57055b4a923cc..5df9160d3d11b 100644 --- a/python/paddle/incubate/optimizer/lars_momentum.py +++ b/python/paddle/incubate/optimizer/lars_momentum.py @@ -14,8 +14,8 @@ import warnings from paddle import _legacy_C_ops -from paddle.fluid import framework -from paddle.fluid.framework import in_dygraph_mode +from paddle.base import framework +from paddle.base.framework import in_dygraph_mode from paddle.optimizer import Optimizer @@ -44,14 +44,14 @@ class LarsMomentumOptimizer(Optimizer): This parameter is required in dygraph mode. \ The default value is None in static graph mode, at this time all parameters will be updated. regularization (WeightDecayRegularizer, optional): The strategy of regularization. There are two method: \ - :ref:`api_fluid_regularizer_L1Decay` , :ref:`api_fluid_regularizer_L2Decay` . If a parameter has set \ - regularizer using :ref:`api_fluid_ParamAttr` already, the regularization setting here in optimizer will be \ + :ref:`api_base_regularizer_L1Decay` , :ref:`api_base_regularizer_L2Decay` . If a parameter has set \ + regularizer using :ref:`api_base_ParamAttr` already, the regularization setting here in optimizer will be \ ignored for this parameter. Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three cliping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): This parameter is used by developers to print debugging information. \ For details, please refer to :ref:`api_guide_Name`. Default is None. exclude_from_weight_decay (list[str], optional): Name string of layers which will be exclude from lars weight decay. Default is None. @@ -64,7 +64,7 @@ class LarsMomentumOptimizer(Optimizer): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -73,11 +73,11 @@ class LarsMomentumOptimizer(Optimizer): name="inp", shape=[2, 2], dtype='float32') out = paddle.static.nn.fc(inp, size=3) out = paddle.sum(out) - optimizer = fluid.optimizer.LarsMomentumOptimizer(learning_rate=0.001, momentum=0.9) + optimizer = base.optimizer.LarsMomentumOptimizer(learning_rate=0.001, momentum=0.9) optimizer.minimize(out) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) exe.run( feed={"inp": np_inp}, fetch_list=[out.name]) diff --git a/python/paddle/incubate/optimizer/lbfgs.py b/python/paddle/incubate/optimizer/lbfgs.py index 04d90d1f8c271..2aa4ae1e21b7b 100644 --- a/python/paddle/incubate/optimizer/lbfgs.py +++ b/python/paddle/incubate/optimizer/lbfgs.py @@ -57,15 +57,15 @@ class LBFGS(Optimizer): This parameter is required in dygraph mode. The default value is None. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ It canbe a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of \ some derived class of ``GradientClipBase`` . There are three cliping strategies \ - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , \ - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , \ + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. The default value is None. diff --git a/python/paddle/incubate/optimizer/lookahead.py b/python/paddle/incubate/optimizer/lookahead.py index 40e0fd55e4b50..e1def5a9c45f2 100644 --- a/python/paddle/incubate/optimizer/lookahead.py +++ b/python/paddle/incubate/optimizer/lookahead.py @@ -13,10 +13,10 @@ # limitations under the License. import paddle -from paddle.fluid import framework, unique_name -from paddle.fluid.dygraph import base as imperative_base -from paddle.fluid.framework import Variable -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import framework, unique_name +from paddle.base.dygraph import base as imperative_base +from paddle.base.framework import Variable +from paddle.base.layer_helper import LayerHelper from paddle.optimizer import Optimizer __all__ = [] @@ -250,9 +250,9 @@ def minimize( Args: loss (Tensor): A ``Tensor`` containing the value to minimize. - startup_program (Program, optional): :ref:`api_fluid_Program` for + startup_program (Program, optional): :ref:`api_base_Program` for initializing parameters in ``parameters``. The default value - is None, at this time :ref:`api_fluid_default_startup_program` will be used. + is None, at this time :ref:`api_base_default_startup_program` will be used. parameters (list, optional): List of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. diff --git a/python/paddle/incubate/optimizer/modelaverage.py b/python/paddle/incubate/optimizer/modelaverage.py index 8e182ab9d8284..f66d546a36657 100644 --- a/python/paddle/incubate/optimizer/modelaverage.py +++ b/python/paddle/incubate/optimizer/modelaverage.py @@ -14,11 +14,11 @@ import paddle from paddle import _C_ops -from paddle.fluid import framework -from paddle.fluid.dygraph import base as imperative_base -from paddle.fluid.framework import Program -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base import framework +from paddle.base.dygraph import base as imperative_base +from paddle.base.framework import Program +from paddle.base.layer_helper import LayerHelper +from paddle.base.wrapped_decorator import signature_safe_contextmanager from paddle.framework import in_dynamic_mode from paddle.optimizer import Optimizer @@ -300,9 +300,9 @@ def minimize( Args: loss (Tensor): A ``Tensor`` containing the value to minimize. - startup_program (Program, optional): :ref:`api_fluid_Program` for + startup_program (Program, optional): :ref:`api_base_Program` for initializing parameters in ``parameters``. The default value - is None, at this time :ref:`api_fluid_default_startup_program` will be used. + is None, at this time :ref:`api_base_default_startup_program` will be used. parameters (list, optional): List of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. diff --git a/python/paddle/incubate/optimizer/pipeline.py b/python/paddle/incubate/optimizer/pipeline.py index 1769ac62a7e02..5487acd5122fc 100644 --- a/python/paddle/incubate/optimizer/pipeline.py +++ b/python/paddle/incubate/optimizer/pipeline.py @@ -20,8 +20,8 @@ import numpy as np import paddle -from paddle.fluid import core, unique_name -from paddle.fluid.framework import ( +from paddle.base import core, unique_name +from paddle.base.framework import ( Parameter, Program, default_startup_program, @@ -49,24 +49,24 @@ class PipelineOptimizer: .. code-block:: python import paddle - import paddle.fluid as fluid - import paddle.fluid.layers as layers + import paddle.base as base + import paddle.base.layers as layers import numpy as np paddle.enable_static() - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64', lod_level=0) y = paddle.static.data(name='y', shape=[-1, 1], dtype='int64', lod_level=0) - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[x, y], capacity=64, use_double_buffer=True, iterable=False) - emb_x = layers.embedding(input=x, param_attr=fluid.ParamAttr(name="embx"), size=[10,2], is_sparse=False) - emb_y = layers.embedding(input=y, param_attr=fluid.ParamAttr(name="emby",learning_rate=0.9), size=[10,2], is_sparse=False) + emb_x = layers.embedding(input=x, param_attr=base.ParamAttr(name="embx"), size=[10,2], is_sparse=False) + emb_y = layers.embedding(input=y, param_attr=base.ParamAttr(name="emby",learning_rate=0.9), size=[10,2], is_sparse=False) - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): concat = layers.concat([emb_x, emb_y], axis=1) fc = paddle.static.nn.fc(x=concat, name="fc", size=1, num_flatten_dims=1, bias_attr=False) loss = paddle.mean(fc) @@ -81,13 +81,13 @@ def train_reader(): yield x, y data_loader.set_sample_generator(train_reader, batch_size=1) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) + exe = base.Executor(place) + exe.run(base.default_startup_program()) batch_size = 1 data_loader.start() exe.train_from_dataset( - fluid.default_main_program()) + base.default_main_program()) data_loader.reset() """ diff --git a/python/paddle/incubate/optimizer/recompute.py b/python/paddle/incubate/optimizer/recompute.py index dde8d723e0d1e..0414e70e17843 100644 --- a/python/paddle/incubate/optimizer/recompute.py +++ b/python/paddle/incubate/optimizer/recompute.py @@ -15,9 +15,9 @@ import logging import paddle -from paddle.fluid import core, framework, unique_name -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Variable, in_dygraph_mode, program_guard +from paddle.base import core, framework, unique_name +from paddle.base.backward import append_backward +from paddle.base.framework import Variable, in_dygraph_mode, program_guard from paddle.optimizer import Optimizer @@ -50,7 +50,7 @@ class RecomputeOptimizer(Optimizer): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base import numpy as np paddle.enable_static() @@ -78,14 +78,14 @@ def mlp(input_x, input_y, hid_dim=128, label_dim=2): sgd.minimize(cost) print("Finished optimize") - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) step = 10 for i in range(step): cost_val = exe.run(feed=gen_data(), - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[cost.name]) print("step=%d cost=%f" % (i, cost_val[0])) @@ -133,7 +133,7 @@ def load(self, state_dict): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() def mlp(input_x, input_y, hid_dim=128, label_dim=2): @@ -178,8 +178,8 @@ def apply_gradients(self, params_grads): .. code-block:: python import paddle - import paddle.fluid as fluid - import paddle.fluid.framework as framework + import paddle.base as base + import paddle.base.framework as framework paddle.enable_static() @@ -666,7 +666,7 @@ def backward( .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() @@ -748,7 +748,7 @@ def apply_optimize(self, loss, startup_program, params_grads): Examples: .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() diff --git a/python/paddle/incubate/passes/ir.py b/python/paddle/incubate/passes/ir.py index c657f20abcb99..8b9d9944d9bee 100644 --- a/python/paddle/incubate/passes/ir.py +++ b/python/paddle/incubate/passes/ir.py @@ -16,19 +16,19 @@ from os import path import paddle -from paddle.fluid.proto import framework_pb2 +from paddle.base.proto import framework_pb2 -from ...fluid import core, unique_name -from ...fluid.framework import OpProtoHolder +from ...base import core, unique_name +from ...base.framework import OpProtoHolder try: - from paddle.fluid.proto import pass_desc_pb2 + from paddle.base.proto import pass_desc_pb2 except ModuleNotFoundError: import sys - fluid_path = path.dirname(__file__) + '/../../fluid' - sys.path.append(path.join(fluid_path, 'proto')) - from paddle.fluid.proto import pass_desc_pb2 + base_path = path.dirname(__file__) + '/../../base' + sys.path.append(path.join(base_path, 'proto')) + from paddle.base.proto import pass_desc_pb2 class RegisterPassHelper: @@ -470,7 +470,7 @@ def RegisterPass(function=None, input_specs={}): .. code-block:: python >>> import paddle - >>> from paddle.fluid.ir import RegisterPass + >>> from paddle.base.ir import RegisterPass >>> @RegisterPass >>> def multi_add_to_addn(): diff --git a/python/paddle/incubate/tensor/manipulation.py b/python/paddle/incubate/tensor/manipulation.py index 70c017a8eec36..a1615fd2b8d20 100644 --- a/python/paddle/incubate/tensor/manipulation.py +++ b/python/paddle/incubate/tensor/manipulation.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/incubate/tensor/math.py b/python/paddle/incubate/tensor/math.py index 04ed1da3e1980..649b1a74fcb08 100644 --- a/python/paddle/incubate/tensor/math.py +++ b/python/paddle/incubate/tensor/math.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.utils import deprecated diff --git a/python/paddle/incubate/xpu/resnet_block.py b/python/paddle/incubate/xpu/resnet_block.py index 081583b6a8c40..7c24943f35b11 100644 --- a/python/paddle/incubate/xpu/resnet_block.py +++ b/python/paddle/incubate/xpu/resnet_block.py @@ -15,9 +15,9 @@ import numpy as np import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.param_attr import ParamAttr +from paddle import _legacy_C_ops, base +from paddle.base.layer_helper import LayerHelper +from paddle.base.param_attr import ParamAttr from paddle.nn import Layer from paddle.nn import initializer as I @@ -60,7 +60,7 @@ def resnet_basic_block( trainable_statistics=False, find_conv_max=True, ): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): attrs = ( 'stride1', stride1, @@ -153,8 +153,8 @@ def resnet_basic_block( ) return out helper = LayerHelper('resnet_basic_block', **locals()) - bn_param_dtype = fluid.core.VarDesc.VarType.FP32 - max_dtype = fluid.core.VarDesc.VarType.FP32 + bn_param_dtype = base.core.VarDesc.VarType.FP32 + max_dtype = base.core.VarDesc.VarType.FP32 out = helper.create_variable_for_type_inference( dtype=x.dtype, stop_gradient=True @@ -526,7 +526,7 @@ def _get_default_param_initializer(channels, kernel_size): return I.Normal(0.0, std) # init filter - bn_param_dtype = fluid.core.VarDesc.VarType.FP32 + bn_param_dtype = base.core.VarDesc.VarType.FP32 bn1_param_shape = [1, 1, num_filter1] bn2_param_shape = [1, 1, num_filter2] filter1_shape = [num_filter1, num_channels1, filter1_size, filter1_size] diff --git a/python/paddle/inference/__init__.py b/python/paddle/inference/__init__.py index e7120bdf7a4c3..f59c5990573db 100644 --- a/python/paddle/inference/__init__.py +++ b/python/paddle/inference/__init__.py @@ -22,7 +22,7 @@ convert_to_mixed_precision, ) -from paddle.fluid.core import ( +from paddle.base.core import ( create_predictor, get_version, _get_phi_kernel_name, diff --git a/python/paddle/inference/contrib/utils/__init__.py b/python/paddle/inference/contrib/utils/__init__.py index 5a52525049250..aef5702903e0d 100644 --- a/python/paddle/inference/contrib/utils/__init__.py +++ b/python/paddle/inference/contrib/utils/__init__.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ....fluid.core import copy_tensor # noqa: F401 +from ....base.core import copy_tensor # noqa: F401 diff --git a/python/paddle/inference/wrapper.py b/python/paddle/inference/wrapper.py index 7163bdee25e36..51095647fee2f 100644 --- a/python/paddle/inference/wrapper.py +++ b/python/paddle/inference/wrapper.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.core import ( +from paddle.base import core +from paddle.base.core import ( AnalysisConfig, PaddleDType, PaddleInferPredictor, @@ -58,7 +58,7 @@ def tensor_share_external_data(self, data): self._share_external_data_bind(data) elif isinstance(data, paddle.Tensor): self._share_external_data_paddle_tensor_bind(data) - elif isinstance(data, paddle.fluid.framework.Variable): + elif isinstance(data, paddle.base.framework.Variable): raise TypeError( "The interface 'share_external_data' can only be used in dynamic graph mode. " "Maybe you called 'paddle.enable_static()' and you are in static graph mode now. " diff --git a/python/paddle/io/dataloader/dataloader_iter.py b/python/paddle/io/dataloader/dataloader_iter.py index 0ffe7c46e77c9..7ddec5c44b83e 100644 --- a/python/paddle/io/dataloader/dataloader_iter.py +++ b/python/paddle/io/dataloader/dataloader_iter.py @@ -25,7 +25,7 @@ import paddle from paddle import profiler -from paddle.fluid.framework import _current_expected_place, _set_expected_place +from paddle.base.framework import _current_expected_place, _set_expected_place from paddle.profiler.timer import benchmark from paddle.profiler.utils import in_profiler_mode @@ -230,7 +230,7 @@ def _thread_loop(self, legacy_expected_place): indices = next(self._sampler_iter) # read data from dataset in mini-batch - # with paddle.fluid.dygraph.guard(place=paddle.CPUPlace()): + # with paddle.base.dygraph.guard(place=paddle.CPUPlace()): # read data from dataset in mini-batch batch = self._dataset_fetcher.fetch( indices, self._thread_done_event @@ -397,7 +397,7 @@ def __init__(self, loader): # Note(zhangbo): shm_buffer_size is used for MemoryMapAllocationPool. # MemoryMapAllocationPool is used to cache and reuse shm, thus reducing munmap in dataloader. - # For more details, please see: paddle/fluid/memory/allocation/mmap_allocator.h + # For more details, please see: paddle/base/memory/allocation/mmap_allocator.h if os.environ.get('FLAGS_use_shm_cache', False) in [ 1, '1', diff --git a/python/paddle/io/dataloader/flat.py b/python/paddle/io/dataloader/flat.py index f674d7fb2b4b9..87c35e6dedd38 100644 --- a/python/paddle/io/dataloader/flat.py +++ b/python/paddle/io/dataloader/flat.py @@ -36,7 +36,7 @@ def _flatten(batch, flat_batch, structure, field_idx): for field in batch: if isinstance( field, - (np.ndarray, paddle.Tensor, paddle.fluid.core.eager.Tensor), + (np.ndarray, paddle.Tensor, paddle.base.core.eager.Tensor), ): structure.append(f'{FIELD_PREFIX}{field_idx}') flat_batch.append(field) @@ -59,7 +59,7 @@ def _flatten(batch, flat_batch, structure, field_idx): for k, field in batch.items(): if isinstance( field, - (np.ndarray, paddle.Tensor, paddle.fluid.core.eager.Tensor), + (np.ndarray, paddle.Tensor, paddle.base.core.eager.Tensor), ): structure[k] = f'{FIELD_PREFIX}{field_idx}' flat_batch.append(field) diff --git a/python/paddle/io/dataloader/worker.py b/python/paddle/io/dataloader/worker.py index 4a1667483da64..814dc667a7cf3 100644 --- a/python/paddle/io/dataloader/worker.py +++ b/python/paddle/io/dataloader/worker.py @@ -370,7 +370,7 @@ def _worker_loop( # may copy CPU tensor to GPU even if users want to use # CPU tensor operation, so we add CPUPlace guard here # to make sure tensor will be operated only on CPU - with paddle.fluid.dygraph.guard(place=paddle.CPUPlace()): + with paddle.base.dygraph.guard(place=paddle.CPUPlace()): batch = fetcher.fetch(indices) except Exception as e: if ( diff --git a/python/paddle/io/reader.py b/python/paddle/io/reader.py index d8db6cc2ab012..81483712bf453 100644 --- a/python/paddle/io/reader.py +++ b/python/paddle/io/reader.py @@ -19,9 +19,9 @@ import warnings import paddle -from paddle.fluid.framework import logging +from paddle.base.framework import logging -from ..fluid.framework import ( +from ..base.framework import ( _current_expected_place, _get_paddle_place, _get_paddle_place_list, diff --git a/python/paddle/ir/__init__.py b/python/paddle/ir/__init__.py index 4fee1c1a064c5..df98fd79fe2a3 100644 --- a/python/paddle/ir/__init__.py +++ b/python/paddle/ir/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.libpaddle.ir import ( +from paddle.base.libpaddle.ir import ( Program, Block, Operation, @@ -21,7 +21,7 @@ OpResult, Type, ) # noqa: F401 -from paddle.fluid.libpaddle.ir import ( +from paddle.base.libpaddle.ir import ( translate_to_new_ir, set_global_program, set_insertion_point, diff --git a/python/paddle/ir/core.py b/python/paddle/ir/core.py index c7f2a73f2ad5e..2a95411292d60 100644 --- a/python/paddle/ir/core.py +++ b/python/paddle/ir/core.py @@ -15,10 +15,10 @@ import numpy as np -from paddle.fluid.libpaddle import DataType -from paddle.fluid.libpaddle.ir import Program, set_global_program +from paddle.base.libpaddle import DataType +from paddle.base.libpaddle.ir import Program, set_global_program -from ..fluid.wrapped_decorator import signature_safe_contextmanager +from ..base.wrapped_decorator import signature_safe_contextmanager np_type_to_paddle_type = { np.dtype("float32"): DataType.FLOAT32, @@ -229,7 +229,7 @@ def program_guard(main_program, startup_program=None): data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') """ - from ..fluid.data_feeder import check_type + from ..base.data_feeder import check_type check_type( main_program, 'main_program', Program, 'paddle.static.program_guard' diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index dd96a1001eae5..b1947e9017716 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -28,14 +28,14 @@ import types import paddle -from paddle.fluid import core, dygraph -from paddle.fluid.compiler import ( +from paddle.base import core, dygraph +from paddle.base.compiler import ( BuildStrategy, CompiledProgram, ExecutionStrategy, ) -from paddle.fluid.data_feeder import check_type -from paddle.fluid.dygraph.base import ( +from paddle.base.data_feeder import check_type +from paddle.base.dygraph.base import ( program_desc_tracing_guard, switch_to_static_graph, ) @@ -59,21 +59,21 @@ INFER_PROPERTY_SUFFIX, ) from paddle.nn import Layer -from paddle.fluid.executor import Executor, scope_guard -from paddle.fluid.framework import ( +from paddle.base.executor import Executor, scope_guard +from paddle.base.framework import ( Block, Program, Variable, Parameter, EagerParamBase, ) -from paddle.fluid.framework import ( +from paddle.base.framework import ( _current_expected_place, _dygraph_guard, _dygraph_tracer, ) -from paddle.fluid.framework import dygraph_only -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle.base.framework import dygraph_only +from paddle.base.wrapped_decorator import wrap_decorator from paddle.static.io import save_inference_model from paddle.framework import in_dynamic_mode @@ -94,7 +94,7 @@ def _extract_vars(inputs, result_list, err_tag='inputs'): _extract_vars(var, result_list, err_tag) else: raise TypeError( - "The type of 'each element of {}' in paddle.jit.TracedLayer.trace must be fluid.Variable, but received {}.".format( + "The type of 'each element of {}' in paddle.jit.TracedLayer.trace must be base.Variable, but received {}.".format( err_tag, type(inputs) ) ) @@ -647,11 +647,11 @@ def _get_output_vars(outputs, output_spec, with_hook=False): # 1. Expected cases: # - paddle.jit.save # - paddle.static.save_inference_model -# - paddle.fluid.io.save_inference_model +# - paddle.base.io.save_inference_model # 2. Error cases: # - paddle.save: no .pdmodel for prefix # - paddle.static.save: no .pdiparams but .pdparams exists -# - paddle.fluid.io.save_params/save_persistables: no __model__ +# - paddle.base.io.save_params/save_persistables: no __model__ # TODO(chenweihang): polish error message in above error cases def _build_load_path_and_config(path, config): # NOTE(chenweihang): If both [prefix save format] and [directory save format] exist, @@ -1336,7 +1336,7 @@ def load(path, **configs): :api_attr: imperative Load model saved by ``paddle.jit.save`` or ``paddle.static.save_inference_model`` or - paddle 1.x API ``paddle.fluid.io.save_inference_model`` as ``paddle.jit.TranslatedLayer``, + paddle 1.x API ``paddle.base.io.save_inference_model`` as ``paddle.jit.TranslatedLayer``, then performing inference or fine-tune training. .. note:: @@ -1455,7 +1455,7 @@ def load(path, **configs): >>> train(loaded_layer, loader, loss_fn, adam) - 2. Load model saved by ``paddle.fluid.io.save_inference_model`` then performing and fine-tune training. + 2. Load model saved by ``paddle.base.io.save_inference_model`` then performing and fine-tune training. .. code-block:: python :name: code-example2 @@ -1523,7 +1523,7 @@ def load(path, **configs): ... ) >>> model_path = "fc.example.model" - >>> paddle.fluid.io.save_inference_model( + >>> paddle.base.io.save_inference_model( >>> model_path, ["image"], [pred], exe) >>> # 2. load model @@ -1755,12 +1755,12 @@ def set_strategy(self, build_strategy=None, exec_strategy=None): assert self._compiled_program is None, "Cannot set strategy after run" assert isinstance( build_strategy, (type(None), BuildStrategy) - ), "The type of 'build_strategy' in paddle.jit.TracedLayer.set_strategy must be fluid.BuildStrategy, but received {}.".format( + ), "The type of 'build_strategy' in paddle.jit.TracedLayer.set_strategy must be base.BuildStrategy, but received {}.".format( type(build_strategy) ) assert isinstance( exec_strategy, (type(None), ExecutionStrategy) - ), "The type of 'exec_strategy' in paddle.jit.TracedLayer.set_strategy must be fluid.ExecutionStrategy, but received {}.".format( + ), "The type of 'exec_strategy' in paddle.jit.TracedLayer.set_strategy must be base.ExecutionStrategy, but received {}.".format( type(exec_strategy) ) self._build_strategy = build_strategy diff --git a/python/paddle/jit/dy2static/base_transformer.py b/python/paddle/jit/dy2static/base_transformer.py index cddea92376007..e3e2dc7b39941 100644 --- a/python/paddle/jit/dy2static/base_transformer.py +++ b/python/paddle/jit/dy2static/base_transformer.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.jit.dy2static.utils import ( FOR_ITER_INDEX_PREFIX, FOR_ITER_ITERATOR_PREFIX, diff --git a/python/paddle/jit/dy2static/basic_api_transformer.py b/python/paddle/jit/dy2static/basic_api_transformer.py index 30af698923de3..af111b55e79a6 100644 --- a/python/paddle/jit/dy2static/basic_api_transformer.py +++ b/python/paddle/jit/dy2static/basic_api_transformer.py @@ -230,7 +230,7 @@ def is_to_variable(node): def to_assign_node(node): - # Transform dygraph api `fluid.dygraph.to_variable` alias `paddle.to_tensor` to static api `paddle.assign`. + # Transform dygraph api `base.dygraph.to_variable` alias `paddle.to_tensor` to static api `paddle.assign`. # NOTE: # 1. Api `to_variable` supports data type {float16, float32, float64, int16, int32, int64, uint8, uint16}, # but api `assign` only supports {float32, float64, int32, int64, bool}; diff --git a/python/paddle/jit/dy2static/break_continue_transformer.py b/python/paddle/jit/dy2static/break_continue_transformer.py index 6e1199bf0ede1..4c6cad0e788e3 100644 --- a/python/paddle/jit/dy2static/break_continue_transformer.py +++ b/python/paddle/jit/dy2static/break_continue_transformer.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.jit.dy2static.utils import BaseNodeVisitor, index_in_list from paddle.jit.dy2static.variable_trans_func import create_bool_node from paddle.utils import gast diff --git a/python/paddle/jit/dy2static/convert_operators.py b/python/paddle/jit/dy2static/convert_operators.py index 5ffb3ebfce978..535c3713100e2 100644 --- a/python/paddle/jit/dy2static/convert_operators.py +++ b/python/paddle/jit/dy2static/convert_operators.py @@ -15,12 +15,9 @@ import re import paddle -from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.dygraph.base import ( - _convert_into_variable, - in_declarative_mode, -) -from paddle.fluid.framework import Variable, core, default_main_program +from paddle.base.data_feeder import convert_dtype +from paddle.base.dygraph.base import _convert_into_variable, in_declarative_mode +from paddle.base.framework import Variable, core, default_main_program from .utils import ( RETURN_NO_VALUE_VAR_NAME, @@ -41,7 +38,7 @@ def convert_attr(x, attr): def convert_load(x): - if in_declarative_mode() and isinstance(x, paddle.fluid.core.eager.Tensor): + if in_declarative_mode() and isinstance(x, paddle.base.core.eager.Tensor): """ TODO:(@xiongkun) may run convert_load in dygraph mode, which should be fixed. """ diff --git a/python/paddle/jit/dy2static/function_spec.py b/python/paddle/jit/dy2static/function_spec.py index 65978176cb177..d34a5dc6288f6 100644 --- a/python/paddle/jit/dy2static/function_spec.py +++ b/python/paddle/jit/dy2static/function_spec.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle.base import core +from paddle.base.dygraph.base import switch_to_static_graph from paddle.jit.translated_layer import TranslatedLayer from paddle.nn.layer import layers @@ -299,7 +299,7 @@ def _replace_value_with_input_spec(args): stop_gradient = input_var.stop_gradient input_var = paddle.static.InputSpec.from_tensor(input_var) input_var.stop_gradient = stop_gradient - elif isinstance(input_var, paddle.fluid.framework.Variable): + elif isinstance(input_var, paddle.base.framework.Variable): stop_gradient = input_var.stop_gradient input_var = paddle.static.InputSpec( input_var.shape, input_var.dtype, input_var.name @@ -325,7 +325,7 @@ def _replace_to_input_spec_with_new_name(args, arg_names): stop_gradient = origin_input.stop_gradient input_var = paddle.static.InputSpec.from_tensor(origin_input) input_var.stop_gradient = stop_gradient - elif isinstance(origin_input, paddle.fluid.framework.Variable): + elif isinstance(origin_input, paddle.base.framework.Variable): stop_gradient = origin_input.stop_gradient input_var = paddle.static.InputSpec( origin_input.shape, origin_input.dtype, origin_input.name @@ -339,7 +339,7 @@ def _replace_to_input_spec_with_new_name(args, arg_names): ( np.ndarray, core.eager.Tensor, - paddle.fluid.framework.Variable, + paddle.base.framework.Variable, ), ): input_var.name = f"_jst.{str(order).zfill(order_digit)}.{name_prefix}.{str(index)}" diff --git a/python/paddle/jit/dy2static/ifelse_transformer.py b/python/paddle/jit/dy2static/ifelse_transformer.py index 0986bc1933dad..8da098959aa6e 100644 --- a/python/paddle/jit/dy2static/ifelse_transformer.py +++ b/python/paddle/jit/dy2static/ifelse_transformer.py @@ -15,7 +15,7 @@ import copy from collections import defaultdict -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.jit.dy2static.utils import ( FOR_ITER_INDEX_PREFIX, FOR_ITER_ITERATOR_PREFIX, diff --git a/python/paddle/jit/dy2static/logging_utils.py b/python/paddle/jit/dy2static/logging_utils.py index 7b45ab82d03ef..23d0435415493 100644 --- a/python/paddle/jit/dy2static/logging_utils.py +++ b/python/paddle/jit/dy2static/logging_utils.py @@ -15,7 +15,7 @@ import os import threading -from paddle.fluid import log_helper +from paddle.base import log_helper from .ast_utils import ast_to_source_code diff --git a/python/paddle/jit/dy2static/loop_transformer.py b/python/paddle/jit/dy2static/loop_transformer.py index 043d5be4b76cf..e96cfa943abca 100644 --- a/python/paddle/jit/dy2static/loop_transformer.py +++ b/python/paddle/jit/dy2static/loop_transformer.py @@ -15,7 +15,7 @@ import copy from collections import defaultdict -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.utils import gast from .base_transformer import ( diff --git a/python/paddle/jit/dy2static/origin_info.py b/python/paddle/jit/dy2static/origin_info.py index 42e5f0ea4d3c9..d6fee112ded91 100644 --- a/python/paddle/jit/dy2static/origin_info.py +++ b/python/paddle/jit/dy2static/origin_info.py @@ -15,8 +15,8 @@ import inspect from collections.abc import Sequence -from paddle.fluid import core -from paddle.fluid.framework import Program +from paddle.base import core +from paddle.base.framework import Program from paddle.utils import gast from .utils import ORIGI_INFO, unwrap diff --git a/python/paddle/jit/dy2static/partial_program.py b/python/paddle/jit/dy2static/partial_program.py index 2c44346054cd6..7679a6d4ed513 100644 --- a/python/paddle/jit/dy2static/partial_program.py +++ b/python/paddle/jit/dy2static/partial_program.py @@ -20,12 +20,12 @@ import paddle from paddle import _legacy_C_ops from paddle.amp.auto_cast import _in_amp_guard, _in_pure_fp16_guard -from paddle.fluid import backward, core, framework, program_guard -from paddle.fluid.compiler import BuildStrategy -from paddle.fluid.data_feeder import check_type, convert_dtype -from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.framework import _apply_pass -from paddle.fluid.unique_name import guard as UniqueNameGuard +from paddle.base import backward, core, framework, program_guard +from paddle.base.compiler import BuildStrategy +from paddle.base.data_feeder import check_type, convert_dtype +from paddle.base.dygraph.base import switch_to_static_graph +from paddle.base.framework import _apply_pass +from paddle.base.unique_name import guard as UniqueNameGuard from paddle.optimizer.lr import LRScheduler from . import logging_utils @@ -190,9 +190,7 @@ def __init__( assert isinstance(self._build_strategy, BuildStrategy) self._origin_main_program = self._verify_program(main_program) - with paddle.fluid.framework._dygraph_guard( - paddle.fluid.dygraph.Tracer() - ): + with paddle.base.framework._dygraph_guard(paddle.base.dygraph.Tracer()): self._cuda_graph_vec = self._create_cuda_graph_vec() self._cuda_graph_capture_mode = "" self._cuda_graph_pool_id = 0 @@ -869,10 +867,10 @@ def _inout_var_names(self): """ var_names = [] for var in self._inputs: - if isinstance(var, paddle.fluid.framework.Variable): + if isinstance(var, paddle.base.framework.Variable): var_names.append(var.desc.name()) for var in self._outputs: - if isinstance(var, paddle.fluid.framework.Variable): + if isinstance(var, paddle.base.framework.Variable): var_names.append(var.desc.name()) return var_names diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index ed044d1fd2293..48c9889cd1507 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -20,15 +20,15 @@ import warnings import weakref -from paddle.fluid import core, framework -from paddle.fluid.data_feeder import check_type -from paddle.fluid.dygraph.base import ( +from paddle.base import core, framework +from paddle.base.data_feeder import check_type +from paddle.base.dygraph.base import ( _switch_declarative_mode_guard_, param_guard, switch_to_static_graph, ) -from paddle.fluid.unique_name import UniqueNameGenerator -from paddle.fluid.unique_name import guard as UniqueNameGuard +from paddle.base.unique_name import UniqueNameGenerator +from paddle.base.unique_name import guard as UniqueNameGuard from paddle.framework import in_dynamic_mode from paddle.nn.layer import layers from paddle.utils import flatten, gast @@ -1182,8 +1182,8 @@ def from_func_spec( main_program, startup_program = framework.Program(), framework.Program() # Note: The random seed should be synchronized into cached program - # if set in `fluid.dygraph_guard` because some ops rely on it, such as - # `fluid.layers.dropout`. + # if set in `base.dygraph_guard` because some ops rely on it, such as + # `base.layers.dropout`. main_program.random_seed = framework.default_main_program().random_seed startup_program.random_seed = ( framework.default_startup_program().random_seed diff --git a/python/paddle/jit/dy2static/return_transformer.py b/python/paddle/jit/dy2static/return_transformer.py index 3b04d39e521e7..3fc259e0a303a 100644 --- a/python/paddle/jit/dy2static/return_transformer.py +++ b/python/paddle/jit/dy2static/return_transformer.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.utils import gast from .base_transformer import BaseTransformer diff --git a/python/paddle/jit/dy2static/utils.py b/python/paddle/jit/dy2static/utils.py index eaa47258b1074..b66d97efa6e1c 100644 --- a/python/paddle/jit/dy2static/utils.py +++ b/python/paddle/jit/dy2static/utils.py @@ -31,11 +31,11 @@ import numpy as np import paddle -from paddle import fluid # noqa: F401 -from paddle.fluid import backward, core, framework, unique_name -from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle import base # noqa: F401 +from paddle.base import backward, core, framework, unique_name +from paddle.base.data_feeder import convert_dtype +from paddle.base.layer_helper import LayerHelper +from paddle.base.wrapped_decorator import signature_safe_contextmanager from paddle.utils import gast from .ast_utils import ast_to_source_code @@ -376,7 +376,7 @@ def to_static_ast(node, class_node): attr='layers', ctx=gast.Load(), value=gast.Name( - ctx=gast.Load(), id='fluid', annotation=None, type_comment=None + ctx=gast.Load(), id='base', annotation=None, type_comment=None ), ), ) @@ -609,7 +609,7 @@ def _inject_import_statements(): import_statements = [ "import paddle", "from paddle import Tensor", - "import paddle.fluid as fluid", + "import paddle.base as base", "import paddle.jit.dy2static as _jst", "from typing import *", "import numpy as np", diff --git a/python/paddle/jit/dy2static/utils_helper.py b/python/paddle/jit/dy2static/utils_helper.py index 4899b8185169c..06f96d2094a1e 100644 --- a/python/paddle/jit/dy2static/utils_helper.py +++ b/python/paddle/jit/dy2static/utils_helper.py @@ -19,10 +19,10 @@ import numpy as np # noqa: F401 import paddle # noqa: F401 -from paddle import fluid # noqa: F401 -from paddle.fluid import dygraph # noqa: F401 -from paddle.fluid import layers # noqa: F401 -from paddle.fluid.dygraph import to_variable # noqa: F401 +from paddle import base # noqa: F401 +from paddle.base import dygraph # noqa: F401 +from paddle.base import layers # noqa: F401 +from paddle.base.dygraph import to_variable # noqa: F401 from paddle.utils import gast from .ast_utils import ast_to_source_code @@ -41,7 +41,7 @@ def index_in_list(array_list, item): # module such as paddlenlp. PADDLE_MODULE_PREFIX = 'paddle.' DYGRAPH_TO_STATIC_MODULE_PREFIX = 'paddle.jit.dy2static' -DYGRAPH_MODULE_PREFIX = 'paddle.fluid.dygraph' +DYGRAPH_MODULE_PREFIX = 'paddle.base.dygraph' def is_dygraph_api(node): diff --git a/python/paddle/jit/dy2static/variable_trans_func.py b/python/paddle/jit/dy2static/variable_trans_func.py index 80c4487dc29c6..ee358d57ee019 100644 --- a/python/paddle/jit/dy2static/variable_trans_func.py +++ b/python/paddle/jit/dy2static/variable_trans_func.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle.fluid.framework import Variable +from paddle.base.framework import Variable from paddle.utils import gast, is_sequence, map_structure from .utils import UndefinedVar, create_undefined_variable diff --git a/python/paddle/jit/layer.py b/python/paddle/jit/layer.py index e3204ab65df02..fca710e87ac1f 100644 --- a/python/paddle/jit/layer.py +++ b/python/paddle/jit/layer.py @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import core -from paddle.fluid.core import Load +from paddle.base import core +from paddle.base.core import Load class Layer: diff --git a/python/paddle/jit/translated_layer.py b/python/paddle/jit/translated_layer.py index edb0b8bd355a9..a7f51c1a8c164 100644 --- a/python/paddle/jit/translated_layer.py +++ b/python/paddle/jit/translated_layer.py @@ -19,10 +19,10 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid import backward, core, framework, unique_name -from paddle.fluid.data_feeder import check_type -from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.framework import OpProtoHolder +from paddle.base import backward, core, framework, unique_name +from paddle.base.data_feeder import check_type +from paddle.base.dygraph.base import switch_to_static_graph +from paddle.base.framework import OpProtoHolder from paddle.framework import in_dynamic_mode from paddle.jit.dy2static.partial_program import ( LazyInitialized, diff --git a/python/paddle/metric/metrics.py b/python/paddle/metric/metrics.py index 21064b9bb0e90..9cac2ff006c72 100644 --- a/python/paddle/metric/metrics.py +++ b/python/paddle/metric/metrics.py @@ -19,9 +19,9 @@ import paddle from paddle import _legacy_C_ops -from ..fluid.data_feeder import check_variable_and_dtype -from ..fluid.framework import _create_tensor -from ..fluid.layer_helper import LayerHelper +from ..base.data_feeder import check_variable_and_dtype +from ..base.framework import _create_tensor +from ..base.layer_helper import LayerHelper from ..framework import in_dynamic_mode __all__ = [] @@ -293,7 +293,7 @@ def update(self, correct, *args): Return: Tensor: the accuracy of current step. """ - if isinstance(correct, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(correct, (paddle.Tensor, paddle.base.core.eager.Tensor)): correct = np.array(correct) num_samples = np.prod(np.array(correct.shape[:-1])) accs = [] @@ -420,12 +420,12 @@ def update(self, preds, labels): the shape should keep the same as preds. The data type is 'int32' or 'int64'. """ - if isinstance(preds, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(preds, (paddle.Tensor, paddle.base.core.eager.Tensor)): preds = np.array(preds) elif not _is_numpy_(preds): raise ValueError("The 'preds' must be a numpy ndarray or Tensor.") - if isinstance(labels, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(labels, (paddle.Tensor, paddle.base.core.eager.Tensor)): labels = np.array(labels) elif not _is_numpy_(labels): raise ValueError("The 'labels' must be a numpy ndarray or Tensor.") @@ -552,12 +552,12 @@ def update(self, preds, labels): the shape should keep the same as preds. Shape: [batch_size, 1], Dtype: 'int32' or 'int64'. """ - if isinstance(preds, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(preds, (paddle.Tensor, paddle.base.core.eager.Tensor)): preds = np.array(preds) elif not _is_numpy_(preds): raise ValueError("The 'preds' must be a numpy ndarray or Tensor.") - if isinstance(labels, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(labels, (paddle.Tensor, paddle.base.core.eager.Tensor)): labels = np.array(labels) elif not _is_numpy_(labels): raise ValueError("The 'labels' must be a numpy ndarray or Tensor.") @@ -703,12 +703,12 @@ def update(self, preds, labels): (batch_size, 1), labels[i] is either o or 1, representing the label of the instance i. """ - if isinstance(labels, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(labels, (paddle.Tensor, paddle.base.core.eager.Tensor)): labels = np.array(labels) elif not _is_numpy_(labels): raise ValueError("The 'labels' must be a numpy ndarray or Tensor.") - if isinstance(preds, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if isinstance(preds, (paddle.Tensor, paddle.base.core.eager.Tensor)): preds = np.array(preds) elif not _is_numpy_(preds): raise ValueError("The 'preds' must be a numpy ndarray or Tensor.") diff --git a/python/paddle/new_ir_utils.py b/python/paddle/new_ir_utils.py index 88a5415e3ec3e..d401cde662b17 100644 --- a/python/paddle/new_ir_utils.py +++ b/python/paddle/new_ir_utils.py @@ -18,18 +18,18 @@ class IrGuard: def __init__(self): - old_flag = paddle.fluid.framework.get_flags("FLAGS_enable_new_ir_api") - paddle.fluid.framework.set_flags({"FLAGS_enable_new_ir_api": False}) + old_flag = paddle.base.framework.get_flags("FLAGS_enable_new_ir_api") + paddle.base.framework.set_flags({"FLAGS_enable_new_ir_api": False}) if not paddle.ir.core._use_new_ir_api(): self.old_Program = paddle.static.Program - self.old_program_guard = paddle.fluid.program_guard + self.old_program_guard = paddle.base.program_guard self.old_default_main_program = paddle.static.default_main_program else: raise RuntimeError( "IrChange only init when paddle.ir.core._use_new_ir_api() is false, \ please set FLAGS_enable_new_ir_api = false" ) - paddle.fluid.framework.set_flags(old_flag) + paddle.base.framework.set_flags(old_flag) def __enter__(self): paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) @@ -46,8 +46,8 @@ def _switch_to_new_ir(self): ) paddle.ir.register_paddle_dialect() paddle.static.Program = paddle.ir.Program - paddle.fluid.Program = paddle.ir.Program - paddle.fluid.program_guard = paddle.ir.core.program_guard + paddle.base.Program = paddle.ir.Program + paddle.base.program_guard = paddle.ir.core.program_guard paddle.static.program_guard = paddle.ir.core.program_guard paddle.framework.default_main_program = ( paddle.ir.core.default_main_program @@ -59,8 +59,8 @@ def _switch_to_old_ir(self): {"FLAGS_enable_new_ir_in_executor": False} ) paddle.static.Program = self.old_Program - paddle.fluid.Program = self.old_Program - paddle.fluid.program_guard = self.old_program_guard + paddle.base.Program = self.old_Program + paddle.base.program_guard = self.old_program_guard paddle.static.program_guard = self.old_program_guard paddle.framework.default_main_program = ( self.old_default_main_program diff --git a/python/paddle/nn/clip.py b/python/paddle/nn/clip.py index f8c674c8a0be4..c00851e3019e6 100644 --- a/python/paddle/nn/clip.py +++ b/python/paddle/nn/clip.py @@ -19,9 +19,9 @@ import paddle import paddle.autograd as imperative_base from paddle import _C_ops +from paddle.base import core, framework, unique_name +from paddle.base.data_feeder import check_variable_and_dtype from paddle.common_ops_import import Variable, check_type, default_main_program -from paddle.fluid import core, framework, unique_name -from paddle.fluid.data_feeder import check_variable_and_dtype from paddle.framework import LayerHelper, in_dynamic_mode from paddle.tensor.layer_function_generator import templatedoc @@ -100,11 +100,11 @@ def merge_selected_rows(x, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - b = fluid.default_main_program().global_block() + import paddle.base as base + b = base.default_main_program().global_block() var = b.create_var( name="X", dtype="float32", persistable=True, - type=fluid.core.VarDesc.VarType.SELECTED_ROWS) + type=base.core.VarDesc.VarType.SELECTED_ROWS) y = nn.merge_selected_rows(var) """ if in_dynamic_mode(): @@ -153,8 +153,8 @@ def get_tensor_from_selected_rows(x, name=None): .. code-block:: python from paddle import nnp.py - b = fluid.default_main_program().global_block() - input = b.create_var(name="X", dtype="float32", persistable=True, type=fluid.core.VarDesc.VarType.SELECTED_ROWS) + b = base.default_main_program().global_block() + input = b.create_var(name="X", dtype="float32", persistable=True, type=base.core.VarDesc.VarType.SELECTED_ROWS) out = nn.get_tensor_from_selected_rows(input) """ @@ -253,21 +253,21 @@ class ErrorClipByValue(BaseErrorClipAttr): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() BATCH_SIZE = 128 CLIP_MAX = 2e-6 CLIP_MIN = -1e-6 - prog = fluid.framework.Program() - with fluid.program_guard(main_program=prog): - image = fluid.layers.data( + prog = base.framework.Program() + with base.program_guard(main_program=prog): + image = base.layers.data( name='x', shape=[784], dtype='float32') - hidden1 = fluid.layers.fc(input=image, size=128, act='relu') - hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu') - predict = fluid.layers.fc( + hidden1 = base.layers.fc(input=image, size=128, act='relu') + hidden2 = base.layers.fc(input=hidden1, size=64, act='relu') + predict = base.layers.fc( input=hidden2, size=10, act='softmax') - label = fluid.layers.data(name='y', shape=[1], dtype='int64') + label = base.layers.data(name='y', shape=[1], dtype='int64') cost = paddle.nn.functional.cross_entropy(input=predict, label=label) avg_cost = paddle.mean(cost) prog_clip = prog.clone() @@ -937,22 +937,22 @@ def set_gradient_clip(clip, param_list=None, program=None): and it may be removed in future releases, so it is not recommended. It is recommended to set ``grad_clip`` when initializing the ``optimizer`` , this is a better method to clip gradient. There are three clipping strategies: - :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` . + :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` . To specify parameters that require gradient clip. Args: grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three cliping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default value: None, and there is no + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default value: None, and there is no gradient clipping. param_list (list(Variable), optional): Parameters that require gradient clip. It can be a list of parameter or a list of parameter's name. Default None, meaning that all parameters in the program will be included. program (Program, optional): The program where parameters are located. - Default None, meaning that using :ref:`api_fluid_default_main_program` . + Default None, meaning that using :ref:`api_base_default_main_program` . Returns: None @@ -961,58 +961,58 @@ def set_gradient_clip(clip, param_list=None, program=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() def network(): image = paddle.static.data(name='image', shape=[ None, 28], dtype='float32') - param_attr1 = fluid.ParamAttr("fc1_param") - fc1 = fluid.layers.fc(image, size=10, param_attr=param_attr1) - param_attr2 = fluid.ParamAttr("fc2_param") - fc2 = fluid.layers.fc(fc1, size=10, param_attr=param_attr2) + param_attr1 = base.ParamAttr("fc1_param") + fc1 = base.layers.fc(image, size=10, param_attr=param_attr1) + param_attr2 = base.ParamAttr("fc2_param") + fc2 = base.layers.fc(fc1, size=10, param_attr=param_attr2) loss = paddle.mean(fc2) return loss # network 1: clip all parameter gradient - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): loss = network() paddle.nn.clip.set_gradient_clip( paddle.nn.ClipGradByGlobalNorm(clip_norm=2.0)) - sgd = fluid.optimizer.SGD(learning_rate=1e-3) + sgd = base.optimizer.SGD(learning_rate=1e-3) sgd.minimize(loss) # network 2: clip parameter gradient by name - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): loss = network() paddle.nn.clip.set_gradient_clip( paddle.nn.ClipGradByValue(min=-1.0, max=1.0), param_list=["fc1_param", "fc2_param"]) - sgd = fluid.optimizer.SGD(learning_rate=1e-3) + sgd = base.optimizer.SGD(learning_rate=1e-3) sgd.minimize(loss) # network 3: clip parameter gradient by value - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): loss = network() - param_var1 = fluid.default_main_program().global_block().var("fc1_param") - param_var2 = fluid.default_main_program().global_block().var("fc2_param") + param_var1 = base.default_main_program().global_block().var("fc1_param") + param_var2 = base.default_main_program().global_block().var("fc2_param") paddle.nn.clip.set_gradient_clip( paddle.nn.ClipGradByValue(min=-1.0, max=1.0), param_list=[param_var1, param_var2]) - sgd = fluid.optimizer.SGD(learning_rate=1e-3) + sgd = base.optimizer.SGD(learning_rate=1e-3) sgd.minimize(loss) # network 4: use 'set_gradient_clip' and 'optimize(grad_clip=clip)' together - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): loss = network() clip1 = paddle.nn.ClipGradByValue(min=-1.0, max=1.0) clip2 = paddle.nn.ClipGradByNorm(clip_norm=1.0) # Set the gradient clipping strategy: clip1 paddle.nn.clip.set_gradient_clip(clip1) # Set the gradient clipping strategy: clip2 - sgd = fluid.optimizer.SGD(learning_rate=1e-3, grad_clip=clip2) + sgd = base.optimizer.SGD(learning_rate=1e-3, grad_clip=clip2) sgd.minimize(loss) # 'set_gradient_clip' will not take effect when setting has a conflict, # and the gradient clipping strategy will be 'clip2' diff --git a/python/paddle/nn/decode.py b/python/paddle/nn/decode.py index 4563df700e411..be0a9fcc16dfa 100644 --- a/python/paddle/nn/decode.py +++ b/python/paddle/nn/decode.py @@ -22,7 +22,7 @@ from paddle.common_ops_import import default_main_program from paddle.framework import in_dynamic_mode -from ..fluid.data_feeder import convert_dtype +from ..base.data_feeder import convert_dtype __all__ = [] diff --git a/python/paddle/nn/functional/activation.py b/python/paddle/nn/functional/activation.py index 621a06ea8e1fc..f909b03cd0036 100644 --- a/python/paddle/nn/functional/activation.py +++ b/python/paddle/nn/functional/activation.py @@ -17,9 +17,9 @@ from paddle.framework import core from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only -from ...fluid.data_feeder import check_dtype, check_variable_and_dtype -from ...fluid.framework import convert_np_dtype_to_dtype_ -from ...fluid.layer_helper import LayerHelper +from ...base.data_feeder import check_dtype, check_variable_and_dtype +from ...base.framework import convert_np_dtype_to_dtype_ +from ...base.layer_helper import LayerHelper from ...tensor.manipulation import chunk from ...tensor.math import tanh # noqa: F401 from ...tensor.math import tanh_ # noqa: F401 diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index bc43e73c4163d..d2bb5523f810d 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -16,12 +16,12 @@ import paddle from paddle import _C_ops +from paddle.base.layer_helper import LayerHelper from paddle.common_ops_import import Variable, default_main_program -from paddle.fluid.layer_helper import LayerHelper from paddle.framework import core, in_dynamic_mode from paddle.tensor.creation import full -from ...fluid.data_feeder import ( +from ...base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 81af0d9be5f4f..00e45f789ae5e 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -13,19 +13,19 @@ # limitations under the License. from paddle import _C_ops, _legacy_C_ops, get_flags, in_dynamic_mode +from paddle.base.framework import _global_flags from paddle.device import ( get_all_custom_device_type, is_compiled_with_cuda, is_compiled_with_rocm, ) -from paddle.fluid.framework import _global_flags from paddle.tensor.manipulation import reshape from paddle.tensor.math import _add_with_axis +from ...base.data_feeder import check_dtype, check_variable_and_dtype +from ...base.layer_helper import LayerHelper from ...common_ops_import import Variable from ...device import get_cudnn_version -from ...fluid.data_feeder import check_dtype, check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper from ...framework import no_grad from ...tensor.manipulation import squeeze, unsqueeze from ...utils import ( diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 64352cd051dea..dc69092daed08 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -16,8 +16,8 @@ from paddle import _C_ops from paddle.framework import in_dynamic_mode -from ...fluid.data_feeder import check_type, check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper +from ...base.data_feeder import check_type, check_variable_and_dtype +from ...base.layer_helper import LayerHelper __all__ = [] @@ -71,7 +71,7 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None): sub = _C_ops.subtract(x, y) # p_norm op has not used epsilon, so change it to the following. if epsilon != 0.0: - epsilon = paddle.fluid.dygraph.base.to_variable( + epsilon = paddle.base.dygraph.base.to_variable( [epsilon], dtype=sub.dtype ) sub = _C_ops.add(sub, epsilon) diff --git a/python/paddle/nn/functional/extension.py b/python/paddle/nn/functional/extension.py index adfb976293987..252afc268bf7c 100644 --- a/python/paddle/nn/functional/extension.py +++ b/python/paddle/nn/functional/extension.py @@ -18,13 +18,13 @@ from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode -from ...common_ops_import import Variable -from ...fluid.data_feeder import ( +from ...base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, ) -from ...fluid.layer_helper import LayerHelper +from ...base.layer_helper import LayerHelper +from ...common_ops_import import Variable from ...framework import convert_np_dtype_to_dtype_, core from ...tensor.creation import assign diff --git a/python/paddle/nn/functional/flash_attention.py b/python/paddle/nn/functional/flash_attention.py index 822348d5be852..11b85df5d1377 100644 --- a/python/paddle/nn/functional/flash_attention.py +++ b/python/paddle/nn/functional/flash_attention.py @@ -15,8 +15,8 @@ import paddle import paddle.nn.functional as F from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base.layer_helper import LayerHelper +from paddle.base.wrapped_decorator import signature_safe_contextmanager g_enable_math = None g_enable_flash = None diff --git a/python/paddle/nn/functional/input.py b/python/paddle/nn/functional/input.py index e9bb3d19fce32..b76c2e5ea9b80 100644 --- a/python/paddle/nn/functional/input.py +++ b/python/paddle/nn/functional/input.py @@ -14,9 +14,9 @@ from paddle import _C_ops +from ...base.data_feeder import check_variable_and_dtype +from ...base.layer_helper import LayerHelper from ...common_ops_import import Variable -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper from ...framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index f0411d096dee4..539224de32ef2 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -16,15 +16,15 @@ # TODO: define loss functions of neural network import paddle -from paddle import _C_ops, fluid, in_dynamic_mode +from paddle import _C_ops, base, in_dynamic_mode from paddle.framework import core from paddle.static.nn.control_flow import Assert from paddle.utils import deprecated +from ...base.data_feeder import check_variable_and_dtype +from ...base.framework import _current_expected_place +from ...base.layer_helper import LayerHelper from ...common_ops_import import Variable -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.framework import _current_expected_place -from ...fluid.layer_helper import LayerHelper from ...tensor.manipulation import reshape __all__ = [] @@ -161,7 +161,7 @@ def log_loss(input, label, epsilon=1e-4, name=None): return loss -def fluid_softmax_with_cross_entropy( +def base_softmax_with_cross_entropy( logits, label, soft_label=False, @@ -380,7 +380,7 @@ def npair_loss(anchor, positive, labels, l2_reg=0.002): similarity_matrix = paddle.matmul( anchor, positive, transpose_x=False, transpose_y=True ) - softmax_ce = fluid_softmax_with_cross_entropy( + softmax_ce = base_softmax_with_cross_entropy( logits=similarity_matrix, label=labels, soft_label=True ) cross_entropy = paddle.sum(labels * softmax_ce, 0) @@ -1199,7 +1199,7 @@ def margin_ranking_loss( out = _C_ops.subtract(other, input) out = _C_ops.multiply(out, label) if margin != 0.0: - margin = fluid.dygraph.base.to_variable([margin], dtype=out.dtype) + margin = base.dygraph.base.to_variable([margin], dtype=out.dtype) out = _C_ops.add(out, margin) out = _C_ops.relu(out) if reduction == 'sum': @@ -1679,13 +1679,13 @@ def kl_div(input, label, reduction='mean', name=None): """ # ugly type promotion if ( - fluid.data_feeder.convert_dtype(input.dtype) == 'float32' - and fluid.data_feeder.convert_dtype(label.dtype) == 'float64' + base.data_feeder.convert_dtype(input.dtype) == 'float32' + and base.data_feeder.convert_dtype(label.dtype) == 'float64' ): input = paddle.cast(input, 'float64') elif ( - fluid.data_feeder.convert_dtype(input.dtype) == 'float64' - and fluid.data_feeder.convert_dtype(label.dtype) == 'float32' + base.data_feeder.convert_dtype(input.dtype) == 'float64' + and base.data_feeder.convert_dtype(label.dtype) == 'float32' ): label = paddle.cast(label, 'float64') @@ -1709,7 +1709,7 @@ def kl_div(input, label, reduction='mean', name=None): check_variable_and_dtype( label, 'label', ['float32', 'float64'], 'kl_div' ) - fluid.data_feeder.check_type(reduction, 'reduction', str, 'kl_div') + base.data_feeder.check_type(reduction, 'reduction', str, 'kl_div') loss = helper.create_variable_for_type_inference(dtype=input.dtype) helper.append_op( @@ -2483,7 +2483,7 @@ def softmax_with_cross_entropy( [1.15328646]) """ - return fluid_softmax_with_cross_entropy( + return base_softmax_with_cross_entropy( logits, label, soft_label, @@ -2823,13 +2823,13 @@ def cross_entropy( out = _C_ops.multiply(out, weight_gather_reshape) if reduction == "sum": - # because of fluid_softmax_with_cross_entropy op's inner logic, + # because of base_softmax_with_cross_entropy op's inner logic, # in the out tensor of this op, the loss of sample with class_index==ignore_index is 0 # so, reduce_sum all directly is ok return _C_ops.sum(out, [], None, False) elif reduction == "mean": # 1. if weight==none, - # numerator: reduce_sum all loss directly is ok causeof fluid_softmax_with_cross_entropy's inner logic + # numerator: reduce_sum all loss directly is ok causeof base_softmax_with_cross_entropy's inner logic # denominator: count sample num with class_index!=ignore_index # 2. else # numerator: loss's weighted sum @@ -3126,7 +3126,7 @@ def sigmoid_focal_loss( ), ) - alpha = fluid.dygraph.base.to_variable([alpha], dtype=loss.dtype) + alpha = base.dygraph.base.to_variable([alpha], dtype=loss.dtype) alpha_t = _C_ops.add( _C_ops.multiply(alpha, label), _C_ops.multiply( @@ -3135,7 +3135,7 @@ def sigmoid_focal_loss( ) loss = _C_ops.multiply(alpha_t, loss) - gamma = fluid.dygraph.base.to_variable([gamma], dtype=loss.dtype) + gamma = base.dygraph.base.to_variable([gamma], dtype=loss.dtype) gamma_t = _C_ops.pow(_C_ops.subtract(one, p_t), gamma) loss = _C_ops.multiply(gamma_t, loss) @@ -3993,10 +3993,10 @@ def soft_margin_loss(input, label, reduction='mean', name=None): ) if not in_dynamic_mode(): - fluid.data_feeder.check_variable_and_dtype( + base.data_feeder.check_variable_and_dtype( input, 'input', ['float32', 'float64'], 'soft_margin_loss' ) - fluid.data_feeder.check_variable_and_dtype( + base.data_feeder.check_variable_and_dtype( label, 'label', ['int32', 'int64', 'float32', 'float64'], diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 81ae8efd37b05..a510c1ac625f6 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -16,11 +16,11 @@ # TODO: define normalization api import paddle -from paddle import _C_ops, fluid, in_dynamic_mode -from paddle.fluid.framework import in_dygraph_mode +from paddle import _C_ops, base, in_dynamic_mode +from paddle.base.framework import in_dygraph_mode -from ...fluid.data_feeder import check_type, check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper +from ...base.data_feeder import check_type, check_variable_and_dtype +from ...base.layer_helper import LayerHelper __all__ = [] @@ -79,7 +79,7 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): """ if in_dygraph_mode(): - eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype) + eps = base.dygraph.base.to_variable([epsilon], dtype=x.dtype) out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False) return x / _C_ops.maximum(out, eps) @@ -236,7 +236,7 @@ def batch_norm( } helper = LayerHelper('batch_norm', **locals()) - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype param_dtype = ( x.dtype @@ -361,7 +361,7 @@ def layer_norm( # create output helper = LayerHelper('layer_norm', **locals()) - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype param_dtype = ( x.dtype if convert_dtype(x.dtype) != 'float16' else 'float32' diff --git a/python/paddle/nn/functional/pooling.py b/python/paddle/nn/functional/pooling.py index 955d63469d399..2f4ac35cbb078 100755 --- a/python/paddle/nn/functional/pooling.py +++ b/python/paddle/nn/functional/pooling.py @@ -15,10 +15,10 @@ import numpy as np from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode -from paddle.fluid.framework import Variable, in_dygraph_mode +from paddle.base.framework import Variable, in_dygraph_mode -from ...fluid.data_feeder import check_type, check_variable_and_dtype -from ...fluid.layers import LayerHelper +from ...base.data_feeder import check_type, check_variable_and_dtype +from ...base.layers import LayerHelper from ...tensor.manipulation import squeeze, unsqueeze # TODO: define pooling functions diff --git a/python/paddle/nn/functional/sparse_attention.py b/python/paddle/nn/functional/sparse_attention.py index bef511a3fa859..1d5f5013435bb 100644 --- a/python/paddle/nn/functional/sparse_attention.py +++ b/python/paddle/nn/functional/sparse_attention.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle import _legacy_C_ops, in_dynamic_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper def sparse_attention( diff --git a/python/paddle/nn/functional/vision.py b/python/paddle/nn/functional/vision.py index 37975595bd61e..d49ca156f77ea 100644 --- a/python/paddle/nn/functional/vision.py +++ b/python/paddle/nn/functional/vision.py @@ -13,12 +13,12 @@ # limitations under the License. from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode -from paddle.fluid.framework import in_dygraph_mode +from paddle.base.framework import in_dygraph_mode +from ...base.data_feeder import check_variable_and_dtype +from ...base.layer_helper import LayerHelper from ...common_ops_import import Variable from ...device import get_cudnn_version, is_compiled_with_rocm -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper __all__ = [] diff --git a/python/paddle/nn/initializer/Bilinear.py b/python/paddle/nn/initializer/Bilinear.py index 403f8773f15b1..c0a1fe4b31ab8 100644 --- a/python/paddle/nn/initializer/Bilinear.py +++ b/python/paddle/nn/initializer/Bilinear.py @@ -16,8 +16,8 @@ from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/initializer/__init__.py b/python/paddle/nn/initializer/__init__.py index 6ef516c8b6af5..adc81e5bbfd5d 100644 --- a/python/paddle/nn/initializer/__init__.py +++ b/python/paddle/nn/initializer/__init__.py @@ -13,7 +13,7 @@ # limitations under the License. # TODO: define the initializers to create a Parameter in neural network -from ...fluid.initializer import set_global_initializer # noqa: F401 +from ...base.initializer import set_global_initializer # noqa: F401 from .Bilinear import Bilinear # noqa: F401 diff --git a/python/paddle/nn/initializer/assign.py b/python/paddle/nn/initializer/assign.py index b85f3e7509fa8..a1cd06cab59b4 100644 --- a/python/paddle/nn/initializer/assign.py +++ b/python/paddle/nn/initializer/assign.py @@ -14,9 +14,9 @@ import paddle from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.data_feeder import check_type -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.data_feeder import check_type +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/initializer/constant.py b/python/paddle/nn/initializer/constant.py index 355bac8b784e3..745ac44ac2273 100644 --- a/python/paddle/nn/initializer/constant.py +++ b/python/paddle/nn/initializer/constant.py @@ -14,8 +14,8 @@ from paddle import _C_ops -from ...fluid import core, framework -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework +from ...base.framework import _current_expected_place, in_dygraph_mode # TODO: define the initializers of Constant in neural network from .initializer import Initializer diff --git a/python/paddle/nn/initializer/dirac.py b/python/paddle/nn/initializer/dirac.py index cb1c5efbf3a91..3673a20ddbf61 100644 --- a/python/paddle/nn/initializer/dirac.py +++ b/python/paddle/nn/initializer/dirac.py @@ -15,11 +15,11 @@ from paddle import _C_ops, in_dynamic_mode from paddle.utils import unique_name -from ... import fluid -from ...fluid import framework -from ...fluid.core import VarDesc -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.framework import _current_expected_place +from ... import base +from ...base import framework +from ...base.core import VarDesc +from ...base.data_feeder import check_variable_and_dtype +from ...base.framework import _current_expected_place from .initializer import Initializer __all__ = [] @@ -133,7 +133,7 @@ def __call__(self, var, block=None): out_var = var op = None if framework.in_dygraph_mode(): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): place = _current_expected_place() _C_ops.full_( out_var, out_var.shape, str(float(0)), out_var.dtype, place @@ -176,7 +176,7 @@ def __call__(self, var, block=None): offset += origin_shape[k] // 2 * stride idx_list.append(offset) if framework.in_dygraph_mode(): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): tmp_out = _C_ops.reshape(out_var, [-1]) tmp_out._share_underline_tensor_to(out_var) else: @@ -203,7 +203,7 @@ def __call__(self, var, block=None): ) if framework.in_dygraph_mode(): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): tmp_tensor = framework._create_tensor() _C_ops.assign_value_( tmp_tensor, @@ -232,7 +232,7 @@ def __call__(self, var, block=None): ) if framework.in_dygraph_mode(): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): tmp_tensor = framework._create_tensor() _C_ops.assign_value_( tmp_tensor, @@ -256,7 +256,7 @@ def __call__(self, var, block=None): ) if framework.in_dygraph_mode(): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): tmp_out = _C_ops.scatter( out_var, index_tensor, value_tensor, True ) diff --git a/python/paddle/nn/initializer/initializer.py b/python/paddle/nn/initializer/initializer.py index 9d5880aa09561..c77d6ae466637 100644 --- a/python/paddle/nn/initializer/initializer.py +++ b/python/paddle/nn/initializer/initializer.py @@ -17,7 +17,7 @@ import numpy as np -from ...fluid.framework import default_main_program, in_dygraph_mode +from ...base.framework import default_main_program, in_dygraph_mode from .lazy_init import lazy_init_helper __all__ = [] diff --git a/python/paddle/nn/initializer/kaiming.py b/python/paddle/nn/initializer/kaiming.py index 1f519a31f77c3..752185ea819a1 100644 --- a/python/paddle/nn/initializer/kaiming.py +++ b/python/paddle/nn/initializer/kaiming.py @@ -17,8 +17,8 @@ from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer, calculate_gain __all__ = [] diff --git a/python/paddle/nn/initializer/lazy_init.py b/python/paddle/nn/initializer/lazy_init.py index e2321f682f77e..7c67649738203 100644 --- a/python/paddle/nn/initializer/lazy_init.py +++ b/python/paddle/nn/initializer/lazy_init.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ...fluid import framework +from ...base import framework __all__ = ["LazyGuard"] diff --git a/python/paddle/nn/initializer/normal.py b/python/paddle/nn/initializer/normal.py index d735782b3090e..c1bcb89f676f7 100644 --- a/python/paddle/nn/initializer/normal.py +++ b/python/paddle/nn/initializer/normal.py @@ -14,9 +14,9 @@ from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.data_feeder import check_variable_and_dtype +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/initializer/orthogonal.py b/python/paddle/nn/initializer/orthogonal.py index 035e13d5c7449..ed16e75827e14 100644 --- a/python/paddle/nn/initializer/orthogonal.py +++ b/python/paddle/nn/initializer/orthogonal.py @@ -15,9 +15,9 @@ from paddle import _C_ops from paddle.utils import unique_name -from ...fluid import framework -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.dygraph import no_grad +from ...base import framework +from ...base.data_feeder import check_variable_and_dtype +from ...base.dygraph import no_grad from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/initializer/uniform.py b/python/paddle/nn/initializer/uniform.py index a46ae2a41e3cd..f5dfa1fb00310 100644 --- a/python/paddle/nn/initializer/uniform.py +++ b/python/paddle/nn/initializer/uniform.py @@ -14,9 +14,9 @@ from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.data_feeder import check_variable_and_dtype +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/initializer/xavier.py b/python/paddle/nn/initializer/xavier.py index 60242ecf5b27c..147328c222c46 100644 --- a/python/paddle/nn/initializer/xavier.py +++ b/python/paddle/nn/initializer/xavier.py @@ -16,9 +16,9 @@ from paddle import _C_ops -from ...fluid import core, framework, unique_name -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.framework import _current_expected_place, in_dygraph_mode +from ...base import core, framework, unique_name +from ...base.data_feeder import check_variable_and_dtype +from ...base.framework import _current_expected_place, in_dygraph_mode from .initializer import Initializer __all__ = [] diff --git a/python/paddle/nn/layer/container.py b/python/paddle/nn/layer/container.py index 8fa65aa8f340e..7798846a3b9d7 100644 --- a/python/paddle/nn/layer/container.py +++ b/python/paddle/nn/layer/container.py @@ -15,8 +15,8 @@ from collections import OrderedDict from collections.abc import Iterable, Mapping -from ...fluid.dygraph.base import param_guard -from ...fluid.framework import Parameter +from ...base.dygraph.base import param_guard +from ...base.framework import Parameter from .layers import Layer __all__ = [] diff --git a/python/paddle/nn/layer/layers.py b/python/paddle/nn/layer/layers.py index 2e3bd18012955..8e8c06ea2df49 100644 --- a/python/paddle/nn/layer/layers.py +++ b/python/paddle/nn/layer/layers.py @@ -23,26 +23,26 @@ import paddle from paddle import nn, profiler -from paddle.fluid import core, framework, unique_name -from paddle.fluid.core import VarDesc -from paddle.fluid.dygraph import no_grad -from paddle.fluid.dygraph.base import ( +from paddle.base import core, framework, unique_name +from paddle.base.core import VarDesc +from paddle.base.dygraph import no_grad +from paddle.base.dygraph.base import ( _convert_into_variable, in_declarative_mode, program_desc_tracing_guard, ) -from paddle.fluid.dygraph_utils import _append_activation_in_dygraph -from paddle.fluid.executor import Executor, global_scope -from paddle.fluid.framework import Parameter, Program -from paddle.fluid.framework import _current_expected_place as _get_device -from paddle.fluid.framework import ( +from paddle.base.dygraph_utils import _append_activation_in_dygraph +from paddle.base.executor import Executor, global_scope +from paddle.base.framework import Parameter, Program +from paddle.base.framework import _current_expected_place as _get_device +from paddle.base.framework import ( _global_flags, convert_np_dtype_to_dtype_, default_main_program, in_dygraph_mode, ) -from paddle.fluid.layer_helper_base import LayerHelperBase -from paddle.fluid.param_attr import ParamAttr +from paddle.base.layer_helper_base import LayerHelperBase +from paddle.base.param_attr import ParamAttr from paddle.profiler.utils import in_profiler_mode from paddle.utils import deprecated @@ -2164,9 +2164,7 @@ def _transform(self, t, device, dtype, blocking): # 2. cast param / Tensor to dtype if dtype is not None and dtype != t_used.dtype: - with paddle.fluid.framework._dygraph_place_guard( - place=t_used.place - ): + with paddle.base.framework._dygraph_place_guard(place=t_used.place): t_casted = t_used.cast(dtype=dtype) else: t_casted = t_used diff --git a/python/paddle/nn/layer/loss.py b/python/paddle/nn/layer/loss.py index b4da52f4ad2ad..eb2a66caedf77 100644 --- a/python/paddle/nn/layer/loss.py +++ b/python/paddle/nn/layer/loss.py @@ -15,8 +15,8 @@ import paddle # TODO: define loss functions of neural network -from paddle import fluid, in_dynamic_mode -from paddle.fluid.framework import in_dygraph_mode +from paddle import base, in_dynamic_mode +from paddle.base.framework import in_dygraph_mode from .. import functional as F from .layers import Layer @@ -583,10 +583,10 @@ def __init__(self, reduction='mean'): def forward(self, input, label): if not in_dynamic_mode(): - fluid.data_feeder.check_variable_and_dtype( + base.data_feeder.check_variable_and_dtype( input, 'input', ['float32', 'float64'], 'MSELoss' ) - fluid.data_feeder.check_variable_and_dtype( + base.data_feeder.check_variable_and_dtype( label, 'label', ['float32', 'float64'], 'MSELoss' ) diff --git a/python/paddle/nn/layer/norm.py b/python/paddle/nn/layer/norm.py index bea68c61d79a9..53e71bb26a3b3 100644 --- a/python/paddle/nn/layer/norm.py +++ b/python/paddle/nn/layer/norm.py @@ -35,8 +35,8 @@ from paddle import _C_ops, in_dynamic_mode from paddle.device import get_all_custom_device_type -from ...fluid import dygraph_utils -from ...fluid.data_feeder import check_variable_and_dtype +from ...base import dygraph_utils +from ...base.data_feeder import check_variable_and_dtype from ...framework import ParamAttr, _global_flags, get_default_dtype, no_grad from .. import functional as F from ..functional import batch_norm, instance_norm, layer_norm @@ -955,14 +955,14 @@ class BatchNorm(Layer): Examples: .. code-block:: python - >>> import paddle.fluid as fluid + >>> import paddle.base as base >>> import paddle.nn as nn - >>> from paddle.fluid.dygraph.base import to_variable + >>> from paddle.base.dygraph.base import to_variable >>> import numpy as np >>> x = np.random.random(size=(3, 10, 3, 7)).astype('float32') - >>> with fluid.dygraph.guard(): + >>> with base.dygraph.guard(): ... x = to_variable(x) ... batch_norm = nn.layer.norm.BatchNorm(10) ... hidden1 = batch_norm(x) diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index a2122c2dab3b7..3f5adf54d3968 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -20,14 +20,14 @@ import paddle from paddle import _C_ops, _legacy_C_ops, framework, in_dynamic_mode -from paddle.common_ops_import import Variable -from paddle.fluid.data_feeder import check_type, check_variable_and_dtype -from paddle.fluid.dygraph.base import NON_PERSISTABLE_VAR_NAME_SUFFIX -from paddle.fluid.framework import ( +from paddle.base.data_feeder import check_type, check_variable_and_dtype +from paddle.base.dygraph.base import NON_PERSISTABLE_VAR_NAME_SUFFIX +from paddle.base.framework import ( default_startup_program, in_dygraph_mode, program_guard, ) +from paddle.common_ops_import import Variable from paddle.framework import core from paddle.nn import functional as F from paddle.nn import initializer as I @@ -272,7 +272,7 @@ def _switch_grad(x, stop=False): ) mask = paddle.reverse(mask, axis=[0]) if sequence_length else None - with paddle.fluid.framework.device_guard("cpu"): + with paddle.base.framework.device_guard("cpu"): start_i = paddle.zeros([], dtype="int64") end = max_seq_len @@ -296,12 +296,12 @@ def _switch_grad(x, stop=False): with while_op.block(): step_in = inputs[start_i] - # step_in = paddle.fluid.layers.Print( step_in, message="step in") + # step_in = paddle.base.layers.Print( step_in, message="step in") pre_state = paddle.utils.map_structure( lambda x: paddle.tensor.array_read(x, start_i), init_array ) outputs, new_states = cell(step_in, pre_state, **kwargs) - assert isinstance(outputs, paddle.fluid.framework.Variable) + assert isinstance(outputs, paddle.base.framework.Variable) paddle.utils.assert_same_structure(new_states, pre_state) if sequence_length: step_mask = paddle.unsqueeze(mask[start_i], 1) @@ -317,7 +317,7 @@ def _switch_grad(x, stop=False): paddle.tensor.array_write(outputs, start_i, out_array) - with paddle.fluid.framework.device_guard("cpu"): + with paddle.base.framework.device_guard("cpu"): start_i = paddle.tensor.increment(x=start_i, value=1) paddle.utils.map_structure( lambda x, y: paddle.tensor.array_write(x, start_i, y), @@ -325,7 +325,7 @@ def _switch_grad(x, stop=False): init_array, ) - with paddle.fluid.framework.device_guard("cpu"): + with paddle.base.framework.device_guard("cpu"): new_cond = paddle.tensor.less_than(start_i, end) paddle.assign(new_cond, cond) @@ -740,7 +740,7 @@ class SimpleRNNCell(RNNCellBase): - **states** (Tensor): shape `[batch_size, hidden_size]`, the new hidden state, corresponding to :math:`h_{t}` in the formula. Notes: - All the weights and bias are initialized with `Uniform(-std, std)` by default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more information about parameter initialization, please refer to :ref:`api_fluid_ParamAttr`. + All the weights and bias are initialized with `Uniform(-std, std)` by default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more information about parameter initialization, please refer to :ref:`api_base_ParamAttr`. Examples: @@ -893,7 +893,7 @@ class LSTMCell(RNNCellBase): Notes: All the weights and bias are initialized with `Uniform(-std, std)` by default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more - information about parameter initialization, please refer to :ref:`api_fluid_ParamAttr`. + information about parameter initialization, please refer to :ref:`api_base_ParamAttr`. Examples: @@ -1054,7 +1054,7 @@ class GRUCell(RNNCellBase): Notes: All the weights and bias are initialized with `Uniform(-std, std)` by default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more - information about parameter initialization, please refer to s:ref:`api_fluid_ParamAttr`. + information about parameter initialization, please refer to s:ref:`api_base_ParamAttr`. Examples: diff --git a/python/paddle/nn/layer/transformer.py b/python/paddle/nn/layer/transformer.py index e2e3f052240a4..d9260f9b22911 100644 --- a/python/paddle/nn/layer/transformer.py +++ b/python/paddle/nn/layer/transformer.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle.fluid.data_feeder import convert_dtype +from paddle.base.data_feeder import convert_dtype from ... import tensor from ...framework import ParamAttr @@ -800,7 +800,7 @@ class TransformerDecoderLayer(Layer): for linear in FFN. Otherwise, the three sub-layers all uses it as `weight_attr` to create parameters. Default: None, which means the default weight parameter property is used. See usage for details - in :ref:`api_paddle_fluid_param_attr_ParamAttr` . + in :ref:`api_paddle_base_param_attr_ParamAttr` . bias_attr (ParamAttr|list|tuple|bool, optional): To specify the bias parameter property. If it is a list/tuple, `bias_attr[0]` would be used as `bias_attr` for self attention, `bias_attr[1]` would be used as `bias_attr` for diff --git a/python/paddle/nn/quant/quant_layers.py b/python/paddle/nn/quant/quant_layers.py index 0df2c1c2c5943..a83cbef801f94 100644 --- a/python/paddle/nn/quant/quant_layers.py +++ b/python/paddle/nn/quant/quant_layers.py @@ -16,9 +16,9 @@ import paddle from paddle import _legacy_C_ops, in_dynamic_mode -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.framework import _create_tensor -from paddle.fluid.log_helper import get_logger +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.framework import _create_tensor +from paddle.base.log_helper import get_logger from paddle.framework import ParamAttr, core from paddle.nn import Layer from paddle.nn import functional as F diff --git a/python/paddle/nn/utils/transform_parameters.py b/python/paddle/nn/utils/transform_parameters.py index 16a5045bb05a0..c3f9562e544e8 100644 --- a/python/paddle/nn/utils/transform_parameters.py +++ b/python/paddle/nn/utils/transform_parameters.py @@ -16,7 +16,7 @@ import paddle from paddle import _C_ops -from paddle.fluid.framework import ( +from paddle.base.framework import ( _create_tensor, _dygraph_tracer, dygraph_only, @@ -28,7 +28,7 @@ def _inplace_reshape_dygraph(x, shape): x_shape = _create_tensor(dtype='int64') if in_dygraph_mode(): - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): tmp_out = _C_ops.reshape(x, shape) tmp_out._share_underline_tensor_to(x) else: @@ -68,7 +68,7 @@ def _stride_column(param): """ assert len(param.shape) == 2 shape = [param.shape[1], param.shape[0]] - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): reshape_var = paddle.reshape(param, shape) transpose_var = paddle.transpose(reshape_var, [1, 0]) transpose_var._share_underline_tensor_to(param) @@ -108,7 +108,7 @@ def parameters_to_vector(parameters, name=None): out = _create_tensor(dtype=dtype) if in_dygraph_mode(): - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): tmp = _C_ops.concat(parameters, 0) tmp._share_underline_tensor_to(out) else: @@ -163,7 +163,7 @@ def vector_to_parameters(vec, parameters, name=None): sections.append(0) if in_dygraph_mode(): - with paddle.fluid.dygraph.no_grad(): + with paddle.base.dygraph.no_grad(): res = _C_ops.split(vec, sections, 0) for i in range(0, len(parameters)): res[i]._share_underline_tensor_to(parameters[i]) diff --git a/python/paddle/nn/utils/weight_norm_hook.py b/python/paddle/nn/utils/weight_norm_hook.py index 3747a14cc118a..4ef5fdf2deefd 100644 --- a/python/paddle/nn/utils/weight_norm_hook.py +++ b/python/paddle/nn/utils/weight_norm_hook.py @@ -14,8 +14,8 @@ import paddle from paddle import _C_ops -from ...fluid.data_feeder import check_variable_and_dtype -from ...fluid.layer_helper import LayerHelper +from ...base.data_feeder import check_variable_and_dtype +from ...base.layer_helper import LayerHelper from ...framework import in_dynamic_mode __all__ = [] diff --git a/python/paddle/optimizer/adadelta.py b/python/paddle/optimizer/adadelta.py index e4826997761c1..3523ece1e831f 100644 --- a/python/paddle/optimizer/adadelta.py +++ b/python/paddle/optimizer/adadelta.py @@ -16,8 +16,8 @@ from paddle import _C_ops -from ..fluid import framework -from ..fluid.dygraph import no_grad +from ..base import framework +from ..base.dygraph import no_grad from ..framework import in_dynamic_mode from .optimizer import Optimizer @@ -54,15 +54,15 @@ class Adadelta(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ It canbe a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three cliping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` . diff --git a/python/paddle/optimizer/adagrad.py b/python/paddle/optimizer/adagrad.py index 3373866ba048a..eae910870ae53 100644 --- a/python/paddle/optimizer/adagrad.py +++ b/python/paddle/optimizer/adagrad.py @@ -13,7 +13,7 @@ # limitations under the License. import warnings -from ..fluid import framework +from ..base import framework from .optimizer import Optimizer __all__ = [] @@ -53,7 +53,7 @@ class Adagrad(Optimizer): weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. It canbe a float value as coeff of L2 regularization or :ref:`api_paddle_regularizer_L1Decay`, :ref:`api_paddle_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_paddle_fluid_param_attr_aramAttr` already, + If a parameter has set regularizer using :ref:`api_paddle_base_param_attr_aramAttr` already, the regularization setting here in optimizer will be ignored for this parameter. Otherwise, the regularization setting here in optimizer will take effect. Default None, meaning there is no regularization. diff --git a/python/paddle/optimizer/adam.py b/python/paddle/optimizer/adam.py index f58b82a13188c..75a1415690445 100644 --- a/python/paddle/optimizer/adam.py +++ b/python/paddle/optimizer/adam.py @@ -18,9 +18,9 @@ import paddle from paddle import _C_ops -from ..fluid import core, framework -from ..fluid.dygraph import base as imperative_base -from ..fluid.framework import Variable, in_dygraph_mode +from ..base import core, framework +from ..base.dygraph import base as imperative_base +from ..base.framework import Variable, in_dygraph_mode from .optimizer import Optimizer __all__ = [] @@ -72,15 +72,15 @@ class Adam(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. It canbe a float value as coeff of L2 regularization or - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, the regularization setting here in optimizer will be ignored for this parameter. Otherwise, the regularization setting here in optimizer will take effect. Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three cliping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. lazy_mode (bool, optional): The official Adam algorithm has two moving-average accumulators. The accumulators are updated at every step. Every element of the two moving-average is updated in both dense mode and sparse mode. If the size of parameter is very large, @@ -419,7 +419,7 @@ def step(self): >>> adam.step() >>> adam.clear_grad() """ - if paddle.fluid.dygraph.base.in_declarative_mode(): + if paddle.base.dygraph.base.in_declarative_mode(): self._declarative_step() return diff --git a/python/paddle/optimizer/adamax.py b/python/paddle/optimizer/adamax.py index 26988e9d3c96a..ff7ce4a232224 100644 --- a/python/paddle/optimizer/adamax.py +++ b/python/paddle/optimizer/adamax.py @@ -16,9 +16,9 @@ from paddle import _C_ops -from ..fluid import core, framework -from ..fluid.dygraph import no_grad -from ..fluid.framework import name_scope +from ..base import core, framework +from ..base.dygraph import no_grad +from ..base.framework import name_scope from .optimizer import Optimizer __all__ = [] @@ -67,15 +67,15 @@ class Adamax(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. It can be a float value as coeff of L2 regularization or - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, the regularization setting here in optimizer will be ignored for this parameter. Otherwise, the regularization setting here in optimizer will take effect. Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. The default value is None. diff --git a/python/paddle/optimizer/adamw.py b/python/paddle/optimizer/adamw.py index 6f61e5ddd5b7c..823358bc1a717 100644 --- a/python/paddle/optimizer/adamw.py +++ b/python/paddle/optimizer/adamw.py @@ -19,9 +19,9 @@ import paddle from .. import _C_ops -from ..fluid import core, framework -from ..fluid.dygraph import base as imperative_base -from ..fluid.framework import Parameter, Variable +from ..base import core, framework +from ..base.dygraph import base as imperative_base +from ..base.framework import Parameter, Variable from ..nn.clip import GradientClipBase from .lr import LRScheduler from .optimizer import Optimizer @@ -77,8 +77,8 @@ class AdamW(Optimizer): Default: None. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. lazy_mode (bool, optional): The official Adam algorithm has two moving-average accumulators. The accumulators are updated at every step. Every element of the two moving-average is updated in both dense mode and sparse mode. If the size of parameter is very large, @@ -557,7 +557,7 @@ def step(self): >>> opt.step() >>> opt.clear_grad() """ - if paddle.fluid.dygraph.base.in_declarative_mode(): + if paddle.base.dygraph.base.in_declarative_mode(): self._declarative_step() return diff --git a/python/paddle/optimizer/lamb.py b/python/paddle/optimizer/lamb.py index f8662941c78d6..b409e88e338e9 100644 --- a/python/paddle/optimizer/lamb.py +++ b/python/paddle/optimizer/lamb.py @@ -13,10 +13,10 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.executor import global_scope +from paddle.base.executor import global_scope -from ..fluid import core, framework -from ..fluid.framework import Variable +from ..base import core, framework +from ..base.framework import Variable from .optimizer import Optimizer __all__ = [] @@ -68,9 +68,9 @@ class Lamb(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_paddle_fluid_clip_ClipGradByGlobalNorm` , :ref:`api_paddle_fluid_clip_ClipGradByNorm` , - :ref:`api_paddle_fluid_clip_ClipGradByValue` ). If you want better convergence, it is recommended - to use :ref:`api_paddle_fluid_clip_ClipGradByGlobalNorm` . Default None, meaning there is no gradient clipping. + ( :ref:`api_paddle_base_clip_ClipGradByGlobalNorm` , :ref:`api_paddle_base_clip_ClipGradByNorm` , + :ref:`api_paddle_base_clip_ClipGradByValue` ). If you want better convergence, it is recommended + to use :ref:`api_paddle_base_clip_ClipGradByGlobalNorm` . Default None, meaning there is no gradient clipping. exclude_from_weight_decay_fn (function, optional): whether to skip weight decay for a parameter when this function returns True while take the parameter as input. always_adapt (bool, optional): whether to use Layer-wise LR adaptation. By default, skip adaptation on parameters that are excluded from weight decay, unless always_adapt == True, then always enable LR adaptation. diff --git a/python/paddle/optimizer/lbfgs.py b/python/paddle/optimizer/lbfgs.py index a3efa46ea5ef5..f552f2d67ab74 100644 --- a/python/paddle/optimizer/lbfgs.py +++ b/python/paddle/optimizer/lbfgs.py @@ -17,7 +17,7 @@ import paddle -from ..fluid import framework +from ..base import framework from .optimizer import Optimizer __all__ = [] @@ -338,16 +338,16 @@ class LBFGS(Optimizer): parameters (list|tuple, optional): List/Tuple of ``Tensor`` names to update to minimize ``loss``. \ This parameter is required in dygraph mode. The default value is None. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ - It can be a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + It canbe a float value as coeff of L2 regularization or \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. - grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of \ - some derived class of ``GradientClipBase`` . There are three clipping strategies \ - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , \ - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of \ + some derived class of ``GradientClipBase`` . There are three cliping strategies \ + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , \ + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. The default value is None. diff --git a/python/paddle/optimizer/lr.py b/python/paddle/optimizer/lr.py index 5915be7cf526a..113b95eec2598 100644 --- a/python/paddle/optimizer/lr.py +++ b/python/paddle/optimizer/lr.py @@ -19,14 +19,14 @@ import paddle from paddle import Tensor -from paddle.fluid import core -from paddle.fluid.data_feeder import check_type -from paddle.fluid.framework import ( +from paddle.base import core +from paddle.base.data_feeder import check_type +from paddle.base.framework import ( Variable, default_main_program, in_dygraph_mode, ) -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper __all__ = [ # noqa 'LRScheduler', diff --git a/python/paddle/optimizer/momentum.py b/python/paddle/optimizer/momentum.py index 59b9657aa43bb..36d09f00c0e61 100644 --- a/python/paddle/optimizer/momentum.py +++ b/python/paddle/optimizer/momentum.py @@ -19,7 +19,7 @@ from paddle.framework import in_dynamic_mode from paddle.regularizer import L2Decay -from ..fluid import core, framework +from ..base import core, framework from .optimizer import Optimizer __all__ = [] @@ -59,15 +59,15 @@ class Momentum(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ It can be a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. multi_precision (bool, optional): Whether to use multi-precision during weight updating. Default is false. rescale_grad (float, optional): Multiply the gradient with `rescale_grad` before updating. \ Often choose to be ``1.0/batch_size``. diff --git a/python/paddle/optimizer/optimizer.py b/python/paddle/optimizer/optimizer.py index 146413b84572d..f95db125adb2a 100644 --- a/python/paddle/optimizer/optimizer.py +++ b/python/paddle/optimizer/optimizer.py @@ -20,8 +20,8 @@ import paddle import paddle.autograd as imperative_base from paddle import _C_ops -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle.base import core +from paddle.base.framework import ( Variable, _current_expected_place, default_main_program, @@ -31,10 +31,10 @@ ) from paddle.regularizer import L2Decay -from ..fluid import framework, unique_name -from ..fluid.backward import _get_no_grad_set_name, append_backward -from ..fluid.framework import Parameter, program_guard -from ..fluid.layer_helper import LayerHelper +from ..base import framework, unique_name +from ..base.backward import _get_no_grad_set_name, append_backward +from ..base.framework import Parameter, program_guard +from ..base.layer_helper import LayerHelper from .lr import LRScheduler __all__ = [] @@ -106,15 +106,15 @@ class Optimizer: The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ It canbe a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient cliping strategy, it's an instance of \ some derived class of ``GradientClipBase`` . There are three cliping strategies \ - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , \ - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , \ + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. The default value is None. @@ -480,7 +480,7 @@ def do_create(): persistable=True, ) - with paddle.fluid.framework.dygraph_guard_if_declarative(): + with paddle.base.framework.dygraph_guard_if_declarative(): do_create() @framework.dygraph_only @@ -1067,7 +1067,7 @@ def _create_optimization_pass( ) if isinstance(parameters_and_grads, list): - with paddle.fluid.framework.dygraph_guard_if_declarative(): + with paddle.base.framework.dygraph_guard_if_declarative(): self._create_accumulators( target_block, [ @@ -1083,7 +1083,7 @@ def _create_optimization_pass( for p in params_acc_dict['params'] if not p[0].stop_gradient ] - with paddle.fluid.framework.dygraph_guard_if_declarative(): + with paddle.base.framework.dygraph_guard_if_declarative(): self._create_accumulators(target_block, params_acc_dict) if framework.in_dygraph_mode(): @@ -1156,9 +1156,9 @@ def backward( Args: loss (Tensor): ``loss`` tensor to run optimizations. - startup_program (Program, optional): :ref:`api_fluid_Program` for + startup_program (Program, optional): :ref:`api_base_Program` for initializing parameters in ``parameters``. The default value - is None, at this time :ref:`api_fluid_default_startup_program` will be used. + is None, at this time :ref:`api_base_default_startup_program` will be used. parameters (list, optional): List of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. @@ -1425,7 +1425,7 @@ def append_regularization_ops( ): repeate_regularizer = True logging.info( - "If regularizer of a Parameter has been set by 'fluid.ParamAttr' or 'fluid.WeightNormParamAttr' already. " + "If regularizer of a Parameter has been set by 'base.ParamAttr' or 'base.WeightNormParamAttr' already. " "The Regularization[%s] in Optimizer will not take effect, and it will only be applied to other Parameters!" % regularization.__str__() ) @@ -1503,9 +1503,9 @@ def minimize( Args: loss (Tensor): A ``Tensor`` containing the value to minimize. - startup_program (Program, optional): :ref:`api_fluid_Program` for + startup_program (Program, optional): :ref:`api_base_Program` for initializing parameters in ``parameters``. The default value - is None, at this time :ref:`api_fluid_default_startup_program` will be used. + is None, at this time :ref:`api_base_default_startup_program` will be used. parameters (list, optional): List of ``Tensor`` or ``Tensor.name`` to update to minimize ``loss``. The default value is None, at this time all parameters will be updated. @@ -1603,7 +1603,7 @@ def step(self): >>> adam.step() >>> adam.clear_grad() """ - if paddle.fluid.dygraph.base.in_declarative_mode(): + if paddle.base.dygraph.base.in_declarative_mode(): self._declarative_step() return diff --git a/python/paddle/optimizer/rmsprop.py b/python/paddle/optimizer/rmsprop.py index 3bab1f25f00f2..13537b7683387 100644 --- a/python/paddle/optimizer/rmsprop.py +++ b/python/paddle/optimizer/rmsprop.py @@ -16,8 +16,8 @@ from paddle import _C_ops -from ..fluid import framework -from ..fluid.framework import in_dygraph_mode +from ..base import framework +from ..base.framework import in_dygraph_mode from .optimizer import Optimizer __all__ = [] @@ -91,15 +91,15 @@ class RMSProp(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. It can be a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, the regularization setting here in optimizer will be ignored for this parameter. Otherwise, the regularization setting here in optimizer will take effect. Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): This parameter is used by developers to print debugging information. For details, please refer to :ref:`api_guide_Name`. Default is None. diff --git a/python/paddle/optimizer/sgd.py b/python/paddle/optimizer/sgd.py index 29de80b59f20d..19851c7386821 100644 --- a/python/paddle/optimizer/sgd.py +++ b/python/paddle/optimizer/sgd.py @@ -16,9 +16,9 @@ from paddle import _C_ops -from ..fluid import framework -from ..fluid.dygraph import no_grad -from ..fluid.framework import in_dygraph_mode +from ..base import framework +from ..base.dygraph import no_grad +from ..base.framework import in_dygraph_mode from .optimizer import Optimizer __all__ = [] @@ -40,15 +40,15 @@ class SGD(Optimizer): The default value is None in static graph mode, at this time all parameters will be updated. weight_decay (float|WeightDecayRegularizer, optional): The strategy of regularization. \ It can be a float value as coeff of L2 regularization or \ - :ref:`api_fluid_regularizer_L1Decay`, :ref:`api_fluid_regularizer_L2Decay`. - If a parameter has set regularizer using :ref:`api_fluid_ParamAttr` already, \ + :ref:`api_base_regularizer_L1Decay`, :ref:`api_base_regularizer_L2Decay`. + If a parameter has set regularizer using :ref:`api_base_ParamAttr` already, \ the regularization setting here in optimizer will be ignored for this parameter. \ Otherwise, the regularization setting here in optimizer will take effect. \ Default None, meaning there is no regularization. grad_clip (GradientClipBase, optional): Gradient clipping strategy, it's an instance of some derived class of ``GradientClipBase`` . There are three clipping strategies - ( :ref:`api_fluid_clip_GradientClipByGlobalNorm` , :ref:`api_fluid_clip_GradientClipByNorm` , - :ref:`api_fluid_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. + ( :ref:`api_base_clip_GradientClipByGlobalNorm` , :ref:`api_base_clip_GradientClipByNorm` , + :ref:`api_base_clip_GradientClipByValue` ). Default None, meaning there is no gradient clipping. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` . diff --git a/python/paddle/profiler/profiler.py b/python/paddle/profiler/profiler.py index 87b63336d04a7..ff82fb912da4f 100644 --- a/python/paddle/profiler/profiler.py +++ b/python/paddle/profiler/profiler.py @@ -22,7 +22,7 @@ from warnings import warn import paddle -from paddle.fluid.core import ( +from paddle.base.core import ( ProfilerOptions, TracerEventType, _Profiler, diff --git a/python/paddle/profiler/profiler_statistic.py b/python/paddle/profiler/profiler_statistic.py index 1a22f9b08f80c..23c38d804f1a1 100755 --- a/python/paddle/profiler/profiler_statistic.py +++ b/python/paddle/profiler/profiler_statistic.py @@ -15,7 +15,7 @@ import re from enum import Enum -from paddle.fluid.core import TracerEventType, TracerMemEventType +from paddle.base.core import TracerEventType, TracerMemEventType from paddle.utils.flops import flops from .statistic_helper import ( diff --git a/python/paddle/profiler/utils.py b/python/paddle/profiler/utils.py index 851d34d5b7aa5..62e57b515c520 100644 --- a/python/paddle/profiler/utils.py +++ b/python/paddle/profiler/utils.py @@ -18,8 +18,8 @@ from typing import Any from warnings import warn -from paddle.fluid import core -from paddle.fluid.core import TracerEventType, _RecordEvent +from paddle.base import core +from paddle.base.core import TracerEventType, _RecordEvent _is_profiler_used = False _has_optimizer_wrapped = False diff --git a/python/paddle/quantization/imperative/qat.py b/python/paddle/quantization/imperative/qat.py index f261f4cabe42a..871b67d51c7ec 100644 --- a/python/paddle/quantization/imperative/qat.py +++ b/python/paddle/quantization/imperative/qat.py @@ -15,7 +15,7 @@ import os import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.nn.quant import quant_layers diff --git a/python/paddle/quantization/quanters/abs_max.py b/python/paddle/quantization/quanters/abs_max.py index 455f918643425..a8f3792463edb 100644 --- a/python/paddle/quantization/quanters/abs_max.py +++ b/python/paddle/quantization/quanters/abs_max.py @@ -14,8 +14,8 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.framework import _create_tensor +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.framework import _create_tensor from paddle.framework import ParamAttr, core from paddle.nn.initializer import Constant from paddle.utils import unique_name diff --git a/python/paddle/reader/decorator.py b/python/paddle/reader/decorator.py index fba81b96f6355..cd05182f5a7f8 100644 --- a/python/paddle/reader/decorator.py +++ b/python/paddle/reader/decorator.py @@ -22,7 +22,7 @@ from queue import Queue from threading import Thread -from paddle.fluid.reader import QUEUE_GET_TIMEOUT +from paddle.base.reader import QUEUE_GET_TIMEOUT __all__ = [] @@ -67,7 +67,7 @@ def cache(reader): ... yield i ... >>> # All data is cached into memory - >>> cached_reader = paddle.fluid.io.cache(reader) + >>> cached_reader = paddle.base.io.cache(reader) >>> for i in cached_reader(): ... print(i) @@ -126,7 +126,7 @@ def reader(): def shuffle(reader, buf_size): """ - paddle.fluid.io.shuffle ( :ref:`api_fluid_io_shuffle` ) is recommended to use, + paddle.base.io.shuffle ( :ref:`api_base_io_shuffle` ) is recommended to use, and paddle.reader.shuffle is an alias. This API creates a decorated reader that outputs the shuffled data. @@ -179,8 +179,8 @@ def chain(*readers): the format of the outputs. **Note**: - ``paddle.reader.chain`` is the alias of ``paddle.fluid.io.chain``, and - ``paddle.fluid.io.chain`` is recommended to use. + ``paddle.reader.chain`` is the alias of ``paddle.base.io.chain``, and + ``paddle.base.io.chain`` is recommended to use. For example, if three input readers' outputs are as follows: [0, 0, 0], @@ -359,7 +359,7 @@ def data_reader(): def firstn(reader, n): """ - paddle.fluid.io.firstn ( :ref:`api_fluid_io_firstn` ) is recommended to use, + paddle.base.io.firstn ( :ref:`api_base_io_firstn` ) is recommended to use, and paddle.reader.firstn is an alias. This API creates a decorated reader, and limits the max number of @@ -553,23 +553,23 @@ def multiprocess_reader(readers, use_pipe=True, queue_size=1000): ... # generate sample input files ... fake_input_files() ... - ... with fluid.program_guard(fluid.Program(), fluid.Program()): - ... place = fluid.CPUPlace() + ... with base.program_guard(base.Program(), base.Program()): + ... place = base.CPUPlace() ... # the 1st 2 is batch size ... ... image = paddle.static.data(name='image', dtype='int64', shape=[2, 1, 2]) ... paddle.static.Print(image) ... # print detailed tensor info of image variable ... - ... reader = fluid.io.PyReader(feed_list=[image], capacity=2) + ... reader = base.io.PyReader(feed_list=[image], capacity=2) ... ... decorated_reader = paddle.reader.multiprocess_reader( ... [generate_reader(sample_files[0]), generate_reader(sample_files[1])], False) ... ... reader.decorate_sample_generator(decorated_reader, batch_size=2, places=[place]) ... - ... exe = fluid.Executor(place) - ... exe.run(fluid.default_startup_program()) + ... exe = base.Executor(place) + ... exe.run(base.default_startup_program()) ... ... for data in reader(): ... res = exe.run(feed=data, fetch_list=[image]) diff --git a/python/paddle/regularizer.py b/python/paddle/regularizer.py index 501a06e32f89e..dabf365e52681 100644 --- a/python/paddle/regularizer.py +++ b/python/paddle/regularizer.py @@ -14,8 +14,8 @@ from paddle import _C_ops -from paddle.fluid import framework -from paddle.fluid.framework import in_dygraph_mode +from paddle.base import framework +from paddle.base.framework import in_dygraph_mode __all__ = ['L1Decay', 'L2Decay'] diff --git a/python/paddle/signal.py b/python/paddle/signal.py index d1dc910f043ea..a6aa6f112d3dd 100644 --- a/python/paddle/signal.py +++ b/python/paddle/signal.py @@ -16,9 +16,9 @@ from paddle import _C_ops from paddle.framework import in_dynamic_mode +from .base.data_feeder import check_variable_and_dtype +from .base.layer_helper import LayerHelper from .fft import fft_c2c, fft_c2r, fft_r2c -from .fluid.data_feeder import check_variable_and_dtype -from .fluid.layer_helper import LayerHelper from .tensor.attribute import is_complex __all__ = [ diff --git a/python/paddle/sparse/binary.py b/python/paddle/sparse/binary.py index 9de436190972a..55efa6b4210cf 100644 --- a/python/paddle/sparse/binary.py +++ b/python/paddle/sparse/binary.py @@ -13,8 +13,8 @@ # limitations under the License. from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.framework import core, dygraph_only -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.framework import core, dygraph_only +from paddle.base.layer_helper import LayerHelper from .unary import cast diff --git a/python/paddle/sparse/creation.py b/python/paddle/sparse/creation.py index 62a83aab39834..4f8b2a17ce15b 100644 --- a/python/paddle/sparse/creation.py +++ b/python/paddle/sparse/creation.py @@ -16,14 +16,14 @@ import paddle from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.framework import ( +from paddle.base.data_feeder import convert_dtype +from paddle.base.framework import ( _current_expected_place, _get_paddle_place, core, dygraph_only, ) -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.tensor import max, to_tensor __all__ = [ diff --git a/python/paddle/sparse/multiary.py b/python/paddle/sparse/multiary.py index 8e3179f0bdcc0..2aa25e8473ba9 100644 --- a/python/paddle/sparse/multiary.py +++ b/python/paddle/sparse/multiary.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid.framework import dygraph_only +from paddle.base.framework import dygraph_only __all__ = [] diff --git a/python/paddle/sparse/nn/functional/activation.py b/python/paddle/sparse/nn/functional/activation.py index db8e5891964cd..966ea27b1368d 100644 --- a/python/paddle/sparse/nn/functional/activation.py +++ b/python/paddle/sparse/nn/functional/activation.py @@ -15,8 +15,8 @@ __all__ = [] from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.framework import dygraph_only -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.framework import dygraph_only +from paddle.base.layer_helper import LayerHelper def relu(x, name=None): diff --git a/python/paddle/sparse/nn/functional/conv.py b/python/paddle/sparse/nn/functional/conv.py index cbe56a88067a4..c3f51421408bf 100644 --- a/python/paddle/sparse/nn/functional/conv.py +++ b/python/paddle/sparse/nn/functional/conv.py @@ -15,7 +15,7 @@ __all__ = [] from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.nn.functional.conv import _update_padding_nd from paddle.utils import convert_to_list diff --git a/python/paddle/sparse/nn/functional/transformer.py b/python/paddle/sparse/nn/functional/transformer.py index bed15cd42f342..ff75ec9028df0 100644 --- a/python/paddle/sparse/nn/functional/transformer.py +++ b/python/paddle/sparse/nn/functional/transformer.py @@ -15,7 +15,7 @@ __all__ = [] from paddle import _C_ops -from paddle.fluid.framework import dygraph_only +from paddle.base.framework import dygraph_only @dygraph_only diff --git a/python/paddle/sparse/nn/layer/norm.py b/python/paddle/sparse/nn/layer/norm.py index dc8c2713f45a4..ebb4b68930ece 100644 --- a/python/paddle/sparse/nn/layer/norm.py +++ b/python/paddle/sparse/nn/layer/norm.py @@ -16,7 +16,7 @@ import paddle from paddle import _C_ops, in_dynamic_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.framework import no_grad from paddle.nn.layer.norm import _BatchNormBase diff --git a/python/paddle/sparse/unary.py b/python/paddle/sparse/unary.py index 8b798bb3bd03c..b97a588af1bbe 100644 --- a/python/paddle/sparse/unary.py +++ b/python/paddle/sparse/unary.py @@ -16,13 +16,9 @@ import paddle from paddle import _C_ops, in_dynamic_mode +from paddle.base.data_feeder import check_type, check_variable_and_dtype +from paddle.base.framework import convert_np_dtype_to_dtype_, core, dygraph_only from paddle.common_ops_import import Variable -from paddle.fluid.data_feeder import check_type, check_variable_and_dtype -from paddle.fluid.framework import ( - convert_np_dtype_to_dtype_, - core, - dygraph_only, -) from paddle.framework import LayerHelper __all__ = [] diff --git a/python/paddle/static/__init__.py b/python/paddle/static/__init__.py index 361a6fc4ee3f5..5cf44a3efc7c0 100644 --- a/python/paddle/static/__init__.py +++ b/python/paddle/static/__init__.py @@ -37,7 +37,7 @@ from .io import load # noqa: F401 from .io import load_program_state # noqa: F401 from .io import set_program_state # noqa: F401 -from ..fluid import Scope # noqa: F401 +from ..base import Scope # noqa: F401 from .input import data # noqa: F401 from .input import InputSpec # noqa: F401 from .input import setitem # noqa: F401 @@ -45,39 +45,39 @@ from ..tensor.creation import create_parameter # noqa: F401 from ..tensor.creation import create_global_var # noqa: F401 -from ..fluid.executor import Executor # noqa: F401 -from ..fluid.executor import global_scope # noqa: F401 -from ..fluid.executor import scope_guard # noqa: F401 -from ..fluid.backward import append_backward # noqa: F401 -from ..fluid.backward import gradients # noqa: F401 -from ..fluid.compiler import BuildStrategy # noqa: F401 -from ..fluid.compiler import CompiledProgram # noqa: F401 -from ..fluid.compiler import IpuCompiledProgram # noqa: F401 -from ..fluid.compiler import IpuStrategy # noqa: F401 -from ..fluid.compiler import ExecutionStrategy # noqa: F401 -from ..fluid.framework import default_main_program # noqa: F401 -from ..fluid.framework import default_startup_program # noqa: F401 -from ..fluid.framework import device_guard # noqa: F401 +from ..base.executor import Executor # noqa: F401 +from ..base.executor import global_scope # noqa: F401 +from ..base.executor import scope_guard # noqa: F401 +from ..base.backward import append_backward # noqa: F401 +from ..base.backward import gradients # noqa: F401 +from ..base.compiler import BuildStrategy # noqa: F401 +from ..base.compiler import CompiledProgram # noqa: F401 +from ..base.compiler import IpuCompiledProgram # noqa: F401 +from ..base.compiler import IpuStrategy # noqa: F401 +from ..base.compiler import ExecutionStrategy # noqa: F401 +from ..base.framework import default_main_program # noqa: F401 +from ..base.framework import default_startup_program # noqa: F401 +from ..base.framework import device_guard # noqa: F401 -from ..fluid.framework import name_scope # noqa: F401 -from ..fluid.framework import cpu_places # noqa: F401 -from ..fluid.framework import cuda_places # noqa: F401 -from ..fluid.framework import xpu_places # noqa: F401 -from ..fluid.framework import Variable # noqa: F401 -from ..fluid.framework import Operator # noqa: F401 -from ..fluid.framework import Parameter # noqa: F401 -from ..fluid.framework import ipu_shard_guard # noqa: F401 -from ..fluid.framework import set_ipu_shard # noqa: F401 +from ..base.framework import name_scope # noqa: F401 +from ..base.framework import cpu_places # noqa: F401 +from ..base.framework import cuda_places # noqa: F401 +from ..base.framework import xpu_places # noqa: F401 +from ..base.framework import Variable # noqa: F401 +from ..base.framework import Operator # noqa: F401 +from ..base.framework import Parameter # noqa: F401 +from ..base.framework import ipu_shard_guard # noqa: F401 +from ..base.framework import set_ipu_shard # noqa: F401 from .nn.control_flow import Print # noqa: F401 -from ..fluid.param_attr import WeightNormParamAttr # noqa: F401 +from ..base.param_attr import WeightNormParamAttr # noqa: F401 from .nn.metric import auc # noqa: F401 from .nn.metric import accuracy # noqa: F401 from .nn.metric import ctr_metric_bundle # noqa: F401 -from ..fluid.framework import program_guard # noqa: F401 -from ..fluid.framework import Program # noqa: F401 +from ..base.framework import program_guard # noqa: F401 +from ..base.framework import Program # noqa: F401 __all__ = [ # noqa 'append_backward', diff --git a/python/paddle/static/amp/amp_nn.py b/python/paddle/static/amp/amp_nn.py index 6e56f253fbd4b..304ad8d7d6c40 100644 --- a/python/paddle/static/amp/amp_nn.py +++ b/python/paddle/static/amp/amp_nn.py @@ -13,10 +13,10 @@ # limitations under the License. from paddle import _C_ops -from paddle.fluid import core -from paddle.fluid.data_feeder import check_type, check_variable_and_dtype -from paddle.fluid.framework import Variable, in_dygraph_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core +from paddle.base.data_feeder import check_type, check_variable_and_dtype +from paddle.base.framework import Variable, in_dygraph_mode +from paddle.base.layer_helper import LayerHelper def check_finite_and_unscale(x, scale, name=None, float_status=None): diff --git a/python/paddle/static/amp/bf16/amp_lists.py b/python/paddle/static/amp/bf16/amp_lists.py index 0d6d3997aebd5..4020b4f5261a1 100644 --- a/python/paddle/static/amp/bf16/amp_lists.py +++ b/python/paddle/static/amp/bf16/amp_lists.py @@ -15,7 +15,7 @@ import copy from paddle.amp.amp_lists import BF16_WHITE_LIST -from paddle.fluid import core +from paddle.base import core from ..fp16_lists import black_list as black_list_fp16 from ..fp16_lists import gray_list as gray_list_fp16 diff --git a/python/paddle/static/amp/bf16/amp_utils.py b/python/paddle/static/amp/bf16/amp_utils.py index 55ca4a5d06c86..071328435e939 100644 --- a/python/paddle/static/amp/bf16/amp_utils.py +++ b/python/paddle/static/amp/bf16/amp_utils.py @@ -19,9 +19,9 @@ import numpy as np -from paddle.fluid import core, framework, global_scope -from paddle.fluid.log_helper import get_logger -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base import core, framework, global_scope +from paddle.base.log_helper import get_logger +from paddle.base.wrapped_decorator import signature_safe_contextmanager from ..fp16_utils import ( _rename_arg, @@ -483,9 +483,9 @@ def cast_parameters_to_bf16(place, program, scope=None, to_bf16_var_names=None): Traverse all parameters in the whole model and set them to the BF16 data type. Whereas, this function will keep parameters of batchnorms in FP32. Args: - place(fluid.CPUPlace|fluid.CUDAPlace): `place` is used to restore the BF16 weight tensors. + place(base.CPUPlace|base.CUDAPlace): `place` is used to restore the BF16 weight tensors. program (Program): The used program. - scope(fluid.Scope, optional): `scope` is used to get the FP32 weight tensor values. + scope(base.Scope, optional): `scope` is used to get the FP32 weight tensor values. Default is None. to_bf16_var_names(set|list, optional): The data types of vars in `to_bf16_var_names` will be set to BF16. Usually, it is the returned diff --git a/python/paddle/static/amp/bf16/decorator.py b/python/paddle/static/amp/bf16/decorator.py index 47b19b697d1ec..74e896bc7f790 100644 --- a/python/paddle/static/amp/bf16/decorator.py +++ b/python/paddle/static/amp/bf16/decorator.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle.fluid import core, default_main_program, program_guard, unique_name +from paddle.base import core, default_main_program, program_guard, unique_name from .amp_lists import AutoMixedPrecisionListsBF16 from .amp_utils import ( diff --git a/python/paddle/static/amp/debugging.py b/python/paddle/static/amp/debugging.py index 38e3764203ab1..9dcef221fbfc0 100644 --- a/python/paddle/static/amp/debugging.py +++ b/python/paddle/static/amp/debugging.py @@ -16,7 +16,7 @@ import logging import paddle -from paddle.fluid.log_helper import get_logger +from paddle.base.log_helper import get_logger _logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' @@ -35,11 +35,11 @@ def update(self, dtype): if dtype is None: self.other_calls = self.other_calls + 1 else: - if dtype == paddle.fluid.core.VarDesc.VarType.FP32: + if dtype == paddle.base.core.VarDesc.VarType.FP32: self.fp32_calls = self.fp32_calls + 1 - elif dtype == paddle.fluid.core.VarDesc.VarType.FP16: + elif dtype == paddle.base.core.VarDesc.VarType.FP16: self.fp16_calls = self.fp16_calls + 1 - elif dtype == paddle.fluid.core.VarDesc.VarType.BF16: + elif dtype == paddle.base.core.VarDesc.VarType.BF16: self.bf16_calls = self.bf16_calls + 1 else: self.other_calls = self.other_calls + 1 @@ -61,10 +61,10 @@ def convert_to_list(self): def _is_floating_point(dtype): if dtype in [ - paddle.fluid.core.VarDesc.VarType.FP64, - paddle.fluid.core.VarDesc.VarType.FP32, - paddle.fluid.core.VarDesc.VarType.FP16, - paddle.fluid.core.VarDesc.VarType.BF16, + paddle.base.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP32, + paddle.base.core.VarDesc.VarType.FP16, + paddle.base.core.VarDesc.VarType.BF16, ]: return True else: diff --git a/python/paddle/static/amp/decorator.py b/python/paddle/static/amp/decorator.py index 440048f70209b..0d51bdf1a5245 100644 --- a/python/paddle/static/amp/decorator.py +++ b/python/paddle/static/amp/decorator.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle.fluid import ( +from paddle.base import ( core, default_main_program, default_startup_program, @@ -415,7 +415,7 @@ def _append_cast_to_master_grad_op(self, param_grads): ] params_master_grads = [] - assert isinstance(target_block, paddle.fluid.framework.Block) + assert isinstance(target_block, paddle.base.framework.Block) # create for p, g in param_grads: if g.name not in self._optimizer._master_grads.keys(): diff --git a/python/paddle/static/amp/fp16_lists.py b/python/paddle/static/amp/fp16_lists.py index 3a0f407198797..3023628e9a389 100644 --- a/python/paddle/static/amp/fp16_lists.py +++ b/python/paddle/static/amp/fp16_lists.py @@ -21,8 +21,8 @@ FP16_BLACK_LIST, FP16_WHITE_LIST, ) -from paddle.fluid import core -from paddle.fluid.log_helper import get_logger +from paddle.base import core +from paddle.base.log_helper import get_logger _logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' diff --git a/python/paddle/static/amp/fp16_utils.py b/python/paddle/static/amp/fp16_utils.py index b5c78c69f6679..5119425348b42 100644 --- a/python/paddle/static/amp/fp16_utils.py +++ b/python/paddle/static/amp/fp16_utils.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle.fluid import core, framework, global_scope -from paddle.fluid.log_helper import get_logger -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base import core, framework, global_scope +from paddle.base.log_helper import get_logger +from paddle.base.wrapped_decorator import signature_safe_contextmanager from .fp16_lists import ( AutoMixedPrecisionLists, @@ -799,9 +799,9 @@ def cast_parameters_to_fp16( Traverse all parameters in the whole model and set them to the FP16 data type. Whereas, this function will keep parameters of batchnorms in FP32. Args: - place(fluid.CPUPlace|fluid.CUDAPlace): `place` is used to restore the FP16 weight tensors. + place(base.CPUPlace|base.CUDAPlace): `place` is used to restore the FP16 weight tensors. program (Program): The used program. - scope(fluid.Scope, optional): `scope` is used to get the FP32 weight tensor values. + scope(base.Scope, optional): `scope` is used to get the FP32 weight tensor values. Default is None. to_fp16_var_names(set|list, optional): The data types of vars in `to_fp16_var_names` will be set to FP16. Usually, it is the returned diff --git a/python/paddle/static/amp/function_overload.py b/python/paddle/static/amp/function_overload.py index 8139401c21db1..0767873dd6d99 100644 --- a/python/paddle/static/amp/function_overload.py +++ b/python/paddle/static/amp/function_overload.py @@ -19,7 +19,7 @@ import logging from enum import Enum -from paddle.fluid.log_helper import get_logger +from paddle.base.log_helper import get_logger _logger = get_logger( __name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s' diff --git a/python/paddle/static/input.py b/python/paddle/static/input.py index 1382ff591b7f9..d31d86f729199 100644 --- a/python/paddle/static/input.py +++ b/python/paddle/static/input.py @@ -15,12 +15,12 @@ import os import paddle -from paddle.fluid import Variable, core -from paddle.fluid.data_feeder import check_type -from paddle.fluid.framework import convert_np_dtype_to_dtype_, static_only -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import Variable, core +from paddle.base.data_feeder import check_type +from paddle.base.framework import convert_np_dtype_to_dtype_, static_only +from paddle.base.layer_helper import LayerHelper -from ..fluid.variable_index import _setitem_impl_, _setitem_static +from ..base.variable_index import _setitem_impl_, _setitem_static __all__ = [] diff --git a/python/paddle/static/io.py b/python/paddle/static/io.py index 1b6f65e28e5ed..27682416f8c1b 100644 --- a/python/paddle/static/io.py +++ b/python/paddle/static/io.py @@ -23,7 +23,7 @@ import numpy as np import paddle -from paddle.fluid import ( +from paddle.base import ( CompiledProgram, Program, Variable, @@ -32,9 +32,9 @@ program_guard, unique_name, ) -from paddle.fluid.executor import Executor, global_scope -from paddle.fluid.framework import Parameter, dygraph_not_support, static_only -from paddle.fluid.log_helper import get_logger +from paddle.base.executor import Executor, global_scope +from paddle.base.framework import Parameter, dygraph_not_support, static_only +from paddle.base.log_helper import get_logger from paddle.framework.io_utils import ( _clone_var_in_block_, _load_program_scope, @@ -110,7 +110,7 @@ def _get_valid_program(program=None): ) if not isinstance(program, Program): raise TypeError( - "The type of input program is invalid, expected type is fluid.Program, but received %s" + "The type of input program is invalid, expected type is base.Program, but received %s" % type(program) ) return program @@ -228,7 +228,7 @@ def normalize_program(program, feed_vars, fetch_vars, **kwargs): """ if not isinstance(program, Program): raise TypeError( - "program type must be `fluid.Program`, but received `%s`" + "program type must be `base.Program`, but received `%s`" % type(program) ) if not isinstance(feed_vars, list): @@ -700,7 +700,7 @@ def deserialize_persistables(program, data, executor): """ if not isinstance(program, Program): raise TypeError( - "program type must be `fluid.Program`, but received `%s`" + "program type must be `base.Program`, but received `%s`" % type(program) ) # load params to a tmp program @@ -747,7 +747,7 @@ def deserialize_persistables(program, data, executor): for var in check_vars: if not isinstance(var, Parameter): continue - var_tmp = paddle.fluid.global_scope().find_var(var.name) + var_tmp = paddle.base.global_scope().find_var(var.name) assert var_tmp is not None, "can't not find var: " + var.name new_shape = (np.array(var_tmp.get_tensor())).shape assert var.name in origin_shape_map, var.name + " MUST in var list." @@ -1228,7 +1228,7 @@ def load_vars( main_program = default_main_program() if not isinstance(main_program, Program): raise TypeError( - "The type of input main_program is invalid, expected type is fluid.Program, but received %s" + "The type of input main_program is invalid, expected type is base.Program, but received %s" % type(main_program) ) @@ -1248,7 +1248,7 @@ def load_vars( if not isinstance(main_program, Program): raise TypeError( - "The type of input main_program is invalid, expected type is fluid.Program, but received %s" + "The type of input main_program is invalid, expected type is base.Program, but received %s" % type(main_program) ) @@ -1373,7 +1373,7 @@ def load_vars( for each_var in check_vars: if not isinstance(each_var, Parameter): continue - var_temp = paddle.fluid.global_scope().find_var(each_var.name) + var_temp = paddle.base.global_scope().find_var(each_var.name) assert var_temp is not None, "can't not find var: " + each_var.name new_shape = (np.array(var_temp.get_tensor())).shape assert each_var.name in orig_para_shape, ( @@ -1548,7 +1548,7 @@ def load(program, model_path, executor=None, var_list=None): parameter_file_name = model_prefix + ".pdparams" if not os.path.exists(parameter_file_name): - # model file save by fluid.save not found, try to load model file saved with + # model file save by base.save not found, try to load model file saved with # [save_vars, save_params, save_persistables] _logger.debug( "{} not found, try to load model file saved with [ save_params, save_persistables, save_vars ]".format( @@ -1641,30 +1641,30 @@ def set_var(var, ndarray): t = global_scope().find_var(var.name).get_tensor() p = t._place() if p.is_cpu_place(): - place = paddle.fluid.CPUPlace() + place = paddle.base.CPUPlace() elif p.is_cuda_pinned_place(): - place = paddle.fluid.CUDAPinnedPlace() + place = paddle.base.CUDAPinnedPlace() elif p.is_xpu_place(): - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(t._place()) - place = paddle.fluid.XPUPlace(p.xpu_device_id()) + place = paddle.base.XPUPlace(p.xpu_device_id()) elif p.is_custom_place(): - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(t._place()) - place = paddle.fluid.CustomPlace( + place = paddle.base.CustomPlace( paddle.device.get_device().split(':')[0], p.custom_device_id() ) else: - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(t._place()) - place = paddle.fluid.CUDAPlace(p.gpu_device_id()) + place = paddle.base.CUDAPlace(p.gpu_device_id()) t.set(ndarray, place) parameter_list = list(filter(is_parameter, program.list_vars())) if executor: - paddle.fluid.core._create_loaded_parameter( + paddle.base.core._create_loaded_parameter( parameter_list, global_scope(), executor._default_executor ) with open(parameter_file_name, 'rb') as f: @@ -1693,7 +1693,7 @@ def set_var(var, ndarray): ), f"Optimizer file [{opt_file_name}] not exits" if executor: - paddle.fluid.core._create_loaded_parameter( + paddle.base.core._create_loaded_parameter( optimizer_var_list, global_scope(), executor._default_executor ) @@ -1750,7 +1750,7 @@ def set_program_state(program, state_dict): used_para_list = {} for para in parameter_list: - var_temp = paddle.fluid.global_scope().find_var(para.name) + var_temp = paddle.base.global_scope().find_var(para.name) assert ( var_temp is not None ), "Variable [ {} ] Not found, Please make sure run startup program".format( @@ -1778,17 +1778,17 @@ def set_program_state(program, state_dict): # assert ten_place.is_gpu_place() or ten_place.is_cpu_place(), \ # "Place not support, only support CPUPlace and GPUPlace, now is {}".format(str(ten_place)) - py_place = paddle.fluid.CPUPlace() + py_place = paddle.base.CPUPlace() if ten_place.is_cuda_pinned_place(): - place = paddle.fluid.CUDAPinnedPlace() + place = paddle.base.CUDAPinnedPlace() elif ten_place.is_gpu_place(): - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(ten_place) - py_place = paddle.fluid.CUDAPlace(p.gpu_device_id()) + py_place = paddle.base.CUDAPlace(p.gpu_device_id()) elif ten_place.is_xpu_place(): - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(ten_place) - py_place = paddle.fluid.XPUPlace(p.xpu_device_id()) + py_place = paddle.base.XPUPlace(p.xpu_device_id()) ten.set(new_para_np, py_place) @@ -1874,7 +1874,7 @@ def load_program_state(model_path, var_list=None): parameter_file_name = model_prefix + ".pdparams" if not os.path.exists(parameter_file_name): - # model file saved with fluid.save is not found, try to load model file saved with + # model file saved with base.save is not found, try to load model file saved with # [save_vars, save_params, save_persistables] _logger.debug( "{} not found, try to load model file saved with [ save_params, save_persistables, save_vars ]".format( @@ -1941,8 +1941,8 @@ def _load_vars_with_try_catch( warnings.warn(error_str % filenames, RuntimeWarning) return False - place = paddle.fluid.CPUPlace() - exe = paddle.fluid.Executor(place) + place = paddle.base.CPUPlace() + exe = paddle.base.Executor(place) loaded_var_list = [] @@ -1982,7 +1982,7 @@ def _load_vars_with_try_catch( res_dict = {} for var in loaded_var_list: res_dict[var.name] = np.asarray( - paddle.fluid.global_scope().find_var(var.name).get_tensor() + paddle.base.global_scope().find_var(var.name).get_tensor() ) return res_dict diff --git a/python/paddle/static/nn/common.py b/python/paddle/static/nn/common.py index 5994742bffbfe..a5743cc2bce52 100644 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -19,14 +19,9 @@ import numpy as np import paddle -from paddle.common_ops_import import ( - LayerHelper, - check_type, - check_variable_and_dtype, -) -from paddle.fluid import core, unique_name -from paddle.fluid.data_feeder import check_dtype -from paddle.fluid.framework import ( +from paddle.base import core, unique_name +from paddle.base.data_feeder import check_dtype +from paddle.base.framework import ( Program, Variable, default_main_program, @@ -35,9 +30,14 @@ program_guard, static_only, ) -from paddle.fluid.layers.layer_function_generator import templatedoc -from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base.layers.layer_function_generator import templatedoc +from paddle.base.param_attr import ParamAttr +from paddle.base.wrapped_decorator import signature_safe_contextmanager +from paddle.common_ops_import import ( + LayerHelper, + check_type, + check_variable_and_dtype, +) from paddle.nn.initializer import Constant, Normal __all__ = [] @@ -180,7 +180,7 @@ def fc( """ - def fc_fluid( + def fc_base( input, size, num_flatten_dims=1, @@ -236,7 +236,7 @@ def fc_fluid( # add activation return helper.append_activation(pre_activation) - return fc_fluid( + return fc_base( input=x, size=size, num_flatten_dims=num_flatten_dims, @@ -1037,7 +1037,7 @@ def _get_default_param_initializer(): if ( core.is_compiled_with_cuda() - and paddle.fluid.get_flags("FLAGS_conv2d_disable_cudnn")[ + and paddle.base.get_flags("FLAGS_conv2d_disable_cudnn")[ "FLAGS_conv2d_disable_cudnn" ] ): @@ -2566,10 +2566,10 @@ def bilinear_tensor_product( :ref:`api_guide_Name` . Usually name is no need to set and None by default. param_attr (ParamAttr|None): To specify the weight parameter attribute. Default: None, which means the default weight parameter property is - used. See usage for details in :ref:`api_fluid_ParamAttr` . + used. See usage for details in :ref:`api_base_ParamAttr` . bias_attr (ParamAttr|None): To specify the bias parameter attribute. Default: None, which means the default bias parameter property is - used. See usage for details in :ref:`api_fluid_ParamAttr` . + used. See usage for details in :ref:`api_base_ParamAttr` . Returns: Tensor, A 2-D Tensor of shape [batch_size, size]. Data type is the same as input **x**. @@ -2887,7 +2887,7 @@ def batch_norm( *attrs_, ) - return paddle.fluid.dygraph_utils._append_activation_in_dygraph( + return paddle.base.dygraph_utils._append_activation_in_dygraph( batch_norm_out, act=act, use_mkldnn=False ) diff --git a/python/paddle/static/nn/control_flow.py b/python/paddle/static/nn/control_flow.py index 39a95f42a7018..87f9ae321d6f8 100644 --- a/python/paddle/static/nn/control_flow.py +++ b/python/paddle/static/nn/control_flow.py @@ -16,6 +16,9 @@ from functools import partial, reduce import paddle +from paddle.base import core +from paddle.base.backward import _infer_var_data_type_shape_ +from paddle.base.framework import Operator, Program, Variable, static_only from paddle.common_ops_import import ( LayerHelper, check_type, @@ -23,9 +26,6 @@ convert_dtype, in_dygraph_mode, ) -from paddle.fluid import core -from paddle.fluid.backward import _infer_var_data_type_shape_ -from paddle.fluid.framework import Operator, Program, Variable, static_only from paddle.utils import ( assert_same_structure, copy_mutable_vars, @@ -415,8 +415,8 @@ class While: while loop control flow. Repeat while body until cond is False. Note: - A new OP :ref:`api_fluid_layers_while_loop` is highly recommended instead of ``While`` if the shape of parameter ``cond`` is [1]. - OP :ref:`api_fluid_layers_while_loop` is easier to use and is called with less code but does the same thing as ``While`` . + A new OP :ref:`api_base_layers_while_loop` is highly recommended instead of ``While`` if the shape of parameter ``cond`` is [1]. + OP :ref:`api_base_layers_while_loop` is easier to use and is called with less code but does the same thing as ``While`` . Notice: Local variables created in ``While`` are similar to that created in while of C++, and cannot be referenced externally. @@ -595,7 +595,7 @@ def has_shape_diff(x_var, y_var): # input is not generated in While sub block and modified by in-place and only # belong to inplace ops in constructing program process, because in-place pass # is only available in Graph level. - with paddle.fluid.framework._stride_in_no_check_dy2st_diff(): + with paddle.base.framework._stride_in_no_check_dy2st_diff(): paddle.assign(input, output) @@ -1213,8 +1213,8 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None): return false_fn() return None - check_variable_and_dtype(pred, "pred", ['bool'], "fluid.layers.cond") - check_type(name, "name", (str, type(None)), "fluid.layers.cond") + check_variable_and_dtype(pred, "pred", ['bool'], "base.layers.cond") + check_type(name, "name", (str, type(None)), "base.layers.cond") helper = LayerHelper('cond', **locals()) true_output = None false_output = None @@ -1758,7 +1758,7 @@ def case(self, condition): condition, 'condition', ['bool'], - 'the member function case of fluid.layers.Switch', + 'the member function case of base.layers.Switch', ) if len(self.pre_not_conditions) == 0: diff --git a/python/paddle/static/nn/loss.py b/python/paddle/static/nn/loss.py index cbc099d963a23..7f1ef25622612 100644 --- a/python/paddle/static/nn/loss.py +++ b/python/paddle/static/nn/loss.py @@ -14,15 +14,15 @@ import numpy as np -from paddle.fluid.framework import static_only +from paddle.base.framework import static_only # TODO: define loss functions of neural network -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.layers.layer_function_generator import templatedoc -from paddle.fluid.param_attr import ParamAttr +from paddle.base.layer_helper import LayerHelper +from paddle.base.layers.layer_function_generator import templatedoc +from paddle.base.param_attr import ParamAttr from paddle.nn.initializer import Assign -from ...fluid.data_feeder import check_variable_and_dtype +from ...base.data_feeder import check_variable_and_dtype __all__ = [] @@ -62,10 +62,10 @@ def nce( sample is 1.0. param_attr (ParamAttr|None): To specify the weight parameter attribute. Default: None, which means the default weight parameter property is - used. See usage for details in :ref:`api_fluid_ParamAttr` . + used. See usage for details in :ref:`api_base_ParamAttr` . bias_attr (ParamAttr|None): To specify the bias parameter attribute. Default: None, which means the default bias parameter property is - used. See usage for details in :ref:`api_fluid_ParamAttr` . + used. See usage for details in :ref:`api_base_ParamAttr` . num_neg_samples (int): ${num_neg_samples_comment}. name(str|None): For detailed information, please refer to :ref:`api_guide_Name` . Usually name is no need to set and None by default. diff --git a/python/paddle/static/nn/metric.py b/python/paddle/static/nn/metric.py index c4fc0a9b4f240..74db729843327 100644 --- a/python/paddle/static/nn/metric.py +++ b/python/paddle/static/nn/metric.py @@ -18,9 +18,9 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.framework import Variable, _create_tensor, in_dygraph_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.framework import Variable, _create_tensor, in_dygraph_mode +from paddle.base.layer_helper import LayerHelper from paddle.nn.initializer import ConstantInitializer __all__ = [] diff --git a/python/paddle/static/nn/sequence_lod.py b/python/paddle/static/nn/sequence_lod.py index f9b9ea355e262..653541d92551c 100644 --- a/python/paddle/static/nn/sequence_lod.py +++ b/python/paddle/static/nn/sequence_lod.py @@ -13,11 +13,11 @@ # limitations under the License. import paddle -from paddle.fluid.core import VarDesc -from paddle.fluid.data_feeder import check_type, check_variable_and_dtype -from paddle.fluid.framework import Variable, in_dygraph_mode -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.layers.layer_function_generator import templatedoc +from paddle.base.core import VarDesc +from paddle.base.data_feeder import check_type, check_variable_and_dtype +from paddle.base.framework import Variable, in_dygraph_mode +from paddle.base.layer_helper import LayerHelper +from paddle.base.layers.layer_function_generator import templatedoc __all__ = [] @@ -38,7 +38,7 @@ def sequence_conv( r""" Note: - Only receives Tensor as input. If your input is Tensor, please use conv2d Op.(fluid.layers.** :ref:`api_fluid_layers_conv2d` ). + Only receives Tensor as input. If your input is Tensor, please use conv2d Op.(base.layers.** :ref:`api_base_layers_conv2d` ). This operator receives input sequences with variable length and other convolutional configuration parameters(num_filters, filter_size) to apply the convolution operation. @@ -108,9 +108,9 @@ def sequence_conv( on both sides of the sequence. If set 0, the length of :math:`filter\_size - 1` data is padded at the end of each input sequence. Default: None. bias_attr (ParamAttr): To specify the bias parameter property. Default: None, which means the - default bias parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr` . + default bias parameter property is used. See usage for details in :ref:`api_base_ParamAttr` . param_attr (ParamAttr): To specify the weight parameter property. Default: None, which means the - default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr` . + default weight parameter property is used. See usage for details in :ref:`api_base_ParamAttr` . act (str): Activation to be applied to the output of this layer, such as tanh, softmax, sigmoid, relu. For more information, please refer to :ref:`api_guide_activations_en` . Default: None. name (str, optional): The default value is None. Normally there is no need for user to set this property. @@ -730,7 +730,7 @@ def sequence_expand(x, y, ref_level=-1, name=None): .. code-block:: python import paddle - from paddle import fluid + from paddle import base paddle.enable_static() import numpy as np @@ -739,11 +739,11 @@ def sequence_expand(x, y, ref_level=-1, name=None): dtype='float32', lod_level=1) out = paddle.static.nn.sequence_expand(x=x, y=y, ref_level=0) - exe = paddle.static.Executor(fluid.CPUPlace()) + exe = paddle.static.Executor(base.CPUPlace()) place = paddle.CPUPlace() np_data = np.array([[1], [2], [3], [4]]).astype('float32') - x_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2]], place) + x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) print(x_lod_tensor) #lod: [[0, 2, 4]] # dim: 4, 1 @@ -752,7 +752,7 @@ def sequence_expand(x, y, ref_level=-1, name=None): # data: [1 2 3 4] np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') - y_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2], [3,3,1,1]], place) + y_lod_tensor = base.create_lod_tensor(np_data, [[2, 2], [3,3,1,1]], place) print(y_lod_tensor) #lod: [[0, 2, 4][0, 3, 6, 7, 8]] # dim: 8, 1 @@ -760,7 +760,7 @@ def sequence_expand(x, y, ref_level=-1, name=None): # dtype: int64_t # data: [0 0 1 1 1 1 1 0] - out_main = exe.run(fluid.default_main_program(), + out_main = exe.run(base.default_main_program(), feed={'x': x_lod_tensor, 'y': y_lod_tensor}, fetch_list=[out], return_numpy=False) print(out_main[0]) @@ -853,7 +853,7 @@ def sequence_expand_as(x, y, name=None): .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base paddle.enable_static() import numpy as np @@ -861,11 +861,11 @@ def sequence_expand_as(x, y, name=None): y = paddle.static.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) out = paddle.static.nn.sequence_expand_as(x=x, y=y) - exe = fluid.Executor(fluid.CPUPlace()) - place = fluid.CPUPlace() + exe = base.Executor(base.CPUPlace()) + place = base.CPUPlace() np_data = np.array([[1], [2], [3], [4]]).astype('float32') - x_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2]], place) + x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) print(x_lod_tensor) #lod: [[0, 2, 4]] # dim: 4, 1 @@ -874,7 +874,7 @@ def sequence_expand_as(x, y, name=None): # data: [1 2 3 4] np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') - y_lod_tensor = fluid.create_lod_tensor(np_data, [[3,3,1,1]], place) + y_lod_tensor = base.create_lod_tensor(np_data, [[3,3,1,1]], place) print(y_lod_tensor) #lod: [[0, 3, 6, 7, 8]] # dim: 8, 1 @@ -882,7 +882,7 @@ def sequence_expand_as(x, y, name=None): # dtype: int64_t # data: [0 0 1 0 1 1 1 0] - out_main = exe.run(fluid.default_main_program(), + out_main = exe.run(base.default_main_program(), feed={'x': x_lod_tensor, 'y': y_lod_tensor}, fetch_list=[out], return_numpy=False) print(out_main[0]) @@ -986,7 +986,7 @@ def sequence_pad(x, pad_value, maxlen=None, name=None): import paddle paddle.enable_static() - import paddle.fluid as fluid + import paddle.base as base import numpy x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) @@ -1072,7 +1072,7 @@ def sequence_unpad(x, length, name=None): import paddle paddle.enable_static() - import paddle.fluid as fluid + import paddle.base as base import numpy # pad data @@ -1418,7 +1418,7 @@ def sequence_reverse(x, name=None): Only supports Tensor as input. It will reverse each sequence for input Tensor. Currently it only supports 1-level Tensor. This operator is very useful when building a - reverse :ref:`api_fluid_layers_DynamicRNN` network. + reverse :ref:`api_base_layers_DynamicRNN` network. .. code-block:: text diff --git a/python/paddle/static/nn/static_pylayer.py b/python/paddle/static/nn/static_pylayer.py index e5fd171d32663..bf6900906b523 100644 --- a/python/paddle/static/nn/static_pylayer.py +++ b/python/paddle/static/nn/static_pylayer.py @@ -13,10 +13,10 @@ # limitations under the License. +from paddle.base import core +from paddle.base.backward import _append_grad_suffix_ +from paddle.base.framework import Variable from paddle.common_ops_import import LayerHelper, check_type, in_dygraph_mode -from paddle.fluid import core -from paddle.fluid.backward import _append_grad_suffix_ -from paddle.fluid.framework import Variable from paddle.utils import flatten, map_structure # NOTE(MarioLulab): Borrowed from `python/paddle/static/nn/control_flow.py` @@ -238,7 +238,7 @@ def backward_fn(dy): ) ) - check_type(name, "name", (str, type(None)), "fluid.layers.static_pylayer") + check_type(name, "name", (str, type(None)), "base.layers.static_pylayer") helper = LayerHelper('static_pylayer', **locals()) copy_to_parent_func = lambda var: copy_var_to_parent_block(var, helper) diff --git a/python/paddle/static/quantization/post_training_quantization.py b/python/paddle/static/quantization/post_training_quantization.py index 61f1cdf6fe5ff..7234fdfb62227 100644 --- a/python/paddle/static/quantization/post_training_quantization.py +++ b/python/paddle/static/quantization/post_training_quantization.py @@ -24,7 +24,7 @@ from .utils import tqdm -from paddle.fluid.framework import IrGraph, _get_var +from paddle.base.framework import IrGraph, _get_var from ... import io, static from ...framework import core diff --git a/python/paddle/static/quantization/quant2_int8_mkldnn_pass.py b/python/paddle/static/quantization/quant2_int8_mkldnn_pass.py index 990f11220927b..cbb46200a6029 100644 --- a/python/paddle/static/quantization/quant2_int8_mkldnn_pass.py +++ b/python/paddle/static/quantization/quant2_int8_mkldnn_pass.py @@ -14,7 +14,7 @@ import numpy as np -from ...fluid.framework import IrGraph +from ...base.framework import IrGraph from ...framework import _get_paddle_place, core OpRole = core.op_proto_and_checker_maker.OpRole diff --git a/python/paddle/static/quantization/quant_int8_mkldnn_pass.py b/python/paddle/static/quantization/quant_int8_mkldnn_pass.py index d11e7d212686b..2d4cc4bfd8364 100644 --- a/python/paddle/static/quantization/quant_int8_mkldnn_pass.py +++ b/python/paddle/static/quantization/quant_int8_mkldnn_pass.py @@ -14,7 +14,7 @@ import numpy as np -from ...fluid.framework import IrGraph +from ...base.framework import IrGraph from ...framework import _get_paddle_place diff --git a/python/paddle/static/quantization/quanter.py b/python/paddle/static/quantization/quanter.py index a5baf899060c4..218f5786724ce 100644 --- a/python/paddle/static/quantization/quanter.py +++ b/python/paddle/static/quantization/quanter.py @@ -20,7 +20,7 @@ import paddle -from ...fluid.framework import IrGraph, core +from ...base.framework import IrGraph, core from ..log_helper import get_logger from .quantization_pass import ( AddQuantDequantPass, diff --git a/python/paddle/static/quantization/quantization_pass.py b/python/paddle/static/quantization/quantization_pass.py index fdb721681f681..4387732362f9a 100644 --- a/python/paddle/static/quantization/quantization_pass.py +++ b/python/paddle/static/quantization/quantization_pass.py @@ -24,7 +24,7 @@ import paddle -from ...fluid.framework import IrGraph, IrNode +from ...base.framework import IrGraph, IrNode from ...framework import _get_paddle_place, core from ...static import Program, data, program_guard, scope_guard from ...utils import unique_name @@ -168,7 +168,7 @@ def __init__( optimizer_func(function): Fuction return a optimizer. When 'is_test' is False and user want to use self-defined quantization function and preprocess function, this function must be set. Default is None. - executor(Fluid.Executor): If user want to use self-defined quantization + executor(base.Executor): If user want to use self-defined quantization function and preprocess function, executor must be set for initialization. Default is None. @@ -179,7 +179,7 @@ def __init__( >>> # The original graph will be rewrite. >>> import paddle.static as static >>> from paddle.static.quantization import QuantizationTransformPass - >>> from paddle.fluid.framework import IrGraph + >>> from paddle.base.framework import IrGraph >>> from paddle.framework import core >>> graph = IrGraph(core.Graph(static.Program().desc), for_test=False) @@ -2449,7 +2449,7 @@ def __init__( >>> import paddle >>> import paddle.static as static >>> from paddle.static.quantization import QuantizationTransformPassV2 - >>> from paddle.fluid.framework import IrGraph + >>> from paddle.base.framework import IrGraph >>> from paddle.framework import core >>> graph = IrGraph(core.Graph(static.Program().desc), for_test=False) @@ -2842,7 +2842,7 @@ def __init__( >>> import paddle >>> import paddle.static as static >>> from paddle.static.quantization import AddQuantDequantPassV2 - >>> from paddle.fluid.framework import IrGraph + >>> from paddle.base.framework import IrGraph >>> from paddle.framework import core >>> graph = IrGraph(core.Graph(static.Program().desc), for_test=False) @@ -3025,7 +3025,7 @@ def __init__(self, scope, place, quant_bits=8): >>> import paddle >>> import paddle.static as static >>> from paddle.static.quantization import ReplaceFakeQuantDequantPass - >>> from paddle.fluid.framework import IrGraph + >>> from paddle.base.framework import IrGraph >>> from paddle.framework import core >>> graph = IrGraph(core.Graph(static.Program().desc), for_test=False) @@ -3182,7 +3182,7 @@ class QuantWeightPass: >>> import paddle >>> import paddle.static as static >>> from paddle.static.quantization import QuantWeightPass - >>> from paddle.fluid.framework import IrGraph + >>> from paddle.base.framework import IrGraph >>> from paddle.framework import core >>> graph = IrGraph(core.Graph(paddle.static.Program().desc), for_test=False) diff --git a/python/paddle/static/quantization/utils.py b/python/paddle/static/quantization/utils.py index 7397ff3fe01d2..29317795c05ba 100644 --- a/python/paddle/static/quantization/utils.py +++ b/python/paddle/static/quantization/utils.py @@ -16,7 +16,7 @@ import numpy as np -from ...fluid.framework import IrNode, Operator +from ...base.framework import IrNode, Operator from .quant_config import SUPPORT_QUANTIZATION_OP_DICT _channelwise_quant_axis1_ops = [ diff --git a/python/paddle/tensor/array.py b/python/paddle/tensor/array.py index 801aa78d4929a..f241071913341 100644 --- a/python/paddle/tensor/array.py +++ b/python/paddle/tensor/array.py @@ -14,8 +14,8 @@ # Define functions about array. +from ..base.data_feeder import check_type, check_variable_and_dtype from ..common_ops_import import Variable -from ..fluid.data_feeder import check_type, check_variable_and_dtype from ..framework import LayerHelper, core, in_dynamic_mode __all__ = [] diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index a7f9e5cef2297..60f142186809c 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -19,9 +19,9 @@ import paddle from paddle import _C_ops +from ..base.data_feeder import check_type, check_variable_and_dtype +from ..base.framework import in_dygraph_mode from ..common_ops_import import Variable -from ..fluid.data_feeder import check_type, check_variable_and_dtype -from ..fluid.framework import in_dygraph_mode from ..framework import LayerHelper, core from .creation import _complex_to_real_dtype, assign diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index 1ad7c82c39a37..ff92f9baae541 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -24,15 +24,15 @@ from paddle import _C_ops from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only -from ..fluid.data_feeder import ( +from ..base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, convert_dtype, convert_float_to_uint16, ) -from ..fluid.framework import Variable, device_guard -from ..fluid.param_attr import ParamAttr +from ..base.framework import Variable, device_guard +from ..base.param_attr import ParamAttr from ..framework import ( LayerHelper, _current_expected_place, diff --git a/python/paddle/tensor/einsum.py b/python/paddle/tensor/einsum.py index 11fb7e6f47607..955d104804bc8 100644 --- a/python/paddle/tensor/einsum.py +++ b/python/paddle/tensor/einsum.py @@ -22,9 +22,9 @@ from paddle import _C_ops -from ..fluid.data_feeder import check_type, check_variable_and_dtype -from ..fluid.framework import in_dygraph_mode -from ..fluid.layer_helper import LayerHelper +from ..base.data_feeder import check_type, check_variable_and_dtype +from ..base.framework import in_dygraph_mode +from ..base.layer_helper import LayerHelper from .linalg import matmul, transpose from .manipulation import reshape, squeeze, unsqueeze from .math import multiply diff --git a/python/paddle/tensor/layer_function_generator.py b/python/paddle/tensor/layer_function_generator.py index 955e2b13ec548..02ab66eb1da2a 100644 --- a/python/paddle/tensor/layer_function_generator.py +++ b/python/paddle/tensor/layer_function_generator.py @@ -18,9 +18,9 @@ from paddle import _C_ops, _legacy_C_ops +from ..base.data_feeder import check_variable_and_dtype +from ..base.proto import framework_pb2 from ..common_ops_import import Variable -from ..fluid.data_feeder import check_variable_and_dtype -from ..fluid.proto import framework_pb2 from ..framework import ( LayerHelper, OpProtoHolder, diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index c8ed387c5b71b..60c5afb99fc7b 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -18,12 +18,8 @@ from paddle import _C_ops from paddle.common_ops_import import VarDesc +from ..base.data_feeder import check_dtype, check_type, check_variable_and_dtype from ..common_ops_import import Variable -from ..fluid.data_feeder import ( - check_dtype, - check_type, - check_variable_and_dtype, -) from ..framework import LayerHelper, in_dynamic_mode from .creation import full from .manipulation import cast diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index 7bf39eb2e59df..8322b66e59a06 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -16,11 +16,11 @@ import paddle +from ..base.data_feeder import check_type, check_variable_and_dtype from ..common_ops_import import Variable -from ..fluid.data_feeder import check_type, check_variable_and_dtype from .layer_function_generator import templatedoc -Tensor = paddle.fluid.framework.core.eager.Tensor +Tensor = paddle.base.framework.core.eager.Tensor from paddle import _C_ops from paddle.tensor.creation import full @@ -1073,7 +1073,7 @@ def is_tensor(x): """ if in_dynamic_mode(): - return isinstance(x, (Tensor, paddle.fluid.core.eager.Tensor)) + return isinstance(x, (Tensor, paddle.base.core.eager.Tensor)) else: return isinstance(x, Variable) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index d44810aea1aaf..a6262b604a52f 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -21,13 +21,13 @@ from paddle.tensor import fill_constant from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only -from ..fluid.data_feeder import ( +from ..base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, convert_dtype, ) -from ..fluid.framework import Variable +from ..base.framework import Variable from ..framework import ( LayerHelper, convert_np_dtype_to_dtype_, @@ -5033,7 +5033,7 @@ def as_strided(x, shape, stride, offset=0, name=None): .. code-block:: python import paddle - paddle.fluid.set_flags({"FLAGS_use_stride_kernel": True}) + paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) x = paddle.rand([2, 4, 6], dtype="float32") @@ -5065,7 +5065,7 @@ def view(x, shape_or_dtype, name=None): .. code-block:: python import paddle - paddle.fluid.set_flags({"FLAGS_use_stride_kernel": True}) + paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) x = paddle.rand([2, 4, 6], dtype="float32") @@ -5074,7 +5074,7 @@ def view(x, shape_or_dtype, name=None): import paddle - paddle.fluid.set_flags({"FLAGS_use_stride_kernel": True}) + paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) x = paddle.rand([2, 4, 6], dtype="float32") @@ -5109,7 +5109,7 @@ def view_as(x, other, name=None): .. code-block:: python import paddle - paddle.fluid.set_flags({"FLAGS_use_stride_kernel": True}) + paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) x = paddle.rand([2, 4, 6], dtype="float32") y = paddle.rand([8, 6], dtype="float32") @@ -5142,7 +5142,7 @@ def unfold(x, axis, size, step, name=None): .. code-block:: python import paddle - paddle.fluid.set_flags({"FLAGS_use_stride_kernel": True}) + paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) x = paddle.arange(9, dtype="float64") diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 02ac8b4669e7e..0f9a3e4ebeb13 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -22,13 +22,13 @@ from paddle.common_ops_import import VarDesc, dygraph_only, dygraph_utils from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only -from ..common_ops_import import Variable -from ..fluid.data_feeder import ( +from ..base.data_feeder import ( check_dtype, check_type, check_variable_and_dtype, convert_dtype, ) +from ..common_ops_import import Variable from ..framework import ( LayerHelper, convert_np_dtype_to_dtype_, diff --git a/python/paddle/tensor/ops.py b/python/paddle/tensor/ops.py index e4d73408c367e..e15bf17beb646 100644 --- a/python/paddle/tensor/ops.py +++ b/python/paddle/tensor/ops.py @@ -16,7 +16,7 @@ from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only from .. import _C_ops -from ..fluid.data_feeder import check_variable_and_dtype +from ..base.data_feeder import check_variable_and_dtype from ..framework import LayerHelper, in_dynamic_mode from .layer_function_generator import ( add_sample_code, diff --git a/python/paddle/tensor/random.py b/python/paddle/tensor/random.py index f92d714dccb0b..bbfc533c5fc92 100644 --- a/python/paddle/tensor/random.py +++ b/python/paddle/tensor/random.py @@ -16,11 +16,11 @@ import paddle from paddle import _C_ops, _legacy_C_ops +from paddle.base.framework import _current_expected_place from paddle.common_ops_import import Variable -from paddle.fluid.framework import _current_expected_place from paddle.framework import in_dynamic_mode -from ..fluid.data_feeder import ( +from ..base.data_feeder import ( check_dtype, check_shape, check_type, @@ -255,7 +255,7 @@ def uniform_random_batch_size_like( Examples: .. code-block:: python import paddle - import paddle.fluid as fluid + import paddle.base as base from paddle.tensor import random paddle.enable_static() # example 1: diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index b7fe99f477ee8..5a90657016067 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -21,7 +21,7 @@ from paddle.common_ops_import import VarDesc, Variable from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only -from ..fluid.data_feeder import check_dtype, check_variable_and_dtype +from ..base.data_feeder import check_dtype, check_variable_and_dtype from ..framework import ( LayerHelper, convert_np_dtype_to_dtype_, @@ -29,8 +29,8 @@ in_dynamic_mode, ) -# from ..fluid.layers import has_inf #DEFINE_ALIAS -# from ..fluid.layers import has_nan #DEFINE_ALIAS +# from ..base.layers import has_inf #DEFINE_ALIAS +# from ..base.layers import has_nan #DEFINE_ALIAS __all__ = [] diff --git a/python/paddle/tensor/stat.py b/python/paddle/tensor/stat.py index 04fe17d85546b..7c5a6693e8c80 100644 --- a/python/paddle/tensor/stat.py +++ b/python/paddle/tensor/stat.py @@ -18,8 +18,8 @@ from paddle import _C_ops from paddle.framework import in_dynamic_mode, in_dynamic_or_new_ir_mode +from ..base.data_feeder import check_type, check_variable_and_dtype from ..common_ops_import import Variable -from ..fluid.data_feeder import check_type, check_variable_and_dtype from ..framework import LayerHelper, core from .math import _get_reduce_axis_with_tensor from .search import where diff --git a/python/paddle/tensor/to_string.py b/python/paddle/tensor/to_string.py index 9875263b45ab7..a9b5740b2fc30 100644 --- a/python/paddle/tensor/to_string.py +++ b/python/paddle/tensor/to_string.py @@ -14,7 +14,7 @@ import numpy as np -from paddle.fluid.data_feeder import check_type, convert_dtype +from paddle.base.data_feeder import check_type, convert_dtype from ..framework import core diff --git a/python/paddle/text/viterbi_decode.py b/python/paddle/text/viterbi_decode.py index 591f7ae6033e5..0091395bd26a9 100644 --- a/python/paddle/text/viterbi_decode.py +++ b/python/paddle/text/viterbi_decode.py @@ -14,9 +14,9 @@ from paddle import _C_ops -from ..fluid.data_feeder import check_type, check_variable_and_dtype -from ..fluid.framework import in_dygraph_mode -from ..fluid.layer_helper import LayerHelper +from ..base.data_feeder import check_type, check_variable_and_dtype +from ..base.framework import in_dygraph_mode +from ..base.layer_helper import LayerHelper from ..nn import Layer __all__ = ['viterbi_decode', 'ViterbiDecoder'] diff --git a/python/paddle/utils/__init__.py b/python/paddle/utils/__init__.py index df62d9982f6f4..630af97f280f5 100644 --- a/python/paddle/utils/__init__.py +++ b/python/paddle/utils/__init__.py @@ -18,7 +18,7 @@ from .op_version import OpLastCheckpointChecker # noqa: F401 from .install_check import run_check # noqa: F401 from . import unique_name # noqa: F401 -from ..fluid.framework import require_version # noqa: F401 +from ..base.framework import require_version # noqa: F401 from . import download # noqa: F401 from . import image_util # noqa: F401 diff --git a/python/paddle/utils/cpp_extension/cpp_extension.py b/python/paddle/utils/cpp_extension/cpp_extension.py index a1c1f794e4f6f..4ea6c9ad591d6 100644 --- a/python/paddle/utils/cpp_extension/cpp_extension.py +++ b/python/paddle/utils/cpp_extension/cpp_extension.py @@ -59,7 +59,7 @@ ) from .extension_utils import CLANG_COMPILE_FLAGS, CLANG_LINK_FLAGS -from ...fluid import core +from ...base import core # Note(zhouwei): On windows, it will export function 'PyInit_[name]' by default, # The solution is: 1.User add function PyInit_[name] 2. set not to export diff --git a/python/paddle/utils/cpp_extension/extension_utils.py b/python/paddle/utils/cpp_extension/extension_utils.py index d98d2c8e7aed3..df2fd45b345ea 100644 --- a/python/paddle/utils/cpp_extension/extension_utils.py +++ b/python/paddle/utils/cpp_extension/extension_utils.py @@ -38,8 +38,8 @@ except ImportError: DEVNULL = open(os.devnull, 'wb') -from ...fluid import core -from ...fluid.framework import OpProtoHolder +from ...base import core +from ...base.framework import OpProtoHolder from ...sysconfig import get_include, get_lib logger = logging.getLogger("utils.cpp_extension") @@ -448,13 +448,13 @@ def get_rocm_arch_flags(cflags): return cflags -def _get_fluid_path(): +def _get_base_path(): """ - Return installed fluid dir path. + Return installed base dir path. """ import paddle - return os.path.join(os.path.dirname(paddle.__file__), 'fluid') + return os.path.join(os.path.dirname(paddle.__file__), 'base') def _get_core_name(): @@ -471,7 +471,7 @@ def _get_lib_core_path(): """ raw_core_name = _get_core_name() lib_core_name = f"lib{raw_core_name[:-3]}.dylib" - return os.path.join(_get_fluid_path(), lib_core_name) + return os.path.join(_get_base_path(), lib_core_name) def _get_dll_core_path(): @@ -480,17 +480,17 @@ def _get_dll_core_path(): """ raw_core_name = _get_core_name() dll_core_name = "libpaddle.dll" - return os.path.join(_get_fluid_path(), dll_core_name) + return os.path.join(_get_base_path(), dll_core_name) def _reset_so_rpath(so_path): """ NOTE(Aurelius84): Runtime path of libpaddle.so is modified into `@loader_path/../libs` in setup.py.in. While loading custom op, `@loader_path` is the dirname of custom op - instead of `paddle/fluid`. So we modify `@loader_path` from custom dylib into `@rpath` + instead of `paddle/base`. So we modify `@loader_path` from custom dylib into `@rpath` to ensure dynamic loader find it correctly. - Moreover, we will add `-rpath site-packages/paddle/fluid` while linking the dylib so + Moreover, we will add `-rpath site-packages/paddle/base` while linking the dylib so that we don't need to set `LD_LIBRARY_PATH` any more. """ assert os.path.exists(so_path) @@ -518,7 +518,7 @@ def _get_include_dirs_when_compiling(compile_dir): with open(include_dirs_file, 'r') as f: include_dirs = [line.strip() for line in f.readlines() if line.strip()] - extra_dirs = ['paddle/fluid/platform'] + extra_dirs = ['paddle/base/platform'] all_include_dirs = list(include_dirs) for extra_dir in extra_dirs: for include_dir in include_dirs: @@ -583,7 +583,7 @@ def normalize_extension_kwargs(kwargs, use_cuda=False): # ----------------------- MacOS Platform ----------------------- # else: # See _reset_so_rpath for details. - extra_link_args.append(f'-Wl,-rpath,{_get_fluid_path()}') + extra_link_args.append(f'-Wl,-rpath,{_get_base_path()}') # On MacOS, ld don't support `-l:xx`, so we create a # liblibpaddle.dylib symbol link. lib_core_name = create_sym_link_if_not_exist() @@ -606,7 +606,7 @@ def normalize_extension_kwargs(kwargs, use_cuda=False): kwargs['runtime_library_dirs'] = runtime_library_dirs if compile_dir is None: - # Add this compile option to isolate fluid headers + # Add this compile option to isolate base headers add_compile_flag(extra_compile_args, ['-DPADDLE_WITH_CUSTOM_KERNEL']) kwargs['extra_compile_args'] = extra_compile_args @@ -621,7 +621,7 @@ def create_sym_link_if_not_exist(): assert OS_NAME.startswith('darwin') or IS_WINDOWS raw_core_name = _get_core_name() - core_path = os.path.join(_get_fluid_path(), raw_core_name) + core_path = os.path.join(_get_base_path(), raw_core_name) if IS_WINDOWS: new_dll_core_path = _get_dll_core_path() # create symbol link on windows @@ -871,8 +871,8 @@ def find_paddle_libraries(use_cuda=False): cuda_lib_dir = find_cuda_libraries() paddle_lib_dirs.extend(cuda_lib_dir) - # add `paddle/fluid` to search `libpaddle.so` - paddle_lib_dirs.append(_get_fluid_path()) + # add `paddle/base` to search `libpaddle.so` + paddle_lib_dirs.append(_get_base_path()) return paddle_lib_dirs @@ -1142,9 +1142,9 @@ def _custom_api_content(op_name): ) API_TEMPLATE = textwrap.dedent( """ - import paddle.fluid.core as core + import paddle.base.core as core from paddle.framework import in_dynamic_mode - from paddle.fluid.layer_helper import LayerHelper + from paddle.base.layer_helper import LayerHelper def {op_name}({params_list}): # The output variable's dtype use default value 'float32', diff --git a/python/paddle/utils/dlpack.py b/python/paddle/utils/dlpack.py index c818d16125eed..ed196beedb356 100644 --- a/python/paddle/utils/dlpack.py +++ b/python/paddle/utils/dlpack.py @@ -14,9 +14,9 @@ import paddle -from ..fluid.core import LoDTensor -from ..fluid.data_feeder import check_type -from ..fluid.framework import in_dygraph_mode +from ..base.core import LoDTensor +from ..base.data_feeder import check_type +from ..base.framework import in_dygraph_mode __all__ = [ 'to_dlpack', @@ -50,7 +50,7 @@ def to_dlpack(x): """ if in_dygraph_mode(): - if not isinstance(x, (paddle.Tensor, paddle.fluid.core.eager.Tensor)): + if not isinstance(x, (paddle.Tensor, paddle.base.core.eager.Tensor)): raise TypeError( "The type of 'x' in to_dlpack must be paddle.Tensor," " but received {}.".format(type(x)) @@ -98,9 +98,9 @@ def from_dlpack(dlpack): ) if in_dygraph_mode(): - out = paddle.fluid.core.from_dlpack(dlpack) + out = paddle.base.core.from_dlpack(dlpack) out = paddle.to_tensor(out) return out - out = paddle.fluid.core.from_dlpack(dlpack) + out = paddle.base.core.from_dlpack(dlpack) return out diff --git a/python/paddle/utils/inplace_utils.py b/python/paddle/utils/inplace_utils.py index bee68e1212598..ee5032d0657fe 100644 --- a/python/paddle/utils/inplace_utils.py +++ b/python/paddle/utils/inplace_utils.py @@ -15,7 +15,7 @@ import warnings import paddle # noqa: F401 -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle.base.wrapped_decorator import wrap_decorator from paddle.framework import in_dynamic_mode @@ -33,7 +33,7 @@ def __impl__(*args, **kwargs): func.__name__, origin_api_name ) ) - from ..fluid.dygraph.base import in_declarative_mode + from ..base.dygraph.base import in_declarative_mode if in_declarative_mode(): for arg in args: diff --git a/python/paddle/utils/layers_utils.py b/python/paddle/utils/layers_utils.py index 57d014c40e0ab..d7ac845926875 100644 --- a/python/paddle/utils/layers_utils.py +++ b/python/paddle/utils/layers_utils.py @@ -20,8 +20,8 @@ import paddle -from ..fluid.data_feeder import check_dtype, convert_dtype -from ..fluid.framework import Block, Variable, in_dygraph_mode +from ..base.data_feeder import check_dtype, convert_dtype +from ..base.framework import Block, Variable, in_dygraph_mode def convert_to_list(value, n, name, dtype=int): @@ -175,7 +175,7 @@ def flatten(nest): """ :alias_main: paddle.flatten :alias: paddle.flatten,paddle.tensor.flatten,paddle.tensor.manipulation.flatten - :old_api: paddle.fluid.layers.flatten + :old_api: paddle.base.layers.flatten Traverse all entries in the nested structure and put them into an list. """ diff --git a/python/paddle/utils/op_version.py b/python/paddle/utils/op_version.py index 793e0b621990c..6d70215f40cbd 100644 --- a/python/paddle/utils/op_version.py +++ b/python/paddle/utils/op_version.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..fluid import core +from ..base import core __all__ = [] diff --git a/python/paddle/utils/unique_name.py b/python/paddle/utils/unique_name.py index 0aae339f29545..bfd26da255fa1 100644 --- a/python/paddle/utils/unique_name.py +++ b/python/paddle/utils/unique_name.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..fluid.unique_name import generate # noqa: F401 -from ..fluid.unique_name import generate_with_ignorable_key # noqa: F401 -from ..fluid.unique_name import guard # noqa: F401 -from ..fluid.unique_name import switch # noqa: F401 +from ..base.unique_name import generate # noqa: F401 +from ..base.unique_name import generate_with_ignorable_key # noqa: F401 +from ..base.unique_name import guard # noqa: F401 +from ..base.unique_name import switch # noqa: F401 __all__ = ['generate', 'switch', 'guard'] # noqa diff --git a/python/paddle/vision/models/alexnet.py b/python/paddle/vision/models/alexnet.py index 7a6e2b0328ae5..26282bf88f7a1 100644 --- a/python/paddle/vision/models/alexnet.py +++ b/python/paddle/vision/models/alexnet.py @@ -17,7 +17,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import Conv2D, Dropout, Linear, MaxPool2D, ReLU from paddle.nn.initializer import Uniform from paddle.utils.download import get_weights_path_from_url diff --git a/python/paddle/vision/models/densenet.py b/python/paddle/vision/models/densenet.py index ccf6573f5588a..90346d4ff7493 100644 --- a/python/paddle/vision/models/densenet.py +++ b/python/paddle/vision/models/densenet.py @@ -16,7 +16,7 @@ import paddle from paddle import nn -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import ( AdaptiveAvgPool2D, AvgPool2D, diff --git a/python/paddle/vision/models/googlenet.py b/python/paddle/vision/models/googlenet.py index 617ce182c5039..174ddb144fe3d 100644 --- a/python/paddle/vision/models/googlenet.py +++ b/python/paddle/vision/models/googlenet.py @@ -15,7 +15,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import ( AdaptiveAvgPool2D, AvgPool2D, diff --git a/python/paddle/vision/models/inceptionv3.py b/python/paddle/vision/models/inceptionv3.py index 9482d7b12e208..24249fbf98aee 100644 --- a/python/paddle/vision/models/inceptionv3.py +++ b/python/paddle/vision/models/inceptionv3.py @@ -16,7 +16,7 @@ import paddle from paddle import nn -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import AdaptiveAvgPool2D, AvgPool2D, Dropout, Linear, MaxPool2D from paddle.nn.initializer import Uniform from paddle.utils.download import get_weights_path_from_url diff --git a/python/paddle/vision/models/squeezenet.py b/python/paddle/vision/models/squeezenet.py index 08cc1a6463324..9133a19993421 100644 --- a/python/paddle/vision/models/squeezenet.py +++ b/python/paddle/vision/models/squeezenet.py @@ -15,7 +15,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import AdaptiveAvgPool2D, Conv2D, Dropout, MaxPool2D from paddle.utils.download import get_weights_path_from_url diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index 0f73f950d1548..ccec5c41a549b 100755 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -18,10 +18,10 @@ from paddle.tensor.math import _add_with_axis from paddle.utils import convert_to_list -from ..fluid import core -from ..fluid.data_feeder import check_type, check_variable_and_dtype -from ..fluid.framework import Variable, in_dygraph_mode -from ..fluid.layer_helper import LayerHelper +from ..base import core +from ..base.data_feeder import check_type, check_variable_and_dtype +from ..base.framework import Variable, in_dygraph_mode +from ..base.layer_helper import LayerHelper from ..framework import _current_expected_place from ..nn import BatchNorm2D, Conv2D, Layer, ReLU, Sequential from ..nn.initializer import Normal diff --git a/python/paddle/vision/transforms/functional.py b/python/paddle/vision/transforms/functional.py index 40a708da5b1f6..eed38fadc7734 100644 --- a/python/paddle/vision/transforms/functional.py +++ b/python/paddle/vision/transforms/functional.py @@ -20,7 +20,7 @@ import paddle -from ...fluid.framework import Variable +from ...base.framework import Variable from . import functional_cv2 as F_cv2 from . import functional_pil as F_pil from . import functional_tensor as F_t diff --git a/python/paddle/vision/transforms/functional_cv2.py b/python/paddle/vision/transforms/functional_cv2.py index 1f76b35d201fe..0c4f70aad78c8 100644 --- a/python/paddle/vision/transforms/functional_cv2.py +++ b/python/paddle/vision/transforms/functional_cv2.py @@ -50,7 +50,7 @@ def to_tensor(pic, data_format='CHW'): else: img = paddle.to_tensor(pic) - if paddle.fluid.data_feeder.convert_dtype(img.dtype) == 'uint8': + if paddle.base.data_feeder.convert_dtype(img.dtype) == 'uint8': return paddle.cast(img, np.float32) / 255.0 else: return img diff --git a/python/paddle/vision/transforms/functional_pil.py b/python/paddle/vision/transforms/functional_pil.py index 3d816c25742f9..6f1a8b9860e79 100644 --- a/python/paddle/vision/transforms/functional_pil.py +++ b/python/paddle/vision/transforms/functional_pil.py @@ -81,7 +81,7 @@ def to_tensor(pic, data_format='CHW'): else: nchannel = len(pic.mode) - dtype = paddle.fluid.data_feeder.convert_dtype(img.dtype) + dtype = paddle.base.data_feeder.convert_dtype(img.dtype) if dtype == 'uint8': img = paddle.cast(img, np.float32) / 255.0 diff --git a/python/paddle/vision/transforms/functional_tensor.py b/python/paddle/vision/transforms/functional_tensor.py index e7b57a011baeb..c22ff07e922ed 100644 --- a/python/paddle/vision/transforms/functional_tensor.py +++ b/python/paddle/vision/transforms/functional_tensor.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F -from ...fluid.framework import Variable +from ...base.framework import Variable __all__ = [] diff --git a/python/setup.py.in b/python/setup.py.in index 61ce98ffda09c..0ff5f3d1814bb 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -422,14 +422,14 @@ packages=['paddle', 'paddle.inference', 'paddle.inference.contrib', 'paddle.inference.contrib.utils', - 'paddle.fluid', - 'paddle.fluid.dygraph', - 'paddle.fluid.proto', - 'paddle.fluid.proto.profiler', - 'paddle.fluid.layers', - 'paddle.fluid.incubate', + 'paddle.base', + 'paddle.base.dygraph', + 'paddle.base.proto', + 'paddle.base.proto.profiler', + 'paddle.base.layers', + 'paddle.base.incubate', 'paddle.incubate.distributed.fleet', - 'paddle.fluid.incubate.checkpoint', + 'paddle.base.incubate.checkpoint', 'paddle.amp', 'paddle.cost_model', 'paddle.hapi', @@ -524,19 +524,19 @@ if not '${WIN32}': paddle_bins = ['${PADDLE_BINARY_DIR}/paddle/scripts/paddle'] if os.name != 'nt': - package_data={'paddle.fluid': ['${FLUID_CORE_NAME}' + '.so']} + package_data={'paddle.base': ['${FLUID_CORE_NAME}' + '.so']} else: - package_data={'paddle.fluid': ['${FLUID_CORE_NAME}' + '.pyd', '${FLUID_CORE_NAME}' + '.lib']} + package_data={'paddle.base': ['${FLUID_CORE_NAME}' + '.pyd', '${FLUID_CORE_NAME}' + '.lib']} -package_data['paddle.fluid'] += ['${PADDLE_BINARY_DIR}/python/paddle/cost_model/static_op_benchmark.json'] +package_data['paddle.base'] += ['${PADDLE_BINARY_DIR}/python/paddle/cost_model/static_op_benchmark.json'] package_dir={ '': '${PADDLE_BINARY_DIR}/python', - # The paddle.fluid.proto will be generated while compiling. + # The paddle.base.proto will be generated while compiling. # So that package points to other directory. - 'paddle.fluid.proto.profiler': '${PADDLE_BINARY_DIR}/paddle/fluid/platform', - 'paddle.fluid.proto': '${PADDLE_BINARY_DIR}/paddle/fluid/framework', - 'paddle.fluid': '${PADDLE_BINARY_DIR}/python/paddle/fluid', + 'paddle.base.proto.profiler': '${PADDLE_BINARY_DIR}/paddle/fluid/platform', + 'paddle.base.proto': '${PADDLE_BINARY_DIR}/paddle/fluid/framework', + 'paddle.base': '${PADDLE_BINARY_DIR}/python/paddle/base', } # put all thirdparty libraries in paddle.libs @@ -706,14 +706,14 @@ package_dir['paddle.libs']=libs_path # change rpath of ${FLUID_CORE_NAME}.ext, add $ORIGIN/../libs/ to it. # The reason is that libwarpctc.ext, libiomp5.ext etc are in paddle.libs, and -# ${FLUID_CORE_NAME}.ext is in paddle.fluid, thus paddle/fluid/../libs will pointer to above libraries. +# ${FLUID_CORE_NAME}.ext is in paddle.base, thus paddle/fluid/../libs will pointer to above libraries. # This operation will fix https://github.com/PaddlePaddle/Paddle/issues/3213 if '${CMAKE_BUILD_TYPE}' == 'Release': if os.name != 'nt': # only change rpath in Release mode, since in Debug mode, ${FLUID_CORE_NAME}.xx is too large to be changed. if "@APPLE@" == "1": - commands = ["install_name_tool -id '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] - commands.append("install_name_tool -add_rpath '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so') + commands = ["install_name_tool -id '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/base/${FLUID_CORE_NAME}" + '.so'] + commands.append("install_name_tool -add_rpath '@loader_path/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/base/${FLUID_CORE_NAME}" + '.so') if('${WITH_SHARED_PHI}' == 'ON'): # change rpath of phi.ext for loading 3rd party libb commands.append("install_name_tool -add_rpath '@loader_path' ${PADDLE_BINARY_DIR}/python/paddle/libs/${PHI_NAME}") @@ -721,7 +721,7 @@ if '${CMAKE_BUILD_TYPE}' == 'Release': # change rpath of ir.ext for loading 3rd party libb commands.append("install_name_tool -add_rpath '@loader_path' ${PADDLE_BINARY_DIR}/python/paddle/libs/${IR_NAME}") else: - commands = ["patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/fluid/${FLUID_CORE_NAME}" + '.so'] + commands = ["patchelf --set-rpath '$ORIGIN/../libs/' ${PADDLE_BINARY_DIR}/python/paddle/base/${FLUID_CORE_NAME}" + '.so'] if('${WITH_SHARED_PHI}' == 'ON'): # change rpath of phi.ext for loading 3rd party lib commands.append("patchelf --set-rpath '$ORIGIN' ${PADDLE_BINARY_DIR}/python/paddle/libs/${PHI_NAME}") diff --git a/r/README.md b/r/README.md index 33f1807cd6afc..15ffddcbc1205 100644 --- a/r/README.md +++ b/r/README.md @@ -32,7 +32,7 @@ library(reticulate) library(RcppCNPy) use_python("/opt/python3.7/bin/python3.7") -paddle <- import("paddle.fluid.core") +paddle <- import("paddle.base.core") ``` Create an `AnalysisConfig`, which is the configuration of the paddle inference engine. diff --git a/r/README_cn.md b/r/README_cn.md index b844e0ad0849b..9dcb49d638722 100644 --- a/r/README_cn.md +++ b/r/README_cn.md @@ -22,7 +22,7 @@ install.packages("reticulate") # 调用Paddle library(reticulate) use_python("/opt/python3.7/bin/python") -paddle <- import("paddle.fluid.core") +paddle <- import("paddle.base.core") ``` 创建一个AnalysisConfig,用于设置预测引擎的各选项 diff --git a/r/example/mobilenet.py b/r/example/mobilenet.py index 806c5ba27ad86..d5f18260dac02 100755 --- a/r/example/mobilenet.py +++ b/r/example/mobilenet.py @@ -18,7 +18,7 @@ import numpy as np -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor +from paddle.base.core import AnalysisConfig, create_paddle_predictor def main(): diff --git a/r/example/mobilenet.r b/r/example/mobilenet.r index 3da8965eb5742..d3978c9f5d096 100755 --- a/r/example/mobilenet.r +++ b/r/example/mobilenet.r @@ -5,7 +5,7 @@ library(reticulate) # call Python library use_python("/opt/python3.7/bin/python") np <- import("numpy") -paddle <- import("paddle.fluid.core") +paddle <- import("paddle.base.core") set_config <- function() { config <- paddle$AnalysisConfig("") diff --git a/security/advisory/pdsa-2022-001.md b/security/advisory/pdsa-2022-001.md index 596b314e749cf..2f4250131233c 100644 --- a/security/advisory/pdsa-2022-001.md +++ b/security/advisory/pdsa-2022-001.md @@ -10,7 +10,7 @@ The PoC is as follows: ```python import paddle -import paddle.fluid as fluid +import paddle.base as fluid import numpy as np ids = paddle.to_tensor([[2,2],[6,1]]) diff --git a/security/advisory/pdsa-2022-001_cn.md b/security/advisory/pdsa-2022-001_cn.md index 60e428e2adddf..ae270510d7e83 100644 --- a/security/advisory/pdsa-2022-001_cn.md +++ b/security/advisory/pdsa-2022-001_cn.md @@ -10,7 +10,7 @@ PoC如下: ```python import paddle -import paddle.fluid as fluid +import paddle.base as fluid import numpy as np ids = paddle.to_tensor([[2,2],[6,1]]) diff --git a/setup.py b/setup.py index 962e9d70b002a..34d68f3efe370 100644 --- a/setup.py +++ b/setup.py @@ -219,10 +219,10 @@ class DevelopCommand(DevelopCommandBase): def run(self): # copy proto and .so to python_source_dir fluid_proto_binary_path = ( - paddle_binary_dir + '/python/paddle/fluid/proto/' + paddle_binary_dir + '/python/paddle/base/proto/' ) fluid_proto_source_path = ( - paddle_source_dir + '/python/paddle/fluid/proto/' + paddle_source_dir + '/python/paddle/base/proto/' ) distributed_proto_binary_path = ( paddle_binary_dir + '/python/paddle/distributed/fleet/proto/' @@ -237,8 +237,8 @@ def run(self): distributed_proto_binary_path, distributed_proto_source_path ) shutil.copy( - paddle_binary_dir + '/python/paddle/fluid/libpaddle.so', - paddle_source_dir + '/python/paddle/fluid/', + paddle_binary_dir + '/python/paddle/base/libpaddle.so', + paddle_source_dir + '/python/paddle/base/', ) dynamic_library_binary_path = paddle_binary_dir + '/python/paddle/libs/' dynamic_library_source_path = paddle_source_dir + '/python/paddle/libs/' @@ -891,16 +891,16 @@ def get_setup_requires(): def get_package_data_and_package_dir(): if os.name != 'nt': package_data = { - 'paddle.fluid': [env_dict.get("FLUID_CORE_NAME") + '.so'] + 'paddle.base': [env_dict.get("FLUID_CORE_NAME") + '.so'] } else: package_data = { - 'paddle.fluid': [ + 'paddle.base': [ env_dict.get("FLUID_CORE_NAME") + '.pyd', env_dict.get("FLUID_CORE_NAME") + '.lib', ] } - package_data['paddle.fluid'] += [ + package_data['paddle.base'] += [ paddle_binary_dir + '/python/paddle/cost_model/static_op_benchmark.json' ] if 'develop' in sys.argv: @@ -908,12 +908,12 @@ def get_package_data_and_package_dir(): else: package_dir = { '': env_dict.get("PADDLE_BINARY_DIR") + '/python', - 'paddle.fluid.proto.profiler': env_dict.get("PADDLE_BINARY_DIR") + 'paddle.base.proto.profiler': env_dict.get("PADDLE_BINARY_DIR") + '/paddle/fluid/platform', - 'paddle.fluid.proto': env_dict.get("PADDLE_BINARY_DIR") + 'paddle.base.proto': env_dict.get("PADDLE_BINARY_DIR") + '/paddle/fluid/framework', - 'paddle.fluid': env_dict.get("PADDLE_BINARY_DIR") - + '/python/paddle/fluid', + 'paddle.base': env_dict.get("PADDLE_BINARY_DIR") + + '/python/paddle/base', } # put all thirdparty libraries in paddle.libs libs_path = paddle_binary_dir + '/python/paddle/libs' @@ -1150,7 +1150,7 @@ def get_package_data_and_package_dir(): # change rpath of ${FLUID_CORE_NAME}.ext, add $ORIGIN/../libs/ to it. # The reason is that libwarpctc.ext, libwarprnnt.ext, libiomp5.ext etc are in paddle.libs, and - # ${FLUID_CORE_NAME}.ext is in paddle.fluid, thus paddle/fluid/../libs will pointer to above libraries. + # ${FLUID_CORE_NAME}.ext is in paddle.base, thus paddle/fluid/../libs will pointer to above libraries. # This operation will fix https://github.com/PaddlePaddle/Paddle/issues/3213 if env_dict.get("CMAKE_BUILD_TYPE") == 'Release': if os.name != 'nt': @@ -1159,14 +1159,14 @@ def get_package_data_and_package_dir(): commands = [ "install_name_tool -id '@loader_path/../libs/' " + env_dict.get("PADDLE_BINARY_DIR") - + '/python/paddle/fluid/' + + '/python/paddle/base/' + env_dict.get("FLUID_CORE_NAME") + '.so' ] commands.append( "install_name_tool -add_rpath '@loader_path/../libs/' " + env_dict.get("PADDLE_BINARY_DIR") - + '/python/paddle/fluid/' + + '/python/paddle/base/' + env_dict.get("FLUID_CORE_NAME") + '.so' ) @@ -1188,7 +1188,7 @@ def get_package_data_and_package_dir(): commands = [ "patchelf --set-rpath '$ORIGIN/../libs/' " + env_dict.get("PADDLE_BINARY_DIR") - + '/python/paddle/fluid/' + + '/python/paddle/base/' + env_dict.get("FLUID_CORE_NAME") + '.so' ] @@ -1420,14 +1420,14 @@ def get_setup_parameters(): 'paddle.inference', 'paddle.inference.contrib', 'paddle.inference.contrib.utils', - 'paddle.fluid', - 'paddle.fluid.dygraph', - 'paddle.fluid.proto', - 'paddle.fluid.proto.profiler', - 'paddle.fluid.layers', - 'paddle.fluid.incubate', + 'paddle.base', + 'paddle.base.dygraph', + 'paddle.base.proto', + 'paddle.base.proto.profiler', + 'paddle.base.layers', + 'paddle.base.incubate', 'paddle.incubate.distributed.fleet', - 'paddle.fluid.incubate.checkpoint', + 'paddle.base.incubate.checkpoint', 'paddle.amp', 'paddle.cost_model', 'paddle.hapi', diff --git a/test/amp/amp_base_models.py b/test/amp/amp_base_models.py index 8487b1a7d83be..8d4bb8a1106bd 100644 --- a/test/amp/amp_base_models.py +++ b/test/amp/amp_base_models.py @@ -20,7 +20,7 @@ import paddle from paddle import nn -from paddle.fluid import core +from paddle.base import core from paddle.framework import in_dynamic_mode diff --git a/test/amp/test_amp_api.py b/test/amp/test_amp_api.py index 58d7c1dd2f8e2..847da4cbcbf20 100644 --- a/test/amp/test_amp_api.py +++ b/test/amp/test_amp_api.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import core +from paddle.base import core from paddle.static import amp @@ -162,7 +162,7 @@ def test_amp_grad_scaler(self): scaler.minimize(optimizer, scaled) optimizer.clear_grad() paddle.amp.debugging.disable_operator_stats_collection() - op_list = paddle.fluid.core.get_low_precision_op_list() + op_list = paddle.base.core.get_low_precision_op_list() self.assertEqual(scaler._enable, False) self.assertEqual(scaler._use_dynamic_loss_scaling, False) diff --git a/test/amp/test_amp_decorate.py b/test/amp/test_amp_decorate.py index 58989571aad8b..f956d37c63b39 100644 --- a/test/amp/test_amp_decorate.py +++ b/test/amp/test_amp_decorate.py @@ -16,7 +16,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core class ConvBNLayer(paddle.nn.Layer): diff --git a/test/amp/test_amp_list.py b/test/amp/test_amp_list.py index 1eb5b1fb7b5b3..20a7a45e95784 100644 --- a/test/amp/test_amp_list.py +++ b/test/amp/test_amp_list.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static.amp import AutoMixedPrecisionLists, fp16_lists diff --git a/test/amp/test_amp_master_grad.py b/test/amp/test_amp_master_grad.py index 3eaf6546009d0..3a30e5d4a712d 100644 --- a/test/amp/test_amp_master_grad.py +++ b/test/amp/test_amp_master_grad.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class SimpleNet(paddle.nn.Layer): @@ -80,7 +80,7 @@ def run_dygraph(self, total_steps, accumulate_batchs_num, model, optimizer): scaler.update() opt.clear_grad() paddle.amp.debugging.disable_operator_stats_collection() - op_list = paddle.fluid.core.get_low_precision_op_list() + op_list = paddle.base.core.get_low_precision_op_list() return fp32_grads, op_list def test_adam_master_grad(self): diff --git a/test/amp/test_amp_promote.py b/test/amp/test_amp_promote.py index 95017df905fc9..52cda97d15fbb 100644 --- a/test/amp/test_amp_promote.py +++ b/test/amp/test_amp_promote.py @@ -18,7 +18,7 @@ from amp_base_models import AmpTestBase, build_conv_model import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import amp @@ -138,7 +138,7 @@ def check_promote_results( scaler.minimize(optimizer, scaled) optimizer.clear_grad() paddle.amp.debugging.disable_operator_stats_collection() - op_stats = paddle.fluid.core.get_low_precision_op_list() + op_stats = paddle.base.core.get_low_precision_op_list() self._check_op_calls( op_stats, diff --git a/test/amp/test_collect_operator_stats.py b/test/amp/test_collect_operator_stats.py index 157a1100df302..d17ece43727f4 100644 --- a/test/amp/test_collect_operator_stats.py +++ b/test/amp/test_collect_operator_stats.py @@ -22,7 +22,7 @@ class TestOpStatsEager(unittest.TestCase): def _check_result(self, dtype): # Returned the dict. - op_list = paddle.fluid.core.get_low_precision_op_list() + op_list = paddle.base.core.get_low_precision_op_list() self.assertTrue('elementwise_add' in op_list) self.assertTrue('conv2d' in op_list) diff --git a/test/amp/test_compare_accuracy_api.py b/test/amp/test_compare_accuracy_api.py index 83eb7af8df68e..ec35288652fa7 100644 --- a/test/amp/test_compare_accuracy_api.py +++ b/test/amp/test_compare_accuracy_api.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( @@ -23,7 +23,7 @@ ) class TestCompareAccuracyApi(unittest.TestCase): def calc(self, path, dtype): - paddle.fluid.core.set_nan_inf_debug_path(path) + paddle.base.core.set_nan_inf_debug_path(path) x = paddle.to_tensor( [2000, 3000, 4, 0], place=core.CUDAPlace(0), dtype=dtype ) diff --git a/test/amp/test_layer_convert_dtype.py b/test/amp/test_layer_convert_dtype.py index 31332e1d47f0a..4c8e7fe9cd2ae 100644 --- a/test/amp/test_layer_convert_dtype.py +++ b/test/amp/test_layer_convert_dtype.py @@ -17,7 +17,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import core +from paddle.base import core class MyModel(paddle.nn.Layer): diff --git a/test/amp/test_model_cast_to_bf16.py b/test/amp/test_model_cast_to_bf16.py index 79751b3d99d0c..da1e82dddc7dd 100644 --- a/test/amp/test_model_cast_to_bf16.py +++ b/test/amp/test_model_cast_to_bf16.py @@ -25,8 +25,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import amp paddle.enable_static() @@ -55,23 +55,23 @@ def static_graph(self): @contextlib.contextmanager def scope_prog_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield def get_static_graph_result( self, feed, fetch_list, amp_fun, with_lod=False, startup_prog=None ): - exe = fluid.Executor(core.CPUPlace()) + exe = base.Executor(core.CPUPlace()) exe.run( - fluid.default_startup_program() + base.default_startup_program() if startup_prog is None else startup_prog ) - prog = fluid.default_main_program() + prog = base.default_main_program() if amp_fun is not None: if startup_prog is not None: amp_fun(prog, startup_prog) @@ -193,7 +193,7 @@ def test_graph_cast(self): ), use_bf16_guard=True, ), - startup_prog=fluid.default_startup_program(), + startup_prog=base.default_startup_program(), ) diff --git a/test/asp/asp_pruning_base.py b/test/asp/asp_pruning_base.py index c6d7ff440cc11..5160d3a9652de 100644 --- a/test/asp/asp_pruning_base.py +++ b/test/asp/asp_pruning_base.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.asp import ASPHelper paddle.enable_static() @@ -27,8 +27,8 @@ class TestASPHelperPruningBase(unittest.TestCase): def setUp(self): - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() def build_model(): img = paddle.static.data( @@ -46,7 +46,7 @@ def build_model(): ) return img, label, prediction - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.img, self.label, self.predict = build_model() def run_inference_pruning_test( @@ -55,14 +55,14 @@ def run_inference_pruning_test( place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) + exe = base.Executor(place) self.__pruning_and_checking( exe, place, get_mask_gen_func, get_mask_check_func, False ) def run_training_pruning_test(self, get_mask_gen_func, get_mask_check_func): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): loss = paddle.mean( paddle.nn.functional.cross_entropy( input=self.predict, @@ -79,7 +79,7 @@ def run_training_pruning_test(self, get_mask_gen_func, get_mask_check_func): place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) + exe = base.Executor(place) self.__pruning_and_checking( exe, place, get_mask_gen_func, get_mask_check_func, True @@ -95,7 +95,7 @@ def __pruning_and_checking( for param in self.main_program.global_block().all_parameters(): if ASPHelper._is_supported_layer(self.main_program, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) self.assertTrue( paddle.incubate.asp.check_sparsity( diff --git a/test/asp/test_asp_customized_pruning.py b/test/asp/test_asp_customized_pruning.py index c63d12449c03d..e7933c9b7142e 100644 --- a/test/asp/test_asp_customized_pruning.py +++ b/test/asp/test_asp_customized_pruning.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate import asp as sparsity from paddle.incubate.asp.supported_layer_list import ( supported_layers_and_prune_func_map, @@ -188,8 +188,8 @@ class TestASPStaticCustomerizedPruneFunc(unittest.TestCase): def setUp(self): paddle.enable_static() - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() self.customer_prefix = "customer_layer" @@ -215,14 +215,14 @@ def build_model(): ) return img, label, prediction - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.img, self.label, self.predict = build_model() self.supported_layer_count_ref = 5 self.place = paddle.CPUPlace() if core.is_compiled_with_cuda(): self.place = paddle.CUDAPlace(0) - self.exe = fluid.Executor(self.place) + self.exe = base.Executor(self.place) sparsity.add_supported_layer(self.customer_prefix, my_own_pruning) @@ -236,7 +236,7 @@ def test_inference_pruning(self): supported_layer_count = 0 for param in self.main_program.global_block().all_parameters(): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if sparsity.asp.ASPHelper._is_supported_layer( self.main_program, param.name @@ -265,7 +265,7 @@ def test_inference_pruning(self): self.assertEqual(supported_layer_count, self.supported_layer_count_ref) def test_training_pruning(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): loss = paddle.mean( paddle.nn.functional.cross_entropy( input=self.predict, @@ -288,13 +288,13 @@ def test_training_pruning(self): supported_layer_count = 0 for param in self.main_program.global_block().all_parameters(): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if sparsity.asp.ASPHelper._is_supported_layer( self.main_program, param.name ): mat_mask = np.array( - fluid.global_scope() + base.global_scope() .find_var(sparsity.asp.ASPHelper._get_mask_name(param.name)) .get_tensor() ) diff --git a/test/asp/test_asp_optimize_dynamic.py b/test/asp/test_asp_optimize_dynamic.py index 79aada06ac562..293a5bbe7e15c 100644 --- a/test/asp/test_asp_optimize_dynamic.py +++ b/test/asp/test_asp_optimize_dynamic.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.asp import ASPHelper diff --git a/test/asp/test_asp_optimize_static.py b/test/asp/test_asp_optimize_static.py index 863bbe4f0c04e..6074bfd7c8310 100644 --- a/test/asp/test_asp_optimize_static.py +++ b/test/asp/test_asp_optimize_static.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.asp import ASPHelper paddle.enable_static() @@ -27,8 +27,8 @@ class TestASPStaticOptimize(unittest.TestCase): def setUp(self): - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() def build_model(): img = paddle.static.data( @@ -46,7 +46,7 @@ def build_model(): ) return img, label, prediction - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.img, self.label, predict = build_model() self.loss = paddle.mean( paddle.nn.functional.cross_entropy( @@ -74,7 +74,7 @@ def check_params(params, params_from_asp): ) self.assertTrue(check_params(params, params_from_asp)) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): ASPHelper._minimize( self.optimizer, self.loss, @@ -166,7 +166,7 @@ def test_decorate(self): param_names = self.__get_param_names( self.main_program.global_block().all_parameters() ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.optimizer = paddle.incubate.asp.decorate(self.optimizer) self.optimizer.minimize(self.loss, self.startup_program) param_names_after_minimize = self.__get_param_names( @@ -178,15 +178,15 @@ def test_decorate(self): ) def test_asp_training(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.optimizer = paddle.incubate.asp.decorate(self.optimizer) self.optimizer.minimize(self.loss, self.startup_program) place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[self.img, self.label], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[self.img, self.label], place=place) exe.run(self.startup_program) paddle.incubate.asp.prune_model(self.main_program) @@ -200,7 +200,7 @@ def test_asp_training(self): for param in self.main_program.global_block().all_parameters(): if ASPHelper._is_supported_layer(self.main_program, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 @@ -216,13 +216,13 @@ def test_asp_training(self): def test_asp_training_with_amp(self): if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.optimizer = paddle.static.amp.decorate(self.optimizer) self.optimizer = paddle.incubate.asp.decorate(self.optimizer) self.optimizer.minimize(self.loss, self.startup_program) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder( + exe = base.Executor(place) + feeder = base.DataFeeder( feed_list=[self.img, self.label], place=place ) @@ -238,7 +238,7 @@ def test_asp_training_with_amp(self): for param in self.main_program.global_block().all_parameters(): if ASPHelper._is_supported_layer(self.main_program, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 diff --git a/test/asp/test_asp_pruning_dynamic.py b/test/asp/test_asp_pruning_dynamic.py index 095180ff3f346..b41f52b7c1050 100644 --- a/test/asp/test_asp_pruning_dynamic.py +++ b/test/asp/test_asp_pruning_dynamic.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.asp import ASPHelper diff --git a/test/asp/test_asp_pruning_static.py b/test/asp/test_asp_pruning_static.py index 3f87a2174eba8..2db7d8d42f6ab 100644 --- a/test/asp/test_asp_pruning_static.py +++ b/test/asp/test_asp_pruning_static.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.asp import ASPHelper paddle.enable_static() @@ -27,8 +27,8 @@ class TestASPStaticPruningBase(unittest.TestCase): def setUp(self): - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() def build_model(): img = paddle.static.data( @@ -49,7 +49,7 @@ def build_model(): ) return img, label, prediction - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.img, self.label, self.predict = build_model() self.set_config() @@ -62,12 +62,12 @@ def test_inference_pruning(self): place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) + exe = base.Executor(place) self.__pruning_and_checking(exe, place, False) def test_training_pruning(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): loss = paddle.mean( paddle.nn.functional.cross_entropy( input=self.predict, @@ -84,7 +84,7 @@ def test_training_pruning(self): place = paddle.CPUPlace() if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) + exe = base.Executor(place) self.__pruning_and_checking(exe, place, True) @@ -96,7 +96,7 @@ def __pruning_and_checking(self, exe, place, with_mask): for param in self.main_program.global_block().all_parameters(): if ASPHelper._is_supported_layer(self.main_program, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 diff --git a/test/asp/test_asp_save_load.py b/test/asp/test_asp_save_load.py index 698aacbd9b401..f9966c321b37e 100644 --- a/test/asp/test_asp_save_load.py +++ b/test/asp/test_asp_save_load.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.asp import ASPHelper @@ -124,8 +124,8 @@ class TestASPStaticOptimize(unittest.TestCase): def setUp(self): paddle.enable_static() - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() def build_model(): img = paddle.static.data( @@ -143,7 +143,7 @@ def build_model(): ) return img, label, prediction - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.img, self.label, predict = build_model() self.loss = paddle.mean( paddle.nn.functional.cross_entropy( @@ -160,7 +160,7 @@ def build_model(): self.place = paddle.CPUPlace() if core.is_compiled_with_cuda(): self.place = paddle.CUDAPlace(0) - self.exe = fluid.Executor(self.place) + self.exe = base.Executor(self.place) self.exe.run(self.startup_program) paddle.incubate.asp.prune_model(self.main_program) @@ -178,7 +178,7 @@ def test_save_and_load(self): state_dict = paddle.load(param_path) prog.set_state_dict(state_dict) - feeder = fluid.DataFeeder( + feeder = base.DataFeeder( feed_list=[self.img, self.label], place=self.place ) @@ -191,7 +191,7 @@ def test_save_and_load(self): for param in prog.global_block().all_parameters(): if ASPHelper._is_supported_layer(prog, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 diff --git a/test/asp/test_fleet_with_asp_dynamic.py b/test/asp/test_fleet_with_asp_dynamic.py index ee2261aaf5248..03e8bbdcb8dd3 100644 --- a/test/asp/test_fleet_with_asp_dynamic.py +++ b/test/asp/test_fleet_with_asp_dynamic.py @@ -19,8 +19,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed import fleet -from paddle.fluid import core from paddle.incubate.asp import ASPHelper cuda_visible_devices = os.getenv('CUDA_VISIBLE_DEVICES') diff --git a/test/asp/test_fleet_with_asp_sharding.py b/test/asp/test_fleet_with_asp_sharding.py index 4dabb3549df33..59cf1d575d33d 100644 --- a/test/asp/test_fleet_with_asp_sharding.py +++ b/test/asp/test_fleet_with_asp_sharding.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.incubate import asp as sparsity from paddle.incubate.asp import ASPHelper @@ -48,7 +48,7 @@ def setUp(self): os.environ['FLAGS_check_nan_inf'] = "0" def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -86,27 +86,27 @@ def net(self, main_prog, startup_prog): def test_with_asp_sharding(self): fleet.init(is_collective=True) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy, input_x, input_y = self.net( train_prog, startup_prog ) - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): optimizer = paddle.optimizer.SGD(learning_rate=0.01) optimizer = fleet.distributed_optimizer( optimizer, strategy=strategy ) optimizer.minimize(avg_cost) - if paddle.fluid.is_compiled_with_cuda(): - place = fluid.CUDAPlace( + if paddle.base.is_compiled_with_cuda(): + place = base.CUDAPlace( int(os.environ.get('FLAGS_selected_gpus', 0)) ) else: - place = fluid.CPUPlace() + place = base.CPUPlace() - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[input_x, input_y], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[input_x, input_y], place=place) exe.run(startup_prog) sparsity.prune_model(train_prog) @@ -117,7 +117,7 @@ def test_with_asp_sharding(self): for param in train_prog.global_block().all_parameters(): if ASPHelper._is_supported_layer(train_prog, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 diff --git a/test/asp/test_fleet_with_asp_static.py b/test/asp/test_fleet_with_asp_static.py index c0763f309e7a4..fc67b1f249f03 100644 --- a/test/asp/test_fleet_with_asp_static.py +++ b/test/asp/test_fleet_with_asp_static.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.incubate import asp as sparsity from paddle.incubate.asp import ASPHelper @@ -41,7 +41,7 @@ def setUp(self): os.environ["PADDLE_TRAINER_ID"] = "0" def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -65,12 +65,12 @@ def net(self, main_prog, startup_prog): def test_with_asp(self): fleet.init(is_collective=True) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy, input_x, input_y = self.net( train_prog, startup_prog ) - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): optimizer = paddle.optimizer.SGD(learning_rate=0.01) optimizer = fleet.distributed_optimizer( optimizer, strategy=strategy @@ -78,13 +78,13 @@ def test_with_asp(self): optimizer.minimize(avg_cost) place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[input_x, input_y], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[input_x, input_y], place=place) exe.run(startup_prog) sparsity.prune_model(train_prog) @@ -95,7 +95,7 @@ def test_with_asp(self): for param in train_prog.global_block().all_parameters(): if ASPHelper._is_supported_layer(train_prog, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 @@ -117,7 +117,7 @@ def setUp(self): os.environ["PADDLE_TRAINER_ID"] = "0" def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -141,13 +141,13 @@ def net(self, main_prog, startup_prog): def test_with_asp_and_amp(self): fleet.init(is_collective=True) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy, input_x, input_y = self.net( train_prog, startup_prog ) strategy.amp = True - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): optimizer = paddle.optimizer.SGD(learning_rate=0.01) optimizer = fleet.distributed_optimizer( optimizer, strategy=strategy @@ -155,13 +155,13 @@ def test_with_asp_and_amp(self): optimizer.minimize(avg_cost) place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[input_x, input_y], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[input_x, input_y], place=place) exe.run(startup_prog) optimizer.amp_init(place) @@ -174,7 +174,7 @@ def test_with_asp_and_amp(self): for param in train_prog.global_block().all_parameters(): if ASPHelper._is_supported_layer(train_prog, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 @@ -189,7 +189,7 @@ def test_with_asp_and_amp(self): def test_with_asp_and_pure_fp16(self): fleet.init(is_collective=True) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() with paddle.static.amp.fp16_guard(): avg_cost, strategy, input_x, input_y = self.net( train_prog, startup_prog @@ -197,7 +197,7 @@ def test_with_asp_and_pure_fp16(self): strategy.amp = True strategy.amp_configs = {'use_pure_fp16': True} - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): with paddle.static.amp.fp16_guard(): optimizer = optimizer = paddle.optimizer.Momentum( learning_rate=0.01, multi_precision=True @@ -208,13 +208,13 @@ def test_with_asp_and_pure_fp16(self): optimizer.minimize(avg_cost) place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[input_x, input_y], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[input_x, input_y], place=place) exe.run(startup_prog) optimizer.amp_init(place) @@ -227,7 +227,7 @@ def test_with_asp_and_pure_fp16(self): for param in train_prog.global_block().all_parameters(): if ASPHelper._is_supported_layer(train_prog, param.name): mat = np.array( - fluid.global_scope().find_var(param.name).get_tensor() + base.global_scope().find_var(param.name).get_tensor() ) if (len(param.shape) == 4 and param.shape[1] < 4) or ( len(param.shape) == 2 and param.shape[0] < 4 diff --git a/test/auto_parallel/1F1B_pass_unittest.py b/test/auto_parallel/1F1B_pass_unittest.py index 4f55031c84f05..8d5b9940aec6d 100644 --- a/test/auto_parallel/1F1B_pass_unittest.py +++ b/test/auto_parallel/1F1B_pass_unittest.py @@ -45,8 +45,8 @@ def apply_pass(use_1f1b=False): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class Test1F1BPass(unittest.TestCase): @@ -63,7 +63,7 @@ def init(self, engine): np.random.seed(2021) random.seed(2021) paddle.distributed.fleet.init(is_collective=True) - place = paddle.fluid.CUDAPlace(ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_1f1b=False): diff --git a/test/auto_parallel/amp_o2_pass.py b/test/auto_parallel/amp_o2_pass.py index 04af0112e31cc..a770be6d1e428 100644 --- a/test/auto_parallel/amp_o2_pass.py +++ b/test/auto_parallel/amp_o2_pass.py @@ -59,8 +59,8 @@ def apply_pass(use_amp=False, amp_dtype="bfloat16"): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestShardingStage2WithNewEXE(unittest.TestCase): @@ -74,7 +74,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_amp=False, amp_dtype="bfloat16"): diff --git a/test/auto_parallel/amp_pass_unittest.py b/test/auto_parallel/amp_pass_unittest.py index f8b422d5281a4..5d326936eb28e 100644 --- a/test/auto_parallel/amp_pass_unittest.py +++ b/test/auto_parallel/amp_pass_unittest.py @@ -44,8 +44,8 @@ def apply_pass(use_amp=False, level=None): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestAMPPass(unittest.TestCase): @@ -61,7 +61,7 @@ def init(self, engine): paddle.seed(2021) np.random.seed(2021) random.seed(2021) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_amp=False, level=None): diff --git a/test/auto_parallel/auto_parallel_relaunch_model.py b/test/auto_parallel/auto_parallel_relaunch_model.py index 65fc730a1dab7..25397e63b654c 100644 --- a/test/auto_parallel/auto_parallel_relaunch_model.py +++ b/test/auto_parallel/auto_parallel_relaunch_model.py @@ -109,7 +109,7 @@ def mlp_pretrain_forward(train_program, start_program): error_cost = paddle.nn.functional.square_error_cost(predict, label) loss = paddle.mean(error_cost) - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[input, label], capacity=4 * batch_size, iterable=True ) diff --git a/test/auto_parallel/clip_grad_by_global_norm.py b/test/auto_parallel/clip_grad_by_global_norm.py index 11fe954b7b51a..071d9c52c7891 100644 --- a/test/auto_parallel/clip_grad_by_global_norm.py +++ b/test/auto_parallel/clip_grad_by_global_norm.py @@ -37,13 +37,13 @@ def apply_pass(use_sharding=False): def get_parameter_value(program): - from paddle.fluid.framework import Parameter + from paddle.base.framework import Parameter def is_parameter(var): return isinstance(var, Parameter) def get_tensor(var): - t = paddle.fluid.global_scope().find_var(var.name).get_tensor() + t = paddle.base.global_scope().find_var(var.name).get_tensor() return np.array(t) def get_name(var): @@ -57,8 +57,8 @@ def get_name(var): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestGradientClipByGlobalNorm(unittest.TestCase): @@ -72,7 +72,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_sharding=False): diff --git a/test/auto_parallel/engine_api.py b/test/auto_parallel/engine_api.py index 38e19468a6bf3..cc921d41a74a9 100644 --- a/test/auto_parallel/engine_api.py +++ b/test/auto_parallel/engine_api.py @@ -306,7 +306,7 @@ def train_builtin_data_vars(): with static.program_guard(engine.main_program, engine.startup_program): feed_list = engine.inputs + engine.labels print(feed_list) - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=feed_list, capacity=4 * batch_size, iterable=False ) @@ -318,7 +318,7 @@ def train_builtin_data_vars(): try: while True: engine.run() - except paddle.fluid.core.EOFException: + except paddle.base.core.EOFException: loader.reset() # call DataLoader.reset() after catching EOFException @@ -336,7 +336,7 @@ def train_non_builtin_data_vars(): ) label = static.data(name="label", shape=[batch_size, 1], dtype='int64') - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[input, label], capacity=4 * batch_size, iterable=False ) places = static.cuda_places() @@ -380,7 +380,7 @@ def train_non_builtin_data_vars(): try: while True: engine.run() - except paddle.fluid.core.EOFException: + except paddle.base.core.EOFException: loader.reset() # call DataLoader.reset() after catching EOFException @@ -398,7 +398,7 @@ def get_cost(): ) label = static.data(name="label", shape=[batch_size, 1], dtype='int64') - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[input, label], capacity=4 * batch_size, iterable=False ) places = static.cuda_places() @@ -455,7 +455,7 @@ def get_cost_by_default_program(): input, process_mesh=PP_MESH_0, shard_spec=[None, None] ) - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[input, label], capacity=4 * batch_size, iterable=False ) places = static.cuda_places() diff --git a/test/auto_parallel/generation_pipeline_pass_unittest.py b/test/auto_parallel/generation_pipeline_pass_unittest.py index e2ad275805df4..9b4a74253fdad 100644 --- a/test/auto_parallel/generation_pipeline_pass_unittest.py +++ b/test/auto_parallel/generation_pipeline_pass_unittest.py @@ -159,7 +159,7 @@ def test_pp2(self): engine._executor.run( engine.main_program, use_program_cache=False, return_numpy=False ) - except paddle.fluid.core.EOFException: + except paddle.base.core.EOFException: print("test done") train_dataloader._inner_dataloader.reset() diff --git a/test/auto_parallel/gradient_merge_pass_unittest.py b/test/auto_parallel/gradient_merge_pass_unittest.py index adf40a236a852..048016be0c702 100644 --- a/test/auto_parallel/gradient_merge_pass_unittest.py +++ b/test/auto_parallel/gradient_merge_pass_unittest.py @@ -38,8 +38,8 @@ def apply_pass(use_gradient_merge=False): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestGradientMergePass(unittest.TestCase): @@ -55,7 +55,7 @@ def init(self, engine): paddle.seed(2021) np.random.seed(2021) random.seed(2021) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_gradient_merge=False): diff --git a/test/auto_parallel/pipeline_scheduler_unittest.py b/test/auto_parallel/pipeline_scheduler_unittest.py index f19666d7aff5f..1617854ac446e 100644 --- a/test/auto_parallel/pipeline_scheduler_unittest.py +++ b/test/auto_parallel/pipeline_scheduler_unittest.py @@ -40,8 +40,8 @@ def apply_pass(schedule_mode="FThenB"): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class Test1F1BPass(unittest.TestCase): @@ -58,7 +58,7 @@ def init(self, engine): np.random.seed(2021) random.seed(2021) paddle.distributed.fleet.init(is_collective=True) - place = paddle.fluid.CUDAPlace(ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, schedule_mode="FThenB"): diff --git a/test/auto_parallel/random_control_unittest.py b/test/auto_parallel/random_control_unittest.py index addc384699885..0ed0857add7ef 100644 --- a/test/auto_parallel/random_control_unittest.py +++ b/test/auto_parallel/random_control_unittest.py @@ -44,8 +44,8 @@ def apply_pass(use_recompute=False, no_recompute_segments=[]): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestRandomControl(unittest.TestCase): @@ -62,7 +62,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_recompute=False, no_recompute_segments=[]): diff --git a/test/auto_parallel/recompute_pass_unittest.py b/test/auto_parallel/recompute_pass_unittest.py index 70350bd2d184f..3888ad9597329 100644 --- a/test/auto_parallel/recompute_pass_unittest.py +++ b/test/auto_parallel/recompute_pass_unittest.py @@ -34,8 +34,8 @@ def apply_pass(use_recompute=False, no_recompute_segments=[]): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestRecomputePass(unittest.TestCase): @@ -51,7 +51,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_recompute=False, no_recompute_segments=[]): diff --git a/test/auto_parallel/reshard_r_to_s.py b/test/auto_parallel/reshard_r_to_s.py index 690c42fa49283..6d69b24a8c97f 100644 --- a/test/auto_parallel/reshard_r_to_s.py +++ b/test/auto_parallel/reshard_r_to_s.py @@ -18,7 +18,7 @@ import paddle import paddle.distributed as dist -from paddle.fluid import core +from paddle.base import core class TestReshardRToS: diff --git a/test/auto_parallel/reshard_s_to_r.py b/test/auto_parallel/reshard_s_to_r.py index 90ba0cc655f36..d3aed1472ffe8 100644 --- a/test/auto_parallel/reshard_s_to_r.py +++ b/test/auto_parallel/reshard_s_to_r.py @@ -18,7 +18,7 @@ import paddle import paddle.distributed as dist -from paddle.fluid import core +from paddle.base import core class TestReshardSToR: diff --git a/test/auto_parallel/sharding_newexe.py b/test/auto_parallel/sharding_newexe.py index 48690f585cbeb..a8d30772cfbae 100644 --- a/test/auto_parallel/sharding_newexe.py +++ b/test/auto_parallel/sharding_newexe.py @@ -67,8 +67,8 @@ def apply_pass(use_sharding=False, use_amp=False, use_recompute=False): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestShardingStage2WithNewEXE(unittest.TestCase): @@ -82,7 +82,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine( diff --git a/test/auto_parallel/sharding_pass_unittest.py b/test/auto_parallel/sharding_pass_unittest.py index a39c455a3cfd0..b0d0a39df3b47 100644 --- a/test/auto_parallel/sharding_pass_unittest.py +++ b/test/auto_parallel/sharding_pass_unittest.py @@ -48,8 +48,8 @@ def apply_pass(use_sharding=False, stage=None): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) paddle.utils.unique_name.switch() diff --git a/test/auto_parallel/test_align_tool.py b/test/auto_parallel/test_align_tool.py index 500b11c78916c..85e5482ae5e0c 100644 --- a/test/auto_parallel/test_align_tool.py +++ b/test/auto_parallel/test_align_tool.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid, nn, optimizer, static +from paddle import base, nn, optimizer, static from paddle.distributed.auto_parallel.static.auto_align_tool import ( AutoAlignTool, ) @@ -29,8 +29,8 @@ paddle.enable_static() paddle.set_device("gpu") -startup_program = fluid.default_startup_program() -main_program = fluid.default_main_program() +startup_program = base.default_startup_program() +main_program = base.default_main_program() class MnistDataset(MNIST): @@ -50,7 +50,7 @@ def __len__(self): dataset = MnistDataset("train") place = paddle.CUDAPlace(0) -with fluid.program_guard(main_program, startup_program): +with base.program_guard(main_program, startup_program): inputs = static.data(name="image", shape=[-1, 1, 28, 28], dtype="float32") labels = static.data(name="label", shape=[-1, 1], dtype="int64") z = nn.Conv2D(1, 6, 3, 1, 1).forward(inputs) @@ -71,7 +71,7 @@ def __len__(self): class TestAlignTool(unittest.TestCase): def test_align_tool(self): - executor = fluid.Executor() + executor = base.Executor() executor.run(startup_program) align_tool = AutoAlignTool(main_program, 1, [losses.name]) diff --git a/test/auto_parallel/test_dist_assign.py b/test/auto_parallel/test_dist_assign.py index bb3558ae11a95..030a6b1513888 100644 --- a/test/auto_parallel/test_dist_assign.py +++ b/test/auto_parallel/test_dist_assign.py @@ -21,8 +21,8 @@ def make_program(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 4, 8], dtype='float32') y = paddle.static.data(name='y', shape=[4, 4, 8], dtype='float32') diff --git a/test/auto_parallel/test_dist_attr_v2.py b/test/auto_parallel/test_dist_attr_v2.py index 07041f478a091..563c8a19019ce 100644 --- a/test/auto_parallel/test_dist_attr_v2.py +++ b/test/auto_parallel/test_dist_attr_v2.py @@ -20,6 +20,7 @@ import paddle import paddle.nn.functional as F from paddle import nn, static +from paddle.base.core import OperatorDistAttr, TensorDistAttr from paddle.distributed import fleet from paddle.distributed.auto_parallel.process_mesh import ProcessMesh from paddle.distributed.auto_parallel.static.dist_context import ( @@ -33,7 +34,6 @@ _copy_dist_attr_to_cpp_for_graph, ) from paddle.distributed.fleet import auto -from paddle.fluid.core import OperatorDistAttr, TensorDistAttr paddle.enable_static() @@ -129,7 +129,7 @@ def get_program(): ) data_holder = [input, label] # dataloader - dataloader = paddle.fluid.io.DataLoader.from_generator( + dataloader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=4 * batch_size, iterable=False ) dataloader.set_batch_generator( diff --git a/test/auto_parallel/test_dist_context.py b/test/auto_parallel/test_dist_context.py index 695949fd698c0..3bc419482374e 100644 --- a/test/auto_parallel/test_dist_context.py +++ b/test/auto_parallel/test_dist_context.py @@ -114,7 +114,7 @@ def get_program(): ) data_holder = [input, label] # dataloader - dataloader = paddle.fluid.io.DataLoader.from_generator( + dataloader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=4 * batch_size, iterable=False ) dataloader.set_batch_generator( diff --git a/test/auto_parallel/test_dist_embedding.py b/test/auto_parallel/test_dist_embedding.py index bdfdc0ef32a78..691d9e4b3c3ba 100644 --- a/test/auto_parallel/test_dist_embedding.py +++ b/test/auto_parallel/test_dist_embedding.py @@ -23,8 +23,8 @@ def make_program_lookup_table_v1_mp_dp(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() block = main_program.global_block() with paddle.static.program_guard(main_program, start_program): src_ids = paddle.static.data( @@ -34,7 +34,7 @@ def make_program_lookup_table_v1_mp_dp(): emb_out = block.create_var(name='emb_out', dtype='float32') w = paddle.create_parameter( - attr=paddle.fluid.ParamAttr(name="emb_weight"), + attr=paddle.base.ParamAttr(name="emb_weight"), shape=[64, 128], dtype='float32', is_bias=False, diff --git a/test/auto_parallel/test_dist_matmul.py b/test/auto_parallel/test_dist_matmul.py index 77c15942709c2..a60f942db104d 100644 --- a/test/auto_parallel/test_dist_matmul.py +++ b/test/auto_parallel/test_dist_matmul.py @@ -15,9 +15,9 @@ import unittest import paddle +from paddle.base import program_guard +from paddle.base.backward import append_backward from paddle.distributed.fleet import auto -from paddle.fluid import program_guard -from paddle.fluid.backward import append_backward paddle.enable_static() @@ -77,8 +77,8 @@ def init_y_col(trans_y): def matmul_dp2mp2(init_x, init_y, trans_x, trans_y): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = init_x(trans_x) y = init_y(trans_y) @@ -90,8 +90,8 @@ def matmul_dp2mp2(init_x, init_y, trans_x, trans_y): def matmulv2_dp2mp2(init_x, init_y, trans_x, trans_y): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = init_x(trans_x) y = init_y(trans_y) diff --git a/test/auto_parallel/test_dist_op_cost.py b/test/auto_parallel/test_dist_op_cost.py index b5ac224987351..6477d8646bca6 100644 --- a/test/auto_parallel/test_dist_op_cost.py +++ b/test/auto_parallel/test_dist_op_cost.py @@ -16,14 +16,14 @@ import unittest import paddle +from paddle.base import program_guard +from paddle.base.backward import append_backward from paddle.distributed.auto_parallel.static.cluster import Cluster from paddle.distributed.auto_parallel.static.operators.common import ( get_distributed_operator_impl_container, is_elementwise_op, ) from paddle.distributed.fleet import auto -from paddle.fluid import program_guard -from paddle.fluid.backward import append_backward paddle.enable_static() diff --git a/test/auto_parallel/test_dist_pnorm.py b/test/auto_parallel/test_dist_pnorm.py index 623114208150c..004f05a56168b 100644 --- a/test/auto_parallel/test_dist_pnorm.py +++ b/test/auto_parallel/test_dist_pnorm.py @@ -15,16 +15,16 @@ import unittest import paddle +from paddle.base import program_guard +from paddle.base.backward import append_backward from paddle.distributed.fleet import auto -from paddle.fluid import program_guard -from paddle.fluid.backward import append_backward paddle.enable_static() def make_program_dp2_axis_None(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') x.stop_gradient = False @@ -36,8 +36,8 @@ def make_program_dp2_axis_None(): def make_program_dp2_axis_0(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') x.stop_gradient = False @@ -49,8 +49,8 @@ def make_program_dp2_axis_0(): def make_program_dp2_axis_1(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') x.stop_gradient = False @@ -62,8 +62,8 @@ def make_program_dp2_axis_1(): def make_program_serial(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') x.stop_gradient = False diff --git a/test/auto_parallel/test_dist_reshape.py b/test/auto_parallel/test_dist_reshape.py index 8df5f25edddb4..adeb8ee906f0b 100644 --- a/test/auto_parallel/test_dist_reshape.py +++ b/test/auto_parallel/test_dist_reshape.py @@ -21,8 +21,8 @@ def make_program_dp2(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 4, 8], dtype='float32') x.stop_gradient = False diff --git a/test/auto_parallel/test_dist_scale.py b/test/auto_parallel/test_dist_scale.py index 0d0b591d84d40..e367e2b579494 100644 --- a/test/auto_parallel/test_dist_scale.py +++ b/test/auto_parallel/test_dist_scale.py @@ -21,8 +21,8 @@ def make_program(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 4, 8], dtype='float32') x.stop_gradient = False diff --git a/test/auto_parallel/test_dist_shape.py b/test/auto_parallel/test_dist_shape.py index 5d510f0a586d4..e048af0680153 100644 --- a/test/auto_parallel/test_dist_shape.py +++ b/test/auto_parallel/test_dist_shape.py @@ -21,8 +21,8 @@ def make_program(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 4, 8], dtype='float32') x.stop_gradient = False diff --git a/test/auto_parallel/test_dist_slice.py b/test/auto_parallel/test_dist_slice.py index e94dcf32f7bf9..211c3f5a2c9fe 100644 --- a/test/auto_parallel/test_dist_slice.py +++ b/test/auto_parallel/test_dist_slice.py @@ -21,8 +21,8 @@ def make_program_dp2(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') auto.shard_tensor( @@ -38,8 +38,8 @@ def make_program_dp2(): def make_program_serial(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 5, 6], dtype='float32') auto.shard_tensor( diff --git a/test/auto_parallel/test_dist_split.py b/test/auto_parallel/test_dist_split.py index 9b2bcf9c9de07..131d6d4d845f9 100644 --- a/test/auto_parallel/test_dist_split.py +++ b/test/auto_parallel/test_dist_split.py @@ -21,8 +21,8 @@ def make_program_dp2(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 12, 16], dtype='float32') x.stop_gradient = False diff --git a/test/auto_parallel/test_fp16_assign.py b/test/auto_parallel/test_fp16_assign.py index a7257cb025470..f2d50708c6a0f 100644 --- a/test/auto_parallel/test_fp16_assign.py +++ b/test/auto_parallel/test_fp16_assign.py @@ -23,8 +23,8 @@ def make_program(): - main_program = paddle.fluid.Program() - start_program = paddle.fluid.Program() + main_program = paddle.base.Program() + start_program = paddle.base.Program() with paddle.static.program_guard(main_program, start_program): x = paddle.static.data(name='x', shape=[4, 6, 8], dtype='float32') y = paddle.static.data(name='y', shape=[4, 6, 6], dtype='float32') diff --git a/test/auto_parallel/test_fused_linear_pass.py b/test/auto_parallel/test_fused_linear_pass.py index aa8a303cd2f91..575b83d0df5fb 100644 --- a/test/auto_parallel/test_fused_linear_pass.py +++ b/test/auto_parallel/test_fused_linear_pass.py @@ -34,8 +34,8 @@ def apply_pass(use_fused_passes=False, fused_passes_list=[]): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestFusedLinearPass(unittest.TestCase): @@ -51,7 +51,7 @@ def init(self, engine): paddle.seed(2021) np.random.seed(2021) random.seed(2021) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_fused_passes=False, fused_passes_list=[]): diff --git a/test/auto_parallel/test_interface.py b/test/auto_parallel/test_interface.py index 5ea4209a6253b..38a3c3bb78daa 100644 --- a/test/auto_parallel/test_interface.py +++ b/test/auto_parallel/test_interface.py @@ -110,7 +110,7 @@ def test_api(self): with ProcessMesh(process_mesh1.mesh, process_mesh1.dim_names): linear0, linear1, linear0_out, gelu_out, linear1_out = mlp(input) - default_program = paddle.fluid.default_main_program() + default_program = paddle.base.default_main_program() default_dist_context = get_default_distributed_context() self.assertEqual(len(default_program.blocks[0].ops), 5) diff --git a/test/auto_parallel/test_pass_base_list.py b/test/auto_parallel/test_pass_base_list.py index f72aceb8bff6e..da7df4ad6fc85 100644 --- a/test/auto_parallel/test_pass_base_list.py +++ b/test/auto_parallel/test_pass_base_list.py @@ -34,8 +34,8 @@ def apply_pass(use_fused_passes=False, fused_passes_list=[]): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestFusedPassBaseList(unittest.TestCase): @@ -51,7 +51,7 @@ def init(self, engine): paddle.seed(2021) np.random.seed(2021) random.seed(2021) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_fused_passes=False, fused_passes_list=[]): diff --git a/test/auto_parallel/test_pass_bf16.py b/test/auto_parallel/test_pass_bf16.py index 411cee39eca54..fd6f6daf41cd1 100644 --- a/test/auto_parallel/test_pass_bf16.py +++ b/test/auto_parallel/test_pass_bf16.py @@ -19,8 +19,8 @@ import paddle from paddle import nn +from paddle.base import core from paddle.distributed.fleet import auto -from paddle.fluid import core from paddle.static import InputSpec from paddle.static.amp.bf16.amp_utils import _valid_types from paddle.static.amp.fp16_utils import find_true_prev_op @@ -57,8 +57,8 @@ def __len__(self): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class Model(nn.Layer): @@ -90,7 +90,7 @@ def init(self, engine): paddle.seed(2021) np.random.seed(2021) random.seed(2021) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine(self, use_bf16=False): diff --git a/test/auto_parallel/test_prim_dist_op.py b/test/auto_parallel/test_prim_dist_op.py index 3b7351b2dd4ab..99e12b2099874 100644 --- a/test/auto_parallel/test_prim_dist_op.py +++ b/test/auto_parallel/test_prim_dist_op.py @@ -15,6 +15,7 @@ import unittest import paddle +from paddle.base.layer_helper import LayerHelper from paddle.distributed.auto_parallel.static.completion import Completer from paddle.distributed.auto_parallel.static.dist_context import ( DistributedContext, @@ -23,7 +24,6 @@ from paddle.distributed.auto_parallel.static.partitioner import Partitioner from paddle.distributed.auto_parallel.static.utils import set_var_dist_attr from paddle.distributed.fleet import auto -from paddle.fluid.layer_helper import LayerHelper from paddle.incubate.autograd import enable_prim paddle.enable_static() diff --git a/test/auto_parallel/test_process_mesh.py b/test/auto_parallel/test_process_mesh.py index d4b91a5dcc345..51812a31ceebc 100644 --- a/test/auto_parallel/test_process_mesh.py +++ b/test/auto_parallel/test_process_mesh.py @@ -135,7 +135,7 @@ def test_context_manager(self): with ProcessMesh(mesh, ["d"]): out = mlp(input) - default_program = paddle.fluid.default_main_program() + default_program = paddle.base.default_main_program() default_dist_context = get_default_distributed_context() for block in default_program.blocks: @@ -196,11 +196,11 @@ def test_merge_process_meshes(self): self.assertEqual(merged_process_mesh, ProcessMesh([0, 1, 2, 3, 4, 5])) merged_process_mesh = merge_process_meshes( - [process_mesh1, paddle.fluid.core.ProcessMesh()] + [process_mesh1, paddle.base.core.ProcessMesh()] ) self.assertEqual(merged_process_mesh, ProcessMesh([0, 1, 2, 3, 4, 5])) merged_process_mesh = merge_process_meshes( - [paddle.fluid.core.ProcessMesh(), process_mesh1] + [paddle.base.core.ProcessMesh(), process_mesh1] ) self.assertEqual(merged_process_mesh, ProcessMesh([0, 1, 2, 3, 4, 5])) diff --git a/test/auto_parallel/test_selective_recompute.py b/test/auto_parallel/test_selective_recompute.py index 21b11abbe84b8..5099a6adefa4f 100644 --- a/test/auto_parallel/test_selective_recompute.py +++ b/test/auto_parallel/test_selective_recompute.py @@ -74,8 +74,8 @@ def apply_pass(use_recompute=False, no_recompute_segments=[]): def reset_prog(): - paddle.fluid.framework.switch_main_program(paddle.static.Program()) - paddle.fluid.framework.switch_startup_program(paddle.static.Program()) + paddle.base.framework.switch_main_program(paddle.static.Program()) + paddle.base.framework.switch_startup_program(paddle.static.Program()) class TestRecomputePassWithRecomputeAPI(unittest.TestCase): @@ -91,7 +91,7 @@ def init(self, engine): paddle.seed(2022) np.random.seed(2022) random.seed(2022) - place = paddle.fluid.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) + place = paddle.base.CUDAPlace(paddle.distributed.ParallelEnv().dev_id) engine._executor = paddle.static.Executor(place) def get_engine( diff --git a/test/auto_parallel/test_serialization.py b/test/auto_parallel/test_serialization.py index adc3f3e337e3c..d246db2ed8884 100644 --- a/test/auto_parallel/test_serialization.py +++ b/test/auto_parallel/test_serialization.py @@ -19,6 +19,8 @@ import paddle import paddle.nn.functional as F from paddle import nn, static +from paddle.base.core import TensorDistAttr +from paddle.base.framework import Program from paddle.distributed import fleet from paddle.distributed.auto_parallel.static.dist_context import ( DistributedContext, @@ -26,8 +28,6 @@ ) from paddle.distributed.auto_parallel.static.process_mesh_v2 import ProcessMesh from paddle.distributed.fleet import auto -from paddle.fluid.core import TensorDistAttr -from paddle.fluid.framework import Program paddle.enable_static() @@ -123,7 +123,7 @@ def get_program(): ) data_holder = [input, label] # dataloader - dataloader = paddle.fluid.io.DataLoader.from_generator( + dataloader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=4 * batch_size, iterable=False ) dataloader.set_batch_generator( diff --git a/test/auto_parallel/test_shard_tensor_api.py b/test/auto_parallel/test_shard_tensor_api.py index af96c715131c6..86be83b41031a 100644 --- a/test/auto_parallel/test_shard_tensor_api.py +++ b/test/auto_parallel/test_shard_tensor_api.py @@ -16,10 +16,10 @@ import paddle import paddle.distributed as dist +from paddle.base.dygraph.base import switch_to_static_graph from paddle.distributed.auto_parallel.static.dist_context import ( get_default_distributed_context, ) -from paddle.fluid.dygraph.base import switch_to_static_graph class TestDistAttrBasic(unittest.TestCase): diff --git a/test/auto_parallel/test_while_op_completion.py b/test/auto_parallel/test_while_op_completion.py index 6cdf31daec505..16ca6a7ae4a60 100644 --- a/test/auto_parallel/test_while_op_completion.py +++ b/test/auto_parallel/test_while_op_completion.py @@ -149,7 +149,7 @@ def get_program(): ) data_holder = [input, label] # dataloader - dataloader = paddle.fluid.io.DataLoader.from_generator( + dataloader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=4 * batch_size, iterable=False ) dataloader.set_batch_generator( diff --git a/test/auto_parallel/test_while_op_partition.py b/test/auto_parallel/test_while_op_partition.py index fd8edc6eba7c1..362f0c0904320 100644 --- a/test/auto_parallel/test_while_op_partition.py +++ b/test/auto_parallel/test_while_op_partition.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid, nn, static +from paddle import base, nn, static from paddle.distributed import fleet from paddle.distributed.auto_parallel.static.completion import Completer from paddle.distributed.auto_parallel.static.dist_context import ( @@ -111,7 +111,7 @@ def get_program(): train_program = static.Program() start_program = static.Program() - with fluid.program_guard(train_program, start_program): + with base.program_guard(train_program, start_program): # 循环计数器 i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) auto.shard_tensor(i, _g_process_mesh, [None]) @@ -134,7 +134,7 @@ def get_program(): data_holder = [input, label] # dataloader - dataloader = fluid.io.DataLoader.from_generator( + dataloader = base.io.DataLoader.from_generator( feed_list=data_holder, capacity=4 * batch_size, iterable=False ) dataloader.set_batch_generator( diff --git a/test/autograd/test_autograd_functional_dynamic.py b/test/autograd/test_autograd_functional_dynamic.py index 021e63728abbe..02c4e61748d0a 100644 --- a/test/autograd/test_autograd_functional_dynamic.py +++ b/test/autograd/test_autograd_functional_dynamic.py @@ -212,7 +212,7 @@ def test_all_cases(self): def test_input_single_tensor(self): self.assertIsInstance( paddle.incubate.autograd.vjp(paddle.tanh, paddle.rand((3, 4)))[1], - paddle.fluid.framework.Variable, + paddle.base.framework.Variable, ) diff --git a/test/autograd/test_autograd_functional_static.py b/test/autograd/test_autograd_functional_static.py index 51e694957a346..127cb93a4cbc6 100644 --- a/test/autograd/test_autograd_functional_static.py +++ b/test/autograd/test_autograd_functional_static.py @@ -23,7 +23,7 @@ import utils import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -252,10 +252,10 @@ class TestJacobianFloat32(unittest.TestCase): @classmethod def setUpClass(self): paddle.enable_static() - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.dtype = 'float32' self.np_dtype = np.float32 prepare_data(self, all_data_shapes, self.dtype) @@ -269,9 +269,9 @@ def setUpClass(self): self.atol = 1e-2 def run_test_by_fullmatrix(self, pd_f, np_f, inps, batch=False): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): xs = make_tensors(inps) JJ = paddle.incubate.autograd.Jacobian(pd_f, xs, is_batched=batch) if batch: @@ -279,7 +279,7 @@ def run_test_by_fullmatrix(self, pd_f, np_f, inps, batch=False): else: nrow, ncol = JJ.shape full_jacobian = JJ[:] - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) if isinstance(inps, list): feeds = {f'x{i}': x for i, x in enumerate(inps)} @@ -299,9 +299,9 @@ def run_test_by_fullmatrix(self, pd_f, np_f, inps, batch=False): ) def run_test_by_rows(self, pd_f, np_f, inps, batch=False): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): xs = make_tensors(inps) JJ = paddle.incubate.autograd.Jacobian(pd_f, xs, is_batched=batch) if batch: @@ -311,7 +311,7 @@ def run_test_by_rows(self, pd_f, np_f, inps, batch=False): nrow, ncol = JJ.shape rows = [JJ[i, :] for i in range(nrow)] - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) if isinstance(inps, list): feeds = {f'x{i}': x for i, x in enumerate(inps)} @@ -325,9 +325,9 @@ def run_test_by_rows(self, pd_f, np_f, inps, batch=False): ) def run_test_by_entries(self, pd_f, np_f, inps, batch=False): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): xs = make_tensors(inps) JJ = paddle.incubate.autograd.Jacobian(pd_f, xs, is_batched=batch) if batch: @@ -338,7 +338,7 @@ def run_test_by_entries(self, pd_f, np_f, inps, batch=False): else: nrow, ncol = JJ.shape entries = [JJ[i, j] for i in range(nrow) for j in range(ncol)] - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) if isinstance(inps, list): feeds = {f'x{i}': x for i, x in enumerate(inps)} @@ -408,10 +408,10 @@ class TestJacobianFloat64(TestJacobianFloat32): @classmethod def setUpClass(self): paddle.enable_static() - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.dtype = 'float64' prepare_data(self, all_data_shapes, self.dtype) self.eps = ( @@ -429,10 +429,10 @@ class TestHessianFloat32(unittest.TestCase): @classmethod def setUpClass(self): paddle.enable_static() - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.dtype = 'float32' prepare_data(self, all_data_shapes, self.dtype) self.eps = ( @@ -450,14 +450,14 @@ def setUpClass(self): ) def run_test_by_fullmatrix(self, pd_f, inps, np_hess, batch=False): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): xs = make_tensors(inps) HH = paddle.incubate.autograd.Hessian(pd_f, xs, is_batched=batch) nrow, ncol = HH.shape full_hessian = HH[:] - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) if isinstance(inps, list): feeds = {f'x{i}': x for i, x in enumerate(inps)} @@ -484,10 +484,10 @@ class TestHessianFloat64(TestHessianFloat32): @classmethod def setUpClass(self): paddle.enable_static() - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.dtype = 'float64' prepare_data(self, all_data_shapes, self.dtype) self.eps = ( diff --git a/test/autograd/test_jvp_and_transpose.py b/test/autograd/test_jvp_and_transpose.py index 0c22c2db4490d..b37fd4e201a4e 100644 --- a/test/autograd/test_jvp_and_transpose.py +++ b/test/autograd/test_jvp_and_transpose.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.incubate.autograd.primrules import _jvp, _transpose paddle.enable_static() diff --git a/test/autograd/test_orig2prim.py b/test/autograd/test_orig2prim.py index 8a42255d711ee..22310e0c8d4a4 100644 --- a/test/autograd/test_orig2prim.py +++ b/test/autograd/test_orig2prim.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.incubate.autograd.primrules import _orig2prim paddle.enable_static() diff --git a/test/autograd/test_prim2orig.py b/test/autograd/test_prim2orig.py index 2d9427b2e8b4f..d2c5431ee2f36 100644 --- a/test/autograd/test_prim2orig.py +++ b/test/autograd/test_prim2orig.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.incubate.autograd.primrules import _prim2orig paddle.enable_static() diff --git a/test/autograd/test_primapi.py b/test/autograd/test_primapi.py index 0095ab0233d55..7bbe4e4476046 100644 --- a/test/autograd/test_primapi.py +++ b/test/autograd/test_primapi.py @@ -24,7 +24,7 @@ import utils import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi, primx @@ -260,8 +260,8 @@ def without_program_guard(): ys_grad = paddle.incubate.autograd.forward_grad( ys, static_xs, static_v ) - sp = paddle.fluid.framework.default_startup_program() - mp = paddle.fluid.framework.default_main_program() + sp = paddle.base.framework.default_startup_program() + mp = paddle.base.framework.default_main_program() exe = paddle.static.Executor() exe.run(sp) out = exe.run(mp, feed=feed, fetch_list=ys_grad) @@ -311,8 +311,8 @@ def without_program_guard(): else self.fun(static_xs) ) xs_grad = paddle.incubate.autograd.grad(ys, static_xs, static_v) - sp = paddle.fluid.framework.default_startup_program() - mp = paddle.fluid.framework.default_main_program() + sp = paddle.base.framework.default_startup_program() + mp = paddle.base.framework.default_main_program() exe = paddle.static.Executor() exe.run(sp) out = exe.run(mp, feed=feed, fetch_list=xs_grad) diff --git a/test/autograd/utils.py b/test/autograd/utils.py index 6b2ea8437228f..f077f520deff6 100644 --- a/test/autograd/utils.py +++ b/test/autograd/utils.py @@ -31,7 +31,7 @@ def _product(t): def _get_item(t, idx): assert isinstance( - t, paddle.fluid.framework.Variable + t, paddle.base.framework.Variable ), "The first argument t must be Tensor." assert isinstance( idx, int @@ -42,7 +42,7 @@ def _get_item(t, idx): def _set_item(t, idx, value): assert isinstance( - t, paddle.fluid.framework.Variable + t, paddle.base.framework.Variable ), "The first argument t must be Tensor." assert isinstance( idx, int diff --git a/test/book/notest_understand_sentiment.py b/test/book/notest_understand_sentiment.py index 606c0f2542d85..250b3f2dc679c 100644 --- a/test/book/notest_understand_sentiment.py +++ b/test/book/notest_understand_sentiment.py @@ -25,7 +25,7 @@ import nets import paddle -from paddle import fluid +from paddle import base def convolution_net( @@ -93,12 +93,12 @@ def train( ), batch_size=BATCH_SIZE, ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[data, label], place=place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[data, label], place=place) def train_loop(main_program): - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) for pass_id in range(PASS_NUM): for data in train_data(): @@ -119,7 +119,7 @@ def train_loop(main_program): raise AssertionError(f"Cost is too large for {net_method.__name__}") if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -148,11 +148,11 @@ def infer(word_dict, use_cuda, save_dirname=None): if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -178,7 +178,7 @@ def infer(word_dict, use_cuda, save_dirname=None): recursive_seq_lens = [[3, 4, 2]] base_shape = [1] # The range of random integers is [low, high] - tensor_words = fluid.create_random_int_lodtensor( + tensor_words = base.create_random_int_lodtensor( recursive_seq_lens, base_shape, place, low=0, high=word_dict_len - 1 ) @@ -198,7 +198,7 @@ def infer(word_dict, use_cuda, save_dirname=None): def main(word_dict, net_method, use_cuda, parallel=False, save_dirname=None): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return train( @@ -218,11 +218,11 @@ def setUpClass(cls): @contextlib.contextmanager def new_program_scope(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield def test_conv_cpu(self): diff --git a/test/book/test_fit_a_line.py b/test/book/test_fit_a_line.py index 0d1e77ce7fcb1..5e51999446725 100644 --- a/test/book/test_fit_a_line.py +++ b/test/book/test_fit_a_line.py @@ -23,7 +23,7 @@ import numpy import paddle -from paddle import fluid +from paddle import base from paddle.static import amp paddle.enable_static() @@ -85,7 +85,7 @@ def train(use_cuda, save_dirname, is_local, use_bf16, pure_bf16): use_pure_bf16=pure_bf16, ) sgd_optimizer.minimize( - avg_cost, startup_program=fluid.default_startup_program() + avg_cost, startup_program=base.default_startup_program() ) BATCH_SIZE = 20 @@ -95,12 +95,12 @@ def train(use_cuda, save_dirname, is_local, use_bf16, pure_bf16): batch_size=BATCH_SIZE, ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) def train_loop(main_program): - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe.run(base.default_startup_program()) test_prog = main_program.clone(for_test=True) if pure_bf16: sgd_optimizer.amp_init( @@ -132,7 +132,7 @@ def train_loop(main_program): ) if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -161,11 +161,11 @@ def infer(use_cuda, save_dirname=None, use_bf16=False): if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -210,10 +210,10 @@ def infer(use_cuda, save_dirname=None, use_bf16=False): def main(use_cuda, is_local=True, use_bf16=False, pure_bf16=False): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return - if use_bf16 and not fluid.core.is_compiled_with_mkldnn(): + if use_bf16 and not base.core.is_compiled_with_mkldnn(): return temp_dir = tempfile.TemporaryDirectory() @@ -228,11 +228,11 @@ def main(use_cuda, is_local=True, use_bf16=False, pure_bf16=False): class TestFitALineBase(unittest.TestCase): @contextlib.contextmanager def program_scope_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield @@ -247,7 +247,7 @@ def test_cuda(self): @unittest.skipIf( - not fluid.core.supports_bfloat16(), "place does not support BF16 evaluation" + not base.core.supports_bfloat16(), "place does not support BF16 evaluation" ) class TestFitALineBF16(TestFitALineBase): def test_bf16(self): diff --git a/test/book/test_image_classification.py b/test/book/test_image_classification.py index bb8c45ea9011c..3e8f771983cac 100644 --- a/test/book/test_image_classification.py +++ b/test/book/test_image_classification.py @@ -26,7 +26,7 @@ import nets import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -130,7 +130,7 @@ def train(net_type, use_cuda, save_dirname, is_local): acc = paddle.static.accuracy(input=predict, label=label) # Test program - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) optimizer = paddle.optimizer.Adam(learning_rate=0.001) optimizer.minimize(avg_cost) @@ -149,12 +149,12 @@ def train(net_type, use_cuda, save_dirname, is_local): paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(place=place, feed_list=[images, label]) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + feeder = base.DataFeeder(place=place, feed_list=[images, label]) def train_loop(main_program): - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) loss = 0.0 for pass_id in range(PASS_NUM): for batch_id, data in enumerate(train_reader()): @@ -194,7 +194,7 @@ def train_loop(main_program): return if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -223,11 +223,11 @@ def infer(use_cuda, save_dirname=None): if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -266,7 +266,7 @@ def infer(use_cuda, save_dirname=None): def main(net_type, use_cuda, is_local=True): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return # Directory for saving the trained model @@ -299,11 +299,11 @@ def test_resnet_cpu(self): @contextlib.contextmanager def scope_prog_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield diff --git a/test/book/test_recognize_digits.py b/test/book/test_recognize_digits.py index aff66af640031..643aaae6ce6d9 100644 --- a/test/book/test_recognize_digits.py +++ b/test/book/test_recognize_digits.py @@ -24,8 +24,8 @@ import nets import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -79,7 +79,7 @@ def train( params_filename=None, is_local=True, ): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return img = paddle.static.data(name='img', shape=[-1, 1, 28, 28], dtype='float32') label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') @@ -94,14 +94,14 @@ def train( else: prediction, avg_loss, acc = net_conf(img, label) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) optimizer = paddle.optimizer.Adam(learning_rate=0.001) optimizer.minimize(avg_loss) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) train_reader = paddle.batch( paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), @@ -110,10 +110,10 @@ def train( test_reader = paddle.batch( paddle.dataset.mnist.test(), batch_size=BATCH_SIZE ) - feeder = fluid.DataFeeder(feed_list=[img, label], place=place) + feeder = base.DataFeeder(feed_list=[img, label], place=place) def train_loop(main_program): - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) PASS_NUM = 100 for pass_id in range(PASS_NUM): @@ -165,7 +165,7 @@ def train_loop(main_program): raise AssertionError("Loss of recognize digits is too large") if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -196,11 +196,11 @@ def infer( if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be feeded # data using feed operators), and the fetch_targets (variables that @@ -269,11 +269,11 @@ class TestRecognizeDigits(unittest.TestCase): def inject_test_method(use_cuda, parallel, nn_type, combine): def __impl__(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): main(use_cuda, parallel, nn_type, combine) fn = 'test_{}_{}_{}_{}'.format( diff --git a/test/book/test_recommender_system.py b/test/book/test_recommender_system.py index 8912467413be1..7a4a70be105d5 100644 --- a/test/book/test_recommender_system.py +++ b/test/book/test_recommender_system.py @@ -24,9 +24,9 @@ import nets import paddle -from paddle import fluid -from paddle.fluid import framework -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import framework +from paddle.base.executor import Executor from paddle.optimizer import SGD paddle.enable_static() @@ -186,12 +186,12 @@ def train(use_cuda, save_dirname, is_local=True): scale_infer, avg_cost = model() # test program - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) sgd_optimizer = SGD(learning_rate=0.2) sgd_optimizer.minimize(avg_cost) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() exe = Executor(place) @@ -233,7 +233,7 @@ def train_loop(main_program): main_program.global_block().var(var_name) for var_name in feed_infer_order ] - feeder = fluid.DataFeeder(feed_list, place) + feeder = base.DataFeeder(feed_list, place) PASS_NUM = 100 for pass_id in range(PASS_NUM): @@ -273,7 +273,7 @@ def train_loop(main_program): sys.exit("got NaN loss, training failed.") if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -302,11 +302,11 @@ def infer(use_cuda, save_dirname=None): if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -328,27 +328,27 @@ def infer(use_cuda, save_dirname=None): # Correspondingly, recursive_sequence_lengths = [[3, 2]] contains one # level of detail info, indicating that `data` consists of two sequences # of length 3 and 2, respectively. - user_id = fluid.create_lod_tensor([[np.int64(1)]], [[1]], place) + user_id = base.create_lod_tensor([[np.int64(1)]], [[1]], place) assert feed_target_names[1] == "gender_id" - gender_id = fluid.create_lod_tensor([[np.int64(1)]], [[1]], place) + gender_id = base.create_lod_tensor([[np.int64(1)]], [[1]], place) assert feed_target_names[2] == "age_id" - age_id = fluid.create_lod_tensor([[np.int64(0)]], [[1]], place) + age_id = base.create_lod_tensor([[np.int64(0)]], [[1]], place) assert feed_target_names[3] == "job_id" - job_id = fluid.create_lod_tensor([[np.int64(10)]], [[1]], place) + job_id = base.create_lod_tensor([[np.int64(10)]], [[1]], place) assert feed_target_names[4] == "movie_id" - movie_id = fluid.create_lod_tensor([[np.int64(783)]], [[1]], place) + movie_id = base.create_lod_tensor([[np.int64(783)]], [[1]], place) assert feed_target_names[5] == "category_id" - category_id = fluid.create_lod_tensor( + category_id = base.create_lod_tensor( [np.array([10, 8, 9], dtype='int64')], [[3]], place ) assert feed_target_names[6] == "movie_title" - movie_title = fluid.create_lod_tensor( + movie_title = base.create_lod_tensor( [np.array([1069, 4140, 2923, 710, 988], dtype='int64')], [[5]], place, @@ -374,7 +374,7 @@ def infer(use_cuda, save_dirname=None): def main(use_cuda): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return # Directory for saving the inference model diff --git a/test/book/test_word2vec_book.py b/test/book/test_word2vec_book.py index f971db41dbbc3..bfa4a05b5e160 100644 --- a/test/book/test_word2vec_book.py +++ b/test/book/test_word2vec_book.py @@ -21,18 +21,18 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.enable_static() def get_place(target): if target == "cuda": - return fluid.CUDAPlace(0) + return base.CUDAPlace(0) elif target == "xpu": - return fluid.XPUPlace(0) + return base.XPUPlace(0) elif target == "cpu": - return fluid.CPUPlace() + return base.CPUPlace() else: raise ValueError( "Target `{}` is not on the support list: `cuda`, `xpu` and `cpu`.".format( @@ -134,21 +134,21 @@ def __network__(words): use_pure_bf16=pure_bf16, ) - sgd_optimizer.minimize(avg_cost, fluid.default_startup_program()) + sgd_optimizer.minimize(avg_cost, base.default_startup_program()) train_reader = paddle.batch( paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE ) place = get_place(target) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder( + exe = base.Executor(place) + feeder = base.DataFeeder( feed_list=[first_word, second_word, third_word, forth_word, next_word], place=place, ) def train_loop(main_program): - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) if pure_bf16: sgd_optimizer.amp_init(exe.place) @@ -172,7 +172,7 @@ def train_loop(main_program): raise AssertionError(f"Cost is too large {avg_cost_np[0]:2.2}") if is_local: - train_loop(fluid.default_main_program()) + train_loop(base.default_main_program()) else: port = os.getenv("PADDLE_PSERVER_PORT", "6174") pserver_ips = os.getenv("PADDLE_PSERVER_IPS") # ip,ip... @@ -202,9 +202,9 @@ def infer(target, save_dirname=None): return place = get_place(target) - exe = fluid.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + exe = base.Executor(place) + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -229,16 +229,16 @@ def infer(target, save_dirname=None): recursive_seq_lens = [[1]] base_shape = [1] # The range of random integers is [low, high] - first_word = fluid.create_random_int_lodtensor( + first_word = base.create_random_int_lodtensor( recursive_seq_lens, base_shape, place, low=0, high=dict_size - 1 ) - second_word = fluid.create_random_int_lodtensor( + second_word = base.create_random_int_lodtensor( recursive_seq_lens, base_shape, place, low=0, high=dict_size - 1 ) - third_word = fluid.create_random_int_lodtensor( + third_word = base.create_random_int_lodtensor( recursive_seq_lens, base_shape, place, low=0, high=dict_size - 1 ) - fourth_word = fluid.create_random_int_lodtensor( + fourth_word = base.create_random_int_lodtensor( recursive_seq_lens, base_shape, place, low=0, high=dict_size - 1 ) @@ -262,17 +262,17 @@ def infer(target, save_dirname=None): ) def to_infer_tensor(lod_tensor): - infer_tensor = fluid.core.PaddleTensor() + infer_tensor = base.core.PaddleTensor() infer_tensor.lod = lod_tensor.lod() - infer_tensor.data = fluid.core.PaddleBuf(np.array(lod_tensor)) + infer_tensor.data = base.core.PaddleBuf(np.array(lod_tensor)) infer_tensor.shape = lod_tensor.shape() - infer_tensor.dtype = fluid.core.PaddleDType.INT64 + infer_tensor.dtype = base.core.PaddleDType.INT64 return infer_tensor infer_inputs = [first_word, second_word, third_word, fourth_word] infer_inputs = [to_infer_tensor(t) for t in infer_inputs] - infer_config = fluid.core.NativeConfig() + infer_config = base.core.NativeConfig() infer_config.prog_file = save_dirname + ".pdmodel" infer_config.param_file = save_dirname + ".pdiparams" if target == "cuda": @@ -281,7 +281,7 @@ def to_infer_tensor(lod_tensor): infer_config.fraction_of_gpu_memory = 0.15 elif target == "xpu": infer_config.use_xpu = True - compiled_program = fluid.compiler.CompiledProgram(inference_program) + compiled_program = base.compiler.CompiledProgram(inference_program) compiled_program._with_inference_optimize(infer_config) assert compiled_program._is_inference is True infer_outputs = exe.run(compiled_program, feed=infer_inputs) @@ -292,12 +292,12 @@ def to_infer_tensor(lod_tensor): def main(target, is_sparse, is_parallel, use_bf16, pure_bf16): - if target == "cuda" and not fluid.core.is_compiled_with_cuda(): + if target == "cuda" and not base.core.is_compiled_with_cuda(): return - if target == "xpu" and not fluid.core.is_compiled_with_xpu(): + if target == "xpu" and not base.core.is_compiled_with_xpu(): return - if use_bf16 and not fluid.core.is_compiled_with_mkldnn(): + if use_bf16 and not base.core.is_compiled_with_mkldnn(): return temp_dir = tempfile.TemporaryDirectory() @@ -349,15 +349,15 @@ def inject_test_method( ) def __impl__(*args, **kwargs): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): main(target, is_sparse, is_parallel, use_bf16, pure_bf16) if ( - not fluid.core.is_compiled_with_cuda() or target == "cuda" + not base.core.is_compiled_with_cuda() or target == "cuda" ) and is_sparse: fn = __impl__ else: diff --git a/test/cinn/op_mappers/op_mapper_test.py b/test/cinn/op_mappers/op_mapper_test.py index 2e277bd879b78..584c26488484a 100644 --- a/test/cinn/op_mappers/op_mapper_test.py +++ b/test/cinn/op_mappers/op_mapper_test.py @@ -23,7 +23,7 @@ from cinn.frontend import PaddleModelConvertor import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper from paddle.static import Variable as PaddleVariable sys.path.append("/work/dev_CINN/build/python/tests") @@ -426,7 +426,7 @@ def paddleddtype2nptype(dtype): paddle.int64: "int64", paddle.uint8: "uint8", paddle.bool: "bool", - paddle.fluid.core.VarDesc.VarType.RAW: "unk", + paddle.base.core.VarDesc.VarType.RAW: "unk", } assert dtype in switch_map, str(dtype) + " not support in CINN" return switch_map[dtype] @@ -444,7 +444,7 @@ def nptype2paddledtype(dtype): "uint8": paddle.uint8, "bool": paddle.bool, # The paddle's phi::DataType::UNDEFINED is mapped into ProtoDataType::RAW, - "unk": paddle.fluid.core.VarDesc.VarType.RAW, + "unk": paddle.base.core.VarDesc.VarType.RAW, } assert dtype in switch_map, dtype + " not support in CINN" return switch_map[dtype] diff --git a/test/cinn/ops/test_conv2d_op.py b/test/cinn/ops/test_conv2d_op.py index 79dc3d7aed8ca..4f7a5ed577863 100755 --- a/test/cinn/ops/test_conv2d_op.py +++ b/test/cinn/ops/test_conv2d_op.py @@ -23,7 +23,7 @@ import paddle set_cinn_cudnn_deterministic(True) -paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': 1}) +paddle.base.set_flags({'FLAGS_cudnn_deterministic': 1}) @OpTestTool.skip_if( diff --git a/test/cinn/test_computation.py b/test/cinn/test_computation.py index 19deb70a3e6cf..ce4b9c5fcc592 100755 --- a/test/cinn/test_computation.py +++ b/test/cinn/test_computation.py @@ -22,7 +22,7 @@ from cinn.frontend import Computation, NetBuilder import paddle -from paddle import fluid, static +from paddle import base, static assert len(sys.argv) == 3 enable_gpu = sys.argv.pop() @@ -113,11 +113,11 @@ def test_compile_paddle_model(self): out = computation.get_tensor("fc_0.tmp_2") res_cinn = out.numpy(self.target) - config = fluid.core.AnalysisConfig(naive_model_dir) + config = base.core.AnalysisConfig(naive_model_dir) config.disable_gpu() config.switch_ir_optim(False) - paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(A_data) + paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(A_data) paddle_out = paddle_predictor.run([data]) res_paddle = paddle_out[0].as_ndarray() diff --git a/test/cinn/test_efficientnet.py b/test/cinn/test_efficientnet.py index 4a627e090220d..f3a817d140fe1 100755 --- a/test/cinn/test_efficientnet.py +++ b/test/cinn/test_efficientnet.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -40,13 +40,13 @@ def setUp(self): self.input_tensor = 'image' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( model_dir + '/__model__', model_dir + '/params' ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_facedet.py b/test/cinn/test_facedet.py index 20290846625c9..b2282cc5faa94 100755 --- a/test/cinn/test_facedet.py +++ b/test/cinn/test_facedet.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -42,13 +42,13 @@ def setUp(self): self.input_tensor = 'x0' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( model_dir + '/__model__', model_dir + '/params' ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_frontend.py b/test/cinn/test_frontend.py index d7ecf05d6840f..634c2c227822f 100755 --- a/test/cinn/test_frontend.py +++ b/test/cinn/test_frontend.py @@ -21,7 +21,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base assert len(sys.argv) == 1 + 2 + 1 # model and enable_gpu count enable_gpu = sys.argv.pop() @@ -113,11 +113,11 @@ def setUp(self): self.model_dir = naive_model_dir def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig(model_dir) + config = base.core.AnalysisConfig(model_dir) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) return results[0].as_ndarray() @@ -156,11 +156,11 @@ def setUp(self): self.model_dir = multi_fc_model_dir def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig(model_dir) + config = base.core.AnalysisConfig(model_dir) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) return results[0].as_ndarray() diff --git a/test/cinn/test_mobilenetv1.py b/test/cinn/test_mobilenetv1.py index 44d6de40c198f..6c4cfaf011100 100644 --- a/test/cinn/test_mobilenetv1.py +++ b/test/cinn/test_mobilenetv1.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -42,11 +42,11 @@ def setUp(self): self.input_tensor = 'image' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig(model_dir) + config = base.core.AnalysisConfig(model_dir) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_mobilenetv2.py b/test/cinn/test_mobilenetv2.py index b81314d87f5b8..4332678788117 100755 --- a/test/cinn/test_mobilenetv2.py +++ b/test/cinn/test_mobilenetv2.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -40,13 +40,13 @@ def setUp(self): self.input_tensor = 'image' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( model_dir + '/__model__', model_dir + '/params' ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_resnet.py b/test/cinn/test_resnet.py index a9c8cc3f40dd7..63ff06f528787 100755 --- a/test/cinn/test_resnet.py +++ b/test/cinn/test_resnet.py @@ -21,7 +21,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -39,13 +39,13 @@ def setUp(self): self.x_shape = [1, 160, 7, 7] def get_paddle_inference_result(self, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( self.model_dir + ".pdmodel", self.model_dir + ".pdiparams" ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) return results[0].as_ndarray() diff --git a/test/cinn/test_resnet18.py b/test/cinn/test_resnet18.py index 7b66b3a7c3538..3d746113ea3a5 100755 --- a/test/cinn/test_resnet18.py +++ b/test/cinn/test_resnet18.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -42,13 +42,13 @@ def setUp(self): self.input_tensor = 'image' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( model_dir + '/__model__', model_dir + '/params' ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_resnet50.py b/test/cinn/test_resnet50.py index e63415db9ed95..2987bd2ad6837 100755 --- a/test/cinn/test_resnet50.py +++ b/test/cinn/test_resnet50.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -42,13 +42,13 @@ def setUp(self): self.input_tensor = 'inputs' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig( + config = base.core.AnalysisConfig( model_dir + '/__model__', model_dir + '/params' ) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/cinn/test_squeezenet.py b/test/cinn/test_squeezenet.py index 78a28d33abf19..2228961e41f4f 100644 --- a/test/cinn/test_squeezenet.py +++ b/test/cinn/test_squeezenet.py @@ -22,7 +22,7 @@ from cinn.common import DefaultHostTarget, DefaultNVGPUTarget from cinn.frontend import Interpreter -from paddle import fluid +from paddle import base enable_gpu = sys.argv.pop() model_dir = sys.argv.pop() @@ -42,11 +42,11 @@ def setUp(self): self.input_tensor = 'data' def get_paddle_inference_result(self, model_dir, data): - config = fluid.core.AnalysisConfig(model_dir) + config = base.core.AnalysisConfig(model_dir) config.disable_gpu() config.switch_ir_optim(False) - self.paddle_predictor = fluid.core.create_paddle_predictor(config) - data = fluid.core.PaddleTensor(data) + self.paddle_predictor = base.core.create_paddle_predictor(config) + data = base.core.PaddleTensor(data) results = self.paddle_predictor.run([data]) get_tensor = self.paddle_predictor.get_output_tensor( self.target_tensor diff --git a/test/collective/collective_allgather_api.py b/test/collective/collective_allgather_api.py index 761703cb49497..800546664529b 100644 --- a/test/collective/collective_allgather_api.py +++ b/test/collective/collective_allgather_api.py @@ -18,8 +18,8 @@ import paddle import paddle.distributed as dist -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -83,7 +83,7 @@ def __init__(self): def get_model(self, main_prog, startup_program, rank, dtype=None): dtype = "float32" if dtype is None else dtype - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tensor_list = [] tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype @@ -94,7 +94,7 @@ def get_model(self, main_prog, startup_program, rank, dtype=None): def get_model_new( self, main_prog, startup_program, rank, dtype=None, reduce_type=None ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tensor_list = [] tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype @@ -103,8 +103,8 @@ def get_model_new( return tensor_list def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -115,14 +115,14 @@ def run_trainer(self, args): paddle.distributed.init_parallel_env() if args['backend'] == 'nccl': device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace( + place = base.CUDAPlace( device_id - ) # if args.use_gpu else fluid.CPUPlace() + ) # if args.use_gpu else base.CPUPlace() elif args['backend'] == 'bkcl': device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: - place = fluid.CPUPlace() + place = base.CPUPlace() indata = test_base.create_test_data( shape=(10, 1000), dtype=args["dtype"], seed=os.getpid() ) @@ -138,7 +138,7 @@ def run_trainer(self, args): train_prog, startup_prog, rank, dtype=args["dtype"] ) ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) fetch_list = [] for elem in result: diff --git a/test/collective/collective_allgather_api_dygraph.py b/test/collective/collective_allgather_api_dygraph.py index 15bf9c4629bad..ec33cf3419d88 100644 --- a/test/collective/collective_allgather_api_dygraph.py +++ b/test/collective/collective_allgather_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveAllgatherAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tensor_list = [] # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": diff --git a/test/collective/collective_allgather_object_api_dygraph.py b/test/collective/collective_allgather_object_api_dygraph.py index 1f3289e74ea69..734e7541954aa 100644 --- a/test/collective/collective_allgather_object_api_dygraph.py +++ b/test/collective/collective_allgather_object_api_dygraph.py @@ -15,7 +15,7 @@ import legacy_test.test_collective_api_base as test_base import paddle -from paddle import fluid +from paddle import base class TestCollectiveAllgatherObjectAPI(test_base.TestCollectiveAPIRunnerBase): @@ -23,7 +23,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): object_list = [] paddle.distributed.all_gather_object(object_list, indata) return object_list diff --git a/test/collective/collective_allreduce_api.py b/test/collective/collective_allreduce_api.py index b1f2770205518..21f4db05a35a4 100644 --- a/test/collective/collective_allreduce_api.py +++ b/test/collective/collective_allreduce_api.py @@ -19,8 +19,8 @@ import paddle import paddle.distributed as dist -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -71,7 +71,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype='float32' ) @@ -86,7 +86,7 @@ def get_model_new( dtype='float32', reduce_type=str(dist.ReduceOp.SUM), ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype ) diff --git a/test/collective/collective_allreduce_api_dygraph.py b/test/collective/collective_allreduce_api_dygraph.py index 58f95e78e5586..9244aad430017 100644 --- a/test/collective/collective_allreduce_api_dygraph.py +++ b/test/collective/collective_allreduce_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveAllreduceAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_allreduce_new_group_api.py b/test/collective/collective_allreduce_new_group_api.py index 36836b8fec0c3..6897bfd31c81e 100644 --- a/test/collective/collective_allreduce_new_group_api.py +++ b/test/collective/collective_allreduce_new_group_api.py @@ -15,7 +15,7 @@ from test_collective_api_base import TestCollectiveAPIRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -25,7 +25,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_allreduce_op.py b/test/collective/collective_allreduce_op.py index 4c3bbd13b4e46..5200db7c7ed9d 100644 --- a/test/collective/collective_allreduce_op.py +++ b/test/collective/collective_allreduce_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_allreduce_op_wait.py b/test/collective/collective_allreduce_op_wait.py index f57c20c787c8a..02232fa31ba76 100644 --- a/test/collective/collective_allreduce_op_wait.py +++ b/test/collective/collective_allreduce_op_wait.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_alltoall_api.py b/test/collective/collective_alltoall_api.py index ab0de9b7c49f2..999cfeb4308ea 100644 --- a/test/collective/collective_alltoall_api.py +++ b/test/collective/collective_alltoall_api.py @@ -19,8 +19,8 @@ import paddle import paddle.distributed as dist -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -103,7 +103,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) @@ -116,7 +116,7 @@ def get_model(self, main_prog, startup_program, rank): def get_model_new( self, main_prog, startup_program, rank, dtype=None, reduce_type=None ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype=dtype ) diff --git a/test/collective/collective_alltoall_api_dygraph.py b/test/collective/collective_alltoall_api_dygraph.py index 83f96883331b7..ef138514e2585 100644 --- a/test/collective/collective_alltoall_api_dygraph.py +++ b/test/collective/collective_alltoall_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveAllToAllAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): toutdata = [] # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": diff --git a/test/collective/collective_alltoall_single_api_dygraph.py b/test/collective/collective_alltoall_single_api_dygraph.py index 24ef25b97a225..b96bdc271ce36 100644 --- a/test/collective/collective_alltoall_single_api_dygraph.py +++ b/test/collective/collective_alltoall_single_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveAllToAllSingleAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_barrier_api.py b/test/collective/collective_barrier_api.py index 0e857e037ee89..05450da9d7907 100644 --- a/test/collective/collective_barrier_api.py +++ b/test/collective/collective_barrier_api.py @@ -19,7 +19,7 @@ from test_collective_api_base import TestCollectiveAPIRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -29,7 +29,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): paddle.distributed.barrier() return [] diff --git a/test/collective/collective_broadcast_api.py b/test/collective/collective_broadcast_api.py index 4e5db14eeaea9..c3f5109c7f304 100644 --- a/test/collective/collective_broadcast_api.py +++ b/test/collective/collective_broadcast_api.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -62,7 +62,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, dtype='float32'): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype=dtype ) @@ -73,7 +73,7 @@ def get_model(self, main_prog, startup_program, rank, dtype='float32'): def get_model_new( self, main_prog, startup_program, rank, dtype=None, reduce_type=None ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype=dtype ) diff --git a/test/collective/collective_broadcast_api_dygraph.py b/test/collective/collective_broadcast_api_dygraph.py index c17447c6716eb..e6f59a506a9bd 100644 --- a/test/collective/collective_broadcast_api_dygraph.py +++ b/test/collective/collective_broadcast_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveBroadcastAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_broadcast_object_list_api_dygraph.py b/test/collective/collective_broadcast_object_list_api_dygraph.py index 50b3050159428..0fb247b01d290 100644 --- a/test/collective/collective_broadcast_object_list_api_dygraph.py +++ b/test/collective/collective_broadcast_object_list_api_dygraph.py @@ -15,7 +15,7 @@ import test_collective_api_base as test_base import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveBroadcastObjectListAPI( @@ -25,7 +25,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): object_list = [indata] dist.broadcast_object_list(object_list, src=1) return object_list diff --git a/test/collective/collective_broadcast_op.py b/test/collective/collective_broadcast_op.py index 72e469ebd6b39..44adc62676f8a 100644 --- a/test/collective/collective_broadcast_op.py +++ b/test/collective/collective_broadcast_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 rootid = 1 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_concat_api.py b/test/collective/collective_concat_api.py index fab9711a91867..c349ad14aa883 100644 --- a/test/collective/collective_concat_api.py +++ b/test/collective/collective_concat_api.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -70,7 +70,7 @@ def get_model(self, main_prog, startup_program): def get_model_new( self, main_prog, startup_program, rank, dtype=None, reduce_type=None ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype ) diff --git a/test/collective/collective_concat_op.py b/test/collective/collective_concat_op.py index 40a41c47632d8..d139ff0b95277 100644 --- a/test/collective/collective_concat_op.py +++ b/test/collective/collective_concat_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_gather_api_dygraph.py b/test/collective/collective_gather_api_dygraph.py index ddb6b7dcd40aa..229861785e9a9 100644 --- a/test/collective/collective_gather_api_dygraph.py +++ b/test/collective/collective_gather_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveGatherAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): gather_list = [] # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": diff --git a/test/collective/collective_global_gather.py b/test/collective/collective_global_gather.py index 2601830a8dae0..674e3c8193f40 100644 --- a/test/collective/collective_global_gather.py +++ b/test/collective/collective_global_gather.py @@ -22,7 +22,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed.utils import moe_utils paddle.enable_static() @@ -33,7 +33,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): seed = os.getpid() np.random.seed(seed) in_feat = 2 @@ -57,8 +57,8 @@ def get_model(self, main_prog, startup_program, rank, indata=None): return [output] def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -66,22 +66,22 @@ def run_trainer(self, args): nranks = 2 if args['backend'] == 'nccl': device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace( + place = base.CUDAPlace( device_id - ) # if args.use_gpu else fluid.CPUPlace() + ) # if args.use_gpu else base.CPUPlace() elif args['backend'] == 'bkcl': device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: - place = fluid.CPUPlace() + place = base.CPUPlace() in_feat = 2 n_expert = 2 world_size = 2 tot_expert = n_expert * world_size - tmp_main_prog = fluid.Program() - with fluid.program_guard(tmp_main_prog, fluid.Program()): + tmp_main_prog = base.Program() + with base.program_guard(tmp_main_prog, base.Program()): local_expert_count = paddle.static.data( name="local_expert_count", shape=[tot_expert], dtype="int64" ) @@ -90,7 +90,7 @@ def run_trainer(self, args): paddle.split(local_expert_count, 2, axis=0), global_expert_count ) global_expert_count = paddle.concat(global_expert_count, axis=0) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) np.random.seed(os.getpid()) local_expert_count = np.random.randint(1, 4, size=tot_expert).astype( diff --git a/test/collective/collective_global_gather_dygraph.py b/test/collective/collective_global_gather_dygraph.py index c816132f9ef90..4dbb3ee1e0bf1 100644 --- a/test/collective/collective_global_gather_dygraph.py +++ b/test/collective/collective_global_gather_dygraph.py @@ -21,7 +21,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed.utils import moe_utils @@ -30,7 +30,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): seed = os.getpid() np.random.seed(seed) in_feat = 2 diff --git a/test/collective/collective_global_scatter.py b/test/collective/collective_global_scatter.py index e4388dd1df3df..c92dcd1631390 100644 --- a/test/collective/collective_global_scatter.py +++ b/test/collective/collective_global_scatter.py @@ -22,7 +22,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed.utils import moe_utils paddle.enable_static() @@ -33,7 +33,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): seed = os.getpid() np.random.seed(seed) in_feat = 2 @@ -57,8 +57,8 @@ def get_model(self, main_prog, startup_program, rank, indata=None): return [output] def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -66,14 +66,14 @@ def run_trainer(self, args): paddle.distributed.init_parallel_env() if args['backend'] == 'nccl': device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace( + place = base.CUDAPlace( device_id - ) # if args.use_gpu else fluid.CPUPlace() + ) # if args.use_gpu else base.CPUPlace() elif args['backend'] == 'bkcl': device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: - place = fluid.CPUPlace() + place = base.CPUPlace() np.random.seed(os.getpid()) in_feat = 2 n_expert = 2 @@ -88,7 +88,7 @@ def run_trainer(self, args): ) if args['static_mode']: result = self.get_model(train_prog, startup_prog, rank) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) fetch_list = [] for elem in result: diff --git a/test/collective/collective_global_scatter_dygraph.py b/test/collective/collective_global_scatter_dygraph.py index 2e5001371fd47..0fc468d73484f 100644 --- a/test/collective/collective_global_scatter_dygraph.py +++ b/test/collective/collective_global_scatter_dygraph.py @@ -21,7 +21,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed.utils import moe_utils @@ -30,7 +30,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): seed = os.getpid() np.random.seed(seed) in_feat = 2 diff --git a/test/collective/collective_identity_op.py b/test/collective/collective_identity_op.py index 5bee820fb2e9e..c56111da62ab5 100644 --- a/test/collective/collective_identity_op.py +++ b/test/collective/collective_identity_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_isend_irecv_api_dygraph.py b/test/collective/collective_isend_irecv_api_dygraph.py index f26160b3f0444..61b3453ffd425 100644 --- a/test/collective/collective_isend_irecv_api_dygraph.py +++ b/test/collective/collective_isend_irecv_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveIsendIrecvAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_reduce_api.py b/test/collective/collective_reduce_api.py index cd3b35091414d..799a8502eee01 100644 --- a/test/collective/collective_reduce_api.py +++ b/test/collective/collective_reduce_api.py @@ -20,8 +20,8 @@ import paddle import paddle.distributed as dist -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -68,7 +68,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) @@ -84,7 +84,7 @@ def get_model_new( dtype='float32', reduce_type=str(dist.ReduceOp.SUM), ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype ) diff --git a/test/collective/collective_reduce_api_dygraph.py b/test/collective/collective_reduce_api_dygraph.py index a3b765b184c93..2dbdfc41a2fe1 100644 --- a/test/collective/collective_reduce_api_dygraph.py +++ b/test/collective/collective_reduce_api_dygraph.py @@ -19,7 +19,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveReduceAPI(test_base.TestCollectiveAPIRunnerBase): @@ -27,7 +27,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_reduce_op.py b/test/collective/collective_reduce_op.py index f250839d6030e..1ce1143122ddc 100644 --- a/test/collective/collective_reduce_op.py +++ b/test/collective/collective_reduce_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 rootid = 1 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_reduce_op_calc_stream.py b/test/collective/collective_reduce_op_calc_stream.py index b7efdbe54cd94..99e4abb759964 100644 --- a/test/collective/collective_reduce_op_calc_stream.py +++ b/test/collective/collective_reduce_op_calc_stream.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 rootid = 1 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_reduce_scatter_api.py b/test/collective/collective_reduce_scatter_api.py index b1cec07badd90..9246303549402 100644 --- a/test/collective/collective_reduce_scatter_api.py +++ b/test/collective/collective_reduce_scatter_api.py @@ -18,7 +18,7 @@ ) import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -38,7 +38,7 @@ def get_model_new( dtype='float32', reduce_type=None, ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype=dtype ) diff --git a/test/collective/collective_reduce_scatter_api_dygraph.py b/test/collective/collective_reduce_scatter_api_dygraph.py index 6e741b8695866..f68c5ca651488 100644 --- a/test/collective/collective_reduce_scatter_api_dygraph.py +++ b/test/collective/collective_reduce_scatter_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveReduceScatterAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_scatter_api.py b/test/collective/collective_scatter_api.py index 4579d00fa9cff..17846321deacb 100644 --- a/test/collective/collective_scatter_api.py +++ b/test/collective/collective_scatter_api.py @@ -19,7 +19,7 @@ from test_collective_api_base import TestCollectiveAPIRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -29,7 +29,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], diff --git a/test/collective/collective_scatter_api_dygraph.py b/test/collective/collective_scatter_api_dygraph.py index 41d35be8f8e46..2861ca82fb17d 100644 --- a/test/collective/collective_scatter_api_dygraph.py +++ b/test/collective/collective_scatter_api_dygraph.py @@ -20,7 +20,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveScatterAPI(test_base.TestCollectiveAPIRunnerBase): @@ -28,7 +28,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_scatter_object_list_api_dygraph.py b/test/collective/collective_scatter_object_list_api_dygraph.py index 9c7810910d5a2..c58d9b385afef 100644 --- a/test/collective/collective_scatter_object_list_api_dygraph.py +++ b/test/collective/collective_scatter_object_list_api_dygraph.py @@ -15,7 +15,7 @@ import legacy_test.test_collective_api_base as test_base import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveScatterObjectListAPI(test_base.TestCollectiveAPIRunnerBase): @@ -23,7 +23,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): data_len = len(indata) // 2 in_object_list = [indata[:data_len], indata[data_len:]] out_object_list = [] diff --git a/test/collective/collective_scatter_op.py b/test/collective/collective_scatter_op.py index aa010c86724c7..8e5a06dc62852 100644 --- a/test/collective/collective_scatter_op.py +++ b/test/collective/collective_scatter_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 rootid = 1 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/collective_sendrecv_api.py b/test/collective/collective_sendrecv_api.py index 9dae4b34801cb..b5fc01be7af15 100644 --- a/test/collective/collective_sendrecv_api.py +++ b/test/collective/collective_sendrecv_api.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid, framework -from paddle.fluid import data_feeder +from paddle import base, framework +from paddle.base import data_feeder paddle.enable_static() @@ -85,7 +85,7 @@ def recv_new(tensor, src, group=None, sync_op=True, dtype='float32'): 'peer': src, 'dynamic_shape': True, 'out_shape': tensor.shape, - 'dtype': fluid.framework.convert_np_dtype_to_dtype_(dtype), + 'dtype': base.framework.convert_np_dtype_to_dtype_(dtype), }, ) return None @@ -96,7 +96,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], @@ -116,7 +116,7 @@ def get_model_new( dtype='float32', reduce_type=None, ): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], diff --git a/test/collective/collective_sendrecv_api_dygraph.py b/test/collective/collective_sendrecv_api_dygraph.py index fe26cc19ba505..a68112e25bb2e 100644 --- a/test/collective/collective_sendrecv_api_dygraph.py +++ b/test/collective/collective_sendrecv_api_dygraph.py @@ -16,7 +16,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base class TestCollectiveSendRecvAPI(test_base.TestCollectiveAPIRunnerBase): @@ -24,7 +24,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank, indata=None): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): # NOTE: this is a hack relying on an undocumented behavior that `to_tensor` uses uint16 to replace bfloat16 if indata.dtype == "bfloat16": tindata = paddle.to_tensor(indata, "float32").cast("uint16") diff --git a/test/collective/collective_sendrecv_op.py b/test/collective/collective_sendrecv_op.py index 30a83f02efd7a..1a3c38cde9e00 100644 --- a/test/collective/collective_sendrecv_op.py +++ b/test/collective/collective_sendrecv_op.py @@ -18,7 +18,7 @@ ) import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -29,7 +29,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = self.global_ring_id - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], diff --git a/test/collective/collective_sendrecv_op_array.py b/test/collective/collective_sendrecv_op_array.py index fb7f0848c103b..e2ac44f14f6b6 100644 --- a/test/collective/collective_sendrecv_op_array.py +++ b/test/collective/collective_sendrecv_op_array.py @@ -19,7 +19,7 @@ ) import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = self.global_ring_id - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], diff --git a/test/collective/collective_sendrecv_op_dynamic_shape.py b/test/collective/collective_sendrecv_op_dynamic_shape.py index d01a0b421dab7..209ff932eee76 100644 --- a/test/collective/collective_sendrecv_op_dynamic_shape.py +++ b/test/collective/collective_sendrecv_op_dynamic_shape.py @@ -18,7 +18,7 @@ ) import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -29,7 +29,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = self.global_ring_id - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], diff --git a/test/collective/collective_split_op.py b/test/collective/collective_split_op.py index bb64bfadc8649..f006617b51f6d 100644 --- a/test/collective/collective_split_op.py +++ b/test/collective/collective_split_op.py @@ -18,8 +18,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,7 +31,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/collective/column_parallel_linear_api.py b/test/collective/column_parallel_linear_api.py index ac30aace6dfc9..b433cca4c528e 100644 --- a/test/collective/column_parallel_linear_api.py +++ b/test/collective/column_parallel_linear_api.py @@ -19,7 +19,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): fleet.init(is_collective=True) np.random.seed(2020) np_array = np.random.rand(1000, 16) @@ -40,11 +40,11 @@ def get_model(self, main_prog, startup_program, rank): ) paddle.distributed.broadcast(data, src=0) if rank == 0: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign(np_array[:, 0:8]), ) else: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign(np_array[:, 8:16]), ) diff --git a/test/collective/fleet/auto_parallel_parallelizer.py b/test/collective/fleet/auto_parallel_parallelizer.py index acf0900333011..473da21fc52d7 100755 --- a/test/collective/fleet/auto_parallel_parallelizer.py +++ b/test/collective/fleet/auto_parallel_parallelizer.py @@ -17,9 +17,9 @@ import paddle import paddle.nn.functional as F from paddle import nn, static, utils +from paddle.base import core from paddle.distributed import fleet from paddle.distributed.fleet import auto -from paddle.fluid import core paddle.enable_static() _global_parallel_strategy = None diff --git a/test/collective/fleet/c_comm_init_op.py b/test/collective/fleet/c_comm_init_op.py index 359266453364f..988c0fcc27954 100644 --- a/test/collective/fleet/c_comm_init_op.py +++ b/test/collective/fleet/c_comm_init_op.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet.base.private_helper_function import ( wait_server_ready, ) @@ -31,20 +31,20 @@ def setUp(self): self.nranks = len(self.endpoints) self.rank = self.endpoints.index(self.current_endpoint) self.gpu_id = int(os.getenv("FLAGS_selected_gpus")) - self.place = fluid.CUDAPlace(self.gpu_id) - self.exe = fluid.Executor(self.place) + self.place = base.CUDAPlace(self.gpu_id) + self.exe = base.Executor(self.place) self.endpoints.remove(self.current_endpoint) self.other_endpoints = self.endpoints if self.rank == 0: wait_server_ready(self.other_endpoints) def test_specifying_devices(self): - program = fluid.Program() + program = base.Program() block = program.global_block() nccl_id_var = block.create_var( - name=fluid.unique_name.generate('nccl_id'), + name=base.unique_name.generate('nccl_id'), persistable=True, - type=fluid.core.VarDesc.VarType.RAW, + type=base.core.VarDesc.VarType.RAW, ) block.append_op( type='c_gen_nccl_id', diff --git a/test/collective/fleet/dist_mnist_gradient_merge.py b/test/collective/fleet/dist_mnist_gradient_merge.py index deeb3b32de857..f37220c8e75e7 100644 --- a/test/collective/fleet/dist_mnist_gradient_merge.py +++ b/test/collective/fleet/dist_mnist_gradient_merge.py @@ -20,14 +20,14 @@ from test_dist_base import TestDistRunnerBase, _insert_comm_op, runtime_main import paddle -from paddle import fluid +from paddle import base DTYPE = "float32" paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistMnist2x2(TestDistRunnerBase): @@ -51,7 +51,7 @@ def get_model(self, batch_size=2, single_device=False): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) opt = paddle.incubate.optimizer.GradientMergeOptimizer(opt, 2) diff --git a/test/collective/fleet/dist_mnist_gradient_merge_raw_optimizer.py b/test/collective/fleet/dist_mnist_gradient_merge_raw_optimizer.py index 369e163c3528c..d44538ee99edd 100644 --- a/test/collective/fleet/dist_mnist_gradient_merge_raw_optimizer.py +++ b/test/collective/fleet/dist_mnist_gradient_merge_raw_optimizer.py @@ -22,7 +22,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid, nn +from paddle import base, nn from paddle.distributed import fleet @@ -32,7 +32,7 @@ def get_model(self, batch_size=2, single_device=False): paddle.seed(1) np.random.seed(1) - assert fluid.core.globals()['FLAGS_apply_pass_to_program'] + assert base.core.globals()['FLAGS_apply_pass_to_program'] strategy = fleet.DistributedStrategy() build_strategy = paddle.static.BuildStrategy() settings = { diff --git a/test/collective/fleet/dygraph_group_sharded_stage3.py b/test/collective/fleet/dygraph_group_sharded_stage3.py index bbe0884d982e4..5587f51e1928b 100644 --- a/test/collective/fleet/dygraph_group_sharded_stage3.py +++ b/test/collective/fleet/dygraph_group_sharded_stage3.py @@ -21,6 +21,7 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_optimizer_stage2 import ( GroupShardedOptimizerStage2, ) @@ -33,7 +34,6 @@ from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import ( GroupShardedScaler, ) -from paddle.fluid import core from paddle.nn import Linear epoch = 10 diff --git a/test/collective/fleet/dygraph_group_sharded_stage3_offload.py b/test/collective/fleet/dygraph_group_sharded_stage3_offload.py index b34f178aa8363..fc7791fd09aaa 100644 --- a/test/collective/fleet/dygraph_group_sharded_stage3_offload.py +++ b/test/collective/fleet/dygraph_group_sharded_stage3_offload.py @@ -18,13 +18,13 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_stage3 import ( GroupShardedStage3, ) from paddle.distributed.fleet.meta_parallel.sharding.group_sharded_utils import ( GroupShardedScaler, ) -from paddle.fluid import core from paddle.nn import Linear epoch = 10 diff --git a/test/collective/fleet/fused_attention_pass_with_mp.py b/test/collective/fleet/fused_attention_pass_with_mp.py index 71ca3ef252711..cdbebc11eec79 100644 --- a/test/collective/fleet/fused_attention_pass_with_mp.py +++ b/test/collective/fleet/fused_attention_pass_with_mp.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.passes import PassManager, new_pass @@ -116,8 +116,8 @@ def setUp(self): self.nranks = len(self.endpoints) self.rank = self.endpoints.index(self.current_endpoint) self.gpu_id = int(os.getenv("FLAGS_selected_gpus")) - self.place = fluid.CUDAPlace(self.gpu_id) - self.exe = fluid.Executor(self.place) + self.place = base.CUDAPlace(self.gpu_id) + self.exe = base.Executor(self.place) self.endpoints.remove(self.current_endpoint) self.other_endpoints = self.endpoints self.add_residual = True @@ -180,9 +180,9 @@ def get_rst(self, use_pass=False): startup_block = startup_prog.global_block() nccl_id_var = startup_block.create_var( - name=fluid.unique_name.generate('nccl_id'), + name=base.unique_name.generate('nccl_id'), persistable=True, - type=fluid.core.VarDesc.VarType.RAW, + type=base.core.VarDesc.VarType.RAW, ) startup_block.append_op( type='c_gen_nccl_id', diff --git a/test/collective/fleet/hybrid_parallel_inference_helper.py b/test/collective/fleet/hybrid_parallel_inference_helper.py index 5e118f0f67fe9..1e365c1822f42 100644 --- a/test/collective/fleet/hybrid_parallel_inference_helper.py +++ b/test/collective/fleet/hybrid_parallel_inference_helper.py @@ -58,12 +58,12 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self): device = "gpu" with paddle.static.program_guard(main_program, startup_program): - with paddle.fluid.device_guard(f'{device}:0'): + with paddle.base.device_guard(f'{device}:0'): X = paddle.static.data( name='X', shape=[None, 2], dtype='float32' ) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): max_len = paddle.tensor.fill_constant( shape=[1], dtype="int64", value=2, force_cpu=False, name="n" ) @@ -87,12 +87,12 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self): ) with while_op.block(): - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): input = paddle.tensor.array_read(array=data, i=step_idx) paddle.increment(x=step_idx, value=1.0) paddle.tensor.array_write(input, i=step_idx, array=data) - with paddle.fluid.device_guard(f'{device}:0'): + with paddle.base.device_guard(f'{device}:0'): param_attr = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ) @@ -104,7 +104,7 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self): ) hidden1 = paddle.matmul(input, weight1) - with paddle.fluid.device_guard(f'{device}:1'): + with paddle.base.device_guard(f'{device}:1'): param_attr = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(2.0) ) @@ -122,15 +122,15 @@ def test_hybrid_parallel_inference_helper_mp1pp2(self): paddle.assign(paddle.less_than(x=step_idx, y=max_len), cond) paddle.assign(paddle.cast(cond, dtype="int32"), cond_int) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # the code below must at end of while block and exists in device:all paddle.assign(paddle.cast(cond_int, dtype='bool'), cond) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): out = paddle.tensor.create_array(data.dtype) paddle.assign(data, out) - with paddle.fluid.device_guard(f'{device}:all'): + with paddle.base.device_guard(f'{device}:all'): # use a empty lod_tensor_array to clear lod_tensor_array paddle.assign(paddle.tensor.create_array(data.dtype), data) diff --git a/test/collective/fleet/hybrid_parallel_qat.py b/test/collective/fleet/hybrid_parallel_qat.py index 00bc0f746e761..484cfd168b530 100644 --- a/test/collective/fleet/hybrid_parallel_qat.py +++ b/test/collective/fleet/hybrid_parallel_qat.py @@ -19,7 +19,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid, nn +from paddle import base, nn from paddle.distributed import fleet from paddle.distributed.utils.launch_utils import find_free_ports, get_cluster from paddle.quantization import ImperativeQuantAware @@ -328,8 +328,8 @@ def train(self, model_a, optimizer_a, model_b, optimizer_b): def test_mp_model_1(self): if ( - not fluid.core.is_compiled_with_cuda() - or fluid.core.get_cuda_device_count() == 0 + not base.core.is_compiled_with_cuda() + or base.core.get_cuda_device_count() == 0 ): return selected_gpus = get_gpus('0,1') @@ -344,8 +344,8 @@ def test_mp_model_1(self): def test_mp_model_2(self): if ( - not fluid.core.is_compiled_with_cuda() - or fluid.core.get_cuda_device_count() == 0 + not base.core.is_compiled_with_cuda() + or base.core.get_cuda_device_count() == 0 ): return selected_gpus = get_gpus('0,1') diff --git a/test/collective/fleet/parallel_dygraph_control_flow_same.py b/test/collective/fleet/parallel_dygraph_control_flow_same.py index 3fbc5fc35b60a..6b53194b12650 100644 --- a/test/collective/fleet/parallel_dygraph_control_flow_same.py +++ b/test/collective/fleet/parallel_dygraph_control_flow_same.py @@ -19,7 +19,7 @@ ) import paddle -from paddle.fluid.dygraph.base import to_variable +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear np.random.seed(2021) diff --git a/test/collective/fleet/parallel_dygraph_no_sync.py b/test/collective/fleet/parallel_dygraph_no_sync.py index 5f2b9751a6733..4c76e4b4e4b51 100644 --- a/test/collective/fleet/parallel_dygraph_no_sync.py +++ b/test/collective/fleet/parallel_dygraph_no_sync.py @@ -25,7 +25,7 @@ import paddle import paddle.distributed as dist -from paddle import fluid +from paddle import base from paddle.nn import Linear seed = 90 @@ -68,15 +68,15 @@ def run_one_loop(self, model, optimizer, batch): return loss def run_trainer_func(self, args): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) + place = base.CUDAPlace(device_id) else: assert "Only support CUDAPlace for now." - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + with base.dygraph.guard(place): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed np.random.seed(seed) random.seed(seed) model, train_reader, opt = self.get_model() diff --git a/test/collective/fleet/parallel_dygraph_se_resnext.py b/test/collective/fleet/parallel_dygraph_se_resnext.py index c24e4e7ebef3d..e9ee2407a3346 100644 --- a/test/collective/fleet/parallel_dygraph_se_resnext.py +++ b/test/collective/fleet/parallel_dygraph_se_resnext.py @@ -18,8 +18,8 @@ from test_dist_base import TestParallelDyGraphRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear batch_size = 64 @@ -56,9 +56,9 @@ def optimizer_setting(params, parameter_list=None): lr = params["lr"] num_epochs = params["num_epochs"] - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): optimizer = paddle.optimizer.Momentum( - learning_rate=fluid.layers.cosine_decay( + learning_rate=base.layers.cosine_decay( learning_rate=lr, step_each_epoch=step, epochs=num_epochs ), momentum=momentum_rate, diff --git a/test/collective/fleet/parallel_dygraph_sync_batch_norm.py b/test/collective/fleet/parallel_dygraph_sync_batch_norm.py index df501c337aae1..6fe091ad498b3 100644 --- a/test/collective/fleet/parallel_dygraph_sync_batch_norm.py +++ b/test/collective/fleet/parallel_dygraph_sync_batch_norm.py @@ -19,7 +19,7 @@ ) import paddle -from paddle.fluid.dygraph.base import to_variable +from paddle.base.dygraph.base import to_variable from paddle.nn import Conv2D, SyncBatchNorm diff --git a/test/collective/fleet/parallel_dygraph_transformer.py b/test/collective/fleet/parallel_dygraph_transformer.py index b92c14a11d40f..e3214b86e79b0 100644 --- a/test/collective/fleet/parallel_dygraph_transformer.py +++ b/test/collective/fleet/parallel_dygraph_transformer.py @@ -20,8 +20,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.nn import Layer from paddle.optimizer.lr import NoamDecay @@ -245,10 +245,10 @@ def __init__(self, d_model, process_cmd, shape_len=None): if cmd == "n": self._layer_norm = paddle.nn.LayerNorm( normalized_shape=d_model, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ), ) @@ -513,7 +513,7 @@ def __init__( src_vocab_size, src_emb_dim, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=word_emb_param_name, initializer=paddle.nn.initializer.Normal( 0.0, src_emb_dim**-0.5 @@ -529,7 +529,7 @@ def __init__( self._src_max_len, src_emb_dim, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=pos_enc_param_name, initializer=paddle.nn.initializer.Assign(pos_inp), trainable=False, diff --git a/test/collective/fleet/pipeline_mnist.py b/test/collective/fleet/pipeline_mnist.py index 8e3ababc443a0..fa9288b04e0ae 100644 --- a/test/collective/fleet/pipeline_mnist.py +++ b/test/collective/fleet/pipeline_mnist.py @@ -18,7 +18,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -39,7 +39,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -50,7 +50,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -60,12 +60,12 @@ def cnn_model(data): param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): predict = paddle.static.nn.fc( x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -74,7 +74,7 @@ def cnn_model(data): x=conv_pool_1, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -85,7 +85,7 @@ def cnn_model(data): class TestDistMnist2x2(TestDistRunnerBase): def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): # Input data - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): images = paddle.static.data( name='pixel', shape=[-1, 1, 28, 28], dtype=DTYPE ) @@ -94,7 +94,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[images, label], capacity=64, use_double_buffer=False, @@ -102,20 +102,20 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) # Train program predict = cnn_model(images) - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): cost = paddle.nn.functional.cross_entropy( input=predict, label=label, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) # Evaluator - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): batch_size_tensor = paddle.tensor.create_tensor(dtype='int64') batch_acc = paddle.static.accuracy( input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() base_lr = self.lr passes = [30, 60, 80, 90] steps_per_pass = 10 diff --git a/test/collective/fleet/pipeline_mnist_multi_device.py b/test/collective/fleet/pipeline_mnist_multi_device.py index 6baac996f8833..06e554f281473 100644 --- a/test/collective/fleet/pipeline_mnist_multi_device.py +++ b/test/collective/fleet/pipeline_mnist_multi_device.py @@ -18,7 +18,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -39,7 +39,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -50,7 +50,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -60,12 +60,12 @@ def cnn_model(data): param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): predict = paddle.static.nn.fc( x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -74,7 +74,7 @@ def cnn_model(data): x=conv_pool_1, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -85,7 +85,7 @@ def cnn_model(data): class TestDistMnist2x2(TestDistRunnerBase): def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): # Input data - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): images = paddle.static.data( name='pixel', shape=[-1, 1, 28, 28], dtype=DTYPE ) @@ -94,7 +94,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[images, label], capacity=64, use_double_buffer=False, @@ -102,20 +102,20 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) # Train program predict = cnn_model(images) - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): cost = paddle.nn.functional.cross_entropy( input=predict, label=label, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) # Evaluator - with fluid.device_guard("gpu:1"): + with base.device_guard("gpu:1"): batch_size_tensor = paddle.tensor.create_tensor(dtype='int64') batch_acc = paddle.static.accuracy( input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() base_lr = self.lr passes = [30, 60, 80, 90] steps_per_pass = 10 diff --git a/test/collective/fleet/pipeline_mnist_one_device.py b/test/collective/fleet/pipeline_mnist_one_device.py index 3d84414829580..a106c9b1098d5 100644 --- a/test/collective/fleet/pipeline_mnist_one_device.py +++ b/test/collective/fleet/pipeline_mnist_one_device.py @@ -18,7 +18,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -39,7 +39,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -50,7 +50,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -64,7 +64,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -77,7 +77,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): device_id = 0 if dist_strategy: fleet.init(is_collective=True) - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): images = paddle.static.data( name='pixel', shape=[-1, 1, 28, 28], dtype=DTYPE ) @@ -86,7 +86,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[images, label], capacity=64, use_double_buffer=False, @@ -94,20 +94,20 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) # Train program predict = cnn_model(images) - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): cost = paddle.nn.functional.cross_entropy( input=predict, label=label, reduction='none', use_softmax=False ) avg_cost = paddle.mean(x=cost) # Evaluator - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): batch_size_tensor = paddle.tensor.create_tensor(dtype='int64') batch_acc = paddle.static.accuracy( input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() base_lr = self.lr passes = [30, 60, 80, 90] steps_per_pass = 10 diff --git a/test/collective/fleet/static_model_parallel_by_col.py b/test/collective/fleet/static_model_parallel_by_col.py index f481e72f0b8e9..3a161298fee33 100644 --- a/test/collective/fleet/static_model_parallel_by_col.py +++ b/test/collective/fleet/static_model_parallel_by_col.py @@ -16,7 +16,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ OUT_SIZE = 2 * MODEL_PARALLEL_SIZE # Fix seed for test -# fluid.default_startup_program().random_seed = 1 -# fluid.default_main_program().random_seed = 1 +# base.default_startup_program().random_seed = 1 +# base.default_main_program().random_seed = 1 def get_param_attr(weight, bias): @@ -76,7 +76,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/collective/fleet/static_model_parallel_by_row.py b/test/collective/fleet/static_model_parallel_by_row.py index 93c76ea71afb4..94e1b56acc9fe 100644 --- a/test/collective/fleet/static_model_parallel_by_row.py +++ b/test/collective/fleet/static_model_parallel_by_row.py @@ -16,7 +16,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ OUT_SIZE = 2 * MODEL_PARALLEL_SIZE # Fix seed for test -# fluid.default_startup_program().random_seed = 1 -# fluid.default_main_program().random_seed = 1 +# base.default_startup_program().random_seed = 1 +# base.default_main_program().random_seed = 1 def get_param_attr(weight, bias): @@ -80,7 +80,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/collective/fleet/static_model_parallel_embedding.py b/test/collective/fleet/static_model_parallel_embedding.py index c762b1c960740..797a8a4f690ee 100644 --- a/test/collective/fleet/static_model_parallel_embedding.py +++ b/test/collective/fleet/static_model_parallel_embedding.py @@ -16,7 +16,7 @@ from legacy_test.test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ OUT_SIZE = 2 * MODEL_PARALLEL_SIZE # Fix seed for test -# fluid.default_startup_program().random_seed = 1 -# fluid.default_main_program().random_seed = 1 +# base.default_startup_program().random_seed = 1 +# base.default_main_program().random_seed = 1 def create_model(data, rank): @@ -70,7 +70,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/collective/fleet/test_auto_checkpoint.py b/test/collective/fleet/test_auto_checkpoint.py index f036d039c9063..5382e522612d7 100644 --- a/test/collective/fleet/test_auto_checkpoint.py +++ b/test/collective/fleet/test_auto_checkpoint.py @@ -18,9 +18,9 @@ from auto_checkpoint_utils import AutoCheckpointBase, get_logger import paddle -import paddle.fluid.incubate.checkpoint.auto_checkpoint as acp +import paddle.base.incubate.checkpoint.auto_checkpoint as acp +from paddle.base.incubate.checkpoint.checkpoint_saver import PaddleModel from paddle.distributed.fleet.utils.fs import HDFSClient, LocalFS -from paddle.fluid.incubate.checkpoint.checkpoint_saver import PaddleModel paddle.enable_static() logger = get_logger() diff --git a/test/collective/fleet/test_auto_checkpoint_dist_basic.py b/test/collective/fleet/test_auto_checkpoint_dist_basic.py index f2f0035ab9cb2..f9625b05f052f 100644 --- a/test/collective/fleet/test_auto_checkpoint_dist_basic.py +++ b/test/collective/fleet/test_auto_checkpoint_dist_basic.py @@ -19,8 +19,8 @@ from test_auto_checkpoint import AutoCheckPointACLBase import paddle -import paddle.fluid.incubate.checkpoint.auto_checkpoint as acp -from paddle import fluid +import paddle.base.incubate.checkpoint.auto_checkpoint as acp +from paddle import base from paddle.distributed.fleet.utils.fs import HDFSClient, LocalFS from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.collective import fleet @@ -76,7 +76,7 @@ def test_distributed_basic(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): dist_optimizer = fleet.distributed_optimizer(optimizer) dist_optimizer.minimize(loss) diff --git a/test/collective/fleet/test_auto_checkpoint_multiple.py b/test/collective/fleet/test_auto_checkpoint_multiple.py index 36421a80e321c..a243ad0dc43ed 100644 --- a/test/collective/fleet/test_auto_checkpoint_multiple.py +++ b/test/collective/fleet/test_auto_checkpoint_multiple.py @@ -19,7 +19,7 @@ from test_auto_checkpoint import AutoCheckPointACLBase import paddle -import paddle.fluid.incubate.checkpoint.auto_checkpoint as acp +import paddle.base.incubate.checkpoint.auto_checkpoint as acp from paddle.distributed.fleet.utils.fs import HDFSClient, LocalFS paddle.enable_static() diff --git a/test/collective/fleet/test_communicator_half_async.py b/test/collective/fleet/test_communicator_half_async.py index ae6d02a24d589..25e5302fb444f 100644 --- a/test/collective/fleet/test_communicator_half_async.py +++ b/test/collective/fleet/test_communicator_half_async.py @@ -20,7 +20,7 @@ import numpy import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -57,8 +57,8 @@ def run_pserver(self, role, strategy): fleet.run_server() def run_trainer(self, role, strategy): - place = fluid.core.CPUPlace() - exe = fluid.Executor(place) + place = base.core.CPUPlace() + exe = base.Executor(place) fleet.init(role) avg_cost, x, y = self.net() @@ -70,7 +70,7 @@ def run_trainer(self, role, strategy): fleet.init_worker() train_reader = paddle.batch(self.fake_reader(), batch_size=24) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) for batch_id, data in enumerate(train_reader()): exe.run( @@ -116,7 +116,7 @@ def test_communicator(self): from test_communicator_half_async import TestCommunicatorHalfAsyncEnd2End import paddle -import paddle.fluid as fluid +import paddle.base as base import paddle.distributed.fleet as fleet import paddle.distributed.fleet.base.role_maker as role_maker diff --git a/test/collective/fleet/test_dgc_momentum_op.py b/test/collective/fleet/test_dgc_momentum_op.py index 62106d4c0f8d6..90152cff3918d 100644 --- a/test/collective/fleet/test_dgc_momentum_op.py +++ b/test/collective/fleet/test_dgc_momentum_op.py @@ -17,8 +17,8 @@ import numpy as np from op import Operator -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDGCMomentumOp1(unittest.TestCase): @@ -28,7 +28,7 @@ def get_tensor(self, name, value, place=None): return name, tensor def setup(self, place, step=0.0): - self.scope = fluid.global_scope() + self.scope = base.global_scope() self.place = place print("place:", place) diff --git a/test/collective/fleet/test_dgc_op.py b/test/collective/fleet/test_dgc_op.py index 9905719e1b4d7..2e8488cc870b3 100644 --- a/test/collective/fleet/test_dgc_op.py +++ b/test/collective/fleet/test_dgc_op.py @@ -17,8 +17,8 @@ import numpy as np from op import Operator -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core g_array_size = 102400 @@ -28,7 +28,7 @@ def setup(self, place, array_size=g_array_size): size = array_size np.random.seed(5) # fix seed - self.scope = fluid.global_scope() + self.scope = base.global_scope() self.place = place print("place:", place) diff --git a/test/collective/fleet/test_dgc_optimizer.py b/test/collective/fleet/test_dgc_optimizer.py index e356f842c9ea6..d2970e66d5bfa 100644 --- a/test/collective/fleet/test_dgc_optimizer.py +++ b/test/collective/fleet/test_dgc_optimizer.py @@ -16,7 +16,7 @@ import paddle from paddle import regularizer -from paddle.fluid import framework +from paddle.base import framework from paddle.nn import clip paddle.enable_static() diff --git a/test/collective/fleet/test_dist_mnist_dgc_nccl.py b/test/collective/fleet/test_dist_mnist_dgc_nccl.py index 4ef20da401263..1bc335a96e6fd 100644 --- a/test/collective/fleet/test_dist_mnist_dgc_nccl.py +++ b/test/collective/fleet/test_dist_mnist_dgc_nccl.py @@ -50,9 +50,9 @@ def _setup_config(self): self._use_dgc = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath("../../legacy_test/dist_mnist_dgc.py"), delta=1e-5, @@ -61,9 +61,9 @@ def test_dist_train(self): ) def tearDown(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): log_file = os.path.join( self.temp_dir.name, 'test_dist_mnist_dgc_nccl_tr0_err.log' ) diff --git a/test/collective/fleet/test_dist_mnist_gradient_merge.py b/test/collective/fleet/test_dist_mnist_gradient_merge.py index fb24cc910f6de..9152ecb8b6ec5 100644 --- a/test/collective/fleet/test_dist_mnist_gradient_merge.py +++ b/test/collective/fleet/test_dist_mnist_gradient_merge.py @@ -17,7 +17,7 @@ from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -30,7 +30,7 @@ def _setup_config(self): self._nccl2_reduce_layer = True def test_dist_train(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist_gradient_merge.py", delta=1e-5, @@ -47,7 +47,7 @@ def _setup_config(self): self._fuse_all_reduce = False def test_dist_train(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist_gradient_merge.py", delta=1e-5, @@ -67,7 +67,7 @@ def enable_avg(self): return False def test_dist_train(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): avg = str(self.enable_avg()) log_name = flag_name + "_raw_optimizer_gm_avg_" + avg self.check_with_place( diff --git a/test/collective/fleet/test_dist_se_resnext_dgc.py b/test/collective/fleet/test_dist_se_resnext_dgc.py index 8d39089d59763..e0ff1f607ef58 100644 --- a/test/collective/fleet/test_dist_se_resnext_dgc.py +++ b/test/collective/fleet/test_dist_se_resnext_dgc.py @@ -30,9 +30,9 @@ def _setup_config(self): @unittest.skip(reason="Skip unstable ci") def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath("../../dist_se_resnext.py"), delta=30, diff --git a/test/collective/fleet/test_distributed_strategy.py b/test/collective/fleet/test_distributed_strategy.py index 4d76d7d3ad00e..337efe6d1e028 100644 --- a/test/collective/fleet/test_distributed_strategy.py +++ b/test/collective/fleet/test_distributed_strategy.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed.transpiler.distribute_transpiler import ( DistributeTranspilerConfig, ServerRuntimeConfig, @@ -82,8 +82,8 @@ def test_geo_strategy(self): self.assertEqual(strategy._program_config.geo_sgd_need_push_nums, 5) self.assertEqual(strategy._build_strategy.async_mode, True) - # test set_build_strategy using fluid.BuildStrategy - build_strategy_class = fluid.BuildStrategy() + # test set_build_strategy using base.BuildStrategy + build_strategy_class = base.BuildStrategy() build_strategy_class.memory_optimize = False strategy.set_build_strategy(build_strategy_class) build_strategy = strategy.get_build_strategy() @@ -160,8 +160,8 @@ def test_async_strategy(self): trainer_runtime_config_illegal, ) - # test set_execute_strategy using fluid.ExecutionStrategy - exec_strategy_class = fluid.ExecutionStrategy() + # test set_execute_strategy using base.ExecutionStrategy + exec_strategy_class = base.ExecutionStrategy() exec_strategy_class.num_threads = 4 strategy.set_execute_strategy(exec_strategy_class) exec_strategy = strategy.get_execute_strategy() diff --git a/test/collective/fleet/test_fleet_amp_init.py b/test/collective/fleet/test_fleet_amp_init.py index e7263d1cf990b..e4815f5cd4d9f 100644 --- a/test/collective/fleet/test_fleet_amp_init.py +++ b/test/collective/fleet/test_fleet_amp_init.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -45,7 +45,7 @@ def mlp(input_x, input_y, hid_dim=128, label_dim=2): class TestFleetAMPInit(unittest.TestCase): def test_fleet_amp_init(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return main_program = paddle.static.Program() @@ -89,7 +89,7 @@ def test_fleet_amp_init(self): ) def test_fleet_amp_meta_optimizer_init(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return main_program = paddle.static.Program() diff --git a/test/collective/fleet/test_fleet_amp_meta_optimizer.py b/test/collective/fleet/test_fleet_amp_meta_optimizer.py index 75a7f445eea2b..9d9c56d7cadc1 100644 --- a/test/collective/fleet/test_fleet_amp_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_amp_meta_optimizer.py @@ -17,7 +17,7 @@ from fleet_meta_optimizer_base import TestFleetMetaOptimizer import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker from paddle.distributed.fleet.meta_optimizers import AMPOptimizer @@ -28,7 +28,7 @@ class TestFleetAMPOptimizer(TestFleetMetaOptimizer): def test_amp_optimizer_backward(self): """test amp optimizer backward""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) @@ -45,7 +45,7 @@ def test_amp_optimizer_backward(self): def test_amp_optimizer_backward_gradients(self): """test amp optimizer backward + gradients""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) @@ -55,7 +55,7 @@ def test_amp_optimizer_backward_gradients(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) opt._set_basic_info(avg_cost, role, opt, strategy) params_grads = opt.backward(avg_cost, startup_prog) - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): opt.apply_gradients(params_grads) ops = [op.type for op in avg_cost.block.ops] @@ -64,7 +64,7 @@ def test_amp_optimizer_backward_gradients(self): def test_amp_optimizer_backward_optimize(self): """test amp optimizer backward + optimizer""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) @@ -82,7 +82,7 @@ def test_amp_optimizer_backward_optimize(self): def test_amp_optimizer(self): """test amp""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -93,14 +93,14 @@ def test_amp_optimizer(self): def test_pure_fp16_optimizer(self): """test pure fp16""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'pure_fp16') self.optimizer(avg_cost, strategy, train_prog, startup_prog) params = train_prog.all_parameters() for param in train_prog.all_parameters(): - self.assertEqual(param.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(param.dtype, base.core.VarDesc.VarType.FP16) ops = [op.type for op in avg_cost.block.ops] self.assertIn('cast', ops) @@ -108,7 +108,7 @@ def test_pure_fp16_optimizer(self): def test_amp_distributed_optimizer(self): """test amp when distributed""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -124,7 +124,7 @@ def test_amp_distributed_optimizer(self): def test_amp_recompute_optimizer(self): """test amp + recompute""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') self.set_strategy(strategy, 'recompute') @@ -144,7 +144,7 @@ def test_amp_recompute_optimizer(self): def test_amp_recompute_lars_optimizer(self): """test amp + recompute""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') self.set_strategy(strategy, 'recompute') @@ -167,7 +167,7 @@ def test_amp_recompute_lars_optimizer(self): self.assertIn('lars_momentum', ops) def test_amp_recompute_lamb_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') self.set_strategy(strategy, 'recompute') diff --git a/test/collective/fleet/test_fleet_checkpoint.py b/test/collective/fleet/test_fleet_checkpoint.py index 4b86e6d57fd7a..82f2f416e189c 100644 --- a/test/collective/fleet/test_fleet_checkpoint.py +++ b/test/collective/fleet/test_fleet_checkpoint.py @@ -16,10 +16,10 @@ import unittest import paddle -from paddle import fluid +from paddle import base +from paddle.base.incubate.checkpoint.auto_checkpoint import ExeTrainStatus +from paddle.base.incubate.checkpoint.checkpoint_saver import CheckpointSaver from paddle.distributed.fleet.utils.fs import HDFSClient, LocalFS -from paddle.fluid.incubate.checkpoint.auto_checkpoint import ExeTrainStatus -from paddle.fluid.incubate.checkpoint.checkpoint_saver import CheckpointSaver from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.collective import fleet @@ -39,8 +39,8 @@ def _test_checkpoint(self, fs, dir_path): name='img', shape=[None, 28, 28], dtype='float32' ) label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - feeder = fluid.DataFeeder( - feed_list=[image, label], place=fluid.CPUPlace() + feeder = base.DataFeeder( + feed_list=[image, label], place=base.CPUPlace() ) predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') loss = paddle.nn.functional.cross_entropy( @@ -52,8 +52,8 @@ def _test_checkpoint(self, fs, dir_path): dist_optimizer = fleet.distributed_optimizer(optimizer) dist_optimizer.minimize(avg_loss) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) status = ExeTrainStatus() status.epoch_no = 2 diff --git a/test/collective/fleet/test_fleet_dgc_meta_optimizer.py b/test/collective/fleet/test_fleet_dgc_meta_optimizer.py index 057e0b0d6c509..feb4ff0a77e0e 100755 --- a/test/collective/fleet/test_fleet_dgc_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_dgc_meta_optimizer.py @@ -18,7 +18,7 @@ from fleet_meta_optimizer_base import TestFleetMetaOptimizer import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker from paddle.distributed.fleet.meta_optimizers import DGCOptimizer @@ -29,7 +29,7 @@ class TestFleetDGCOptimizer(TestFleetMetaOptimizer): def test_dgc_optimizer_backward(self): """test dgc optimizer backward""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') @@ -44,7 +44,7 @@ def test_dgc_optimizer_backward(self): def test_dgc_optimizer_gradients(self): """test dgc optimizer backward + gradients""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') @@ -53,7 +53,7 @@ def test_dgc_optimizer_gradients(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) dgc_opt._set_basic_info(avg_cost, role, opt, strategy) params_grads = dgc_opt.backward(avg_cost, startup_prog) - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): dgc_opt.apply_gradients(params_grads) ops = [op.type for op in avg_cost.block.ops] @@ -62,7 +62,7 @@ def test_dgc_optimizer_gradients(self): def test_dgc_optimizer_optimize(self): """test dgc optimizer backward + optimize""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') @@ -78,7 +78,7 @@ def test_dgc_optimizer_optimize(self): self.assertIn('dgc_momentum', ops) def test_dgc_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -88,7 +88,7 @@ def test_dgc_optimizer(self): self.assertIn('dgc_momentum', ops) def test_dgc_not_apply_with_adam(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') self.optimizer(avg_cost, strategy, train_prog, startup_prog, 'adam') @@ -101,7 +101,7 @@ def test_dgc_not_apply_with_one_worker(self): os.environ["PADDLE_TRAINER_ID"] = "0" os.environ["PADDLE_TRAINER_ENDPOINTS"] = "127.0.0.1:36001" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -111,7 +111,7 @@ def test_dgc_not_apply_with_one_worker(self): self.assertNotIn('dgc_momentum', ops) def test_dgc_recompute_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') self.set_strategy(strategy, 'recompute') @@ -131,7 +131,7 @@ def test_amp_recompute_lars_dgc_not_apply_optimizer(self): """test amp + recompute + lars + dgc, amp -/-> dgc, max_path is amp-->recompute-->lars """ - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'dgc') self.set_strategy(strategy, 'amp') diff --git a/test/collective/fleet/test_fleet_distributed_strategy.py b/test/collective/fleet/test_fleet_distributed_strategy.py index ba49cbf125a62..38ed379295f47 100644 --- a/test/collective/fleet/test_fleet_distributed_strategy.py +++ b/test/collective/fleet/test_fleet_distributed_strategy.py @@ -453,7 +453,7 @@ def test_strategy_prototxt(self): strategy.dgc = True localsgd_configs = {"k_steps": 5, "begin_step": 1} strategy.localsgd_configs = localsgd_configs - build_strategy = paddle.fluid.BuildStrategy() + build_strategy = paddle.base.BuildStrategy() build_strategy.enable_sequential_execution = True build_strategy.nccl_comm_num = 10 build_strategy.use_hierarchical_allreduce = True @@ -470,7 +470,7 @@ def test_strategy_prototxt(self): build_strategy.enable_backward_optimizer_op_deps = True build_strategy.trainers_endpoints = ["1", "2"] strategy.build_strategy = build_strategy - exe_strategy = paddle.fluid.ExecutionStrategy() + exe_strategy = paddle.base.ExecutionStrategy() exe_strategy.num_threads = 10 exe_strategy.num_iteration_per_drop_scope = 10 exe_strategy.num_iteration_per_run = 10 @@ -481,7 +481,7 @@ def test_strategy_prototxt(self): self.assertEqual(strategy.dgc, strategy2.dgc) def test_build_strategy(self): - build_strategy = paddle.fluid.BuildStrategy() + build_strategy = paddle.base.BuildStrategy() build_strategy.enable_sequential_execution = True build_strategy.nccl_comm_num = 10 build_strategy.use_hierarchical_allreduce = True @@ -502,7 +502,7 @@ def test_build_strategy(self): strategy.build_strategy = build_strategy def test_execution_strategy(self): - exe_strategy = paddle.fluid.ExecutionStrategy() + exe_strategy = paddle.base.ExecutionStrategy() exe_strategy.num_threads = 10 exe_strategy.num_iteration_per_drop_scope = 10 exe_strategy.num_iteration_per_run = 10 diff --git a/test/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py b/test/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py index f78f8319691f0..bb4c222725f60 100644 --- a/test/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -29,7 +29,7 @@ def setUp(self): os.environ["PADDLE_TRAINER_ENDPOINTS"] = "127.0.0.1:36001" def net(self, main_prog, startup_prog, dtype='float32'): - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): input_x = paddle.static.data(name="x", shape=[-1, 32], dtype=dtype) input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') @@ -53,7 +53,7 @@ def net(self, main_prog, startup_prog, dtype='float32'): def test_fp16_allreduce_optimizer(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.SGD(learning_rate=0.01) @@ -80,7 +80,7 @@ def test_fp16_allreduce_optimizer(self): def test_fp16_allreduce_not_apply_fp16_net(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog, dtype='float16') optimizer = paddle.optimizer.SGD(learning_rate=0.01) diff --git a/test/collective/fleet/test_fleet_gradient_merge_meta_optimizer.py b/test/collective/fleet/test_fleet_gradient_merge_meta_optimizer.py index 1590fcaa3f1a9..fc8e116f1e9e0 100644 --- a/test/collective/fleet/test_fleet_gradient_merge_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_gradient_merge_meta_optimizer.py @@ -24,8 +24,8 @@ class TestFleetGradientMergeMetaOptimizer(TestFleetMetaOptimizer): def test_gradient_merge_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'gradient_merge') @@ -36,8 +36,8 @@ def test_gradient_merge_optimizer(self): def test_recom_gm_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'gradient_merge') @@ -50,8 +50,8 @@ def test_recom_gm_optimizer(self): def test_gm_amp_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'gradient_merge') @@ -64,8 +64,8 @@ def test_gm_amp_optimizer(self): def test_gm_pure_fp16_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'gradient_merge') @@ -75,9 +75,7 @@ def test_gm_pure_fp16_optimizer(self): params = train_prog.all_parameters() for param in train_prog.all_parameters(): - self.assertEqual( - param.dtype, paddle.fluid.core.VarDesc.VarType.FP16 - ) + self.assertEqual(param.dtype, paddle.base.core.VarDesc.VarType.FP16) vars = [x.name for x in train_prog.list_vars()] self.assertIn('@GradientMerge', ''.join(vars)) diff --git a/test/collective/fleet/test_fleet_lamb_meta_optimizer.py b/test/collective/fleet/test_fleet_lamb_meta_optimizer.py index 5708578c951dd..f348af7875eb6 100755 --- a/test/collective/fleet/test_fleet_lamb_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_lamb_meta_optimizer.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -31,8 +31,8 @@ def setUp(self): ] = "127.0.0.1:36001,127.0.0.1:36002" def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -67,8 +67,8 @@ def net(self, main_prog, startup_prog): def test_lamb_optimizer(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Adam(learning_rate=0.01) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) @@ -80,8 +80,8 @@ def test_lamb_optimizer(self): def test_lamb_not_apply_with_momentum(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Momentum(learning_rate=0.1, momentum=0.9) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) @@ -93,8 +93,8 @@ def test_lamb_not_apply_with_momentum(self): def test_lamb_exclude_fn(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Adam(learning_rate=0.01) strategy.lamb_configs = { diff --git a/test/collective/fleet/test_fleet_lars_meta_optimizer.py b/test/collective/fleet/test_fleet_lars_meta_optimizer.py index 8f6e330719175..4d60aa381a8cb 100755 --- a/test/collective/fleet/test_fleet_lars_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_lars_meta_optimizer.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -31,8 +31,8 @@ def setUp(self): ] = "127.0.0.1:36001,127.0.0.1:36002" def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -69,8 +69,8 @@ def net(self, main_prog, startup_prog): def test_lars_optimizer(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) @@ -82,8 +82,8 @@ def test_lars_optimizer(self): def test_lars_not_apply_with_adam(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Adam(learning_rate=0.01) optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) @@ -95,8 +95,8 @@ def test_lars_not_apply_with_adam(self): def test_lars_exclude_fn(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - startup_prog = fluid.Program() - train_prog = fluid.Program() + startup_prog = base.Program() + train_prog = base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9) diff --git a/test/collective/fleet/test_fleet_localsgd_meta_optimizer.py b/test/collective/fleet/test_fleet_localsgd_meta_optimizer.py index 0a716b3ddb825..d331ef59410df 100644 --- a/test/collective/fleet/test_fleet_localsgd_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_localsgd_meta_optimizer.py @@ -17,14 +17,14 @@ from fleet_meta_optimizer_base import TestFleetMetaOptimizer import paddle -from paddle import fluid +from paddle import base paddle.enable_static() class TestFleetLocalSGDMetaOptimizer(TestFleetMetaOptimizer): def test_localsgd_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'localsgd') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -40,7 +40,7 @@ def test_localsgd_optimizer(self): self.assertIn('@SNAPSHOT', ''.join(outs)) def test_localsgd_amp_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'localsgd') self.set_strategy(strategy, 'amp') @@ -63,7 +63,7 @@ def test_localsgd_amp_optimizer(self): class TestFleetAdaptiveLocalSGDMetaOptimizer(TestFleetMetaOptimizer): def test_adaptive_localsgd_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'adaptive_localsgd') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -79,7 +79,7 @@ def test_adaptive_localsgd_optimizer(self): self.assertIn('@SNAPSHOT', ''.join(outs)) def test_localsgd_amp_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'adaptive_localsgd') self.set_strategy(strategy, 'amp') diff --git a/test/collective/fleet/test_fleet_meta_optimizer_base.py b/test/collective/fleet/test_fleet_meta_optimizer_base.py index 7f8db79edf22b..326f1bc64d48c 100755 --- a/test/collective/fleet/test_fleet_meta_optimizer_base.py +++ b/test/collective/fleet/test_fleet_meta_optimizer_base.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker from paddle.distributed.fleet.meta_optimizers.meta_optimizer_base import ( @@ -25,8 +25,8 @@ class TestFleetMetaOptimizerBase(unittest.TestCase): def net(main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) input_x = paddle.static.data( @@ -61,7 +61,7 @@ def net(main_prog, startup_prog): ) return None - net(fluid.default_startup_program(), fluid.default_main_program()) + net(base.default_startup_program(), base.default_main_program()) if __name__ == "__main__": diff --git a/test/collective/fleet/test_fleet_pipeline_meta_optimizer.py b/test/collective/fleet/test_fleet_pipeline_meta_optimizer.py index 2780c51e26d6d..78ad33ce44f28 100644 --- a/test/collective/fleet/test_fleet_pipeline_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_pipeline_meta_optimizer.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid, static +from paddle import base, static from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -75,7 +75,7 @@ def test_pipeline_optimizer(self): train_prog, startup_prog = static.Program(), static.Program() with static.program_guard(train_prog, startup_prog): - with fluid.unique_name.guard(): + with base.unique_name.guard(): avg_cost = self.net() optimizer = paddle.optimizer.Adam(0.01) @@ -99,7 +99,7 @@ def test_pipeline_amp_optimizer(self): train_prog, startup_prog = static.Program(), static.Program() with static.program_guard(train_prog, startup_prog): - with fluid.unique_name.guard(): + with base.unique_name.guard(): avg_cost = self.net() optimizer = paddle.optimizer.Adam(0.01) diff --git a/test/collective/fleet/test_fleet_pipeline_meta_optimizer_with_recompute.py b/test/collective/fleet/test_fleet_pipeline_meta_optimizer_with_recompute.py index a0f3b1d9bb35f..f1a0b7376c102 100644 --- a/test/collective/fleet/test_fleet_pipeline_meta_optimizer_with_recompute.py +++ b/test/collective/fleet/test_fleet_pipeline_meta_optimizer_with_recompute.py @@ -33,7 +33,7 @@ def test_pipeline_optimizer(self): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - with paddle.fluid.device_guard("gpu:0"): + with paddle.base.device_guard("gpu:0"): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -45,7 +45,7 @@ def test_pipeline_optimizer(self): fc_5 = paddle.static.nn.fc(x=fc_4, size=64, activation='tanh') fc_6 = paddle.static.nn.fc(x=fc_5, size=64, activation='tanh') - with paddle.fluid.device_guard("gpu:1"): + with paddle.base.device_guard("gpu:1"): fc_7 = paddle.static.nn.fc(x=fc_6, size=64, activation='tanh') prediction = paddle.static.nn.fc( x=[fc_7], size=2, activation='softmax' diff --git a/test/collective/fleet/test_fleet_qat_meta_optimizer.py b/test/collective/fleet/test_fleet_qat_meta_optimizer.py index 7559bd01973d3..4d6be6bb6aa7f 100644 --- a/test/collective/fleet/test_fleet_qat_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_qat_meta_optimizer.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn from paddle.distributed import fleet paddle.enable_static() @@ -48,8 +48,8 @@ def setup_strategy(self, strategy): strategy.qat = True def generate_program(self, strategy): - train_prog, startup_prog = fluid.Program(), fluid.Program() - with fluid.program_guard(train_prog, startup_prog): + train_prog, startup_prog = base.Program(), base.Program() + with base.program_guard(train_prog, startup_prog): input_x = paddle.static.data( name='X', shape=[self.batch_size, self.input_size], @@ -73,12 +73,12 @@ def generate_program(self, strategy): def execute_program(self, train_prog, startup_prog, input_x, input_y): place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[input_x, input_y], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[input_x, input_y], place=place) exe.run(startup_prog) data = ( np.random.randn(self.batch_size, self.input_size), @@ -108,9 +108,9 @@ def test_fleet_with_qat(self): optimizer, ) = self.generate_program(dist_strategy) place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) eval_prog = train_prog.clone(for_test=True) optimizer.qat_init( diff --git a/test/collective/fleet/test_fleet_recompute_meta_optimizer.py b/test/collective/fleet/test_fleet_recompute_meta_optimizer.py index e0315fbf407ff..54708d69e26af 100644 --- a/test/collective/fleet/test_fleet_recompute_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_recompute_meta_optimizer.py @@ -17,7 +17,7 @@ from fleet_meta_optimizer_base import TestFleetMetaOptimizer import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet.meta_optimizers import RecomputeOptimizer paddle.enable_static() @@ -26,7 +26,7 @@ class TestFleetRecomputeMetaOptimizer(TestFleetMetaOptimizer): def test_recompute_optimizer_backward(self): """test recompute optimizer backward""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') @@ -42,7 +42,7 @@ def test_recompute_optimizer_backward(self): def test_recompute_optimizer_backward_gradients(self): """test recompute optimizer backward + gradients""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') @@ -50,7 +50,7 @@ def test_recompute_optimizer_backward_gradients(self): opt = RecomputeOptimizer(opt) opt.user_defined_strategy = strategy params_grads = opt.backward(avg_cost, startup_prog) - with fluid.program_guard(train_prog, startup_prog): + with base.program_guard(train_prog, startup_prog): opt.apply_gradients(params_grads) outs = [ @@ -60,7 +60,7 @@ def test_recompute_optimizer_backward_gradients(self): def test_recompute_optimizer_backward_optimize(self): """test recompute optimizer backward + optimize""" - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') @@ -76,7 +76,7 @@ def test_recompute_optimizer_backward_optimize(self): self.assertIn('subprog', ''.join(outs)) def test_recompute_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') self.optimizer(avg_cost, strategy, train_prog, startup_prog) @@ -88,7 +88,7 @@ def test_recompute_optimizer(self): self.assertIn('subprog', ''.join(outs)) def test_recompute_lars_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') self.set_strategy(strategy, 'lars') @@ -103,7 +103,7 @@ def test_recompute_lars_optimizer(self): self.assertIn('lars_momentum', ops) def test_recompute_lamb_optimizer(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute') self.set_strategy(strategy, 'lamb') @@ -118,7 +118,7 @@ def test_recompute_lamb_optimizer(self): self.assertIn('lamb', ops) def test_recompute_offload(self): - train_prog, startup_prog = fluid.Program(), fluid.Program() + train_prog, startup_prog = base.Program(), base.Program() avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'recompute-offload') self.optimizer(avg_cost, strategy, train_prog, startup_prog) diff --git a/test/collective/fleet/test_fleet_sharding_meta_optimizer.py b/test/collective/fleet/test_fleet_sharding_meta_optimizer.py index 9df8b670c7e99..8eaee6344cbff 100755 --- a/test/collective/fleet/test_fleet_sharding_meta_optimizer.py +++ b/test/collective/fleet/test_fleet_sharding_meta_optimizer.py @@ -28,8 +28,8 @@ class TestFleetShardingMetaOptimizer(TestFleetMetaOptimizer): def test_sharding_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -106,8 +106,8 @@ def test_sharding_optimizer(self): def test_sharding_amp_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -212,8 +212,8 @@ def test_sharding_amp_optimizer(self): def test_sharding_recompute_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -296,8 +296,8 @@ def test_sharding_recompute_optimizer(self): def test_sharding_amp_recompute_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -421,8 +421,8 @@ def test_sharding_amp_recompute_optimizer(self): def test_sharding_amp_asp_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -534,8 +534,8 @@ def test_sharding_amp_asp_optimizer(self): def test_sharding_weight_decay(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -617,8 +617,8 @@ def test_sharding_weight_decay(self): def test_sharding_gradient_clip(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -708,8 +708,8 @@ def test_sharding_gradient_clip(self): def test_sharding_clone_for_test(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.net(train_prog, startup_prog) self.set_strategy(strategy, 'sharding') @@ -766,8 +766,8 @@ def setUp(self): def test_sharding_with_mp(self): # NOTE(JZ-LIANG) MP parallelism need user to build model with MP API train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, _ = self.net(train_prog, startup_prog) strategy = paddle.distributed.fleet.DistributedStrategy() @@ -817,8 +817,8 @@ def test_sharding_with_mp(self): def test_sharding_hybrid_dp(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, _ = self.net(train_prog, startup_prog) strategy = paddle.distributed.fleet.DistributedStrategy() @@ -939,8 +939,8 @@ def test_sharding_hybrid_dp(self): def test_sharding_hybrid_dp_gm(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, _ = self.net(train_prog, startup_prog) strategy = paddle.distributed.fleet.DistributedStrategy() @@ -1072,8 +1072,8 @@ def test_sharding_hybrid_dp_gm(self): def test_sharding_with_pp(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.sharding = True @@ -1239,8 +1239,8 @@ def test_sharding_with_pp(self): def test_sharding_dp_with_allreduce_fuse(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, _ = self.net(train_prog, startup_prog) strategy = paddle.distributed.fleet.DistributedStrategy() @@ -1271,8 +1271,8 @@ def test_sharding_dp_with_allreduce_fuse(self): def test_hybrid_with_mp_pp_amp_gclip(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') @@ -1509,8 +1509,8 @@ def test_hybrid_with_mp_pp_amp_gclip(self): def test_hybrid_with_mp_pp_amp_gclip_for_optimizer(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) self.set_strategy(strategy, 'amp') @@ -1777,8 +1777,8 @@ def test_hybrid_with_mp_pp_amp_gclip_for_optimizer(self): def test_hybrid_with_pp_dp_amp_fp16allreduce(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -1977,8 +1977,8 @@ def test_hybrid_with_pp_dp_amp_fp16allreduce(self): def test_hybrid_with_sharding_pp_amp_fp16allreduce_in_optimize(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -2078,8 +2078,8 @@ def test_hybrid_with_sharding_pp_amp_fp16allreduce_in_optimize(self): def test_hybrid_with_pp_dp_amp_fp16allreduce_optimize_cast(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -2286,8 +2286,8 @@ def test_hybrid_with_pp_dp_amp_fp16allreduce_optimize_cast(self): def test_hybrid_with_pp_dp_amp_fp16allreduce_optimize_offload(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -2517,8 +2517,8 @@ def test_hybrid_with_pp_dp_amp_fp16allreduce_optimize_cast_with_gradient_fuse( self, ): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -2716,8 +2716,8 @@ def test_hybrid_with_pp_dp_amp_fp16allreduce_optimize_cast_with_gradient_fuse( def test_hybrid_with_pp_dp_amp_with_gradient_fuse(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -2898,8 +2898,8 @@ def test_hybrid_with_pp_dp_amp_with_gradient_fuse(self): def test_hybrid_with_pp_dp_amp_with_gradient_fuse_and_avg_after_sum(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.amp = True @@ -3052,8 +3052,8 @@ def test_hybrid_with_pp_dp_amp_with_gradient_fuse_and_avg_after_sum(self): def test_hybrid_with_pp_dp_with_gradient_fuse_and_avg_after_sum(self): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.sharding = True @@ -3180,8 +3180,8 @@ def test_hybrid_with_pp_dp_with_amp_no_dynamic_gradient_fuse_and_avg_after_sum( self, ): train_prog, startup_prog = ( - paddle.fluid.Program(), - paddle.fluid.Program(), + paddle.base.Program(), + paddle.base.Program(), ) avg_cost, strategy = self.pp_net(train_prog, startup_prog) strategy.sharding = True diff --git a/test/collective/fleet/test_fleet_tensor_parallel_extra_sync.py b/test/collective/fleet/test_fleet_tensor_parallel_extra_sync.py index eba8f6926e0b4..70724709329fd 100644 --- a/test/collective/fleet/test_fleet_tensor_parallel_extra_sync.py +++ b/test/collective/fleet/test_fleet_tensor_parallel_extra_sync.py @@ -21,7 +21,7 @@ paddle.enable_static() -class TensorParallelNet(paddle.fluid.dygraph.Layer): +class TensorParallelNet(paddle.base.dygraph.Layer): def __init__(self, hidden_size): super().__init__() self.embedding = paddle.nn.Embedding(hidden_size, hidden_size) diff --git a/test/collective/fleet/test_hdfs1.py b/test/collective/fleet/test_hdfs1.py index 418a9ae3a68fa..a91241fed81cf 100644 --- a/test/collective/fleet/test_hdfs1.py +++ b/test/collective/fleet/test_hdfs1.py @@ -17,7 +17,7 @@ from hdfs_test_utils import FSTestBase -from paddle import fluid +from paddle import base from paddle.distributed.fleet.utils.fs import FSTimeOut, HDFSClient java_home = os.environ["JAVA_HOME"] @@ -42,7 +42,7 @@ def test_timeout(self): except FSTimeOut as e: print(f"execute mv {src} to {dst} timeout") - ret, output = fluid.core.shell_execute_cmd(cmd, 6 * 1000, 2 * 1000) + ret, output = base.core.shell_execute_cmd(cmd, 6 * 1000, 2 * 1000) self.assertNotEqual(ret, 0) print(f"second mv ret:{ret} output:{output}") diff --git a/test/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py b/test/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py index 05bd76127542c..7bfdcf5723f2a 100644 --- a/test/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py +++ b/test/collective/fleet/test_imperative_auto_mixed_precision_for_eager.py @@ -20,12 +20,12 @@ from test_imperative_resnet import ResNet, optimizer_setting, train_parameters import paddle -from paddle import fluid, nn +from paddle import base, nn from paddle.autograd import PyLayer from paddle.static import InputSpec -if fluid.core.is_compiled_with_cuda(): - fluid.set_flags({"FLAGS_cudnn_deterministic": True}) +if base.core.is_compiled_with_cuda(): + base.set_flags({"FLAGS_cudnn_deterministic": True}) class SimpleConv(paddle.nn.Layer): @@ -56,38 +56,38 @@ def forward(self, inputs): class TestAutoCast(unittest.TestCase): def amp_guard_white_op(self): data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): conv2d = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - data = fluid.dygraph.to_variable(data) + data = base.dygraph.to_variable(data) with paddle.amp.amp_guard(True): out_fp16 = conv2d(data) with paddle.amp.amp_guard(False): out_fp32 = conv2d(data) - self.assertTrue(data.dtype == fluid.core.VarDesc.VarType.FP32) - self.assertTrue(out_fp16.dtype == fluid.core.VarDesc.VarType.FP16) - self.assertTrue(out_fp32.dtype == fluid.core.VarDesc.VarType.FP32) + self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32) + self.assertTrue(out_fp16.dtype == base.core.VarDesc.VarType.FP16) + self.assertTrue(out_fp32.dtype == base.core.VarDesc.VarType.FP32) def test_amp_guard_white_op(self): self.amp_guard_white_op() def amp_guard_black_op(self): data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32') - with fluid.dygraph.guard(): - data = fluid.dygraph.to_variable(data) + with base.dygraph.guard(): + data = base.dygraph.to_variable(data) with paddle.amp.amp_guard(True): out_fp32 = paddle.mean(data) - self.assertTrue(data.dtype == fluid.core.VarDesc.VarType.FP32) - self.assertTrue(out_fp32.dtype == fluid.core.VarDesc.VarType.FP32) + self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32) + self.assertTrue(out_fp32.dtype == base.core.VarDesc.VarType.FP32) def test_amp_guard_black_op(self): self.amp_guard_black_op() def custom_op_list(self): - with fluid.dygraph.guard(): - tracer = fluid.framework._dygraph_tracer() + with base.dygraph.guard(): + tracer = base.framework._dygraph_tracer() base_white_list = paddle.amp.white_list()["float16"]["O1"] base_black_list = paddle.amp.black_list()["float16"]["O1"] with paddle.amp.amp_guard( @@ -129,7 +129,7 @@ def custom_op_list_exception(self): inp_np = np.random.random(size=[1, 3, 128, 128]).astype(np.float32) def func(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = SimpleConv( num_channels=3, num_filters=64, @@ -140,7 +140,7 @@ def func(): with paddle.amp.amp_guard( custom_white_list=["conv2d"], custom_black_list=["conv2d"] ): - inp = fluid.dygraph.to_variable(inp_np) + inp = base.dygraph.to_variable(inp_np) out = model(inp) self.assertRaises(ValueError, func) @@ -150,9 +150,9 @@ def test_custom_op_list_exception(self): def amp_guard_upsupported_fp16_op(self): data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): conv2d = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - data = fluid.dygraph.to_variable(data) + data = base.dygraph.to_variable(data) with paddle.amp.amp_guard(True): out_amp_fp16 = conv2d(data) out_amp_fp32 = paddle.expand_as( @@ -164,14 +164,14 @@ def amp_guard_upsupported_fp16_op(self): out_purefp16_fp32 = paddle.expand_as( out_purefp16_fp16, out_purefp16_fp16 ) # expand_as_v2 has no fp16 kernel - self.assertTrue(data.dtype == fluid.core.VarDesc.VarType.FP32) - self.assertTrue(out_amp_fp16.dtype == fluid.core.VarDesc.VarType.FP16) - self.assertTrue(out_amp_fp32.dtype == fluid.core.VarDesc.VarType.FP32) + self.assertTrue(data.dtype == base.core.VarDesc.VarType.FP32) + self.assertTrue(out_amp_fp16.dtype == base.core.VarDesc.VarType.FP16) + self.assertTrue(out_amp_fp32.dtype == base.core.VarDesc.VarType.FP32) self.assertTrue( - out_purefp16_fp16.dtype == fluid.core.VarDesc.VarType.FP16 + out_purefp16_fp16.dtype == base.core.VarDesc.VarType.FP16 ) self.assertTrue( - out_purefp16_fp32.dtype == fluid.core.VarDesc.VarType.FP32 + out_purefp16_fp32.dtype == base.core.VarDesc.VarType.FP32 ) def test_amp_guard_upsupported_fp16_op(self): @@ -180,9 +180,9 @@ def test_amp_guard_upsupported_fp16_op(self): def mode_exception(self): def func(): data = np.random.uniform(-1, 1, [10, 3, 32, 32]).astype('float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): conv2d = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) - data = fluid.dygraph.to_variable(data) + data = base.dygraph.to_variable(data) with paddle.amp.amp_guard(level='O'): out = conv2d(data) @@ -196,7 +196,7 @@ class TestAmpScaler(unittest.TestCase): def scale(self): if not paddle.amp.is_float16_supported(): return - with fluid.dygraph.guard(): + with base.dygraph.guard(): with paddle.amp.auto_cast(dtype='float16'): data = paddle.rand([10, 1024]) scaler = paddle.amp.AmpScaler(init_loss_scaling=1024) @@ -214,7 +214,7 @@ def minimize(self): def run_simple_conv(inp_np, use_scaler=True): paddle.seed(10) paddle.framework.random._manual_program_seed(10) - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = SimpleConv( num_channels=3, num_filters=64, @@ -226,7 +226,7 @@ def run_simple_conv(inp_np, use_scaler=True): learning_rate=0.01, parameters=model.parameters() ) scaler = paddle.amp.AmpScaler(init_loss_scaling=1024) - data = fluid.dygraph.to_variable(inp_np) + data = base.dygraph.to_variable(inp_np) out = model(data) loss = paddle.mean(out) @@ -275,7 +275,7 @@ def step(self): def run_simple_conv(inp_np, use_scaler=True): paddle.seed(10) paddle.framework.random._manual_program_seed(10) - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = SimpleConv( num_channels=3, num_filters=64, @@ -287,7 +287,7 @@ def run_simple_conv(inp_np, use_scaler=True): learning_rate=0.01, parameters=model.parameters() ) scaler = paddle.amp.GradScaler(init_loss_scaling=1024) - data = fluid.dygraph.to_variable(inp_np) + data = base.dygraph.to_variable(inp_np) out = model(data) loss = paddle.mean(out) @@ -320,7 +320,7 @@ def test_step(self): def nan_inf(self): inp_np = np.random.random(size=[1, 3, 128, 128]).astype(np.float32) inp_np[0][1][2][3] = np.nan - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = SimpleConv( num_channels=3, num_filters=64, @@ -335,7 +335,7 @@ def nan_inf(self): learning_rate=0.01, parameters=model.parameters() ) scaler = paddle.amp.AmpScaler(init_loss_scaling=1024) - data = fluid.dygraph.to_variable(inp_np) + data = base.dygraph.to_variable(inp_np) with paddle.amp.auto_cast(dtype='float16'): out = model(data) loss = paddle.mean(out) @@ -408,7 +408,7 @@ def test_step_update_exception(self): self.step_update_exception() def test_get_and_set(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): scaler = paddle.amp.GradScaler( enable=True, init_loss_scaling=1024, @@ -437,7 +437,7 @@ def test_get_and_set(self): self.assertEqual(scaler.get_init_loss_scaling() == 100, True) def test_state_dict_and_load_state_dict(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): scaler1 = paddle.amp.GradScaler( enable=True, init_loss_scaling=14, @@ -515,7 +515,7 @@ def train_resnet( batch_size=batch_size, drop_last=True, ) - train_loader = fluid.io.DataLoader.from_generator( + train_loader = base.io.DataLoader.from_generator( capacity=4, use_double_buffer=True, iterable=True, @@ -566,7 +566,7 @@ def train_resnet( if param.trainable: np_array = np.array(param._grad_ivar().value().get_tensor()) dy_grad_value[ - param.name + fluid.core.grad_var_suffix() + param.name + base.core.grad_var_suffix() ] = np_array resnet.clear_gradients() @@ -585,7 +585,7 @@ def train_resnet( def test_with_state_dict(self): def func_isinstance(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_use_state_dict = self.train_resnet( enable_amp=True, use_data_loader=True, use_save_load=True ) @@ -603,7 +603,7 @@ def func_isinstance(): class TestAmpDecorator(unittest.TestCase): def test_mode_exception(self): def func(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) opt = paddle.optimizer.SGD(parameters=model.parameters()) model, opt = paddle.amp.decorate( @@ -619,7 +619,7 @@ def __init__(self): print("A fake Model") model = MyModel() - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.amp.decorate(models=model, optimizers=None, level='O2') self.assertRaises(TypeError, test_error_model) @@ -627,7 +627,7 @@ def __init__(self): def test_error_distributed_model(): model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) model = paddle.DataParallel(model) - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = paddle.amp.decorate(models=model, level='O2') self.assertRaises(RuntimeError, test_error_distributed_model) @@ -639,7 +639,7 @@ def __init__(self): model = paddle.nn.Conv2D(3, 2, 3, bias_attr=False) opt = MyOptimizer() - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.amp.decorate(models=model, optimizers=opt, level='O2') self.assertRaises(TypeError, test_error_optimizer) @@ -809,7 +809,7 @@ def train_resnet( batch_size=batch_size, drop_last=True, ) - train_loader = fluid.io.DataLoader.from_generator( + train_loader = base.io.DataLoader.from_generator( capacity=4, use_double_buffer=True, iterable=True, @@ -869,7 +869,7 @@ def train_resnet( if param.trainable: np_array = np.array(param._grad_ivar().value().get_tensor()) dy_grad_value[ - param.name + fluid.core.grad_var_suffix() + param.name + base.core.grad_var_suffix() ] = np_array resnet.clear_gradients() @@ -909,7 +909,7 @@ def train_resnet( def test_with_save_load(self): def func_isinstance(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_use_save_load = self.train_resnet( enable_amp=True, use_data_loader=True, use_save_load=True ) @@ -1101,7 +1101,7 @@ def train_resnet( batch_size=batch_size, drop_last=True, ) - train_loader = fluid.io.DataLoader.from_generator( + train_loader = base.io.DataLoader.from_generator( capacity=4, use_double_buffer=True, iterable=True, @@ -1157,7 +1157,7 @@ def train_resnet( if param.trainable: np_array = np.array(param._grad_ivar().value().get_tensor()) dy_grad_value[ - param.name + fluid.core.grad_var_suffix() + param.name + base.core.grad_var_suffix() ] = np_array resnet.clear_gradients() @@ -1171,7 +1171,7 @@ def train_resnet( def test_resnet(self): def func_isinstance(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_fp32 = self.train_resnet(enable_amp=False) out_amp = self.train_resnet(enable_amp=True) out_pure_fp16 = self.train_resnet(enable_amp=True, level='O2') @@ -1187,7 +1187,7 @@ def func_isinstance(): def test_with_data_loader(self): def func_isinstance(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_fp32 = self.train_resnet( enable_amp=False, use_data_loader=True ) @@ -1209,7 +1209,7 @@ def func_isinstance(): def test_param_group(self): def func_isinstance(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_fp32 = self.train_resnet( enable_amp=False, use_data_loader=True, use_param_group=True ) @@ -1244,7 +1244,7 @@ def train_resnet(self, enable_amp=True, level='O1'): batch_size = train_parameters["batch_size"] batch_num = 1 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -1291,8 +1291,8 @@ def train_resnet(self, enable_amp=True, level='O1'): .astype('int64') .reshape(-1, 1) ) - img = fluid.dygraph.to_variable(dy_x_data) - label = fluid.dygraph.to_variable(y_data) + img = base.dygraph.to_variable(dy_x_data) + label = base.dygraph.to_variable(y_data) label.stop_gradient = True with paddle.amp.amp_guard(enable=enable_amp, level=level): out = resnet(img) @@ -1316,7 +1316,7 @@ def train_resnet(self, enable_amp=True, level='O1'): param._grad_ivar().value().get_tensor() ) dy_grad_value[ - param.name + fluid.core.grad_var_suffix() + param.name + base.core.grad_var_suffix() ] = np_array resnet.clear_gradients() @@ -1350,16 +1350,14 @@ class TestLayerNormFp16(unittest.TestCase): def test_layer_norm_fp16(self): def func_isinstance(): - if fluid.is_compiled_with_cuda(): - with fluid.dygraph.guard(fluid.CUDAPlace(0)): + if base.is_compiled_with_cuda(): + with base.dygraph.guard(base.CUDAPlace(0)): x = paddle.rand([2, 2, 2, 3]) layer_norm = paddle.nn.LayerNorm(x.shape[1:]) with paddle.amp.auto_cast(custom_white_list=['layer_norm']): out = layer_norm(x) - self.assertTrue( - out.dtype == fluid.core.VarDesc.VarType.FP16 - ) + self.assertTrue(out.dtype == base.core.VarDesc.VarType.FP16) func_isinstance() @@ -1387,8 +1385,8 @@ def train(self, enable_amp=True, amp_level='O1'): def test_bf16(self): def func_isinstance(): if ( - fluid.core.is_compiled_with_cuda() - and fluid.core.is_bfloat16_supported(paddle.CUDAPlace(0)) + base.core.is_compiled_with_cuda() + and base.core.is_bfloat16_supported(paddle.CUDAPlace(0)) ): out_fp32 = self.train(enable_amp=False) out_bf16_O1 = self.train(enable_amp=True, amp_level='O1') @@ -1432,7 +1430,7 @@ def backward(ctx, grad): class TestAmpWithHook(unittest.TestCase): def test_hook_change_dtype(self): def func_isinstance(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): v = paddle.rand([3, 3]) v.stop_gradient = False @@ -1452,7 +1450,7 @@ def foo(grad): def test_hook_change_place(self): def func_isinstance(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): v = paddle.rand([3, 3]) v.stop_gradient = False diff --git a/test/collective/fleet/test_parallel_dygraph_control_flow.py b/test/collective/fleet/test_parallel_dygraph_control_flow.py index 71ebfc8b109ee..ee5ec6a01edc7 100644 --- a/test/collective/fleet/test_parallel_dygraph_control_flow.py +++ b/test/collective/fleet/test_parallel_dygraph_control_flow.py @@ -17,7 +17,7 @@ from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -30,7 +30,7 @@ def _setup_config(self): self._find_unused_parameters = True def test_net(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_control_flow_same.py", delta=1e-5, @@ -65,7 +65,7 @@ def _setup_config(self): self._find_unused_parameters = True def test_net(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_control_flow_different.py", delta=1e-5, diff --git a/test/collective/fleet/test_parallel_dygraph_mnist.py b/test/collective/fleet/test_parallel_dygraph_mnist.py index f3c8b8f5c6d56..352ef30ac8d9d 100644 --- a/test/collective/fleet/test_parallel_dygraph_mnist.py +++ b/test/collective/fleet/test_parallel_dygraph_mnist.py @@ -19,7 +19,7 @@ from legacy_test.spawn_runner_base import TestDistSpawnRunner from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -32,7 +32,7 @@ def _setup_config(self): self._find_unused_parameters = True def test_mnist(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath("../../legacy_test/parallel_dygraph_mnist.py"), delta=1e-5, @@ -52,7 +52,7 @@ def _setup_config(self): self._enforce_place = "XPU" def test_mnist_xpu(self): - if fluid.core.is_compiled_with_xpu(): + if base.core.is_compiled_with_xpu(): self.check_with_place( os.path.abspath("../../legacy_test/parallel_dygraph_mnist.py"), delta=1e-4, @@ -63,7 +63,7 @@ def test_mnist_xpu(self): class TestParallelDygraphMnistSpawn(TestDistSpawnRunner): def test_mnist_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn(test_class=TestMnist, delta=1e-5) @@ -77,7 +77,7 @@ def _setup_config(self): self._find_unused_parameters = False def test_mnist(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath("../../legacy_test/parallel_dygraph_mnist.py"), delta=1e-5, @@ -95,7 +95,7 @@ def _setup_config(self): self._use_fleet_api = True def test_mnist(self): - if fluid.core.is_compiled_with_xpu(): + if base.core.is_compiled_with_xpu(): self.check_with_place( os.path.abspath("../../legacy_test/parallel_dygraph_mnist.py"), delta=1e-4, diff --git a/test/collective/fleet/test_parallel_dygraph_no_sync.py b/test/collective/fleet/test_parallel_dygraph_no_sync.py index 180eb65cffc42..05b575afe9900 100644 --- a/test/collective/fleet/test_parallel_dygraph_no_sync.py +++ b/test/collective/fleet/test_parallel_dygraph_no_sync.py @@ -21,7 +21,7 @@ from parallel_dygraph_no_sync_control_flow import TestNoSyncControlFlow from parallel_dygraph_no_sync_unused_params import TestNoSyncUnusedParam -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -34,7 +34,7 @@ def _setup_config(self): self._find_unused_parameters = False def test_no_sync(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_no_sync.py", delta=1e-5, @@ -51,7 +51,7 @@ def _setup_config(self): self._find_unused_parameters = True def test_no_sync_ununsed_param(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_no_sync_unused_params.py", delta=1e-5, @@ -68,7 +68,7 @@ def _setup_config(self): self._find_unused_parameters = True def test_no_sync_control_flow(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_no_sync_control_flow.py", delta=1e-5, @@ -79,7 +79,7 @@ def test_no_sync_control_flow(self): class TestParallelDygraphNoSyncSpawn(TestDistSpawnRunner): def test_no_sync_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn(test_class=TestNoSync, delta=1e-5) @@ -88,7 +88,7 @@ def _args_config(self, args): args.find_unused_parameters = True def test_no_sync_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestNoSyncUnusedParam, delta=1e-5 ) @@ -99,7 +99,7 @@ def _args_config(self, args): args.find_unused_parameters = True def test_no_sync_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestNoSyncControlFlow, delta=1e-5 ) diff --git a/test/collective/fleet/test_parallel_dygraph_qat.py b/test/collective/fleet/test_parallel_dygraph_qat.py index 5457e97b27df9..a5b736ce5b291 100644 --- a/test/collective/fleet/test_parallel_dygraph_qat.py +++ b/test/collective/fleet/test_parallel_dygraph_qat.py @@ -18,7 +18,7 @@ import time import unittest -from paddle import fluid +from paddle import base from paddle.distributed.utils.launch_utils import ( TrainerProc, find_free_ports, @@ -107,8 +107,8 @@ def start_local_trainers( class TestMultipleGpus(unittest.TestCase): def run_2gpu(self, target_file_name, eager_mode=True): if ( - not fluid.core.is_compiled_with_cuda() - or fluid.core.get_cuda_device_count() == 0 + not base.core.is_compiled_with_cuda() + or base.core.get_cuda_device_count() == 0 ): return diff --git a/test/collective/fleet/test_parallel_dygraph_se_resnext.py b/test/collective/fleet/test_parallel_dygraph_se_resnext.py index 6916d72745314..124762096cef1 100644 --- a/test/collective/fleet/test_parallel_dygraph_se_resnext.py +++ b/test/collective/fleet/test_parallel_dygraph_se_resnext.py @@ -19,7 +19,7 @@ from spawn_runner_base import TestDistSpawnRunner from test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -31,7 +31,7 @@ def _setup_config(self): self._dygraph = True def test_se_resnext(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_se_resnext.py", delta=0.01, @@ -42,7 +42,7 @@ def test_se_resnext(self): class TestParallelDygraphSeResNeXtSpawn(TestDistSpawnRunner): def test_se_resnext_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestSeResNeXt, delta=0.01 ) diff --git a/test/collective/fleet/test_parallel_dygraph_sparse_embedding.py b/test/collective/fleet/test_parallel_dygraph_sparse_embedding.py index ebcb01c3dbc2a..48be7158ea46c 100644 --- a/test/collective/fleet/test_parallel_dygraph_sparse_embedding.py +++ b/test/collective/fleet/test_parallel_dygraph_sparse_embedding.py @@ -22,7 +22,7 @@ from spawn_runner_base import TestDistSpawnRunner from test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -34,7 +34,7 @@ def _setup_config(self): self._dygraph = True def test_sparse_embedding(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_sparse_embedding.py" @@ -52,7 +52,7 @@ def _setup_config(self): self._dygraph = True def test_sparse_embedding_fp64(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_sparse_embedding_fp64.py" @@ -65,7 +65,7 @@ def test_sparse_embedding_fp64(self): class TestParallelDygraphSparseEmdeddingSpawn(TestDistSpawnRunner): def test_sparse_embedding_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestSparseEmbedding, delta=1e-5 ) diff --git a/test/collective/fleet/test_parallel_dygraph_sparse_embedding_over_height.py b/test/collective/fleet/test_parallel_dygraph_sparse_embedding_over_height.py index ee95b428fdf26..b92707d407719 100644 --- a/test/collective/fleet/test_parallel_dygraph_sparse_embedding_over_height.py +++ b/test/collective/fleet/test_parallel_dygraph_sparse_embedding_over_height.py @@ -21,7 +21,7 @@ from legacy_test.spawn_runner_base import TestDistSpawnRunner from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -33,7 +33,7 @@ def _setup_config(self): self._dygraph = True def test_sparse_embedding(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_sparse_embedding_over_height.py" @@ -46,7 +46,7 @@ def test_sparse_embedding(self): class TestParallelDygraphSparseEmdeddingOverHeightSpawn(TestDistSpawnRunner): def test_sparse_embedding_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestSparseEmbeddingOverHeight, delta=1e-5 ) diff --git a/test/collective/fleet/test_parallel_dygraph_sync_batch_norm.py b/test/collective/fleet/test_parallel_dygraph_sync_batch_norm.py index 9aac85f7e494e..45e0e2e90e852 100644 --- a/test/collective/fleet/test_parallel_dygraph_sync_batch_norm.py +++ b/test/collective/fleet/test_parallel_dygraph_sync_batch_norm.py @@ -17,7 +17,7 @@ from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -29,7 +29,7 @@ def _setup_config(self): self._dygraph = True def test_mnist(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_sync_batch_norm.py", delta=1e-5, diff --git a/test/collective/fleet/test_parallel_dygraph_transformer.py b/test/collective/fleet/test_parallel_dygraph_transformer.py index f856fe58aa4e6..5e03eec84ce39 100644 --- a/test/collective/fleet/test_parallel_dygraph_transformer.py +++ b/test/collective/fleet/test_parallel_dygraph_transformer.py @@ -17,7 +17,7 @@ from legacy_test.test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -29,7 +29,7 @@ def _setup_config(self): self._dygraph = True def test_transformer(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_transformer.py", delta=1e-5, @@ -47,7 +47,7 @@ def _setup_config(self): self._find_unused_parameters = False def test_transformer(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_transformer.py", delta=1e-5, diff --git a/test/collective/fleet/test_parallel_dygraph_unused_variables.py b/test/collective/fleet/test_parallel_dygraph_unused_variables.py index dfe1b997d9345..9a2e900b10a4d 100644 --- a/test/collective/fleet/test_parallel_dygraph_unused_variables.py +++ b/test/collective/fleet/test_parallel_dygraph_unused_variables.py @@ -22,7 +22,7 @@ from spawn_runner_base import TestDistSpawnRunner from test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -34,7 +34,7 @@ def _setup_config(self): self._dygraph = True def test_net(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_unused_variables.py" @@ -55,7 +55,7 @@ def _setup_config(self): class TestSparseEmbeddingUnusedVarsSpawn(TestDistSpawnRunner): def test_mnist_with_spawn(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_dist_result_with_spawn( test_class=TestSparseEmbeddingUnusedVars, delta=1e-5 ) @@ -68,7 +68,7 @@ def _setup_config(self): self._dygraph = True def test_net(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_none_var.py" @@ -86,7 +86,7 @@ def _setup_config(self): self._dygraph = True def test_mnist(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( os.path.abspath( "../../legacy_test/parallel_dygraph_shared_unused_var.py" diff --git a/test/collective/fleet/test_pipeline.py b/test/collective/fleet/test_pipeline.py index e86280c50073a..b8ce3d2c5e794 100644 --- a/test/collective/fleet/test_pipeline.py +++ b/test/collective/fleet/test_pipeline.py @@ -18,7 +18,7 @@ from legacy_test.test_dist_base import TestDistBase import paddle -from paddle import fluid +from paddle import base paddle.enable_static() flag_name = os.path.splitext(__file__)[0] @@ -36,7 +36,7 @@ def need_envs(self): return {} def test_dist_train(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): # TODO (sandyhouse) fix the delta value. # Now pipeline only gets the loss value of the last # microbatch, so it is not consistable with the @@ -50,7 +50,7 @@ def test_dist_train(self): ) def test_dist_train_multi_device(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "pipeline_mnist_multi_device.py", check_error_log=True, @@ -60,7 +60,7 @@ def test_dist_train_multi_device(self): ) def test_dist_train_one_device(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "pipeline_mnist_one_device.py", check_error_log=True, diff --git a/test/collective/fleet/test_recv_save_op.py b/test/collective/fleet/test_recv_save_op.py index 442b15198b1ec..b032cae1f5dcf 100644 --- a/test/collective/fleet/test_recv_save_op.py +++ b/test/collective/fleet/test_recv_save_op.py @@ -23,9 +23,9 @@ from dist_test_utils import remove_ps_flag from op import Operator -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.framework import Program, program_guard from paddle.incubate.distributed.fleet.parameter_server.mode import ( DistributedMode, ) @@ -33,12 +33,12 @@ def run_pserver(pserver_id): remove_ps_flag(os.getpid()) - scope = fluid.core.Scope() + scope = base.core.Scope() program = Program() - with fluid.scope_guard(scope): + with base.scope_guard(scope): with program_guard(program, startup_program=Program()): # create table parameter in scope - place = fluid.CPUPlace() + place = base.CPUPlace() # create and initialize Param Variable param = scope.var('table').get_tensor() @@ -61,7 +61,7 @@ def run_pserver(pserver_id): }, ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(program) @@ -96,9 +96,9 @@ def _get_pserver_port(self, pid): return port def _run_nce_op_two_pserver(self, place, port0, port1, model_file): - scope = fluid.core.Scope() + scope = base.core.Scope() program = Program() - with fluid.scope_guard(scope): + with base.scope_guard(scope): with program_guard(program, startup_program=Program()): emaps = ['127.0.0.1:' + str(port0), '127.0.0.1:' + str(port1)] @@ -118,12 +118,12 @@ def _run_nce_op_two_pserver(self, place, port0, port1, model_file): remote_recv_op.run(scope, place) def _load_slice_var(self, model_file): - load_prog = fluid.Program() + load_prog = base.Program() load_block = load_prog.global_block() origin = load_block.create_var( name="var.origin", - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[10, 8], dtype="float32", persistable=True, @@ -131,7 +131,7 @@ def _load_slice_var(self, model_file): slice0 = load_block.create_var( name="var.slice0", - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[3, 8], dtype="float32", persistable=True, @@ -139,7 +139,7 @@ def _load_slice_var(self, model_file): slice1 = load_block.create_var( name="var.slice1", - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[5, 8], dtype="float32", persistable=True, @@ -174,12 +174,12 @@ def _load_slice_var(self, model_file): }, ) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) exe.run(load_prog) - origin_var = fluid.global_scope().find_var("var.origin") - slice0_var = fluid.global_scope().find_var("var.slice0") - slice1_var = fluid.global_scope().find_var("var.slice1") + origin_var = base.global_scope().find_var("var.origin") + slice0_var = base.global_scope().find_var("var.slice0") + slice1_var = base.global_scope().find_var("var.slice1") origin = np.array(origin_var.get_tensor()) slice0 = np.array(slice0_var.get_tensor()) diff --git a/test/collective/fleet/test_static_model_parallel.py b/test/collective/fleet/test_static_model_parallel.py index 299fdd8370679..cc39e732e2f81 100644 --- a/test/collective/fleet/test_static_model_parallel.py +++ b/test/collective/fleet/test_static_model_parallel.py @@ -32,9 +32,9 @@ def _setup_config(self): self._pipeline_mode = True def test_dist_static_model_parallel(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "static_model_parallel_by_row.py", delta=1e-5, @@ -43,9 +43,9 @@ def test_dist_static_model_parallel(self): ) def test_dist_static_model_parallel2(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "static_model_parallel_by_col.py", delta=1e-5, @@ -54,9 +54,9 @@ def test_dist_static_model_parallel2(self): ) def test_dist_static_model_parallel3(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "static_model_parallel_embedding.py", delta=1e-5, diff --git a/test/collective/fleet/test_tcp_store.py b/test/collective/fleet/test_tcp_store.py index 29dab1f6be04d..7e054f4ebf5c7 100644 --- a/test/collective/fleet/test_tcp_store.py +++ b/test/collective/fleet/test_tcp_store.py @@ -22,7 +22,7 @@ class TestTCPStore(unittest.TestCase): def test_tcp_store(self): dist_port = int(os.getenv("PADDLE_DIST_UT_PORT", 6170)) print("get dist_port:", dist_port) - store = paddle.fluid.core.TCPStore("127.0.0.1", dist_port, True, 1, 1) + store = paddle.base.core.TCPStore("127.0.0.1", dist_port, True, 1, 1) store.add("my", 3) ret1 = store.get('my') store.add("my", 3) diff --git a/test/collective/parallel_embedding_api.py b/test/collective/parallel_embedding_api.py index 544078a855b8c..826c3dbe95fd6 100644 --- a/test/collective/parallel_embedding_api.py +++ b/test/collective/parallel_embedding_api.py @@ -19,7 +19,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): fleet.init(is_collective=True) np.random.seed(2020) # (num_embeddings, embedding_dim) = (12, 8) @@ -44,13 +44,13 @@ def get_model(self, main_prog, startup_program, rank): ) per_part_size = size[0] // 2 if rank == 0: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign( np_array[0:per_part_size, :] ), ) else: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign( np_array[per_part_size : size[0], :] ), diff --git a/test/collective/process_group_gloo.py b/test/collective/process_group_gloo.py index b6ae187cc5ffa..ae1cebbc359e2 100644 --- a/test/collective/process_group_gloo.py +++ b/test/collective/process_group_gloo.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class TestProcessGroupFp32(unittest.TestCase): @@ -37,10 +37,10 @@ def test_create_process_group_gloo(self): nranks = paddle.distributed.ParallelEnv().nranks rank = paddle.distributed.ParallelEnv().local_rank is_master = True if rank == 0 else False - store = paddle.fluid.core.TCPStore( + store = paddle.base.core.TCPStore( "127.0.0.1", 6272, is_master, nranks, 30 ) - pg = paddle.fluid.core.ProcessGroupGloo.create(store, rank, nranks) + pg = paddle.base.core.ProcessGroupGloo.create(store, rank, nranks) # test allreduce sum # rank 0 diff --git a/test/collective/process_group_mpi.py b/test/collective/process_group_mpi.py index b5d9fd4b74377..3f363e90df24b 100644 --- a/test/collective/process_group_mpi.py +++ b/test/collective/process_group_mpi.py @@ -20,6 +20,8 @@ import paddle import paddle.distributed as dist +from paddle.base import core +from paddle.base.framework import _set_expected_place from paddle.distributed.collective import ( Group, _default_group_name, @@ -27,8 +29,6 @@ _set_group_map_backend, _set_group_map_by_name, ) -from paddle.fluid import core -from paddle.fluid.framework import _set_expected_place ctypes.CDLL("libmpi.so", mode=ctypes.RTLD_GLOBAL) diff --git a/test/collective/row_parallel_linear_api.py b/test/collective/row_parallel_linear_api.py index e220ad468f699..715c85181727e 100644 --- a/test/collective/row_parallel_linear_api.py +++ b/test/collective/row_parallel_linear_api.py @@ -19,7 +19,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): self.global_ring_id = 0 def get_model(self, main_prog, startup_program, rank): - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): fleet.init(is_collective=True) np.random.seed(2020) np_array = np.random.rand(1000, 16) @@ -41,13 +41,13 @@ def get_model(self, main_prog, startup_program, rank): paddle.distributed.broadcast(data, src=0) data = paddle.split(data, 2, axis=1)[rank] if rank == 0: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign( np_array[0:500, :] ), ) else: - param_attr = paddle.fluid.ParamAttr( + param_attr = paddle.base.ParamAttr( initializer=paddle.nn.initializer.Assign( np_array[500:1000, :] ), diff --git a/test/collective/test_collective_allreduce_api.py b/test/collective/test_collective_allreduce_api.py index 64d3885e8a6a9..1566449f08edc 100644 --- a/test/collective/test_collective_allreduce_api.py +++ b/test/collective/test_collective_allreduce_api.py @@ -27,7 +27,7 @@ def _setup_config(self): pass def test_allreduce_nccl(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.check_with_place( "collective_allreduce_api.py", "allreduce", "nccl" ) @@ -60,7 +60,7 @@ def test_allreduce_nccl_with_comm_context(self): ) def test_allreduce_bkcl(self): - if paddle.fluid.core.is_compiled_with_xpu(): + if paddle.base.core.is_compiled_with_xpu(): self.check_with_place( "collective_allreduce_api.py", "allreduce", "bkcl" ) diff --git a/test/collective/test_collective_cpu_barrier_with_gloo.py b/test/collective/test_collective_cpu_barrier_with_gloo.py index cf491f173c08c..70f7d495b6412 100644 --- a/test/collective/test_collective_cpu_barrier_with_gloo.py +++ b/test/collective/test_collective_cpu_barrier_with_gloo.py @@ -19,7 +19,7 @@ from contextlib import closing import paddle -from paddle import fluid +from paddle import base port_set = set() paddle.enable_static() @@ -64,15 +64,15 @@ def barrier_func(self, id, rank_num, server_endpoint, out_dict, sleep_time): def barrier_op(self, id, rank_num, server_endpoint, out_dict, sleep_time): try: - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() paddle.distributed.gloo_init_parallel_env( id, rank_num, server_endpoint ) - place = fluid.CPUPlace() - with fluid.program_guard(main_prog, startup_prog): + place = base.CPUPlace() + with base.program_guard(main_prog, startup_prog): paddle.distributed.barrier() - exe = fluid.Executor(place) + exe = base.Executor(place) # Run barrier to synchronize processes after starting exe.run(main_prog) # Let rank 0 sleep for one second and check that all processes diff --git a/test/collective/test_collective_reduce_api.py b/test/collective/test_collective_reduce_api.py index b5f4be7519369..d56488f124128 100644 --- a/test/collective/test_collective_reduce_api.py +++ b/test/collective/test_collective_reduce_api.py @@ -27,7 +27,7 @@ def _setup_config(self): pass def test_reduce_nccl(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.check_with_place("collective_reduce_api.py", "reduce", "nccl") def test_reduce_nccl_with_comm_context(self): @@ -47,7 +47,7 @@ def test_reduce_nccl_with_comm_context(self): if self._nccl_version >= 21000: dtypes_to_test.append("bfloat16") for dtype in dtypes_to_test: - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): for red_type in red_types_to_test: self.check_with_place( "collective_reduce_api.py", @@ -59,7 +59,7 @@ def test_reduce_nccl_with_comm_context(self): ) def test_reduce_bkcl(self): - if paddle.fluid.core.is_compiled_with_xpu(): + if paddle.base.core.is_compiled_with_xpu(): self.check_with_place("collective_reduce_api.py", "reduce", "bkcl") def test_reduce_gloo(self): diff --git a/test/collective/test_collective_sendrecv_api.py b/test/collective/test_collective_sendrecv_api.py index 378476f36420a..7fe9e571c5dee 100644 --- a/test/collective/test_collective_sendrecv_api.py +++ b/test/collective/test_collective_sendrecv_api.py @@ -26,7 +26,7 @@ def _setup_config(self): pass # def test_sendrecv_nccl(self): - # if paddle.fluid.core.is_compiled_with_cuda(): + # if paddle.base.core.is_compiled_with_cuda(): # self.check_with_place("collective_sendrecv_api.py", "sendrecv", # "nccl") @@ -44,7 +44,7 @@ def test_sendrecv_nccl_with_comm_context(self): if self._nccl_version >= 21000: dtypes_to_test.append("bfloat16") for dtype in dtypes_to_test: - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.check_with_place( "collective_sendrecv_api.py", "sendrecv", diff --git a/test/collective/test_gen_nccl_id_op.py b/test/collective/test_gen_nccl_id_op.py index 846e1e672ca27..8af379307237d 100644 --- a/test/collective/test_gen_nccl_id_op.py +++ b/test/collective/test_gen_nccl_id_op.py @@ -21,7 +21,7 @@ os.environ['GLOG_vmodule'] = "gen_nccl_id_op*=10,gen_comm_id*=10" import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/contrib/test_bf16_utils.py b/test/contrib/test_bf16_utils.py index 75ce0045b39ab..049f8500c8cc6 100644 --- a/test/contrib/test_bf16_utils.py +++ b/test/contrib/test_bf16_utils.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import amp paddle.enable_static() @@ -109,13 +109,13 @@ def test_amp_lists_(self): ) def test_find_op_index(self): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() op_desc = core.OpDesc() idx = amp.fp16_utils.find_op_index(block.desc, op_desc) assert idx == -1 def test_is_in_fp32_varnames(self): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() var1 = block.create_var(name="X", shape=[3], dtype='float32') var2 = block.create_var(name="Y", shape=[3], dtype='float32') @@ -137,7 +137,7 @@ def test_is_in_fp32_varnames(self): assert amp.bf16.amp_utils._is_in_fp32_varnames(op1, amp_lists_2) def test_find_true_post_op(self): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() var1 = block.create_var(name="X", shape=[3], dtype='float32') var2 = block.create_var(name="Y", shape=[3], dtype='float32') @@ -152,9 +152,9 @@ def test_find_true_post_op(self): assert res == [op2] def test_find_true_post_op_with_search_all(self): - program = fluid.Program() + program = base.Program() block = program.current_block() - startup_block = fluid.default_startup_program().global_block() + startup_block = base.default_startup_program().global_block() var1 = block.create_var(name="X", shape=[3], dtype='float32') var2 = block.create_var(name="Y", shape=[3], dtype='float32') diff --git a/test/contrib/test_correlation.py b/test/contrib/test_correlation.py index 39f9c20da42ea..59259a278c5a2 100644 --- a/test/contrib/test_correlation.py +++ b/test/contrib/test_correlation.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable paddle.enable_static() @@ -81,7 +81,7 @@ def corr( class TestCorrelationOp(unittest.TestCase): def test_check_output(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return np.random.seed(13) np.set_printoptions(threshold=np.inf) @@ -128,8 +128,8 @@ def test_check_output(self): optimizer = paddle.optimizer.Momentum(0.0001, 0.9) optimizer.minimize(loss) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) res = exe.run( feed={'x1': x1_np, 'x2': x2_np}, fetch_list=[out.name, loss.name] ) @@ -156,14 +156,14 @@ def forward(self, x1, x2): class TestCorrelationOpDyGraph(unittest.TestCase): def test_check_output(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return np.random.seed(13) np.set_printoptions(threshold=np.inf) x_shape = (2, 10, 3, 3) x_type = 'float32' - place = fluid.CUDAPlace(0) - with fluid.dygraph.guard(place): + place = base.CUDAPlace(0) + with base.dygraph.guard(place): x1_np = np.random.randn(2, 3, 4, 5).astype(x_type) x2_np = np.random.randn(2, 3, 4, 5).astype(x_type) out_np = corr( diff --git a/test/contrib/test_fp16_utils.py b/test/contrib/test_fp16_utils.py index 1469c73375827..028cf1563cb06 100644 --- a/test/contrib/test_fp16_utils.py +++ b/test/contrib/test_fp16_utils.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static.amp import fp16_utils paddle.enable_static() @@ -24,13 +24,13 @@ class AMPTest(unittest.TestCase): def test_find_op_index(self): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() op_desc = core.OpDesc() idx = fp16_utils.find_op_index(block.desc, op_desc) assert idx == -1 def test_find_true_post_op(self): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() var1 = block.create_var(name="X", shape=[3], dtype='float32') var2 = block.create_var(name="Y", shape=[3], dtype='float32') diff --git a/test/contrib/test_image_classification_fp16.py b/test/contrib/test_image_classification_fp16.py index 9aac48b5fe2dd..f04df38b03234 100644 --- a/test/contrib/test_image_classification_fp16.py +++ b/test/contrib/test_image_classification_fp16.py @@ -27,7 +27,7 @@ import nets import paddle -from paddle import fluid +from paddle import base from paddle.static.amp import decorate paddle.enable_static() @@ -110,11 +110,11 @@ def train(net_type, use_cuda, save_dirname, is_local): classdim = 10 data_shape = [3, 32, 32] - train_program = fluid.Program() - startup_prog = fluid.Program() + train_program = base.Program() + startup_prog = base.Program() train_program.random_seed = 123 startup_prog.random_seed = 456 - with fluid.program_guard(train_program, startup_prog): + with base.program_guard(train_program, startup_prog): images = paddle.static.data( name='pixel', shape=[-1] + data_shape, dtype='float32' ) @@ -167,9 +167,9 @@ def train(net_type, use_cuda, save_dirname, is_local): paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(place=place, feed_list=[images, label]) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + feeder = base.DataFeeder(place=place, feed_list=[images, label]) def train_loop(main_program): exe.run(startup_prog) @@ -257,11 +257,11 @@ def infer(use_cuda, save_dirname=None): if save_dirname is None: return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - inference_scope = fluid.core.Scope() - with fluid.scope_guard(inference_scope): + inference_scope = base.core.Scope() + with base.scope_guard(inference_scope): # Use paddle.static.io.load_inference_model to obtain the inference program desc, # the feed_target_names (the names of variables that will be fed # data using feed operators), and the fetch_targets (variables that @@ -305,7 +305,7 @@ def tearDown(self): self.temp_dir.cleanup() def main(self, net_type, use_cuda, is_local=True): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return # Directory for saving the trained model @@ -477,11 +477,11 @@ def test_resnet_cuda(self): @contextlib.contextmanager def scope_prog_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield @@ -490,7 +490,7 @@ def decorate_with_data_loader(self): main_prog = paddle.static.Program() start_prog = paddle.static.Program() with paddle.static.program_guard(main_prog, start_prog): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): image = paddle.static.data( name='image', shape=[-1, 3, 224, 224], dtype='float32' ) diff --git a/test/contrib/test_multi_precision_fp16_train.py b/test/contrib/test_multi_precision_fp16_train.py index 0b5f58e550462..137f2269173f3 100644 --- a/test/contrib/test_multi_precision_fp16_train.py +++ b/test/contrib/test_multi_precision_fp16_train.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.io import Dataset from paddle.static.amp.fp16_utils import cast_model_to_fp16 @@ -99,11 +99,11 @@ def train(use_pure_fp16=True, use_nesterov=False, optimizer=""): data_shape = [3, 32, 32] PASS_NUM = 1 - train_program = fluid.Program() - startup_prog = fluid.Program() + train_program = base.Program() + startup_prog = base.Program() train_program.random_seed = 123 startup_prog.random_seed = 456 - with fluid.program_guard(train_program, startup_prog): + with base.program_guard(train_program, startup_prog): images = paddle.static.data( name='pixel', shape=[-1] + data_shape, dtype='float32' ) @@ -160,9 +160,9 @@ def train(use_pure_fp16=True, use_nesterov=False, optimizer=""): drop_last=True, ) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(place=place, feed_list=[images, label]) + place = base.CUDAPlace(0) + exe = base.Executor(place) + feeder = base.DataFeeder(place=place, feed_list=[images, label]) def train_loop(): exe.run(startup_prog) @@ -206,7 +206,7 @@ def train_loop(): class TestImageMultiPrecision(unittest.TestCase): def test_resnet_pure_fp16(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return def do_test(use_nesterov=False, optimizer=""): @@ -263,11 +263,11 @@ def do_test(use_nesterov=False, optimizer=""): @contextlib.contextmanager def scope_prog_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): yield @@ -276,7 +276,7 @@ def decorate_with_data_loader(self): main_prog = paddle.static.Program() start_prog = paddle.static.Program() with paddle.static.program_guard(main_prog, start_prog): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): image = paddle.static.data( name='image', shape=[-1, 3, 224, 224], dtype='float32' ) @@ -301,14 +301,14 @@ def decorate_with_data_loader(self): block = main_prog.global_block() for op in block.ops: if op.type == "mul": - op._set_attr('in_dtype', fluid.core.VarDesc.VarType.FP32) - op._set_attr('out_dtype', fluid.core.VarDesc.VarType.FP32) - op._set_attr('dtype', fluid.core.VarDesc.VarType.FP32) + op._set_attr('in_dtype', base.core.VarDesc.VarType.FP32) + op._set_attr('out_dtype', base.core.VarDesc.VarType.FP32) + op._set_attr('dtype', base.core.VarDesc.VarType.FP32) cast_model_to_fp16(main_prog, use_fp16_guard=False) def test_non_iterable_dataloader(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.decorate_with_data_loader() diff --git a/test/custom_kernel/custom_kernel_dot_c_setup.py b/test/custom_kernel/custom_kernel_dot_c_setup.py index c8cb7f4de4002..da6bd72e3d766 100644 --- a/test/custom_kernel/custom_kernel_dot_c_setup.py +++ b/test/custom_kernel/custom_kernel_dot_c_setup.py @@ -55,7 +55,7 @@ def build_extensions(self): # libs path paddle_custom_kernel_library_dir = [ - os.path.join(site_packages_path, 'paddle', 'fluid'), + os.path.join(site_packages_path, 'paddle', 'base'), ] # libs diff --git a/test/custom_kernel/custom_kernel_dot_setup.py b/test/custom_kernel/custom_kernel_dot_setup.py index 7bf6f2fbe6f2a..cce752284ef05 100644 --- a/test/custom_kernel/custom_kernel_dot_setup.py +++ b/test/custom_kernel/custom_kernel_dot_setup.py @@ -56,7 +56,7 @@ def build_extensions(self): # libs path paddle_custom_kernel_library_dir = [ - os.path.join(path, 'paddle', 'fluid') for path in site_packages_path + os.path.join(path, 'paddle', 'base') for path in site_packages_path ] # libs diff --git a/test/custom_op/custom_raw_op_kernel_op_setup.py b/test/custom_op/custom_raw_op_kernel_op_setup.py index bd035a0e3efe6..41af03872ef31 100644 --- a/test/custom_op/custom_raw_op_kernel_op_setup.py +++ b/test/custom_op/custom_raw_op_kernel_op_setup.py @@ -17,7 +17,7 @@ from utils import extra_compile_args, paddle_includes import paddle -from paddle.fluid import core +from paddle.base import core from paddle.utils.cpp_extension import CppExtension, CUDAExtension, setup if paddle.is_compiled_with_cuda(): diff --git a/test/custom_op/test_custom_concat.py b/test/custom_op/test_custom_concat.py index a1d93de4f2d3f..4fa1bcb618db8 100644 --- a/test/custom_op/test_custom_concat.py +++ b/test/custom_op/test_custom_concat.py @@ -31,9 +31,9 @@ run_cmd(cmd, True) if os.name == 'nt': - test_include = "..\\python\\paddle\\fluid\\tests\\custom_op" + test_include = "..\\python\\paddle\\base\\tests\\custom_op" else: - test_include = "../python/paddle/fluid/tests/custom_op" + test_include = "../python/paddle/base/tests/custom_op" paddle_includes.append(test_include) custom_ops = load( diff --git a/test/custom_op/test_custom_conj.py b/test/custom_op/test_custom_conj.py index f51038ae1b34c..6b4677175cd83 100644 --- a/test/custom_op/test_custom_conj.py +++ b/test/custom_op/test_custom_conj.py @@ -42,8 +42,8 @@ def is_complex(dtype): return ( - dtype == paddle.fluid.core.VarDesc.VarType.COMPLEX64 - or dtype == paddle.fluid.core.VarDesc.VarType.COMPLEX128 + dtype == paddle.base.core.VarDesc.VarType.COMPLEX64 + or dtype == paddle.base.core.VarDesc.VarType.COMPLEX128 ) diff --git a/test/custom_runtime/process_group_xccl.py b/test/custom_runtime/process_group_xccl.py index 0e4181ba04ad3..b753d0c3ff485 100644 --- a/test/custom_runtime/process_group_xccl.py +++ b/test/custom_runtime/process_group_xccl.py @@ -18,14 +18,14 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def init_process_group(strategy=None): nranks = paddle.distributed.ParallelEnv().nranks rank = paddle.distributed.ParallelEnv().local_rank is_master = True if rank == 0 else False - store = paddle.fluid.core.TCPStore("127.0.0.1", 6173, is_master, nranks) + store = paddle.base.core.TCPStore("127.0.0.1", 6173, is_master, nranks) pg_group = core.ProcessGroupCustom.create( store, paddle.distributed.ParallelEnv().device_type, @@ -100,13 +100,13 @@ def test_create_process_group_xccl(self): if pg.rank() == 0: task = pg.broadcast(tensor_x, 0, sync_op=True) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) assert task.is_completed() # assert np.array_equal(broadcast_result, tensor_x) else: task = pg.broadcast(tensor_y, 0, sync_op=True) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) assert task.is_completed() # assert np.array_equal(broadcast_result, tensor_y) @@ -138,12 +138,12 @@ def test_create_process_group_xccl(self): if pg.rank() == 0: task = pg.all_gather(tensor_out, tensor_x, sync_op=True) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) # rank 1 else: task = pg.all_gather(tensor_out, tensor_y, sync_op=True) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) out_1 = paddle.slice(tensor_out, [0], [0], [out_shape[0] // 2]) out_2 = paddle.slice( tensor_out, [0], [out_shape[0] // 2], [out_shape[0]] @@ -169,12 +169,12 @@ def test_create_process_group_xccl(self): if pg.rank() == 0: task = pg.alltoall(tensor_x, tensor_out1) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) # rank 1 else: task = pg.alltoall(tensor_y, tensor_out2) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) out1_2 = paddle.slice( tensor_out1, [0], [self.shape[0] // 2], [self.shape[0]] ) @@ -195,12 +195,12 @@ def test_create_process_group_xccl(self): if pg.rank() == 0: task = pg.reduce(tensor_x, 0) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) # rank 1 else: task = pg.reduce(tensor_y, 0) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) # if pg.rank() == 0: # assert np.array_equal(tensor_x, sum_result) print("test reduce sum api ok\n") @@ -216,12 +216,12 @@ def test_create_process_group_xccl(self): if pg.rank() == 0: task = pg.scatter(tensor_x, tensor_y, 0) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) # rank 1 else: task = pg.scatter(tensor_x, tensor_y, 0) task.wait() - # paddle.fluid.core._custom_device_synchronize("custom_cpu", -1) + # paddle.base.core._custom_device_synchronize("custom_cpu", -1) out1 = paddle.slice(tensor_x, [0], [0], [self.shape[0]]) out2 = paddle.slice(tensor_x, [0], [self.shape[0]], [self.shape[0] * 2]) # if pg.rank() == 0: diff --git a/test/custom_runtime/test_custom_op_setup.py b/test/custom_runtime/test_custom_op_setup.py index 6ed6fd79e4ab3..f0f5d1cb505a6 100644 --- a/test/custom_runtime/test_custom_op_setup.py +++ b/test/custom_runtime/test_custom_op_setup.py @@ -133,7 +133,7 @@ def setUp(self): # [Why specific paddle_includes directory?] # Add paddle_includes to pass CI, for more details, - # please refer to the comments in `paddle/fluid/tests/custom_op/utils.py`` + # please refer to the comments in `paddle/tests/custom_op/utils.py`` paddle_includes = [] for site_packages_path in getsitepackages(): paddle_includes.append( diff --git a/test/distributed_passes/auto_parallel_pass_test_base.py b/test/distributed_passes/auto_parallel_pass_test_base.py index 11dbd3f361d34..69c2d051c7db3 100644 --- a/test/distributed_passes/auto_parallel_pass_test_base.py +++ b/test/distributed_passes/auto_parallel_pass_test_base.py @@ -84,7 +84,7 @@ def _run_gpu_main(self, model, apply_pass, dump_file, **kwargs): paddle.static.Program(), paddle.static.Program() ): with paddle.static.scope_guard(scope): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): ( main_prog, startup_prog, @@ -109,7 +109,7 @@ def _run_gpu_main(self, model, apply_pass, dump_file, **kwargs): print(f'batch {batch_id}, outputs {output_dict}') all_fetch_values.append(fetch_values) batch_id += 1 - except paddle.fluid.core.EOFException: + except paddle.base.core.EOFException: data_loader.reset() break with open(dump_file, "wb") as f: @@ -178,7 +178,7 @@ def gen_data(): ) data_holder = [tokens, position_ids, attention_mask, labels, loss_mask] - data_loader = paddle.fluid.io.DataLoader.from_generator( + data_loader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=70, iterable=False ) data_loader.set_batch_generator(gen_data, paddle.static.cuda_places()) diff --git a/test/distributed_passes/dist_pass_test_base.py b/test/distributed_passes/dist_pass_test_base.py index 70b5356071d58..dc8da03bd6a4f 100644 --- a/test/distributed_passes/dist_pass_test_base.py +++ b/test/distributed_passes/dist_pass_test_base.py @@ -137,7 +137,7 @@ def _run_gpu_main(self, model, apply_pass, dump_file, **kwargs): paddle.static.Program(), paddle.static.Program() ): with paddle.static.scope_guard(scope): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): main_prog, startup_prog, inputs, outputs, reader = model( place, **kwargs ) diff --git a/test/distributed_passes/test_auto_parallel_gradient_merge_pass.py b/test/distributed_passes/test_auto_parallel_gradient_merge_pass.py index 9e90288912b2d..f1aef2b30ca7c 100644 --- a/test/distributed_passes/test_auto_parallel_gradient_merge_pass.py +++ b/test/distributed_passes/test_auto_parallel_gradient_merge_pass.py @@ -174,7 +174,7 @@ def gen_data(): ) input.stop_gradient = False data_holder = [input, label] - data_loader = paddle.fluid.io.DataLoader.from_generator( + data_loader = paddle.base.io.DataLoader.from_generator( feed_list=data_holder, capacity=70, iterable=False ) data_loader.set_batch_generator( diff --git a/test/distributed_passes/test_dist_inplace_addto_pass.py b/test/distributed_passes/test_dist_inplace_addto_pass.py index 3a07d24e8aed8..e383c8d98a270 100644 --- a/test/distributed_passes/test_dist_inplace_addto_pass.py +++ b/test/distributed_passes/test_dist_inplace_addto_pass.py @@ -47,7 +47,7 @@ class TestInplaceAddtoPass(DistPassTestBase): def init(self): self.atol = 0.0 self.rtol = 0.0 - paddle.fluid.set_flags({"FLAGS_max_inplace_grad_add": 8}) + paddle.base.set_flags({"FLAGS_max_inplace_grad_add": 8}) def get_model(self, place, batch_size=32, image_shape=[224, 224, 3]): image = paddle.static.data( diff --git a/test/distribution/test_dirichlet_op.py b/test/distribution/test_dirichlet_op.py index 0e51e4463f8d5..b2c1c15bbf91f 100644 --- a/test/distribution/test_dirichlet_op.py +++ b/test/distribution/test_dirichlet_op.py @@ -24,7 +24,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/distribution/test_distribution_bernoulli.py b/test/distribution/test_distribution_bernoulli.py index 1a00503eed19f..4845efcd6a29d 100644 --- a/test/distribution/test_distribution_bernoulli.py +++ b/test/distribution/test_distribution_bernoulli.py @@ -27,9 +27,9 @@ from test_distribution import DistributionNumpy import paddle +from paddle.base.data_feeder import convert_dtype from paddle.distribution import Bernoulli from paddle.distribution.kl import kl_divergence -from paddle.fluid.data_feeder import convert_dtype np.random.seed(2023) paddle.seed(2023) @@ -132,7 +132,7 @@ def _probs_to_logits(self, probs, is_binary=False): class BernoulliTest(unittest.TestCase): def setUp(self): paddle.disable_static(self.place) - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): # just for convenience self.dtype = self.expected_dtype @@ -229,7 +229,7 @@ def init_dynamic_data(self, probs, default_dtype, dtype): ) class BernoulliTestFeature(BernoulliTest): def test_mean(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self.rv_paddle.mean, self.rv_np.mean, @@ -238,7 +238,7 @@ def test_mean(self): ) def test_variance(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self.rv_paddle.variance, self.rv_np.variance, @@ -265,7 +265,7 @@ def test_variance(self): ] ) def test_log_prob(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.probs.dtype ): @@ -297,7 +297,7 @@ def test_log_prob(self, value): ] ) def test_prob(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.probs.dtype ): @@ -331,7 +331,7 @@ def test_prob(self, value): ] ) def test_cdf(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.probs.dtype ): @@ -349,7 +349,7 @@ def test_cdf(self, value): self.rv_paddle.cdf(value) def test_entropy(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self.rv_paddle.entropy(), self.rv_np.entropy(), @@ -358,7 +358,7 @@ def test_entropy(self): ) def test_kl_divergence(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): other_probs = paddle.to_tensor([0.9], dtype=self.dtype) rv_paddle_other = Bernoulli(other_probs) @@ -489,7 +489,7 @@ def test_kl_divergence(self): ) class BernoulliTestSample(BernoulliTest): def test_sample(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): sample_np = self.rv_np.sample(self.shape) sample_paddle = self.rv_paddle.sample(self.shape) @@ -520,7 +520,7 @@ def test_sample(self): ) def test_rsample(self, temperature): """Compare two samples from `rsample` method, one from scipy `sample` and another from paddle `rsample`.""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): sample_np = self.rv_np.sample(self.shape) rsample_paddle = self.rv_paddle.rsample(self.shape, temperature) @@ -548,7 +548,7 @@ def test_rsample(self, temperature): ) def test_rsample_backpropagation(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): self.rv_paddle.probs.stop_gradient = False rsample_paddle = self.rv_paddle.rsample(self.shape) rsample_paddle = paddle.nn.functional.sigmoid(rsample_paddle) @@ -573,7 +573,7 @@ def setUp(self): ] ) def test_bad_init(self, probs, error): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): self.assertRaises(error, Bernoulli, probs) @parameterize_func( @@ -585,7 +585,7 @@ def test_bad_init(self, probs, error): ] ) def test_bad_broadcast(self, probs, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Bernoulli(probs) self.assertRaises(ValueError, rv.cdf, value) self.assertRaises(ValueError, rv.log_prob, value) diff --git a/test/distribution/test_distribution_beta.py b/test/distribution/test_distribution_beta.py index ecedfa2d86a70..92c581a92f5c9 100644 --- a/test/distribution/test_distribution_beta.py +++ b/test/distribution/test_distribution_beta.py @@ -45,7 +45,7 @@ def setUp(self): self._paddle_beta = paddle.distribution.Beta(alpha, beta) def test_mean(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_beta.mean, scipy.stats.beta.mean(self.alpha, self.beta), @@ -54,7 +54,7 @@ def test_mean(self): ) def test_variance(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_beta.variance, scipy.stats.beta.var(self.alpha, self.beta), @@ -66,7 +66,7 @@ def test_prob(self): value = [np.random.rand(*self._paddle_beta.alpha.shape)] for v in value: - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_beta.prob(paddle.to_tensor(v)), scipy.stats.beta.pdf(v, self.alpha, self.beta), @@ -78,7 +78,7 @@ def test_log_prob(self): value = [np.random.rand(*self._paddle_beta.alpha.shape)] for v in value: - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_beta.log_prob(paddle.to_tensor(v)), scipy.stats.beta.logpdf(v, self.alpha, self.beta), @@ -87,7 +87,7 @@ def test_log_prob(self): ) def test_entropy(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_beta.entropy(), scipy.stats.beta.entropy(self.alpha, self.beta), diff --git a/test/distribution/test_distribution_categorical.py b/test/distribution/test_distribution_categorical.py index 446321872684d..d87c72e73438c 100644 --- a/test/distribution/test_distribution_categorical.py +++ b/test/distribution/test_distribution_categorical.py @@ -18,7 +18,7 @@ from test_distribution import DistributionNumpy import paddle -from paddle import fluid +from paddle import base from paddle.distribution import Categorical, Distribution, Normal, Uniform np.random.seed(2022) @@ -56,10 +56,10 @@ class CategoricalTest(unittest.TestCase): def setUp(self, use_gpu=False, batch_size=3, dims=5): self.use_gpu = use_gpu if not use_gpu: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.gpu_id = -1 else: - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self.gpu_id = 0 self.batch_size = batch_size @@ -70,8 +70,8 @@ def setUp(self, use_gpu=False, batch_size=3, dims=5): self.init_dynamic_data(batch_size, dims) paddle.enable_static() - self.test_program = fluid.Program() - self.executor = fluid.Executor(self.place) + self.test_program = base.Program() + self.executor = base.Executor(self.place) self.init_static_data(batch_size, dims) def init_numpy_data(self, batch_size, dims): @@ -102,7 +102,7 @@ def init_dynamic_data(self, batch_size, dims): self.value = paddle.to_tensor(self.value_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = paddle.static.data( name='logits', shape=self.logits_shape, dtype='float32' ) @@ -168,7 +168,7 @@ def test_categorical_distribution_dygraph(self, tolerance=1e-6): def test_categorical_distribution_static(self, tolerance=1e-6): paddle.enable_static() - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): categorical = Categorical(self.logits_static) other_categorical = Categorical(self.other_logits_static) @@ -186,7 +186,7 @@ def test_categorical_distribution_static(self, tolerance=1e-6): 'value': self.value_np, } - self.executor.run(fluid.default_startup_program()) + self.executor.run(base.default_startup_program()) fetch_list = self.executor.run( program=self.test_program, feed=feed_vars, fetch_list=fetch_list ) @@ -210,7 +210,7 @@ def init_numpy_data(self, batch_size, dims): self.value_shape = [3] def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = paddle.static.data( name='logits', shape=self.logits_shape, dtype='float64' ) @@ -231,7 +231,7 @@ def init_dynamic_data(self, batch_size, dims): self.value = paddle.to_tensor(self.value_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = self.logits_np self.other_logits_static = self.other_logits_np self.value_static = paddle.static.data( @@ -260,7 +260,7 @@ def init_dynamic_data(self, batch_size, dims): self.value = paddle.to_tensor(self.value_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = self.logits_np self.other_logits_static = self.other_logits_np self.value_static = paddle.static.data( @@ -341,7 +341,7 @@ def init_dynamic_data(self, batch_size, dims): self.value = paddle.to_tensor(self.value_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = self.logits_np.tolist() self.other_logits_static = self.other_logits_np.tolist() self.value_static = paddle.static.data( @@ -358,7 +358,7 @@ def init_dynamic_data(self, batch_size, dims): self.value = paddle.to_tensor(self.value_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.logits_static = tuple(self.logits_np.tolist()) self.other_logits_static = tuple(self.other_logits_np.tolist()) self.value_static = paddle.static.data( diff --git a/test/distribution/test_distribution_cauchy.py b/test/distribution/test_distribution_cauchy.py index d6ea9b882370a..d0d570a37df07 100644 --- a/test/distribution/test_distribution_cauchy.py +++ b/test/distribution/test_distribution_cauchy.py @@ -27,9 +27,9 @@ from test_distribution import DistributionNumpy import paddle +from paddle.base.data_feeder import convert_dtype from paddle.distribution import Cauchy from paddle.distribution.kl import kl_divergence -from paddle.fluid.data_feeder import convert_dtype np.random.seed(2023) paddle.seed(2023) @@ -91,7 +91,7 @@ def kl_divergence(self, other): class CauchyTest(unittest.TestCase): def setUp(self): paddle.disable_static(self.place) - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): # just for convenience self.dtype = self.expected_dtype @@ -275,7 +275,7 @@ class CauchyTestFeature(CauchyTest): ] ) def test_log_prob(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.loc.dtype ): @@ -302,7 +302,7 @@ def test_log_prob(self, value): ] ) def test_prob(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.loc.dtype ): @@ -329,7 +329,7 @@ def test_prob(self, value): ] ) def test_cdf(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): if convert_dtype(value.dtype) == convert_dtype( self.rv_paddle.loc.dtype ): @@ -347,7 +347,7 @@ def test_cdf(self, value): self.rv_paddle.cdf(value) def test_entropy(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self.rv_paddle.entropy(), self.rv_np.entropy(), @@ -362,7 +362,7 @@ def test_entropy(self): ] ) def test_kl_divergence(self, loc, scale): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): # convert loc/scale to paddle's dtype(float32/float64) rv_paddle_other = Cauchy( loc=paddle.full((), loc, dtype=self.rv_paddle.loc.dtype), @@ -557,7 +557,7 @@ def test_kl_divergence(self, loc, scale): ) class CauchyTestSample(CauchyTest): def test_sample(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): sample_np = self.rv_np.sample(self.shape) sample_paddle = self.rv_paddle.sample(self.shape) @@ -581,7 +581,7 @@ def test_sample(self): ) def test_rsample(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): sample_np = self.rv_np.sample(self.shape) rsample_paddle = self.rv_paddle.rsample(self.shape) @@ -605,7 +605,7 @@ def test_rsample(self): ) def test_rsample_backpropagation(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): self.rv_paddle.loc.stop_gradient = False self.rv_paddle.scale.stop_gradient = False rsample_paddle = self.rv_paddle.rsample(self.shape) @@ -638,14 +638,14 @@ def setUp(self): ] ) def test_bad_init(self, loc, scale, error): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): self.assertRaises(error, Cauchy, loc, scale) def test_bad_property(self): """For property like mean/variance/stddev which is undefined in math, we should raise `ValueError` instead of `NotImplementedError`. """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Cauchy(loc=0.0, scale=1.0) with self.assertRaises(ValueError): _ = rv.mean @@ -661,7 +661,7 @@ def test_bad_property(self): ] ) def test_bad_sample_shape_type(self, shape): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Cauchy(loc=0.0, scale=1.0) with self.assertRaises(TypeError): @@ -680,7 +680,7 @@ def test_bad_sample_shape_type(self, shape): ] ) def test_bad_value_type(self, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Cauchy(loc=0.0, scale=1.0) with self.assertRaises(TypeError): @@ -698,7 +698,7 @@ def test_bad_value_type(self, value): ] ) def test_bad_kl_other_type(self, other): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Cauchy(loc=0.0, scale=1.0) with self.assertRaises(TypeError): @@ -714,7 +714,7 @@ def test_bad_kl_other_type(self, other): ] ) def test_bad_broadcast(self, loc, scale, value): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): rv = Cauchy(loc=loc, scale=scale) self.assertRaises(ValueError, rv.cdf, value) self.assertRaises(ValueError, rv.log_prob, value) diff --git a/test/distribution/test_distribution_dirichlet.py b/test/distribution/test_distribution_dirichlet.py index 6db297beb1106..308c703d3b568 100644 --- a/test/distribution/test_distribution_dirichlet.py +++ b/test/distribution/test_distribution_dirichlet.py @@ -39,7 +39,7 @@ def setUp(self): ) def test_mean(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_diric.mean, scipy.stats.dirichlet.mean(self.concentration), @@ -48,7 +48,7 @@ def test_mean(self): ) def test_variance(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_diric.variance, scipy.stats.dirichlet.var(self.concentration), @@ -61,7 +61,7 @@ def test_prob(self): value = [v / v.sum() for v in value] for v in value: - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_diric.prob(paddle.to_tensor(v)), scipy.stats.dirichlet.pdf(v, self.concentration), @@ -74,7 +74,7 @@ def test_log_prob(self): value = [v / v.sum() for v in value] for v in value: - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_diric.log_prob(paddle.to_tensor(v)), scipy.stats.dirichlet.logpdf(v, self.concentration), @@ -83,7 +83,7 @@ def test_log_prob(self): ) def test_entropy(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_diric.entropy(), scipy.stats.dirichlet.entropy(self.concentration), diff --git a/test/distribution/test_distribution_geometric.py b/test/distribution/test_distribution_geometric.py index 10dcf330b3b02..dd2ef33242c06 100644 --- a/test/distribution/test_distribution_geometric.py +++ b/test/distribution/test_distribution_geometric.py @@ -59,7 +59,7 @@ def setUp(self): self._paddle_geom = geometric.Geometric(probs) def test_mean(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.mean, scipy.stats.geom.mean(self.probs), @@ -68,7 +68,7 @@ def test_mean(self): ) def test_variance(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.variance, scipy.stats.geom.var(self.probs), @@ -77,7 +77,7 @@ def test_variance(self): ) def test_stddev(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.stddev, scipy.stats.geom.std(self.probs), @@ -86,7 +86,7 @@ def test_stddev(self): ) def test_entropy(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.entropy(), scipy.stats.geom.entropy(self.probs), @@ -181,7 +181,7 @@ def test_rsample(self): def test_back_rsample(self): sample_shape = (100000,) - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): self._paddle_geom.probs.stop_gradient = False rs_value = self._paddle_geom.rsample(sample_shape) @@ -236,7 +236,7 @@ def setUp(self): ) def test_pmf(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.pmf(self.value), scipy.stats.geom.pmf(self.value, self.probs), @@ -245,7 +245,7 @@ def test_pmf(self): ) def test_log_pmf(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.log_pmf(self.value), scipy.stats.geom.logpmf(self.value, self.probs), @@ -254,7 +254,7 @@ def test_log_pmf(self): ) def test_cdf(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( self._paddle_geom.cdf(self.value), scipy.stats.geom.cdf(self.value, self.probs), diff --git a/test/distribution/test_distribution_lognormal.py b/test/distribution/test_distribution_lognormal.py index e7069f7b2b11f..53e0ba55f4436 100644 --- a/test/distribution/test_distribution_lognormal.py +++ b/test/distribution/test_distribution_lognormal.py @@ -130,7 +130,7 @@ def test_entropy(self): ) def test_probs(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): probs = self.paddle_lognormal.probs(paddle.to_tensor(self.value)) np_probs = self.np_lognormal.probs(self.value) np.testing.assert_allclose( @@ -141,7 +141,7 @@ def test_probs(self): ) def test_log_prob(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): log_prob = self.paddle_lognormal.log_prob( paddle.to_tensor(self.value) ) diff --git a/test/distribution/test_distribution_normal.py b/test/distribution/test_distribution_normal.py index 4edc07e1a3b6a..6f13fc42ae016 100644 --- a/test/distribution/test_distribution_normal.py +++ b/test/distribution/test_distribution_normal.py @@ -22,7 +22,7 @@ from test_distribution import DistributionNumpy import paddle -from paddle import fluid +from paddle import base from paddle.distribution import Normal np.random.seed(2022) @@ -74,10 +74,10 @@ class NormalTest(unittest.TestCase): def setUp(self, use_gpu=False, batch_size=2, dims=3): self.use_gpu = use_gpu if not use_gpu: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.gpu_id = -1 else: - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self.gpu_id = 0 self.init_numpy_data(batch_size, dims) @@ -86,8 +86,8 @@ def setUp(self, use_gpu=False, batch_size=2, dims=3): self.init_dynamic_data(batch_size, dims) paddle.enable_static() - self.test_program = fluid.Program() - self.executor = fluid.Executor(self.place) + self.test_program = base.Program() + self.executor = base.Executor(self.place) self.init_static_data(batch_size, dims) def init_numpy_data(self, batch_size, dims): @@ -115,7 +115,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1], dtype='float32' ) @@ -167,7 +167,7 @@ def test_normal_distribution_dygraph(self, sample_shape=7, tolerance=1e-6): def test_normal_distribution_static(self, sample_shape=7, tolerance=1e-6): paddle.enable_static() - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): normal = Normal(self.static_loc, self.static_scale) sample = normal.sample([sample_shape]) @@ -189,7 +189,7 @@ def test_normal_distribution_static(self, sample_shape=7, tolerance=1e-6): 'other_scale': self.other_scale_np, } - self.executor.run(fluid.default_startup_program()) + self.executor.run(base.default_startup_program()) fetch_list = self.executor.run( program=self.test_program, feed=feed_vars, fetch_list=fetch_list ) @@ -235,7 +235,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -264,7 +264,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -300,7 +300,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float64' ) @@ -332,7 +332,7 @@ def init_dynamic_data(self, batch_size, dims): self.dynamic_other_scale = paddle.to_tensor(self.other_scale_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_loc = paddle.static.data( name='loc', shape=[-1, dims], dtype='float32' ) @@ -380,7 +380,7 @@ def init_dynamic_data(self, batch_size, dims): ) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_loc = paddle.static.data( name='loc', shape=[-1, dims], dtype='float64' ) @@ -428,7 +428,7 @@ def init_dynamic_data(self, batch_size, dims): ) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_loc = paddle.static.data( name='loc', shape=[-1, dims], dtype='float64' ) @@ -475,7 +475,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -510,7 +510,7 @@ def init_static_data(self, batch_size, dims): self.static_scale = self.scale_np self.static_other_loc = self.other_loc_np self.static_other_scale = self.other_scale_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) diff --git a/test/distribution/test_distribution_uniform.py b/test/distribution/test_distribution_uniform.py index ca59b5118e677..8283017ab7349 100644 --- a/test/distribution/test_distribution_uniform.py +++ b/test/distribution/test_distribution_uniform.py @@ -18,7 +18,7 @@ from test_distribution import DistributionNumpy import paddle -from paddle import fluid +from paddle import base from paddle.distribution import Uniform np.random.seed(2022) @@ -56,10 +56,10 @@ class UniformTest(unittest.TestCase): def setUp(self, use_gpu=False, batch_size=5, dims=6): self.use_gpu = use_gpu if not use_gpu: - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self.gpu_id = -1 else: - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self.gpu_id = 0 self.init_numpy_data(batch_size, dims) @@ -68,8 +68,8 @@ def setUp(self, use_gpu=False, batch_size=5, dims=6): self.init_dynamic_data(batch_size, dims) paddle.enable_static() - self.test_program = fluid.Program() - self.executor = fluid.Executor(self.place) + self.test_program = base.Program() + self.executor = base.Executor(self.place) self.init_static_data(batch_size, dims) def init_numpy_data(self, batch_size, dims): @@ -86,7 +86,7 @@ def init_dynamic_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1], dtype='float32' ) @@ -111,7 +111,7 @@ def compare_with_numpy(self, fetch_list, sample_shape=7, tolerance=1e-6): def test_uniform_distribution_static(self, sample_shape=7, tolerance=1e-6): paddle.enable_static() - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): uniform = Uniform(self.static_low, self.static_high) sample = uniform.sample([sample_shape]) entropy = uniform.entropy() @@ -125,7 +125,7 @@ def test_uniform_distribution_static(self, sample_shape=7, tolerance=1e-6): 'values': self.values_np, } - self.executor.run(fluid.default_startup_program()) + self.executor.run(base.default_startup_program()) fetch_list = self.executor.run( program=self.test_program, feed=feed_vars, fetch_list=fetch_list ) @@ -168,7 +168,7 @@ def init_numpy_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -186,7 +186,7 @@ def init_numpy_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -209,7 +209,7 @@ def init_dynamic_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float64' ) @@ -230,7 +230,7 @@ def init_dynamic_data(self, batch_size, dims): self.dynamic_values = paddle.to_tensor(self.values_np) def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_low = paddle.static.data( name='low', shape=[-1, dims], dtype='float32' ) @@ -257,7 +257,7 @@ def init_dynamic_data(self, batch_size, dims): self.dynamic_values = paddle.to_tensor(self.values_np, dtype='float64') def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_low = paddle.static.data( name='low', shape=[-1, dims], dtype='float64' ) @@ -284,7 +284,7 @@ def init_dynamic_data(self, batch_size, dims): self.dynamic_values = paddle.to_tensor(self.values_np, dtype='float32') def init_static_data(self, batch_size, dims): - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_low = paddle.static.data( name='low', shape=[-1, dims], dtype='float64' ) @@ -309,7 +309,7 @@ def init_numpy_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -331,7 +331,7 @@ def init_numpy_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) @@ -353,7 +353,7 @@ def init_numpy_data(self, batch_size, dims): def init_static_data(self, batch_size, dims): self.static_low = self.low_np self.static_high = self.high_np - with fluid.program_guard(self.test_program): + with base.program_guard(self.test_program): self.static_values = paddle.static.data( name='values', shape=[-1, dims], dtype='float32' ) diff --git a/test/distribution/test_kl.py b/test/distribution/test_kl.py index 444f469e0a630..b1f0b16b3437a 100644 --- a/test/distribution/test_kl.py +++ b/test/distribution/test_kl.py @@ -52,7 +52,7 @@ def setUp(self): ) def test_kl_divergence(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( paddle.distribution.kl_divergence(self.p, self.q), self.scipy_kl_beta_beta(self.a1, self.b1, self.a2, self.b2), @@ -87,7 +87,7 @@ def setUp(self): self.q = paddle.distribution.Dirichlet(paddle.to_tensor(self.conc2)) def test_kl_divergence(self): - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( paddle.distribution.kl_divergence(self.p, self.q), self.scipy_kl_diric_diric(self.conc1, self.conc2), diff --git a/test/dygraph_to_static/bert_dygraph_model.py b/test/dygraph_to_static/bert_dygraph_model.py index 7406f2bfaa5a5..d10fa881270c0 100644 --- a/test/dygraph_to_static/bert_dygraph_model.py +++ b/test/dygraph_to_static/bert_dygraph_model.py @@ -15,7 +15,7 @@ from transformer_dygraph_model import MultiHeadAttention, PrePostProcessLayer import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.nn import Layer, Linear @@ -35,7 +35,7 @@ def __init__( self._i2h = Linear( in_features=d_model, out_features=d_inner_hid, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=name + '_fc_0.w_0', initializer=param_initializer ), bias_attr=name + '_fc_0.b_0', @@ -44,7 +44,7 @@ def __init__( self._h2o = Linear( in_features=d_inner_hid, out_features=d_model, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=name + '_fc_1.w_0', initializer=param_initializer ), bias_attr=name + '_fc_1.b_0', @@ -207,7 +207,7 @@ def __init__(self, config, return_pooled_out=True, use_fp16=False): self._src_emb = paddle.nn.Embedding( self._voc_size, self._emb_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=self._word_emb_name, initializer=self._param_initializer ), ) @@ -215,7 +215,7 @@ def __init__(self, config, return_pooled_out=True, use_fp16=False): self._pos_emb = paddle.nn.Embedding( self._max_position_seq_len, self._emb_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=self._pos_emb_name, initializer=self._param_initializer ), ) @@ -223,7 +223,7 @@ def __init__(self, config, return_pooled_out=True, use_fp16=False): self._sent_emb = paddle.nn.Embedding( self._sent_types, self._emb_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=self._sent_emb_name, initializer=self._param_initializer ), ) @@ -231,7 +231,7 @@ def __init__(self, config, return_pooled_out=True, use_fp16=False): self.pooled_fc = Linear( in_features=self._emb_size, out_features=self._emb_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name="pooled_fc.w_0", initializer=self._param_initializer ), bias_attr="pooled_fc.b_0", @@ -332,13 +332,13 @@ def __init__( self.pooled_fc = Linear( in_features=self._emb_size, out_features=self._emb_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name="mask_lm_trans_fc.w_0", initializer=self._param_initializer ), bias_attr="mask_lm_trans_fc.b_0", ) - self.mask_lm_out_bias_attr = fluid.ParamAttr( + self.mask_lm_out_bias_attr = base.ParamAttr( name="mask_lm_out_fc.b_0", initializer=paddle.nn.initializer.Constant(value=0.0), ) @@ -347,7 +347,7 @@ def __init__( self.out_fc = Linear( in_features=self._emb_size, out_features=self._voc_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name="mask_lm_out_fc.w_0", initializer=self._param_initializer, ), @@ -364,7 +364,7 @@ def __init__( self.next_sent_fc = Linear( in_features=self._emb_size, out_features=2, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name="next_sent_fc.w_0", initializer=self._param_initializer ), bias_attr="next_sent_fc.b_0", diff --git a/test/dygraph_to_static/darknet.py b/test/dygraph_to_static/darknet.py index 396dfab950b0d..7606407a18615 100644 --- a/test/dygraph_to_static/darknet.py +++ b/test/dygraph_to_static/darknet.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle.fluid.param_attr import ParamAttr +from paddle.base.param_attr import ParamAttr from paddle.nn import BatchNorm from paddle.regularizer import L2Decay diff --git a/test/dygraph_to_static/dygraph_to_static_util.py b/test/dygraph_to_static/dygraph_to_static_util.py index 69a7c3ba93fc9..c95cab80f1112 100644 --- a/test/dygraph_to_static/dygraph_to_static_util.py +++ b/test/dygraph_to_static/dygraph_to_static_util.py @@ -19,7 +19,7 @@ import numpy as np from paddle import set_flags, static -from paddle.fluid import core +from paddle.base import core @contextlib.contextmanager diff --git a/test/dygraph_to_static/ifelse_simple_func.py b/test/dygraph_to_static/ifelse_simple_func.py index 14540375cfb05..c72ccca1ac7ae 100644 --- a/test/dygraph_to_static/ifelse_simple_func.py +++ b/test/dygraph_to_static/ifelse_simple_func.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle import fluid +from paddle import base def add_fn(x): @@ -378,7 +378,7 @@ def __init__(self): def if_tensor_case(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) mean = paddle.mean(x) # It is equivalent to `if mean != 0` diff --git a/test/dygraph_to_static/predictor_utils.py b/test/dygraph_to_static/predictor_utils.py index 8f0420f50d3bf..7ecb516f2c77d 100644 --- a/test/dygraph_to_static/predictor_utils.py +++ b/test/dygraph_to_static/predictor_utils.py @@ -16,8 +16,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor +from paddle import base +from paddle.base.core import AnalysisConfig, create_paddle_predictor class PredictorTools: @@ -47,7 +47,7 @@ def _load_model_and_set_config(self): else: config = AnalysisConfig(os.path.join(self.model_path)) - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): config.enable_use_gpu(100, 0) else: config.disable_gpu() @@ -78,7 +78,7 @@ def _get_analysis_outputs(self, config): tensor = predictor.get_input_tensor(name) feed_data = self.feeds_var[i] tensor.copy_from_cpu(np.array(feed_data)) - if type(feed_data) == fluid.LoDTensor: + if type(feed_data) == base.LoDTensor: tensor.set_lod(feed_data.lod()) # ensure no diff in multiple repeat times diff --git a/test/dygraph_to_static/seq2seq_dygraph_model.py b/test/dygraph_to_static/seq2seq_dygraph_model.py index 77eda7ab401bf..48c2ced2c2d8f 100644 --- a/test/dygraph_to_static/seq2seq_dygraph_model.py +++ b/test/dygraph_to_static/seq2seq_dygraph_model.py @@ -16,9 +16,9 @@ from seq2seq_utils import Seq2SeqModelHyperParams as args import paddle -from paddle import fluid -from paddle.fluid import ParamAttr -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import ParamAttr +from paddle.base.dygraph.base import to_variable from paddle.jit.api import to_static from paddle.nn import Embedding, Layer @@ -120,7 +120,7 @@ def __init__( self.src_embeder = Embedding( self.src_vocab_size, self.hidden_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=uniform_initializer(init_scale) ), ) @@ -129,7 +129,7 @@ def __init__( self.tar_vocab_size, self.hidden_size, sparse=False, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=uniform_initializer(init_scale) ), ) @@ -547,7 +547,7 @@ def __init__( self.src_embeder = Embedding( self.src_vocab_size, self.hidden_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='source_embedding', initializer=uniform_initializer(init_scale), ), @@ -557,7 +557,7 @@ def __init__( self.tar_vocab_size, self.hidden_size, sparse=False, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='target_embedding', initializer=uniform_initializer(init_scale), ), diff --git a/test/dygraph_to_static/simnet_dygraph_model.py b/test/dygraph_to_static/simnet_dygraph_model.py index 341785a9d6a5a..9bf53450e6fa0 100644 --- a/test/dygraph_to_static/simnet_dygraph_model.py +++ b/test/dygraph_to_static/simnet_dygraph_model.py @@ -15,7 +15,7 @@ from functools import reduce import paddle -import paddle.fluid.param_attr as attr +import paddle.base.param_attr as attr from paddle.jit.api import to_static from paddle.nn import Layer @@ -301,12 +301,12 @@ class FC(Layer): Examples: .. code-block:: python - from paddle.fluid.dygraph.base import to_variable - import paddle.fluid as fluid - from paddle.fluid.dygraph import FC + from paddle.base.dygraph.base import to_variable + import paddle.base as base + from paddle.base.dygraph import FC import numpy as np data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32') - with fluid.dygraph.guard(): + with base.dygraph.guard(): fc = FC("fc", 64, num_flatten_dims=2) data = to_variable(data) conv = fc(data) diff --git a/test/dygraph_to_static/test_assert.py b/test/dygraph_to_static/test_assert.py index d57084aa06f62..dc01413d0c8be 100644 --- a/test/dygraph_to_static/test_assert.py +++ b/test/dygraph_to_static/test_assert.py @@ -18,13 +18,13 @@ from dygraph_to_static_util import test_and_compare_with_new_ir import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static @paddle.jit.to_static def dyfunc_assert_variable(x): - x_v = fluid.dygraph.to_variable(x) + x_v = base.dygraph.to_variable(x) assert x_v @@ -38,10 +38,10 @@ def _run(self, func, x, with_exception, to_static): paddle.jit.enable_to_static(to_static) if with_exception: with self.assertRaises(BaseException): - with fluid.dygraph.guard(): + with base.dygraph.guard(): func(x) else: - with fluid.dygraph.guard(): + with base.dygraph.guard(): func(x) def _run_dy_static(self, func, x, with_exception): diff --git a/test/dygraph_to_static/test_ast_util.py b/test/dygraph_to_static/test_ast_util.py index d17780029bd81..52920d81433c6 100644 --- a/test/dygraph_to_static/test_ast_util.py +++ b/test/dygraph_to_static/test_ast_util.py @@ -26,7 +26,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base from paddle.jit.dy2static.utils import ast_to_func from paddle.utils import gast @@ -55,8 +55,8 @@ def test_ast2func_dygraph(self): funcs = [dyfunc_with_if_else, dyfunc_with_if_else2, nested_if_else] x_data = np.random.random([10, 16]).astype('float32') for func in funcs: - with fluid.dygraph.guard(): - x_v = fluid.dygraph.to_variable(x_data) + with base.dygraph.guard(): + x_v = base.dygraph.to_variable(x_data) true_ret = func(x_v).numpy() test_ret = self._ast2func(func)(x_v).numpy() self.assertTrue((true_ret == test_ret).all()) @@ -71,12 +71,12 @@ def func(x): return loss x_data = np.random.random([10, 16]).astype('float32') - main_program = fluid.Program() - with fluid.program_guard(main_program): + main_program = base.Program() + with base.program_guard(main_program): x_v = paddle.assign(x_data) true_ret = func(x_v) test_ret = self._ast2func(func)(x_v) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) ret = exe.run(main_program, fetch_list=[true_ret, test_ret]) self.assertTrue((ret[0] == ret[1]).all()) diff --git a/test/dygraph_to_static/test_basic_api_transformation.py b/test/dygraph_to_static/test_basic_api_transformation.py index 17e72b793f144..efa9caa17dd51 100644 --- a/test/dygraph_to_static/test_basic_api_transformation.py +++ b/test/dygraph_to_static/test_basic_api_transformation.py @@ -19,9 +19,9 @@ from dygraph_to_static_util import test_and_compare_with_new_ir import paddle -from paddle import fluid, to_tensor -from paddle.fluid import dygraph -from paddle.fluid.dygraph import to_variable +from paddle import base, to_tensor +from paddle.base import dygraph +from paddle.base.dygraph import to_variable from paddle.jit.api import dygraph_to_static_func from paddle.jit.dy2static.utils import is_dygraph_api from paddle.utils import gast @@ -35,7 +35,7 @@ def dyfunc_to_variable(x): - res = fluid.dygraph.to_variable(x, name=None, zero_copy=None) + res = base.dygraph.to_variable(x, name=None, zero_copy=None) return res @@ -82,24 +82,24 @@ def setUp(self): dyfunc_to_variable_3, ] self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) def get_dygraph_output(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.dygraph_func(self.input).numpy() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program): + with base.program_guard(main_program): static_out = dygraph_to_static_func(self.dygraph_func)(self.input) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -118,17 +118,17 @@ def dyfunc_BilinearTensorProduct(layer1, layer2): 5, 4, 1000, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ), ) res = bilinearTensorProduct( - fluid.dygraph.base.to_variable(layer1), - fluid.dygraph.base.to_variable(layer2), + base.dygraph.base.to_variable(layer1), + base.dygraph.base.to_variable(layer2), ) return res @@ -157,7 +157,7 @@ def dyfunc_Conv3D(input): weight_attr=paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ), ) @@ -170,10 +170,10 @@ def dyfunc_Conv2DTranspose(input): 3, 12, 12, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ), ) @@ -222,7 +222,7 @@ def dyfunc_Pool2D(input): def dyfunc_Prelu(input): prelu0 = paddle.nn.PReLU( - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), ) @@ -239,25 +239,25 @@ def setUp(self): self.dygraph_func = dyfunc_Pool2D def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED - data = fluid.dygraph.to_variable(self.input) + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED + data = base.dygraph.to_variable(self.input) res = self.dygraph_func(data).numpy() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): data = paddle.assign(self.input) static_out = dygraph_to_static_func(self.dygraph_func)(data) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -275,24 +275,24 @@ def setUp(self): self.dygraph_func = dyfunc_BilinearTensorProduct def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func(self.input1, self.input2).numpy() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): static_out = dygraph_to_static_func(self.dygraph_func)( self.input1, self.input2 ) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -401,22 +401,22 @@ def setUp(self): self.dygraph_func = dyfunc_CosineDecay def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func().numpy() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): static_out = dygraph_to_static_func(self.dygraph_func)() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -432,23 +432,23 @@ def setUp(self): self.dygraph_func = dyfunc_ExponentialDecay def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): static_out = dygraph_to_static_func(self.dygraph_func)() static_out = paddle.to_tensor(static_out) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -459,23 +459,23 @@ def setUp(self): self.dygraph_func = dyfunc_InverseTimeDecay def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): static_out = dygraph_to_static_func(self.dygraph_func)() static_out = paddle.to_tensor(static_out) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -486,23 +486,23 @@ def setUp(self): self.dygraph_func = dyfunc_NaturalExpDecay def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func() return res @test_and_compare_with_new_ir(True) def get_static_output(self): - startup_program = fluid.Program() + startup_program = base.Program() startup_program.random_seed = SEED - main_program = fluid.Program() + main_program = base.Program() main_program.random_seed = SEED - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): static_out = dygraph_to_static_func(self.dygraph_func)() static_out = paddle.to_tensor(static_out) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) static_res = exe.run(main_program, fetch_list=static_out) return static_res[0] @@ -523,19 +523,19 @@ def setUp(self): self.dygraph_func = dyfunc_PolynomialDecay def get_dygraph_output(self): - with fluid.dygraph.guard(): - fluid.default_startup_program.random_seed = SEED - fluid.default_main_program.random_seed = SEED + with base.dygraph.guard(): + base.default_startup_program.random_seed = SEED + base.default_main_program.random_seed = SEED res = self.dygraph_func() return res def _dygraph_fn(): - from paddle import fluid + from paddle import base x = np.random.random((1, 3)).astype('float32') - with fluid.dygraph.guard(): - fluid.dygraph.to_variable(x) + with base.dygraph.guard(): + base.dygraph.to_variable(x) np.random.random(1) diff --git a/test/dygraph_to_static/test_bert.py b/test/dygraph_to_static/test_bert.py index 431c9425c8f60..c7b5272ff4765 100644 --- a/test/dygraph_to_static/test_bert.py +++ b/test/dygraph_to_static/test_bert.py @@ -24,13 +24,11 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() SEED = 2020 STEP_NUM = 10 PRINT_STEP = 2 @@ -93,9 +91,9 @@ def tearDown(self): self.temp_dir.cleanup() def train(self, bert_config, data_reader, to_static): - with fluid.dygraph.guard(place): - fluid.default_main_program().random_seed = SEED - fluid.default_startup_program().random_seed = SEED + with base.dygraph.guard(place): + base.default_main_program().random_seed = SEED + base.default_startup_program().random_seed = SEED fake_dataset = FakeBertDataset(data_reader, STEP_NUM) data_loader = paddle.io.DataLoader( @@ -175,7 +173,7 @@ def train_static(self, bert_config, data_reader): def predict_static(self, data): paddle.enable_static() - exe = fluid.Executor(place) + exe = base.Executor(place) # load inference model [ inference_program, @@ -197,7 +195,7 @@ def predict_static(self, data): def predict_dygraph(self, bert_config, data): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): bert = PretrainModelLayer( config=bert_config, weight_sharing=False, use_fp16=False ) @@ -206,7 +204,7 @@ def predict_dygraph(self, bert_config, data): bert.set_dict(model_dict) bert.eval() - input_vars = [fluid.dygraph.to_variable(x) for x in data] + input_vars = [base.dygraph.to_variable(x) for x in data] ( src_ids, pos_ids, @@ -230,7 +228,7 @@ def predict_dygraph(self, bert_config, data): return pred_res def predict_dygraph_jit(self, data): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): bert = paddle.jit.load(self.model_save_prefix) bert.eval() diff --git a/test/dygraph_to_static/test_bmn.py b/test/dygraph_to_static/test_bmn.py index 4c0987941f9cc..f5f8d35759869 100644 --- a/test/dygraph_to_static/test_bmn.py +++ b/test/dygraph_to_static/test_bmn.py @@ -22,9 +22,9 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid import ParamAttr -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base import ParamAttr +from paddle.base.dygraph import to_variable from paddle.jit import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX @@ -34,8 +34,8 @@ # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, # some algorithm results are non-deterministic, like convolution algorithms. -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) def get_interp1d_mask( @@ -215,7 +215,7 @@ def __init__(self, cfg): self.num_sample, self.num_sample_perbin, ) - self.sample_mask = fluid.dygraph.base.to_variable(sample_mask) + self.sample_mask = base.dygraph.base.to_variable(sample_mask) self.sample_mask.stop_gradient = True self.p_conv3d1 = paddle.nn.Conv3D( @@ -642,9 +642,9 @@ class TestTrain(unittest.TestCase): def setUp(self): self.args = Args() self.place = ( - fluid.CPUPlace() - if not fluid.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not base.is_compiled_with_cuda() + else base.CUDAPlace(0) ) self.temp_dir = tempfile.TemporaryDirectory() @@ -661,7 +661,7 @@ def train_bmn(self, args, place, to_static): paddle.jit.enable_to_static(to_static) loss_data = [] - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) global local_random @@ -834,7 +834,7 @@ def verify_predict(self): def predict_dygraph(self, data): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): bmn = BMN(self.args) # load dygraph trained parameters model_dict = paddle.load(self.dy_param_path + ".pdparams") @@ -849,7 +849,7 @@ def predict_dygraph(self, data): def predict_static(self, data): paddle.enable_static() - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) # load inference model [ inference_program, @@ -870,7 +870,7 @@ def predict_static(self, data): return pred_res def predict_dygraph_jit(self, data): - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): bmn = paddle.jit.load(self.model_save_prefix) bmn.eval() diff --git a/test/dygraph_to_static/test_break_continue.py b/test/dygraph_to_static/test_break_continue.py index 946dfb9850c59..dc5e74ff8d193 100644 --- a/test/dygraph_to_static/test_break_continue.py +++ b/test/dygraph_to_static/test_break_continue.py @@ -18,7 +18,7 @@ from dygraph_to_static_util import ast_only_test, dy2static_unittest import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.jit.dy2static.utils import Dygraph2StaticException @@ -39,12 +39,12 @@ def test_error(self): with self.assertRaisesRegex(Dygraph2StaticException, self.error): paddle.jit.enable_to_static(True) self.assertTrue(to_static(self.dyfunc)(self.x)) - paddle.fluid.dygraph.base.global_var._in_declarative_mode_ = False + paddle.base.dygraph.base.global_var._in_declarative_mode_ = False paddle.jit.enable_to_static(False) def test_continue_in_for(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(10): x += 1 if i > 5: @@ -55,7 +55,7 @@ def test_continue_in_for(x): def test_continue_in_for_at_end(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(10): x += 1 if i > 5: @@ -64,7 +64,7 @@ def test_continue_in_for_at_end(x): def test_continue_in_while(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) i = paddle.tensor.fill_constant(shape=[1], dtype='int32', value=0) while i < 10: i += 1 @@ -76,7 +76,7 @@ def test_continue_in_while(x): def test_break_in_for(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(10): x += 1 if i > 5: @@ -87,7 +87,7 @@ def test_break_in_for(x): def test_break_in_for_at_end(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(10): x += 1 if i > 5: @@ -96,7 +96,7 @@ def test_break_in_for_at_end(x): def test_break_in_while(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) i = paddle.tensor.fill_constant(shape=[1], dtype='int32', value=0) while i < 10: i += 1 @@ -108,7 +108,7 @@ def test_break_in_while(x): def test_break_continue_in_for(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(1, 10, 1): if i <= 4: @@ -138,7 +138,7 @@ def test_break_continue_in_for(x): def test_for_in_else(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) # Case 1: if False: @@ -169,7 +169,7 @@ def __init__(self): self.c = 5 foo = Foo() - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) while i < 10: foo.b = paddle.zeros(shape=[1], dtype='float32') foo.c = foo.b + foo.a @@ -209,9 +209,9 @@ class TestContinueInFor(unittest.TestCase): def setUp(self): self.input = np.zeros(1).astype('int64') self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.init_dygraph_func() @@ -219,12 +219,12 @@ def init_dygraph_func(self): self.dygraph_func = test_continue_in_for def run_dygraph_mode(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.dygraph_func(self.input) return res.numpy() def run_static_mode(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = to_static(self.dygraph_func)(self.input) return res.numpy() diff --git a/test/dygraph_to_static/test_build_strategy.py b/test/dygraph_to_static/test_build_strategy.py index 14e29b9ef4508..92968fabf28f5 100644 --- a/test/dygraph_to_static/test_build_strategy.py +++ b/test/dygraph_to_static/test_build_strategy.py @@ -31,7 +31,7 @@ def setUp(self): self.build_strategy.enable_addto = True self.resnet_helper = ResNetHelper() # NOTE: for enable_addto - paddle.fluid.set_flags({"FLAGS_max_inplace_grad_add": 8}) + paddle.base.set_flags({"FLAGS_max_inplace_grad_add": 8}) def train(self, to_static): paddle.jit.enable_to_static(to_static) @@ -82,12 +82,12 @@ def test_resnet(self): @ast_only_test def test_in_static_mode_mkldnn(self): - paddle.fluid.set_flags({'FLAGS_use_mkldnn': True}) + paddle.base.set_flags({'FLAGS_use_mkldnn': True}) try: - if paddle.fluid.core.is_compiled_with_mkldnn(): + if paddle.base.core.is_compiled_with_mkldnn(): self.resnet_helper.train(True, self.build_strategy) finally: - paddle.fluid.set_flags({'FLAGS_use_mkldnn': False}) + paddle.base.set_flags({'FLAGS_use_mkldnn': False}) class TestError(unittest.TestCase): diff --git a/test/dygraph_to_static/test_cache_program.py b/test/dygraph_to_static/test_cache_program.py index eaf45b49a61e2..2b8a88245de87 100644 --- a/test/dygraph_to_static/test_cache_program.py +++ b/test/dygraph_to_static/test_cache_program.py @@ -20,7 +20,7 @@ from test_fetch_feed import Linear, Pool2D import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.jit.dy2static import convert_to_static @@ -35,7 +35,7 @@ def setUp(self): def test_cache(self): prev_ops, cur_ops = Counter(), Counter() prev_out, cur_out = None, None - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): static_net = self.dygraph_class() for batch_id in range(self.batch_num): out = static_net(paddle.to_tensor(self.data)) @@ -45,10 +45,7 @@ def test_cache(self): # Check forward ops prev_ops = cur_ops cur_ops = Counter( - [ - op.type - for op in fluid.default_main_program().block(0).ops - ] + [op.type for op in base.default_main_program().block(0).ops] ) if batch_id > 0: prev_out_numpy = ( @@ -94,14 +91,14 @@ def train_dygraph(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): dygraph_net = self.dygraph_class() adam = paddle.optimizer.Adam( learning_rate=0.001, parameters=dygraph_net.parameters() ) loss_data = [] for batch_id in range(self.batch_num): - input = fluid.dygraph.to_variable(self.data) + input = base.dygraph.to_variable(self.data) pred, avg_loss = dygraph_net(input) loss_data.append(avg_loss.numpy()) @@ -125,7 +122,7 @@ def test_with_optimizer(self): def simple_func(x): - inputs = fluid.dygraph.to_variable(x) + inputs = base.dygraph.to_variable(x) mean = paddle.mean(inputs) return mean @@ -140,7 +137,7 @@ def test_cache(self): @to_static def sum_even_until_limit(max_len, limit): - ret_sum = fluid.dygraph.to_variable(np.zeros(1).astype('int32')) + ret_sum = base.dygraph.to_variable(np.zeros(1).astype('int32')) for i in range(max_len): if i % 2 > 0: continue @@ -152,8 +149,8 @@ def sum_even_until_limit(max_len, limit): def sum_under_while(limit): - i = fluid.dygraph.to_variable(np.zeros(1).astype('int32')) - ret_sum = fluid.dygraph.to_variable(np.zeros(1).astype('int32')) + i = base.dygraph.to_variable(np.zeros(1).astype('int32')) + ret_sum = base.dygraph.to_variable(np.zeros(1).astype('int32')) while i <= limit: ret_sum += i i += 1 @@ -162,7 +159,7 @@ def sum_under_while(limit): class TestToOutputWithCache(unittest.TestCase): def test_output(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): ret = sum_even_until_limit(80, 10) self.assertEqual(ret.numpy(), 30) diff --git a/test/dygraph_to_static/test_cast.py b/test/dygraph_to_static/test_cast.py index c524cae6cf83b..23e31f2b4cc99 100644 --- a/test/dygraph_to_static/test_cast.py +++ b/test/dygraph_to_static/test_cast.py @@ -17,7 +17,7 @@ import numpy as np from dygraph_to_static_util import ast_only_test, test_and_compare_with_new_ir -from paddle import fluid +from paddle import base from paddle.jit.api import to_static SEED = 2020 @@ -26,20 +26,20 @@ @to_static def test_bool_cast(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x = bool(x) return x @to_static def test_int_cast(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x = int(x) return x def test_float_cast(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x = float(x) return x @@ -52,7 +52,7 @@ def test_not_var_cast(x): @to_static def test_mix_cast(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x = int(x) x = float(x) x = bool(x) @@ -63,9 +63,9 @@ def test_mix_cast(x): class TestCastBase(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.prepare() self.set_func() @@ -84,7 +84,7 @@ def set_func(self): self.func = test_bool_cast def do_test(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.func(self.input) return res diff --git a/test/dygraph_to_static/test_cinn_prim.py b/test/dygraph_to_static/test_cinn_prim.py index 960e9bcfc6c0e..0bf905ec846f9 100644 --- a/test/dygraph_to_static/test_cinn_prim.py +++ b/test/dygraph_to_static/test_cinn_prim.py @@ -23,7 +23,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/dygraph_to_static/test_cinn_prim_gelu.py b/test/dygraph_to_static/test_cinn_prim_gelu.py index 88fa501f7696b..be2e8f67c1e98 100644 --- a/test/dygraph_to_static/test_cinn_prim_gelu.py +++ b/test/dygraph_to_static/test_cinn_prim_gelu.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core TOLERANCE = { "float16": {"rtol": 1e-3, "atol": 1e-3}, diff --git a/test/dygraph_to_static/test_cinn_prim_layer_norm.py b/test/dygraph_to_static/test_cinn_prim_layer_norm.py index 9e15c98ed8a38..18c48883d75a6 100644 --- a/test/dygraph_to_static/test_cinn_prim_layer_norm.py +++ b/test/dygraph_to_static/test_cinn_prim_layer_norm.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core TOLERANCE = { "float16": {"rtol": 1e-2, "atol": 1e-2}, diff --git a/test/dygraph_to_static/test_cinn_prim_mean.py b/test/dygraph_to_static/test_cinn_prim_mean.py index 65451ffad5911..cb32f5b466035 100644 --- a/test/dygraph_to_static/test_cinn_prim_mean.py +++ b/test/dygraph_to_static/test_cinn_prim_mean.py @@ -19,7 +19,7 @@ import paddle from paddle import tensor -from paddle.fluid import core +from paddle.base import core TOLERANCE = { "float16": {"rtol": 1e-3, "atol": 1e-3}, diff --git a/test/dygraph_to_static/test_convert_call.py b/test/dygraph_to_static/test_convert_call.py index 7b54ea5956134..79f23351cb6dd 100644 --- a/test/dygraph_to_static/test_convert_call.py +++ b/test/dygraph_to_static/test_convert_call.py @@ -20,7 +20,7 @@ import paddle import paddle.jit.dy2static as _jst -from paddle import fluid +from paddle import base from paddle.jit.dy2static.convert_call_func import CONVERSION_OPTIONS from paddle.jit.dy2static.utils import func_to_source_code @@ -42,7 +42,7 @@ def dyfunc_with_if(x_v): @paddle.jit.to_static def nested_func(x_v): - x_v = fluid.dygraph.to_variable(x_v) + x_v = base.dygraph.to_variable(x_v) def fn1(): return x_v @@ -81,9 +81,9 @@ class TestRecursiveCall1(unittest.TestCase): def setUp(self): self.input = np.random.random([10, 16]).astype('float32') self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.init_test_func() @@ -92,13 +92,13 @@ def init_test_func(self): def get_dygraph_output(self): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.dyfunc(self.input).numpy() return res def get_static_output(self): paddle.jit.enable_to_static(True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.dyfunc(self.input).numpy() return res @@ -174,9 +174,9 @@ class TestRecursiveCall2(unittest.TestCase): def setUp(self): self.input = np.random.random((1, 3, 3, 5)).astype('float32') self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.set_func() @@ -184,8 +184,8 @@ def set_func(self): self.dygraph_func = MyLayer() def _run(self): - with fluid.dygraph.guard(): - data = fluid.dygraph.to_variable(self.input) + with base.dygraph.guard(): + data = base.dygraph.to_variable(self.input) res = self.dygraph_func(data) return res.numpy() diff --git a/test/dygraph_to_static/test_cycle_gan.py b/test/dygraph_to_static/test_cycle_gan.py index dbe3d07dfd264..3484b27d5fac5 100644 --- a/test/dygraph_to_static/test_cycle_gan.py +++ b/test/dygraph_to_static/test_cycle_gan.py @@ -31,7 +31,7 @@ import numpy as np from PIL import Image, ImageOps -from paddle import fluid +from paddle import base # Use GPU:0 to elimate the influence of other tasks. os.environ["CUDA_VISIBLE_DEVICES"] = "1" @@ -39,7 +39,7 @@ from dygraph_to_static_util import test_and_compare_with_new_ir import paddle -from paddle.fluid.dygraph import to_variable +from paddle.base.dygraph import to_variable from paddle.jit.api import to_static from paddle.nn import BatchNorm @@ -48,8 +48,8 @@ # some algorithm results are non-deterministic, like convolution algorithms. # 2. If include BatchNorm, please set `use_global_stats=True` to avoid using # cudnnBatchNormalizationBackward which is non-deterministic. -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) # set False to speed up training. use_cudnn = False @@ -353,7 +353,7 @@ def __init__( if not use_bias: con_bias_attr = False else: - con_bias_attr = fluid.ParamAttr( + con_bias_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ) @@ -371,16 +371,16 @@ def __init__( # Note(Aurelius84): The calculation of GPU kernel in BN is non-deterministic, # failure rate is 1/100 in Dev but seems incremental in CE platform. # If on GPU, we disable BN temporarily. - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): norm = False if norm: self.bn = BatchNorm( use_global_stats=True, # set True to use deterministic algorithm num_channels=num_filters, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal(1.0, 0.02) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ), trainable_statistics=True, @@ -420,7 +420,7 @@ def __init__( if not use_bias: de_bias_attr = False else: - de_bias_attr = fluid.ParamAttr( + de_bias_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ) @@ -430,21 +430,21 @@ def __init__( filter_size, stride=stride, padding=padding, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal(mean=0.0, std=stddev) ), bias_attr=de_bias_attr, ) - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): norm = False if norm: self.bn = BatchNorm( use_global_stats=True, # set True to use deterministic algorithm num_channels=num_filters, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal(1.0, 0.02) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ), trainable_statistics=True, @@ -550,21 +550,19 @@ def optimizer_setting(parameters): def train(args, to_static): place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() ) paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): max_images_num = args.max_images_num data_shape = [-1] + args.image_shape random.seed(SEED) np.random.seed(SEED) - fluid.default_startup_program().random_seed = SEED - fluid.default_main_program().random_seed = SEED + base.default_startup_program().random_seed = SEED + base.default_main_program().random_seed = SEED A_pool = ImagePool() B_pool = ImagePool() @@ -704,7 +702,7 @@ def test_train(self): # Note(Aurelius84): Because we disable BN on GPU, # but here we enhance the check on CPU by `np.array_equal` # which means the dy_out and st_out shall be exactly same. - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): np.testing.assert_array_equal(dy_out, st_out) else: np.testing.assert_allclose(dy_out, st_out, rtol=1e-5, atol=1e-8) diff --git a/test/dygraph_to_static/test_declarative.py b/test/dygraph_to_static/test_declarative.py index 9ea71498ee317..807babfced90b 100644 --- a/test/dygraph_to_static/test_declarative.py +++ b/test/dygraph_to_static/test_declarative.py @@ -20,8 +20,8 @@ from test_basic_api_transformation import dyfunc_to_variable import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.jit.api import to_static from paddle.jit.dy2static.program_translator import ( ConcreteProgram, @@ -90,7 +90,7 @@ def func_with_list_dict(self, dl): class TestStaticFunctionInstance(unittest.TestCase): def test_instance_same_class(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): net_1 = SimpleNet() net_2 = SimpleNet() @@ -114,7 +114,7 @@ def tearDown(self): self.temp_dir.cleanup() def test_with_input_spec(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 @@ -150,7 +150,7 @@ def test_with_input_spec(self): out = net.func_with_list_dict([int_np, {'x': x, 'y': y}]) def test_with_error(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 @@ -174,7 +174,7 @@ def test_with_error(self): net.add_func(x, y) def test_concrete_program(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x = to_variable(np.ones([4, 10]).astype('float32')) y = to_variable(np.ones([4, 10]).astype('float32') * 2) int_val = 4.0 @@ -213,7 +213,7 @@ def setUp(self): paddle.jit.enable_to_static(True) def test_with_different_input(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x_data = np.ones([16, 10]).astype('float32') y_data = np.ones([10]).astype('float32') * 2 z_data = np.ones([10]).astype('float32') * 2.2 @@ -298,7 +298,7 @@ def test_get_concrete_program(self): ) def test_concrete_program(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): # usage 1 foo_1 = paddle.jit.to_static( foo_func, @@ -358,7 +358,7 @@ def test_error(self): paddle.jit.enable_to_static(False) with self.assertRaises(AssertionError): # AssertionError: We Only support to_variable in imperative mode, - # please use fluid.dygraph.guard() as context to run it in imperative Mode + # please use base.dygraph.guard() as context to run it in imperative Mode func(np.ones(5).astype("int32")) diff --git a/test/dygraph_to_static/test_dict.py b/test/dygraph_to_static/test_dict.py index 263f4c069bdb9..650d7c58b0ba1 100644 --- a/test/dygraph_to_static/test_dict.py +++ b/test/dygraph_to_static/test_dict.py @@ -17,12 +17,10 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit import to_static -PLACE = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +PLACE = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() class SubNetWithDict(paddle.nn.Layer): @@ -53,7 +51,7 @@ def __init__(self, hidden_size=16, output_size=16): ) def forward(self, input, cache=None): - input = fluid.dygraph.to_variable(input) + input = base.dygraph.to_variable(input) q = self.q_fc(input) k = self.k_fc(input) @@ -82,7 +80,7 @@ def __init__(self, batch_size=64, hidden_size=16, output_size=16): @to_static def forward(self, input, max_len=4): - input = fluid.dygraph.to_variable(input) + input = base.dygraph.to_variable(input) cache = { "k": paddle.tensor.fill_constant( shape=[self.batch_size, self.output_size], @@ -120,7 +118,7 @@ def update_cache(cache): class TestNetWithDict(unittest.TestCase): """ TestCase for the transformation from control flow `if/else` - dependent on tensor in Dygraph into Static `fluid.layers.cond`. + dependent on tensor in Dygraph into Static `base.layers.cond`. """ def setUp(self): @@ -135,7 +133,7 @@ def _run_dygraph(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(PLACE): + with base.dygraph.guard(PLACE): net = MainNetWithDict(batch_size=self.batch_size) ret = net(self.x) return ret.numpy() @@ -235,7 +233,7 @@ def setUp(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(PLACE): + with base.dygraph.guard(PLACE): net = NetWithDictPop() ret = net(z=0, x=self.x, y=True) return ret.numpy() diff --git a/test/dygraph_to_static/test_duplicate_output.py b/test/dygraph_to_static/test_duplicate_output.py index 08066874a0630..2358bc945d6f7 100644 --- a/test/dygraph_to_static/test_duplicate_output.py +++ b/test/dygraph_to_static/test_duplicate_output.py @@ -20,10 +20,10 @@ np.random.seed(1) -if paddle.fluid.is_compiled_with_cuda(): - place = paddle.fluid.CUDAPlace(0) +if paddle.base.is_compiled_with_cuda(): + place = paddle.base.CUDAPlace(0) else: - place = paddle.fluid.CPUPlace() + place = paddle.base.CPUPlace() class SimpleNet(paddle.nn.Layer): @@ -40,7 +40,7 @@ def forward(self, x): class TestDuplicateOutput(unittest.TestCase): """ TestCase for the transformation from control flow `if/else` - dependent on tensor in Dygraph into Static `fluid.layers.cond`. + dependent on tensor in Dygraph into Static `base.layers.cond`. """ def setUp(self): diff --git a/test/dygraph_to_static/test_error.py b/test/dygraph_to_static/test_error.py index 9602594fb54d4..22d2d9676fbd4 100644 --- a/test/dygraph_to_static/test_error.py +++ b/test/dygraph_to_static/test_error.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit.dy2static import error from paddle.jit.dy2static.origin_info import unwrap @@ -33,7 +33,7 @@ def inner_func(): @paddle.jit.to_static def func_error_in_compile_time(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) inner_func() if paddle.mean(x) < 0: x_v = x - 1 @@ -44,14 +44,14 @@ def func_error_in_compile_time(x): @paddle.jit.to_static def func_error_in_compile_time_2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x = paddle.reshape(x, shape=[1, 2]) return x @paddle.jit.to_static def func_error_in_runtime(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) two = paddle.tensor.fill_constant(shape=[1], value=2, dtype="int32") x = paddle.reshape(x, shape=[1, two]) return x @@ -102,7 +102,7 @@ def test_func(self): @paddle.jit.to_static def func_error_in_runtime_with_empty_line(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) two = paddle.tensor.fill_constant(shape=[1], value=2, dtype="int32") x = paddle.reshape(x, shape=[1, two]) @@ -292,7 +292,7 @@ def set_message(self): self.filepath ), 'def func_error_in_compile_time_2(x):', - 'x = fluid.dygraph.to_variable(x)', + 'x = base.dygraph.to_variable(x)', 'x = paddle.reshape(x, shape=[1, 2])', '<--- HERE', 'return x', @@ -341,7 +341,7 @@ def set_message(self): 'File "{}", line 56, in func_error_in_runtime'.format( self.filepath ), - 'x = fluid.dygraph.to_variable(x)', + 'x = base.dygraph.to_variable(x)', 'two = paddle.tensor.fill_constant(shape=[1], value=2, dtype="int32")', 'x = paddle.reshape(x, shape=[1, two])', '<--- HERE', diff --git a/test/dygraph_to_static/test_eval_frame.py b/test/dygraph_to_static/test_eval_frame.py index dfa5e04b44ffb..e653e67393e7a 100644 --- a/test/dygraph_to_static/test_eval_frame.py +++ b/test/dygraph_to_static/test_eval_frame.py @@ -57,9 +57,9 @@ def add(a, b): x = 1 y = 2 - paddle.fluid.core.set_eval_frame(callback) + paddle.base.core.set_eval_frame(callback) assert add(x, y) == 2, "should be 2" - paddle.fluid.core.set_eval_frame(None) + paddle.base.core.set_eval_frame(None) assert add(x, y) == 3, "should be 3" diff --git a/test/dygraph_to_static/test_fetch_feed.py b/test/dygraph_to_static/test_fetch_feed.py index d830896e5b065..0d754a595ac96 100644 --- a/test/dygraph_to_static/test_fetch_feed.py +++ b/test/dygraph_to_static/test_fetch_feed.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static SEED = 2020 @@ -69,9 +69,9 @@ def setUp(self): def train(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(): + with base.dygraph.guard(): dy_layer = self.dygraph_class() - x = fluid.dygraph.to_variable(self.data) + x = base.dygraph.to_variable(self.data) prediction = dy_layer(x) if isinstance(prediction, (list, tuple)): prediction = prediction[0] diff --git a/test/dygraph_to_static/test_for_enumerate.py b/test/dygraph_to_static/test_for_enumerate.py index 1aca0549213c2..bbb64e8756ea3 100644 --- a/test/dygraph_to_static/test_for_enumerate.py +++ b/test/dygraph_to_static/test_for_enumerate.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.static import InputSpec @@ -27,7 +27,7 @@ @paddle.jit.to_static def for_in_range(x): z = paddle.tensor.fill_constant([1], 'int32', 0) - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(x.numpy().item()): z = z + i return z @@ -55,7 +55,7 @@ def for_enumerate_list(x_array): @paddle.jit.to_static def for_iter_var_numpy(x_array): z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for x in x_array.numpy(): z = z + x return z @@ -66,7 +66,7 @@ def for_iter_var_numpy(x_array): def for_enumerate_var_numpy(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy()): y = y + i z = z + x @@ -78,7 +78,7 @@ def for_enumerate_var_numpy(x_array): def for_enumerate_var_numpy_with_start(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy(), 1): y = y + i z = z + x @@ -89,7 +89,7 @@ def for_enumerate_var_numpy_with_start(x_array): @paddle.jit.to_static def for_in_range_with_break(x): z = paddle.tensor.fill_constant([1], 'int32', 0) - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(x.numpy()[0]): z = z + i if i > 2: @@ -102,7 +102,7 @@ def for_in_range_with_break(x): def for_enumerate_var_numpy_with_break(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy()): y = y + i z = z + x @@ -116,7 +116,7 @@ def for_enumerate_var_numpy_with_break(x_array): def for_enumerate_var_numpy_with_continue(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy()): y = y + i if i > 2: @@ -130,7 +130,7 @@ def for_enumerate_var_numpy_with_continue(x_array): def for_enumerate_var_numpy_with_start_break(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy(), 1): y = y + i z = z + x @@ -144,7 +144,7 @@ def for_enumerate_var_numpy_with_start_break(x_array): def for_enumerate_var_numpy_with_start_continue(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array.numpy(), 1): y = y + i if i > 2: @@ -157,7 +157,7 @@ def for_enumerate_var_numpy_with_start_continue(x_array): @paddle.jit.to_static def for_iter_var(x_array): z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for x in x_array: z = z + x @@ -169,7 +169,7 @@ def for_iter_var(x_array): def for_enumerate_var(x_array): y = paddle.tensor.fill_constant([1], 'int32', 0) z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, x in enumerate(x_array): y = y + i z = z + x @@ -180,7 +180,7 @@ def for_enumerate_var(x_array): @paddle.jit.to_static def for_iter_var_list(x): # 1. prepare data, ref test_list.py - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.tensor.fill_constant(shape=[1], value=5, dtype="int32") a = [] for i in range(iter_num): @@ -196,7 +196,7 @@ def for_iter_var_list(x): @paddle.jit.to_static def for_enumerate_var_list(x): # 1. prepare data, ref test_list.py - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.tensor.fill_constant(shape=[1], value=5, dtype="int32") a = [] for i in range(iter_num): @@ -214,7 +214,7 @@ def for_enumerate_var_list(x): @paddle.jit.to_static def for_enumerate_var_with_nested_range(x_array): x = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for i, num in enumerate(x_array): for idx in range(num): x = x + num @@ -225,7 +225,7 @@ def for_enumerate_var_with_nested_range(x_array): @paddle.jit.to_static def for_iter_var_idx(x_array): z = paddle.tensor.fill_constant([1], 'int32', 0) - x_array = fluid.dygraph.to_variable(x_array) + x_array = base.dygraph.to_variable(x_array) for x in x_array[0:]: z = z + x @@ -356,9 +356,9 @@ def tensor_array_slice_in_enumerate(): class TestTransformBase(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.set_input() self.set_test_func() @@ -373,7 +373,7 @@ def set_test_func(self): def _run(self, to_static): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(): + with base.dygraph.guard(): return self.dygraph_func(self.input) def get_dygraph_output(self): @@ -401,7 +401,7 @@ def transformed_result_compare(self): class TestTransformForOriginalList(TestTransform): def _run(self, to_static): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(): + with base.dygraph.guard(): return self.dygraph_func() diff --git a/test/dygraph_to_static/test_full_name_usage.py b/test/dygraph_to_static/test_full_name_usage.py index f9cbbeb639a51..0332480891e16 100644 --- a/test/dygraph_to_static/test_full_name_usage.py +++ b/test/dygraph_to_static/test_full_name_usage.py @@ -18,12 +18,12 @@ from dygraph_to_static_util import ast_only_test import paddle -from paddle import fluid +from paddle import base @paddle.jit.to_static def dygraph_decorated_func(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) if paddle.mean(x) > 0: x_v = x - 1 else: @@ -33,7 +33,7 @@ def dygraph_decorated_func(x): @paddle.jit.to_static def jit_decorated_func(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) if paddle.mean(x) > 0: x_v = x - 1 else: @@ -63,7 +63,7 @@ class TestFullNameDecorator(unittest.TestCase): def test_run_success(self): x = np.ones([1, 2]).astype("float32") answer = np.zeros([1, 2]).astype("float32") - with fluid.dygraph.guard(): + with base.dygraph.guard(): np.testing.assert_allclose( dygraph_decorated_func(x).numpy(), answer, rtol=1e-05 ) diff --git a/test/dygraph_to_static/test_ifelse.py b/test/dygraph_to_static/test_ifelse.py index 7c3fe0315aa0a..381ba5b8daef4 100644 --- a/test/dygraph_to_static/test_ifelse.py +++ b/test/dygraph_to_static/test_ifelse.py @@ -19,6 +19,7 @@ from ifelse_simple_func import ( NetWithControlFlowIf, add_fn, + base, dyfunc_empty_nonlocal, dyfunc_ifelse_ret_int1, dyfunc_ifelse_ret_int2, @@ -28,7 +29,6 @@ dyfunc_with_if_else2, dyfunc_with_if_else3, dyfunc_with_if_else_with_list_generator, - fluid, if_tensor_case, if_with_and_or, if_with_and_or_1, @@ -44,15 +44,15 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.jit.dy2static.utils import Dygraph2StaticException np.random.seed(1) -if fluid.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) +if base.is_compiled_with_cuda(): + place = base.CUDAPlace(0) else: - place = fluid.CPUPlace() + place = base.CPUPlace() @dy2static_unittest @@ -68,14 +68,14 @@ def test_error(self): with self.assertRaisesRegex(Dygraph2StaticException, self.error): paddle.jit.enable_to_static(True) self.assertTrue(paddle.jit.to_static(self.dyfunc)(self.x)) - paddle.fluid.dygraph.base.global_var._in_declarative_mode_ = False + paddle.base.dygraph.base.global_var._in_declarative_mode_ = False paddle.jit.enable_to_static(False) class TestDygraphIfElse(unittest.TestCase): """ TestCase for the transformation from control flow `if/else` - dependent on tensor in Dygraph into Static `fluid.layers.cond`. + dependent on tensor in Dygraph into Static `base.layers.cond`. """ def setUp(self): @@ -86,8 +86,8 @@ def _run_static(self): return self._run_dygraph(to_static=True) def _run_dygraph(self, to_static=False): - with fluid.dygraph.guard(place): - x_v = fluid.dygraph.to_variable(self.x) + with base.dygraph.guard(place): + x_v = base.dygraph.to_variable(self.x) if to_static: ret = paddle.jit.to_static(self.dyfunc)(x_v) else: @@ -241,7 +241,7 @@ def setUp(self): class TestDygraphIfElseNet(unittest.TestCase): """ TestCase for the transformation from control flow `if/else` - dependent on tensor in Dygraph into Static `fluid.layers.cond`. + dependent on tensor in Dygraph into Static `base.layers.cond`. """ def setUp(self): @@ -257,9 +257,9 @@ def _run_dygraph(self): def _run(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): net = self.Net() - x_v = fluid.dygraph.to_variable(self.x) + x_v = base.dygraph.to_variable(self.x) ret = net(x_v) return ret.numpy() @@ -473,7 +473,7 @@ def test_ast_to_func(self): # that the code block is under @to_static, but in this UT # an exception is thrown during Dy2St, making the `_in_declarative_mode_` # a wrong value. So We need set `_in_declarative_mode_` to False manually. - paddle.fluid.dygraph.base.global_var._in_declarative_mode_ = False + paddle.base.dygraph.base.global_var._in_declarative_mode_ = False paddle.jit.enable_to_static(False) diff --git a/test/dygraph_to_static/test_lac.py b/test/dygraph_to_static/test_lac.py index e3382d26964e4..a650a25c25b07 100644 --- a/test/dygraph_to_static/test_lac.py +++ b/test/dygraph_to_static/test_lac.py @@ -23,8 +23,8 @@ os.environ["CUDA_VISIBLE_DEVICES"] = "2" import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid.dygraph import to_variable +from paddle import _legacy_C_ops, base +from paddle.base.dygraph import to_variable from paddle.framework import in_dynamic_mode from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX @@ -96,7 +96,7 @@ def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None): self.pre_gru = paddle.nn.Linear( in_features=input_dim, out_features=grnn_hidden_dim * 3, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -107,7 +107,7 @@ def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None): self.gru = DynamicGRU( size=grnn_hidden_dim, h_0=h_0, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -118,7 +118,7 @@ def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None): self.pre_gru_r = paddle.nn.Linear( in_features=input_dim, out_features=grnn_hidden_dim * 3, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -130,7 +130,7 @@ def __init__(self, input_dim, grnn_hidden_dim, init_bound, h_0=None): size=grnn_hidden_dim, is_reverse=True, h_0=h_0, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-init_bound, high=init_bound ), @@ -363,7 +363,7 @@ def __init__(self, args, length=None): self.word_embedding = paddle.nn.Embedding( self.vocab_size, self.word_emb_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( learning_rate=self.emb_lr, name="word_emb", initializer=paddle.nn.initializer.Uniform( @@ -405,7 +405,7 @@ def __init__(self, args, length=None): self.fc = paddle.nn.Linear( in_features=self.grnn_hidden_dim * 2, out_features=self.num_labels, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self.init_bound, high=self.init_bound ), @@ -414,14 +414,14 @@ def __init__(self, args, length=None): ) self.linear_chain_crf = LinearChainCRF( - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='linear_chain_crfw', learning_rate=self.crf_lr ), size=self.num_labels, ) self.crf_decoding = CRFDecoding( - param_attr=fluid.ParamAttr(name='crfw', learning_rate=self.crf_lr), + param_attr=base.ParamAttr(name='crfw', learning_rate=self.crf_lr), size=self.num_labels, ) # share weight @@ -504,7 +504,7 @@ def __reader__(): def create_dataloader(reader, place): - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( capacity=16, use_double_buffer=True, iterable=True ) @@ -517,9 +517,9 @@ class TestLACModel(unittest.TestCase): def setUp(self): self.args = Args() self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.temp_dir = tempfile.TemporaryDirectory() self.model_save_dir = os.path.join(self.temp_dir.name, 'inference') @@ -531,11 +531,11 @@ def setUp(self): def train(self, args, to_static): paddle.jit.enable_to_static(to_static) place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -646,7 +646,7 @@ def verify_predict(self): def predict_dygraph(self, batch): words, targets, length = batch paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): model = LexNet(self.args) # load dygraph trained parameters model_dict = paddle.load(self.dy_param_path + ".pdparams") @@ -665,7 +665,7 @@ def predict_static(self, batch): Load inference model to test it's ok for prediction. """ paddle.enable_static() - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) # load inference model [ inference_program, @@ -688,7 +688,7 @@ def predict_static(self, batch): def predict_dygraph_jit(self, batch): words, targets, length = batch - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): model = paddle.jit.load(self.model_save_prefix) model.eval() diff --git a/test/dygraph_to_static/test_lambda.py b/test/dygraph_to_static/test_lambda.py index bab465afe7821..c1ff57147564c 100644 --- a/test/dygraph_to_static/test_lambda.py +++ b/test/dygraph_to_static/test_lambda.py @@ -18,11 +18,11 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base def call_lambda_as_func(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) add_func = lambda x, y: x + y mean_func = lambda x: paddle.mean(x) @@ -35,7 +35,7 @@ def call_lambda_as_func(x): def call_lambda_directly(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = (lambda x, y: x + y)(x, x) out = (lambda x: paddle.mean(x))(y) @@ -44,7 +44,7 @@ def call_lambda_directly(x): def call_lambda_in_func(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) add_func = lambda x: x + 1 @@ -55,7 +55,7 @@ def call_lambda_in_func(x): def call_lambda_with_ifExpr(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) add_func = lambda x: x + 1 @@ -66,7 +66,7 @@ def call_lambda_with_ifExpr(x): def call_lambda_with_ifExpr2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) add_func = lambda x: x + 1 @@ -84,9 +84,9 @@ def setUp(self): self.x = np.random.random([10, 16]).astype('float32') self.x = np.array([1, 3]).astype('float32') self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.init_func() @@ -103,8 +103,8 @@ def run_static(self, func): return self.run_dygraph(func, to_static=True) def run_dygraph(self, func, to_static=False): - with fluid.dygraph.guard(self.place): - x_v = fluid.dygraph.to_variable(self.x) + with base.dygraph.guard(self.place): + x_v = base.dygraph.to_variable(self.x) if to_static: ret = paddle.jit.to_static(func)(x_v) else: diff --git a/test/dygraph_to_static/test_len.py b/test/dygraph_to_static/test_len.py index eb72977f16bba..e2cee7c4dc8b4 100644 --- a/test/dygraph_to_static/test_len.py +++ b/test/dygraph_to_static/test_len.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit.dy2static import Call from paddle.nn import clip @@ -27,13 +27,13 @@ def len_with_tensor(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x_len = len(x) return x_len def len_with_lod_tensor_array(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=0) arr = paddle.tensor.array_write(x, i=i) @@ -45,9 +45,9 @@ def len_with_lod_tensor_array(x): class TestLen(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.x_data = np.random.random([10, 16]).astype('float32') self.init_func() @@ -56,13 +56,13 @@ def init_func(self): self.func = len_with_tensor def _run(self, to_static): - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): if to_static: out = paddle.jit.to_static(self.func)(self.x_data) else: out = self.func(self.x_data) - if isinstance(out, fluid.core.eager.Tensor): + if isinstance(out, base.core.eager.Tensor): out = out.numpy() return out @@ -80,14 +80,14 @@ def init_func(self): # Note: Variable(SelectedRows) is not exposed directly in dygraph. # The unittest is used to test coverage by fake transformed code. def len_with_selected_rows(place): - block = fluid.default_main_program().global_block() + block = base.default_main_program().global_block() # create selected_rows variable var = block.create_var( name="X", dtype="float32", shape=[-1], persistable=True, - type=fluid.core.VarDesc.VarType.SELECTED_ROWS, + type=base.core.VarDesc.VarType.SELECTED_ROWS, ) # y is Variable(SelectedRows) y = clip.merge_selected_rows(var) @@ -102,23 +102,23 @@ def len_with_selected_rows(place): row_numel = 2 np_array = np.ones((len(x_rows), row_numel)).astype("float32") - x_var = fluid.global_scope().var("X").get_selected_rows() + x_var = base.global_scope().var("X").get_selected_rows() x_var.set_rows(x_rows) x_var.set_height(20) x_tensor = x_var.get_tensor() x_tensor.set(np_array, place) - exe = fluid.Executor(place=place) - result = exe.run(fluid.default_main_program(), fetch_list=[y_len, z_len]) + exe = base.Executor(place=place) + result = exe.run(base.default_main_program(), fetch_list=[y_len, z_len]) return result class TestLenWithSelectedRows(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) def test_len(self): diff --git a/test/dygraph_to_static/test_list.py b/test/dygraph_to_static/test_list.py index 4a68cab9b63fe..b0febb2b0c9ee 100644 --- a/test/dygraph_to_static/test_list.py +++ b/test/dygraph_to_static/test_list.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base SEED = 2020 np.random.seed(SEED) @@ -27,7 +27,7 @@ # Situation 1: Test list append def test_list_append_without_control_flow(x): # Python list will not be transformed. - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] # It's a plain python control flow which won't be transformed if 2 > 1: @@ -36,7 +36,7 @@ def test_list_append_without_control_flow(x): def test_list_append_in_if(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] if x.numpy()[0] > 0: a.append(x) @@ -49,7 +49,7 @@ def test_list_append_in_if(x): def test_list_append_in_for_loop(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) # Use `fill_constant` so that static analysis can analyze the type of iter_num is Tensor iter_num = paddle.tensor.fill_constant( shape=[1], value=iter_num, dtype="int32" @@ -61,7 +61,7 @@ def test_list_append_in_for_loop(x, iter_num): def test_list_append_in_for_subscript(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.shape(x)[0] a = [] for i in range(iter_num): @@ -72,7 +72,7 @@ def test_list_append_in_for_subscript(x): def test_list_append_in_while_loop_subscript(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.shape(x)[0] a = [] i = 0 @@ -85,7 +85,7 @@ def test_list_append_in_while_loop_subscript(x): def test_list_append_in_for_loop_with_concat(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] # Use `fill_constant` so that static analysis can analyze the type of iter_num is Tensor iter_num = paddle.tensor.fill_constant( @@ -98,7 +98,7 @@ def test_list_append_in_for_loop_with_concat(x, iter_num): def test_list_append_in_while_loop(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.tensor.fill_constant( shape=[1], value=iter_num, dtype="int32" ) @@ -111,7 +111,7 @@ def test_list_append_in_while_loop(x, iter_num): def test_list_append_in_while_loop_with_stack(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.tensor.fill_constant( shape=[1], value=iter_num, dtype="int32" ) @@ -134,7 +134,7 @@ def test_tensor_array_slice(x, iter_num): # Situation 2: Test list pop def test_list_pop_without_control_flow_1(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] if 2 > 1: a.append(x) @@ -143,7 +143,7 @@ def test_list_pop_without_control_flow_1(x): def test_list_pop_without_control_flow_2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] if 2 > 1: a.append(x) @@ -153,7 +153,7 @@ def test_list_pop_without_control_flow_2(x): def test_list_pop_in_if(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) a = [] b = [x * 2 + (x + 1)] if x.numpy()[0] > 0: @@ -169,7 +169,7 @@ def test_list_pop_in_if(x): def test_list_pop_in_for_loop(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) # Use `fill_constant` so that static analysis can analyze the type of iter_num is Tensor iter_num = paddle.tensor.fill_constant( shape=[1], value=iter_num, dtype="int32" @@ -188,7 +188,7 @@ def test_list_pop_in_for_loop(x, iter_num): def test_list_pop_in_while_loop(x, iter_num): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) iter_num = paddle.tensor.fill_constant( shape=[1], value=iter_num, dtype="int32" ) @@ -210,9 +210,9 @@ def test_list_pop_in_while_loop(x, iter_num): class TestListWithoutControlFlow(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.init_data() @@ -242,7 +242,7 @@ def run_dygraph_mode(self): return self.train(to_static=False) def train(self, to_static=False): - with fluid.dygraph.guard(): + with base.dygraph.guard(): if to_static: res = paddle.jit.to_static(self.dygraph_func)(self.input) else: @@ -284,7 +284,7 @@ def init_dygraph_func(self): ] def train(self, to_static=False): - with fluid.dygraph.guard(): + with base.dygraph.guard(): if to_static: # print(paddle.jit.to_static(self.dygraph_func).code) res = paddle.jit.to_static(self.dygraph_func)( diff --git a/test/dygraph_to_static/test_logical.py b/test/dygraph_to_static/test_logical.py index f23a8beb07278..3b00903bc478c 100644 --- a/test/dygraph_to_static/test_logical.py +++ b/test/dygraph_to_static/test_logical.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit.dy2static.logical_transformer import cmpop_node_to_str from paddle.utils import gast @@ -172,7 +172,7 @@ def setUp(self): self.input = np.array([3]).astype('int32') self.place = ( paddle.CUDAPlace(0) - if fluid.is_compiled_with_cuda() + if base.is_compiled_with_cuda() else paddle.CPUPlace() ) self._set_test_func() @@ -184,7 +184,7 @@ def _set_test_func(self): def _run(self, to_static): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): result = self.dygraph_func(self.input) return result.numpy() diff --git a/test/dygraph_to_static/test_loop.py b/test/dygraph_to_static/test_loop.py index e89f9def593b6..77f568e2c5eec 100644 --- a/test/dygraph_to_static/test_loop.py +++ b/test/dygraph_to_static/test_loop.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base from paddle.jit.dy2static.loop_transformer import NameVisitor from paddle.utils import gast @@ -28,7 +28,7 @@ def while_loop_dyfunc(x): - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) while x < 10: i = i + x x = x + 1 @@ -47,7 +47,7 @@ def while_loop_dyfunc_without_tensor(x): def while_loop_dyfun_with_conflict_var(x): - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) def relu(y): # 'y' is not visible outside the scope. @@ -65,12 +65,12 @@ def relu(y): def while_loop_dyfunc_with_none(x): i = ( - fluid.dygraph.to_variable(x) + base.dygraph.to_variable(x) if x is not None - else fluid.dygraph.to_variable(x + 1) + else base.dygraph.to_variable(x + 1) ) # Use `to_variable` so that static analysis can analyze the type of X is Tensor - x = fluid.dygraph.to_variable( + x = base.dygraph.to_variable( x ) # TODO(liym27): Delete it if the type of parameter x can be resolved flag = 1 @@ -133,7 +133,7 @@ def for_break_single_return(max_len): def while_loop_bool_op(x): - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) while x <= -1 or x < -3 or (x < -7 or x < -5) or (x >= 0 and x < 10): i = i + x @@ -142,7 +142,7 @@ def while_loop_bool_op(x): def while_loop_bool_op2(x): - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) a = 1 # In the while condition, there are both Paddle Variable and non-Variable. @@ -161,7 +161,7 @@ def __init__(self): self.c = 5 foo = Foo() - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) while i < 10: foo.b = paddle.zeros(shape=[1], dtype='float32') foo.c = foo.b + foo.a @@ -302,9 +302,9 @@ def test_nested_loop_vars(self): class TestTransformWhileLoop(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.x = np.zeros(shape=(1), dtype=np.int32) self._init_dyfunc() @@ -319,9 +319,9 @@ def _run_dygraph(self): return self._run(to_static=False) def _run(self, to_static): - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): # Set the input of dyfunc to Tensor - tensor_x = fluid.dygraph.to_variable(self.x, zero_copy=False) + tensor_x = base.dygraph.to_variable(self.x, zero_copy=False) if to_static: ret = paddle.jit.to_static(self.dyfunc)(tensor_x) else: @@ -381,9 +381,9 @@ def _init_dyfunc(self): class TestTransformForLoop(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.len = 100 self._init_dyfunc() @@ -398,7 +398,7 @@ def _run_dygraph(self): return self._run(to_static=False) def _run(self, to_static): - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): if to_static: ret = paddle.jit.to_static(self.dyfunc)(self.len) else: diff --git a/test/dygraph_to_static/test_mnist.py b/test/dygraph_to_static/test_mnist.py index f6c178aab1845..5aeb16e87564d 100644 --- a/test/dygraph_to_static/test_mnist.py +++ b/test/dygraph_to_static/test_mnist.py @@ -22,17 +22,17 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import base +from paddle.base.dygraph import to_variable +from paddle.base.dygraph.base import switch_to_static_graph from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import Linear from paddle.optimizer import Adam SEED = 2020 -if paddle.fluid.is_compiled_with_cuda(): - paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if paddle.base.is_compiled_with_cuda(): + paddle.base.set_flags({'FLAGS_cudnn_deterministic': True}) class SimpleImgConvPool(paddle.nn.Layer): @@ -131,9 +131,9 @@ def setUp(self): self.epoch_num = 1 self.batch_size = 64 self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.train_reader = paddle.batch( paddle.dataset.mnist.train(), @@ -174,11 +174,11 @@ def test_mnist_to_static(self): def test_mnist_declarative_cpu_vs_mkldnn(self): dygraph_loss_cpu = self.train_dygraph() - fluid.set_flags({'FLAGS_use_mkldnn': True}) + base.set_flags({'FLAGS_use_mkldnn': True}) try: dygraph_loss_mkldnn = self.train_dygraph() finally: - fluid.set_flags({'FLAGS_use_mkldnn': False}) + base.set_flags({'FLAGS_use_mkldnn': False}) np.testing.assert_allclose( dygraph_loss_cpu, dygraph_loss_mkldnn, @@ -190,9 +190,9 @@ def test_mnist_declarative_cpu_vs_mkldnn(self): def train(self, to_static=False): loss_data = [] - with fluid.dygraph.guard(self.place): - fluid.default_main_program().random_seed = SEED - fluid.default_startup_program().random_seed = SEED + with base.dygraph.guard(self.place): + base.default_main_program().random_seed = SEED + base.default_startup_program().random_seed = SEED mnist = MNIST() if to_static: mnist = paddle.jit.to_static(mnist) @@ -301,7 +301,7 @@ def jit_load_and_run_inference_static( self, model_path, model_filename, params_filename, inputs ): paddle.enable_static() - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) [ inference_program, feed_target_names, diff --git a/test/dygraph_to_static/test_mnist_amp.py b/test/dygraph_to_static/test_mnist_amp.py index 667181adbbe86..e5e11062aad7b 100644 --- a/test/dygraph_to_static/test_mnist_amp.py +++ b/test/dygraph_to_static/test_mnist_amp.py @@ -22,8 +22,8 @@ import paddle from paddle.optimizer import Adam -if paddle.fluid.is_compiled_with_cuda(): - paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if paddle.base.is_compiled_with_cuda(): + paddle.base.set_flags({'FLAGS_cudnn_deterministic': True}) class TestAMP(TestMNIST): diff --git a/test/dygraph_to_static/test_mnist_pure_fp16.py b/test/dygraph_to_static/test_mnist_pure_fp16.py index 2815f47ffa7fe..9d5ae58edbbd7 100644 --- a/test/dygraph_to_static/test_mnist_pure_fp16.py +++ b/test/dygraph_to_static/test_mnist_pure_fp16.py @@ -21,8 +21,8 @@ import paddle -if paddle.fluid.is_compiled_with_cuda(): - paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if paddle.base.is_compiled_with_cuda(): + paddle.base.set_flags({'FLAGS_cudnn_deterministic': True}) class TestPureFP16(TestMNIST): @@ -34,7 +34,7 @@ def train_dygraph(self): @test_and_compare_with_new_ir(False) def test_mnist_to_static(self): - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): dygraph_loss = self.train_dygraph() static_loss = self.train_static() # NOTE: In pure fp16 training, loss is not stable, so we enlarge atol here. diff --git a/test/dygraph_to_static/test_mobile_net.py b/test/dygraph_to_static/test_mobile_net.py index 649f00c3eec0a..607d7644763de 100644 --- a/test/dygraph_to_static/test_mobile_net.py +++ b/test/dygraph_to_static/test_mobile_net.py @@ -23,8 +23,8 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid.param_attr import ParamAttr +from paddle import base +from paddle.base.param_attr import ParamAttr from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm, Linear @@ -32,8 +32,8 @@ # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, # some algorithm results are non-deterministic, like convolution algorithms. -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) SEED = 2020 @@ -496,9 +496,7 @@ class Args: print_step = 1 train_step = 10 place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() ) model_save_dir = None model_save_prefix = None @@ -509,7 +507,7 @@ class Args: def train_mobilenet(args, to_static): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(args.place): + with base.dygraph.guard(args.place): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -601,7 +599,7 @@ def train_mobilenet(args, to_static): def predict_static(args, data): paddle.enable_static() - exe = fluid.Executor(args.place) + exe = base.Executor(args.place) # load inference model [ @@ -625,7 +623,7 @@ def predict_static(args, data): def predict_dygraph(args, data): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(args.place): + with base.dygraph.guard(args.place): if args.model == "MobileNetV1": model = MobileNetV1(class_dim=args.class_dim, scale=1.0) elif args.model == "MobileNetV2": @@ -635,13 +633,13 @@ def predict_dygraph(args, data): model.set_dict(model_dict) model.eval() - pred_res = model(fluid.dygraph.to_variable(data)) + pred_res = model(base.dygraph.to_variable(data)) return pred_res.numpy() def predict_dygraph_jit(args, data): - with fluid.dygraph.guard(args.place): + with base.dygraph.guard(args.place): model = paddle.jit.load(args.model_save_prefix) model.eval() diff --git a/test/dygraph_to_static/test_partial_program.py b/test/dygraph_to_static/test_partial_program.py index 8b646acab64fc..ddb77a04a2628 100644 --- a/test/dygraph_to_static/test_partial_program.py +++ b/test/dygraph_to_static/test_partial_program.py @@ -19,7 +19,7 @@ from test_fetch_feed import Linear import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static SEED = 2020 @@ -50,7 +50,7 @@ def nested_output(x, y): def fake_data(shape): x_data = np.random.random(shape).astype('float32') - return fluid.dygraph.to_variable(x_data) + return base.dygraph.to_variable(x_data) @dy2static_unittest @@ -73,7 +73,7 @@ def fake_input(self): ] def _run(self, to_static): - with fluid.dygraph.guard(): + with base.dygraph.guard(): if self.x is None or self.y is None: self.fake_input() @@ -97,7 +97,7 @@ def setUp(self): self.y = None def _run(self, to_static): - with fluid.dygraph.guard(): + with base.dygraph.guard(): if self.x is None or self.y is None: self.x = fake_data([10, 16]) self.y = fake_data([10, 16]) @@ -119,7 +119,7 @@ def test_nest(self): self.assertTrue(len(dygraph_res) == len(static_res)) for dy_var, st_var in zip(dygraph_res, static_res): - if isinstance(dy_var, fluid.core.eager.Tensor): + if isinstance(dy_var, base.core.eager.Tensor): np.testing.assert_allclose( dy_var.numpy(), st_var.numpy(), rtol=1e-05 ) @@ -131,11 +131,11 @@ def test_nest(self): class TestWithTrainAndEval(unittest.TestCase): @ast_only_test def test_switch_eval_and_train(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_net = Linear() linear_net = paddle.jit.to_static(linear_net) x_data = np.random.random((4, 10)).astype('float32') - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) linear_net(x) _, train_partial_layer = linear_net.forward.program_cache.last()[-1] @@ -164,11 +164,11 @@ def test_switch_eval_and_train(self): class TestWithNoGrad(unittest.TestCase): @ast_only_test def test_with_no_grad(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_net = Linear() linear_net = paddle.jit.to_static(linear_net) x_data = np.random.random((5, 10)).astype('float32') - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) with paddle.no_grad(): linear_net.train() @@ -201,8 +201,8 @@ class TestPruneUnusedParamInProgram(unittest.TestCase): def test_prune(self): input_ids = np.array([[15, 11, 6, 3, 18, 13]]).astype("float32") - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): model = GPT2LMHeadModel() model.eval() input_ids = paddle.to_tensor(input_ids) diff --git a/test/dygraph_to_static/test_partial_program_hook.py b/test/dygraph_to_static/test_partial_program_hook.py index 8910b9b42214a..cb177862692d3 100644 --- a/test/dygraph_to_static/test_partial_program_hook.py +++ b/test/dygraph_to_static/test_partial_program_hook.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core from paddle.jit.dy2static import partial_program, program_translator diff --git a/test/dygraph_to_static/test_print.py b/test/dygraph_to_static/test_print.py index 1f8ab755f8939..fceed1c0e1cad 100644 --- a/test/dygraph_to_static/test_print.py +++ b/test/dygraph_to_static/test_print.py @@ -17,7 +17,7 @@ import numpy import paddle -from paddle import fluid +from paddle import base from paddle.jit import to_static @@ -99,7 +99,7 @@ def set_test_func(self): def _run(self, to_static): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.dygraph_func(self.input) def get_dygraph_output(self): diff --git a/test/dygraph_to_static/test_program_translator.py b/test/dygraph_to_static/test_program_translator.py index 609272619ef17..ac23492bc33b4 100644 --- a/test/dygraph_to_static/test_program_translator.py +++ b/test/dygraph_to_static/test_program_translator.py @@ -26,7 +26,7 @@ import paddle import paddle.jit.dy2static as _jst -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.jit.dy2static.utils import func_to_source_code from paddle.utils import gast @@ -39,8 +39,8 @@ # Because initialized ops will be added into program and be executed many times. # The parameters are assumed to initialized outside of the function. def simple_func(x, weight_numpy): - x = fluid.dygraph.to_variable(x) - w = fluid.dygraph.to_variable(weight_numpy) + x = base.dygraph.to_variable(x) + w = base.dygraph.to_variable(weight_numpy) y = paddle.matmul(x, w) z = paddle.mean(y) return z @@ -48,8 +48,8 @@ def simple_func(x, weight_numpy): @to_static def decorated_simple_func(x, weight_numpy): - x = fluid.dygraph.to_variable(x) - w = fluid.dygraph.to_variable(weight_numpy) + x = base.dygraph.to_variable(x) + w = base.dygraph.to_variable(weight_numpy) y = paddle.matmul(x, w) z = paddle.mean(y) return z @@ -219,19 +219,19 @@ def setUp(self): @ast_only_test def test_raise_error(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.jit.enable_to_static(True) net = NetWithError() with self.assertRaises(ValueError): - net(fluid.dygraph.to_variable(self.x)) + net(base.dygraph.to_variable(self.x)) def test_enable_disable_declarative(self): paddle.jit.enable_to_static(True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): static_output = decorated_simple_func(self.x, self.weight) paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): + with base.dygraph.guard(): dygraph_output = decorated_simple_func(self.x, self.weight) np.testing.assert_allclose( static_output.numpy(), diff --git a/test/dygraph_to_static/test_ptb_lm.py b/test/dygraph_to_static/test_ptb_lm.py index 540586c4ee972..cf00a1475c896 100644 --- a/test/dygraph_to_static/test_ptb_lm.py +++ b/test/dygraph_to_static/test_ptb_lm.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.jit.api import to_static from paddle.optimizer import SGD @@ -48,7 +48,7 @@ def __init__( for i in range(self._num_layers): weight_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -61,7 +61,7 @@ def __init__( ) self.weight_1_arr.append(self.add_parameter('w_%d' % i, weight_1)) bias_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -156,7 +156,7 @@ def __init__( vocab_size, hidden_size, sparse=False, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -164,7 +164,7 @@ def __init__( ), ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -172,7 +172,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -235,7 +235,7 @@ def train(place): vocab_size = 1000 batch_num = 200 - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) ptb_model = PtbModel( @@ -322,9 +322,9 @@ def train_static(place): class TestPtb(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) def test_check_result(self): diff --git a/test/dygraph_to_static/test_reinforcement_learning.py b/test/dygraph_to_static/test_reinforcement_learning.py index e8aa1dbf9bfef..82e75bec891c9 100644 --- a/test/dygraph_to_static/test_reinforcement_learning.py +++ b/test/dygraph_to_static/test_reinforcement_learning.py @@ -21,8 +21,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.jit.api import to_static from paddle.nn import Layer @@ -65,7 +65,7 @@ def train(args, place, to_static): env = gym.make('CartPole-v0') env.seed(SEED) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) local_random = np.random.RandomState(SEED) @@ -204,9 +204,9 @@ def finish_episode(): class TestDeclarative(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.args = Args() diff --git a/test/dygraph_to_static/test_resnet.py b/test/dygraph_to_static/test_resnet.py index 72d0e19518f10..3d4585117c977 100644 --- a/test/dygraph_to_static/test_resnet.py +++ b/test/dygraph_to_static/test_resnet.py @@ -23,8 +23,8 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm @@ -36,13 +36,11 @@ # NOTE: Reduce batch_size from 8 to 2 to avoid unittest timeout. batch_size = 2 epoch_num = 1 -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) def optimizer_setting(parameter_list=None): @@ -189,7 +187,7 @@ def __init__(self, layers=50, class_dim=102): self.out = paddle.nn.Linear( self.pool2d_avg_output, class_dim, - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) @@ -256,7 +254,7 @@ def train(self, to_static, build_strategy=None): """ Tests model decorated by `dygraph_to_static_output` in static graph mode. For users, the model is defined in dygraph mode and trained in static graph mode. """ - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -337,20 +335,20 @@ def train(self, to_static, build_strategy=None): def predict_dygraph(self, data): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): resnet = ResNet() model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams') resnet.set_dict(model_dict) resnet.eval() - pred_res = resnet(fluid.dygraph.to_variable(data)) + pred_res = resnet(base.dygraph.to_variable(data)) return pred_res.numpy() def predict_static(self, data): paddle.enable_static() - exe = fluid.Executor(place) + exe = base.Executor(place) [ inference_program, feed_target_names, @@ -371,7 +369,7 @@ def predict_static(self, data): return pred_res[0] def predict_dygraph_jit(self, data): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): resnet = paddle.jit.load(self.model_save_prefix) resnet.eval() @@ -468,12 +466,12 @@ def test_resnet_composite_forward_backward(self): ) def test_in_static_mode_mkldnn(self): - fluid.set_flags({'FLAGS_use_mkldnn': True}) + base.set_flags({'FLAGS_use_mkldnn': True}) try: - if paddle.fluid.core.is_compiled_with_mkldnn(): + if paddle.base.core.is_compiled_with_mkldnn(): self.resnet_helper.train(to_static=True) finally: - fluid.set_flags({'FLAGS_use_mkldnn': False}) + base.set_flags({'FLAGS_use_mkldnn': False}) if __name__ == '__main__': diff --git a/test/dygraph_to_static/test_resnet_amp.py b/test/dygraph_to_static/test_resnet_amp.py index 96f3b48d342e1..40c60520fbcad 100644 --- a/test/dygraph_to_static/test_resnet_amp.py +++ b/test/dygraph_to_static/test_resnet_amp.py @@ -20,26 +20,24 @@ from test_resnet import SEED, ResNet, optimizer_setting import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core # NOTE: Reduce batch_size from 8 to 2 to avoid unittest timeout. batch_size = 2 epoch_num = 1 -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) def train(to_static, build_strategy=None): """ Tests model decorated by `dygraph_to_static_output` in static graph mode. For users, the model is defined in dygraph mode and trained in static graph mode. """ - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) diff --git a/test/dygraph_to_static/test_resnet_pure_fp16.py b/test/dygraph_to_static/test_resnet_pure_fp16.py index a52aa3a41c79f..c878372fbd406 100644 --- a/test/dygraph_to_static/test_resnet_pure_fp16.py +++ b/test/dygraph_to_static/test_resnet_pure_fp16.py @@ -20,16 +20,16 @@ from test_resnet import SEED, ResNet, optimizer_setting import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core # NOTE: Reduce batch_size from 8 to 2 to avoid unittest timeout. batch_size = 2 epoch_num = 1 -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) def train(to_static, build_strategy=None): @@ -123,7 +123,7 @@ def train(self, to_static): @test_and_compare_with_new_ir(False) def test_resnet(self): - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): static_loss = self.train(to_static=True) dygraph_loss = self.train(to_static=False) # NOTE: In pure fp16 training, loss is not stable, so we enlarge atol here. @@ -138,7 +138,7 @@ def test_resnet(self): ) def test_resnet_composite(self): - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): core._set_prim_backward_enabled(True) static_loss = self.train(to_static=True) core._set_prim_backward_enabled(False) diff --git a/test/dygraph_to_static/test_resnet_v2.py b/test/dygraph_to_static/test_resnet_v2.py index 29e3552b89816..2b4e9676c5f36 100644 --- a/test/dygraph_to_static/test_resnet_v2.py +++ b/test/dygraph_to_static/test_resnet_v2.py @@ -23,7 +23,7 @@ from predictor_utils import PredictorTools import paddle -from paddle.fluid import core +from paddle.base import core SEED = 2020 IMAGENET1000 = 1281167 @@ -39,7 +39,7 @@ if paddle.is_compiled_with_cuda(): - paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': True}) + paddle.base.set_flags({'FLAGS_cudnn_deterministic': True}) def optimizer_setting(parameter_list=None): @@ -133,7 +133,7 @@ def forward(self, inputs): y = paddle.add(x=short, y=conv2) # TODO: uncomment this lines to reproduce the oneDNN segment fault error. - # layer_helper = paddle.fluid.layer_helper.LayerHelper( + # layer_helper = paddle.base.layer_helper.LayerHelper( # self.full_name(), act='relu' # ) # return layer_helper.append_activation(y) @@ -474,12 +474,12 @@ def test_resnet_composite(self): ) def test_in_static_mode_mkldnn(self): - paddle.fluid.set_flags({'FLAGS_use_mkldnn': True}) + paddle.base.set_flags({'FLAGS_use_mkldnn': True}) try: - if paddle.fluid.core.is_compiled_with_mkldnn(): + if paddle.base.core.is_compiled_with_mkldnn(): self.train(to_static=True) finally: - paddle.fluid.set_flags({'FLAGS_use_mkldnn': False}) + paddle.base.set_flags({'FLAGS_use_mkldnn': False}) if __name__ == '__main__': diff --git a/test/dygraph_to_static/test_return.py b/test/dygraph_to_static/test_return.py index d8bf86e208277..41c622e9ed03a 100644 --- a/test/dygraph_to_static/test_return.py +++ b/test/dygraph_to_static/test_return.py @@ -19,8 +19,8 @@ from ifelse_simple_func import dyfunc_with_if_else import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.jit import to_static from paddle.jit.dy2static.utils import Dygraph2StaticException @@ -30,13 +30,13 @@ @to_static def test_return_base(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) return x @to_static def test_inside_func_base(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) def inner_func(x): return x @@ -46,7 +46,7 @@ def inner_func(x): @to_static def test_return_if(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) if x < 0: x -= 1 return -x @@ -56,7 +56,7 @@ def test_return_if(x): @to_static def test_return_if_else(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) if x > 0: x += 10086 return x @@ -69,7 +69,7 @@ def test_return_if_else(x): @to_static def test_return_in_while(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) i = paddle.tensor.fill_constant(shape=[1], dtype='int32', value=0) while i < 10: i += 1 @@ -82,7 +82,7 @@ def test_return_in_while(x): @to_static def test_return_in_for(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) for i in range(10): if i <= 4: x += 1 @@ -94,13 +94,13 @@ def test_return_in_for(x): @to_static def test_recursive_return(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) return dyfunc_with_if_else(x) @to_static def test_return_different_length_if_body(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = x + 1 if x > 0: # x = to_variable(np.ones(1)) so it will return here @@ -111,7 +111,7 @@ def test_return_different_length_if_body(x): @to_static def test_return_different_length_else(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = x + 1 if x < 0: return x, y @@ -122,13 +122,13 @@ def test_return_different_length_else(x): @to_static def test_no_return(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = x + 1 @to_static def test_return_none(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = x + 1 if x > 0: # x = to_variable(np.ones(1)) so it will return here @@ -139,7 +139,7 @@ def test_return_none(x): @to_static def test_return_no_variable(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = x + 1 if x < 0: return x, y @@ -150,14 +150,14 @@ def test_return_no_variable(x): @to_static def test_return_list_one_value(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x += 1 return [x] @to_static def test_return_list_many_values(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x += 1 y = x * 2 z = x * x @@ -166,14 +166,14 @@ def test_return_list_many_values(x): @to_static def test_return_tuple_one_value(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x += 1 return (x,) @to_static def test_return_tuple_many_values(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x += 1 y = x * 2 z = x * x @@ -268,9 +268,9 @@ class TestReturnBase(unittest.TestCase): def setUp(self): self.input = np.ones(1).astype('int32') self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.init_dygraph_func() @@ -279,7 +279,7 @@ def init_dygraph_func(self): def _run(self, to_static=False): paddle.jit.enable_to_static(to_static) - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = self.dygraph_func(self.input) if isinstance(res, (tuple, list)): return tuple(r.numpy() for r in res) diff --git a/test/dygraph_to_static/test_save_inference_model.py b/test/dygraph_to_static/test_save_inference_model.py index b050c201c13ca..8842b18b37a8f 100644 --- a/test/dygraph_to_static/test_save_inference_model.py +++ b/test/dygraph_to_static/test_save_inference_model.py @@ -20,7 +20,7 @@ from dygraph_to_static_util import ast_only_test import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.jit.dy2static.partial_program import partial_program_from from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX @@ -29,9 +29,7 @@ np.random.seed(SEED) -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() class SimpleFcLayer(paddle.nn.Layer): @@ -58,11 +56,11 @@ def tearDown(self): def test_save_inference_model(self): fc_size = 20 x_data = np.random.random((fc_size, fc_size)).astype('float32') - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = SEED - fluid.default_main_program().random_seed = SEED + with base.dygraph.guard(place): + base.default_startup_program().random_seed = SEED + base.default_main_program().random_seed = SEED - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) layer = SimpleFcLayer(fc_size) adam = paddle.optimizer.SGD( learning_rate=0.1, parameters=layer.parameters() @@ -125,7 +123,7 @@ def load_and_run_inference( self, model_path, model_filename, params_filename, inputs ): paddle.enable_static() - exe = fluid.Executor(place) + exe = base.Executor(place) [ inference_program, feed_target_names, @@ -151,9 +149,9 @@ def test_param_type(self): paddle.jit.enable_to_static(True) x_data = np.random.random((20, 20)).astype('float32') - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): net = SimpleFcLayer(20) - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) out = net(x) program_cache = net.forward.program_cache @@ -163,7 +161,7 @@ def test_param_type(self): concrete_program.parameters = params[0] # TypeError: Type of self._params should be list or tuple, - # but received . + # but received . with self.assertRaises(TypeError): partial_program_from(concrete_program) diff --git a/test/dygraph_to_static/test_save_load.py b/test/dygraph_to_static/test_save_load.py index d817f77913fd8..c963b627d8b8f 100644 --- a/test/dygraph_to_static/test_save_load.py +++ b/test/dygraph_to_static/test_save_load.py @@ -22,16 +22,14 @@ import paddle import paddle.nn.functional as F -from paddle import fluid, nn -from paddle.fluid import core +from paddle import base, nn +from paddle.base import core from paddle.nn import BatchNorm from paddle.optimizer import Adam np.random.seed(2020) -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() class PrimeNet(paddle.nn.Layer): @@ -71,9 +69,9 @@ def test_save_load_same_result(self): x_data = np.random.randn(30, 10, 32).astype('float32') batch_num = 3 - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.jit.enable_to_static(True) - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) net = Linear(32, 64) adam = Adam(learning_rate=0.1, parameters=net.parameters()) @@ -92,7 +90,7 @@ def test_save_load_same_result(self): static_out, static_loss = net(x) # load parameters into dygraph - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): dygraph_net = Linear(32, 64) # Load parameters @@ -101,7 +99,7 @@ def test_save_load_same_result(self): # Switch into eval mode. dygraph_net.eval() - x = fluid.dygraph.to_variable(x_data) + x = base.dygraph.to_variable(x_data) # predict output paddle.jit.enable_to_static(False) dygraph_out, dygraph_loss = dygraph_net(x) @@ -115,7 +113,7 @@ def test_save_load_same_result(self): @ast_only_test def test_save_load_prim(self): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): self.x = paddle.randn([4, 2, 6, 6], dtype="float32") self.x.stop_gradient = False net = PrimeNet(data_layout="NCHW") @@ -156,7 +154,7 @@ def test_save_load_prim(self): @ast_only_test def test_save_load_prim_with_hook(self): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): self.x = paddle.randn([4, 2, 6, 6], dtype="float32") self.x.stop_gradient = False net = PrimeNet(data_layout="NCHW") diff --git a/test/dygraph_to_static/test_se_resnet.py b/test/dygraph_to_static/test_se_resnet.py index 34f16e7ae6f53..aef9b3a2f0b6a 100644 --- a/test/dygraph_to_static/test_se_resnet.py +++ b/test/dygraph_to_static/test_se_resnet.py @@ -24,8 +24,8 @@ from predictor_utils import PredictorTools import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm, Linear @@ -38,15 +38,13 @@ PRINT_STEP = 2 STEP_NUM = 10 -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, # some algorithm results are non-deterministic, like convolution algorithms. -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) train_parameters = { "learning_strategy": { @@ -130,7 +128,7 @@ def __init__(self, num_channels, reduction_ratio): self._fc = Linear( num_channels, num_channels // reduction_ratio, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) @@ -138,7 +136,7 @@ def __init__(self, num_channels, reduction_ratio): self._excitation = Linear( num_channels // reduction_ratio, num_channels, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) @@ -315,7 +313,7 @@ def __init__(self, layers=50, class_dim=102): self.out = Linear( self.pool2d_avg_output, class_dim, - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) @@ -377,7 +375,7 @@ def train(self, train_reader, to_static): np.random.seed(SEED) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) se_resnext = SeResNeXt() @@ -473,7 +471,7 @@ def train(self, train_reader, to_static): def predict_dygraph(self, data): paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): se_resnext = SeResNeXt() model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams') @@ -481,15 +479,15 @@ def predict_dygraph(self, data): se_resnext.eval() label = np.random.random([1, 1]).astype("int64") - img = fluid.dygraph.to_variable(data) - label = fluid.dygraph.to_variable(label) + img = base.dygraph.to_variable(data) + label = base.dygraph.to_variable(label) pred_res, _, _, _ = se_resnext(img, label) return pred_res.numpy() def predict_static(self, data): paddle.enable_static() - exe = fluid.Executor(place) + exe = base.Executor(place) [ inference_program, feed_target_names, @@ -510,7 +508,7 @@ def predict_static(self, data): return pred_res[0] def predict_dygraph_jit(self, data): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): se_resnext = paddle.jit.load(self.model_save_prefix) se_resnext.eval() diff --git a/test/dygraph_to_static/test_sentiment.py b/test/dygraph_to_static/test_sentiment.py index 3da23c0849216..f2692671729a8 100644 --- a/test/dygraph_to_static/test_sentiment.py +++ b/test/dygraph_to_static/test_sentiment.py @@ -18,8 +18,8 @@ from test_lac import DynamicGRU import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.jit.api import to_static from paddle.nn import Embedding, Linear @@ -28,8 +28,8 @@ # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, # some algorithm results are non-deterministic, like convolution algorithms. -if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) +if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) class SimpleConvPool(paddle.nn.Layer): @@ -170,7 +170,7 @@ def __init__(self, dict_dim, batch_size, seq_len): self.embedding = Embedding( self.dict_dim + 1, self.emb_dim, - weight_attr=fluid.ParamAttr(learning_rate=30), + weight_attr=base.ParamAttr(learning_rate=30), sparse=False, ) h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32") @@ -218,7 +218,7 @@ def __init__(self, dict_dim, batch_size, seq_len): self.embedding = Embedding( self.dict_dim + 1, self.emb_dim, - weight_attr=fluid.ParamAttr(learning_rate=30), + weight_attr=base.ParamAttr(learning_rate=30), sparse=False, ) h_0 = np.zeros((self.batch_size, self.hid_dim), dtype="float32") @@ -304,12 +304,10 @@ class Args: def train(args, to_static): paddle.jit.enable_to_static(to_static) place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() ) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -317,7 +315,7 @@ def train(args, to_static): train_reader = fake_data_reader( args.class_num, args.vocab_size, args.batch_size, args.padding_size ) - train_loader = fluid.io.DataLoader.from_generator(capacity=24) + train_loader = base.io.DataLoader.from_generator(capacity=24) train_loader.set_sample_list_generator(train_reader) if args.model_type == 'cnn_net': diff --git a/test/dygraph_to_static/test_seq2seq.py b/test/dygraph_to_static/test_seq2seq.py index 3478bf47efed3..fee69b74bfdfe 100644 --- a/test/dygraph_to_static/test_seq2seq.py +++ b/test/dygraph_to_static/test_seq2seq.py @@ -22,12 +22,10 @@ from seq2seq_utils import Seq2SeqModelHyperParams, get_data_iter import paddle -from paddle import fluid +from paddle import base from paddle.nn import ClipGradByGlobalNorm -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() STEP_NUM = 10 PRINT_STEP = 2 @@ -45,9 +43,9 @@ def prepare_input(batch): def train(args, attn_model=False): - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = 2020 - fluid.default_main_program().random_seed = 2020 + with base.dygraph.guard(place): + base.default_startup_program().random_seed = 2020 + base.default_main_program().random_seed = 2020 if attn_model: model = AttentionModel( @@ -87,7 +85,7 @@ def train(args, attn_model=False): batch_start_time = time.time() input_data_feed, word_num = prepare_input(batch) input_data_feed = [ - fluid.dygraph.to_variable(np_inp) for np_inp in input_data_feed + base.dygraph.to_variable(np_inp) for np_inp in input_data_feed ] word_count += word_num loss = model(input_data_feed) @@ -132,7 +130,7 @@ def train(args, attn_model=False): def infer(args, attn_model=False): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): if attn_model: model = AttentionModel( args.hidden_size, @@ -168,7 +166,7 @@ def infer(args, attn_model=False): for batch_id, batch in enumerate(train_data_iter): input_data_feed, word_num = prepare_input(batch) input_data_feed = [ - fluid.dygraph.to_variable(np_inp) for np_inp in input_data_feed + base.dygraph.to_variable(np_inp) for np_inp in input_data_feed ] outputs = model.beam_search(input_data_feed) break diff --git a/test/dygraph_to_static/test_simnet.py b/test/dygraph_to_static/test_simnet.py index 09ea063f9ad8e..7d6cad6d03381 100644 --- a/test/dygraph_to_static/test_simnet.py +++ b/test/dygraph_to_static/test_simnet.py @@ -21,7 +21,7 @@ from simnet_dygraph_model import BOW, HingeLoss import paddle -from paddle import fluid +from paddle import base SEED = 102 random.seed(SEED) @@ -128,12 +128,12 @@ def train(conf_dict, to_static): paddle.jit.enable_to_static(to_static) # Get device - if fluid.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.is_compiled_with_cuda(): + place = base.CUDAPlace(0) else: - place = fluid.CPUPlace() + place = base.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -179,8 +179,8 @@ def train(conf_dict, to_static): class TestSimnet(unittest.TestCase): @test_and_compare_with_new_ir(True) def test_dygraph_static_same_loss(self): - if fluid.is_compiled_with_cuda(): - fluid.set_flags({"FLAGS_cudnn_deterministic": True}) + if base.is_compiled_with_cuda(): + base.set_flags({"FLAGS_cudnn_deterministic": True}) conf_dict = create_conf_dict() dygraph_loss = train(conf_dict, to_static=False) static_loss = train(conf_dict, to_static=True) diff --git a/test/dygraph_to_static/test_simnet_v2.py b/test/dygraph_to_static/test_simnet_v2.py index 316464ab79132..a54cfe14dcbf8 100644 --- a/test/dygraph_to_static/test_simnet_v2.py +++ b/test/dygraph_to_static/test_simnet_v2.py @@ -180,7 +180,7 @@ class TestSimnet(unittest.TestCase): @test_and_compare_with_new_ir(True) def test_dygraph_static_same_loss(self): if paddle.is_compiled_with_cuda(): - paddle.fluid.set_flags({"FLAGS_cudnn_deterministic": True}) + paddle.base.set_flags({"FLAGS_cudnn_deterministic": True}) conf_dict = create_conf_dict() dygraph_loss = train(conf_dict, to_static=False) static_loss = train(conf_dict, to_static=True) diff --git a/test/dygraph_to_static/test_static_analysis.py b/test/dygraph_to_static/test_static_analysis.py index b849bbe47261a..e4e5afb574417 100644 --- a/test/dygraph_to_static/test_static_analysis.py +++ b/test/dygraph_to_static/test_static_analysis.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.jit.dy2static import NodeVarType, StaticAnalysisVisitor from paddle.utils import gast @@ -77,10 +77,10 @@ def func_to_test3(): def func_to_test4(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(0.1, 1, [1, 2]) b = 1 + a - c = fluid.dygraph.to_variable(b) + c = base.dygraph.to_variable(b) d = (c + 1) * 0.3 @@ -123,7 +123,7 @@ def inner_unknown_func(x): def func_to_test6(x, y=1): - i = fluid.dygraph.to_variable(x) + i = base.dygraph.to_variable(x) def add(x, y): return x + y diff --git a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py index 45f3a0a555018..a8e955be9e863 100644 --- a/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py +++ b/test/dygraph_to_static/test_tensor_memcpy_on_cpu.py @@ -48,7 +48,7 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_tensor_cpu_on_default_cpu(self): - paddle.fluid.framework._set_expected_place(paddle.CPUPlace()) + paddle.base.framework._set_expected_place(paddle.CPUPlace()) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -68,7 +68,7 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_tensor_cuda_on_default_cpu(self): - if not paddle.fluid.is_compiled_with_cuda(): + if not paddle.base.is_compiled_with_cuda(): return """ @@ -77,7 +77,7 @@ def test_tensor_cuda_on_default_cpu(self): whether is still taking effect or not. See ConstructDeviceContext() in interpreter_util.cc. """ - paddle.fluid.framework._set_expected_place(paddle.CPUPlace()) + paddle.base.framework._set_expected_place(paddle.CPUPlace()) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -97,10 +97,10 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_with_warning_on_cpu(self): - if not paddle.fluid.is_compiled_with_cuda(): + if not paddle.base.is_compiled_with_cuda(): return - paddle.fluid.framework._set_expected_place(paddle.CPUPlace()) + paddle.base.framework._set_expected_place(paddle.CPUPlace()) x1 = paddle.ones([1, 2, 3]) with self.assertWarns(UserWarning, msg="ignored") as cm: diff --git a/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py b/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py index de642dd4087e8..30e8e55611959 100644 --- a/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py +++ b/test/dygraph_to_static/test_tensor_memcpy_on_gpu.py @@ -49,13 +49,13 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_tensor_cpu_on_default_gpu(self): - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): place = paddle.CUDAPlace( int(os.environ.get('FLAGS_selected_gpus', 0)) ) else: return - paddle.fluid.framework._set_expected_place(place) + paddle.base.framework._set_expected_place(place) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -75,13 +75,13 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_tensor_cuda_on_default_gpu(self): - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): place = paddle.CUDAPlace( int(os.environ.get('FLAGS_selected_gpus', 0)) ) else: return - paddle.fluid.framework._set_expected_place(place) + paddle.base.framework._set_expected_place(place) dygraph_x1_place, dygraph_place, dygraph_res = self._run( to_static=False ) @@ -101,13 +101,13 @@ def _run(self, to_static): return x1.place, x2.place, x2.numpy() def test_with_warning_on_gpu(self): - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): place = paddle.CUDAPlace( int(os.environ.get('FLAGS_selected_gpus', 0)) ) else: return - paddle.fluid.framework._set_expected_place(place) + paddle.base.framework._set_expected_place(place) x1 = paddle.ones([1, 2, 3]) with self.assertWarns(UserWarning, msg="ignored") as cm: diff --git a/test/dygraph_to_static/test_tensor_shape.py b/test/dygraph_to_static/test_tensor_shape.py index 6bf0a0f4016cc..ac4a943296782 100644 --- a/test/dygraph_to_static/test_tensor_shape.py +++ b/test/dygraph_to_static/test_tensor_shape.py @@ -18,11 +18,11 @@ from dygraph_to_static_util import ast_only_test, dy2static_unittest import paddle -from paddle import fluid +from paddle import base def dyfunc_tensor_shape_1(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.reshape(x, shape=x.shape) return res @@ -37,33 +37,33 @@ def dyfunc_tensor_shape_2(x): def dyfunc_tensor_shape_3(x): # Transform y.shape but run y.shape actually because y is not Tensor - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) y = paddle.ones([1, 5]) res = paddle.reshape(x, shape=y.shape) return res def dyfunc_tensor_shape_4(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.reshape(x, shape=(-1, x.shape[0], len(x.shape))) return res def dyfunc_tensor_shape_5(x): - # `res = fluid.layers.reshape(x, shape=(-1, s))` to - # `res = fluid.layers.reshape(x, shape=(-1, + # `res = base.layers.reshape(x, shape=(-1, s))` to + # `res = base.layers.reshape(x, shape=(-1, # paddle.jit.dy2static.convert_var_shape(x)[0]))` - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) s = x.shape[0] res = paddle.reshape(x, shape=(-1, s)) return res def dyfunc_tensor_shape_6(x): - # `res = fluid.layers.reshape(x, shape=(-1, s))` to - # `res = fluid.layers.reshape(x, shape=(-1, + # `res = base.layers.reshape(x, shape=(-1, s))` to + # `res = base.layers.reshape(x, shape=(-1, # paddle.jit.dy2static.convert_var_shape(x)[0:]))` - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) s = x.shape[0:] res = paddle.reshape(x, shape=s) return res @@ -96,14 +96,14 @@ def dyfunc_paddle_shape_api(x): # paddle.shape will not be converted. a = paddle.shape(x)[0] # alias api will also not be converted. - alias_old_api = paddle.fluid.layers + alias_old_api = paddle.base.layers b = paddle.shape(x)[1] res = paddle.reshape(x, shape=(b, a)) return res def dyfunc_with_if_1(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.reshape(x, [-1, 1]) x_shape_0 = x.shape[0] if x_shape_0 < 1: @@ -121,7 +121,7 @@ def dyfunc_with_if_1(x): def dyfunc_with_if_2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) # `len(x.shape)` will not be transformed because x.shape is not used by Paddle api. if len(x.shape) < 1: res = x @@ -132,7 +132,7 @@ def dyfunc_with_if_2(x): def dyfunc_with_for_1(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") # `x.shape[0]` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` for i in range(x.shape[0]): @@ -141,7 +141,7 @@ def dyfunc_with_for_1(x): def dyfunc_with_for_2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x_shape_0 = x.shape[0] res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") @@ -152,7 +152,7 @@ def dyfunc_with_for_2(x): def dyfunc_with_for_3(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") # `len(x.shape)` is not transformed. for i in range(len(x.shape)): @@ -162,7 +162,7 @@ def dyfunc_with_for_3(x): def dyfunc_with_while_1(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") # `x.shape[0]` is transformed into `paddle.jit.dy2static.convert_var_shape(x)[0]` i = 1 @@ -173,7 +173,7 @@ def dyfunc_with_while_1(x): def dyfunc_with_while_2(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x_shape_0 = x.shape[0] res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") i = 1 @@ -185,7 +185,7 @@ def dyfunc_with_while_2(x): def dyfunc_with_while_3(x): - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) x_shape = x.shape res = paddle.tensor.fill_constant(value=0, shape=[1], dtype="int32") i = 1 @@ -236,9 +236,9 @@ class TestTensorShapeBasic(unittest.TestCase): def setUp(self): self.input = np.ones(5).astype("int32") self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self._set_input_spec() self._set_expected_op_num() @@ -251,7 +251,7 @@ def _set_input_spec(self): self.input_spec = [paddle.static.InputSpec(shape=[5], dtype="int32")] def _run(self, to_static): - with fluid.dygraph.guard(): + with base.dygraph.guard(): if to_static: res = paddle.jit.to_static(self.dygraph_func)( self.input diff --git a/test/dygraph_to_static/test_to_tensor.py b/test/dygraph_to_static/test_to_tensor.py index e13cc9cc700a1..bab0ad018b76f 100644 --- a/test/dygraph_to_static/test_to_tensor.py +++ b/test/dygraph_to_static/test_to_tensor.py @@ -22,8 +22,8 @@ ) import paddle -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle.base import core +from paddle.base.framework import Program, program_guard def case0(x): diff --git a/test/dygraph_to_static/test_transformer.py b/test/dygraph_to_static/test_transformer.py index 0942937bb68b7..af1918d982cc0 100644 --- a/test/dygraph_to_static/test_transformer.py +++ b/test/dygraph_to_static/test_transformer.py @@ -27,12 +27,10 @@ ) import paddle -from paddle import fluid +from paddle import base trainer_count = 1 -place = ( - fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() -) +place = base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() SEED = 10 STEP_NUM = 10 @@ -41,11 +39,11 @@ def train_static(args, batch_generator): paddle.enable_static() paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() - with fluid.program_guard(train_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(train_prog, startup_prog): + with base.unique_name.guard(): # define input and reader input_field_names = ( util.encoder_data_input_fields @@ -121,7 +119,7 @@ def train_static(args, batch_generator): step_idx = 0 total_batch_num = 0 avg_loss = [] - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) for pass_id in range(args.epoch): batch_id = 0 @@ -182,7 +180,7 @@ def train_static(args, batch_generator): def train_dygraph(args, batch_generator): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): if SEED is not None: paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -324,7 +322,7 @@ def train_dygraph(args, batch_generator): def predict_dygraph(args, batch_generator): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -420,8 +418,8 @@ def predict_dygraph(args, batch_generator): def predict_static(args, batch_generator): - test_prog = fluid.Program() - with fluid.program_guard(test_prog): + test_prog = base.Program() + with base.program_guard(test_prog): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -483,7 +481,7 @@ def predict_static(args, batch_generator): test_prog = test_prog.clone(for_test=True) # define the executor and program for training - exe = fluid.Executor(place) + exe = base.Executor(place) util.load( test_prog, os.path.join(args.save_static_model_path, "transformer"), exe diff --git a/test/dygraph_to_static/test_tsm.py b/test/dygraph_to_static/test_tsm.py index 3febac5d22799..e68406bd4c9ab 100644 --- a/test/dygraph_to_static/test_tsm.py +++ b/test/dygraph_to_static/test_tsm.py @@ -23,8 +23,8 @@ from tsm_config_utils import merge_configs, parse_config, print_configs import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.jit.api import to_static from paddle.nn import BatchNorm, Linear @@ -43,7 +43,7 @@ def parse_args(): parser.add_argument( '--use_gpu', type=bool, - default=fluid.is_compiled_with_cuda(), + default=base.is_compiled_with_cuda(), help='default use gpu.', ) args = parser.parse_args( @@ -71,15 +71,15 @@ def __init__( stride=stride, padding=(filter_size - 1) // 2, groups=1, - weight_attr=fluid.param_attr.ParamAttr(), + weight_attr=base.param_attr.ParamAttr(), bias_attr=False, ) self._batch_norm = BatchNorm( num_filters, act=act, - param_attr=fluid.param_attr.ParamAttr(), - bias_attr=fluid.param_attr.ParamAttr(), + param_attr=base.param_attr.ParamAttr(), + bias_attr=base.param_attr.ParamAttr(), ) def forward(self, inputs): @@ -301,11 +301,11 @@ def train(args, fake_data_reader, to_static): valid_config = merge_configs(config, 'valid', vars(args)) print_configs(train_config, 'Train') - place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() + place = base.CUDAPlace(0) if args.use_gpu else base.CPUPlace() random.seed(0) np.random.seed(0) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(1000) paddle.framework.random._manual_program_seed(1000) @@ -387,8 +387,8 @@ def train(args, fake_data_reader, to_static): class TestTsm(unittest.TestCase): @test_and_compare_with_new_ir(False) def test_dygraph_static_same_loss(self): - if fluid.is_compiled_with_cuda(): - fluid.set_flags({"FLAGS_cudnn_deterministic": True}) + if base.is_compiled_with_cuda(): + base.set_flags({"FLAGS_cudnn_deterministic": True}) args = parse_args() fake_data_reader = FakeDataReader("train", parse_config(args.config)) dygraph_loss = train(args, fake_data_reader, to_static=False) diff --git a/test/dygraph_to_static/test_typehint.py b/test/dygraph_to_static/test_typehint.py index e349bbce669f1..630c22c981756 100644 --- a/test/dygraph_to_static/test_typehint.py +++ b/test/dygraph_to_static/test_typehint.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base SEED = 2020 np.random.seed(SEED) @@ -35,9 +35,9 @@ def function(x: A) -> A: class TestTransformWhileLoop(unittest.TestCase): def setUp(self): self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.x = np.zeros(shape=(1), dtype=np.int32) self._init_dyfunc() @@ -52,9 +52,9 @@ def _run_dygraph(self): return self._run(to_static=False) def _run(self, to_static): - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): # Set the input of dyfunc to Tensor - tensor_x = fluid.dygraph.to_variable(self.x, zero_copy=False) + tensor_x = base.dygraph.to_variable(self.x, zero_copy=False) if to_static: ret = paddle.jit.to_static(self.dyfunc)(tensor_x) else: diff --git a/test/dygraph_to_static/test_unuseful_inputs.py b/test/dygraph_to_static/test_unuseful_inputs.py index 821afa9be876e..74f3a4acbdd21 100644 --- a/test/dygraph_to_static/test_unuseful_inputs.py +++ b/test/dygraph_to_static/test_unuseful_inputs.py @@ -64,7 +64,7 @@ def forward(self, x): class TestDuplicateOutput(unittest.TestCase): """ TestCase for the transformation from control flow `if/else` - dependent on tensor in Dygraph into Static `fluid.layers.cond`. + dependent on tensor in Dygraph into Static `base.layers.cond`. """ def test_case(self): diff --git a/test/dygraph_to_static/test_word2vec.py b/test/dygraph_to_static/test_word2vec.py index 424d1f3a7ef83..85edea2093d82 100644 --- a/test/dygraph_to_static/test_word2vec.py +++ b/test/dygraph_to_static/test_word2vec.py @@ -20,7 +20,7 @@ from dygraph_to_static_util import test_and_compare_with_new_ir import paddle -from paddle import fluid +from paddle import base from paddle.jit.api import to_static from paddle.nn import Embedding @@ -226,7 +226,7 @@ def __init__(self, name_scope, vocab_size, embedding_size, init_scale=0.1): self.embedding = Embedding( self.vocab_size, self.embedding_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-0.5 / self.embedding_size, @@ -238,7 +238,7 @@ def __init__(self, name_scope, vocab_size, embedding_size, init_scale=0.1): self.embedding_out = Embedding( self.vocab_size, self.embedding_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_out_para', initializer=paddle.nn.initializer.Uniform( low=-0.5 / self.embedding_size, @@ -281,13 +281,11 @@ def train(to_static): np.random.seed(0) place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) if base.is_compiled_with_cuda() else base.CPUPlace() ) - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = 1000 - fluid.default_main_program().random_seed = 1000 + with base.dygraph.guard(place): + base.default_startup_program().random_seed = 1000 + base.default_main_program().random_seed = 1000 skip_gram_model = SkipGram( "skip_gram_model", vocab_size, embedding_size @@ -302,9 +300,9 @@ def train(to_static): for center_words, target_words, label, eval_words in build_batch( dataset, batch_size, epoch_num ): - center_words_var = fluid.dygraph.to_variable(center_words) - target_words_var = fluid.dygraph.to_variable(target_words) - label_var = fluid.dygraph.to_variable(label) + center_words_var = base.dygraph.to_variable(center_words) + target_words_var = base.dygraph.to_variable(target_words) + label_var = base.dygraph.to_variable(label) pred, loss = skip_gram_model( center_words_var, target_words_var, label_var ) diff --git a/test/dygraph_to_static/test_yolov3.py b/test/dygraph_to_static/test_yolov3.py index 4b649107bc303..3f31b666c7f31 100644 --- a/test/dygraph_to_static/test_yolov3.py +++ b/test/dygraph_to_static/test_yolov3.py @@ -21,8 +21,8 @@ from yolov3 import YOLOv3, cfg import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable paddle.enable_static() random.seed(0) @@ -83,10 +83,10 @@ def train(to_static): random.seed(0) np.random.seed(0) - place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace() - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = 1000 - fluid.default_main_program().random_seed = 1000 + place = base.CUDAPlace(0) if cfg.use_gpu else base.CPUPlace() + with base.dygraph.guard(place): + base.default_startup_program().random_seed = 1000 + base.default_main_program().random_seed = 1000 model = YOLOv3(3, is_train=True) boundaries = cfg.lr_steps diff --git a/test/dygraph_to_static/transformer_dygraph_model.py b/test/dygraph_to_static/transformer_dygraph_model.py index d2d89cea6d227..dd31fbd7d6a0f 100644 --- a/test/dygraph_to_static/transformer_dygraph_model.py +++ b/test/dygraph_to_static/transformer_dygraph_model.py @@ -16,8 +16,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.dygraph import to_variable +from paddle import base +from paddle.base.dygraph import to_variable from paddle.jit.api import dygraph_to_static_func from paddle.nn import Layer, Linear @@ -58,10 +58,10 @@ def __init__(self, process_cmd, d_model, dropout_rate): "layer_norm_%d" % len(list(self.children())), paddle.nn.LayerNorm( normalized_shape=d_model, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ), ), @@ -104,25 +104,25 @@ def __init__( in_features=d_model, out_features=d_key * n_head, bias_attr=False, - weight_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=base.ParamAttr(initializer=param_initializer), ) self.k_fc = Linear( in_features=d_model, out_features=d_key * n_head, bias_attr=False, - weight_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=base.ParamAttr(initializer=param_initializer), ) self.v_fc = Linear( in_features=d_model, out_features=d_value * n_head, bias_attr=False, - weight_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=base.ParamAttr(initializer=param_initializer), ) self.proj_fc = Linear( in_features=d_value * n_head, out_features=d_model, bias_attr=False, - weight_attr=fluid.ParamAttr(initializer=param_initializer), + weight_attr=base.ParamAttr(initializer=param_initializer), ) def forward(self, queries, keys, values, attn_bias, cache=None): @@ -289,7 +289,7 @@ def __init__(self, vocab_size, emb_dim, bos_idx=0): self.word_embedder = paddle.nn.Embedding( vocab_size, emb_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal(0.0, emb_dim**-0.5) ), ) @@ -324,7 +324,7 @@ def __init__( self.pos_encoder = paddle.nn.Embedding( max_length, self.emb_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Assign( position_encoding_init(max_length, self.emb_dim) ), @@ -516,7 +516,7 @@ def __init__( self.pos_encoder = paddle.nn.Embedding( max_length, self.emb_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Assign( position_encoding_init(max_length, self.emb_dim) ), diff --git a/test/dygraph_to_static/yolov3.py b/test/dygraph_to_static/yolov3.py index b13a7eb8e16ea..f72ee8bb8f8ff 100644 --- a/test/dygraph_to_static/yolov3.py +++ b/test/dygraph_to_static/yolov3.py @@ -18,8 +18,8 @@ from darknet import ConvBNLayer, DarkNet53_conv_body import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid.param_attr import ParamAttr +from paddle import _legacy_C_ops, base +from paddle.base.param_attr import ParamAttr from paddle.jit.api import to_static from paddle.regularizer import L2Decay @@ -107,11 +107,11 @@ def __setattr__(self, name, value): # derived learning rate the to get the final learning rate. cfg.learning_rate = 0.001 # maximum number of iterations -cfg.max_iter = 20 if fluid.is_compiled_with_cuda() else 1 +cfg.max_iter = 20 if base.is_compiled_with_cuda() else 1 # Disable mixup in last N iter -cfg.no_mixup_iter = 10 if fluid.is_compiled_with_cuda() else 1 +cfg.no_mixup_iter = 10 if base.is_compiled_with_cuda() else 1 # warm up to learning rate -cfg.warm_up_iter = 10 if fluid.is_compiled_with_cuda() else 1 +cfg.warm_up_iter = 10 if base.is_compiled_with_cuda() else 1 cfg.warm_up_factor = 0.0 # lr steps_with_decay cfg.lr_steps = [400000, 450000] @@ -124,7 +124,7 @@ def __setattr__(self, name, value): # ENV options # # support both CPU and GPU -cfg.use_gpu = fluid.is_compiled_with_cuda() +cfg.use_gpu = base.is_compiled_with_cuda() # Class number cfg.class_num = 80 diff --git a/test/fft/test_fft.py b/test/fft/test_fft.py index bcb4199777c1a..a49e030fa6353 100644 --- a/test/fft/test_fft.py +++ b/test/fft/test_fft.py @@ -117,7 +117,7 @@ def decorate(cls): class TestFft(unittest.TestCase): def test_fft(self): """Test fft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.fft(self.x, self.n, self.axis, self.norm), paddle.fft.fft( @@ -156,7 +156,7 @@ def test_fft(self): class TestIfft(unittest.TestCase): def test_fft(self): """Test ifft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ifft(self.x, self.n, self.axis, self.norm), paddle.fft.ifft( @@ -242,7 +242,7 @@ def test_fft(self): class TestFft2(unittest.TestCase): def test_fft2(self): """Test fft2 with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.fft2(self.x, self.n, self.axis, self.norm), paddle.fft.fft2( @@ -314,7 +314,7 @@ def test_fft2(self): - axis type error - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.fft2( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -349,7 +349,7 @@ def test_fft2(self): class TestFftn(unittest.TestCase): def test_fftn(self): """Test fftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.fftn(self.x, self.n, self.axis, self.norm), paddle.fft.fftn( @@ -388,7 +388,7 @@ def test_fftn(self): class TestIFftn(unittest.TestCase): def test_ifftn(self): """Test ifftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ifftn(self.x, self.n, self.axis, self.norm), paddle.fft.ifftn( @@ -452,7 +452,7 @@ def test_ifftn(self): class TestHfft(unittest.TestCase): def test_hfft(self): """Test hfft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.hfft(self.x, self.n, self.axis, self.norm), paddle.fft.hfft( @@ -516,7 +516,7 @@ def test_hfft(self): class TestIrfft(unittest.TestCase): def test_irfft(self): """Test irfft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.irfft(self.x, self.n, self.axis, self.norm), paddle.fft.irfft( @@ -580,7 +580,7 @@ def test_irfft(self): class TestIrfftn(unittest.TestCase): def test_irfftn(self): """Test irfftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.irfftn(self.x, self.n, self.axis, self.norm), paddle.fft.irfftn( @@ -644,7 +644,7 @@ def test_irfftn(self): class TestHfftn(unittest.TestCase): def test_hfftn(self): """Test hfftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.hfftn(self.x, self.n, self.axis, self.norm), paddle.fft.hfftn( @@ -702,7 +702,7 @@ def test_hfftn(self): class TestHfft2(unittest.TestCase): def test_hfft2(self): """Test hfft2 with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.hfft2(self.x, self.s, self.axis, self.norm), paddle.fft.hfft2( @@ -759,7 +759,7 @@ def test_hfft2(self): class TestIrfft2(unittest.TestCase): def test_irfft2(self): """Test irfft2 with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.irfft2(self.x, self.s, self.axis, self.norm), paddle.fft.irfft2( @@ -845,7 +845,7 @@ def test_hfft(self): - axis type error - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.hfft( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -917,7 +917,7 @@ def test_irfft(self): - axis out of range - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.irfft( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1008,7 +1008,7 @@ def test_hfft2(self): - the dimensions of n and axis are different - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.hfft2( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1097,7 +1097,7 @@ def test_irfft2(self): - the dimensions of n and axis are different - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.irfft2( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1187,7 +1187,7 @@ def test_hfftn(self): - the dimensions of n and axis are different - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.hfftn( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1266,7 +1266,7 @@ def test_irfftn(self): - norm out of range - the dimensions of n and axis are different """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.irfftn( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1300,7 +1300,7 @@ def test_irfftn(self): class TestRfft(unittest.TestCase): def test_rfft(self): """Test rfft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.rfft(self.x, self.n, self.axis, self.norm), paddle.fft.rfft( @@ -1380,7 +1380,7 @@ def test_rfft(self): class TestRfft2(unittest.TestCase): def test_rfft2(self): """Test rfft2 with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.rfft2(self.x, self.n, self.axis, self.norm), paddle.fft.rfft2( @@ -1444,7 +1444,7 @@ def test_rfft2(self): - norm out of range - the dimensions of n and axis are different """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.rfft2( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1478,7 +1478,7 @@ def test_rfft2(self): class TestRfftn(unittest.TestCase): def test_rfftn(self): """Test rfftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.rfftn(self.x, self.n, self.axis, self.norm), paddle.fft.rfftn( @@ -1531,7 +1531,7 @@ def test_rfftn(self): - norm out of range - the dimensions of n and axis are different """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.rfftn( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1565,7 +1565,7 @@ def test_rfftn(self): class TestIhfft(unittest.TestCase): def test_ihfft(self): """Test ihfft with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ihfft(self.x, self.n, self.axis, self.norm), paddle.fft.ihfft( @@ -1609,7 +1609,7 @@ def test_ihfft(self): - axis out of range - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.ihfft( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1644,7 +1644,7 @@ def test_ihfft(self): class TestIhfft2(unittest.TestCase): def test_ihfft2(self): """Test ihfft2 with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ihfft2(self.x, self.n, self.axis, self.norm), paddle.fft.ihfft2( @@ -1716,7 +1716,7 @@ def test_ihfft2(self): - axis out of range - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.ihfft2( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1750,7 +1750,7 @@ def test_ihfft2(self): class TestIhfftn(unittest.TestCase): def test_ihfftn(self): """Test ihfftn with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ihfftn(self.x, self.n, self.axis, self.norm), paddle.fft.ihfftn( @@ -1795,7 +1795,7 @@ def test_ihfftn(self): - axis out of range - norm out of range """ - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.ihfftn( paddle.to_tensor(self.x), self.n, self.axis, self.norm @@ -1813,7 +1813,7 @@ def test_ihfftn(self): class TestFftFreq(unittest.TestCase): def test_fftfreq(self): """Test fftfreq with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.fftfreq(self.n, self.d).astype(self.dtype), paddle.fft.fftfreq(self.n, self.d, self.dtype).numpy(), @@ -1834,7 +1834,7 @@ def test_fftfreq(self): class TestFftFreqException(unittest.TestCase): def test_fftfreq2(self): """Test fftfreq with d = 0""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.fftfreq(self.n, self.d, self.dtype) @@ -1850,7 +1850,7 @@ def test_fftfreq2(self): class TestRfftFreq(unittest.TestCase): def test_rfftfreq(self): """Test rfftfreq with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.rfftfreq(self.n, self.d).astype(self.dtype), paddle.fft.rfftfreq(self.n, self.d, self.dtype).numpy(), @@ -1871,7 +1871,7 @@ def test_rfftfreq(self): class TestRfftFreqException(unittest.TestCase): def test_rfftfreq2(self): """Test fftfreq with d = 0""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): with self.assertRaises(self.expect_exception): paddle.fft.rfftfreq(self.n, self.d, self.dtype) @@ -1894,7 +1894,7 @@ def test_rfftfreq2(self): class TestFftShift(unittest.TestCase): def test_fftshift(self): """Test fftshift with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.fftshift(self.x, self.axes), paddle.fft.fftshift( @@ -1923,7 +1923,7 @@ def test_fftshift(self): class TestIfftShift(unittest.TestCase): def test_ifftshift(self): """Test ifftshift with norm condition""" - with paddle.fluid.dygraph.guard(self.place): + with paddle.base.dygraph.guard(self.place): np.testing.assert_allclose( scipy.fft.ifftshift(self.x, self.axes), paddle.fft.ifftshift( diff --git a/test/indexing/test_getitem.py b/test/indexing/test_getitem.py index 6ecd7750ec795..df0801ad4b61d 100644 --- a/test/indexing/test_getitem.py +++ b/test/indexing/test_getitem.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.variable_index import _getitem_static +from paddle.base.variable_index import _getitem_static class TestGetitemInDygraph(unittest.TestCase): diff --git a/test/indexing/test_setitem.py b/test/indexing/test_setitem.py index b5e23ed309de1..b188d84ca2a66 100644 --- a/test/indexing/test_setitem.py +++ b/test/indexing/test_setitem.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.variable_index import _setitem_static +from paddle.base.variable_index import _setitem_static class TestSetitemInDygraph(unittest.TestCase): diff --git a/test/ipu/custom_ops/test_custom_leaky_relu_ipu.py b/test/ipu/custom_ops/test_custom_leaky_relu_ipu.py index edb6b819c5b34..f8fbfc0af4052 100644 --- a/test/ipu/custom_ops/test_custom_leaky_relu_ipu.py +++ b/test/ipu/custom_ops/test_custom_leaky_relu_ipu.py @@ -24,7 +24,7 @@ from paddle.utils.cpp_extension import load sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from op_test_ipu import IPUOpTest, np_dtype_to_fluid_str +from op_test_ipu import IPUOpTest, np_dtype_to_base_str def load_custom_ops(): @@ -60,7 +60,7 @@ def set_feed_attr(self): self.feed_shape = [x.shape for x in self.feed.values()] self.feed_list = list(self.feed.keys()) self.feed_dtype = [ - np_dtype_to_fluid_str(x.dtype) for x in self.feed.values() + np_dtype_to_base_str(x.dtype) for x in self.feed.values() ] def set_attrs(self): diff --git a/test/ipu/distributed/test_dist_pod128_sample.py b/test/ipu/distributed/test_dist_pod128_sample.py index 9b0a33dfd87fb..94e87bab49afe 100644 --- a/test/ipu/distributed/test_dist_pod128_sample.py +++ b/test/ipu/distributed/test_dist_pod128_sample.py @@ -46,7 +46,7 @@ def TestDistTraining(): attrs = {"size": [128, 16], "padding_idx": -1, "dtype": 'float32'} - scope = paddle.fluid.core.Scope() + scope = paddle.base.core.Scope() main_prog = paddle.static.Program() startup_prog = paddle.static.Program() main_prog.random_seed = 42 @@ -55,7 +55,7 @@ def TestDistTraining(): np.random.seed(42) input_data = np.random.uniform(0, 127, size=[128, 3, 2, 1]).astype(np.int32) - with paddle.fluid.scope_guard(scope): + with paddle.base.scope_guard(scope): with paddle.static.program_guard(main_prog, startup_prog): x = paddle.static.data(name="x", shape=[3, 2, 1], dtype='int64') with paddle.static.ipu_shard_guard(index=0, stage=0): diff --git a/test/ipu/distributed/test_dist_sample.py b/test/ipu/distributed/test_dist_sample.py index a5506db7e349f..45c5d0d366b9f 100644 --- a/test/ipu/distributed/test_dist_sample.py +++ b/test/ipu/distributed/test_dist_sample.py @@ -67,13 +67,13 @@ def Test(use_dist, file_name): attrs = {"size": [128, 16], "padding_idx": -1, "dtype": 'float32'} - scope = paddle.fluid.core.Scope() + scope = paddle.base.core.Scope() main_prog = paddle.static.Program() startup_prog = paddle.static.Program() main_prog.random_seed = 42 startup_prog.random_seed = 42 - with paddle.fluid.scope_guard(scope): + with paddle.base.scope_guard(scope): with paddle.static.program_guard(main_prog, startup_prog): x = paddle.static.data(name="x", shape=[3, 2, 1], dtype='int64') diff --git a/test/ipu/op_test_ipu.py b/test/ipu/op_test_ipu.py index 2564f76599c3d..aab7e64b2fc9f 100644 --- a/test/ipu/op_test_ipu.py +++ b/test/ipu/op_test_ipu.py @@ -23,7 +23,7 @@ import paddle import paddle.static -map_np_dtype_to_fluid_dtype = { +map_np_dtype_to_base_dtype = { 'bool': "bool", 'int8': "int8", 'uint8': "uint8", @@ -35,8 +35,8 @@ } -def np_dtype_to_fluid_str(dtype: np.dtype) -> str: - return map_np_dtype_to_fluid_dtype[dtype.name] +def np_dtype_to_base_str(dtype: np.dtype) -> str: + return map_np_dtype_to_base_dtype[dtype.name] class ExecutionModeFull(IntEnum): diff --git a/test/ipu/test_dy2static_fp16_ipu.py b/test/ipu/test_dy2static_fp16_ipu.py index 90e140e08664a..3b37073bb0a73 100644 --- a/test/ipu/test_dy2static_fp16_ipu.py +++ b/test/ipu/test_dy2static_fp16_ipu.py @@ -102,7 +102,7 @@ def _test(self, use_ipu=False): result.append(loss) if use_ipu: - paddle.fluid.core.IpuBackend.get_instance().weights_to_host() + paddle.base.core.IpuBackend.get_instance().weights_to_host() paddle.save(model.state_dict(), model_path) paddle.save(optim.state_dict(), optim_path) diff --git a/test/ipu/test_dy2static_ipu.py b/test/ipu/test_dy2static_ipu.py index 22f755dea5271..b98bdc0351400 100644 --- a/test/ipu/test_dy2static_ipu.py +++ b/test/ipu/test_dy2static_ipu.py @@ -167,7 +167,7 @@ def _test(self, use_ipu=False): result.append(loss) if use_ipu: - paddle.fluid.core.IpuBackend.get_instance().weights_to_host() + paddle.base.core.IpuBackend.get_instance().weights_to_host() paddle.save(model.state_dict(), model_path) paddle.save(optim.state_dict(), optim_path) diff --git a/test/ipu/test_greater_op_ipu.py b/test/ipu/test_greater_op_ipu.py index 68ee9902256c0..d40368d70262e 100644 --- a/test/ipu/test_greater_op_ipu.py +++ b/test/ipu/test_greater_op_ipu.py @@ -28,7 +28,7 @@ def setUp(self): self.set_test_op() def set_test_op(self): - self.op = paddle.fluid.layers.greater_than + self.op = paddle.base.layers.greater_than def set_op_attrs(self): self.attrs = {} @@ -115,22 +115,22 @@ def test_case3(self): class TestLessThan(TestGreaterThan): def set_test_op(self): - self.op = paddle.fluid.layers.less_than + self.op = paddle.base.layers.less_than class TestEqual(TestGreaterThan): def set_test_op(self): - self.op = paddle.fluid.layers.equal + self.op = paddle.base.layers.equal class TestGreaterEqual(TestGreaterThan): def set_test_op(self): - self.op = paddle.fluid.layers.greater_equal + self.op = paddle.base.layers.greater_equal class TestLessEqual(TestGreaterThan): def set_test_op(self): - self.op = paddle.fluid.layers.less_equal + self.op = paddle.base.layers.less_equal if __name__ == "__main__": diff --git a/test/ipu/test_identity_loss_ipu.py b/test/ipu/test_identity_loss_ipu.py index 14f0b4484f765..819e97b64e665 100644 --- a/test/ipu/test_identity_loss_ipu.py +++ b/test/ipu/test_identity_loss_ipu.py @@ -15,13 +15,13 @@ import unittest import numpy as np -from op_test_ipu import IPUOpTest, np_dtype_to_fluid_str +from op_test_ipu import IPUOpTest, np_dtype_to_base_str import paddle import paddle.optimizer import paddle.static -from paddle import fluid -from paddle.fluid import compiler +from paddle import base +from paddle.base import compiler paddle.enable_static() @@ -49,18 +49,18 @@ def set_feed_attr(self): self.feed_shape = [x.shape for x in self.feed.values()] self.feed_list = list(self.feed.keys()) self.feed_dtype = [ - np_dtype_to_fluid_str(x.dtype) for x in self.feed.values() + np_dtype_to_base_str(x.dtype) for x in self.feed.values() ] def _test_base(self, reduction): - scope = fluid.core.Scope() + scope = base.core.Scope() main_prog = paddle.static.Program() startup_prog = paddle.static.Program() SEED = 0 main_prog.random_seed = SEED startup_prog.random_seed = SEED - with fluid.scope_guard(scope): + with base.scope_guard(scope): with paddle.static.program_guard(main_prog, startup_prog): x = paddle.static.data( name=self.feed_list[0], diff --git a/test/ipu/test_inference_model_io_ipu.py b/test/ipu/test_inference_model_io_ipu.py index 418c3da94a1b3..6151928fea282 100644 --- a/test/ipu/test_inference_model_io_ipu.py +++ b/test/ipu/test_inference_model_io_ipu.py @@ -58,12 +58,12 @@ def _test_save(self): startup_prog = paddle.static.Program() main_prog.random_seed = self.SEED startup_prog.random_seed = self.SEED - generator = paddle.fluid.unique_name.UniqueNameGenerator() + generator = paddle.base.unique_name.UniqueNameGenerator() self.full_name = '/'.join( [self.attrs['path'].name, self.attrs['model_name']] ) - with paddle.fluid.unique_name.guard(generator): + with paddle.base.unique_name.guard(generator): with paddle.static.scope_guard(scope): with paddle.static.program_guard(main_prog, startup_prog): x = paddle.static.data( diff --git a/test/ipu/test_one_hot_v2_op_ipu.py b/test/ipu/test_one_hot_v2_op_ipu.py index 9118f756fd4e7..9e9fafb044d96 100644 --- a/test/ipu/test_one_hot_v2_op_ipu.py +++ b/test/ipu/test_one_hot_v2_op_ipu.py @@ -46,7 +46,7 @@ def build_model(self): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], dtype='int32' ) - out = paddle.fluid.input.one_hot(x, **self.attrs) + out = paddle.base.input.one_hot(x, **self.attrs) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/test/ipu/test_save_load_ipu.py b/test/ipu/test_save_load_ipu.py index c11b9fdd27b8e..7012f1cacf33d 100644 --- a/test/ipu/test_save_load_ipu.py +++ b/test/ipu/test_save_load_ipu.py @@ -52,8 +52,8 @@ def set_optimizer(self): @IPUOpTest.static_graph def build_model(self): - generator = paddle.fluid.unique_name.UniqueNameGenerator() - with paddle.fluid.unique_name.guard(generator): + generator = paddle.base.unique_name.UniqueNameGenerator() + with paddle.base.unique_name.guard(generator): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], diff --git a/test/ipu/test_weight_sharing_ipu.py b/test/ipu/test_weight_sharing_ipu.py index 9f114fec99ab6..abe7435a3d938 100644 --- a/test/ipu/test_weight_sharing_ipu.py +++ b/test/ipu/test_weight_sharing_ipu.py @@ -59,12 +59,12 @@ def build_model(self): input=x, size=[768, 768], dtype='float32', - param_attr=paddle.fluid.ParamAttr(name='word_embedding'), + param_attr=paddle.base.ParamAttr(name='word_embedding'), is_sparse=False, ) with paddle.static.ipu_shard_guard(index=1, stage=1): z = paddle.static.nn.fc( - x=y, size=768, weight_attr=paddle.fluid.ParamAttr(name="fc") + x=y, size=768, weight_attr=paddle.base.ParamAttr(name="fc") ) with paddle.static.ipu_shard_guard(index=0, stage=2): out = paddle.matmul( diff --git a/test/ir/inference/auto_scan_test.py b/test/ir/inference/auto_scan_test.py index 4140449cf1ba9..105ea3e0925b7 100755 --- a/test/ir/inference/auto_scan_test.py +++ b/test/ir/inference/auto_scan_test.py @@ -34,7 +34,7 @@ import paddle import paddle.inference as paddle_infer -from paddle.fluid.core import PassVersionChecker +from paddle.base.core import PassVersionChecker from paddle.static.log_helper import get_logger LOGLEVEL = os.environ.get("PADDLE_TEST_LOGLEVEL", "INFO").upper() diff --git a/test/ir/inference/inference_pass_test.py b/test/ir/inference/inference_pass_test.py index 5cbf69b3527be..8d8a484f77cf1 100644 --- a/test/ir/inference/inference_pass_test.py +++ b/test/ir/inference/inference_pass_test.py @@ -21,9 +21,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, create_paddle_predictor class InferencePassTest(unittest.TestCase): @@ -31,8 +31,8 @@ def __init__(self, methodName='runTest'): paddle.enable_static() super().__init__(methodName) paddle.enable_static() - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() self.feeds = None self.fetch_list = None @@ -57,7 +57,7 @@ def _get_place(self): def _save_models( self, dirname, feeded_var_names, target_vars, executor, program, scope ): - with fluid.scope_guard(scope): + with base.scope_guard(scope): # save models as combined but sometimes params is null # To adapt to this situation, the path needs to be adjusted to the old version format. feeded_vars = [] @@ -92,7 +92,7 @@ def _get_paddle_outs(self, executor, program, scope): ''' Return PaddlePaddle outputs. ''' - with fluid.scope_guard(scope): + with base.scope_guard(scope): outs = executor.run( program=program, feed=self.feeds, @@ -114,7 +114,7 @@ def _get_inference_outs(self, config): tensor = predictor.get_input_tensor(name) feed_data = list(self.feeds.values())[i] tensor.copy_from_cpu(np.array(feed_data)) - if type(feed_data) == fluid.LoDTensor: + if type(feed_data) == base.LoDTensor: tensor.set_lod(feed_data.lod()) predictor.zero_copy_run() @@ -201,11 +201,11 @@ def check_output_with_option( or disable TensorRT, enable MKLDNN or disable MKLDNN are all the same. ''' - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - executor = fluid.Executor(place) - scope = fluid.Scope() + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + executor = base.Executor(place) + scope = base.Scope() device = "GPU" if use_gpu else "CPU" - with fluid.scope_guard(scope): + with base.scope_guard(scope): executor.run(self.startup_program) self._save_models( self.path, diff --git a/test/ir/inference/program_config.py b/test/ir/inference/program_config.py index 3c4d82126b59a..4516c2cb4ad0c 100644 --- a/test/ir/inference/program_config.py +++ b/test/ir/inference/program_config.py @@ -18,10 +18,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.executor import global_scope -from paddle.fluid.framework import ( +from paddle import base +from paddle.base import core, framework +from paddle.base.executor import global_scope +from paddle.base.framework import ( IrGraph, IrNode, Operator, @@ -291,7 +291,7 @@ def create_fake_model(program_config): '''Create a Paddle model(in memory) according to the given config.''' paddle.enable_static() main_program_desc = core.ProgramDesc() - util_program = fluid.Program() + util_program = base.Program() main_block_desc = main_program_desc.block(0) var_desc = main_block_desc.var(b"feed") @@ -409,10 +409,10 @@ def create_fake_model(program_config): model = main_program_desc.serialize_to_string() util_program._sync_with_cpp() - place = fluid.CPUPlace() - executor = fluid.Executor(place) - scope = fluid.Scope() - with fluid.scope_guard(scope): + place = base.CPUPlace() + executor = base.Executor(place) + scope = base.Scope() + with base.scope_guard(scope): executor.run(util_program) params = scope.find_var("out_var_0").get_bytes() diff --git a/test/ir/inference/quant_dequant_test.py b/test/ir/inference/quant_dequant_test.py index 32155b8d22d38..4f1e2335f5d49 100644 --- a/test/ir/inference/quant_dequant_test.py +++ b/test/ir/inference/quant_dequant_test.py @@ -21,10 +21,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, Variable, core -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor -from paddle.fluid.framework import IrGraph +from paddle import base +from paddle.base import Program, Variable, core +from paddle.base.core import AnalysisConfig, create_paddle_predictor +from paddle.base.framework import IrGraph from paddle.static.io import append_fetch_ops, prepend_feed_ops from paddle.static.quantization import ( AddQuantDequantPass, @@ -39,10 +39,10 @@ class QuantDequantTest(unittest.TestCase): def __init__(self, methodName='runTest'): super().__init__(methodName) paddle.enable_static() - self.main_program = fluid.Program() - self.startup_program = fluid.Program() - self.test_main_program = fluid.Program() - self.test_startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() + self.test_main_program = base.Program() + self.test_startup_program = base.Program() self.feeds = None self.fetch_list = None self.enable_mkldnn = False @@ -64,7 +64,7 @@ def __init__(self, methodName='runTest'): def _normalize_program(self, program, feed_vars, fetch_vars): if not isinstance(program, Program): raise TypeError( - "program type must be `fluid.Program`, but received `%s`" + "program type must be `base.Program`, but received `%s`" % type(program) ) if not isinstance(feed_vars, list): @@ -127,7 +127,7 @@ def _save_models( if var.name in feeded_var_names: feeded_vars.append(var) - with fluid.scope_guard(scope): + with base.scope_guard(scope): paddle.static.io.save_inference_model( dirname, feeded_vars, @@ -155,7 +155,7 @@ def _get_paddle_outs(self, feed, fetch_list, executor, program, scope): ''' Return PaddlePaddle outputs. ''' - with fluid.scope_guard(scope): + with base.scope_guard(scope): outs = executor.run( program=program, feed=feed, @@ -177,7 +177,7 @@ def _get_inference_outs(self, config): tensor = predictor.get_input_tensor(name) feed_data = list(self.feeds.values())[i] tensor.copy_from_cpu(np.array(feed_data)) - if type(feed_data) == fluid.LoDTensor: + if type(feed_data) == base.LoDTensor: tensor.set_lod(feed_data.lod()) predictor.zero_copy_run() @@ -244,12 +244,12 @@ def check_output_with_option( or disable TensorRT, enable MKLDNN or disable MKLDNN are all the same. ''' - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - executor = fluid.Executor(place) - scope = fluid.Scope() + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + executor = base.Executor(place) + scope = base.Scope() device = "GPU" if use_gpu else "CPU" - with fluid.scope_guard(scope): + with base.scope_guard(scope): executor.run(self.startup_program) executor.run(self.test_startup_program) main_graph = IrGraph(core.Graph(self.main_program.desc), for_test=False) @@ -273,11 +273,11 @@ def check_output_with_option( scale_training_pass = OutScaleForTrainingPass(scope=scope, place=place) scale_training_pass.apply(main_graph) - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.memory_optimize = False build_strategy.enable_inplace = False build_strategy.fuse_all_reduce_ops = False - binary = fluid.CompiledProgram(main_graph.graph) + binary = base.CompiledProgram(main_graph.graph) iters = 10 batch_size = 1 @@ -285,10 +285,8 @@ def check_output_with_option( paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=batch_size, ) - feeder = fluid.DataFeeder( - feed_list=[self.data, self.label], place=place - ) - with fluid.scope_guard(scope): + feeder = base.DataFeeder(feed_list=[self.data, self.label], place=place) + with base.scope_guard(scope): for _ in range(iters): data = next(train_reader()) loss_v = executor.run( @@ -308,7 +306,7 @@ def check_output_with_option( self.main_program = test_graph.to_program() - with fluid.scope_guard(scope): + with base.scope_guard(scope): self.main_program = self._normalize_program( self.main_program, self.data, self.fetch_list ) @@ -455,6 +453,6 @@ def __init__( self.disable_trt_plugin_fp16 = disable_trt_plugin_fp16 def quant_dequant(self): - place = fluid.CPUPlace() - exe = fluid.Executor(place) - scope = fluid.Scope() + place = base.CPUPlace() + exe = base.Executor(place) + scope = base.Scope() diff --git a/test/ir/inference/test_inplace_op_pass.py b/test/ir/inference/test_inplace_op_pass.py index 9885c8a32924d..c001b44e2f513 100644 --- a/test/ir/inference/test_inplace_op_pass.py +++ b/test/ir/inference/test_inplace_op_pass.py @@ -20,7 +20,7 @@ from auto_scan_test import PassAutoScanTest from program_config import OpConfig, ProgramConfig, TensorConfig -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/ir/inference/test_mkldnn_cpu_bfloat16_pass.py b/test/ir/inference/test_mkldnn_cpu_bfloat16_pass.py index df39289b94986..ec6f71cd62436 100644 --- a/test/ir/inference/test_mkldnn_cpu_bfloat16_pass.py +++ b/test/ir/inference/test_mkldnn_cpu_bfloat16_pass.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid.core import PassVersionChecker +from paddle import base +from paddle.base.core import PassVersionChecker class TestMKLDNNCpuBfloat16Pass(InferencePassTest): def setUp(self): self.init_data() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name='x', shape=[-1] + self.shape_x, dtype=self.d_type ) diff --git a/test/ir/inference/test_mkldnn_elt_act_fuse_pass.py b/test/ir/inference/test_mkldnn_elt_act_fuse_pass.py index 582d3efec9d3f..e49aff343cd23 100644 --- a/test/ir/inference/test_mkldnn_elt_act_fuse_pass.py +++ b/test/ir/inference/test_mkldnn_elt_act_fuse_pass.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.core import PassVersionChecker +from paddle import base +from paddle.base.core import PassVersionChecker class ElementwiseActivationOneDNNFusePassTest(InferencePassTest): @@ -30,7 +30,7 @@ class ElementwiseActivationOneDNNFusePassTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data_A = paddle.static.data( name="data_A", shape=[-1, 3, 100, 100], dtype="float32" ) diff --git a/test/ir/inference/test_mkldnn_matmul_op_output_fuse_pass.py b/test/ir/inference/test_mkldnn_matmul_op_output_fuse_pass.py index 2d13b668f319d..8ebc5d0564333 100644 --- a/test/ir/inference/test_mkldnn_matmul_op_output_fuse_pass.py +++ b/test/ir/inference/test_mkldnn_matmul_op_output_fuse_pass.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestMKLDNNMatmulFuseOp(InferencePassTest): @@ -31,7 +31,7 @@ def init_data(self): self.enable_mkldnn = True def make_network(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name='x', shape=[-1] + self.shape_x, dtype=self.d_type ) @@ -73,7 +73,7 @@ def init_data(self): class TestMKLDNNMatmulOpNotFusedWrongTransposeAxis(TestMKLDNNMatmulFuseOp): def make_network(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name='x', shape=[-1] + self.shape_x, dtype=self.d_type ) @@ -96,7 +96,7 @@ def init_data(self): self.enable_mkldnn = True def make_network(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name='x', shape=[-1] + self.shape_x, dtype=self.d_type ) diff --git a/test/ir/inference/test_mkldnn_reshape_transpose_matmul_v2_fuse_pass.py b/test/ir/inference/test_mkldnn_reshape_transpose_matmul_v2_fuse_pass.py index f05e91f04188c..cdfe819ce69af 100644 --- a/test/ir/inference/test_mkldnn_reshape_transpose_matmul_v2_fuse_pass.py +++ b/test/ir/inference/test_mkldnn_reshape_transpose_matmul_v2_fuse_pass.py @@ -18,8 +18,8 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid.core import PassVersionChecker +from paddle import base +from paddle.base.core import PassVersionChecker class TestReshapeTransposeMatmulV2OneDNNFusePass(InferencePassTest): @@ -28,7 +28,7 @@ def setUp(self): self.tranpose_perm = [0, 2, 1, 3] self.pass_name = 'reshape_transpose_matmul_mkldnn_fuse_pass' - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=self.data_shape, dtype="float32" ) diff --git a/test/ir/inference/test_trt_c_allreduce_infer_script.py b/test/ir/inference/test_trt_c_allreduce_infer_script.py index 9985a3fc2ec76..98b7e38e00c2c 100644 --- a/test/ir/inference/test_trt_c_allreduce_infer_script.py +++ b/test/ir/inference/test_trt_c_allreduce_infer_script.py @@ -19,8 +19,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed import fleet -from paddle.fluid import core from paddle.inference import Config, PrecisionType, create_predictor diff --git a/test/ir/inference/test_trt_conv3d_op.py b/test/ir/inference/test_trt_conv3d_op.py index 8251f31d31525..605d13963314f 100644 --- a/test/ir/inference/test_trt_conv3d_op.py +++ b/test/ir/inference/test_trt_conv3d_op.py @@ -18,16 +18,16 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TensorRTSubgraphPassConv3dTest(InferencePassTest): def setUp(self): self.init_params() self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 6, 32, 32], dtype="float32" ) @@ -111,7 +111,7 @@ def set_params(self): class DynamicShapeTensorRTSubgraphPassConv3dTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, -1, -1, -1], dtype="float32" ) diff --git a/test/ir/inference/test_trt_conv3d_transpose_op.py b/test/ir/inference/test_trt_conv3d_transpose_op.py index 4ce5e50e91710..238fcbbbe2b4d 100644 --- a/test/ir/inference/test_trt_conv3d_transpose_op.py +++ b/test/ir/inference/test_trt_conv3d_transpose_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TensorRTSubgraphPassConv3dTransposeTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 4, 4, 32, 32], dtype="float32" ) @@ -93,7 +93,7 @@ def set_params(self): class DynamicShapeTensorRTSubgraphPassConv3dTransposeTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, -1, -1, -1], dtype="float32" ) diff --git a/test/ir/inference/test_trt_conv_pass.py b/test/ir/inference/test_trt_conv_pass.py index 1ba73cbc6576b..2408c59c1b50a 100644 --- a/test/ir/inference/test_trt_conv_pass.py +++ b/test/ir/inference/test_trt_conv_pass.py @@ -19,9 +19,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker os.environ['NVIDIA_TF32_OVERRIDE'] = '0' @@ -29,7 +29,7 @@ class TensorRTSubgraphPassConvTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -107,7 +107,7 @@ def set_params(self): class TensorRTSubgraphPassConvTransposeTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -206,7 +206,7 @@ def set_params(self): class DynamicShapeTensorRTSubgraphPassConvTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, -1, -1], dtype="float32" ) diff --git a/test/ir/inference/test_trt_conv_quant_dequant_pass.py b/test/ir/inference/test_trt_conv_quant_dequant_pass.py index c7f7d03cf3ca6..e3c8e95b24dfe 100644 --- a/test/ir/inference/test_trt_conv_quant_dequant_pass.py +++ b/test/ir/inference/test_trt_conv_quant_dequant_pass.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class QuantDequantTensorRTSubgraphPassConvTest(QuantDequantTest): @@ -69,13 +69,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -179,13 +179,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -287,13 +287,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() diff --git a/test/ir/inference/test_trt_convert_preln_residual_bias.py b/test/ir/inference/test_trt_convert_preln_residual_bias.py index f9431e07511c3..6f9763b58e344 100644 --- a/test/ir/inference/test_trt_convert_preln_residual_bias.py +++ b/test/ir/inference/test_trt_convert_preln_residual_bias.py @@ -166,7 +166,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): attrs = [ program_config.ops[i].attrs for i in range(len(program_config.ops)) ] - # for static_shape, fall back to fluid fused op + # for static_shape, fall back to base fused op clear_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) diff --git a/test/ir/inference/test_trt_convert_preln_residual_no_bias.py b/test/ir/inference/test_trt_convert_preln_residual_no_bias.py index 7be194a953a2e..c9e4162847684 100644 --- a/test/ir/inference/test_trt_convert_preln_residual_no_bias.py +++ b/test/ir/inference/test_trt_convert_preln_residual_no_bias.py @@ -155,7 +155,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): program_config.ops[i].attrs for i in range(len(program_config.ops)) ] - # for static_shape, fall back to fluid fused op + # for static_shape, fall back to base fused op clear_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) diff --git a/test/ir/inference/test_trt_deformable_conv.py b/test/ir/inference/test_trt_deformable_conv.py index c5bc20449e379..73088b3ee959d 100644 --- a/test/ir/inference/test_trt_deformable_conv.py +++ b/test/ir/inference/test_trt_deformable_conv.py @@ -19,9 +19,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker os.environ['NVIDIA_TF32_OVERRIDE'] = '0' @@ -29,7 +29,7 @@ class TRTDeformableConvTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): input = paddle.static.data( name='input', shape=self.input_size, dtype=self.dtype ) diff --git a/test/ir/inference/test_trt_dynamic_shape.py b/test/ir/inference/test_trt_dynamic_shape.py index 0a5ef451b93b8..dfab8a666e55f 100644 --- a/test/ir/inference/test_trt_dynamic_shape.py +++ b/test/ir/inference/test_trt_dynamic_shape.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig class TRTDynamicShapeTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 16, 16], dtype="float32" ) diff --git a/test/ir/inference/test_trt_elementwise_op.py b/test/ir/inference/test_trt_elementwise_op.py index 7f4a34db52fbb..fd351fb87c4ec 100644 --- a/test/ir/inference/test_trt_elementwise_op.py +++ b/test/ir/inference/test_trt_elementwise_op.py @@ -20,15 +20,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TensorRTSubgraphPassElementwiseBroadcastTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 3, 64, 64], dtype="float32" ) diff --git a/test/ir/inference/test_trt_fc_fuse_pass.py b/test/ir/inference/test_trt_fc_fuse_pass.py index b89ffae51bbe5..a3b06a2156030 100644 --- a/test/ir/inference/test_trt_fc_fuse_pass.py +++ b/test/ir/inference/test_trt_fc_fuse_pass.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig class FCFusePassTRTTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 2, 2], dtype="float32" ) @@ -55,7 +55,7 @@ def test_check_output(self): class FCFusePassTRTStaticDims4Cols1Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 32, 8], dtype="float32" ) @@ -83,7 +83,7 @@ def test_check_output(self): class FCFusePassTRTStaticDims4Cols2Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[3, 24, 16, 16], dtype="float32" ) @@ -111,7 +111,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims2Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128], dtype="float32" ) @@ -145,7 +145,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims3Cols1Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 32], dtype="float32" ) @@ -179,7 +179,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims3Cols2Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 32], dtype="float32" ) @@ -213,7 +213,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims4Cols1Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 12, 4, 6], dtype="float32" ) @@ -249,7 +249,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims4Cols2Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 32, 32], dtype="float32" ) @@ -285,7 +285,7 @@ def test_check_output(self): class FCFusePassTRTDynamicDims4Cols3Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128, 32, 32], dtype="float32" ) diff --git a/test/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py b/test/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py index 5e93b651f24fc..078d14fb5fbe7 100644 --- a/test/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py +++ b/test/ir/inference/test_trt_fc_fuse_quant_dequant_pass.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class FCQuantDequantFusePassTRTDims3Cols1Test(QuantDequantTest): @@ -54,13 +54,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -128,13 +128,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -204,13 +204,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() diff --git a/test/ir/inference/test_trt_flatten_op.py b/test/ir/inference/test_trt_flatten_op.py index ad9f37f224dd9..e1489ab969ca5 100644 --- a/test/ir/inference/test_trt_flatten_op.py +++ b/test/ir/inference/test_trt_flatten_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TRTFlattenTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -55,7 +55,7 @@ def test_check_output(self): class TRTFlattenDynamicTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) diff --git a/test/ir/inference/test_trt_gather_nd_op.py b/test/ir/inference/test_trt_gather_nd_op.py index a6389756df915..c78d544b92391 100644 --- a/test/ir/inference/test_trt_gather_nd_op.py +++ b/test/ir/inference/test_trt_gather_nd_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TRTGatherNdTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 4], dtype="float32" ) @@ -65,7 +65,7 @@ def test_check_output(self): class TRTGatherNdFp16Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 1280, 192], dtype="float32" ) diff --git a/test/ir/inference/test_trt_gather_op.py b/test/ir/inference/test_trt_gather_op.py index b432438ebdbd1..96092ff85e358 100644 --- a/test/ir/inference/test_trt_gather_op.py +++ b/test/ir/inference/test_trt_gather_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TRTGatherTest1(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[-1, 128], dtype='float32' ) @@ -69,7 +69,7 @@ def test_check_output(self): class TRTGatherTest2(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[16, 64], dtype='float32' ) diff --git a/test/ir/inference/test_trt_inspector.py b/test/ir/inference/test_trt_inspector.py index a1bb1579891ed..52d02fd1213cb 100644 --- a/test/ir/inference/test_trt_inspector.py +++ b/test/ir/inference/test_trt_inspector.py @@ -20,15 +20,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig class TensorRTInspectorTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[1, 16, 16], dtype="float32" ) diff --git a/test/ir/inference/test_trt_instance_norm_op.py b/test/ir/inference/test_trt_instance_norm_op.py index fdf3523e880ce..5002579438f8d 100644 --- a/test/ir/inference/test_trt_instance_norm_op.py +++ b/test/ir/inference/test_trt_instance_norm_op.py @@ -21,9 +21,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn @@ -42,7 +42,7 @@ def build(self): 1 << 30, self.bs, 2, self.precision, self.serialize, False ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): shape = [-1, self.channel, self.height, self.width] data = paddle.static.data(name='in', shape=shape, dtype='float32') instance_norm_out = nn.instance_norm(data) diff --git a/test/ir/inference/test_trt_matmul.py b/test/ir/inference/test_trt_matmul.py index 87e24a545416c..77baebabb5385 100644 --- a/test/ir/inference/test_trt_matmul.py +++ b/test/ir/inference/test_trt_matmul.py @@ -18,16 +18,16 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TensorRTMatMulDims2Test(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[24, 24], dtype="float32" ) @@ -66,7 +66,7 @@ def test_check_output(self): class TensorRTMatMulTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 24, 24], dtype="float32" ) @@ -126,8 +126,8 @@ def set_params(self): class TensorRTMatMulBroadcastTest(InferencePassTest): def setUp(self): self.set_params() - place = fluid.CPUPlace() - with fluid.program_guard(self.main_program, self.startup_program): + place = base.CPUPlace() + with base.program_guard(self.main_program, self.startup_program): data_x = paddle.static.data( name="data_x", shape=[-1, 6, 24], dtype="float32" ) diff --git a/test/ir/inference/test_trt_matmul_quant_dequant.py b/test/ir/inference/test_trt_matmul_quant_dequant.py index 808df546b6249..03f0e303c4668 100644 --- a/test/ir/inference/test_trt_matmul_quant_dequant.py +++ b/test/ir/inference/test_trt_matmul_quant_dequant.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TensorRTMatMulQuantDequantDims3Test(QuantDequantTest): @@ -63,13 +63,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -175,13 +175,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() @@ -286,13 +286,13 @@ def network(): self.startup_program.random_seed = 2 self.test_main_program.random_seed = 2 # self.test_startup_program.random_seed = 2 - with fluid.unique_name.guard(): - with fluid.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): + with base.program_guard(self.main_program, self.startup_program): self.loss, result = network() opt = paddle.optimizer.Adam(learning_rate=0.0001) opt.minimize(self.loss) - with fluid.unique_name.guard(): - with fluid.program_guard( + with base.unique_name.guard(): + with base.program_guard( self.test_main_program, self.startup_program ): network() diff --git a/test/ir/inference/test_trt_multiclass_nms3_op.py b/test/ir/inference/test_trt_multiclass_nms3_op.py index 9b83863a87ea9..a38a597cf856f 100644 --- a/test/ir/inference/test_trt_multiclass_nms3_op.py +++ b/test/ir/inference/test_trt_multiclass_nms3_op.py @@ -19,10 +19,10 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.static import nn @@ -217,7 +217,7 @@ def setUp(self): ) def build(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): boxes = paddle.static.data( name='bboxes', shape=[-1, self.num_boxes, 4], dtype='float32' ) diff --git a/test/ir/inference/test_trt_nearest_interp_op.py b/test/ir/inference/test_trt_nearest_interp_op.py index 31d4edf85b80e..254bcc818e5ea 100644 --- a/test/ir/inference/test_trt_nearest_interp_op.py +++ b/test/ir/inference/test_trt_nearest_interp_op.py @@ -18,9 +18,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn @@ -28,7 +28,7 @@ class TRTNearestInterpTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): if self.data_layout == 'NCHW': shape = [ -1, diff --git a/test/ir/inference/test_trt_nearest_interp_v2_op.py b/test/ir/inference/test_trt_nearest_interp_v2_op.py index 6749246ebbb15..88353fbe1a58b 100644 --- a/test/ir/inference/test_trt_nearest_interp_v2_op.py +++ b/test/ir/inference/test_trt_nearest_interp_v2_op.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn @@ -29,7 +29,7 @@ class TRTNearestInterpTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): if self.data_layout == 'NCHW': shape = [ -1, diff --git a/test/ir/inference/test_trt_pad_op.py b/test/ir/inference/test_trt_pad_op.py index 71139a039e5c9..aa79ff13b5a40 100644 --- a/test/ir/inference/test_trt_pad_op.py +++ b/test/ir/inference/test_trt_pad_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig from paddle.static import nn class PadOpTRTTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[1, 3, 128, 128], dtype="float32" ) diff --git a/test/ir/inference/test_trt_pool3d_op.py b/test/ir/inference/test_trt_pool3d_op.py index f64ff97e4e8db..66a05775b071d 100644 --- a/test/ir/inference/test_trt_pool3d_op.py +++ b/test/ir/inference/test_trt_pool3d_op.py @@ -21,9 +21,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TensorRTPool3dTest(InferencePassTest): @@ -57,7 +57,7 @@ def build_network(self): 1 << 30, self.bs, 0, self.precision, self.serialize, False ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[-1, self.channel, self.depth, self.height, self.width], @@ -188,7 +188,7 @@ def build_network(self): 1 << 30, self.bs, 0, self.precision, self.serialize, False ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[-1, self.channel, self.depth, self.height, self.width], @@ -289,7 +289,7 @@ def build_network(self): 1 << 30, self.bs, 0, self.precision, self.serialize, False ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[-1, self.channel, self.depth, self.height, self.width], diff --git a/test/ir/inference/test_trt_pool_op.py b/test/ir/inference/test_trt_pool_op.py index 8826a3f06cd01..37ffe6452e0f5 100644 --- a/test/ir/inference/test_trt_pool_op.py +++ b/test/ir/inference/test_trt_pool_op.py @@ -21,9 +21,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn @@ -58,7 +58,7 @@ def build_network(self): 1 << 30, self.bs, 0, self.precision, self.serialize, False ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=[-1, self.channel, self.height, self.width], diff --git a/test/ir/inference/test_trt_reduce_sum_op.py b/test/ir/inference/test_trt_reduce_sum_op.py index f4c456e35fdb0..9380867c38478 100644 --- a/test/ir/inference/test_trt_reduce_sum_op.py +++ b/test/ir/inference/test_trt_reduce_sum_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TRTReduceSumTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 10, 192], dtype="float32" ) @@ -59,7 +59,7 @@ def test_check_output(self): class TRTReduceSumAllTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 10, 192], dtype="float32" ) diff --git a/test/ir/inference/test_trt_reshape_op.py b/test/ir/inference/test_trt_reshape_op.py index 96e8d2189ea91..4e9261ae3d795 100644 --- a/test/ir/inference/test_trt_reshape_op.py +++ b/test/ir/inference/test_trt_reshape_op.py @@ -18,9 +18,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn @@ -35,7 +35,7 @@ def setUp(self): self.input_shape[1], self.input_shape[2], ] - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=self.data_shape, dtype='float32' ) @@ -73,7 +73,7 @@ def setUp(self): self.input_shape[1], self.input_shape[2], ] - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=self.data_shape, dtype='float32' ) @@ -100,7 +100,7 @@ def setUp(self): self.input_shape[1], self.input_shape[2], ] - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=self.data_shape, dtype='float32' ) @@ -127,7 +127,7 @@ def setUp(self): self.input_shape[1], self.input_shape[2], ] - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name='data', shape=self.data_shape, dtype='float32' ) diff --git a/test/ir/inference/test_trt_scale_op.py b/test/ir/inference/test_trt_scale_op.py index c1e7084c300f1..c1d693696b954 100644 --- a/test/ir/inference/test_trt_scale_op.py +++ b/test/ir/inference/test_trt_scale_op.py @@ -18,15 +18,15 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TRTScaleTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 512], dtype="float32" ) @@ -58,7 +58,7 @@ def test_check_output(self): class TRTScaleShape2Test(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 512, 512], dtype="float32" ) diff --git a/test/ir/inference/test_trt_shuffle_channel_detect_pass.py b/test/ir/inference/test_trt_shuffle_channel_detect_pass.py index 8a1388719d8d0..fa91f600e5564 100644 --- a/test/ir/inference/test_trt_shuffle_channel_detect_pass.py +++ b/test/ir/inference/test_trt_shuffle_channel_detect_pass.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class ShuffleChannelFuseTRTPassTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) diff --git a/test/ir/inference/test_trt_skip_layernorm_fuse_pass.py b/test/ir/inference/test_trt_skip_layernorm_fuse_pass.py index 1bf140a365aae..4d671c33cdfb8 100644 --- a/test/ir/inference/test_trt_skip_layernorm_fuse_pass.py +++ b/test/ir/inference/test_trt_skip_layernorm_fuse_pass.py @@ -20,14 +20,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class SkipLayernormFusePassTest0(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 3, 128, 128], dtype="float32" ) @@ -73,7 +73,7 @@ def test_check_output(self): class SkipLayernormFusePassTest1(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 256, 1536], dtype="float32" ) @@ -121,7 +121,7 @@ def test_check_output(self): class SkipLayernormFusePassTest2(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 128, 64, 768], dtype="float32" ) @@ -169,7 +169,7 @@ def test_check_output(self): class SkipLayernormFusePassTest3(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 128, 128], dtype="float32" ) diff --git a/test/ir/inference/test_trt_slice_dynamic_plugin.py b/test/ir/inference/test_trt_slice_dynamic_plugin.py index 5a155194ef45c..7712d00041a8a 100644 --- a/test/ir/inference/test_trt_slice_dynamic_plugin.py +++ b/test/ir/inference/test_trt_slice_dynamic_plugin.py @@ -18,9 +18,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig # normal starts && ends @@ -45,7 +45,7 @@ def setUpTensorRTParams(self): def setUp(self): self.setUpSliceParams() self.setUpTensorRTParams() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[3, 3, 3, 3], dtype="float32" ) diff --git a/test/ir/inference/test_trt_slice_plugin.py b/test/ir/inference/test_trt_slice_plugin.py index 5596729a93aee..ab8e9cab62bf6 100644 --- a/test/ir/inference/test_trt_slice_plugin.py +++ b/test/ir/inference/test_trt_slice_plugin.py @@ -18,9 +18,9 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig from paddle.static import nn @@ -40,7 +40,7 @@ def setUpTensorRTParams(self): def setUp(self): self.setUpSliceParams() self.setUpTensorRTParams() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[3, 3, 3, 3], dtype="float32" ) @@ -111,7 +111,7 @@ class SlicePluginTRTTestInt32(SlicePluginTRTTest): def setUp(self): self.setUpSliceParams() self.setUpTensorRTParams() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[3, 3, 3, 3], dtype="int32" ) @@ -138,7 +138,7 @@ def setUpTensorRTParams(self): def setUp(self): self.setUpSliceParams() self.setUpTensorRTParams() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[3, 3, 3, 3], dtype="int32" ) diff --git a/test/ir/inference/test_trt_subgraph_pass.py b/test/ir/inference/test_trt_subgraph_pass.py index 4031a882758b9..b343eaf89322d 100644 --- a/test/ir/inference/test_trt_subgraph_pass.py +++ b/test/ir/inference/test_trt_subgraph_pass.py @@ -20,14 +20,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base.core import AnalysisConfig, PassVersionChecker from paddle.static import nn class TensorRTSubgraphPassFcTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -54,7 +54,7 @@ def test_check_output(self): class TensorRTSubgraphPassConcatTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -84,7 +84,7 @@ def test_check_output(self): class TensorRTSubgraphPassSplitTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -110,7 +110,7 @@ def test_check_output(self): class TensorRTSubgraphPassSplitSerializeTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -139,7 +139,7 @@ def test_check_output(self): class TensorRTSubgraphPassDynamicSplitFp16SerializeTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -176,15 +176,15 @@ def test_check_output(self): class TensorRTSubgraphPassInstanceNormTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( name='instance_norm_w', initializer=paddle.nn.initializer.Constant(value=1.0), ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( name='instance_norm_b', initializer=paddle.nn.initializer.Constant(value=0.0), ) @@ -213,7 +213,7 @@ def test_check_output(self): class TensorRTSubgraphPassTransposeTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -243,7 +243,7 @@ def test_check_output(self): class TensorRTSubgraphPassLayerNormTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -274,7 +274,7 @@ def test_check_output(self): class TensorRTSubgraphPassLayerNormDynamicTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -362,7 +362,7 @@ def set_params(self): class TensorRTSubgraphPassElementwiseTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 3, 64, 64], dtype="float32" ) @@ -418,7 +418,7 @@ def test_check_output(self): class TensorRTSubgraphPassElementwiseBroadcastDynamicTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[-1, 3, 64, 64], dtype="float32" ) diff --git a/test/ir/inference/test_trt_tile_op.py b/test/ir/inference/test_trt_tile_op.py index 014e541a4f126..94e3d67265e1a 100644 --- a/test/ir/inference/test_trt_tile_op.py +++ b/test/ir/inference/test_trt_tile_op.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig, PassVersionChecker class TRTTileTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[4, 3, 224, 256], dtype="float32" ) @@ -52,7 +52,7 @@ def test_check_output(self): class TRTTileExpandTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[1, 1, 1, 1], dtype="float32" ) @@ -79,7 +79,7 @@ def test_check_output(self): class TRTTileExpandStaticTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[1, 1, 1, 1], dtype="float32" ) @@ -106,7 +106,7 @@ def test_check_output(self): class TRTTileExpandHalfTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[1, 1, 1, 1], dtype="float32" ) diff --git a/test/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py b/test/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py index ce29fe4762d81..b0886a99c7d12 100644 --- a/test/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py +++ b/test/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.core import AnalysisConfig +from paddle import base +from paddle.base import core +from paddle.base.core import AnalysisConfig class TransposeFlattenConcatFusePassTRTTest(InferencePassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data1 = paddle.static.data( name="data1", shape=[8, 32, 128], dtype="float32" ) diff --git a/test/ir/inference/test_trt_tuned_dynamic_shape.py b/test/ir/inference/test_trt_tuned_dynamic_shape.py index 12e3e76dc3fd6..1c69f3f1db735 100644 --- a/test/ir/inference/test_trt_tuned_dynamic_shape.py +++ b/test/ir/inference/test_trt_tuned_dynamic_shape.py @@ -19,18 +19,18 @@ import paddle paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.inference import Config, create_predictor class TRTTunedDynamicShapeTest(unittest.TestCase): def get_model(self): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) diff --git a/test/ir/inference/test_trt_yolo_box_op.py b/test/ir/inference/test_trt_yolo_box_op.py index d625f65b4ece1..5856a4a6055cc 100644 --- a/test/ir/inference/test_trt_yolo_box_op.py +++ b/test/ir/inference/test_trt_yolo_box_op.py @@ -18,14 +18,14 @@ from inference_pass_test import InferencePassTest import paddle -from paddle import fluid -from paddle.fluid.core import AnalysisConfig, PassVersionChecker +from paddle import base +from paddle.base.core import AnalysisConfig, PassVersionChecker class TRTYoloBoxTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): image_shape = [self.bs, self.channel, self.height, self.width] image = paddle.static.data( name='image', shape=image_shape, dtype='float32' @@ -79,7 +79,7 @@ def test_check_output(self): class TRTYoloBoxFP16Test(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): image_shape = [self.bs, self.channel, self.height, self.width] image = paddle.static.data( name='image', shape=image_shape, dtype='float32' @@ -131,7 +131,7 @@ def test_check_output(self): class TRTYoloBoxIoUAwareTest(InferencePassTest): def setUp(self): self.set_params() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): image_shape = [self.bs, self.channel, self.height, self.width] image = paddle.static.data( name='image', shape=image_shape, dtype='float32' diff --git a/test/ir/inference/test_yolo_box_post.py b/test/ir/inference/test_yolo_box_post.py index 28fe1043cdefb..8985f2d48d3ba 100644 --- a/test/ir/inference/test_yolo_box_post.py +++ b/test/ir/inference/test_yolo_box_post.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper paddle.enable_static() diff --git a/test/ir/new_ir/test_data_op.py b/test/ir/new_ir/test_data_op.py index 90e40e3635d7c..a7659e32486c4 100644 --- a/test/ir/new_ir/test_data_op.py +++ b/test/ir/new_ir/test_data_op.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper def data(): diff --git a/test/ir/new_ir/test_ir_pybind.py b/test/ir/new_ir/test_ir_pybind.py index 26844acc958f8..63e9703cedf9f 100644 --- a/test/ir/new_ir/test_ir_pybind.py +++ b/test/ir/new_ir/test_ir_pybind.py @@ -77,7 +77,7 @@ def test_value(self): tanh_op = newir_program.block().ops[3] self.assertEqual( - matmul_op.result(0).dtype, paddle.fluid.core.DataType.FLOAT32 + matmul_op.result(0).dtype, paddle.base.core.DataType.FLOAT32 ) self.assertEqual(matmul_op.result(0).shape, [4, 4]) self.assertEqual( @@ -161,8 +161,8 @@ def test_attr(self): self.assertEqual(conv_attr["paddings"], [0, 0]) self.assertEqual(conv_attr["padding_algorithm"], "EXPLICIT") self.assertEqual(conv_attr["groups"], 1) - self.assertEqual(full_attr["dtype"], paddle.fluid.core.DataType.FLOAT32) - self.assertTrue(isinstance(full_attr["place"], paddle.fluid.core.Place)) + self.assertEqual(full_attr["dtype"], paddle.base.core.DataType.FLOAT32) + self.assertTrue(isinstance(full_attr["place"], paddle.base.core.Place)) def test_operands(self): newir_program = get_ir_program() diff --git a/test/ir/new_ir/test_ir_vjp.py b/test/ir/new_ir/test_ir_vjp.py index 595715aa5f22f..c770153738d2b 100644 --- a/test/ir/new_ir/test_ir_vjp.py +++ b/test/ir/new_ir/test_ir_vjp.py @@ -16,7 +16,7 @@ import paddle from paddle import ir -from paddle.fluid.core import call_vjp, has_vjp +from paddle.base.core import call_vjp, has_vjp paddle.enable_static() diff --git a/test/ir/new_ir/test_pass_manager.py b/test/ir/new_ir/test_pass_manager.py index 761baaea13be8..81184eb08e8a8 100644 --- a/test/ir/new_ir/test_pass_manager.py +++ b/test/ir/new_ir/test_pass_manager.py @@ -16,7 +16,7 @@ import paddle from paddle import ir -from paddle.fluid import core +from paddle.base import core from paddle.framework import LayerHelper paddle.enable_static() diff --git a/test/ir/new_ir/test_special_op_translator.py b/test/ir/new_ir/test_special_op_translator.py index 5763ad03e5948..24eadccf034bd 100644 --- a/test/ir/new_ir/test_special_op_translator.py +++ b/test/ir/new_ir/test_special_op_translator.py @@ -18,7 +18,7 @@ import paddle from paddle import ir -from paddle.fluid import core +from paddle.base import core from paddle.framework import LayerHelper paddle.enable_static() diff --git a/test/ir/pass_test.py b/test/ir/pass_test.py index 176bd46616d09..2d809e2f5e9bb 100644 --- a/test/ir/pass_test.py +++ b/test/ir/pass_test.py @@ -19,16 +19,16 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import Block +from paddle import base +from paddle.base import core +from paddle.base.framework import Block class PassTest(unittest.TestCase): @classmethod def setUpClass(self): - self.main_program = fluid.Program() - self.startup_program = fluid.Program() + self.main_program = base.Program() + self.startup_program = base.Program() self.feeds = None self.fetch_list = None self.pass_names = None @@ -41,9 +41,9 @@ def setUpClass(self): random.seed(124) def _get_places(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) return places def grad(self, var): @@ -51,9 +51,9 @@ def grad(self, var): return self.main_program.global_block().var(grad_name) def append_gradients(self, outs): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): loss = paddle.mean(outs) - fluid.backward.append_backward(loss) + base.backward.append_backward(loss) def check_output(self, startup_on_cpu=False, atol=1e-5): ''' @@ -84,7 +84,7 @@ def _run_program(self, executor, program): def _apply_ir_passes(self): graph = core.Graph(self.main_program.desc) - graph.set_not_owned("__param_scope__", fluid.global_scope()) + graph.set_not_owned("__param_scope__", base.global_scope()) for attr_name, attr_value in self.graph_attrs.items(): graph.set(attr_name, attr_value) @@ -101,7 +101,7 @@ def _apply_ir_passes(self): ir_pass.set(key, attrs[key]) trans_pass = pass_builder.append_pass("graph_to_program_pass") - opt_program = fluid.Program() + opt_program = base.Program() trans_pass.set_not_owned("program", opt_program.desc) for p in pass_builder.all_passes(): p.apply(graph) @@ -120,10 +120,10 @@ def check_output_with_place(self, place, startup_on_cpu=False, atol=1e-5): after apply all specified passes, then copy the parameters to GPUPlace. We can set startup_on_cpu to True to test inference pass. ''' - executor = fluid.Executor(place) + executor = base.Executor(place) if startup_on_cpu: # Initialize parameters on CPU - cpu_executor = fluid.Executor(fluid.CPUPlace()) + cpu_executor = base.Executor(base.CPUPlace()) cpu_executor.run(self.startup_program) outs, lods = self._run_program(cpu_executor, self.main_program) else: @@ -140,7 +140,7 @@ def check_output_with_place(self, place, startup_on_cpu=False, atol=1e-5): opt_program = self._apply_ir_passes() self.check_program(opt_program) - if startup_on_cpu and not isinstance(place, fluid.CPUPlace): + if startup_on_cpu and not isinstance(place, base.CPUPlace): warnings.warn( "Parameters are on CPU, and will be transferred to GPU " "automatically by data transform." diff --git a/test/ir/test_fuse_resnet_unit.py b/test/ir/test_fuse_resnet_unit.py index d76a806c0c8f5..f9599386f8bfc 100644 --- a/test/ir/test_fuse_resnet_unit.py +++ b/test/ir/test_fuse_resnet_unit.py @@ -18,7 +18,7 @@ import paddle import paddle.incubate -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(0) @@ -49,7 +49,7 @@ def test_fuse_resenet_unit(self): out = batch_norm(conv2d(x)) graph = core.Graph(program.desc) core.get_pass("fuse_resnet_unit").apply(graph) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() params = paddle.static.amp.cast_model_to_fp16(program) after_params = paddle.static.amp.cast_model_to_fp16(after_program) exe = paddle.static.Executor(place) diff --git a/test/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py b/test/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py index dbdcdffdf5be1..4c0b5d5689885 100644 --- a/test/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py +++ b/test/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py @@ -18,13 +18,13 @@ from pass_test import PassTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class EmbEltwiseLayerNormFusePassTest(PassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): word_id = paddle.static.data( name="word_id", shape=[1, 128], @@ -127,7 +127,7 @@ def test_check_output(self): self.pass_attrs = { "embedding_eltwise_layernorm_fuse_pass": {"use_gpu": True} } - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) self.check_output_with_place(place, startup_on_cpu=True) diff --git a/test/ir/test_ir_fc_fuse_pass.py b/test/ir/test_ir_fc_fuse_pass.py index 5ad3692db9a03..78fa2c29f33bf 100644 --- a/test/ir/test_ir_fc_fuse_pass.py +++ b/test/ir/test_ir_fc_fuse_pass.py @@ -18,13 +18,13 @@ from pass_test import PassTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class FCFusePassTest(PassTest): def setUp(self): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): data = paddle.static.data( name="data", shape=[32, 128], dtype="float32", lod_level=0 ) @@ -46,7 +46,7 @@ def test_check_output(self): use_gpu_set.append(True) for use_gpu in use_gpu_set: self.pass_attrs = {"fc_fuse_pass": {"use_gpu": use_gpu}} - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() self.check_output_with_place(place, startup_on_cpu=True) diff --git a/test/ir/test_ir_fusion_group_pass.py b/test/ir/test_ir_fusion_group_pass.py index 67f7e797db5b6..91a9dafa77744 100644 --- a/test/ir/test_ir_fusion_group_pass.py +++ b/test/ir/test_ir_fusion_group_pass.py @@ -18,13 +18,13 @@ from pass_test import PassTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class FusionGroupPassTest(PassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([32, 128], dtype, 2) self.feed_vars.append( paddle.static.data(name="data2", shape=[128, 128], dtype=dtype) @@ -61,15 +61,15 @@ def _prepare_feed_vars(self, shape, dtype, num_data, stop_gradient=True): def _feed_random_data(self, feed_vars): feeds = {} for var in feed_vars: - if var.type != fluid.core.VarDesc.VarType.LOD_TENSOR: + if var.type != base.core.VarDesc.VarType.LOD_TENSOR: raise TypeError("Feed data of non LoDTensor is not supported.") shape = var.shape - if var.dtype == fluid.core.VarDesc.VarType.FP32: + if var.dtype == base.core.VarDesc.VarType.FP32: dtype = "float32" - elif var.dtype == fluid.core.VarDesc.VarType.FP64: + elif var.dtype == base.core.VarDesc.VarType.FP64: dtype = "float64" - elif var.dtype == fluid.core.VarDesc.VarType.FP16: + elif var.dtype == base.core.VarDesc.VarType.FP16: dtype = "float16" else: raise ValueError("Unsupported dtype %s" % var.dtype) @@ -79,12 +79,12 @@ def _feed_random_data(self, feed_vars): def test_check_output(self): if core.is_compiled_with_cuda(): self.pass_attrs = {"fusion_group_pass": {"use_gpu": True}} - self.check_output_with_place(fluid.CUDAPlace(0)) + self.check_output_with_place(base.CUDAPlace(0)) class FusionGroupPassComplicatedTest(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([32, 64], dtype, 5, False) one = paddle.tensor.fill_constant(shape=[1], dtype=dtype, value=1.0) @@ -107,7 +107,7 @@ def build_program(self, dtype): class FusionGroupPassInplaceTest(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([32, 128], dtype, 3) self.feed_vars.append( paddle.static.data(name="data3", shape=[128, 32], dtype=dtype) @@ -133,7 +133,7 @@ def setUp(self): class FusionGroupPassTestCastAndFP16(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([32, 128], dtype, 2) self.feed_vars.append( paddle.static.data(name="data2", shape=[128, 128], dtype=dtype) @@ -164,7 +164,7 @@ def build_program(self, dtype): class FusionGroupPassSumTest(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([32, 128], dtype, 3) self.feed_vars.append( paddle.static.data(name="data3", shape=[128, 128], dtype=dtype) @@ -188,7 +188,7 @@ def build_program(self, dtype): class FusionGroupPassCastTest(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([2, 2], dtype, 2) tmp_0 = paddle.add(self.feed_vars[0], self.feed_vars[1]) @@ -210,7 +210,7 @@ def setUp(self): class FusionGroupPassFillConstantTest(FusionGroupPassTest): def build_program(self, dtype): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): self.feed_vars = self._prepare_feed_vars([2, 2], dtype, 2) tmp_0 = paddle.add(self.feed_vars[0], self.feed_vars[1]) diff --git a/test/ir/test_ir_generate_pass.py b/test/ir/test_ir_generate_pass.py index a4b57f9bba063..062569c5211aa 100644 --- a/test/ir/test_ir_generate_pass.py +++ b/test/ir/test_ir_generate_pass.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.passes import ir from paddle.static import InputSpec @@ -274,7 +274,7 @@ def check_multi_add_to_sum(self, pass_type): core.get_pass(pass_type).apply(graph) after_node_nums = len(graph.nodes()) self.assertEqual(after_node_nums, before_node_nums - 2) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() executor = paddle.static.Executor(paddle.CPUPlace()) executor.run(startup_program) feed = { @@ -309,7 +309,7 @@ def test_generate_combine_mul_v1(self): core.get_pass("generate_combine_mul_v1").apply(graph) after_node_nums = len(graph.nodes()) self.assertEqual(after_node_nums, before_node_nums + 4) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() executor = paddle.static.Executor(paddle.CPUPlace()) executor.run(startup_program) feed = { @@ -357,7 +357,7 @@ def check_generate_simplify_inference(self, pass_type): core.get_pass(pass_type).apply(graph) after_node_nums = len(graph.nodes()) self.assertEqual(after_node_nums, before_node_nums - 6) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() executor = paddle.static.Executor(paddle.CPUPlace()) executor.run(startup_program) feed = {"x": np.random.random([10, 16, 16]).astype("float32")} @@ -393,7 +393,7 @@ def test_generate_layer_norm_fuse_pass(self): core.get_pass("generate_layer_norm_fuse_pass").apply(graph) after_node_nums = len(graph.nodes()) self.assertEqual(after_node_nums, before_node_nums - 14) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() executor = paddle.static.Executor(paddle.CPUPlace()) executor.run(startup_program) feed = {"x": np.random.random([3, 64, 120]).astype("float32")} diff --git a/test/ir/test_ir_graph_to_program_pass.py b/test/ir/test_ir_graph_to_program_pass.py index d98768e901c17..22af43f7f9a01 100644 --- a/test/ir/test_ir_graph_to_program_pass.py +++ b/test/ir/test_ir_graph_to_program_pass.py @@ -15,14 +15,14 @@ import unittest import paddle -from paddle import fluid, static +from paddle import base, static paddle.enable_static() def program_to_IRGraph(program): - graph = fluid.core.Graph(program.desc) - ir_graph = fluid.framework.IrGraph(graph, for_test=False) + graph = base.core.Graph(program.desc) + ir_graph = base.framework.IrGraph(graph, for_test=False) return ir_graph @@ -152,7 +152,7 @@ def multiblock_model(): b = static.data(name='b', shape=[10, 1], dtype='int64') cond = paddle.greater_than(a, b) - ie = fluid.layers.IfElse(cond) + ie = base.layers.IfElse(cond) with ie.true_block(): hidden = paddle.nn.functional.relu(data) ie.output(hidden) diff --git a/test/ir/test_ir_skip_layernorm_pass.py b/test/ir/test_ir_skip_layernorm_pass.py index 04b7aaad2fb27..015538bcd9b45 100644 --- a/test/ir/test_ir_skip_layernorm_pass.py +++ b/test/ir/test_ir_skip_layernorm_pass.py @@ -17,14 +17,14 @@ from pass_test import PassTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class SkipLayerNormFusePassTest(PassTest): def setUp(self): paddle.enable_static() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name="x", shape=[128, 768], dtype="float32", lod_level=0 ) @@ -48,7 +48,7 @@ def test_check_program(self): if core.is_compiled_with_cuda(): use_gpu_set.append(True) for use_gpu in use_gpu_set: - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() opt_program = self._apply_ir_passes() self.check_program(opt_program) diff --git a/test/ir/test_ir_subgraph_python_interface.py b/test/ir/test_ir_subgraph_python_interface.py index 08f614fa062a6..6738dc6dccef6 100644 --- a/test/ir/test_ir_subgraph_python_interface.py +++ b/test/ir/test_ir_subgraph_python_interface.py @@ -17,9 +17,9 @@ from eager_op_test import OpTestTool import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import IrGraph, Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.framework import IrGraph, Program, program_guard from paddle.static.quantization import QuantizationTransformPass paddle.enable_static() @@ -78,9 +78,9 @@ def false_func(): def test_quant_sub_graphs(self, use_cuda=False): graph, sub_graphs = self.build_graph_with_sub_graph() - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() transform_pass = QuantizationTransformPass( - scope=fluid.global_scope(), + scope=base.global_scope(), place=place, activation_quantize_type='abs_max', weight_quantize_type='range_abs_max', diff --git a/test/ir/test_ir_yolo_box_pass.py b/test/ir/test_ir_yolo_box_pass.py index 5947a71b93c7b..5ee434acef1f8 100644 --- a/test/ir/test_ir_yolo_box_pass.py +++ b/test/ir/test_ir_yolo_box_pass.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core +from paddle.base.layer_helper import LayerHelper paddle.enable_static() @@ -89,7 +89,7 @@ def test_yolo_box_pass(self): ) graph = core.Graph(program.desc) core.get_pass("yolo_box_fuse_pass").apply(graph) - graph = paddle.fluid.framework.IrGraph(graph) + graph = paddle.base.framework.IrGraph(graph) op_nodes = graph.all_op_nodes() for op_node in op_nodes: op_type = op_node.op().type() diff --git a/test/legacy_test/auto_checkpoint_utils.py b/test/legacy_test/auto_checkpoint_utils.py index 6f8a58a267655..eff2829ec4221 100644 --- a/test/legacy_test/auto_checkpoint_utils.py +++ b/test/legacy_test/auto_checkpoint_utils.py @@ -18,10 +18,10 @@ import numpy as np import paddle -import paddle.fluid.incubate.checkpoint.auto_checkpoint as acp -from paddle import fluid -from paddle.fluid import unique_name -from paddle.fluid.framework import program_guard +import paddle.base.incubate.checkpoint.auto_checkpoint as acp +from paddle import base +from paddle.base import unique_name +from paddle.base.framework import program_guard BATCH_NUM = 4 BATCH_SIZE = 1 @@ -30,7 +30,7 @@ CLASS_NUM = 2 USE_GPU = False # whether use GPU to run model -places = fluid.cuda_places() if USE_GPU else fluid.cpu_places() +places = base.cuda_places() if USE_GPU else base.cpu_places() logger = None @@ -86,10 +86,10 @@ def simple_net(): sgd, loss, image, label = simple_net() if minimize: - compiled = fluid.CompiledProgram(main_prog) + compiled = base.CompiledProgram(main_prog) else: compiled = None - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( feed_list=[image, label], capacity=64, use_double_buffer=True, @@ -106,15 +106,15 @@ def simple_net(): return compiled, loader, sgd, loss, image, label def _generate(self): - main_prog = fluid.Program() - startup_prog = fluid.Program() - exe = fluid.Executor(places[0]) + main_prog = base.Program() + startup_prog = base.Program() + exe = base.Executor(places[0]) return exe, main_prog, startup_prog def _reset_generator(self): - unique_name.generator = fluid.unique_name.UniqueNameGenerator() - acp.generator = fluid.unique_name.UniqueNameGenerator() + unique_name.generator = base.unique_name.UniqueNameGenerator() + acp.generator = base.unique_name.UniqueNameGenerator() acp.g_acp_type = None acp.g_checker = acp.AutoCheckpointChecker() acp.g_program_attr = {} diff --git a/test/legacy_test/auto_parallel_gpt_model.py b/test/legacy_test/auto_parallel_gpt_model.py index 4d5e1955e23f1..3f25aeb19b64c 100644 --- a/test/legacy_test/auto_parallel_gpt_model.py +++ b/test/legacy_test/auto_parallel_gpt_model.py @@ -809,7 +809,7 @@ def forward( x_dims_mapping = ["x"] + [None for i in range(len(x.shape) - 1)] w_dims_mapping = ["y"] + [None for i in range(len(w.shape) - 1)] - with paddle.fluid.name_scope('skip_quant'): + with paddle.base.name_scope('skip_quant'): if mesh: matmul = auto.shard_op( paddle.matmul, mesh, [x_dims_mapping, w_dims_mapping, None] diff --git a/test/legacy_test/check_nan_inf_base.py b/test/legacy_test/check_nan_inf_base.py index fad2ebaa752e5..31dbb1bed98a9 100644 --- a/test/legacy_test/check_nan_inf_base.py +++ b/test/legacy_test/check_nan_inf_base.py @@ -19,8 +19,8 @@ os.environ["FLAGS_check_nan_inf"] = "1" import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -73,16 +73,16 @@ def net(): def check(use_cuda): - main = fluid.Program() - startup = fluid.Program() - scope = fluid.core.Scope() + main = base.Program() + startup = base.Program() + scope = base.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(main, startup): + with base.scope_guard(scope): + with base.program_guard(main, startup): y_predict, avg_cost, acc_top1 = net() - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup) step = 0.0 diff --git a/test/legacy_test/collective_allgather_op.py b/test/legacy_test/collective_allgather_op.py index fb2841468bfb1..516603f71affc 100644 --- a/test/legacy_test/collective_allgather_op.py +++ b/test/legacy_test/collective_allgather_op.py @@ -15,8 +15,8 @@ from test_collective_base import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/legacy_test/collective_reducescatter_op.py b/test/legacy_test/collective_reducescatter_op.py index 2d10b66fb88dc..bffbafd8a341c 100644 --- a/test/legacy_test/collective_reducescatter_op.py +++ b/test/legacy_test/collective_reducescatter_op.py @@ -15,8 +15,8 @@ from test_collective_base import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[-1, 10, 1000], dtype='float32' ) diff --git a/test/legacy_test/decorator_helper.py b/test/legacy_test/decorator_helper.py index d0164f7beac64..d713878c3fb2d 100644 --- a/test/legacy_test/decorator_helper.py +++ b/test/legacy_test/decorator_helper.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import fluid +from paddle import base __all__ = ['many_times', 'prog_scope'] @@ -31,11 +31,11 @@ def __fn__(*args, **kwargs): def prog_scope(): def __impl__(fn): def __fn__(*args, **kwargs): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): fn(*args, **kwargs) return __fn__ diff --git a/test/legacy_test/detected_gpu.py b/test/legacy_test/detected_gpu.py index 55249b8c8cb9e..ff7d625805f6b 100644 --- a/test/legacy_test/detected_gpu.py +++ b/test/legacy_test/detected_gpu.py @@ -14,15 +14,12 @@ import sys -from paddle import fluid +from paddle import base -print("compile with cuda:", fluid.core.is_compiled_with_cuda()) -print("get_cuda_device_count:", fluid.core.get_cuda_device_count()) +print("compile with cuda:", base.core.is_compiled_with_cuda()) +print("get_cuda_device_count:", base.core.get_cuda_device_count()) -if ( - fluid.core.is_compiled_with_cuda() - and fluid.core.get_cuda_device_count() > 0 -): +if base.core.is_compiled_with_cuda() and base.core.get_cuda_device_count() > 0: sys.exit(0) else: sys.exit(1) diff --git a/test/legacy_test/detected_xpu.py b/test/legacy_test/detected_xpu.py index df22fea92462f..3d7e944c3b40d 100644 --- a/test/legacy_test/detected_xpu.py +++ b/test/legacy_test/detected_xpu.py @@ -14,12 +14,12 @@ import sys -from paddle import fluid +from paddle import base -print("compile with xpu:", fluid.core.is_compiled_with_xpu()) -print("get_xpu_device_count:", fluid.core.get_xpu_device_count()) +print("compile with xpu:", base.core.is_compiled_with_xpu()) +print("get_xpu_device_count:", base.core.get_xpu_device_count()) -if fluid.core.is_compiled_with_xpu() and fluid.core.get_xpu_device_count() > 0: +if base.core.is_compiled_with_xpu() and base.core.get_xpu_device_count() > 0: sys.exit(0) else: sys.exit(1) diff --git a/test/legacy_test/dist_allreduce_op.py b/test/legacy_test/dist_allreduce_op.py index 2a35dbdf18143..ba2ec0180a299 100644 --- a/test/legacy_test/dist_allreduce_op.py +++ b/test/legacy_test/dist_allreduce_op.py @@ -18,7 +18,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -27,8 +27,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -39,7 +39,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -50,7 +50,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -64,7 +64,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -92,7 +92,7 @@ def get_model(self, batch_size=2, single_device=False): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Reader train_reader = paddle.batch( diff --git a/test/legacy_test/dist_ctr.py b/test/legacy_test/dist_ctr.py index 3edee71832412..7b1010067cea0 100644 --- a/test/legacy_test/dist_ctr.py +++ b/test/legacy_test/dist_ctr.py @@ -18,14 +18,14 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base IS_SPARSE = True os.environ['PADDLE_ENABLE_REMOTE_PREFETCH'] = "1" # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistCTR2x2(TestDistRunnerBase): @@ -57,7 +57,7 @@ def get_model(self, batch_size=2): is_distributed=False, input=dnn_data, size=[dnn_input_dim, dnn_layer_dims[0]], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="deep_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -72,7 +72,7 @@ def get_model(self, batch_size=2): x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, @@ -84,7 +84,7 @@ def get_model(self, batch_size=2): is_distributed=False, input=lr_data, size=[lr_input_dim, 1], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -108,7 +108,7 @@ def get_model(self, batch_size=2): ) avg_cost = paddle.mean(x=cost) - inference_program = paddle.fluid.default_main_program().clone() + inference_program = paddle.base.default_main_program().clone() regularization = None use_l2_decay = bool(os.getenv('USE_L2_DECAY', 0)) diff --git a/test/legacy_test/dist_ctr_reader.py b/test/legacy_test/dist_ctr_reader.py index 657904fad0793..23f4daf2a5d8f 100644 --- a/test/legacy_test/dist_ctr_reader.py +++ b/test/legacy_test/dist_ctr_reader.py @@ -16,7 +16,7 @@ import tarfile import paddle -from paddle.fluid.log_helper import get_logger +from paddle.base.log_helper import get_logger logger = get_logger("paddle", logging.INFO) diff --git a/test/legacy_test/dist_fleet_ctr.py b/test/legacy_test/dist_fleet_ctr.py index 419344edfae5d..64c4f69a55654 100644 --- a/test/legacy_test/dist_fleet_ctr.py +++ b/test/legacy_test/dist_fleet_ctr.py @@ -25,13 +25,13 @@ from test_dist_fleet_base import FleetDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def fake_ctr_reader(): @@ -85,14 +85,14 @@ def net(self, args, is_train=True, batch_size=4, lr=0.01): if args.reader == "pyreader": if is_train: - self.reader = fluid.io.PyReader( + self.reader = base.io.PyReader( feed_list=datas, capacity=64, iterable=False, use_double_buffer=False, ) else: - self.test_reader = fluid.io.PyReader( + self.test_reader = base.io.PyReader( feed_list=datas, capacity=64, iterable=False, @@ -105,7 +105,7 @@ def net(self, args, is_train=True, batch_size=4, lr=0.01): is_distributed=False, input=dnn_data, size=[dnn_input_dim, dnn_layer_dims[0]], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="deep_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -121,7 +121,7 @@ def net(self, args, is_train=True, batch_size=4, lr=0.01): x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, @@ -133,7 +133,7 @@ def net(self, args, is_train=True, batch_size=4, lr=0.01): is_distributed=False, input=lr_data, size=[lr_input_dim, 1], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -174,7 +174,7 @@ def check_model_right(self, dirname): with open(model_filename, "rb") as f: program_desc_str = f.read() - program = fluid.Program.parse_from_string(program_desc_str) + program = base.Program.parse_from_string(program_desc_str) with open(os.path.join(dirname, "__model__.proto"), "w") as wn: wn.write(str(program)) @@ -204,7 +204,7 @@ def do_distributed_testing(self, fleet): batch_idx, loss_val ) fleet.util.print_on_rank(message, 0) - except fluid.core.EOFException: + except base.core.EOFException: self.test_reader.reset() pass_time = time.time() - pass_start @@ -218,7 +218,7 @@ def do_pyreader_training(self, fleet): fleet(Fleet api): the fleet object of Parameter Server, define distribute training role """ exe = self.get_executor() - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() batch_size = 4 @@ -231,7 +231,7 @@ def do_pyreader_training(self, fleet): pass_start = time.time() while True: loss_val = exe.run( - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[self.avg_cost.name], ) loss_val = np.mean(loss_val) @@ -246,7 +246,7 @@ def do_pyreader_training(self, fleet): fleet.util.print_on_rank(message, 0) pass_time = time.time() - pass_start - except fluid.core.EOFException: + except base.core.EOFException: self.reader.reset() dirname = os.getenv("SAVE_DIRNAME", None) @@ -265,7 +265,7 @@ def do_dataset_training_queuedataset(self, fleet): train_file_list = ctr_dataset_reader.prepare_fake_data() exe = self.get_executor() - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() thread_num = 2 @@ -289,7 +289,7 @@ def do_dataset_training_queuedataset(self, fleet): pass_start = time.time() dataset.set_filelist(filelist) exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, fetch_list=[self.avg_cost], fetch_info=["cost"], @@ -318,7 +318,7 @@ def do_dataset_training(self, fleet): train_file_list = ctr_dataset_reader.prepare_fake_data() exe = self.get_executor() - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() thread_num = 2 @@ -326,7 +326,7 @@ def do_dataset_training(self, fleet): filelist = train_file_list # config dataset - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_use_var(self.feeds) dataset.set_batch_size(128) dataset.set_thread(2) @@ -344,7 +344,7 @@ def do_dataset_training(self, fleet): for epoch_id in range(1): pass_start = time.time() exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, fetch_list=[self.avg_cost], fetch_info=["cost"], @@ -381,8 +381,8 @@ def do_dataset_training(self, fleet): fleet.save_dense_params( exe, dense_param_dirname, - fluid.global_scope(), - fluid.default_main_program(), + base.global_scope(), + base.default_main_program(), ) save_one_table_dirname = os.getenv("SAVE_ONE_TABLE_DIRNAME", None) diff --git a/test/legacy_test/dist_fleet_ctr_ps_gpu.py b/test/legacy_test/dist_fleet_ctr_ps_gpu.py index 43253c4674a7c..bf109e6a61306 100644 --- a/test/legacy_test/dist_fleet_ctr_ps_gpu.py +++ b/test/legacy_test/dist_fleet_ctr_ps_gpu.py @@ -26,11 +26,11 @@ from test_dist_fleet_base import runtime_main import paddle -from paddle import fluid +from paddle import base # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistGpuPsCTR2x2(TestDistCTR2x2): @@ -44,7 +44,7 @@ def check_model_right(self, dirname): with open(model_filename, "rb") as f: program_desc_str = f.read() - program = fluid.Program.parse_from_string(program_desc_str) + program = base.Program.parse_from_string(program_desc_str) with open(os.path.join(dirname, "__model__.proto"), "w") as wn: wn.write(str(program)) @@ -55,8 +55,8 @@ def do_pyreader_training(self, fleet): fleet(Fleet api): the fleet object of Parameter Server, define distribute training role """ device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - exe = fluid.Executor(place) + place = base.CUDAPlace(device_id) + exe = base.Executor(place) exe.run(fleet.startup_program) fleet.init_worker() @@ -86,7 +86,7 @@ def do_pyreader_training(self, fleet): fleet.util.print_on_rank(message, 0) pass_time = time.time() - pass_start - except fluid.core.EOFException: + except base.core.EOFException: self.reader.reset() model_dir = tempfile.mkdtemp() @@ -107,8 +107,8 @@ def do_dataset_training(self, fleet): ) = ctr_dataset_reader.prepare_data() device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - exe = fluid.Executor(place) + place = base.CUDAPlace(device_id) + exe = base.Executor(place) exe.run(fleet.startup_program) fleet.init_worker() diff --git a/test/legacy_test/dist_fleet_debug_gloo.py b/test/legacy_test/dist_fleet_debug_gloo.py index e4d86d57a873a..20ff924798d79 100644 --- a/test/legacy_test/dist_fleet_debug_gloo.py +++ b/test/legacy_test/dist_fleet_debug_gloo.py @@ -21,7 +21,7 @@ ) logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s") -logger = logging.getLogger("fluid") +logger = logging.getLogger("base") logger.setLevel(logging.INFO) # role = role_maker.GeneralRoleMaker( # init_timeout_seconds=100, diff --git a/test/legacy_test/dist_fleet_heter_pipeline_ctr.py b/test/legacy_test/dist_fleet_heter_pipeline_ctr.py index db7bcf8fac1a4..cb33e527d4155 100644 --- a/test/legacy_test/dist_fleet_heter_pipeline_ctr.py +++ b/test/legacy_test/dist_fleet_heter_pipeline_ctr.py @@ -22,13 +22,13 @@ from test_dist_fleet_heter_base import FleetDistHeterRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestHeterPipelinePsCTR2x2(FleetDistHeterRunnerBase): @@ -48,7 +48,7 @@ def net(self, args, batch_size=4, lr=0.01): """ dnn_input_dim, lr_input_dim = int(1e5), int(1e5) - with fluid.device_guard("cpu"): + with base.device_guard("cpu"): dnn_data = paddle.static.data( name="dnn_data", shape=[-1, 1], @@ -76,7 +76,7 @@ def net(self, args, batch_size=4, lr=0.01): is_distributed=False, input=dnn_data, size=[dnn_input_dim, dnn_layer_dims[0]], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="deep_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -92,7 +92,7 @@ def net(self, args, batch_size=4, lr=0.01): is_distributed=False, input=lr_data, size=[lr_input_dim, 1], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -102,20 +102,20 @@ def net(self, args, batch_size=4, lr=0.01): input=lr_embedding, pool_type="sum" ) - with fluid.device_guard("gpu"): + with base.device_guard("gpu"): for i, dim in enumerate(dnn_layer_dims[1:]): fc = paddle.static.nn.fc( x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, ) dnn_out = fc - with fluid.device_guard("cpu"): + with base.device_guard("cpu"): merge_layer = paddle.concat([dnn_out, lr_pool], axis=1) label = paddle.cast(label, dtype="int64") predict = paddle.static.nn.fc( @@ -141,20 +141,20 @@ def check_model_right(self, dirname): with open(model_filename, "rb") as f: program_desc_str = f.read() - program = fluid.Program.parse_from_string(program_desc_str) + program = base.Program.parse_from_string(program_desc_str) with open(os.path.join(dirname, "__model__.proto"), "w") as wn: wn.write(str(program)) def do_dataset_training(self, fleet): train_file_list = ctr_dataset_reader.prepare_fake_data() - exe = fluid.Executor(fluid.CPUPlace()) - real_program = fluid.default_main_program()._heter_pipeline_opt[ + exe = base.Executor(base.CPUPlace()) + real_program = base.default_main_program()._heter_pipeline_opt[ "section_program" ] print(real_program) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() thread_num = int(os.getenv("CPU_NUM", 2)) @@ -164,7 +164,7 @@ def do_dataset_training(self, fleet): print(f"filelist: {filelist}") # config dataset - dataset = fluid.DatasetFactory().create_dataset() + dataset = base.DatasetFactory().create_dataset() dataset.set_batch_size(batch_size) dataset.set_use_var(self.feeds) pipe_command = 'python3 ctr_dataset_reader.py' @@ -177,7 +177,7 @@ def do_dataset_training(self, fleet): pass_start = time.time() dataset.set_filelist(filelist) exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, fetch_list=[self.avg_cost], fetch_info=["cost"], @@ -189,10 +189,10 @@ def do_dataset_training(self, fleet): exe.close() def do_dataset_heter_training(self, fleet): - exe = fluid.Executor() - exe.run(fluid.default_startup_program()) + exe = base.Executor() + exe.run(base.default_startup_program()) fleet.init_worker() - real_program = fluid.default_main_program()._heter_pipeline_opt[ + real_program = base.default_main_program()._heter_pipeline_opt[ "section_program" ] print(real_program) @@ -202,7 +202,7 @@ def do_dataset_heter_training(self, fleet): pass_start = time.time() exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[self.avg_cost], fetch_info=["cost"], print_period=2, @@ -216,7 +216,7 @@ def do_dataset_heter_training(self, fleet): # pass_start = time.time() # dataset.set_filelist(filelist) # exe.train_from_dataset( - # program=fluid.default_main_program(), + # program=base.default_main_program(), # dataset=dataset, # fetch_list=[self.avg_cost], # fetch_info=["cost"], diff --git a/test/legacy_test/dist_fleet_raw_program_optimizer.py b/test/legacy_test/dist_fleet_raw_program_optimizer.py index 69b5bf88702f4..e753ad3cc9da2 100644 --- a/test/legacy_test/dist_fleet_raw_program_optimizer.py +++ b/test/legacy_test/dist_fleet_raw_program_optimizer.py @@ -18,7 +18,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -28,8 +28,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -40,7 +40,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -51,7 +51,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -65,7 +65,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -93,7 +93,7 @@ def get_model(self, batch_size=2, single_device=False): input=predict, label=label, total=batch_size_tensor ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) # Reader train_reader = paddle.batch( diff --git a/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py b/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py index f19bb7424c6ab..7f118af943d12 100644 --- a/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py +++ b/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py @@ -18,7 +18,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -28,8 +28,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -40,7 +40,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -51,7 +51,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -65,7 +65,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -93,7 +93,7 @@ def get_model(self, batch_size=2, single_device=False): input=predict, label=label, total=batch_size_tensor ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) # Reader train_reader = paddle.batch( diff --git a/test/legacy_test/dist_fleet_simnet_bow.py b/test/legacy_test/dist_fleet_simnet_bow.py index 5885f39569450..ffbe371cc228a 100644 --- a/test/legacy_test/dist_fleet_simnet_bow.py +++ b/test/legacy_test/dist_fleet_simnet_bow.py @@ -19,7 +19,7 @@ from test_dist_fleet_base import FleetDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -37,8 +37,8 @@ sample_rate = 1 # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def fake_simnet_reader(): @@ -111,7 +111,7 @@ def train_network( reader = None if is_pyreader: - reader = fluid.io.PyReader( + reader = base.io.PyReader( feed_list=datas, capacity=64, iterable=False, @@ -123,7 +123,7 @@ def train_network( input=q, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", ), @@ -139,7 +139,7 @@ def train_network( q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -151,7 +151,7 @@ def train_network( input=pt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -168,11 +168,11 @@ def train_network( pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # embedding @@ -180,7 +180,7 @@ def train_network( input=nt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", ), @@ -196,11 +196,11 @@ def train_network( nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) @@ -235,7 +235,7 @@ def check_model_right(self, dirname): with open(model_filename, "rb") as f: program_desc_str = f.read() - program = fluid.Program.parse_from_string(program_desc_str) + program = base.Program.parse_from_string(program_desc_str) with open(os.path.join(dirname, "__model__.proto"), "w") as wn: wn.write(str(program)) @@ -246,8 +246,8 @@ def do_pyreader_training(self, fleet): fleet(Fleet api): the fleet object of Parameter Server, define distribute training role """ - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) fleet.init_worker() batch_size = 4 # reader @@ -259,7 +259,7 @@ def do_pyreader_training(self, fleet): pass_start = time.time() while True: loss_val = exe.run( - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[self.avg_cost.name], ) loss_val = np.mean(loss_val) @@ -269,7 +269,7 @@ def do_pyreader_training(self, fleet): fleet.util.print_on_rank(message, 0) pass_time = time.time() - pass_start - except fluid.core.EOFException: + except base.core.EOFException: self.reader.reset() def do_dataset_training(self, fleet): diff --git a/test/legacy_test/dist_fleet_sparse_embedding_ctr.py b/test/legacy_test/dist_fleet_sparse_embedding_ctr.py index 922a458b5d9a3..120b7e51305d7 100644 --- a/test/legacy_test/dist_fleet_sparse_embedding_ctr.py +++ b/test/legacy_test/dist_fleet_sparse_embedding_ctr.py @@ -21,7 +21,7 @@ from test_dist_fleet_base import FleetDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base def fake_ctr_reader(): @@ -74,7 +74,7 @@ def net(self, args, batch_size=4, lr=0.01): datas = [dnn_data, lr_data, label] if args.reader == "pyreader": - self.reader = fluid.io.PyReader( + self.reader = base.io.PyReader( feed_list=datas, capacity=64, iterable=False, @@ -101,7 +101,7 @@ def net(self, args, batch_size=4, lr=0.01): size=[dnn_input_dim, dnn_layer_dims[0]], is_test=inference, entry=entry, - param_attr=fluid.ParamAttr(name="deep_embedding", initializer=init), + param_attr=base.ParamAttr(name="deep_embedding", initializer=init), ) dnn_pool = paddle.static.nn.sequence_lod.sequence_pool( input=dnn_embedding, pool_type="sum" @@ -112,7 +112,7 @@ def net(self, args, batch_size=4, lr=0.01): x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, @@ -125,7 +125,7 @@ def net(self, args, batch_size=4, lr=0.01): size=[lr_input_dim, 1], is_test=inference, entry=entry, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -161,9 +161,9 @@ def do_pyreader_training(self, fleet): fleet(Fleet api): the fleet object of Parameter Server, define distribute training role """ - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() batch_size = 4 @@ -176,7 +176,7 @@ def do_pyreader_training(self, fleet): try: while True: loss_val = exe.run( - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[self.avg_cost.name], ) loss_val = np.mean(loss_val) @@ -185,7 +185,7 @@ def do_pyreader_training(self, fleet): epoch_id, loss_val ) ) - except fluid.core.EOFException: + except base.core.EOFException: self.reader.reset() model_dir = os.getenv("MODEL_DIR", None) diff --git a/test/legacy_test/dist_fleet_sync_batch_norm.py b/test/legacy_test/dist_fleet_sync_batch_norm.py index 09e11bda2d671..86e8e921adf10 100644 --- a/test/legacy_test/dist_fleet_sync_batch_norm.py +++ b/test/legacy_test/dist_fleet_sync_batch_norm.py @@ -20,9 +20,9 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.distributed import fleet -from paddle.fluid import core from paddle.static import Executor, Program, program_guard @@ -30,7 +30,7 @@ def get_program(args): main, startup = Program(), Program() main.random_seed = 10 startup.random_seed = 10 - with fluid.unique_name.guard(): + with base.unique_name.guard(): with program_guard(main, startup): data = paddle.static.data( name='input', @@ -42,14 +42,14 @@ def get_program(args): input=data, num_filters=32, filter_size=1, - param_attr=fluid.ParamAttr(name='conv2d_weight'), + param_attr=base.ParamAttr(name='conv2d_weight'), bias_attr=False, use_cudnn=args.use_cudnn, ) bn = paddle.static.nn.batch_norm( conv, - param_attr=fluid.ParamAttr(name='bn_scale'), - bias_attr=fluid.ParamAttr(name='bn_bias'), + param_attr=base.ParamAttr(name='bn_scale'), + bias_attr=base.ParamAttr(name='bn_bias'), moving_mean_name='bn_moving_mean', moving_variance_name='bn_moving_variance', data_layout=args.layout, @@ -69,7 +69,7 @@ def get_program(args): def train(args): - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.sync_batch_norm = True build_strategy.enable_inplace = False build_strategy.memory_optimize = False @@ -86,7 +86,7 @@ def train(args): exe.run(startup) for nm in args.fetch_list: - fv = fluid.framework._get_var(str(nm), program=main) + fv = base.framework._get_var(str(nm), program=main) fv.persistable = True fetch_list = [v.name for v in outs] + args.fetch_list @@ -100,7 +100,7 @@ def train(args): ) data = np.load(filepath) - comp_prog = fluid.compiler.CompiledProgram( + comp_prog = base.compiler.CompiledProgram( main, build_strategy=build_strategy ) sync_bn_fetches = exe.run( diff --git a/test/legacy_test/dist_hapi_mnist_dynamic.py b/test/legacy_test/dist_hapi_mnist_dynamic.py index 64983a5054853..6c3a6aabd3705 100644 --- a/test/legacy_test/dist_hapi_mnist_dynamic.py +++ b/test/legacy_test/dist_hapi_mnist_dynamic.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import Model, fluid, set_device +from paddle import Model, base, set_device from paddle.metric import Accuracy from paddle.nn.layer.loss import CrossEntropyLoss from paddle.static import InputSpec as Input @@ -50,7 +50,7 @@ def compute_accuracy(pred, gt): @unittest.skipIf( - not fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestDistTraining(unittest.TestCase): def test_dynamic_multiple_gpus(self): diff --git a/test/legacy_test/dist_hapi_mnist_static.py b/test/legacy_test/dist_hapi_mnist_static.py index 9229e34529ba5..dd5dbf914278e 100644 --- a/test/legacy_test/dist_hapi_mnist_static.py +++ b/test/legacy_test/dist_hapi_mnist_static.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import Model, fluid, set_device +from paddle import Model, base, set_device from paddle.metric import Accuracy from paddle.nn.layer.loss import CrossEntropyLoss from paddle.static import InputSpec as Input @@ -50,7 +50,7 @@ def compute_accuracy(pred, gt): @unittest.skipIf( - not fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestDistTraining(unittest.TestCase): def test_static_multiple_gpus(self): diff --git a/test/legacy_test/dist_hapi_pure_fp16_static.py b/test/legacy_test/dist_hapi_pure_fp16_static.py index 3506b22d31f30..9abdd545f1325 100644 --- a/test/legacy_test/dist_hapi_pure_fp16_static.py +++ b/test/legacy_test/dist_hapi_pure_fp16_static.py @@ -17,18 +17,18 @@ import numpy as np import paddle -from paddle import Model, fluid +from paddle import Model, base from paddle.nn.layer.loss import CrossEntropyLoss from paddle.static import InputSpec from paddle.vision.models import LeNet @unittest.skipIf( - not fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestDistTrainingWithPureFP16(unittest.TestCase): def test_amp_training_purefp16(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): self.skipTest('module not tested when ONLY_CPU compiling') data = np.random.random(size=(4, 1, 28, 28)).astype(np.float32) label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64) diff --git a/test/legacy_test/dist_mnist.py b/test/legacy_test/dist_mnist.py index 925a6f4f6739e..9bd3747524242 100644 --- a/test/legacy_test/dist_mnist.py +++ b/test/legacy_test/dist_mnist.py @@ -18,7 +18,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.incubate.distributed.fleet.collective import fleet paddle.enable_static() @@ -27,8 +27,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -39,7 +39,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -50,7 +50,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -64,7 +64,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -92,7 +92,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization # TODO(typhoonzero): fix distributed adam optimizer # opt = paddle.optimizer.Adam( diff --git a/test/legacy_test/dist_mnist_batch_merge.py b/test/legacy_test/dist_mnist_batch_merge.py index e6a5070a53c99..5a1aa871932a3 100644 --- a/test/legacy_test/dist_mnist_batch_merge.py +++ b/test/legacy_test/dist_mnist_batch_merge.py @@ -16,7 +16,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base DTYPE = "float32" @@ -56,7 +56,7 @@ def get_model(self, batch_size=2): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) diff --git a/test/legacy_test/dist_mnist_dgc.py b/test/legacy_test/dist_mnist_dgc.py index 6f9d892cbd570..29d848c373915 100644 --- a/test/legacy_test/dist_mnist_dgc.py +++ b/test/legacy_test/dist_mnist_dgc.py @@ -22,7 +22,7 @@ ) import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -30,8 +30,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def cnn_model(data): @@ -42,7 +42,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -53,7 +53,7 @@ def cnn_model(data): pool_size=2, pool_stride=2, act="relu", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -67,7 +67,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -95,7 +95,7 @@ def get_model(self, batch_size=2, use_dgc=False, build_strategy=None): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() if not use_dgc: opt = paddle.optimizer.Momentum(learning_rate=self.lr, momentum=0.9) else: diff --git a/test/legacy_test/dist_mnist_fp16_allreduce.py b/test/legacy_test/dist_mnist_fp16_allreduce.py index 44626be4f01da..669ce95021e28 100644 --- a/test/legacy_test/dist_mnist_fp16_allreduce.py +++ b/test/legacy_test/dist_mnist_fp16_allreduce.py @@ -16,7 +16,7 @@ from test_dist_base import TestDistRunnerBase, _insert_comm_op, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.meta_optimizers import ( FP16AllReduceOptimizer as FP16AllReduce, @@ -26,8 +26,8 @@ paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistMnist2x2(TestDistRunnerBase): @@ -51,7 +51,7 @@ def get_model(self, batch_size=2, single_device=False): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9) diff --git a/test/legacy_test/dist_mnist_lars.py b/test/legacy_test/dist_mnist_lars.py index 9c963bc4cbb39..02ed25c76ccac 100644 --- a/test/legacy_test/dist_mnist_lars.py +++ b/test/legacy_test/dist_mnist_lars.py @@ -16,14 +16,14 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base DTYPE = "float32" paddle.dataset.mnist.fetch() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistMnist2x2(TestDistRunnerBase): @@ -47,7 +47,7 @@ def get_model(self, batch_size=2): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = paddle.incubate.optimizer.LarsMomentumOptimizer( learning_rate=0.001, momentum=0.9 diff --git a/test/legacy_test/dist_se_resnext.py b/test/legacy_test/dist_se_resnext.py index f7b31d315722f..98b6af3af08ee 100644 --- a/test/legacy_test/dist_se_resnext.py +++ b/test/legacy_test/dist_se_resnext.py @@ -17,13 +17,13 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base paddle.enable_static() # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 train_parameters = { "input_size": [3, 224, 224], @@ -120,7 +120,7 @@ def net(self, input, class_dim=1000): x=drop, size=class_dim, activation='softmax', - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.05) ), ) @@ -173,7 +173,7 @@ def conv_bn_layer( groups=groups, act=None, # avoid pserver CPU init differs from GPU - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.05) ), bias_attr=False, @@ -186,7 +186,7 @@ def squeeze_excitation(self, input, num_channels, reduction_ratio): squeeze = paddle.static.nn.fc( x=pool, size=num_channels // reduction_ratio, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.05) ), activation='relu', @@ -195,7 +195,7 @@ def squeeze_excitation(self, input, num_channels, reduction_ratio): excitation = paddle.static.nn.fc( x=squeeze, size=num_channels, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.05) ), activation='sigmoid', @@ -226,7 +226,7 @@ def get_model(self, batch_size=2, use_dgc=False): acc_top5 = paddle.static.accuracy(input=out, label=label, k=5) # Evaluator - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) # Optimization total_images = 6149 # flowers diff --git a/test/legacy_test/dist_sharding_save.py b/test/legacy_test/dist_sharding_save.py index 7483e02b48c5d..8eb61d220971f 100755 --- a/test/legacy_test/dist_sharding_save.py +++ b/test/legacy_test/dist_sharding_save.py @@ -17,13 +17,13 @@ from dist_mnist import cnn_model # noqa: F401 import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet.base import role_maker from paddle.distributed.fleet.meta_optimizers import sharding # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 def runtime_main(): @@ -34,12 +34,12 @@ def runtime_main(): paddle.enable_static() # model definition - train_prog = paddle.fluid.Program() - startup_prog = paddle.fluid.Program() + train_prog = paddle.base.Program() + startup_prog = paddle.base.Program() role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - with fluid.program_guard(train_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(train_prog, startup_prog): + with base.unique_name.guard(): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -76,8 +76,8 @@ def runtime_main(): # execution device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - exe = fluid.Executor(place) + place = base.CUDAPlace(device_id) + exe = base.Executor(place) exe.run(startup_prog) dirname = "./ut_sharding_save_model" sharding.utils.save_persistables( diff --git a/test/legacy_test/dist_text_classification.py b/test/legacy_test/dist_text_classification.py index 3a6bb6c152c96..0e3c79d758c80 100644 --- a/test/legacy_test/dist_text_classification.py +++ b/test/legacy_test/dist_text_classification.py @@ -21,7 +21,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base DTYPE = "float32" VOCAB_URL = 'http://paddle-dist-ce-data.bj.bcebos.com/imdb.vocab' @@ -59,7 +59,7 @@ def conv_net( input=input, size=[dict_dim, emb_dim], is_sparse=False, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -70,7 +70,7 @@ def conv_net( filter_size=window_size, act="tanh", pool_type="max", - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -78,7 +78,7 @@ def conv_net( fc_0 = paddle.static.nn.fc( x=[conv_3], size=fc0_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -87,7 +87,7 @@ def conv_net( x=[fc_0], size=class_dim, activation="softmax", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), ) @@ -138,7 +138,7 @@ def get_model(self, batch_size=2): ) avg_cost = paddle.mean(x=cost) acc = paddle.static.accuracy(input=predict, label=label) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = get_optimizer(learning_rate=0.001) diff --git a/test/legacy_test/dist_word2vec.py b/test/legacy_test/dist_word2vec.py index d6fcf02a43bdd..d32f836cdd15b 100644 --- a/test/legacy_test/dist_word2vec.py +++ b/test/legacy_test/dist_word2vec.py @@ -17,7 +17,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base IS_SPARSE = True EMBED_SIZE = 32 @@ -25,8 +25,8 @@ N = 5 # Fix seed for test -fluid.default_startup_program().random_seed = 1 -fluid.default_main_program().random_seed = 1 +base.default_startup_program().random_seed = 1 +base.default_main_program().random_seed = 1 class TestDistWord2vec2x2(TestDistRunnerBase): @@ -39,7 +39,7 @@ def __network__(words): size=[dict_size, EMBED_SIZE], dtype='float32', is_sparse=IS_SPARSE, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='shared_w', initializer=paddle.nn.initializer.Constant(value=0.1), ), @@ -49,7 +49,7 @@ def __network__(words): size=[dict_size, EMBED_SIZE], dtype='float32', is_sparse=IS_SPARSE, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='shared_w', initializer=paddle.nn.initializer.Constant(value=0.1), ), @@ -59,7 +59,7 @@ def __network__(words): size=[dict_size, EMBED_SIZE], dtype='float32', is_sparse=IS_SPARSE, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='shared_w', initializer=paddle.nn.initializer.Constant(value=0.1), ), @@ -69,7 +69,7 @@ def __network__(words): size=[dict_size, EMBED_SIZE], dtype='float32', is_sparse=IS_SPARSE, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='shared_w', initializer=paddle.nn.initializer.Constant(value=0.1), ), @@ -83,7 +83,7 @@ def __network__(words): x=concat_embed, size=HIDDEN_SIZE, activation='sigmoid', - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.1) ), ) @@ -91,7 +91,7 @@ def __network__(words): x=hidden1, size=dict_size, activation='softmax', - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.1) ), ) @@ -126,7 +126,7 @@ def __network__(words): [first_word, second_word, third_word, forth_word, next_word] ) - inference_program = paddle.fluid.default_main_program().clone() + inference_program = paddle.base.default_main_program().clone() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) sgd_optimizer.minimize(avg_cost) diff --git a/test/legacy_test/distributed_fused_lamb_test_base.py b/test/legacy_test/distributed_fused_lamb_test_base.py index b0926c93c2082..baffc7dd5e546 100644 --- a/test/legacy_test/distributed_fused_lamb_test_base.py +++ b/test/legacy_test/distributed_fused_lamb_test_base.py @@ -19,9 +19,9 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed import fleet from paddle.distributed.fleet.meta_optimizers.common import CollectiveHelper -from paddle.fluid import core from paddle.incubate import DistributedFusedLamb from paddle.nn.clip import ClipGradBase, _clip_by_global_norm_using_mp_type from paddle.vision.models import resnet18 as resnet @@ -126,7 +126,7 @@ def run_model(use_distributed_lamb, use_fp16, use_master_param_norm, **kwargs): main = paddle.static.Program() startup = paddle.static.Program() with paddle.static.program_guard(main, startup): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.amp.fp16_guard(): image = paddle.static.data( name='image', diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index 66a48acc6c5f6..e7e8b8059ab99 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -39,12 +39,12 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.autograd.ir_backward import grad as ir_grad -from paddle.fluid import core, unique_name -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor -from paddle.fluid.framework import ( +from paddle.base import core, unique_name +from paddle.base.backward import append_backward +from paddle.base.executor import Executor +from paddle.base.framework import ( OpProtoHolder, Program, _current_expected_place, @@ -52,7 +52,7 @@ get_flags, set_flags, ) -from paddle.fluid.wrapped_decorator import signature_safe_contextmanager +from paddle.base.wrapped_decorator import signature_safe_contextmanager sys.path.append(os.path.abspath(os.path.dirname(__file__))) @@ -78,7 +78,7 @@ def check_out_dtype(api_fn, in_specs, expect_dtypes, target_index=0, **configs): config(dict): other arguments of paddle api function Example: - check_out_dtype(fluid.layers.pad_constant_like, [([2,3,2,3], 'float64'), ([1, 3, 1,3], )], ['float32', 'float64', 'int64'], target_index=1, pad_value=0.) + check_out_dtype(base.layers.pad_constant_like, [([2,3,2,3], 'float64'), ([1, 3, 1,3], )], ['float32', 'float64', 'int64'], target_index=1, pad_value=0.) """ with paddle_static_guard(): @@ -106,7 +106,7 @@ def check_out_dtype(api_fn, in_specs, expect_dtypes, target_index=0, **configs): ) out = api_fn(*input_t, **configs) - out_dtype = fluid.data_feeder.convert_dtype(out.dtype) + out_dtype = base.data_feeder.convert_dtype(out.dtype) if out_dtype != expect_dtype: raise ValueError( @@ -578,7 +578,7 @@ def _enable_check_cinn_test(self, place, inputs, outputs): if ( not core.is_compiled_with_cinn() or not core.is_compiled_with_cuda() - or not isinstance(place, fluid.CUDAPlace) + or not isinstance(place, base.CUDAPlace) ): return False # CINN not support bfloat16 now, skip cinn test @@ -851,11 +851,11 @@ def _create_var_from_numpy(self, value): if isinstance(value, tuple): data = value[0] lod = value[1] - v = fluid.dygraph.base.to_variable(value=data) + v = base.dygraph.base.to_variable(value=data) v.value().get_tensor().set_recursive_sequence_lengths(lod) return v else: - return fluid.dygraph.base.to_variable(value) + return base.dygraph.base.to_variable(value) def get_sequence_batch_size_1_input(self, lod=None, shape=None): """Get LoD input data whose batch size is 1. @@ -1100,8 +1100,8 @@ def cal_python_api(python_api, args, kernel_sig): ][i] return result - with fluid.dygraph.base.guard(place=place): - block = fluid.default_main_program().global_block() + with base.dygraph.base.guard(place=place): + block = base.default_main_program().global_block() op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) # prepare input variable dygraph_tensor_inputs = ( @@ -1166,8 +1166,8 @@ def _calc_dygraph_output( self.__class__.op_type = ( self.op_type ) # for ci check, please not delete it for now - with fluid.dygraph.base.guard(place=place): - block = fluid.default_main_program().global_block() + with base.dygraph.base.guard(place=place): + block = base.default_main_program().global_block() op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) @@ -1204,8 +1204,8 @@ def _calc_dygraph_output( return outputs def get_kernel_signature(self, place, egr_inps=None, egr_oups=None): - with fluid.dygraph.base.guard(place=place): - block = fluid.default_main_program().global_block() + with base.dygraph.base.guard(place=place): + block = base.default_main_program().global_block() op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) # prepare input variable dygraph_tensor_inputs = ( @@ -1385,7 +1385,7 @@ def _check_ir_output(self, place, program, feed_map, fetch_list, outs): executor = Executor(place) new_program = None if isinstance(program, paddle.static.CompiledProgram): - new_program = fluid.CompiledProgram( + new_program = base.CompiledProgram( program._program, build_strategy=program._build_strategy ) else: @@ -1436,7 +1436,7 @@ def _calc_output( for_inplace_test=None, check_cinn=False, ): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): program = Program() block = program.global_block() op = self._append_ops(block) @@ -1457,9 +1457,9 @@ def _calc_output( original_program = program if parallel: use_cuda = False - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): use_cuda = True - compiled_prog = fluid.CompiledProgram(program) + compiled_prog = base.CompiledProgram(program) program = compiled_prog fetch_list = getattr(self, "fetch_list", []) # if the fetch_list is customized by user, we use it directly. @@ -1488,14 +1488,14 @@ def _calc_output( self.rtol = self.cinn_rtol if (enable_inplace is not None) or enable_cinn_test: - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() if enable_inplace is not None: build_strategy.enable_inplace = enable_inplace if enable_cinn_test: build_strategy.build_cinn_pass = check_cinn self._check_cinn = enable_cinn_test - compiled_prog = fluid.CompiledProgram( + compiled_prog = base.CompiledProgram( program, build_strategy=build_strategy ) program = compiled_prog @@ -1698,8 +1698,8 @@ def _get_need_run_ops(self, op_desc, fwd_op_desc=None): def _dfs_grad_op(op_desc, fwd_op_desc=None): visited_ops.append(op_desc.type()) - has_infer_inplace = fluid.core.has_infer_inplace(op_desc.type()) - has_grad_op_maker = fluid.core.has_grad_op_maker(op_desc.type()) + has_infer_inplace = base.core.has_infer_inplace(op_desc.type()) + has_grad_op_maker = base.core.has_grad_op_maker(op_desc.type()) has_infer_inplace_in_grad_descendants = False if not has_grad_op_maker: has_infer_inplace_in_descendants = False @@ -1782,7 +1782,7 @@ def _calc_grad_output( Returns: res (tuple(outs, fetch_list, feed_map, program, op_desc)): The results of given grad_op_desc. """ - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): ( fwd_outs, fwd_fetch_list, @@ -1803,9 +1803,9 @@ def _calc_grad_output( exe = Executor(place) program = grad_program if enable_inplace is not None: - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.enable_inplace = enable_inplace - compiled_program = fluid.CompiledProgram( + compiled_program = base.CompiledProgram( grad_program, build_strategy=build_strategy ) program = compiled_program @@ -1876,8 +1876,8 @@ def check_inplace_output_with_place( if os.getenv("FLAGS_enable_new_ir_in_executor"): return - has_infer_inplace = fluid.core.has_infer_inplace(self.op_type) - has_grad_op_maker = fluid.core.has_grad_op_maker(self.op_type) + has_infer_inplace = base.core.has_infer_inplace(self.op_type) + has_grad_op_maker = base.core.has_grad_op_maker(self.op_type) fwd_res = self._calc_output( place, no_check_set=no_check_set, for_inplace_test=True ) @@ -1889,7 +1889,7 @@ def check_inplace_output_with_place( return for op_desc, father_op_desc in reversed(need_run_ops): # The first one is the forward op - has_infer_inplace = fluid.core.has_infer_inplace(op_desc.type()) + has_infer_inplace = base.core.has_infer_inplace(op_desc.type()) if op_desc.type() == self.op_type: if has_infer_inplace: res[op_desc] = self._check_forward_inplace( @@ -1904,7 +1904,7 @@ def check_inplace_output_with_place( else: # TODO(zhiqiu): enhance inplace_grad test for ops (sum and activation) using mkldnn # skip op that use_mkldnn currently - flags_use_mkldnn = fluid.core.globals()["FLAGS_use_mkldnn"] + flags_use_mkldnn = base.core.globals()["FLAGS_use_mkldnn"] attrs_use_mkldnn = hasattr(self, 'attrs') and bool( self.attrs.get('use_mkldnn', False) ) @@ -2246,7 +2246,7 @@ def convert_uint16_to_float_ifneed(self, actual_np, expect_np): return actual_np, expect_np def find_actual_value(self, name): - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): imperative_actual = find_imperative_actual( name, self.outputs, place ) @@ -2256,7 +2256,7 @@ def find_actual_value(self, name): return imperative_actual, imperative_actual_t def find_expect_value(self, name): - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): imperative_expect = find_imperative_expect( name, self.ref_outputs, place ) @@ -2267,7 +2267,7 @@ def find_expect_value(self, name): def _compare_list(self, name, actual, expect): """if expect is a tuple, we need to compare list.""" - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): self.op_test.assertListEqual( actual.value() .get_tensor() @@ -2467,8 +2467,8 @@ def _is_skip_name(self, name): and check_new_ir ): if ( - type(place) is paddle.fluid.libpaddle.CPUPlace - or type(place) is paddle.fluid.libpaddle.CUDAPlace + type(place) is paddle.base.libpaddle.CPUPlace + or type(place) is paddle.base.libpaddle.CUDAPlace ): print("New IR checker begins...........") with paddle.new_ir_utils.IrGuard(): @@ -2565,7 +2565,7 @@ def _get_places(self): return [] else: return [] - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] cpu_only = self._cpu_only if hasattr(self, '_cpu_only') else False if ( core.is_compiled_with_cuda() @@ -2990,7 +2990,7 @@ def check_grad_with_place( ) if check_dygraph: - with fluid.dygraph.base.guard(place): + with base.dygraph.base.guard(place): dygraph_dygraph_grad = self._get_dygraph_grad( inputs_to_check, place, @@ -3026,8 +3026,8 @@ def check_grad_with_place( and check_new_ir ): if ( - type(place) is paddle.fluid.libpaddle.CPUPlace - or type(place) is paddle.fluid.libpaddle.CUDAPlace + type(place) is paddle.base.libpaddle.CPUPlace + or type(place) is paddle.base.libpaddle.CUDAPlace ): print("New IR gradient begins...........") with paddle.new_ir_utils.IrGuard(): @@ -3075,8 +3075,8 @@ def _get_dygraph_grad( if hasattr(self, "use_custom_device") and self.use_custom_device: check_dygraph = False - with fluid.dygraph.base.guard(place=place): - block = fluid.default_main_program().global_block() + with base.dygraph.base.guard(place=place): + block = base.default_main_program().global_block() op_proto = OpProtoHolder.instance().get_op_proto(self.op_type) @@ -3181,15 +3181,15 @@ def _numpy_to_lod_tensor(np_value, lod, place): return tensor @staticmethod - def np_dtype_to_fluid_dtype(input): + def np_dtype_to_base_dtype(input): return input @staticmethod - def fluid_dtype_to_np_dtype(self, dtype): + def base_dtype_to_np_dtype(self, dtype): return dtype @staticmethod - def np_value_to_fluid_value(input): + def np_value_to_base_value(input): return input def cast_bf16_output(self, block, cast_inputs): @@ -3286,7 +3286,7 @@ def _get_gradient( parallel=False, check_cinn=False, ): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): prog = Program() scope = core.Scope() ir_scope = core.Scope() @@ -3371,18 +3371,18 @@ def _get_gradient( if parallel or enable_cinn_test: use_cuda = False - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): use_cuda = True build_strategy = None if enable_cinn_test: - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.build_cinn_pass = check_cinn self._check_cinn = True - compiled_prog = fluid.CompiledProgram(prog, build_strategy) + compiled_prog = base.CompiledProgram(prog, build_strategy) prog = compiled_prog - executor = fluid.Executor(place) + executor = base.Executor(place) res = list( map( np.array, diff --git a/test/legacy_test/feed_data_reader.py b/test/legacy_test/feed_data_reader.py index 68c2f22cda148..f91641d578c17 100644 --- a/test/legacy_test/feed_data_reader.py +++ b/test/legacy_test/feed_data_reader.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import fluid -from paddle.fluid.framework import Variable +from paddle import base +from paddle.base.framework import Variable def cyclic_reader(reader): @@ -45,5 +45,5 @@ def _feed_executor(self): return feed_data def get_next(self, exe, program): - assert isinstance(exe, fluid.Executor), "exe must be Executor" + assert isinstance(exe, base.Executor), "exe must be Executor" return self._feed_executor() diff --git a/test/legacy_test/fleet_heter_ps_training.py b/test/legacy_test/fleet_heter_ps_training.py index e2555e0c58a69..f54101b9c8f3c 100644 --- a/test/legacy_test/fleet_heter_ps_training.py +++ b/test/legacy_test/fleet_heter_ps_training.py @@ -13,14 +13,14 @@ # limitations under the License. import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet -fluid.disable_dygraph() +base.disable_dygraph() def get_dataset(inputs): - dataset = fluid.DatasetFactory().create_dataset() + dataset = base.DatasetFactory().create_dataset() dataset.set_use_var(inputs) dataset.set_batch_size(1) dataset.set_filelist([]) @@ -40,7 +40,7 @@ def net(batch_size=4, lr=0.01): """ dnn_input_dim, lr_input_dim = int(2), int(2) - with fluid.device_guard("cpu"): + with base.device_guard("cpu"): dnn_data = paddle.static.data( name="dnn_data", shape=[-1, 1], @@ -68,7 +68,7 @@ def net(batch_size=4, lr=0.01): is_distributed=False, input=dnn_data, size=[dnn_input_dim, dnn_layer_dims[0]], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="deep_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -84,7 +84,7 @@ def net(batch_size=4, lr=0.01): is_distributed=False, input=lr_data, size=[lr_input_dim, 1], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -94,13 +94,13 @@ def net(batch_size=4, lr=0.01): input=lr_embedding, pool_type="sum" ) - with fluid.device_guard("gpu"): + with base.device_guard("gpu"): for i, dim in enumerate(dnn_layer_dims[1:]): fc = paddle.static.nn.fc( x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, @@ -149,13 +149,13 @@ def net(batch_size=4, lr=0.01): fleet.stop_worker() elif fleet.is_worker(): pass - # place = fluid.CPUPlace() - # exe = fluid.Executor(place) - # exe.run(fluid.default_startup_program()) + # place = base.CPUPlace() + # exe = base.Executor(place) + # exe.run(base.default_startup_program()) # fleet.init_worker() # step = 1 # for i in range(step): # exe.train_from_dataset( - # program=fluid.default_main_program(), dataset=dataset, debug=False) + # program=base.default_main_program(), dataset=dataset, debug=False) # exe.close() # fleet.stop_worker() diff --git a/test/legacy_test/fleet_meta_optimizer_base.py b/test/legacy_test/fleet_meta_optimizer_base.py index acd7bb0a2f7d8..c0f2e2fc2a32f 100755 --- a/test/legacy_test/fleet_meta_optimizer_base.py +++ b/test/legacy_test/fleet_meta_optimizer_base.py @@ -17,7 +17,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -51,8 +51,8 @@ def debug_program(self, main_prog, startup_prog): print(startup_prog_op_types) def net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) input_x = paddle.static.data( @@ -87,11 +87,11 @@ def fc_block(input_x): fc_3 = paddle.static.nn.fc(x=fc_2, size=64, activation='tanh') return fc_3 - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): role = role_maker.PaddleCloudRoleMaker(is_collective=True) fleet.init(role) - with fluid.device_guard("gpu:0"): + with base.device_guard("gpu:0"): input_x = paddle.static.data( name="x", shape=[-1, 32], dtype='float32' ) @@ -100,10 +100,10 @@ def fc_block(input_x): ) for stage_idx in range(pp_degree): - with fluid.device_guard("gpu:" + str(stage_idx)): + with base.device_guard("gpu:" + str(stage_idx)): input_x = fc_block(input_x) - with fluid.device_guard("gpu:" + str(pp_degree - 1)): + with base.device_guard("gpu:" + str(pp_degree - 1)): prediction = paddle.static.nn.fc( x=[input_x], size=2, activation='softmax' ) @@ -119,7 +119,7 @@ def fc_block(input_x): return avg_cost, strategy def boundary_net(self, main_prog, startup_prog): - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): fleet.init(is_collective=True) x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32') with paddle.static.device_guard('gpu:0'): @@ -142,8 +142,8 @@ def optimizer( regularization=None, grad_clip=None, ): - with fluid.program_guard(train_prog, startup_prog): - with fluid.unique_name.guard(): + with base.program_guard(train_prog, startup_prog): + with base.unique_name.guard(): if name == 'momentum': optimizer = paddle.optimizer.Momentum( learning_rate=0.01, diff --git a/test/legacy_test/fleet_ps_training.py b/test/legacy_test/fleet_ps_training.py index 773448b417c71..2afb25664c800 100644 --- a/test/legacy_test/fleet_ps_training.py +++ b/test/legacy_test/fleet_ps_training.py @@ -16,7 +16,7 @@ from utils import gen_data import paddle -from paddle import fluid +from paddle import base from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.parameter_server.distribute_transpiler import ( fleet, @@ -26,7 +26,7 @@ input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') input_y = paddle.cast(input_y, dtype="float32") -with fluid.device_guard("gpu"): +with base.device_guard("gpu"): input_y = paddle.cast(input_y, dtype="int64") cost = mlp(input_x, input_y) @@ -42,8 +42,8 @@ fleet.init_server() fleet.run_server() elif fleet.is_worker(): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(fleet.startup_program) step = 1001 for i in range(step): diff --git a/test/legacy_test/gradient_checker.py b/test/legacy_test/gradient_checker.py index c24691eda1503..085100540d11f 100644 --- a/test/legacy_test/gradient_checker.py +++ b/test/legacy_test/gradient_checker.py @@ -19,9 +19,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import _append_grad_suffix_, _as_list +from paddle import base +from paddle.base import core +from paddle.base.backward import _append_grad_suffix_, _as_list def _product(t): @@ -81,7 +81,7 @@ def var_to_np_array_in_scope(scope, place, name): def make_jacobian(x, y_size, np_dtype): - if isinstance(x, fluid.framework.Variable): + if isinstance(x, base.framework.Variable): return np.zeros((_product(x.shape), y_size), dtype=np_dtype) elif isinstance(x, Sequence): jacobians = list( @@ -105,7 +105,7 @@ def _compute_numerical_jacobian(program, x, y, place, scope, delta): program (Program): the network program. x (Variable): the input variables. y (list[Variable]): the output variables. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. scope (Scope): the scope used to run program. delta: the amount of perturbation we give to the input @@ -116,12 +116,12 @@ def _compute_numerical_jacobian(program, x, y, place, scope, delta): where "x_size" is the number of elements in x and "y_size" is the number of elements in each y_i. """ - if not isinstance(x, fluid.framework.Variable): + if not isinstance(x, base.framework.Variable): raise TypeError('x is not Variable') # To compute the jacobian, treat x and y as one-dimensional vectors. y = _as_list(y) - exe = fluid.Executor(place) + exe = base.Executor(place) def run(): y_res = exe.run(program, scope=scope, fetch_list=y) @@ -160,7 +160,7 @@ def _compute_analytical_jacobian(program, x, y, place, scope): program (Program): a Program with forward pass. x (Variable|list[Variable]): a variable or list of variable y (Variable): the target variable. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. scope (Scope): the scope used to run program. Returns: @@ -170,7 +170,7 @@ def _compute_analytical_jacobian(program, x, y, place, scope): where "x_size" is the number of elements in x_i and "dy_size" is the number of elements in y. """ - if not isinstance(y, fluid.framework.Variable): + if not isinstance(y, base.framework.Variable): raise TypeError('y is not Variable') dy_name = _append_grad_suffix_(y.name) @@ -181,13 +181,13 @@ def _compute_analytical_jacobian(program, x, y, place, scope): name=dy_name, shape=y.shape, dtype=np_type, persistable=True ) # append backward - dx = fluid.gradients(y, x, dy) + dx = base.gradients(y, x, dy) # init dy tensor in scope value = np.zeros(y.shape, dtype=np_type) dy_t = set_var_in_scope(scope, place, dy_name, value) - exe = fluid.Executor(place) + exe = base.Executor(place) y_size = _product(y.shape) @@ -237,9 +237,9 @@ def grad_check( x (Variable|list[Variable]): input variables to the program. y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. program (Program|None): a Program with forward pass. - If None, use fluid.default_main_program(). + If None, use base.default_main_program(). eps (float): perturbation for finite differences. atol (float): absolute tolerance. rtol (float): relative tolerance. @@ -265,14 +265,14 @@ def fail_test(msg): u.stop_gradient = False u.persistable = True if place is None: - place = fluid.CPUPlace() + place = base.CPUPlace() if program is None: - program = fluid.default_main_program() + program = base.default_main_program() # init variable in startup program - scope = fluid.executor.global_scope() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + scope = base.executor.global_scope() + exe = base.Executor(place) + exe.run(base.default_startup_program()) x_init = _as_list(x_init) # init inputs if x_init is not None @@ -352,9 +352,9 @@ def double_grad_check( y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. y_grads (numpy.array|list[numpy.array]|None): the gradients with respect to y. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. program (Program|None): a Program with forward pass. - If None, use fluid.default_main_program(). + If None, use base.default_main_program(). eps (float): perturbation for finite differences. atol (float): absolute tolerance. rtol (float): relative tolerance. @@ -374,10 +374,10 @@ def double_grad_check( u.persistable = True if program is None: - program = fluid.default_main_program() + program = base.default_main_program() if y_grads is None: - scope = fluid.executor.global_scope() + scope = base.executor.global_scope() y_grads = [] y_grads_init = [] for yi in y: @@ -398,7 +398,7 @@ def double_grad_check( ] # append first order grads - target_grads = fluid.gradients(y, x, y_grads) + target_grads = base.gradients(y, x, y_grads) # y_grads are the input of first-order backward, # so, they are also the input of second-order backward. @@ -437,9 +437,9 @@ def triple_grad_check( x_init (numpy.array|list[numpy.array]|None): the init value for input x. y_grads (numpy.array|list[numpy.array]|None): the gradients with respect to y. x_grads_grads (numpy.array|list[numpy.array]|None): the gradients with respect to your input. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. program (Program|None): a Program with forward pass. - If None, use fluid.default_main_program(). + If None, use base.default_main_program(). eps (float): perturbation for finite differences. atol (float): absolute tolerance. rtol (float): relative tolerance. @@ -459,10 +459,10 @@ def triple_grad_check( u.persistable = True if program is None: - program = fluid.default_main_program() + program = base.default_main_program() if y_grads is None: - scope = fluid.executor.global_scope() + scope = base.executor.global_scope() y_grads = [] y_grads_init = [] for yi in y: @@ -483,10 +483,10 @@ def triple_grad_check( ] # append first order grads - target_grads = fluid.gradients(y, x, y_grads) + target_grads = base.gradients(y, x, y_grads) if x_grads_grads is None: - scope = fluid.executor.global_scope() + scope = base.executor.global_scope() x_grads_grads = [] x_grads_grads_init = [] for dxi in target_grads: @@ -511,7 +511,7 @@ def triple_grad_check( x_init += y_grads_init # append second order grads - target_grads_grads = fluid.gradients(target_grads, x, x_grads_grads) + target_grads_grads = base.gradients(target_grads, x, x_grads_grads) # filter None in target_grads_grads for Dy/Dx may be None in kernel filted = [ @@ -546,16 +546,16 @@ def get_static_double_grad( y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. dy_init (numpy.array|list[numpy.array]|None): the init value for output y. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. program (Program|None): a Program with forward pass. - If None, use fluid.default_main_program(). + If None, use base.default_main_program(). Returns: A list of numpy array that stores second derivative result calculated by static graph. """ if program is None: - program = fluid.default_main_program() - scope = fluid.executor.global_scope() + program = base.default_main_program() + scope = base.executor.global_scope() y_grads = [] for i in range(len(y)): yi = y[i] @@ -569,7 +569,7 @@ def get_static_double_grad( y_grads.append(dy) # append first order grads - dx = fluid.gradients(y, x, y_grads) + dx = base.gradients(y, x, y_grads) # y_grads are the input of first-order backward, # so, they are also the input of second-order backward. @@ -591,14 +591,14 @@ def get_static_double_grad( u.stop_gradient = False u.persistable = True if place is None: - place = fluid.CPUPlace() + place = base.CPUPlace() if program is None: - program = fluid.default_main_program() + program = base.default_main_program() # init variable in startup program - scope = fluid.executor.global_scope() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + scope = base.executor.global_scope() + exe = base.Executor(place) + exe.run(base.default_startup_program()) x_init = _as_list(x_init) # init inputs if x_init is not None @@ -628,8 +628,8 @@ def get_static_double_grad( dys.append(dy) # append second order backward - ddx = fluid.gradients(y, x, dys) - exe = fluid.Executor(place) + ddx = base.gradients(y, x, dys) + exe = base.Executor(place) # filter None in dx for DX/DY may be None in kernel # only fetch not None dx in exe.run @@ -650,7 +650,7 @@ def get_eager_double_grad( func: A wrapped dygraph function that its logic is equal to static program x_init (numpy.array|list[numpy.array]|None): the init value for input x. dy_init (numpy.array|list[numpy.array]|None): the init value for gradient of output. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. return_mid_result (bool): A flag that controls the return content. Returns: If 'return_mid_result' set True. @@ -659,9 +659,9 @@ def get_eager_double_grad( If 'return_mid_result' set False. A list of numpy array that stores second derivative result calculated by dygraph. """ - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") inputs = [] dys = [] @@ -736,7 +736,7 @@ def double_grad_check_for_dygraph( x (Variable|list[Variable]): input variables to the program. y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. atol (float): absolute tolerance. rtol (float): relative tolerance. raise_exception (bool): whether to raise an exception if @@ -804,15 +804,15 @@ def get_static_triple_grad( y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. dy_init (numpy.array|list[numpy.array]|None): the init value for output y. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. program (Program|None): a Program with forward pass. - If None, use fluid.default_main_program(). + If None, use base.default_main_program(). Returns: A list of numpy array that stores third derivative result calculated by static graph. """ if program is None: - program = fluid.default_main_program() - scope = fluid.executor.global_scope() + program = base.default_main_program() + scope = base.executor.global_scope() y_grads = [] for i in range(len(y)): yi = y[i] @@ -826,7 +826,7 @@ def get_static_triple_grad( y_grads.append(dy) # append first order grads - dx = fluid.gradients(y, x, y_grads) + dx = base.gradients(y, x, y_grads) # y_grads are the input of first-order backward, # so, they are also the input of second-order backward. @@ -855,7 +855,7 @@ def get_eager_triple_grad( func: A wrapped dygraph function that its logic is equal to static program x_init (numpy.array|list[numpy.array]|None): the init value for input x. dy_init (numpy.array|list[numpy.array]|None): the init value for gradient of output. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. return_mid_result (list[Tensor], list[Tensor]): If set True, the Returns: A list of numpy array that stores second derivative result calculated by dygraph @@ -899,7 +899,7 @@ def triple_grad_check_for_dygraph( x (Variable|list[Variable]): input variables to the program. y (Variable|list[Variable]): output variables to the program. x_init (numpy.array|list[numpy.array]|None): the init value for input x. - place (fluid.CPUPlace or fluid.CUDAPlace): the device. + place (base.CPUPlace or base.CUDAPlace): the device. atol (float): absolute tolerance. rtol (float): relative tolerance. raise_exception (bool): whether to raise an exception if diff --git a/test/legacy_test/ir_memory_optimize_net_base.py b/test/legacy_test/ir_memory_optimize_net_base.py index 795852e7b0341..5404e28ef839b 100644 --- a/test/legacy_test/ir_memory_optimize_net_base.py +++ b/test/legacy_test/ir_memory_optimize_net_base.py @@ -21,8 +21,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import compiler, core +from paddle import base +from paddle.base import compiler, core # open eager delete mode os.environ['FLAGS_eager_delete_tensor_gb'] = '0.0' @@ -56,8 +56,8 @@ def check_network_convergence( 'Skip use_parallel_executor=True because Paddle comes without parallel support on windows' ) return - fluid.default_startup_program().random_seed = 100 - fluid.default_main_program().random_seed = 100 + base.default_startup_program().random_seed = 100 + base.default_main_program().random_seed = 100 data = paddle.static.data( name="words", shape=[-1, 1], dtype="int64", lod_level=1 @@ -68,19 +68,19 @@ def check_network_convergence( cost = network(data, label, len(self.word_dict)) optimizer = paddle.optimizer.Adam(learning_rate=0.001) optimizer.minimize(cost) - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.enable_inplace = enable_inplace build_strategy.memory_optimize = use_ir_memory_optimize # execution - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=[data, label], place=place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + feeder = base.DataFeeder(feed_list=[data, label], place=place) reader = feeder.feed(self.train_reader()) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) train_cp = compiler.CompiledProgram( - fluid.default_main_program(), build_strategy=build_strategy + base.default_main_program(), build_strategy=build_strategy ) fetch_list = [cost.name] @@ -127,8 +127,8 @@ def test_network(self): self.setup_reader() - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(core.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(core.Scope()): ( baseline_first_loss, baseline_last_loss, diff --git a/test/legacy_test/jit_load_rename_var.py b/test/legacy_test/jit_load_rename_var.py index 645e3faf37760..c7d74522de496 100644 --- a/test/legacy_test/jit_load_rename_var.py +++ b/test/legacy_test/jit_load_rename_var.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid import unique_name -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle.base import unique_name +from paddle.base.dygraph.base import switch_to_static_graph @switch_to_static_graph diff --git a/test/legacy_test/nets.py b/test/legacy_test/nets.py index 16a947b221e8b..621ad4b462cca 100644 --- a/test/legacy_test/nets.py +++ b/test/legacy_test/nets.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from paddle.fluid.data_feeder import check_variable_and_dtype, convert_dtype +from paddle.base.data_feeder import check_variable_and_dtype, convert_dtype from paddle.utils import deprecated __all__ = [ @@ -46,7 +46,7 @@ def simple_img_conv_pool( r""" :api_attr: Static Graph - The simple_img_conv_pool api is composed of :ref:`api_fluid_layers_conv2d` and :ref:`api_fluid_layers_pool2d` . + The simple_img_conv_pool api is composed of :ref:`api_base_layers_conv2d` and :ref:`api_base_layers_pool2d` . Args: input (Variable): 4-D Tensor, shape is [N, C, H, W], data type can be float32 or float64. @@ -106,11 +106,11 @@ def simple_img_conv_pool( Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() img = paddle.static.data(name='img', shape=[100, 1, 28, 28], dtype='float32') - conv_pool = fluid.nets.simple_img_conv_pool(input=img, + conv_pool = base.nets.simple_img_conv_pool(input=img, filter_size=5, num_filters=20, pool_size=2, @@ -208,12 +208,12 @@ def img_conv_group( Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() img = paddle.static.data(name='img', shape=[None, 1, 28, 28], dtype='float32') - conv_pool = fluid.nets.img_conv_group(input=img, + conv_pool = base.nets.img_conv_group(input=img, conv_padding=1, conv_num_filter=[3, 3], conv_filter_size=3, @@ -288,10 +288,10 @@ def sequence_conv_pool( :api_attr: Static Graph **This api takes input as an LoDTensor. If input is a Tensor, please use** - :ref:`api_fluid_nets_simple_img_conv_pool` **instead** + :ref:`api_base_nets_simple_img_conv_pool` **instead** - The sequence_conv_pool is composed of :ref:`api_fluid_layers_sequence_conv` - and :ref:`api_fluid_layers_sequence_pool` . + The sequence_conv_pool is composed of :ref:`api_base_layers_sequence_conv` + and :ref:`api_base_layers_sequence_pool` . Args: input (Tensor): 2-D LoDTensor, the input of sequence_conv, @@ -323,7 +323,7 @@ def sequence_conv_pool( Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() input_dim = 100 #len(word_dict) @@ -331,7 +331,7 @@ def sequence_conv_pool( hid_dim = 512 data = paddle.static.data(name="words", shape=[None, 1], dtype="int64", lod_level=1) emb = paddle.static.nn.embedding(input=data, size=[input_dim, emb_dim], is_sparse=True) - seq_conv = fluid.nets.sequence_conv_pool(input=emb, + seq_conv = base.nets.sequence_conv_pool(input=emb, num_filters=hid_dim, filter_size=3, act="tanh", @@ -359,8 +359,8 @@ def glu(input, dim=-1): r""" :api_attr: Static Graph - The Gated Linear Units(GLU) composed by :ref:`api_fluid_layers_split` , - :ref:`api_fluid_layers_sigmoid` and :ref:`api_fluid_layers_elementwise_mul` . + The Gated Linear Units(GLU) composed by :ref:`api_base_layers_split` , + :ref:`api_base_layers_sigmoid` and :ref:`api_base_layers_elementwise_mul` . Specifically, GLU will plit the input into two equal-sized parts, :math:`a` and :math:`b`, along the given dimension and then compute as following: @@ -385,14 +385,14 @@ def glu(input, dim=-1): Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() data = paddle.static.data( name="words", shape=[-1, 6, 3, 9], dtype="float32") # shape of output: [-1, 3, 3, 9] - output = fluid.nets.glu(input=data, dim=1) + output = base.nets.glu(input=data, dim=1) """ check_variable_and_dtype( input, 'input', ['float16', 'float32', 'float64'], "glu" @@ -428,7 +428,7 @@ def scaled_dot_product_attention( Note that the implementation is adapted to batch, and all matrix multiplication in :math:`Attention(Q, K, V)` is batched matrix multiplication. Refer to - :ref:`api_fluid_layers_matmul` . + :ref:`api_base_layers_matmul` . Args: queries (Variable): A 3-D Tensor with shape :math:`[N, L_q, d_k \\times h]` , @@ -466,14 +466,14 @@ def scaled_dot_product_attention( Examples: .. code-block:: python - import paddle.fluid as fluid + import paddle.base as base import paddle paddle.enable_static() queries = paddle.static.data(name="queries", shape=[3, 5, 9], dtype="float32") keys = paddle.static.data(name="keys", shape=[3, 6, 9], dtype="float32") values = paddle.static.data(name="values", shape=[3, 6, 10], dtype="float32") - contexts = fluid.nets.scaled_dot_product_attention(queries, keys, values) + contexts = base.nets.scaled_dot_product_attention(queries, keys, values) contexts.shape # [3, 5, 10] """ check_variable_and_dtype( diff --git a/test/legacy_test/nproc_process.py b/test/legacy_test/nproc_process.py index 0c98c4a45dfd6..bee588de40bd4 100644 --- a/test/legacy_test/nproc_process.py +++ b/test/legacy_test/nproc_process.py @@ -15,11 +15,11 @@ import os import sys -from paddle import fluid +from paddle import base def train(prefix): - if fluid.core.is_compiled_with_xpu(): + if base.core.is_compiled_with_xpu(): selected_devices = os.getenv("FLAGS_selected_xpus") else: selected_devices = os.getenv("FLAGS_selected_gpus") diff --git a/test/legacy_test/op.py b/test/legacy_test/op.py index 6adc9daa52a09..0dec2f001188e 100644 --- a/test/legacy_test/op.py +++ b/test/legacy_test/op.py @@ -14,8 +14,8 @@ import numpy as np -from paddle.fluid import core -from paddle.fluid.proto import framework_pb2 +from paddle.base import core +from paddle.base.proto import framework_pb2 # NOTE: this is added to support creating a Scalar message diff --git a/test/legacy_test/parallel_dygraph_mnist.py b/test/legacy_test/parallel_dygraph_mnist.py index f3c20bc5c653b..f8908a0338508 100644 --- a/test/legacy_test/parallel_dygraph_mnist.py +++ b/test/legacy_test/parallel_dygraph_mnist.py @@ -19,7 +19,7 @@ ) import paddle -from paddle.fluid.dygraph.base import to_variable +from paddle.base.dygraph.base import to_variable class SimpleImgConvPool(paddle.nn.Layer): diff --git a/test/legacy_test/parallel_dygraph_shared_unused_var.py b/test/legacy_test/parallel_dygraph_shared_unused_var.py index 46f98854547f0..f51a4f765859e 100644 --- a/test/legacy_test/parallel_dygraph_shared_unused_var.py +++ b/test/legacy_test/parallel_dygraph_shared_unused_var.py @@ -24,7 +24,7 @@ ) import paddle -from paddle.fluid.dygraph.base import to_variable +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear np.random.seed(2021) diff --git a/test/legacy_test/parallel_dygraph_sparse_embedding.py b/test/legacy_test/parallel_dygraph_sparse_embedding.py index 7236b05ffd32e..eaeb9bed5b78f 100644 --- a/test/legacy_test/parallel_dygraph_sparse_embedding.py +++ b/test/legacy_test/parallel_dygraph_sparse_embedding.py @@ -24,8 +24,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.nn import Embedding @@ -48,14 +48,14 @@ def __init__( self.vocab_size, self.hidden_size, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale ) ), ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.vocab_size], dtype=dtype, default_initializer=paddle.nn.initializer.Uniform( @@ -63,7 +63,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype=dtype, default_initializer=paddle.nn.initializer.Uniform( diff --git a/test/legacy_test/parallel_executor_test_base.py b/test/legacy_test/parallel_executor_test_base.py index 65034bdb1dd69..9cf5b00324f3a 100644 --- a/test/legacy_test/parallel_executor_test_base.py +++ b/test/legacy_test/parallel_executor_test_base.py @@ -23,8 +23,8 @@ from feed_data_reader import FeedDataReader import paddle -from paddle import fluid -from paddle.fluid import compiler, core +from paddle import base +from paddle.base import compiler, core __all__ = ['TestParallelExecutorBase'] DeviceType = core.DeviceType @@ -71,22 +71,22 @@ def run_executor(exe, binary, feed, fetch_list): paddle.seed(0) paddle.framework.random._manual_program_seed(0) - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): feed_dict, loss = cls.build_model( feed_dict, get_data_from_feeder, main, method, optimizer ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if use_device == DeviceType.CUDA - else fluid.XPUPlace(0) + else base.XPUPlace(0) if use_device == DeviceType.XPU - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) build_strategy, exec_strategy = cls.set_strategy( @@ -112,9 +112,9 @@ def run_executor(exe, binary, feed, fetch_list): if batch_size is not None: batch_size *= ( - fluid.core.get_cuda_device_count() + base.core.get_cuda_device_count() if use_device == DeviceType.CUDA - else fluid.core.get_xpu_device_count() + else base.core.get_xpu_device_count() if use_device == DeviceType.XPU else int(os.environ.get('CPU_NUM', multiprocessing.cpu_count())) ) @@ -171,21 +171,21 @@ def check_pass_conflict( use_fast_executor=True, enable_sequential_execution=False, ): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): feed_dict, loss = cls.build_model( feed_dict, get_data_from_feeder, main, method, optimizer ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if use_device == DeviceType.CUDA - else fluid.XPUPlace(0) + else base.XPUPlace(0) if use_device == DeviceType.XPU - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) build_strategy, exec_strategy = cls.set_strategy( @@ -222,14 +222,14 @@ def set_strategy( use_reduce, use_device, ): - exec_strategy = fluid.ExecutionStrategy() + exec_strategy = base.ExecutionStrategy() if use_fast_executor: exec_strategy.use_experimental_executor = True - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.reduce_strategy = ( - fluid.BuildStrategy.ReduceStrategy.Reduce + base.BuildStrategy.ReduceStrategy.Reduce if use_reduce - else fluid.BuildStrategy.ReduceStrategy.AllReduce + else base.BuildStrategy.ReduceStrategy.AllReduce ) build_strategy.fuse_elewise_add_act_ops = fuse_elewise_add_act_ops build_strategy.fuse_relu_depthwise_conv = fuse_relu_depthwise_conv diff --git a/test/legacy_test/prim_op_test.py b/test/legacy_test/prim_op_test.py index cca23904a922d..c41cfc05d79ca 100644 --- a/test/legacy_test/prim_op_test.py +++ b/test/legacy_test/prim_op_test.py @@ -20,8 +20,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle.base import core +from paddle.base.framework import ( OpProtoHolder, _dygraph_tracer, canonicalize_attrs, @@ -401,7 +401,7 @@ def init_checker_threshold(self): def check(self): if ( - type(self.place) is paddle.fluid.libpaddle.CUDAPlace + type(self.place) is paddle.base.libpaddle.CUDAPlace and not paddle.is_compiled_with_cuda() ): return @@ -417,9 +417,9 @@ def check(self): def get_kernel_sig(self): paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") ( eager_tensor_inputs, @@ -437,9 +437,9 @@ def get_kernel_sig(self): def get_eager_desire(self): paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") ( eager_tensor_inputs, @@ -578,7 +578,7 @@ def check_static_comp(self): # forward comp only for comp op if self.prim_op_type == "prim": return - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): core._set_prim_forward_enabled(self.enable_fw_comp) startup_program, main_program = ( paddle.static.Program(), @@ -655,9 +655,9 @@ def check_jit_comp(self): if self.prim_op_type == "prim": return paddle.disable_static() - if type(self.place) == paddle.fluid.libpaddle.CPUPlace: + if type(self.place) == paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) == paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) == paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") atol = self.fw_comp_atol if self.enable_fw_comp else self.jit_comp_atol rtol = self.fw_comp_rtol if self.enable_fw_comp else self.jit_comp_rtol @@ -734,7 +734,7 @@ def check_jit_comp_with_cinn(self): return # cinn doesn't support cpu place if ( - type(self.place) == paddle.fluid.libpaddle.CPUPlace + type(self.place) == paddle.base.libpaddle.CPUPlace and self.enable_cinn and core.is_compiled_with_cinn() ): @@ -751,9 +751,9 @@ def check_jit_comp_with_cinn(self): else self.fw_comp_rtol ) core._set_prim_forward_enabled(self.enable_fw_comp) - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") ( eager_tensor_inputs, @@ -848,7 +848,7 @@ def init(self): def check(self): if ( - type(self.place) is paddle.fluid.libpaddle.CUDAPlace + type(self.place) is paddle.base.libpaddle.CUDAPlace and not paddle.is_compiled_with_cuda() ): return @@ -925,9 +925,9 @@ def gen_no_grad_set(self, var_dict): def get_eager_desire(self): paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") ( eager_tensor_inputs, @@ -978,9 +978,9 @@ def check_eager_comp(self): if self.prim_op_type == "comp": return paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") atol = self.rev_comp_atol rtol = self.rev_comp_rtol @@ -1027,7 +1027,7 @@ def check_static_comp(self): core._set_prim_backward_enabled(self.enable_rev_comp) atol = self.rev_comp_atol if self.enable_rev_comp else self.fw_comp_atol rtol = self.rev_comp_rtol if self.enable_rev_comp else self.fw_comp_rtol - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): startup_program, main_program = ( paddle.static.Program(), paddle.static.Program(), @@ -1130,9 +1130,9 @@ def check_static_comp(self): def check_jit_comp(self): paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") if self.prim_op_type == "prim": core._set_prim_backward_enabled(self.enable_rev_comp) @@ -1247,15 +1247,15 @@ def check_jit_comp(self): def check_jit_comp_with_cinn(self): # cinn doesn't support cpu place if ( - type(self.place) is paddle.fluid.libpaddle.CPUPlace + type(self.place) is paddle.base.libpaddle.CPUPlace and self.enable_cinn and core.is_compiled_with_cinn() ): return paddle.disable_static() - if type(self.place) is paddle.fluid.libpaddle.CPUPlace: + if type(self.place) is paddle.base.libpaddle.CPUPlace: paddle.device.set_device("cpu") - if type(self.place) is paddle.fluid.libpaddle.CUDAPlace: + if type(self.place) is paddle.base.libpaddle.CUDAPlace: paddle.device.set_device("gpu:0") if self.prim_op_type == "prim": core._set_prim_backward_enabled(self.enable_rev_comp) diff --git a/test/legacy_test/seresnext_net.py b/test/legacy_test/seresnext_net.py index 1cfda9d1d50a5..357b5b7e226b1 100644 --- a/test/legacy_test/seresnext_net.py +++ b/test/legacy_test/seresnext_net.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import fluid +from paddle import base -fluid.core._set_eager_deletion_mode(-1, -1, False) +base.core._set_eager_deletion_mode(-1, -1, False) import os @@ -45,7 +45,7 @@ def squeeze_excitation(input, num_channels, reduction_ratio): - # pool = fluid.layers.pool2d( + # pool = base.layers.pool2d( # input=input, pool_size=0, pool_type='avg', global_pooling=True) conv = input shape = conv.shape diff --git a/test/legacy_test/seresnext_test_base.py b/test/legacy_test/seresnext_test_base.py index bed6180caea74..73ad9c27c0196 100644 --- a/test/legacy_test/seresnext_test_base.py +++ b/test/legacy_test/seresnext_test_base.py @@ -16,7 +16,7 @@ import seresnext_net from parallel_executor_test_base import DeviceType, TestParallelExecutorBase -from paddle.fluid import core +from paddle.base import core class TestResnetBase(TestParallelExecutorBase): diff --git a/test/legacy_test/simple_nets.py b/test/legacy_test/simple_nets.py index 8ff57cdce22db..0a0ab0f298353 100644 --- a/test/legacy_test/simple_nets.py +++ b/test/legacy_test/simple_nets.py @@ -15,7 +15,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def simple_fc_net_with_inputs(img, label, class_num=10): @@ -25,7 +25,7 @@ def simple_fc_net_with_inputs(img, label, class_num=10): hidden, size=100, activation='relu', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -52,7 +52,7 @@ def batchnorm_fc_with_inputs(img, label, class_num=10): hidden, size=200, activation='relu', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -87,7 +87,7 @@ def bow_net( """ BOW net This model is from https://github.com/PaddlePaddle/models: - fluid/PaddleNLP/text_classification/nets.py + base/PaddleNLP/text_classification/nets.py """ data = paddle.static.data( name="words", shape=[-1, 1], dtype="int64", lod_level=1 diff --git a/test/legacy_test/static_model_parallel_fused_attention.py b/test/legacy_test/static_model_parallel_fused_attention.py index 5110b0aa255c9..981dda453f87f 100644 --- a/test/legacy_test/static_model_parallel_fused_attention.py +++ b/test/legacy_test/static_model_parallel_fused_attention.py @@ -16,7 +16,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.incubate.nn import FusedMultiHeadAttention @@ -116,7 +116,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/legacy_test/static_model_parallel_fused_feedforward.py b/test/legacy_test/static_model_parallel_fused_feedforward.py index dcd502e58f89d..8f1406322ab8e 100644 --- a/test/legacy_test/static_model_parallel_fused_feedforward.py +++ b/test/legacy_test/static_model_parallel_fused_feedforward.py @@ -16,7 +16,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.incubate.nn import FusedFeedForward @@ -107,7 +107,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/legacy_test/static_model_parallel_fused_multi_transformer.py b/test/legacy_test/static_model_parallel_fused_multi_transformer.py index 4979fee8f9b99..1da41d0e0dd21 100644 --- a/test/legacy_test/static_model_parallel_fused_multi_transformer.py +++ b/test/legacy_test/static_model_parallel_fused_multi_transformer.py @@ -16,7 +16,7 @@ from test_dist_base import TestDistRunnerBase, runtime_main import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.incubate.nn import FusedMultiTransformer @@ -149,7 +149,7 @@ def get_model(self, batch_size=2, use_dgc=False, dist_strategy=None): ) if dist_strategy: - data_loader = fluid.io.DataLoader.from_generator( + data_loader = base.io.DataLoader.from_generator( feed_list=[data_in], capacity=64, use_double_buffer=False, diff --git a/test/legacy_test/test_accuracy_op.py b/test/legacy_test/test_accuracy_op.py index ae60df680e2b7..e722b6912c24d 100755 --- a/test/legacy_test/test_accuracy_op.py +++ b/test/legacy_test/test_accuracy_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def accuracy_wrapper(infer, indices, label): @@ -111,8 +111,8 @@ def test_type_errors(self): with paddle_static_guard(): with program_guard(Program(), Program()): # The input type of accuracy_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) label = paddle.static.data( name='label', shape=[-1, 1], dtype="int32" @@ -175,7 +175,7 @@ def test_api(self): class TestAccuracyAPI2(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): predictions = paddle.to_tensor( [[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32', @@ -188,7 +188,7 @@ def test_api(self): class TestAccuracyAPI(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): predictions = paddle.to_tensor( [[0.2, 0.1, 0.4, 0.1, 0.1], [0.2, 0.3, 0.1, 0.15, 0.25]], dtype='float32', diff --git a/test/legacy_test/test_activation_nn_grad.py b/test/legacy_test/test_activation_nn_grad.py index 58a2085a3a880..0c7742fa9c57d 100644 --- a/test/legacy_test/test_activation_nn_grad.py +++ b/test/legacy_test/test_activation_nn_grad.py @@ -20,8 +20,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestSigmoidTripleGradCheck(unittest.TestCase): @@ -41,9 +41,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -71,9 +71,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -92,7 +92,7 @@ def func(self, place): y = paddle.tanh(x) x_arr = np.random.random(shape).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.triple_grad_check( @@ -105,9 +105,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -126,7 +126,7 @@ def func(self, place): y = paddle.tanh(x) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.double_grad_check( @@ -139,9 +139,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -169,9 +169,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -195,9 +195,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -229,9 +229,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places = [fluid.CUDAPlace(0)] + places = [base.CUDAPlace(0)] for p in places: self.func(p) @@ -263,9 +263,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -297,9 +297,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -332,9 +332,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -364,9 +364,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places = [fluid.CUDAPlace(0)] + places = [base.CUDAPlace(0)] for p in places: self.func(p) @@ -396,9 +396,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places = [fluid.CUDAPlace(0)] + places = [base.CUDAPlace(0)] for p in places: self.func(p) @@ -428,9 +428,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -460,9 +460,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -490,9 +490,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -520,9 +520,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -549,9 +549,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -578,9 +578,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -608,9 +608,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -637,9 +637,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -666,9 +666,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -695,9 +695,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -725,9 +725,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_activation_op.py b/test/legacy_test/test_activation_op.py index e9c6ef7b1985d..a7fe2cf3f602f 100644 --- a/test/legacy_test/test_activation_op.py +++ b/test/legacy_test/test_activation_op.py @@ -23,9 +23,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid, static -from paddle.fluid import Program, core, program_guard -from paddle.fluid.layer_helper import LayerHelper +from paddle import base, static +from paddle.base import Program, core, program_guard +from paddle.base.layer_helper import LayerHelper @contextmanager @@ -39,7 +39,7 @@ def dynamic_guad(): class TestSqrtOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program(), Program()): # The input type of sqrt op must be Variable or numpy.ndarray. in1 = 1 @@ -70,7 +70,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.exp(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -120,7 +120,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.exp(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.if_enable_cinn() self.convert_input_output() @@ -156,7 +156,7 @@ def init_shape(self): class Test_Exp_Op_Fp16(unittest.TestCase): def test_api_fp16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -194,7 +194,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.expm1(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -226,7 +226,7 @@ def setUp(self): def test_static_api(self): def run(place): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): X = paddle.static.data('X', self.shape, dtype=self.dtype) out = paddle.expm1(X) @@ -266,23 +266,23 @@ def test_api_int(self): class TestParameter: def test_out_name(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): np_x = np.array([0.1]).astype('float32').reshape((-1, 1)) data = paddle.static.data( name="X", shape=[-1, 1], dtype="float32" ) out = eval("paddle.%s(data, name='Y')" % self.op_type) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) (result,) = exe.run(feed={"X": np_x}, fetch_list=[out]) expected = eval("np.%s(np_x)" % self.op_type) np.testing.assert_allclose(result, expected, rtol=1e-05) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([0.1]) - x = fluid.dygraph.to_variable(np_x) + x = base.dygraph.to_variable(np_x) z = eval("paddle.%s(x).numpy()" % self.op_type) z_expected = eval("np.%s(np_x)" % self.op_type) np.testing.assert_allclose(z, z_expected, rtol=1e-05) @@ -301,7 +301,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = 1 / (1 + np.exp(-x)) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -341,7 +341,7 @@ def setUp(self): out = 1 / (1 + np.exp(-x)) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(convert_float_to_uint16(x)) + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(x)) } self.outputs = {'Out': convert_float_to_uint16(out)} @@ -390,7 +390,7 @@ def setUp(self): + 1j * np.random.uniform(-1, 1, self.shape) ).astype(self.dtype) out = x / (np.exp(-x) + 1) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -435,7 +435,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [11, 17]) out1 = F.silu(x) @@ -459,7 +459,7 @@ def test_dygraph_api(self): paddle.enable_static() def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.silu, 1) @@ -491,7 +491,7 @@ def setUp(self): else: x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.log(1 / (1 + np.exp(-x))) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -529,7 +529,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [11, 17]) out1 = F.log_sigmoid(x) @@ -553,7 +553,7 @@ def test_dygraph_api(self): paddle.enable_static() def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.log_sigmoid, 1) @@ -587,7 +587,7 @@ def setUp(self): + 1j * np.random.uniform(-1, 1, self.shape) ).astype(self.dtype) out = np.tanh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -642,7 +642,7 @@ def executed_api(self): self.tanh = F.tanh def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], self.dtype) out1 = self.tanh(x) @@ -666,7 +666,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, self.tanh, 1) @@ -699,7 +699,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.arctan(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -709,23 +709,23 @@ def test_check_grad(self): self.check_grad(['X'], 'Out') def test_out_name(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): np_x = np.array([0.1]).astype('float32').reshape((-1, 1)) data = paddle.static.data( name="X", shape=[-1, 1], dtype="float32" ) out = paddle.atan(data, name='Y') - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) (result,) = exe.run(feed={"X": np_x}, fetch_list=[out]) expected = np.arctan(np_x) self.assertEqual(result, expected) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([0.1]) - x = fluid.dygraph.to_variable(np_x) + x = base.dygraph.to_variable(np_x) z = paddle.atan(x).numpy() z_expected = np.arctan(np_x) self.assertEqual(z, z_expected) @@ -746,7 +746,7 @@ def setUp(self): np.random.seed(1024) x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.sinh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -764,17 +764,17 @@ def init_shape(self): class TestSinhAPI(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([0.1]) - x = fluid.dygraph.to_variable(np_x) + x = base.dygraph.to_variable(np_x) z = paddle.sinh(x).numpy() z_expected = np.sinh(np_x) np.testing.assert_allclose(z, z_expected, rtol=1e-05) def test_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): test_data_shape = [11, 17] - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = np.random.uniform(0.1, 1, test_data_shape).astype( "float32" ) @@ -785,10 +785,10 @@ def test_api(self): ) pd_sinh_out = paddle.sinh(data_x) - exe = fluid.Executor(place=fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place=base.CPUPlace()) + exe.run(base.default_startup_program()) (np_sinh_res,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data_x": input_x}, fetch_list=[pd_sinh_out], ) @@ -798,11 +798,11 @@ def test_api(self): def test_backward(self): test_data_shape = [11, 17] - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_x = np.random.uniform(0.1, 1, test_data_shape).astype( "float32" ) - var = fluid.dygraph.to_variable(input_x) + var = base.dygraph.to_variable(input_x) var.stop_gradient = False loss = paddle.sinh(var) loss.backward() @@ -812,7 +812,7 @@ def test_backward(self): class TestSinhOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program()): # The input type must be Variable. self.assertRaises(TypeError, paddle.sinh, 1) @@ -838,7 +838,7 @@ def setUp(self): np.random.seed(1024) x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.cosh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -856,17 +856,17 @@ def init_shape(self): class TestCoshAPI(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([0.1]) - x = fluid.dygraph.to_variable(np_x) + x = base.dygraph.to_variable(np_x) z = paddle.cosh(x).numpy() z_expected = np.cosh(np_x) np.testing.assert_allclose(z, z_expected, rtol=1e-05) def test_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): test_data_shape = [11, 17] - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = np.random.uniform(0.1, 1, test_data_shape).astype( "float32" ) @@ -877,10 +877,10 @@ def test_api(self): ) pd_cosh_out = paddle.cosh(data_x) - exe = fluid.Executor(place=fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place=base.CPUPlace()) + exe.run(base.default_startup_program()) (np_cosh_res,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data_x": input_x}, fetch_list=[pd_cosh_out], ) @@ -890,11 +890,11 @@ def test_api(self): def test_backward(self): test_data_shape = [11, 17] - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_x = np.random.uniform(0.1, 1, test_data_shape).astype( "float32" ) - var = fluid.dygraph.to_variable(input_x) + var = base.dygraph.to_variable(input_x) var.stop_gradient = False loss = paddle.cosh(var) loss.backward() @@ -904,7 +904,7 @@ def test_backward(self): class TestCoshOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program()): # The input type must be Variable. self.assertRaises(TypeError, paddle.cosh, 1) @@ -935,7 +935,7 @@ def setUp(self): np.random.seed(1024) x = np.random.uniform(10, 20, self.shape).astype(self.dtype) out = ref_tanhshrink(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -963,7 +963,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.tanhshrink(x) @@ -986,7 +986,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.tanhshrink, 1) @@ -1020,7 +1020,7 @@ def setUp(self): np.random.seed(1024) x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) * 10 out = ref_hardshrink(x, self.threshold) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'threshold': self.threshold} @@ -1064,7 +1064,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = F.hardshrink(x) @@ -1094,7 +1094,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.hardshrink, 1) @@ -1130,7 +1130,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = F.hardtanh(x) @@ -1160,7 +1160,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.hardtanh, 1) @@ -1197,7 +1197,7 @@ def setUp(self): x = np.random.uniform(0.25, 10, self.shape).astype(self.dtype) out = ref_softshrink(x, threshold) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"lambda": threshold} @@ -1226,7 +1226,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.softshrink(x, self.threshold) @@ -1249,7 +1249,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.softshrink, 1) @@ -1285,7 +1285,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.sqrt(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1314,7 +1314,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.sqrt(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def test_check_grad(self): @@ -1356,7 +1356,7 @@ def setUp(self): out = np.sqrt(x) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(convert_float_to_uint16(x)) + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(x)) } self.outputs = {'Out': convert_float_to_uint16(out)} @@ -1392,7 +1392,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.sqrt(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1421,7 +1421,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.sqrt(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def if_enable_cinn(self): @@ -1458,7 +1458,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = 1.0 / np.sqrt(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1509,7 +1509,7 @@ def setUp(self): x[np.abs(x) < 0.005] = 0.02 out = np.abs(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1541,7 +1541,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.ceil(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1572,7 +1572,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.floor(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1626,7 +1626,7 @@ def setUp(self): + 1j * np.random.uniform(-1, 1, self.shape) ).astype(self.dtype) out = np.cos(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1686,7 +1686,7 @@ def setUp(self): out = np.tan(self.x_np) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x_np)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x_np)} self.outputs = {'Out': out} self.convert_input_output() @@ -1738,7 +1738,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, out_test.numpy(), rtol=1e-05) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [11, 17], self.dtype) out = paddle.tan(x) @@ -1749,7 +1749,7 @@ def test_static_api(self): def test_backward(self): test_data_shape = [11, 17] - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_x = np.random.uniform(0.1, 1, test_data_shape).astype( "float32" ) @@ -1772,7 +1772,7 @@ def setUp(self): x = np.random.uniform(-0.95, 0.95, self.shape).astype(self.dtype) out = np.arccos(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1808,7 +1808,7 @@ def setUp(self): + 1j * np.random.uniform(-1, 1, self.shape) ).astype(self.dtype) out = np.sin(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1854,7 +1854,7 @@ def setUp(self): x = np.random.uniform(-0.95, 0.95, self.shape).astype(self.dtype) out = np.arcsin(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1883,7 +1883,7 @@ def setUp(self): x = np.random.uniform(2, 3, self.shape).astype(self.dtype) out = np.arccosh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1912,7 +1912,7 @@ def setUp(self): x = np.random.uniform(1, 2, self.shape).astype(self.dtype) out = np.arcsinh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1941,7 +1941,7 @@ def setUp(self): x = np.random.uniform(-0.9, 0.9, self.shape).astype(self.dtype) out = np.arctanh(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -1970,7 +1970,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.round(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -2039,7 +2039,7 @@ def executed_api(self): self.relu = F.relu def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = self.relu(x) @@ -2062,8 +2062,8 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, self.relu, 1) @@ -2163,7 +2163,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = F.leaky_relu(x) @@ -2193,7 +2193,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.leaky_relu, 1) @@ -2272,7 +2272,7 @@ def setUp(self): out = gelu(x, approximate) self.if_enable_cinn() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() self.attrs = {"approximate": approximate} @@ -2319,7 +2319,7 @@ def setUp(self): self.rev_comp_atol = 1e-8 def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [11, 17], dtype="float32") out1 = F.gelu(x) @@ -2349,7 +2349,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.gelu, 1) @@ -2382,7 +2382,7 @@ def setUp(self): t[t < t_min] = t_min t[t > t_max] = t_max - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': t} self.convert_input_output() self.attrs = {'t_min': t_min, 't_max': t_max} @@ -2414,7 +2414,7 @@ def setUp(self): self.attrs = {'threshold': 6.0} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -2445,7 +2445,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.relu6(x) @@ -2467,18 +2467,18 @@ def test_dygraph_api(self): for r in [out1, out2]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.nn.functional.relu6(x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_relu6(self.x_np) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.relu6, 1) @@ -2496,7 +2496,7 @@ def test_errors(self): class TestRelu6APIWarnings(unittest.TestCase): def test_warnings(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with warnings.catch_warnings(record=True) as context: warnings.simplefilter("always") @@ -2550,7 +2550,7 @@ def setUp(self): x[np.abs(x - threshold + offset) < 0.005] = threshold - offset + 0.02 out = ref_hardswish(x, threshold, scale, offset) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() self.attrs = {'threshold': threshold, 'scale': scale, 'offset': offset} @@ -2589,7 +2589,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.hardswish(x) @@ -2611,12 +2611,12 @@ def test_dygraph_api(self): for r in [out1, out2]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.nn.functional.hardswish(x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_hardswish(self.x_np) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) @@ -2627,7 +2627,7 @@ def test_fluid_api(self): np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.hardswish, 1) @@ -2659,7 +2659,7 @@ def setUp(self): t[t > threshold] = threshold out = np.log(np.exp(t) + 1) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() self.attrs = {'threshold': threshold} @@ -2694,7 +2694,7 @@ def setUp(self): # Note: unlike other Relu extensions, point 0 on standard ELU function (i.e. alpha = 1) # is differentiable, so we can skip modifications like x[np.abs(x) < 0.005] = 0.02 here - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() self.attrs = {'alpha': alpha} @@ -2737,7 +2737,7 @@ def executed_api(self): self.elu = F.elu def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = self.elu(x) @@ -2769,7 +2769,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, self.elu, 1) @@ -2813,7 +2813,7 @@ def setUp(self): alpha = 1.5 out = celu(x, alpha) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() self.attrs = {'alpha': alpha} @@ -2848,7 +2848,7 @@ def executed_api(self): self.celu = F.celu def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out1 = self.celu(x, 1.5) @@ -2880,7 +2880,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, self.celu, 1) @@ -2912,7 +2912,7 @@ def setUp(self): x = np.random.uniform(1, 2, self.shape).astype(self.dtype) out = np.reciprocal(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -2944,7 +2944,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.log(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -2959,7 +2959,7 @@ def test_check_grad(self): class Test_Log_Op_Fp16(unittest.TestCase): def test_api_fp16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -2972,7 +2972,7 @@ def test_api_fp16(self): (res,) = exe.run(fetch_list=[out]) def test_api_bf16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3012,7 +3012,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.log2(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3022,7 +3022,7 @@ def test_check_grad(self): self.check_grad(['X'], 'Out') def test_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3032,7 +3032,7 @@ def test_api(self): ) out1 = paddle.log2(data_x) - exe = paddle.static.Executor(place=fluid.CPUPlace()) + exe = paddle.static.Executor(place=base.CPUPlace()) exe.run(paddle.static.default_startup_program()) (res1,) = exe.run( paddle.static.default_main_program(), @@ -3043,7 +3043,7 @@ def test_api(self): np.testing.assert_allclose(res1, expected_res, rtol=1e-05) # dygraph - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64") data_x = paddle.to_tensor(np_x) z = paddle.log2(data_x) @@ -3069,7 +3069,7 @@ def test_api_int(self): paddle.enable_static() def test_api_bf16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3092,7 +3092,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.log10(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3119,7 +3119,7 @@ def test_api_int(self): paddle.enable_static() def test_api_bf16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3134,7 +3134,7 @@ def test_api_bf16(self): class TestLog10API(unittest.TestCase): def test_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3155,7 +3155,7 @@ def test_api(self): np.testing.assert_allclose(res1, expected_res, rtol=1e-05) # dygraph - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64") data_x = paddle.to_tensor(np_x) z = paddle.log10(data_x) @@ -3175,7 +3175,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.log1p(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3187,7 +3187,7 @@ def test_check_grad(self): class Test_Log1p_Op_Fp16(unittest.TestCase): def test_api_fp16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3212,7 +3212,7 @@ def test_api_int(self): paddle.enable_static() def test_api_bf16(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with static.program_guard( paddle.static.Program(), paddle.static.Program() ): @@ -3232,8 +3232,8 @@ def init_shape(self): class TestLog1pAPI(unittest.TestCase): def test_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program(), base.Program()): input_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64") data_x = paddle.static.data( name="data_x", @@ -3242,10 +3242,10 @@ def test_api(self): ) out1 = paddle.log1p(data_x) - exe = fluid.Executor(place=fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place=base.CPUPlace()) + exe.run(base.default_startup_program()) (res1,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data_x": input_x}, fetch_list=[out1], ) @@ -3253,9 +3253,9 @@ def test_api(self): np.testing.assert_allclose(res1, expected_res, rtol=1e-05) # dygraph - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.uniform(0.1, 1, [11, 17]).astype("float64") - data_x = fluid.dygraph.to_variable(np_x) + data_x = base.dygraph.to_variable(np_x) z = paddle.log1p(data_x) np_z = z.numpy() z_expected = np.array(np.log1p(np_x)) @@ -3273,7 +3273,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.square(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3306,7 +3306,7 @@ def setUp(self): out = np.square(x) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(convert_float_to_uint16(x)) + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(x)) } self.outputs = {'Out': convert_float_to_uint16(out)} @@ -3336,7 +3336,7 @@ def setUp(self): x = np.random.uniform(1, 2, self.shape).astype(self.dtype) out = np.power(x, 3) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'factor': 3.0} self.convert_input_output() @@ -3370,7 +3370,7 @@ def setUp(self): out = np.power(x, 3) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), + 'X': OpTest.np_dtype_to_base_dtype(x), 'FactorTensor': np.array([3.0]).astype(self.dtype), } @@ -3386,7 +3386,7 @@ def test_check_grad(self): self.check_grad(['X'], 'Out') def test_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): input = np.random.uniform(1, 2, [11, 17]).astype("float32") x = paddle.static.data(name="x", shape=[11, 17], dtype="float32") res = paddle.static.data( @@ -3401,9 +3401,9 @@ def test_api(self): out_6 = paddle.pow(x, factor_2) self.assertEqual(('pow_res' in out_4.name), True) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res, res_6 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": input}, fetch_list=[out_1, out_2, res, out_6], ) @@ -3445,7 +3445,7 @@ def setUp(self): # The same reason with TestAbs out = ref_stanh(x, scale_a, scale_b) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'scale_a': scale_a, 'scale_b': scale_b} self.convert_input_output() @@ -3491,7 +3491,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', [10, 12]) out = paddle.stanh(x, self.scale_a, self.scale_b) @@ -3509,18 +3509,18 @@ def test_dygraph_api(self): for r in [out]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', [10, 12], dtype="float32") out = paddle.stanh(x, self.scale_a, self.scale_b) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_stanh(self.x_np, self.scale_a, self.scale_b) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, paddle.stanh, 1) @@ -3632,7 +3632,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.softplus(x, self.beta, self.threshold) @@ -3655,7 +3655,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.softplus, 1) @@ -3688,7 +3688,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = ref_softsign(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3718,7 +3718,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.softsign(x) @@ -3741,7 +3741,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.softsign, 1) @@ -3776,7 +3776,7 @@ def setUp(self): x[np.abs(x) < 0.005] = 0.02 out = ref_thresholded_relu(x, threshold) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"threshold": threshold} self.convert_input_output() @@ -3809,7 +3809,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.thresholded_relu(x, self.threshold) @@ -3832,7 +3832,7 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.thresholded_relu, 1) @@ -3875,7 +3875,7 @@ def setUp(self): self.attrs = {'slope': self.slope, 'offset': self.offset} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -3913,7 +3913,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.hardsigmoid(x) @@ -3935,12 +3935,12 @@ def test_dygraph_api(self): for r in [out1, out2]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.nn.functional.hardsigmoid(x, slope=0.2) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_hardsigmoid(self.x_np, 0.2, 0.5) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) @@ -3951,7 +3951,7 @@ def test_fluid_api(self): np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.hardsigmoid, 1) @@ -3983,7 +3983,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = ref_swish(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'beta': 1.0} self.convert_input_output() @@ -4017,7 +4017,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.swish(x) @@ -4039,18 +4039,18 @@ def test_dygraph_api(self): for r in [out1, out2]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.nn.functional.swish(x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_swish(self.x_np) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.swish, 1) @@ -4084,7 +4084,7 @@ def setUp(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = ref_mish(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.convert_input_output() @@ -4117,7 +4117,7 @@ def setUp(self): ) def test_static_api(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out1 = F.mish(x) @@ -4139,18 +4139,18 @@ def test_dygraph_api(self): for r in [out1, out2]: np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) - def test_fluid_api(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.nn.functional.mish(x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_mish(self.x_np) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, F.mish, 1) diff --git a/test/legacy_test/test_activation_sparse_op.py b/test/legacy_test/test_activation_sparse_op.py index 48bf62a5947ef..32da9b9483e7b 100644 --- a/test/legacy_test/test_activation_sparse_op.py +++ b/test/legacy_test/test_activation_sparse_op.py @@ -18,7 +18,7 @@ from op import Operator import paddle -from paddle.fluid import core +from paddle.base import core class TestSparseSquareOp(unittest.TestCase): diff --git a/test/legacy_test/test_adadelta_op.py b/test/legacy_test/test_adadelta_op.py index 42080a4280b55..a1733fa45a150 100644 --- a/test/legacy_test/test_adadelta_op.py +++ b/test/legacy_test/test_adadelta_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base def adadelta_wrapper( @@ -173,9 +173,9 @@ def test_adadelta_dygraph(self): def test_adadelta(self): paddle.enable_static() - place = fluid.CPUPlace() - main = fluid.Program() - with fluid.program_guard(main): + place = base.CPUPlace() + main = base.Program() + with base.program_guard(main): x = paddle.static.data(name='x', shape=[-1, 13], dtype='float32') y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') y_predict = paddle.static.nn.fc(x, size=1) @@ -191,9 +191,9 @@ def test_adadelta(self): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) diff --git a/test/legacy_test/test_adagrad_op.py b/test/legacy_test/test_adagrad_op.py index 66f91de028dd5..552f19f6c0aff 100644 --- a/test/legacy_test/test_adagrad_op.py +++ b/test/legacy_test/test_adagrad_op.py @@ -20,7 +20,7 @@ from op import Operator import paddle -from paddle.fluid import core +from paddle.base import core def adamgrad_wrapper( diff --git a/test/legacy_test/test_adam_op.py b/test/legacy_test/test_adam_op.py index 0c5707c8e59fa..f0326fc54d744 100644 --- a/test/legacy_test/test_adam_op.py +++ b/test/legacy_test/test_adam_op.py @@ -19,8 +19,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adam_wrapper( @@ -649,13 +649,13 @@ def test_check_output(self): class TestAdamOpV2(unittest.TestCase): def test_adam_op(self): - place = fluid.CPUPlace() + place = base.CPUPlace() shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): data = paddle.static.data(name="data", shape=shape) conv = paddle.static.nn.conv2d(data, 8, 3) loss = paddle.mean(conv) @@ -684,7 +684,7 @@ def test_adam_op(self): def test_adam_op_dygraph(self): paddle.disable_static() value = np.arange(26).reshape(2, 13).astype("float32") - a = fluid.dygraph.to_variable(value) + a = base.dygraph.to_variable(value) linear = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam( @@ -732,7 +732,7 @@ def test_adam_op_with_state_dict(self): def test_adam_with_grad_clip(self): paddle.disable_static() value = np.arange(26).reshape(2, 13).astype("float32") - a = fluid.dygraph.to_variable(value) + a = base.dygraph.to_variable(value) linear = paddle.nn.Linear(13, 5) clip = paddle.nn.ClipGradByGlobalNorm(clip_norm=1.0) adam = paddle.optimizer.Adam( diff --git a/test/legacy_test/test_adam_optimizer_fp32_fp64.py b/test/legacy_test/test_adam_optimizer_fp32_fp64.py index b5a65b6938906..da78cd4403e7c 100644 --- a/test/legacy_test/test_adam_optimizer_fp32_fp64.py +++ b/test/legacy_test/test_adam_optimizer_fp32_fp64.py @@ -15,21 +15,21 @@ import unittest import paddle -from paddle import fluid +from paddle import base def get_places(): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) return places def main_test_func(place, dtype): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): - with fluid.scope_guard(fluid.Scope()): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): + with base.scope_guard(base.Scope()): x = paddle.static.data(name='x', shape=[None, 13], dtype=dtype) y = paddle.static.data(name='y', shape=[None, 1], dtype=dtype) y_predict = paddle.static.nn.fc(x, size=1) @@ -45,9 +45,9 @@ def main_test_func(place, dtype): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) diff --git a/test/legacy_test/test_adamax_api.py b/test/legacy_test/test_adamax_api.py index 91f28a79d38fb..1fc1878d81995 100644 --- a/test/legacy_test/test_adamax_api.py +++ b/test/legacy_test/test_adamax_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestAdamaxAPI(unittest.TestCase): @@ -38,13 +38,13 @@ def test_adamax_api_dygraph(self): def test_adamax_api(self): paddle.enable_static() - place = fluid.CPUPlace() + place = base.CPUPlace() shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): data = paddle.static.data(name="data", shape=shape) conv = paddle.static.nn.conv2d(data, 8, 3) loss = paddle.mean(conv) diff --git a/test/legacy_test/test_adamw_op.py b/test/legacy_test/test_adamw_op.py index e94a0de7e41ff..a6a30d3af101a 100644 --- a/test/legacy_test/test_adamw_op.py +++ b/test/legacy_test/test_adamw_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base from paddle.framework import core @@ -245,13 +245,13 @@ def test_adamw_op_coverage(self): def test_adamw_op(self): paddle.enable_static() - place = fluid.CPUPlace() + place = base.CPUPlace() shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): data = paddle.static.data(name="data", shape=shape) conv = paddle.static.nn.conv2d(data, 8, 3) loss = paddle.mean(conv) @@ -755,7 +755,7 @@ def get_numpy_output(param, grad, moment1, moment2, lr_ratio, t): def test_adamw_op(self): paddle.enable_static() - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) learning_rate = 0.0001 beta1 = 0.85 @@ -763,10 +763,10 @@ def test_adamw_op(self): weight_decay = 0.01 epsilon = 1e-8 - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): x = paddle.static.data( name='x', shape=[None, 10], dtype='float32' ) @@ -863,7 +863,7 @@ def get_numpy_output(param, grad, moment1, moment2, lr_ratio, t): "linear_1.b_0@GRAD", ] - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) test_prog = train_prog.clone(for_test=True) diff --git a/test/legacy_test/test_adaptive_avg_pool1d.py b/test/legacy_test/test_adaptive_avg_pool1d.py index 3b77091f6e83e..bca37ba88794f 100644 --- a/test/legacy_test/test_adaptive_avg_pool1d.py +++ b/test/legacy_test/test_adaptive_avg_pool1d.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -80,14 +80,14 @@ def avg_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_adaptive_avg_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.adaptive_avg_pool1d(input, output_size=16) result_np = avg_pool1D_forward_naive( input_np, ksize=[16], strides=[0], paddings=[0], adaptive=True @@ -107,7 +107,7 @@ def check_adaptive_avg_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_adaptive_avg_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -118,9 +118,9 @@ def check_adaptive_avg_static_results(self, place): input_np, ksize=[16], strides=[2], paddings=[0], adaptive=True ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) diff --git a/test/legacy_test/test_adaptive_avg_pool2d.py b/test/legacy_test/test_adaptive_avg_pool2d.py index 663ac74781597..9c6c0c96287a4 100644 --- a/test/legacy_test/test_adaptive_avg_pool2d.py +++ b/test/legacy_test/test_adaptive_avg_pool2d.py @@ -19,8 +19,8 @@ from test_attribute_var import UnittestBase import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def adaptive_start_index(index, input_size, output_size): @@ -143,7 +143,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_4, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_4, out_5], ) @@ -263,7 +263,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_4, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_4, out_5], ) diff --git a/test/legacy_test/test_adaptive_avg_pool3d.py b/test/legacy_test/test_adaptive_avg_pool3d.py index d5054ba2107af..f3aa4cdf81efa 100755 --- a/test/legacy_test/test_adaptive_avg_pool3d.py +++ b/test/legacy_test/test_adaptive_avg_pool3d.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -164,7 +164,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_4, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_4, out_5], ) @@ -296,7 +296,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_4, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_4, out_5], ) diff --git a/test/legacy_test/test_adaptive_max_pool1d.py b/test/legacy_test/test_adaptive_max_pool1d.py index f303a761134bf..b4f44790af5d3 100644 --- a/test/legacy_test/test_adaptive_max_pool1d.py +++ b/test/legacy_test/test_adaptive_max_pool1d.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -71,14 +71,14 @@ def max_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_adaptive_max_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.adaptive_max_pool1d(input, output_size=16) result_np = max_pool1D_forward_naive( @@ -94,7 +94,7 @@ def check_adaptive_max_dygraph_results(self, place): def check_adaptive_max_static_results(self, place): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -109,9 +109,9 @@ def check_adaptive_max_static_results(self, place): adaptive=True, ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) diff --git a/test/legacy_test/test_adaptive_max_pool2d.py b/test/legacy_test/test_adaptive_max_pool2d.py index 104271b955257..e4625fbd8eb33 100644 --- a/test/legacy_test/test_adaptive_max_pool2d.py +++ b/test/legacy_test/test_adaptive_max_pool2d.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -144,7 +144,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_5], ) @@ -250,7 +250,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_5], ) diff --git a/test/legacy_test/test_adaptive_max_pool3d.py b/test/legacy_test/test_adaptive_max_pool3d.py index 13eed4823d88f..b09b5779fc444 100755 --- a/test/legacy_test/test_adaptive_max_pool3d.py +++ b/test/legacy_test/test_adaptive_max_pool3d.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -165,7 +165,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_5], ) @@ -275,7 +275,7 @@ def test_static_graph(self): exe = paddle.static.Executor(place=place) [res_1, res_2, res_3, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=[out_1, out_2, out_3, out_5], ) diff --git a/test/legacy_test/test_add_position_encoding_op.py b/test/legacy_test/test_add_position_encoding_op.py index 1f4906045b01b..48f718ae4f98f 100644 --- a/test/legacy_test/test_add_position_encoding_op.py +++ b/test/legacy_test/test_add_position_encoding_op.py @@ -55,7 +55,7 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), + 'X': OpTest.np_dtype_to_base_dtype(self.x), } self.outputs = {'Out': self.out} self.attrs = {'alpha': self.alpha, 'beta': self.beta} diff --git a/test/legacy_test/test_add_reader_dependency.py b/test/legacy_test/test_add_reader_dependency.py index abcde1833f4e6..9dfd4a500d1f7 100644 --- a/test/legacy_test/test_add_reader_dependency.py +++ b/test/legacy_test/test_add_reader_dependency.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base.layer_helper import LayerHelper def inplace_add(x, bias): @@ -40,18 +40,18 @@ def setUp(self): self.use_double_buffer = True def test_main(self): - self.run_main(fluid.CPUPlace()) + self.run_main(base.CPUPlace()) - if fluid.is_compiled_with_cuda(): - self.run_main(fluid.CUDAPlace(0)) + if base.is_compiled_with_cuda(): + self.run_main(base.CUDAPlace(0)) def run_main(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): tmp_in = paddle.static.data( name='tmp_in', dtype='float32', shape=[1] ) - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( feed_list=[tmp_in], capacity=16, iterable=False, @@ -71,9 +71,9 @@ def data_source(): persistable_in.persistable = True persistable_in = inplace_add(persistable_in, bias=1) - prog = fluid.CompiledProgram(fluid.default_main_program()) + prog = base.CompiledProgram(base.default_main_program()) - exe = fluid.Executor(place) + exe = base.Executor(place) loader.set_batch_generator(data_source) loader.start() @@ -95,12 +95,12 @@ def data_source(): self.assertEqual(ret.shape, (1,)) self.assertEqual(ret[0], batch_id) batch_id += 1 - except fluid.core.EOFException: + except base.core.EOFException: loader.reset() self.assertEqual(batch_id, self.batch_num) t = ( - fluid.global_scope() + base.global_scope() .find_var(persistable_in.name) .get_tensor() ) diff --git a/test/legacy_test/test_addmm_op.py b/test/legacy_test/test_addmm_op.py index 1f92270cbeeac..201d86f7d17c4 100644 --- a/test/legacy_test/test_addmm_op.py +++ b/test/legacy_test/test_addmm_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestAddMMOp(OpTest): @@ -119,14 +119,14 @@ def test_errors(self): with program_guard(Program(), Program()): # The input type of addmm_op must be Variable. - input = fluid.create_lod_tensor( - np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace() + input = base.create_lod_tensor( + np.array([[-1, -1], [-1, -1]]), [[2]], base.CPUPlace() ) - x1 = fluid.create_lod_tensor( - np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1, -1], [-1, -1]]), [[2]], base.CPUPlace() ) - x2 = fluid.create_lod_tensor( - np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace() + x2 = base.create_lod_tensor( + np.array([[-1, -1], [-1, -1]]), [[2]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.addmm, input, x1, x2) @@ -323,10 +323,10 @@ def test_api_with_dygraph(self): np_x = np.random.random((20, 6)).astype(np.float32) np_y = np.random.random((6, 30)).astype(np.float32) - with fluid.dygraph.guard(): - input = fluid.dygraph.to_variable(np_input) - x = fluid.dygraph.to_variable(np_x) - y = fluid.dygraph.to_variable(np_y) + with base.dygraph.guard(): + input = base.dygraph.to_variable(np_input) + x = base.dygraph.to_variable(np_x) + y = base.dygraph.to_variable(np_y) out = paddle.tensor.addmm(input, x, y) np.testing.assert_allclose( np_input + np.dot(np_x, np_y), out.numpy(), rtol=1e-5, atol=1e-8 diff --git a/test/legacy_test/test_affine_grid_function.py b/test/legacy_test/test_affine_grid_function.py index 20114a5304d5d..63f20d663971c 100644 --- a/test/legacy_test/test_affine_grid_function.py +++ b/test/legacy_test/test_affine_grid_function.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class AffineGridTestCase(unittest.TestCase): @@ -45,12 +45,12 @@ def __init__( def setUp(self): self.theta = np.random.randn(*(self.theta_shape)).astype(self.dtype) - def fluid_layer(self, place): + def base_layer(self, place): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): theta_var = paddle.static.data( "input", self.theta_shape, dtype=self.dtype ) @@ -58,17 +58,17 @@ def fluid_layer(self, place): theta_var, self.output_shape ) feed_dict = {"input": self.theta} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): theta_var = paddle.static.data( "input", self.theta_shape, dtype=self.dtype ) @@ -78,7 +78,7 @@ def functional(self, place): align_corners=self.align_corners, ) feed_dict = {"input": self.theta} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -100,8 +100,8 @@ def paddle_dygraph_layer(self): return y_np def _test_equivalence(self, place): - place = fluid.CPUPlace() - result1 = self.fluid_layer(place) + place = base.CPUPlace() + result1 = self.base_layer(place) result2 = self.functional(place) result3 = self.paddle_dygraph_layer() if self.align_corners: @@ -109,17 +109,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class AffineGridErrorTestCase(AffineGridTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_dygraph_layer() diff --git a/test/legacy_test/test_affine_grid_op.py b/test/legacy_test/test_affine_grid_op.py index c623f68554ce1..0bb9f2a19b435 100644 --- a/test/legacy_test/test_affine_grid_op.py +++ b/test/legacy_test/test_affine_grid_op.py @@ -152,7 +152,7 @@ def initTestCase(self): self.output_shape = np.array([20, 2, 5, 7]).astype("int32") self.dynamic_shape = True self.use_cudnn = True - if paddle.fluid.core.is_compiled_with_rocm(): + if paddle.base.core.is_compiled_with_rocm(): self.use_cudnn = ( False # ROCM platform do not have MIOPEN kernel for affine_grid ) diff --git a/test/legacy_test/test_allclose_layer.py b/test/legacy_test/test_allclose_layer.py index 3bf2d34fc24f5..7357199b55ced 100644 --- a/test/legacy_test/test_allclose_layer.py +++ b/test/legacy_test/test_allclose_layer.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -37,9 +37,9 @@ def allclose_check(self, use_cuda, dtype='float32'): a, b, rtol=0.01, atol=0.0, name="corner_case" ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) x = np.array([10000.0, 1e-07]).astype(dtype) y = np.array([10000.1, 1e-08]).astype(dtype) @@ -73,33 +73,33 @@ def allclose_check(self, use_cuda, dtype='float32'): self.assertEqual(result_c, corner_res) def test_allclose_cpu_fp32(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.allclose_check(use_cuda=False, dtype='float32') def test_allclose_cpu_fp64(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.allclose_check(use_cuda=False, dtype='float64') def test_allclose_gpu_fp32(self): - if fluid.core.is_compiled_with_cuda(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + if base.core.is_compiled_with_cuda(): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.allclose_check(use_cuda=True, dtype='float32') def test_allclose_gpu_fp64(self): - if fluid.core.is_compiled_with_cuda(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + if base.core.is_compiled_with_cuda(): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.allclose_check(use_cuda=True, dtype='float64') def test_dygraph_mode(self): @@ -114,7 +114,7 @@ def test_dygraph_mode(self): x_5 = np.array([10.1]).astype("float64") y_5 = np.array([10]).astype("float64") - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_v_1 = paddle.to_tensor(x_1) y_v_1 = paddle.to_tensor(y_1) ret_1 = paddle.allclose( diff --git a/test/legacy_test/test_allclose_op.py b/test/legacy_test/test_allclose_op.py index d79d3cd69af49..e272e98403903 100644 --- a/test/legacy_test/test_allclose_op.py +++ b/test/legacy_test/test_allclose_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core class TestAllcloseOp(OpTest): diff --git a/test/legacy_test/test_angle_op.py b/test/legacy_test/test_angle_op.py index 6321a06606de9..1433bcd117b09 100644 --- a/test/legacy_test/test_angle_op.py +++ b/test/legacy_test/test_angle_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import core, dygraph +from paddle.base import core, dygraph paddle.enable_static() diff --git a/test/legacy_test/test_apply_pass_to_program.py b/test/legacy_test/test_apply_pass_to_program.py index b5a81df5c27ca..bb76bae2b3b59 100644 --- a/test/legacy_test/test_apply_pass_to_program.py +++ b/test/legacy_test/test_apply_pass_to_program.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.framework import _apply_pass +from paddle import base +from paddle.base.framework import _apply_pass from paddle.framework.ir import apply_build_strategy from paddle.nn import CrossEntropyLoss from paddle.vision.models import resnet50 @@ -79,7 +79,7 @@ class TestIRPassBase(unittest.TestCase): def setUp(self): paddle.enable_static() if paddle.is_compiled_with_cuda(): - fluid.set_flags( + base.set_flags( { 'FLAGS_cudnn_deterministic': 1, 'FLAGS_max_inplace_grad_add': 6, diff --git a/test/legacy_test/test_arange.py b/test/legacy_test/test_arange.py index 9412cb992aa3a..19390754048f4 100644 --- a/test/legacy_test/test_arange.py +++ b/test/legacy_test/test_arange.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard diff --git a/test/legacy_test/test_arg_min_max_op.py b/test/legacy_test/test_arg_min_max_op.py index fba469a0e333e..000e5d88b080b 100644 --- a/test/legacy_test/test_arg_min_max_op.py +++ b/test/legacy_test/test_arg_min_max_op.py @@ -20,7 +20,7 @@ from test_attribute_var import UnittestBase import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard class BaseTestCase(OpTest): diff --git a/test/legacy_test/test_arg_min_max_v2_op.py b/test/legacy_test/test_arg_min_max_v2_op.py index bca85ebd4887e..f7e79b56e9f0b 100644 --- a/test/legacy_test/test_arg_min_max_v2_op.py +++ b/test/legacy_test/test_arg_min_max_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def create_kernel_case(op_type, numpy_op_type): @@ -166,7 +166,7 @@ def setUp(self): np.random.seed(123) self.input_data = np.random.rand(10, 10).astype("float32") self.places = [] - self.places.append(fluid.CPUPlace()) + self.places.append(base.CPUPlace()) if core.is_compiled_with_cuda(): self.places.append(paddle.CUDAPlace(0)) self.op = eval("paddle.%s" % (op_type)) diff --git a/test/legacy_test/test_argsort_op.py b/test/legacy_test/test_argsort_op.py index fb96f0f9bee54..4ecbe2a8386b1 100644 --- a/test/legacy_test/test_argsort_op.py +++ b/test/legacy_test/test_argsort_op.py @@ -18,11 +18,11 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program, grad_var_name +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.executor import Executor +from paddle.base.framework import Program, grad_var_name np.random.seed(123) paddle.enable_static() @@ -85,7 +85,7 @@ def setUp(self): self.input_shape, self.axis, self.descending, self.dtype ) - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): x = paddle.static.data( name="x", shape=[-1] + list(self.input_shape), dtype=self.dtype ) @@ -141,7 +141,7 @@ def backward(self): def test_backward(self, numeric_grad_delta=1e-5, max_relative_error=1e-7): self.check_forward() - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): append_backward(self.loss) ana_grad = [np.array(x) for x in self.backward()] @@ -356,14 +356,14 @@ def setUp(self): self.place = core.CPUPlace() def test_error(self): - def test_fluid_var_type(): - with fluid.program_guard(fluid.Program()): + def test_base_var_type(): + with base.program_guard(base.Program()): x = [1] output = paddle.argsort(x=x) - self.assertRaises(TypeError, test_fluid_var_type) + self.assertRaises(TypeError, test_base_var_type) def test_paddle_var_type(): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = [1] output = paddle.argsort(x=x) self.assertRaises(TypeError, test_paddle_var_type) @@ -393,7 +393,7 @@ def setUp(self): self.data = np.random.rand(*self.input_shape) def test_api(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): input = paddle.static.data( name="input", shape=self.input_shape, dtype="float64" ) @@ -401,7 +401,7 @@ def test_api(self): output = paddle.argsort(input, axis=self.axis) output2 = paddle.argsort(input, axis=self.axis, descending=True) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) result, result2 = exe.run( feed={'input': self.data}, fetch_list=[output, output2] ) diff --git a/test/legacy_test/test_array_read_write_op.py b/test/legacy_test/test_array_read_write_op.py index 208bf7483eb6c..3e76d7e3d309f 100644 --- a/test/legacy_test/test_array_read_write_op.py +++ b/test/legacy_test/test_array_read_write_op.py @@ -17,11 +17,11 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor -from paddle.fluid.framework import default_main_program +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.backward import append_backward +from paddle.base.executor import Executor +from paddle.base.framework import default_main_program def _test_read_write(x): @@ -104,10 +104,10 @@ def test_read_write(self): # the input gradient should also be 1 self.assertAlmostEqual(1.0, g_out_sum, delta=0.1) - with fluid.dygraph.guard(place): - tensor1 = fluid.dygraph.to_variable(tensor) - tensor2 = fluid.dygraph.to_variable(tensor) - tensor3 = fluid.dygraph.to_variable(tensor) + with base.dygraph.guard(place): + tensor1 = base.dygraph.to_variable(tensor) + tensor2 = base.dygraph.to_variable(tensor) + tensor3 = base.dygraph.to_variable(tensor) x_dygraph = [tensor1, tensor2, tensor3] for each_x in x_dygraph: each_x.stop_gradient = False diff --git a/test/legacy_test/test_assert_op.py b/test/legacy_test/test_assert_op.py index f94d60f67f85b..3f153de651272 100644 --- a/test/legacy_test/test_assert_op.py +++ b/test/legacy_test/test_assert_op.py @@ -15,17 +15,17 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.static.nn.control_flow import Assert class TestAssertOp(unittest.TestCase): def run_network(self, net_func): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): net_func() - exe = fluid.Executor() + exe = base.Executor() exe.run(main_program) def test_assert_true(self): diff --git a/test/legacy_test/test_assign_op.py b/test/legacy_test/test_assign_op.py index e42d29cb0b1c6..e9dc9889d5fc3 100644 --- a/test/legacy_test/test_assign_op.py +++ b/test/legacy_test/test_assign_op.py @@ -21,9 +21,9 @@ from eager_op_test import convert_float_to_uint16, convert_uint16_to_float import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.backward import append_backward +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.backward import append_backward class TestAssignOp(eager_op_test.OpTest): @@ -126,11 +126,11 @@ def test_assign_LoDTensorArray(self): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) feed_x = np.random.random(size=(100, 10)).astype('float32') ones = np.ones((100, 10)).astype('float32') feed_add = feed_x + ones @@ -149,8 +149,8 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # The type of input must be Variable or numpy.ndarray. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.assign, x1) # When the type of input is numpy.ndarray, the dtype of input must be float32, int32. @@ -179,11 +179,11 @@ def test_assign_LoDTensorArray(self): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) feed_x = np.random.random(size=(100, 10)).astype('float32') ones = np.ones((100, 10)).astype('float32') feed_add = feed_x + ones @@ -198,7 +198,7 @@ def test_assign_LoDTensorArray(self): def test_assign_NumpyArray(self): for dtype in [np.bool_, np.float32, np.int32, np.int64]: - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.random.random(size=(100, 10)).astype(dtype) result1 = paddle.zeros(shape=[3, 3], dtype='float32') paddle.assign(array, result1) @@ -279,8 +279,8 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # The type of input must be Variable or numpy.ndarray. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.assign, x1) # When the type of input is numpy.ndarray, the dtype of input must be float32, int32. @@ -321,9 +321,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) paddle.disable_static() @@ -353,9 +353,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) paddle.disable_static() diff --git a/test/legacy_test/test_assign_pos_op.py b/test/legacy_test/test_assign_pos_op.py index a98cb87379121..4b039eb1bd91d 100644 --- a/test/legacy_test/test_assign_pos_op.py +++ b/test/legacy_test/test_assign_pos_op.py @@ -18,8 +18,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.models.moe import utils -from paddle.fluid import core def assign_pos(x, _cum_count): diff --git a/test/legacy_test/test_assign_value_op.py b/test/legacy_test/test_assign_value_op.py index 7cb5dece346c8..28f9b0fba0597 100644 --- a/test/legacy_test/test_assign_value_op.py +++ b/test/legacy_test/test_assign_value_op.py @@ -18,12 +18,12 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework def assign_value_wrapper( - shape=[], dtype=fluid.core.VarDesc.VarType.FP32, values=0.0 + shape=[], dtype=base.core.VarDesc.VarType.FP32, values=0.0 ): tensor = paddle.Tensor() return paddle._C_ops.assign_value_( @@ -80,9 +80,9 @@ def setUp(self): self.dtype ) self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) def init_dtype(self): @@ -90,12 +90,12 @@ def init_dtype(self): def test_assign(self): with eager_op_test.paddle_static_guard(): - main_program = fluid.Program() - with fluid.program_guard(main_program): + main_program = base.Program() + with base.program_guard(main_program): x = paddle.tensor.create_tensor(dtype=self.dtype) paddle.assign(self.value, output=x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) [fetched_x] = exe.run(main_program, feed={}, fetch_list=[x]) np.testing.assert_array_equal(fetched_x, self.value) self.assertEqual(fetched_x.dtype, self.value.dtype) @@ -119,9 +119,9 @@ def setUp(self): np.bool_ ) self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) def init_dtype(self): diff --git a/test/legacy_test/test_async_read_write.py b/test/legacy_test/test_async_read_write.py index 98aee8d905669..1af4e21c5c9a3 100644 --- a/test/legacy_test/test_async_read_write.py +++ b/test/legacy_test/test_async_read_write.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.device import cuda -from paddle.fluid import core class TestAsyncRead(unittest.TestCase): diff --git a/test/legacy_test/test_atan2_op.py b/test/legacy_test/test_atan2_op.py index ddcaa40dee980..74d5bb7d8f290 100644 --- a/test/legacy_test/test_atan2_op.py +++ b/test/legacy_test/test_atan2_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(0) diff --git a/test/legacy_test/test_attribute_var.py b/test/legacy_test/test_attribute_var.py index b74141a10ebd5..50593501eb92b 100644 --- a/test/legacy_test/test_attribute_var.py +++ b/test/legacy_test/test_attribute_var.py @@ -20,7 +20,7 @@ import paddle import paddle.inference as paddle_infer -from paddle.fluid.framework import OpProtoHolder, Program, program_guard +from paddle.base.framework import OpProtoHolder, Program, program_guard paddle.enable_static() diff --git a/test/legacy_test/test_auc_op.py b/test/legacy_test/test_auc_op.py index 81261ab5b0e45..64b617c1eee22 100644 --- a/test/legacy_test/test_auc_op.py +++ b/test/legacy_test/test_auc_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base class TestAucOp(OpTest): @@ -139,7 +139,7 @@ def test_static(self): class TestAucOpError(unittest.TestCase): def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): def test_type1(): data1 = paddle.static.data( diff --git a/test/legacy_test/test_auto_growth_allocator_gpu.py b/test/legacy_test/test_auto_growth_allocator_gpu.py index f2edff5eac4a8..3ac11c1baf86f 100644 --- a/test/legacy_test/test_auto_growth_allocator_gpu.py +++ b/test/legacy_test/test_auto_growth_allocator_gpu.py @@ -17,10 +17,10 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base # it should be set at the beginning -if fluid.is_compiled_with_cuda(): +if base.is_compiled_with_cuda(): paddle.set_flags( { 'FLAGS_allocator_strategy': 'auto_growth', @@ -32,24 +32,24 @@ class TestMemoryLimit(unittest.TestCase): def setUp(self): self._limit = 10 - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): paddle.set_flags({'FLAGS_gpu_memory_limit_mb': 10}) def test_allocate(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): return other_dim = int(1024 * 1024 / 4) - place = fluid.CUDAPlace(0) - t = fluid.LoDTensor() + place = base.CUDAPlace(0) + t = base.LoDTensor() t.set( np.ndarray([int(self._limit / 2), other_dim], dtype='float32'), place, ) del t - t = fluid.LoDTensor() + t = base.LoDTensor() large_np = np.ndarray([2 * self._limit, other_dim], dtype='float32') try: @@ -61,7 +61,7 @@ def test_allocate(self): class TestChunkSize(unittest.TestCase): def test_allocate(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): return paddle.rand([1024]) diff --git a/test/legacy_test/test_auto_parallel_cost_model.py b/test/legacy_test/test_auto_parallel_cost_model.py index 9c32caf214e44..8725abad91ba8 100644 --- a/test/legacy_test/test_auto_parallel_cost_model.py +++ b/test/legacy_test/test_auto_parallel_cost_model.py @@ -17,6 +17,7 @@ import paddle import paddle.nn.functional as F from paddle import nn, static, utils +from paddle.base import core from paddle.distributed import fleet from paddle.distributed.auto_parallel.static.completion import Completer from paddle.distributed.auto_parallel.static.cost_model import estimate_cost @@ -29,7 +30,6 @@ from paddle.distributed.auto_parallel.static.partitioner import Partitioner from paddle.distributed.auto_parallel.static.reshard import Resharder from paddle.distributed.fleet import auto -from paddle.fluid import core paddle.enable_static() _global_parallel_strategy = "dp_mp_pp" diff --git a/test/legacy_test/test_auto_parallel_mapper.py b/test/legacy_test/test_auto_parallel_mapper.py index bcf791dd9711f..a1159f823f605 100644 --- a/test/legacy_test/test_auto_parallel_mapper.py +++ b/test/legacy_test/test_auto_parallel_mapper.py @@ -21,7 +21,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid, nn, static, utils +from paddle import base, nn, static, utils +from paddle.base import core from paddle.distributed import fleet from paddle.distributed.auto_parallel.static.cluster import Cluster from paddle.distributed.auto_parallel.static.completion import Completer @@ -39,7 +40,6 @@ from paddle.distributed.auto_parallel.static.partitioner import Partitioner from paddle.distributed.auto_parallel.static.reshard import Resharder from paddle.distributed.fleet import auto -from paddle.fluid import core if os.getenv("CUDA_VISIBLE_DEVICES") is not None: os.environ["CUDA_VISIBLE_DEVICES"] = "" @@ -595,7 +595,7 @@ def test_mapper_misc(self): ring_id = 0 root_id = 0 nranks = 2 - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): input = paddle.static.data( name="input", shape=[-1, 10, 10], dtype='float32' ) @@ -635,8 +635,8 @@ def test_mapper_misc(self): inputs={"X": input}, outputs={"Out": output}, attrs={ - "in_dtype": fluid.core.VarDesc.VarType.FP32, - "out_dtype": fluid.core.VarDesc.VarType.FP32, + "in_dtype": base.core.VarDesc.VarType.FP32, + "out_dtype": base.core.VarDesc.VarType.FP32, }, ) self.assertRaises(ValueError, get_comm_volume, cast_op, 0, 1) diff --git a/test/legacy_test/test_auto_search_dist_op.py b/test/legacy_test/test_auto_search_dist_op.py index 369fdec36e55a..2fbf3c9d70293 100644 --- a/test/legacy_test/test_auto_search_dist_op.py +++ b/test/legacy_test/test_auto_search_dist_op.py @@ -16,6 +16,7 @@ import paddle import paddle.nn.functional as F from paddle import nn, static, utils +from paddle.base import core from paddle.distributed.auto_parallel.static.dist_attribute import ( OperatorDistAttr, ) @@ -23,7 +24,6 @@ from paddle.distributed.auto_parallel.static.operators.common import ( get_distributed_operator_impl_container, ) -from paddle.fluid import core paddle.enable_static() device = "gpu" if core.is_compiled_with_cuda() else "cpu" diff --git a/test/legacy_test/test_avoid_twice_initialization.py b/test/legacy_test/test_avoid_twice_initialization.py index 0f29188399a83..5afb4cd20ccc3 100644 --- a/test/legacy_test/test_avoid_twice_initialization.py +++ b/test/legacy_test/test_avoid_twice_initialization.py @@ -15,12 +15,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestAvoidTwiceInitialization(unittest.TestCase): def test_avoid_twice_initialization(self): - cur_program = fluid.Program() + cur_program = base.Program() cur_block = cur_program.current_block() var = cur_block.create_parameter( initializer=paddle.nn.initializer.Constant(value=0.01), diff --git a/test/legacy_test/test_backward.py b/test/legacy_test/test_backward.py index 55fd9b85227bc..2ae9ede04987a 100644 --- a/test/legacy_test/test_backward.py +++ b/test/legacy_test/test_backward.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid, static -from paddle.fluid import backward +from paddle import base, static +from paddle.base import backward class BackwardNet: @@ -59,16 +59,16 @@ class TestBackward(unittest.TestCase): def _check_all(self, net): place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): loss = net.build_model() self._check_backward(loss, main) @@ -96,11 +96,11 @@ def _check_backward(self, loss, main_program): # update no_grad_dict block_no_grad_set.update(no_grad_vars) no_grad_dict[global_block_idx].update( - list(map(fluid.backward._append_grad_suffix_, block_no_grad_set)) + list(map(base.backward._append_grad_suffix_, block_no_grad_set)) ) def _check_params_grad(self, loss, parameter_list=None, no_grad_set=None): - params_grads = fluid.backward.append_backward( + params_grads = base.backward.append_backward( loss, parameter_list, no_grad_set ) params_names = { @@ -111,7 +111,7 @@ def _check_params_grad(self, loss, parameter_list=None, no_grad_set=None): return params_grads def _check_stop_gradient(self, program): - no_grad_dict = fluid.backward._get_stop_gradients_(program) + no_grad_dict = base.backward._get_stop_gradients_(program) if no_grad_dict is not None and isinstance(no_grad_dict, dict): self.assertSetEqual( no_grad_dict[self.global_block_idx], @@ -126,11 +126,11 @@ def _check_op_path(self, root_block, outputs, inputs=[], no_grad_dict=None): else: block_no_grad_set = set( map( - fluid.backward._strip_grad_suffix_, + base.backward._strip_grad_suffix_, no_grad_dict[self.global_block_idx], ) ) - op_path = fluid.backward._find_op_path_( + op_path = base.backward._find_op_path_( root_block, outputs, inputs, block_no_grad_set ) op_types = [op.type for op in op_path] @@ -141,7 +141,7 @@ def _check_op_path(self, root_block, outputs, inputs=[], no_grad_dict=None): def _check_find_no_grad_vars( self, root_block, op_path, targets, block_no_grad_set ): - no_grad_vars = fluid.backward._find_no_grad_vars( + no_grad_vars = base.backward._find_no_grad_vars( root_block, op_path, targets, block_no_grad_set ) self.assertSetEqual(no_grad_vars, self.net.no_grad_vars) @@ -150,16 +150,16 @@ def _check_find_no_grad_vars( def _check_error_param_list(self, net, parameter_list): place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): loss = net.build_model() optimizer = paddle.optimizer.SGD(learning_rate=0.1) optimizer.minimize(loss, parameter_list=parameter_list) @@ -168,16 +168,16 @@ def _check_error_param_list(self, net, parameter_list): def _check_error_no_grad_set(self, net, no_grad_set): place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): loss = net.build_model() optimizer = paddle.optimizer.SGD(learning_rate=0.1) optimizer.minimize(loss, no_grad_set=no_grad_set) @@ -239,13 +239,13 @@ def build_model(self): # shared layer, the grad of 'w2v' will be summed and renamed. # To test _addup_repetitive_outputs_ x_emb = paddle.static.nn.embedding( - x, size=[100, 64], param_attr=fluid.ParamAttr(name='w2v') + x, size=[100, 64], param_attr=base.ParamAttr(name='w2v') ) x2_emb = paddle.static.nn.embedding( - x2, size=[100, 64], param_attr=fluid.ParamAttr(name='w2v') + x2, size=[100, 64], param_attr=base.ParamAttr(name='w2v') ) x3_emb = paddle.static.nn.embedding( - x3, size=[100, 64], param_attr=fluid.ParamAttr(name='w2v') + x3, size=[100, 64], param_attr=base.ParamAttr(name='w2v') ) # merge layers x_merge = paddle.add(x_emb, x2_emb, name='x_add_x2') @@ -255,7 +255,7 @@ def build_model(self): x=x_merge, size=1, activation='softmax', - weight_attr=fluid.ParamAttr(name='fc_w'), + weight_attr=base.ParamAttr(name='fc_w'), name='fc_predict', ) # useless layer for calculating loss @@ -263,7 +263,7 @@ def build_model(self): x=x2_merge, size=1, activation='sigmoid', - weight_attr=fluid.ParamAttr(name='fc_w'), + weight_attr=base.ParamAttr(name='fc_w'), name='fc_no_use', ) # loss @@ -293,16 +293,16 @@ def test_error(self): y = F.relu(conv) with self.assertRaises(TypeError): - x_grad = fluid.gradients(y.name, x) + x_grad = base.gradients(y.name, x) with self.assertRaises(TypeError): - x_grad = fluid.gradients(y, x.name) + x_grad = base.gradients(y, x.name) with self.assertRaises(TypeError): - x_grad = fluid.gradients([y], [x], target_gradients=x.name) + x_grad = base.gradients([y], [x], target_gradients=x.name) with self.assertRaises(TypeError): - x_grad = fluid.gradients([y], x, no_grad_set=conv) + x_grad = base.gradients([y], x, no_grad_set=conv) class TestSimpleNetWithErrorParamList(TestBackward): @@ -345,24 +345,24 @@ def build_net(self): avg_loss = paddle.mean(loss) param_names = [ param.name - for param in fluid.default_main_program().block(0).all_parameters() + for param in base.default_main_program().block(0).all_parameters() ] return avg_loss, param_names def setUp(self): - main_program = fluid.Program() - with fluid.program_guard(main_program): + main_program = base.Program() + with base.program_guard(main_program): self.avg_loss, self.param_names = self.build_net() def test_loss_type_error(self): with self.assertRaises(TypeError): - fluid.backward.append_backward(loss=self.avg_loss.name) + base.backward.append_backward(loss=self.avg_loss.name) def test_parameter_list_type_error(self): with self.assertRaises(TypeError): self.param_names[0] = np.random.random([10]) - fluid.backward.append_backward( + base.backward.append_backward( loss=self.avg_loss, parameter_list=self.param_names ) @@ -372,7 +372,7 @@ def test_callback_type_error(self): def callback(block, context): return - fluid.backward.append_backward( + base.backward.append_backward( loss=self.avg_loss, callbacks=callback ) @@ -387,10 +387,10 @@ def _check_grad_op_name(self, forward_list, optimiezed_list): ) def test_gradient_with_optimizer(self): - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): img = static.data(name='image', shape=[None, 784]) pred = static.nn.fc(x=img, size=10, activation='relu') loss = paddle.mean(pred) @@ -436,7 +436,7 @@ def test(self): + (gt[2:4] * x).sum() ) exe = paddle.static.Executor() - paddle.fluid.backward.gradients(loss, []) + paddle.base.backward.gradients(loss, []) exe.run(startup_prg) # Optimizer out = exe.run( diff --git a/test/legacy_test/test_backward_infer_var_data_type_shape.py b/test/legacy_test/test_backward_infer_var_data_type_shape.py index 2fb83a96bc675..c68ef82d6284b 100644 --- a/test/legacy_test/test_backward_infer_var_data_type_shape.py +++ b/test/legacy_test/test_backward_infer_var_data_type_shape.py @@ -18,18 +18,18 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestBackwardInferVarDataTypeShape(unittest.TestCase): def test_backward_infer_var_data_type_shape(self): paddle.enable_static() - program = fluid.default_main_program() + program = base.default_main_program() dy = program.global_block().create_var( name="Tmp@GRAD", shape=[1, 1], dtype=np.float32, persistable=True ) # invoke warning - fluid.backward._infer_var_data_type_shape_( + base.backward._infer_var_data_type_shape_( "Tmp@GRAD", program.global_block() ) res = False diff --git a/test/legacy_test/test_base_layer.py b/test/legacy_test/test_base_layer.py index 9aa3327efe080..28b8d0cac762f 100644 --- a/test/legacy_test/test_base_layer.py +++ b/test/legacy_test/test_base_layer.py @@ -17,15 +17,15 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.dygraph import to_variable -from paddle.fluid.framework import EagerParamBase +from paddle import base +from paddle.base.dygraph import to_variable +from paddle.base.framework import EagerParamBase class L1(paddle.nn.Layer): def __init__(self): super().__init__() - self._param_attr = fluid.ParamAttr( + self._param_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.1) ) self.w1 = self.create_parameter( @@ -61,7 +61,7 @@ def forward(self): class TestBaseLayer(unittest.TestCase): def test_one_level(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): l = L1() ret = l() expected_names = ['l1.w1', 'l1.w2'] @@ -74,7 +74,7 @@ def test_one_level(self): ) def test_three_level(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): l = L3() expected_names = [ 'l3.layer1.layer1.w1', @@ -96,7 +96,7 @@ def test_three_level(self): ) def test_add_parameter_with_error(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() param = net.create_parameter(shape=[1]) @@ -152,7 +152,7 @@ def test_buffers_and_named_buffers(self): def names(named_buffers): return [name for name, _ in named_buffers] - with fluid.dygraph.guard(): + with base.dygraph.guard(): layer = BufferLayer() net = BufferNet() @@ -172,7 +172,7 @@ def names(named_buffers): ) def test_register_buffer_with_error(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var = to_variable(np.zeros([1])) @@ -208,7 +208,7 @@ def test_register_buffer_with_error(self): net.register_buffer("attr_name", var) def test_register_buffer_same_name(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) var2 = to_variable(np.zeros([2])) @@ -222,7 +222,7 @@ def test_register_buffer_same_name(self): self.assert_var_base_equal(net.buffer_name, var3) def test_buffer_not_persistable(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) @@ -231,7 +231,7 @@ def test_buffer_not_persistable(self): self.assertEqual(len(net.state_dict()), 0) def test_buffer_not_persistable_del(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) net.register_buffer("buffer_name", var1, persistable=False) @@ -239,7 +239,7 @@ def test_buffer_not_persistable_del(self): self.assertEqual(len(net.buffers()), 0) def test_buffer_not_persistable_overwrite(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) var2 = to_variable(np.zeros([2])) @@ -255,7 +255,7 @@ def test_buffer_not_persistable_overwrite(self): self.assertEqual(len(net.state_dict()), 0) def test_buffer_not_persistable_assign(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) net.register_buffer("buffer_name", var1, persistable=False) @@ -276,14 +276,14 @@ def test_buffer_not_persistable_assign(self): self.assertEqual(len(net.state_dict()), 1) def test_buffer_not_persistable_load(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([1])) net.register_buffer("buffer_name", var1, persistable=False) net.load_dict({}) def test_buffer_state_dict(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Layer() var1 = to_variable(np.zeros([2, 3])) var2 = to_variable(np.zeros([3, 2])) @@ -364,39 +364,37 @@ def funcsetUp(self): def func_test_to_api(self): self.linear.to(dtype='double') self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) self.linear.to() self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) for p in self.linear.parameters(): - self.assertTrue( - isinstance(p, paddle.fluid.framework.EagerParamBase) - ) + self.assertTrue(isinstance(p, paddle.base.framework.EagerParamBase)) - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): self.linear.to(device=paddle.CUDAPlace(0)) self.assertTrue(self.linear.weight.place.is_gpu_place()) self.assertEqual(self.linear.weight.place.gpu_device_id(), 0) @@ -422,7 +420,7 @@ def func_test_to_api(self): ) for p in self.linear.parameters(): self.assertTrue( - isinstance(p, paddle.fluid.framework.EagerParamBase) + isinstance(p, paddle.base.framework.EagerParamBase) ) self.linear.to(device=paddle.CPUPlace()) @@ -442,72 +440,68 @@ def func_test_to_api(self): def func_test_to_api_paddle_dtype(self): self.linear.to(dtype=paddle.float64) self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) self.linear.to() self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) for p in self.linear.parameters(): - self.assertTrue( - isinstance(p, paddle.fluid.framework.EagerParamBase) - ) + self.assertTrue(isinstance(p, paddle.base.framework.EagerParamBase)) def func_test_to_api_numpy_dtype(self): self.linear.to(dtype=np.float64) self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) self.linear.to() self.assertEqual( - self.linear.weight.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.weight.dtype, paddle.base.core.VarDesc.VarType.FP64 ) self.assertEqual( - self.linear.buf_name.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + self.linear.buf_name.dtype, paddle.base.core.VarDesc.VarType.FP64 ) np.testing.assert_allclose( self.linear.weight.grad.numpy(), self.new_grad, rtol=1e-05 ) self.assertEqual( self.linear.weight._grad_ivar().dtype, - paddle.fluid.core.VarDesc.VarType.FP64, + paddle.base.core.VarDesc.VarType.FP64, ) for p in self.linear.parameters(): - self.assertTrue( - isinstance(p, paddle.fluid.framework.EagerParamBase) - ) + self.assertTrue(isinstance(p, paddle.base.framework.EagerParamBase)) def func_test_to_api_none_buffer(self): model = paddle.nn.Linear(2, 4) diff --git a/test/legacy_test/test_batch_fc_op.py b/test/legacy_test/test_batch_fc_op.py index 1ca4b640af075..e5f045a49d604 100644 --- a/test/legacy_test/test_batch_fc_op.py +++ b/test/legacy_test/test_batch_fc_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core def np_cal_batchfc(input, w, bias): diff --git a/test/legacy_test/test_batch_norm_op.py b/test/legacy_test/test_batch_norm_op.py index bbe322ae0175b..4f3f06f692cba 100644 --- a/test/legacy_test/test_batch_norm_op.py +++ b/test/legacy_test/test_batch_norm_op.py @@ -25,9 +25,9 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import grad_var_name +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.framework import grad_var_name _set_use_system_allocator(True) @@ -274,19 +274,19 @@ def check_with_place(self, place, data_layout, dtype, shape): # create input x_tensor = create_or_get_tensor( - scope, "x_val", OpTest.np_dtype_to_fluid_dtype(x_val), place + scope, "x_val", OpTest.np_dtype_to_base_dtype(x_val), place ) scale_tensor = create_or_get_tensor( - scope, "scale_val", OpTest.np_dtype_to_fluid_dtype(scale_val), place + scope, "scale_val", OpTest.np_dtype_to_base_dtype(scale_val), place ) bias_tensor = create_or_get_tensor( - scope, "bias_val", OpTest.np_dtype_to_fluid_dtype(bias_val), place + scope, "bias_val", OpTest.np_dtype_to_base_dtype(bias_val), place ) mean_tensor = create_or_get_tensor( - scope, "mean", OpTest.np_dtype_to_fluid_dtype(mean), place + scope, "mean", OpTest.np_dtype_to_base_dtype(mean), place ) variance_tensor = create_or_get_tensor( - scope, "variance", OpTest.np_dtype_to_fluid_dtype(variance), place + scope, "variance", OpTest.np_dtype_to_base_dtype(variance), place ) # create output @@ -333,7 +333,7 @@ def check_with_place(self, place, data_layout, dtype, shape): # Create executor to have MKL-DNN cache # cleared after NHWC unit test place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) dims = y_tensor.shape() c = dims.pop(1) dims.append(c) @@ -555,8 +555,8 @@ def test_with_place(place, data_layout, shape): ] ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -618,7 +618,7 @@ def test_with_place(place, data_layout, shape): program._sync_with_cpp() - exe = fluid.Executor(place) + exe = base.Executor(place) out = exe.run( program, feed={ @@ -799,8 +799,8 @@ class TestBatchNormOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # the input of batch_norm must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.static.nn.batch_norm, x1) @@ -821,8 +821,8 @@ def test_errors(self): with program_guard(Program(), Program()): batch_norm = paddle.nn.BatchNorm(10) # the input of BatchNorm must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, batch_norm, x1) @@ -836,20 +836,20 @@ def test_errors(self): class TestDygraphBatchNormTrainableStats(unittest.TestCase): def test_dygraph(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute(x, is_test, trainable_statistics): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm( shape[1], is_test=is_test, trainable_statistics=trainable_statistics, ) - y = bn(fluid.dygraph.to_variable(x)) + y = bn(base.dygraph.to_variable(x)) return y.numpy() x = np.random.randn(*shape).astype("float32") @@ -858,11 +858,11 @@ def compute(x, is_test, trainable_statistics): np.testing.assert_allclose(y1, y2, rtol=1e-05) def test_static(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: - exe = fluid.Executor(p) + exe = base.Executor(p) shape = [4, 10, 16, 16] def compute(x_np, is_test, trainable_statistics): @@ -876,7 +876,7 @@ def compute(x_np, is_test, trainable_statistics): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = bn(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r diff --git a/test/legacy_test/test_batch_norm_op_prim_nchw.py b/test/legacy_test/test_batch_norm_op_prim_nchw.py index 9d11d264908f0..a83fc9fbc2c88 100644 --- a/test/legacy_test/test_batch_norm_op_prim_nchw.py +++ b/test/legacy_test/test_batch_norm_op_prim_nchw.py @@ -23,7 +23,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_batch_norm_op_prim_nhwc.py b/test/legacy_test/test_batch_norm_op_prim_nhwc.py index 4f1e3e2d22522..11d8c4a0db459 100644 --- a/test/legacy_test/test_batch_norm_op_prim_nhwc.py +++ b/test/legacy_test/test_batch_norm_op_prim_nhwc.py @@ -19,7 +19,7 @@ from test_batch_norm_op_prim_nchw import TestBatchNormOp import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_batch_norm_op_v2.py b/test/legacy_test/test_batch_norm_op_v2.py index 618513a0d044b..b53bfb9e73373 100644 --- a/test/legacy_test/test_batch_norm_op_v2.py +++ b/test/legacy_test/test_batch_norm_op_v2.py @@ -17,23 +17,23 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestBatchNorm(unittest.TestCase): def test_name(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): batch_norm1d = paddle.nn.BatchNorm1D(1, name="test") def test_error(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: # paddle.disable_static() x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') @@ -69,7 +69,7 @@ def error3d(): batch_norm3d = paddle.nn.BatchNorm3D(1) batch_norm3d(paddle.to_tensor(x_data_4)) - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): self.assertRaises(ValueError, error1d) self.assertRaises(ValueError, error2d) self.assertRaises(ValueError, error3d) @@ -79,7 +79,7 @@ def error3d(): def test_large_batch(self): def compute_baseline(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm(shape[1]) x1 = paddle.to_tensor(x) x1.stop_gradient = False @@ -88,7 +88,7 @@ def compute_baseline(x): return y.numpy(), x1.gradient() def compute_1d(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm1D(shape[1]) x1 = paddle.to_tensor(x) x1.stop_gradient = False @@ -96,9 +96,9 @@ def compute_1d(x): y.backward() return y.numpy(), x1.gradient() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: # [N, C] shape = [200000, 4] @@ -117,14 +117,14 @@ def compute_1d(x): np.testing.assert_allclose(y1, y2, rtol=1e-05) def test_eager_api(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v1(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm(shape[1]) # bn = paddle.nn.BatchNorm2D(shape[1]) x1 = paddle.to_tensor(x) @@ -134,7 +134,7 @@ def compute_v1(x): return y.numpy(), x1.gradient() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): print("v2") bn = paddle.nn.BatchNorm2D(shape[1]) x1 = paddle.to_tensor(x) @@ -150,14 +150,14 @@ def compute_v2(x): np.testing.assert_allclose(y1, y2, rtol=1e-05) def test_dygraph(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v1(x, is_test, trainable_statistics): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm( shape[1], is_test=is_test, @@ -167,7 +167,7 @@ def compute_v1(x, is_test, trainable_statistics): return y.numpy() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm2D(shape[1]) y = bn(paddle.to_tensor(x)) @@ -177,15 +177,15 @@ def compute_v2(x): return y.numpy() def compute_v3(x, is_test, trainable_statistics): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm( shape[1], is_test=is_test, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0), trainable=False, ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0), trainable=False, ), @@ -195,7 +195,7 @@ def compute_v3(x, is_test, trainable_statistics): return y.numpy() def compute_v4(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.BatchNorm2D( shape[1], weight_attr=False, bias_attr=False ) @@ -211,11 +211,11 @@ def compute_v4(x): np.testing.assert_allclose(y3, y4, rtol=1e-05) def test_static(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: - exe = fluid.Executor(p) + exe = base.Executor(p) shape = [4, 10, 16, 16] def compute_v1(x_np, is_test, trainable_statistics): @@ -229,7 +229,7 @@ def compute_v1(x_np, is_test, trainable_statistics): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = bn(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -240,7 +240,7 @@ def compute_v2(x_np): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = bn(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -258,16 +258,16 @@ def setUp(self): paddle.set_default_dtype("float32") else: paddle.set_default_dtype("float64") - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def tearDown(self): paddle.set_default_dtype(self.original_dtyep) def test_1d(self): for p in self.places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.randn([2, 6, 4]) net1 = paddle.nn.BatchNorm1D(4, data_format="NLC") net2 = paddle.nn.BatchNorm1D(4) @@ -289,7 +289,7 @@ def test_1d(self): def test_2d(self): for p in self.places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.randn([2, 6, 6, 4]) net1 = paddle.nn.BatchNorm2D(4, data_format="NHWC") net2 = paddle.nn.BatchNorm2D(4) @@ -311,7 +311,7 @@ def test_2d(self): def test_3d(self): for p in self.places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.randn([2, 6, 6, 6, 4]) net1 = paddle.nn.BatchNorm3D(4, data_format="NDHWC") net2 = paddle.nn.BatchNorm3D(4) @@ -332,7 +332,7 @@ def test_3d(self): ) def test_1d_opt(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): batch_size = 13700 channels = 16 shape = (batch_size, channels) @@ -364,9 +364,9 @@ def test_1d_opt(self): class TestBatchNormUseGlobalStats(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) self.init_test() # train mode @@ -376,11 +376,11 @@ def init_test(self): def test_global_stats(self): for p in self.places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.randn([2, 6, 6, 4]) net1 = paddle.nn.BatchNorm( 6, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), use_global_stats=self.use_global_stats, diff --git a/test/legacy_test/test_bce_loss.py b/test/legacy_test/test_bce_loss.py index 38310e5a62025..dc95248ae9bde 100644 --- a/test/legacy_test/test_bce_loss.py +++ b/test/legacy_test/test_bce_loss.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def test_static_layer( @@ -155,9 +155,9 @@ class TestBCELoss(unittest.TestCase): def test_BCELoss(self): input_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float64) label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float64) - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: @@ -194,9 +194,9 @@ def test_BCELoss_weight(self): ) weight_np = np.random.random(size=(3, 4, 10)).astype(np.float64) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) for reduction in ['sum', 'mean', 'none']: static_result = test_static_layer( diff --git a/test/legacy_test/test_bce_with_logits_loss.py b/test/legacy_test/test_bce_with_logits_loss.py index d9905fe463232..32444e34b6102 100644 --- a/test/legacy_test/test_bce_with_logits_loss.py +++ b/test/legacy_test/test_bce_with_logits_loss.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def call_bce_layer( @@ -92,7 +92,7 @@ def test_dygraph( pos_weight_np=None, functional=False, ): - with paddle.fluid.dygraph.base.guard(): + with paddle.base.dygraph.base.guard(): logit = paddle.to_tensor(logit_np) label = paddle.to_tensor(label_np) weight = None @@ -141,9 +141,9 @@ class TestBCEWithLogitsLoss(unittest.TestCase): def test_BCEWithLogitsLoss(self): logit_np = np.random.uniform(0.1, 0.8, size=(20, 30)).astype(np.float64) label_np = np.random.randint(0, 2, size=(20, 30)).astype(np.float64) - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) reductions = ['sum', 'mean', 'none'] for place in places: for reduction in reductions: @@ -191,9 +191,9 @@ def test_BCEWithLogitsLoss_weight(self): ) weight_np = np.random.random(size=(2, 3, 4, 10)).astype(np.float64) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) for reduction in ['sum', 'mean', 'none']: static_result = test_static( @@ -248,9 +248,9 @@ def test_BCEWithLogitsLoss_pos_weight(self): pos_weight_np = np.random.random(size=(3, 4, 10)).astype(np.float64) weight_np = np.random.random(size=(2, 3, 4, 10)).astype(np.float64) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) reduction = "mean" static_result = test_static( diff --git a/test/legacy_test/test_beam_search_decode_op.py b/test/legacy_test/test_beam_search_decode_op.py index eee6832f2d254..38f4a200283ff 100644 --- a/test/legacy_test/test_beam_search_decode_op.py +++ b/test/legacy_test/test_beam_search_decode_op.py @@ -17,7 +17,7 @@ import numpy as np from op import Operator -from paddle.fluid import core +from paddle.base import core class TestBeamSearchDecodeOp(unittest.TestCase): diff --git a/test/legacy_test/test_beam_search_op.py b/test/legacy_test/test_beam_search_op.py index 940f365bdee77..012eef68bf5d3 100644 --- a/test/legacy_test/test_beam_search_op.py +++ b/test/legacy_test/test_beam_search_op.py @@ -17,7 +17,7 @@ import numpy as np from op import Operator -from paddle.fluid import core +from paddle.base import core def create_tensor(scope, name, np_data): diff --git a/test/legacy_test/test_bernoulli_op.py b/test/legacy_test/test_bernoulli_op.py index cce3c09400e02..51cd296436ef3 100644 --- a/test/legacy_test/test_bernoulli_op.py +++ b/test/legacy_test/test_bernoulli_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def output_hist(out): diff --git a/test/legacy_test/test_bicubic_interp_op.py b/test/legacy_test/test_bicubic_interp_op.py index 07c2f1a3e8292..8cec2f266cf61 100644 --- a/test/legacy_test/test_bicubic_interp_op.py +++ b/test/legacy_test/test_bicubic_interp_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard from paddle.nn.functional import interpolate @@ -286,15 +286,15 @@ def test_case(self): actual_size_data = np.array([12, 12]).astype("int32") scale_data = np.array([2.0]).astype("float32") - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): x = paddle.static.data( name="x", shape=[2, 3, 6, 6], dtype="float32" ) @@ -329,10 +329,10 @@ def test_case(self): align_corners=False, ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "dim": dim_data, @@ -350,8 +350,8 @@ def test_case(self): for res in results: np.testing.assert_allclose(res, expect_res, rtol=1e-05) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(x_data) + with base.dygraph.guard(): + x = base.dygraph.to_variable(x_data) interp = interpolate( x, size=[12, 12], mode='bicubic', align_corners=False ) @@ -366,8 +366,8 @@ class TestBicubicOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # the input of interpoalte must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, interpolate, x1) @@ -423,8 +423,8 @@ def test_attr_data_format(): def test_actual_shape(): # the actual_shape must be Variable. - x = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) out = interpolate( x, size=[12, 12], mode='BICUBIC', align_corners=False @@ -460,8 +460,8 @@ def test_scale_type(): x = paddle.static.data( name="x", shape=[2, 3, 6, 6], dtype="float32" ) - scale = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + scale = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) out = interpolate( x, diff --git a/test/legacy_test/test_bicubic_interp_v2_op.py b/test/legacy_test/test_bicubic_interp_v2_op.py index 4975b1bf0684f..e034015f9a8f0 100644 --- a/test/legacy_test/test_bicubic_interp_v2_op.py +++ b/test/legacy_test/test_bicubic_interp_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard from paddle.nn.functional import interpolate @@ -591,15 +591,15 @@ def test_case(self): actual_size_data = np.array([12, 12]).astype("int32") scale_data = np.array([2.0]).astype("float32") - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): x = paddle.static.data( name="x", shape=[2, 3, 6, 6], dtype="float32" ) @@ -640,10 +640,10 @@ def test_case(self): x, scale_factor=[2.0, 2.0], mode='bicubic', align_corners=False ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "dim": dim_data, @@ -661,8 +661,8 @@ def test_case(self): for res in results: np.testing.assert_allclose(res, expect_res, rtol=1e-05) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(x_data) + with base.dygraph.guard(): + x = base.dygraph.to_variable(x_data) interp = interpolate( x, size=[12, 12], mode='bicubic', align_corners=False ) @@ -676,8 +676,8 @@ def test_case(self): class TestBicubicOpError(unittest.TestCase): def test_imperative_errors(self): # the input of interpoalte must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, interpolate, x1) @@ -720,8 +720,8 @@ def test_attr_data_format(): def test_actual_shape(): # the actual_shape must be Variable. - x = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) out = interpolate( x, size=[12, 12], mode='BICUBIC', align_corners=False @@ -754,8 +754,8 @@ def test_scale_type(): x = paddle.static.data( name="x", shape=[2, 3, 6, 6], dtype="float32" ) - scale = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + scale = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) out = interpolate( x, @@ -914,7 +914,7 @@ def test_errors(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestBicubicInterpOpForFloat16(unittest.TestCase): def init_test_case(self): diff --git a/test/legacy_test/test_bilateral_slice_op.py b/test/legacy_test/test_bilateral_slice_op.py index f62b19a813e87..1cf5f292240d5 100644 --- a/test/legacy_test/test_bilateral_slice_op.py +++ b/test/legacy_test/test_bilateral_slice_op.py @@ -143,7 +143,7 @@ def naive_bilateral_slice(x, guide, grid, has_offset): @unittest.skipIf( - not paddle.fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not paddle.base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestBilateralSliceOp(OpTest): def setUp(self): @@ -169,11 +169,11 @@ def setUp(self): self.outputs = {'Out': output_np} def test_check_output(self): - place = paddle.fluid.CUDAPlace(0) + place = paddle.base.CUDAPlace(0) self.check_output_with_place(place, atol=1e-5) def test_check_grad(self): - place = paddle.fluid.CUDAPlace(0) + place = paddle.base.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out') def initTestCase(self): @@ -182,7 +182,7 @@ def initTestCase(self): @unittest.skipIf( - not paddle.fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not paddle.base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestBilateralSliceOp1(TestBilateralSliceOp): def initTestCase(self): @@ -204,10 +204,10 @@ def test_api(self): ) bilateral_slice(x, guide, grid, False) - if not paddle.fluid.is_compiled_with_cuda(): + if not paddle.base.is_compiled_with_cuda(): return - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x1 = paddle.rand([3, 1, 50, 30]) guide1 = paddle.rand([3, 50, 30]) grid1 = paddle.rand([3, 2, 2, 5, 3]) diff --git a/test/legacy_test/test_bilinear_api.py b/test/legacy_test/test_bilinear_api.py index f7f22c0725aab..5331252af2de0 100644 --- a/test/legacy_test/test_bilinear_api.py +++ b/test/legacy_test/test_bilinear_api.py @@ -17,20 +17,20 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestBilinearAPI(unittest.TestCase): def test_api(self): - with fluid.program_guard( - fluid.default_startup_program(), fluid.default_main_program() + with base.program_guard( + base.default_startup_program(), base.default_main_program() ): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) else: place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) data1 = paddle.static.data(name='X1', shape=[5, 5], dtype='float32') data2 = paddle.static.data(name='X2', shape=[5, 4], dtype='float32') @@ -43,7 +43,7 @@ def test_api(self): ) ret = bilinear(data1, data2) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) ret_fetch = exe.run( feed={'X1': layer1, 'X2': layer2}, fetch_list=[ret.name] ) diff --git a/test/legacy_test/test_bilinear_interp_op.py b/test/legacy_test/test_bilinear_interp_op.py index 33104d3cf2d02..fca64f8b802df 100755 --- a/test/legacy_test/test_bilinear_interp_op.py +++ b/test/legacy_test/test_bilinear_interp_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_bilinear_interp_v2_op.py b/test/legacy_test/test_bilinear_interp_v2_op.py index 06249697f0c6b..93805968673a8 100755 --- a/test/legacy_test/test_bilinear_interp_v2_op.py +++ b/test/legacy_test/test_bilinear_interp_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional import interpolate @@ -885,7 +885,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") input_x = paddle.to_tensor(input_data) expect_res = bilinear_interp_np( @@ -905,7 +905,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") size_np = np.array([12, 12]).astype("int64") input_x = paddle.to_tensor(input_data) @@ -927,7 +927,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") size_1 = np.array([12]).astype("int64") input_x = paddle.to_tensor(input_data) @@ -952,7 +952,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") scale_np = np.array([2, 2]).astype("int64") input_x = paddle.to_tensor(input_data) @@ -970,7 +970,7 @@ def test_case(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestBilinearInterpOpZoomOutForFloat16(unittest.TestCase): def init_test_case(self): @@ -1013,7 +1013,7 @@ def test_main(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestBilinearInterpOpZoomInForFloat16(unittest.TestCase): def init_test_case(self): @@ -1063,7 +1063,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") input_x = paddle.to_tensor(input_data) expect_res = bilinear_interp_np( @@ -1087,7 +1087,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") input_x = paddle.to_tensor(input_data) expect_res = bilinear_interp_np( @@ -1111,7 +1111,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("float32") input_x = paddle.to_tensor(input_data) expect_res = bilinear_interp_np( diff --git a/test/legacy_test/test_bilinear_tensor_product_op.py b/test/legacy_test/test_bilinear_tensor_product_op.py index a121f0df21b4c..3e8a81393ff05 100644 --- a/test/legacy_test/test_bilinear_tensor_product_op.py +++ b/test/legacy_test/test_bilinear_tensor_product_op.py @@ -18,17 +18,17 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid +from paddle import base class TestDygraphBilinearTensorProductAPIError(unittest.TestCase): def test_errors(self): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): layer = paddle.nn.Bilinear(5, 4, 1000) # the input must be Variable. - x0 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x0 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, layer, x0) # the input dtype must be float32 or float64 @@ -60,7 +60,7 @@ def setUp(self): size0 = 5 size1 = 4 size2 = 5 - dtype = "float32" if fluid.core.is_compiled_with_rocm() else "float64" + dtype = "float32" if base.core.is_compiled_with_rocm() else "float64" a = np.random.random((batch_size, size0)).astype(dtype) b = np.random.random((batch_size, size1)).astype(dtype) w = np.random.random((size2, size0, size1)).astype(dtype) diff --git a/test/legacy_test/test_bincount_op.py b/test/legacy_test/test_bincount_op.py index 504b4b51c77d5..f788dcf2798c7 100644 --- a/test/legacy_test/test_bincount_op.py +++ b/test/legacy_test/test_bincount_op.py @@ -21,8 +21,8 @@ import paddle import paddle.inference as paddle_infer -from paddle import fluid -from paddle.fluid.framework import in_dygraph_mode +from paddle import base +from paddle.base.framework import in_dygraph_mode paddle.enable_static() @@ -31,18 +31,18 @@ class TestBincountOpAPI(unittest.TestCase): """Test bincount api.""" def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): inputs = paddle.static.data(name='input', dtype='int64', shape=[7]) weights = paddle.static.data( name='weights', dtype='int64', shape=[7] ) output = paddle.bincount(inputs, weights=weights) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([0, 1, 1, 3, 2, 1, 7]).astype(np.int64) w = np.array([0, 1, 1, 2, 2, 1, 0]).astype(np.int64) @@ -59,9 +59,9 @@ def test_static_graph(self): ) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): inputs_np = np.array([0, 1, 1, 3, 2, 1, 7]).astype(np.int64) - inputs = fluid.dygraph.to_variable(inputs_np) + inputs = base.dygraph.to_variable(inputs_np) actual = paddle.bincount(inputs) expected = np.bincount(inputs) self.assertTrue( @@ -74,7 +74,7 @@ class TestBincountOpError(unittest.TestCase): """Test bincount op error.""" def run_network(self, net_func): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net_func() def test_input_value_error(self): @@ -104,7 +104,7 @@ def net_func(): input_value = paddle.to_tensor([1, 2, 3, 4, 5]) paddle.bincount(input_value, minlength=-1) - with fluid.dygraph.guard(): + with base.dygraph.guard(): if in_dygraph_mode(): # InvalidArgument for phi BincountKernel with self.assertRaises(ValueError): diff --git a/test/legacy_test/test_bmm_op.py b/test/legacy_test/test_bmm_op.py index 62d1c1e44c9b9..91a0f806dbe16 100644 --- a/test/legacy_test/test_bmm_op.py +++ b/test/legacy_test/test_bmm_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestBmmOp(OpTest): @@ -88,7 +88,7 @@ def test_checkout_grad(self): class API_TestBmm(unittest.TestCase): def test_out(self): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( 'data1', shape=[-1, 3, 4], dtype='float64' ) @@ -96,8 +96,8 @@ def test_out(self): 'data2', shape=[-1, 4, 5], dtype='float64' ) result_bmm = paddle.bmm(data1, data2) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([10, 3, 4]).astype('float64') input2 = np.random.random([10, 4, 5]).astype('float64') (result,) = exe.run( @@ -122,9 +122,9 @@ def test_out(self): [[4.0, 4.0], [5.0, 5.0], [6.0, 6.0]], ] ) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(input1) - y = fluid.dygraph.to_variable(input2) + with base.dygraph.guard(): + x = base.dygraph.to_variable(input1) + y = base.dygraph.to_variable(input2) out = paddle.bmm(x, y) out_np = out.numpy() expected_result = np.matmul(input1, input2) diff --git a/test/legacy_test/test_boxps.py b/test/legacy_test/test_boxps.py index 545c485f25c32..78c7473d4fcfd 100644 --- a/test/legacy_test/test_boxps.py +++ b/test/legacy_test/test_boxps.py @@ -15,9 +15,9 @@ import unittest import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.distributed.transpiler import collective -from paddle.fluid import core from paddle.incubate.layers.nn import _pull_box_sparse @@ -32,8 +32,8 @@ def get_transpile(self, mode, trainers="127.0.0.1:6174"): return t def test_transpile(self): - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() t = self.get_transpile("single_process_multi_thread") t.transpile( trainer_id=0, @@ -56,8 +56,8 @@ def test_single_trainers(self): transpiler = collective.GradAllReduce(0) try: transpiler.transpile( - startup_program=fluid.Program(), - main_program=fluid.Program(), + startup_program=base.Program(), + main_program=base.Program(), rank=1, endpoints="127.0.0.1:6174", current_endpoint="127.0.0.1:6174", @@ -68,8 +68,8 @@ def test_single_trainers(self): transpiler = collective.LocalSGD(0) try: transpiler.transpile( - startup_program=fluid.Program(), - main_program=fluid.Program(), + startup_program=base.Program(), + main_program=base.Program(), rank=1, endpoints="127.0.0.1:6174", current_endpoint="127.0.0.1:6174", @@ -94,8 +94,8 @@ class TestPullBoxSparseOP(unittest.TestCase): def test_pull_box_sparse_op(self): paddle.enable_static() - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): x = paddle.static.data( name='x', shape=[-1, 1], dtype='int64', lod_level=0 ) diff --git a/test/legacy_test/test_broadcast_error.py b/test/legacy_test/test_broadcast_error.py index 8235cef218a34..3e8f924c85f2f 100644 --- a/test/legacy_test/test_broadcast_error.py +++ b/test/legacy_test/test_broadcast_error.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core class TestBroadcastOpCpu(OpTest): diff --git a/test/legacy_test/test_broadcast_tensors_op.py b/test/legacy_test/test_broadcast_tensors_op.py index be37ff0157898..8e691f02fefff 100644 --- a/test/legacy_test/test_broadcast_tensors_op.py +++ b/test/legacy_test/test_broadcast_tensors_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core random.seed(2021) diff --git a/test/legacy_test/test_broadcast_to_op.py b/test/legacy_test/test_broadcast_to_op.py index e2da6a1117296..331addd30909b 100644 --- a/test/legacy_test/test_broadcast_to_op.py +++ b/test/legacy_test/test_broadcast_to_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -26,8 +26,8 @@ class TestBroadcastToError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) shape = [2, 2] self.assertRaises(TypeError, paddle.tensor.broadcast_to, x1, shape) @@ -55,11 +55,11 @@ def test_api(self): out_2 = paddle.broadcast_to(x, shape=[positive_2, 14]) out_3 = paddle.broadcast_to(x, shape=expand_shape) - g0 = fluid.backward.calc_gradient(out_2, x) + g0 = base.backward.calc_gradient(out_2, x) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": input, "expand_shape": np.array([12, 14]).astype("int32"), @@ -71,7 +71,7 @@ def test_api(self): np.testing.assert_array_equal(res_3, np.tile(input, (1, 1))) def test_api_fp16_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_bucketize_api.py b/test/legacy_test/test_bucketize_api.py index 544b7c2a1ffe7..b8b9ca714a8a3 100644 --- a/test/legacy_test/test_bucketize_api.py +++ b/test/legacy_test/test_bucketize_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_buffer_shared_memory_reuse_pass.py b/test/legacy_test/test_buffer_shared_memory_reuse_pass.py index 930b85a8201da..4eaa5387216f0 100644 --- a/test/legacy_test/test_buffer_shared_memory_reuse_pass.py +++ b/test/legacy_test/test_buffer_shared_memory_reuse_pass.py @@ -19,7 +19,7 @@ from simple_nets import simple_fc_net import paddle -from paddle import fluid +from paddle import base batch_size = 32 @@ -39,38 +39,36 @@ def initParameter(self): def setUp(self): paddle.enable_static() self.initParameter() - if self.use_cuda and fluid.core.is_compiled_with_cuda(): - self.device_count = fluid.core.get_cuda_device_count() + if self.use_cuda and base.core.is_compiled_with_cuda(): + self.device_count = base.core.get_cuda_device_count() else: self.device_count = 4 assert batch_size % self.device_count == 0 def build_program_and_scope(self): - self.place = fluid.CUDAPlace(0) if self.use_cuda else fluid.CPUPlace() + self.place = base.CUDAPlace(0) if self.use_cuda else base.CPUPlace() paddle.seed(1) paddle.framework.random._manual_program_seed(1) - startup_program = fluid.Program() - main_program = fluid.Program() + startup_program = base.Program() + main_program = base.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.unique_name.guard(): + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.unique_name.guard(): loss = simple_fc_net() adam = paddle.optimizer.Adam(learning_rate=1e-3) adam.minimize(loss) - with fluid.scope_guard(scope): - exe = fluid.Executor( - fluid.CUDAPlace(0) - if self.use_cuda - else fluid.CPUPlace() + with base.scope_guard(scope): + exe = base.Executor( + base.CUDAPlace(0) if self.use_cuda else base.CPUPlace() ) exe.run(startup_program) return main_program, scope, exe, loss def is_invalid_test(self): - return self.use_cuda and not fluid.core.is_compiled_with_cuda() + return self.use_cuda and not base.core.is_compiled_with_cuda() def get_all_vars(self, program): all_vars = program.global_block().vars @@ -92,13 +90,13 @@ def check_single_card_fetch_var(self): for enable_inplace in [False, True]: prog, scope, _, loss = self.build_program_and_scope() scopes.append(scope) - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.memory_optimize = memory_optimize build_strategy.enable_inplace = enable_inplace build_strategy.fuse_all_optimizer_ops = ( self.fuse_all_optimizer_ops ) - compiled_prog = fluid.CompiledProgram( + compiled_prog = base.CompiledProgram( prog, build_strategy=build_strategy ) compiled_programs.append(compiled_prog) @@ -109,13 +107,13 @@ def check_single_card_fetch_var(self): for fetch_var in repeated_var_names[:4]: for _ in range(2): - with fluid.scope_guard(scope1): + with base.scope_guard(scope1): (fetch_val1,) = exe.run( prog1, feed=feed_dict, fetch_list=[fetch_var] ) for scope, compiled_prog in zip(scopes, compiled_programs): - with fluid.scope_guard(scope): + with base.scope_guard(scope): (fetch_val2,) = exe.run( compiled_prog, feed=feed_dict, diff --git a/test/legacy_test/test_build_strategy_fusion_group_pass.py b/test/legacy_test/test_build_strategy_fusion_group_pass.py index e635479e9ea45..14400a0c2f16b 100644 --- a/test/legacy_test/test_build_strategy_fusion_group_pass.py +++ b/test/legacy_test/test_build_strategy_fusion_group_pass.py @@ -17,8 +17,8 @@ from test_eager_deletion_padding_rnn import PaddingRNNTestBase, RNNConfig import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class FusionGroupPaddingRNNTest(PaddingRNNTestBase): @@ -27,12 +27,12 @@ def set_customed_config(self): # Use CUDA executor if core.is_compiled_with_cuda(): - self.exe = fluid.Executor(fluid.CUDAPlace(0)) + self.exe = base.Executor(base.CUDAPlace(0)) def test_train_enable_fusion_group(self): rnn_model = "static" config = RNNConfig("test", rnn_model) - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): self.train(config, use_program_cache=False) diff --git a/test/legacy_test/test_c_comm_init_all_op.py b/test/legacy_test/test_c_comm_init_all_op.py index 61d86fae2dd28..c87018d4aceea 100644 --- a/test/legacy_test/test_c_comm_init_all_op.py +++ b/test/legacy_test/test_c_comm_init_all_op.py @@ -15,29 +15,29 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestCCommInitAllOp(unittest.TestCase): def setUp(self): - self.place = fluid.CUDAPlace(0) - self.exe = fluid.Executor(self.place) + self.place = base.CUDAPlace(0) + self.exe = base.Executor(self.place) def test_default_attrs(self): - program = fluid.Program() + program = base.Program() block = program.global_block() block.append_op(type='c_comm_init_all', attrs={'ring_id': 0}) self.exe.run(program) def test_init_with_same_ring_id(self): - program = fluid.Program() + program = base.Program() block = program.global_block() block.append_op(type='c_comm_init_all', attrs={'ring_id': 0}) with self.assertRaises(ValueError): self.exe.run(program) def test_specifying_devices(self): - program = fluid.Program() + program = base.Program() block = program.global_block() block.append_op( type='c_comm_init_all', attrs={'devices': [0], 'ring_id': 1} diff --git a/test/legacy_test/test_calc_gradient.py b/test/legacy_test/test_calc_gradient.py index 4cecbd5273650..945acf18bb932 100644 --- a/test/legacy_test/test_calc_gradient.py +++ b/test/legacy_test/test_calc_gradient.py @@ -17,34 +17,34 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.backward import calc_gradient +from paddle import base +from paddle.base.backward import calc_gradient paddle.enable_static() class TestCalcGradient(unittest.TestCase): def test_calc_gradient(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): x = paddle.create_parameter(dtype="float32", shape=[5, 10]) y = paddle.create_parameter(dtype="float32", shape=[10, 8]) mul_out = paddle.matmul(x=x, y=y) mean_out = paddle.mean(mul_out) a = calc_gradient(mean_out, mul_out) b = calc_gradient(mean_out, x) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup) exe.run(main, feed={}, fetch_list=[a, b]) class TestDoubleGrad(unittest.TestCase): def test1(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): net = lambda x: x * x x = paddle.create_parameter( name='x', @@ -52,21 +52,21 @@ def test1(self): dtype='float32', default_initializer=paddle.nn.initializer.Constant(3), ) - (grad1,) = fluid.gradients(net(x), x) # 2x = 6 + (grad1,) = base.gradients(net(x), x) # 2x = 6 z = net(x - grad1) - (grad2,) = fluid.gradients(z, x) # gradients( (x - 2x)^2) = 2x = 6 + (grad2,) = base.gradients(z, x) # gradients( (x - 2x)^2) = 2x = 6 - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup) out = exe.run(main, fetch_list=[grad1.name, grad2.name]) self.assertEqual(6, out[0][0]) self.assertEqual(6, out[1][0]) def test2(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): x = paddle.create_parameter( name='x', shape=[1], @@ -74,12 +74,12 @@ def test2(self): default_initializer=paddle.nn.initializer.Constant(1), ) y = x * x - (dx1,) = fluid.gradients(y, x) + (dx1,) = base.gradients(y, x) z = dx1 * dx1 + y * y - (dx2,) = fluid.gradients(z, x) + (dx2,) = base.gradients(z, x) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup) (out,) = exe.run(main, fetch_list=[dx2]) self.assertEqual(12, out[0]) @@ -87,16 +87,16 @@ def test2(self): class TestGradientWithPrune(unittest.TestCase): def test_prune(self): - with paddle.fluid.scope_guard(paddle.static.Scope()): + with paddle.base.scope_guard(paddle.static.Scope()): x = paddle.static.data(name='x', shape=[3], dtype='float32') x.stop_gradient = False x1, x2, x3 = paddle.split(x, axis=0, num_or_sections=3) y = x1 * 2 - x1_grad = fluid.gradients(y, x) + x1_grad = base.gradients(y, x) - exe = fluid.Executor(fluid.CPUPlace()) - main = fluid.default_main_program() - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + main = base.default_main_program() + exe.run(base.default_startup_program()) out = exe.run( main, feed={'x': np.ones([3]).astype('float32')}, @@ -127,7 +127,7 @@ def build_program(self): return start_prog, main_prog, [grad_x, jvp] def test_calc_gradient(self): - with paddle.fluid.scope_guard(paddle.static.Scope()): + with paddle.base.scope_guard(paddle.static.Scope()): start_prog, main_prog, fetch_list = self.build_program() exe = paddle.static.Executor() exe.run(start_prog) @@ -167,7 +167,7 @@ def build_program(self): return start_prog, main_prog, [grad_x, jvp] def test_calc_gradient(self): - with paddle.fluid.scope_guard(paddle.static.Scope()): + with paddle.base.scope_guard(paddle.static.Scope()): start_prog, main_prog, fetch_list = self.build_program() exe = paddle.static.Executor() exe.run(start_prog) diff --git a/test/legacy_test/test_case.py b/test/legacy_test/test_case.py index 48cca3b63ec6d..294f43542bfe6 100644 --- a/test/legacy_test/test_case.py +++ b/test/legacy_test/test_case.py @@ -18,10 +18,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.framework import Program, program_guard paddle.enable_static() @@ -84,11 +84,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, fetch_list=[out_0, out_1, out_2, out_3, out_4] @@ -145,11 +145,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, fetch_list=[out_0, out_1, out_2, out_3, out_4] @@ -180,11 +180,11 @@ def test_0d_tensor_backward(self): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=[out.name, x.grad_name]) np.testing.assert_allclose( @@ -289,11 +289,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(main_program, fetch_list=out) np.testing.assert_allclose( @@ -411,11 +411,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=[out_1, out_2, out_3]) @@ -511,11 +511,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=[out_1, out_2, out_3]) @@ -630,15 +630,15 @@ def fn_2(): pred_fn_pairs=[(switch_id == one, fn_1)], default=fn_2 ) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) for epoch in range(EPOCH_NUM): np.random.seed(epoch) feed_image = np.random.random(size=[BATCH_SIZE, INPUT_SIZE]).astype( 'float32' ) - main_program = fluid.default_main_program() + main_program = base.default_main_program() out = exe.run( main_program, feed={ diff --git a/test/legacy_test/test_cast_op.py b/test/legacy_test/test_cast_op.py index d1353b24f49f5..e3bae330f0910 100644 --- a/test/legacy_test/test_cast_op.py +++ b/test/legacy_test/test_cast_op.py @@ -24,8 +24,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def cast_wrapper(x, out_dtype=None): @@ -166,15 +166,15 @@ class TestCastOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # The input type of cast_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.cast, x1, 'int32') class TestCastOpEager(unittest.TestCase): def test_eager(self): - with paddle.fluid.dygraph.base.guard(): + with paddle.base.dygraph.base.guard(): x = paddle.ones([2, 2], dtype="float16") x.stop_gradient = False out = paddle.cast(x, "float32") @@ -210,9 +210,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -241,9 +241,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_channel_shuffle.py b/test/legacy_test/test_channel_shuffle.py index f8b6ef1df9514..2ade43213190d 100644 --- a/test/legacy_test/test_channel_shuffle.py +++ b/test/legacy_test/test_channel_shuffle.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def channel_shuffle_np(x, groups, data_format="NCHW"): @@ -107,14 +107,14 @@ def test_static_graph_functional(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, ) res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -147,14 +147,14 @@ def test_static_graph_layer(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, ) res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -211,28 +211,28 @@ def test_dygraph2(self): class TestChannelShuffleError(unittest.TestCase): def test_error_functional(self): def error_input(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle(paddle.to_tensor(x), 3) self.assertRaises(ValueError, error_input) def error_groups_1(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle(paddle.to_tensor(x), 3.33) self.assertRaises(TypeError, error_groups_1) def error_groups_2(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle(paddle.to_tensor(x), -1) self.assertRaises(ValueError, error_groups_2) def error_data_format(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") channel_shuffle = F.channel_shuffle( paddle.to_tensor(x), 3, "WOW" @@ -242,7 +242,7 @@ def error_data_format(): def test_error_layer(self): def error_input_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([9, 4, 4]).astype("float64") cs = paddle.nn.ChannelShuffle(3) cs(paddle.to_tensor(x)) @@ -250,21 +250,21 @@ def error_input_layer(): self.assertRaises(ValueError, error_input_layer) def error_groups_layer_1(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") cs = paddle.nn.ChannelShuffle(3.33) self.assertRaises(TypeError, error_groups_layer_1) def error_groups_layer_2(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") cs = paddle.nn.ChannelShuffle(-1) self.assertRaises(ValueError, error_groups_layer_2) def error_data_format_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") cs = paddle.nn.ChannelShuffle(3, "MEOW") diff --git a/test/legacy_test/test_checkpoint_saver.py b/test/legacy_test/test_checkpoint_saver.py index b6ee6e28c7b06..643ea78816579 100644 --- a/test/legacy_test/test_checkpoint_saver.py +++ b/test/legacy_test/test_checkpoint_saver.py @@ -14,8 +14,8 @@ import unittest +from paddle.base.incubate.checkpoint.checkpoint_saver import CheckpointSaver from paddle.distributed.fleet.utils.fs import HDFSClient -from paddle.fluid.incubate.checkpoint.checkpoint_saver import CheckpointSaver class CheckpointerSaverTest(unittest.TestCase): diff --git a/test/legacy_test/test_cholesky_op.py b/test/legacy_test/test_cholesky_op.py index ecb59fa5c4c50..daacb067cdff8 100644 --- a/test/legacy_test/test_cholesky_op.py +++ b/test/legacy_test/test_cholesky_op.py @@ -20,8 +20,8 @@ from gradient_checker import grad_check import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core @skip_check_grad_ci( @@ -61,9 +61,9 @@ def test_check_output(self): self.check_output() def test_check_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and (not core.is_compiled_with_rocm()): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -71,8 +71,8 @@ def test_check_grad(self): def func(self, place): # use small size since Jacobian gradients is time consuming root_data = self.root_data[..., :3, :3] - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): root = paddle.create_parameter( dtype=root_data.dtype, shape=root_data.shape ) @@ -98,7 +98,7 @@ def init_config(self): class TestDygraph(unittest.TestCase): def test_dygraph(self): if core.is_compiled_with_rocm(): - paddle.disable_static(place=fluid.CPUPlace()) + paddle.disable_static(place=base.CPUPlace()) else: paddle.disable_static() a = np.random.rand(3, 3) @@ -110,12 +110,12 @@ def test_dygraph(self): class TestCholeskySingularAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda() and (not core.is_compiled_with_rocm()): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place, with_out=False): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float64" ) @@ -123,10 +123,10 @@ def check_static_result(self, place, with_out=False): input_np = np.zeros([4, 4]).astype("float64") - exe = fluid.Executor(place) + exe = base.Executor(place) try: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -141,14 +141,14 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.array( [ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], ] ).astype("float64") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) try: result = paddle.cholesky(input) except RuntimeError as ex: diff --git a/test/legacy_test/test_cholesky_solve_op.py b/test/legacy_test/test_cholesky_solve_op.py index 37268c84bd4a3..413119ecc7587 100644 --- a/test/legacy_test/test_cholesky_solve_op.py +++ b/test/legacy_test/test_cholesky_solve_op.py @@ -23,8 +23,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard paddle.enable_static() @@ -171,7 +171,7 @@ def setUp(self): def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data(name="x", shape=[10, 2], dtype=self.dtype) y = paddle.static.data(name="y", shape=[10, 10], dtype=self.dtype) z = paddle.linalg.cholesky_solve(x, y, upper=self.upper) @@ -185,9 +185,9 @@ def check_static_result(self, place): z_np = cholesky_solution(umat, x_np, upper=self.upper) z2_np = scipy_cholesky_solution(umat, x_np, upper=self.upper) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "y": umat}, fetch_list=[z], ) @@ -243,11 +243,11 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # The input type of solve_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) - y1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + y1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.linalg.cholesky_solve, x1, y1) diff --git a/test/legacy_test/test_chunk_op.py b/test/legacy_test/test_chunk_op.py index aea019e630f44..f682ab0c80b05 100644 --- a/test/legacy_test/test_chunk_op.py +++ b/test/legacy_test/test_chunk_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard class TestChunkOpError(unittest.TestCase): @@ -52,7 +52,7 @@ def test_axis_type_tensor(): self.assertRaises(TypeError, test_axis_type_tensor) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_0_chunks_tensor(): x = paddle.uniform([1, 1, 1], dtype='float32') @@ -63,7 +63,7 @@ def test_0_chunks_tensor(): class API_TestChunk(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( 'data1', shape=[4, 6, 6], dtype='float64' ) @@ -88,7 +88,7 @@ def test_out(self): class API_TestChunk1(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( 'data1', shape=[4, 6, 6], dtype='float64' ) @@ -109,10 +109,10 @@ def test_out(self): class API_TestDygraphChunk(unittest.TestCase): def test_out1(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] - input = fluid.dygraph.to_variable(input_1) + input = base.dygraph.to_variable(input_1) x0, x1, x2 = paddle.chunk(input, chunks=3, axis=1) x0_out = x0.numpy() x1_out = x1.numpy() @@ -123,10 +123,10 @@ def test_out1(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_out2(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("bool") # input is a variable which shape is [4, 6, 6] - input = fluid.dygraph.to_variable(input_1) + input = base.dygraph.to_variable(input_1) x0, x1, x2 = paddle.chunk(input, chunks=3, axis=1) x0_out = x0.numpy() x1_out = x1.numpy() @@ -137,10 +137,10 @@ def test_out2(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_axis_tensor_input(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] - input = fluid.dygraph.to_variable(input_1) + input = base.dygraph.to_variable(input_1) num1 = paddle.full(shape=[1], fill_value=1, dtype='int32') x0, x1, x2 = paddle.chunk(input, chunks=3, axis=num1) x0_out = x0.numpy() diff --git a/test/legacy_test/test_class_center_sample_op.py b/test/legacy_test/test_class_center_sample_op.py index 8e064731e76b9..ffc7350e0f703 100644 --- a/test/legacy_test/test_class_center_sample_op.py +++ b/test/legacy_test/test_class_center_sample_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard def class_center_sample_numpy(label, classes_list, num_samples): @@ -131,9 +131,9 @@ def setUp(self): self.initParams() np.random.seed(self.seed) paddle.framework.random._manual_program_seed(2021) - self.places = [paddle.fluid.CPUPlace()] + self.places = [paddle.base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(paddle.fluid.CUDAPlace(0)) + self.places.append(paddle.base.CUDAPlace(0)) def initParams(self): self.batch_size = 10 @@ -173,9 +173,9 @@ def check_static_result(self, place): ) = class_center_sample_numpy( label_np, [self.num_classes], self.num_samples ) - exe = paddle.fluid.Executor(place) + exe = paddle.base.Executor(place) [remapped_label_res, sampled_class_index_res] = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={'label': label_np}, fetch_list=[remapped_label, sampled_class_index], ) @@ -192,7 +192,7 @@ def test_dynamic(self): self.check_dynamic_result(place=place) def check_dynamic_result(self, place): - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): label_np = np.random.randint( 0, self.num_classes, (self.batch_size,), dtype=self.dtype ) @@ -230,9 +230,9 @@ class TestClassCenterSampleAPIError(unittest.TestCase): def setUp(self): self.initParams() np.random.seed(self.seed) - self.places = [paddle.fluid.CPUPlace()] + self.places = [paddle.base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(paddle.fluid.CUDAPlace(0)) + self.places.append(paddle.base.CUDAPlace(0)) def initParams(self): self.batch_size = 20 @@ -247,7 +247,7 @@ def init_dtype(self): def test_dynamic_errors(self): def test_num_samples(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): label_np = np.random.randint( 0, self.num_classes, @@ -270,9 +270,9 @@ class TestClassCenterSampleAPIError1(unittest.TestCase): def setUp(self): self.initParams() np.random.seed(self.seed) - self.places = [paddle.fluid.CPUPlace()] + self.places = [paddle.base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(paddle.fluid.CUDAPlace(0)) + self.places.append(paddle.base.CUDAPlace(0)) def initParams(self): self.batch_size = 5 @@ -287,7 +287,7 @@ def init_dtype(self): def test_dynamic_errors(self): def test_empty_label(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): label = paddle.to_tensor(np.array([], dtype=self.dtype)) ( @@ -299,7 +299,7 @@ def test_empty_label(): def test_group_value(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): label_np = np.random.randint( 0, self.num_classes, diff --git a/test/legacy_test/test_clip_by_norm_op.py b/test/legacy_test/test_clip_by_norm_op.py index da78ee681aac0..8319255bc925d 100644 --- a/test/legacy_test/test_clip_by_norm_op.py +++ b/test/legacy_test/test_clip_by_norm_op.py @@ -19,7 +19,7 @@ from op import Operator import paddle -from paddle.fluid import core +from paddle.base import core from paddle.nn import clip diff --git a/test/legacy_test/test_clip_op.py b/test/legacy_test/test_clip_op.py index b807d01ada068..354bc1ae95cad 100644 --- a/test/legacy_test/test_clip_op.py +++ b/test/legacy_test/test_clip_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestClipOp(OpTest): @@ -277,11 +277,11 @@ def test_clip(self): max = paddle.static.data(name='max', shape=[1], dtype='float32') place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) out_1 = self._executed_api(images, min=min, max=max) out_2 = self._executed_api(images, min=0.2, max=0.9) @@ -314,7 +314,7 @@ def test_clip(self): res10, res11, ) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "image": data, "min": np.array([0.2]).astype('float32'), @@ -357,9 +357,9 @@ def test_clip(self): def test_clip_dygraph(self): paddle.disable_static() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) paddle.disable_static(place) data_shape = [1, 9, 9, 4] @@ -442,7 +442,7 @@ def test_fp16(self): min = paddle.static.data(name='min1', shape=[1], dtype='float16') max = paddle.static.data(name='max1', shape=[1], dtype='float16') out = paddle.clip(images, min, max) - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) exe = paddle.static.Executor(place) res1 = exe.run( diff --git a/test/legacy_test/test_coalesce_tensor_op.py b/test/legacy_test/test_coalesce_tensor_op.py index a4fa649694ca7..46be0e58bec2e 100644 --- a/test/legacy_test/test_coalesce_tensor_op.py +++ b/test/legacy_test/test_coalesce_tensor_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def coalesce_tensor_eager_api( @@ -59,7 +59,7 @@ class TestAllocContinuousSpace(OpTest): def setUp(self): self.python_api = coalesce_tensor_eager_api self.op_type = "coalesce_tensor" - self.dtype, self.fluid_dtype = self.init_dtype() + self.dtype, self.base_dtype = self.init_dtype() self.attrs = self.init_attr() self.Inputs = self.init_input() self.Outputs, self.FusedOutput = self.init_output( @@ -86,7 +86,7 @@ def init_attr(self): "copy_data": True, "set_constant": False, "constant": 0.0, - "dtype": self.fluid_dtype, + "dtype": self.base_dtype, } def init_output(self, input_list, set_constant, constant): @@ -114,9 +114,9 @@ def init_output(self, input_list, set_constant, constant): return outputs, coalesce_tensor_var def verify_output(self, place): - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): tensor_input = [ - fluid.dygraph.base.to_variable(value=data[1]) + base.dygraph.base.to_variable(value=data[1]) for data in self.inputs["Input"] ] eager_outputs, eager_fused_output = coalesce_tensor_eager_api( @@ -177,7 +177,7 @@ def init_attr(self): "copy_data": False, "set_constant": True, "constant": 0.5, - "dtype": self.fluid_dtype, + "dtype": self.base_dtype, "user_defined_size_of_dtype": 2, } diff --git a/test/legacy_test/test_collective_api_base.py b/test/legacy_test/test_collective_api_base.py index b736b1bd82694..375653d5e7b19 100644 --- a/test/legacy_test/test_collective_api_base.py +++ b/test/legacy_test/test_collective_api_base.py @@ -28,8 +28,8 @@ import paddle import paddle.distributed as dist -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def create_bool_test_data(shape=None, seed=None): @@ -119,8 +119,8 @@ def get_model( ) def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -131,14 +131,14 @@ def run_trainer(self, args): paddle.distributed.init_parallel_env() if args['backend'] == 'nccl': device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace( + place = base.CUDAPlace( device_id - ) # if args.use_gpu else fluid.CPUPlace() + ) # if args.use_gpu else base.CPUPlace() elif args['backend'] == 'bkcl': device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: - place = fluid.CPUPlace() + place = base.CPUPlace() indata = create_test_data( shape=(10, 1000), dtype=args["dtype"], seed=os.getpid() ) @@ -154,7 +154,7 @@ def run_trainer(self, args): if args["use_comm_context"] else self.get_model(train_prog, startup_prog, rank) ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) fetch_list = [] for elem in result: diff --git a/test/legacy_test/test_collective_base.py b/test/legacy_test/test_collective_base.py index 453d5abec61da..52bf62591a7b5 100644 --- a/test/legacy_test/test_collective_base.py +++ b/test/legacy_test/test_collective_base.py @@ -24,9 +24,9 @@ import numpy as np -import paddle.fluid.unique_name as nameGen -from paddle import fluid -from paddle.fluid import core +import paddle.base.unique_name as nameGen +from paddle import base +from paddle.base import core class TestCollectiveRunnerBase: @@ -104,8 +104,8 @@ def initCommunicator( ) def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -116,10 +116,10 @@ def run_trainer(self, args): self.rank = rank result = self.get_model(train_prog, startup_prog) device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace( + place = base.CUDAPlace( device_id - ) # if args.use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) + ) # if args.use_gpu else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) np.random.seed(os.getpid()) indata = np.random.random((10, 1000)) diff --git a/test/legacy_test/test_communicator_geo.py b/test/legacy_test/test_communicator_geo.py index 1b1713c0601eb..e2b84702c8e94 100644 --- a/test/legacy_test/test_communicator_geo.py +++ b/test/legacy_test/test_communicator_geo.py @@ -21,7 +21,7 @@ import numpy import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker from paddle.distributed.utils.launch_utils import find_free_ports @@ -39,7 +39,7 @@ def net(self): emb = paddle.static.nn.embedding( input=x1, size=[10000, 10], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -78,8 +78,8 @@ def run_pserver(self, role, strategy): fleet.run_server() def run_trainer(self, role, strategy): - place = fluid.core.CPUPlace() - exe = fluid.Executor(place) + place = base.core.CPUPlace() + exe = base.Executor(place) fleet.init(role) avg_cost, x, z, y = self.net() @@ -87,15 +87,15 @@ def run_trainer(self, role, strategy): optimizer = fleet.distributed_optimizer(optimizer, strategy) optimizer.minimize(avg_cost) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) fleet.init_worker() train_reader = paddle.batch(self.fake_reader(), batch_size=24) - feeder = fluid.DataFeeder(place=place, feed_list=[x, z, y]) + feeder = base.DataFeeder(place=place, feed_list=[x, z, y]) for batch_id, data in enumerate(train_reader()): exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=feeder.feed(data), fetch_list=[], ) diff --git a/test/legacy_test/test_communicator_ps_gpu.py b/test/legacy_test/test_communicator_ps_gpu.py index f487366e51693..4afc5eef90fee 100644 --- a/test/legacy_test/test_communicator_ps_gpu.py +++ b/test/legacy_test/test_communicator_ps_gpu.py @@ -21,7 +21,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -80,7 +80,7 @@ def test_communicator_ps_gpu(self): dataset.load_into_memory(is_shuffle=True) os.environ["TEST_MODE"] = "1" - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) main_program._fleet_opt = {"stat_var_names": [x.name]} fleet.init_worker() diff --git a/test/legacy_test/test_compare_op.py b/test/legacy_test/test_compare_op.py index 2f4e12f2b4e40..85bdc6cd11838 100755 --- a/test/legacy_test/test_compare_op.py +++ b/test/legacy_test/test_compare_op.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def create_test_class(op_type, typename, callback): @@ -73,7 +73,7 @@ def setUp(self): self.input_x = np.array([1, 2, 3, 4]).astype(np.int64) self.input_y = np.array([1, 3, 2, 4]).astype(np.int64) self.real_result = callback(self.input_x, self.input_y) - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() if core.is_compiled_with_cuda(): self.place = paddle.CUDAPlace(0) @@ -84,7 +84,7 @@ def test_api(self): y = paddle.static.data(name='y', shape=[4], dtype='int64') op = eval("paddle.%s" % (self.op_type)) out = op(x, y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) (res,) = exe.run( feed={"x": self.input_x, "y": self.input_y}, fetch_list=[out], @@ -99,7 +99,7 @@ def test_api_float(self): y = paddle.static.data(name='y', shape=[], dtype='int64') op = eval("paddle.%s" % (self.op_type)) out = op(x, y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) (res,) = exe.run( feed={"x": self.input_x, "y": 1.0}, fetch_list=[out] ) @@ -481,8 +481,8 @@ def test_errors(self): with program_guard(Program(), Program()): # The input x and y of compare_op must be Variable. x = paddle.static.data(name='x', shape=[-1, 1], dtype="float32") - y = fluid.create_lod_tensor( - numpy.array([[-1]]), [[1]], fluid.CPUPlace() + y = base.create_lod_tensor( + numpy.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.greater_equal, x, y) @@ -490,33 +490,33 @@ def test_errors(self): class API_TestElementwise_Equal(unittest.TestCase): def test_api(self): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): label = paddle.assign(np.array([3, 3], dtype="int32")) limit = paddle.assign(np.array([3, 2], dtype="int32")) out = paddle.equal(x=label, y=limit) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) (res,) = exe.run(fetch_list=[out]) self.assertEqual((res == np.array([True, False])).all(), True) - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): label = paddle.assign(np.array([3, 3], dtype="int32")) limit = paddle.assign(np.array([3, 3], dtype="int32")) out = paddle.equal(x=label, y=limit) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) (res,) = exe.run(fetch_list=[out]) self.assertEqual((res == np.array([True, True])).all(), True) def test_api_fp16(self): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): label = paddle.to_tensor([3, 3], dtype="float16") limit = paddle.to_tensor([3, 2], dtype="float16") out = paddle.equal(x=label, y=limit) if core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) + exe = base.Executor(place) (res,) = exe.run(fetch_list=[out]) self.assertEqual((res == np.array([True, False])).all(), True) @@ -546,7 +546,7 @@ def test_place_1(self): label = paddle.assign(np.array([3, 3], dtype="int32")) limit = paddle.assign(np.array([3, 2], dtype="int32")) out = paddle.less_than(label, limit) - exe = fluid.Executor(place) + exe = base.Executor(place) (res,) = exe.run(fetch_list=[out]) self.assertEqual((res == np.array([False, False])).all(), True) diff --git a/test/legacy_test/test_compiled_program.py b/test/legacy_test/test_compiled_program.py index ac29c33174fb7..f2a135e025f4e 100644 --- a/test/legacy_test/test_compiled_program.py +++ b/test/legacy_test/test_compiled_program.py @@ -19,8 +19,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestCompiledProgram(unittest.TestCase): @@ -34,17 +34,17 @@ def setUp(self): paddle.seed(self.seed) paddle.framework.random._manual_program_seed(self.seed) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) loss = simple_fc_net() - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) (loss_data,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"image": self.img, "label": self.label}, fetch_list=[loss.name], ) @@ -55,15 +55,15 @@ def test_compiled_program_base(self): paddle.seed(self.seed) paddle.framework.random._manual_program_seed(self.seed) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) loss = simple_fc_net() - exe.run(fluid.default_startup_program()) - compiled_prog = fluid.CompiledProgram(fluid.default_main_program()) + exe.run(base.default_startup_program()) + compiled_prog = base.CompiledProgram(base.default_main_program()) (loss_data,) = exe.run( compiled_prog, @@ -75,7 +75,7 @@ def test_compiled_program_base(self): class TestCompiledProgramError(unittest.TestCase): def test_program_or_graph_error(self): - self.assertRaises(TypeError, fluid.CompiledProgram, "program") + self.assertRaises(TypeError, base.CompiledProgram, "program") def build_simple_model(self): img = paddle.static.data( @@ -89,14 +89,14 @@ def build_simple_model(self): avg_loss = paddle.mean(loss) def compile_program(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): # build model self.build_simple_model() # compile program - program = fluid.default_main_program() - compiled_program = fluid.CompiledProgram(program) - scope = fluid.global_scope() - place = fluid.CPUPlace() + program = base.default_main_program() + compiled_program = base.CompiledProgram(program) + scope = base.global_scope() + place = base.CPUPlace() compiled_program._compile(scope, place) return compiled_program, scope, place @@ -110,7 +110,7 @@ def test_compile_place_error(self): # need create different place if core.is_compiled_with_cuda(): compiled_program, scope, _ = self.compile_program() - new_place = fluid.CUDAPlace(0) + new_place = base.CUDAPlace(0) with self.assertRaises(ValueError): compiled_program._compile(scope, new_place) diff --git a/test/legacy_test/test_complex_abs.py b/test/legacy_test/test_complex_abs.py index 11c0fbc2b735e..845308b9bb026 100644 --- a/test/legacy_test/test_complex_abs.py +++ b/test/legacy_test/test_complex_abs.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg class TestComplexAbsOp(OpTest): @@ -30,7 +30,7 @@ def setUp(self): self.shape = (2, 3, 4, 5) self.init_input_output() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Out': self.out} def init_input_output(self): @@ -58,7 +58,7 @@ def setUp(self): self.shape = (2, 3, 4, 5) self.init_input_output() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Out': self.out} def init_input_output(self): @@ -102,7 +102,7 @@ def setUp(self): self.shape = (2, 3, 4, 5) self.init_input_output() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Out': self.out} def init_input_output(self): diff --git a/test/legacy_test/test_complex_elementwise_layers.py b/test/legacy_test/test_complex_elementwise_layers.py index cee39ea2d363e..fe23f28715535 100644 --- a/test/legacy_test/test_complex_elementwise_layers.py +++ b/test/legacy_test/test_complex_elementwise_layers.py @@ -18,8 +18,8 @@ from numpy.random import random as rand import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base paddle_apis = { "add": paddle.add, @@ -33,7 +33,7 @@ class TestComplexElementwiseLayers(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] self._places = [paddle.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self._places.append(paddle.CUDAPlace(0)) def paddle_calc(self, x, y, op, place): diff --git a/test/legacy_test/test_complex_getitem.py b/test/legacy_test/test_complex_getitem.py index 2214e9d7cc240..a3b249212e93d 100644 --- a/test/legacy_test/test_complex_getitem.py +++ b/test/legacy_test/test_complex_getitem.py @@ -16,15 +16,15 @@ import numpy as np -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class TestComplexGetitemLayer(unittest.TestCase): def setUp(self): - self._places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self._places.append(fluid.CUDAPlace(0)) + self._places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self._places.append(base.CUDAPlace(0)) def test_case1(self): x_np = np.random.randn(2, 3, 4) + 1j * np.random.randn(2, 3, 4) diff --git a/test/legacy_test/test_complex_grad_accumulated.py b/test/legacy_test/test_complex_grad_accumulated.py index 0502a914da997..bf76f1d248fa5 100644 --- a/test/legacy_test/test_complex_grad_accumulated.py +++ b/test/legacy_test/test_complex_grad_accumulated.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class Optimization_ex1(paddle.nn.Layer): diff --git a/test/legacy_test/test_complex_kron.py b/test/legacy_test/test_complex_kron.py index 8588afa842313..fe6a14bf56b89 100644 --- a/test/legacy_test/test_complex_kron.py +++ b/test/legacy_test/test_complex_kron.py @@ -17,8 +17,8 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class ComplexKronTestCase(unittest.TestCase): @@ -30,7 +30,7 @@ def __init__(self, methodName='runTest', x=None, y=None): def setUp(self): self.ref_result = np.kron(self.x, self.y) self._places = [paddle.CPUPlace()] - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): self._places.append(paddle.CUDAPlace(0)) def runTest(self): diff --git a/test/legacy_test/test_complex_matmul.py b/test/legacy_test/test_complex_matmul.py index c6a89bb3cecbe..36c0e43511189 100644 --- a/test/legacy_test/test_complex_matmul.py +++ b/test/legacy_test/test_complex_matmul.py @@ -17,16 +17,16 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class TestComplexMatMulLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self._places.append(fluid.CUDAPlace(0)) + self._places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self._places.append(base.CUDAPlace(0)) def compare_by_basic_api(self, x, y, np_result): for place in self._places: diff --git a/test/legacy_test/test_complex_op.py b/test/legacy_test/test_complex_op.py index e0e509d6d41de..910e8945afda4 100644 --- a/test/legacy_test/test_complex_op.py +++ b/test/legacy_test/test_complex_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import dygraph +from paddle.base import dygraph paddle.enable_static() diff --git a/test/legacy_test/test_complex_reshape.py b/test/legacy_test/test_complex_reshape.py index d3a3f09ef8691..14eb0fd75e729 100644 --- a/test/legacy_test/test_complex_reshape.py +++ b/test/legacy_test/test_complex_reshape.py @@ -17,15 +17,15 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class TestComplexReshape(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] self._places = [paddle.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self._places.append(paddle.CUDAPlace(0)) def test_shape_norm_dims(self): diff --git a/test/legacy_test/test_complex_simplenet.py b/test/legacy_test/test_complex_simplenet.py index 34c99234f4ff9..acedc7a3170a8 100644 --- a/test/legacy_test/test_complex_simplenet.py +++ b/test/legacy_test/test_complex_simplenet.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class Optimization_ex1(paddle.nn.Layer): diff --git a/test/legacy_test/test_complex_sum_layer.py b/test/legacy_test/test_complex_sum_layer.py index 9b09fa192575f..2ecbdbef4cfaf 100644 --- a/test/legacy_test/test_complex_sum_layer.py +++ b/test/legacy_test/test_complex_sum_layer.py @@ -18,15 +18,15 @@ from numpy.random import random as rand import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid, tensor +import paddle.base.dygraph as dg +from paddle import base, tensor class TestComplexSumLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] self._places = [paddle.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self._places.append(paddle.CUDAPlace(0)) def test_complex_basic_api(self): diff --git a/test/legacy_test/test_complex_trace_layer.py b/test/legacy_test/test_complex_trace_layer.py index c59917a49194a..3df257fe4e916 100644 --- a/test/legacy_test/test_complex_trace_layer.py +++ b/test/legacy_test/test_complex_trace_layer.py @@ -17,16 +17,16 @@ import numpy as np from numpy.random import random as rand -import paddle.fluid.dygraph as dg -from paddle import fluid, tensor +import paddle.base.dygraph as dg +from paddle import base, tensor class TestComplexTraceLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] - self._places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self._places.append(fluid.CUDAPlace(0)) + self._places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self._places.append(base.CUDAPlace(0)) def test_basic_api(self): for dtype in self._dtypes: diff --git a/test/legacy_test/test_complex_transpose.py b/test/legacy_test/test_complex_transpose.py index 86c2fbc345e66..5ce77e6e00715 100644 --- a/test/legacy_test/test_complex_transpose.py +++ b/test/legacy_test/test_complex_transpose.py @@ -17,15 +17,15 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class TestComplexTransposeLayer(unittest.TestCase): def setUp(self): self._dtypes = ["float32", "float64"] self._places = [paddle.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self._places.append(paddle.CUDAPlace(0)) def test_transpose_by_complex_api(self): diff --git a/test/legacy_test/test_complex_variable.py b/test/legacy_test/test_complex_variable.py index 98dbaa2ba500c..83c4f634a78c5 100644 --- a/test/legacy_test/test_complex_variable.py +++ b/test/legacy_test/test_complex_variable.py @@ -17,10 +17,10 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle.fluid import core -from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +import paddle.base.dygraph as dg +from paddle.base import core +from paddle.base.data_feeder import convert_dtype +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestComplexVariable(unittest.TestCase): diff --git a/test/legacy_test/test_complex_view_op.py b/test/legacy_test/test_complex_view_op.py index 81a5dcba620e9..6055a9781419a 100644 --- a/test/legacy_test/test_complex_view_op.py +++ b/test/legacy_test/test_complex_view_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import dygraph +from paddle.base import dygraph paddle.enable_static() diff --git a/test/legacy_test/test_concat_op.py b/test/legacy_test/test_concat_op.py index db848d7b5cff4..286f59797caed 100644 --- a/test/legacy_test/test_concat_op.py +++ b/test/legacy_test/test_concat_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestConcatOp(OpTest): @@ -419,11 +419,11 @@ def test_errors(self): paddle.concat(x1) # The item in input must be Variable. - x2 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x2 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) - x3 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x3 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.concat, [x2]) # The input dtype of concat_op must be float16, float32, float64, int32, int64. @@ -449,7 +449,7 @@ def test_input_same_dtype(): class TestConcatAPI(unittest.TestCase): - def test_fluid_api(self): + def test_base_api(self): paddle.enable_static() x_1 = paddle.static.data( shape=[None, 1, 4, 5], dtype='int32', name='x_1' @@ -466,9 +466,9 @@ def test_fluid_api(self): out_2 = paddle.concat([x_2, x_3], axis=positive_1_int32) out_3 = paddle.concat([x_2, x_3], axis=positive_1_int64) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) [res_1, res_2, res_3] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_2, "x_2": input_2, "x_3": input_3}, fetch_list=[out_1, out_2, out_3], ) @@ -539,11 +539,11 @@ def test_imperative(self): def test_errors(self): with program_guard(Program(), Program()): # The item in input must be Variable. - x2 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x2 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) - x3 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x3 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.concat, [x2]) # The input dtype of concat_op must be float16, float32, float64, int32, int64. @@ -579,16 +579,16 @@ def setUp(self): self.input_shape = [2, 3] self.x = np.random.random(self.input_shape).astype("float32") self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) - def set_program(self, use_fluid_api): + def set_program(self, use_base_api): paddle.enable_static() - if use_fluid_api: - self.program = fluid.Program() - with fluid.program_guard(self.program): + if use_base_api: + self.program = base.Program() + with base.program_guard(self.program): input = paddle.assign(self.x) tensor_array = paddle.tensor.create_array(dtype='float32') zero = paddle.tensor.fill_constant( @@ -614,16 +614,16 @@ def set_program(self, use_fluid_api): self.out_var = paddle.concat(tensor_array, axis=self.axis) - def test_fluid_api(self): - self._run_static_mode(use_fluid_api=True) + def test_base_api(self): + self._run_static_mode(use_base_api=True) def test_paddle_api(self): - self._run_static_mode(use_fluid_api=False) + self._run_static_mode(use_base_api=False) - def _run_static_mode(self, use_fluid_api): - self.set_program(use_fluid_api) + def _run_static_mode(self, use_base_api): + self.set_program(use_base_api) self.assertTrue(self.out_var.shape[self.axis] == -1) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(self.program, fetch_list=self.out_var) np.testing.assert_array_equal( res[0], np.concatenate([self.x] * self.iter_num, axis=self.axis) @@ -664,9 +664,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -705,9 +705,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_cond.py b/test/legacy_test/test_cond.py index 037ec5c5cce5c..55e6f8116cf33 100644 --- a/test/legacy_test/test_cond.py +++ b/test/legacy_test/test_cond.py @@ -18,10 +18,10 @@ from simple_nets import batchnorm_fc_with_inputs, simple_fc_net_with_inputs import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core, framework +from paddle.base.backward import append_backward +from paddle.base.framework import Program, program_guard np.random.seed(123) @@ -63,11 +63,11 @@ def false_func(): # out is one tensor place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) (ret,) = exe.run(main_program, fetch_list=[out.name]) np.testing.assert_allclose( np.asarray(ret), np.full((3, 2), -1, np.int32), rtol=1e-05 @@ -101,11 +101,11 @@ def false_func(): # out is one tensor place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) (ret,) = exe.run(main_program, fetch_list=[out.name]) np.testing.assert_allclose(np.asarray(ret), np.array(2), rtol=1e-05) self.assertEqual(ret.shape, ()) @@ -138,11 +138,11 @@ def false_func(): # out is a tensor place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) (ret,) = exe.run(main_program, fetch_list=[out.name]) np.testing.assert_allclose( np.asarray(ret), np.full((3, 3), 2, np.int32), rtol=1e-05 @@ -170,12 +170,12 @@ def test_0d_tensor_backward(self): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(main_program, fetch_list=[out.name, a.grad_name]) np.testing.assert_allclose( np.asarray(ret[0]), np.array(2.0), rtol=1e-05 @@ -244,11 +244,11 @@ def false_func(): # out is a tuple containing 2 tensors place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(main_program, fetch_list=out) np.testing.assert_allclose( np.asarray(ret[0]), np.full((1, 2), 1, np.int32), rtol=1e-05 @@ -290,11 +290,11 @@ def false_func(a, i): pred, lambda: true_func(a, i), lambda: false_func(a, i) ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) for feed_i in range(5): expected_a = 7 * (feed_i + 1) if feed_i % 2 == 0 else 8 - feed_i (ret,) = exe.run( @@ -335,11 +335,11 @@ def false_func(): out2 = paddle.static.nn.cond(pred, None, false_func) out3 = paddle.static.nn.cond(pred, true_func, None) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) for feed_i in range(5): # Test that output is None is runnable exe.run(main_program, feed={'i': np.full((1), feed_i, np.int32)}) @@ -409,9 +409,9 @@ def func_return_two_tensors(): def test_extremely_simple_net_with_op_in_condition(self): paddle.enable_static() - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): a = paddle.tensor.fill_constant( shape=[1], dtype='float32', value=1.23 ) @@ -424,11 +424,11 @@ def test_extremely_simple_net_with_op_in_condition(self): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run( main_program, fetch_list=[out, b, a.grad_name, b.grad_name] ) @@ -488,11 +488,11 @@ def greater_equal_branch(i, a): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) for feed_i in range(0, 10): expected_a = 2.0 * feed_i if feed_i < 5: @@ -557,11 +557,11 @@ def greater_equal_branch(i, a): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run( main_program, fetch_list=[out.name, i.grad_name], @@ -577,10 +577,10 @@ def greater_equal_branch(i, a): def test_cond_op_in_condition(self): paddle.enable_static() - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): a = paddle.tensor.fill_constant( shape=[1], dtype='float32', value=1.23 ) @@ -605,11 +605,11 @@ def test_cond_op_in_condition(self): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(main_program, fetch_list=[out, a.grad_name, b.grad_name]) # Note: fill_constant has loss of precision, so we assertAlmostEqual. self.assertAlmostEqual(ret[0][0], 1.5252) @@ -638,8 +638,8 @@ def backward_value_helper(self, cond_func, use_cuda): i = paddle.static.data(name="i", shape=[1], dtype='int32') loss = cond_func(i, img, label) append_backward(loss) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) num_devices = 1 @@ -698,8 +698,8 @@ def add_optimizer_helper(self, cond_func, use_cuda): optimizer = paddle.optimizer.SGD(learning_rate=0.1) optimizer.minimize(loss) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) for feed_i in range(0, 10): diff --git a/test/legacy_test/test_conditional_block.py b/test/legacy_test/test_conditional_block.py index a5383dd6d10f9..90a8200375c65 100644 --- a/test/legacy_test/test_conditional_block.py +++ b/test/legacy_test/test_conditional_block.py @@ -17,17 +17,17 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import Executor, append_backward from paddle.static.nn.control_flow import ConditionalBlock class ConditionalBlockTest(unittest.TestCase): def test_forward(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): data = paddle.static.data(name='X', shape=[-1, 1], dtype='float32') data.stop_gradient = False cond = ConditionalBlock(inputs=[data]) @@ -56,9 +56,9 @@ def test_forward(self): class TestConditionalBlockOpInferShape(unittest.TestCase): def test_infer_shape(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): global_block = main_program.global_block() sub_block = main_program._create_block() main_program._rollback() diff --git a/test/legacy_test/test_conj_op.py b/test/legacy_test/test_conj_op.py index 8e9f90d08516e..4cad7bc276fc1 100644 --- a/test/legacy_test/test_conj_op.py +++ b/test/legacy_test/test_conj_op.py @@ -23,9 +23,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 from numpy.random import random as rand -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg from paddle import static -from paddle.fluid import core +from paddle.base import core paddle.enable_static() @@ -46,7 +46,7 @@ def init_input_output(self): ).astype(self.dtype) out = np.conj(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def test_check_output(self): diff --git a/test/legacy_test/test_const_value.py b/test/legacy_test/test_const_value.py index b26892f8a3607..0bfa04bdb0ea4 100644 --- a/test/legacy_test/test_const_value.py +++ b/test/legacy_test/test_const_value.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import framework +from paddle.base import framework class ConstantTest(unittest.TestCase): diff --git a/test/legacy_test/test_context_manager.py b/test/legacy_test/test_context_manager.py index 43ddca5fb83e1..afdd54b5ede57 100644 --- a/test/legacy_test/test_context_manager.py +++ b/test/legacy_test/test_context_manager.py @@ -14,23 +14,23 @@ import unittest -from paddle import fluid +from paddle import base class TestContextManagerRaiseException(unittest.TestCase): # When exception raised in 'with' context, we should safely exit the context def test_func1(self): def foo(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): print("raise error in context manager") raise TypeError("error") self.assertRaises(TypeError, foo) def test_func2(self): - # After test_func1 executed, if fluid.dygraph.guard() in test_func1 safely exited, - # fluid.in_dygraph_mode() should be false. - self.assertEqual(fluid.in_dygraph_mode(), False) + # After test_func1 executed, if base.dygraph.guard() in test_func1 safely exited, + # base.in_dygraph_mode() should be false. + self.assertEqual(base.in_dygraph_mode(), False) if __name__ == '__main__': diff --git a/test/legacy_test/test_conv1d_layer.py b/test/legacy_test/test_conv1d_layer.py index 5e3ce31f80640..e284c25568abf 100644 --- a/test/legacy_test/test_conv1d_layer.py +++ b/test/legacy_test/test_conv1d_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn class Conv1DTestCase(unittest.TestCase): @@ -84,10 +84,10 @@ def setUp(self): self.bias = None def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, self.num_channels, -1) if not self.channel_last @@ -115,7 +115,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -147,17 +147,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result1, result2) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv1DErrorTestCase(Conv1DTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() @@ -165,7 +165,7 @@ def runTest(self): class Conv1DTypeErrorTestCase(Conv1DTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(TypeError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv1d_transpose_layer.py b/test/legacy_test/test_conv1d_transpose_layer.py index 386b1fbc1a424..a5809cab6b6c0 100644 --- a/test/legacy_test/test_conv1d_transpose_layer.py +++ b/test/legacy_test/test_conv1d_transpose_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn class Conv1DTransposeTestCase(unittest.TestCase): @@ -90,10 +90,10 @@ def setUp(self): self.bias = None def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, self.in_channels, -1) if not self.channel_last @@ -123,7 +123,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -155,17 +155,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result1, result2) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv1DTransposeErrorTestCase(Conv1DTransposeTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv2d_api.py b/test/legacy_test/test_conv2d_api.py index 418cb52776d4f..1b02bf550f6c4 100644 --- a/test/legacy_test/test_conv2d_api.py +++ b/test/legacy_test/test_conv2d_api.py @@ -19,8 +19,8 @@ import paddle paddle.enable_static() -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestConv2DAPI(unittest.TestCase): @@ -318,7 +318,7 @@ def run_1(): ) class TestConv2DEnviron(unittest.TestCase): def run1(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): inputs = paddle.static.data( shape=[2, 3, 5, 5], name="inputs", @@ -334,17 +334,17 @@ def run1(self, place): groups=1, data_format="NCHW", ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"inputs": self.input_np}, fetch_list=[result], ) def run2(self, place): - with fluid.dygraph.guard(place): - inputs = fluid.dygraph.to_variable(self.input_np) + with base.dygraph.guard(place): + inputs = base.dygraph.to_variable(self.input_np) conv = paddle.nn.Conv2D( in_channels=3, out_channels=4, @@ -360,9 +360,9 @@ def run_all(self, place): def test_environ(self): self.input_np = np.random.random([2, 3, 5, 5]).astype("float32") for place in [paddle.CPUPlace(), paddle.CUDAPlace(0)]: - fluid.set_flags({'FLAGS_conv2d_disable_cudnn': False}) + base.set_flags({'FLAGS_conv2d_disable_cudnn': False}) self.run_all(place) - fluid.set_flags({'FLAGS_conv2d_disable_cudnn': True}) + base.set_flags({'FLAGS_conv2d_disable_cudnn': True}) self.run_all(place) diff --git a/test/legacy_test/test_conv2d_fusion_op.py b/test/legacy_test/test_conv2d_fusion_op.py index 2aee55d2c0439..6aff6cb38b967 100644 --- a/test/legacy_test/test_conv2d_fusion_op.py +++ b/test/legacy_test/test_conv2d_fusion_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from test_conv2d_op import conv2d_forward_naive -from paddle.fluid import core +from paddle.base import core def create_test_padding_SAME_class(parent): @@ -116,16 +116,16 @@ def setUp(self): self.output = self.output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), - 'Bias': OpTest.np_dtype_to_fluid_dtype(bias), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), + 'Bias': OpTest.np_dtype_to_base_dtype(bias), } if self.add_residual_data: residual_data = np.random.random(self.output.shape).astype( self.dtype ) - self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype( + self.inputs['ResidualData'] = OpTest.np_dtype_to_base_dtype( residual_data ) self.output += residual_data diff --git a/test/legacy_test/test_conv2d_layer.py b/test/legacy_test/test_conv2d_layer.py index 3c9a18419c2cb..4290a7352afed 100644 --- a/test/legacy_test/test_conv2d_layer.py +++ b/test/legacy_test/test_conv2d_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn def _reverse_repeat_list(t, n): @@ -98,11 +98,11 @@ def setUp(self): else: self.bias = None - def fluid_layer(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + def base_layer(self, place): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, self.num_channels) if self.channel_last @@ -141,16 +141,16 @@ def fluid_layer(self, place): ) feed_dict = {"input": self.input} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, self.num_channels) if self.channel_last @@ -190,7 +190,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -219,8 +219,8 @@ def paddle_nn_layer(self): return y_np, t1 def _test_equivalence(self, place): - place = fluid.CPUPlace() - result1 = self.fluid_layer(place) + place = base.CPUPlace() + result1 = self.base_layer(place) result2 = self.functional(place) with dg.guard(place): result3, g1 = self.paddle_nn_layer() @@ -228,17 +228,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv2DErrorTestCase(Conv2DTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv2d_op.py b/test/legacy_test/test_conv2d_op.py index b7c83f2c6ec71..3d4ffe78b935c 100644 --- a/test/legacy_test/test_conv2d_op.py +++ b/test/legacy_test/test_conv2d_op.py @@ -19,8 +19,8 @@ from testsuite import create_op import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def conv2d_forward_naive( @@ -467,14 +467,14 @@ def setUp(self): 'Filter': convert_float_to_uint16(filter), } self.inputs_fp32 = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } else: output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { @@ -725,8 +725,8 @@ def test_errors(self): def test_Variable(): # the input of conv2d must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.static.nn.conv2d(x1, 1, 1) @@ -798,8 +798,8 @@ def setUp(self): output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/legacy_test/test_conv2d_op_depthwise_conv.py b/test/legacy_test/test_conv2d_op_depthwise_conv.py index 0354446f1d8bb..19e5b335768b0 100644 --- a/test/legacy_test/test_conv2d_op_depthwise_conv.py +++ b/test/legacy_test/test_conv2d_op_depthwise_conv.py @@ -31,7 +31,7 @@ ) from testsuite import create_op -from paddle.fluid import core +from paddle.base import core # ----------------TestDepthwiseConv ----- diff --git a/test/legacy_test/test_conv2d_transpose_layer.py b/test/legacy_test/test_conv2d_transpose_layer.py index 6a7f77fdcdfc8..78634d5124929 100644 --- a/test/legacy_test/test_conv2d_transpose_layer.py +++ b/test/legacy_test/test_conv2d_transpose_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn class Conv2DTransposeTestCase(unittest.TestCase): @@ -89,11 +89,11 @@ def setUp(self): else: self.bias = None - def fluid_layer(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + def base_layer(self, place): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, self.num_channels) if self.channel_last @@ -122,16 +122,16 @@ def fluid_layer(self, place): data_format=self.data_format, ) feed_dict = {"input": self.input} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, self.num_channels) if self.channel_last @@ -167,7 +167,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -199,9 +199,9 @@ def paddle_nn_layer(self): return y_np def _test_equivalence(self, place): - place = fluid.CPUPlace() + place = base.CPUPlace() - result1 = self.fluid_layer(place) + result1 = self.base_layer(place) result2 = self.functional(place) with dg.guard(place): @@ -211,17 +211,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv2DTransposeErrorTestCase(Conv2DTransposeTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv2d_transpose_op.py b/test/legacy_test/test_conv2d_transpose_op.py index b7fe99f85c3c2..28478c886ab7d 100644 --- a/test/legacy_test/test_conv2d_transpose_op.py +++ b/test/legacy_test/test_conv2d_transpose_op.py @@ -25,8 +25,8 @@ from test_attribute_var import UnittestBase from testsuite import create_op -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def conv2dtranspose_forward_naive(input_, filter_, attrs): @@ -1172,10 +1172,10 @@ def test_case1(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data1": data1_np, "data2": data2_np}, fetch_list=[out1, out2, out3, out4, out5, out6, out7], return_numpy=True, diff --git a/test/legacy_test/test_conv3d_layer.py b/test/legacy_test/test_conv3d_layer.py index d3b1dd6960106..da1a21edbc435 100644 --- a/test/legacy_test/test_conv3d_layer.py +++ b/test/legacy_test/test_conv3d_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn class Conv3DTestCase(unittest.TestCase): @@ -85,11 +85,11 @@ def setUp(self): else: self.bias = None - def fluid_layer(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + def base_layer(self, place): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, -1, self.num_channels) if self.channel_last @@ -116,16 +116,16 @@ def fluid_layer(self, place): data_format=self.data_format, ) feed_dict = {"input": self.input} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, -1, self.num_channels) if self.channel_last @@ -153,7 +153,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -181,8 +181,8 @@ def paddle_nn_layer(self): return y_np, t1 def _test_equivalence(self, place): - place = fluid.CPUPlace() - result1 = self.fluid_layer(place) + place = base.CPUPlace() + result1 = self.base_layer(place) result2 = self.functional(place) with dg.guard(place): result3, g1 = self.paddle_nn_layer() @@ -190,17 +190,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv3DErrorTestCase(Conv3DTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv3d_op.py b/test/legacy_test/test_conv3d_op.py index 0d96766323afa..5ae2fb3a0f888 100644 --- a/test/legacy_test/test_conv3d_op.py +++ b/test/legacy_test/test_conv3d_op.py @@ -24,7 +24,7 @@ from testsuite import create_op import paddle -from paddle.fluid import core +from paddle.base import core def conv3d_forward_naive( @@ -420,14 +420,14 @@ def setUp(self): 'Filter': convert_float_to_uint16(filter), } self.inputs_fp32 = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } else: output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { @@ -751,8 +751,8 @@ def setUp(self): ).astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/legacy_test/test_conv3d_transpose_layer.py b/test/legacy_test/test_conv3d_transpose_layer.py index 90ddc1ecfdb00..ecfad7710e4ba 100644 --- a/test/legacy_test/test_conv3d_transpose_layer.py +++ b/test/legacy_test/test_conv3d_transpose_layer.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid, nn +from paddle import base, nn class Conv3DTransposeTestCase(unittest.TestCase): @@ -87,11 +87,11 @@ def setUp(self): -1, 1, size=(self.num_filters,) ).astype(self.dtype) - def fluid_layer(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + def base_layer(self, place): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, -1, self.num_channels) if self.channel_last @@ -119,16 +119,16 @@ def fluid_layer(self, place): data_format=self.data_format, ) feed_dict = {"input": self.input} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): input_shape = ( (-1, -1, -1, -1, self.num_channels) if self.channel_last @@ -157,7 +157,7 @@ def functional(self, place): feed_dict = {"input": self.input, "weight": self.weight} if self.bias is not None: feed_dict["bias"] = self.bias - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -182,8 +182,8 @@ def paddle_nn_layer(self): return y_np def _test_equivalence(self, place): - place = fluid.CPUPlace() - result1 = self.fluid_layer(place) + place = base.CPUPlace() + result1 = self.base_layer(place) result2 = self.functional(place) with dg.guard(place): result3 = self.paddle_nn_layer() @@ -191,17 +191,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class Conv3DTransposeErrorTestCase(Conv3DTransposeTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_nn_layer() diff --git a/test/legacy_test/test_conv3d_transpose_op.py b/test/legacy_test/test_conv3d_transpose_op.py index c0814754cc231..e8cdb7ddf42c7 100644 --- a/test/legacy_test/test_conv3d_transpose_op.py +++ b/test/legacy_test/test_conv3d_transpose_op.py @@ -21,7 +21,7 @@ paddle.enable_static() from eager_op_test import OpTest, copy_bits_from_float_to_uint16 -from paddle.fluid import core +from paddle.base import core def convert_float_to_uint16(float_list, data_format="NCHW"): diff --git a/test/legacy_test/test_conv3d_transpose_part2_op.py b/test/legacy_test/test_conv3d_transpose_part2_op.py index 54d31a7bbd22b..f691e623e4254 100644 --- a/test/legacy_test/test_conv3d_transpose_part2_op.py +++ b/test/legacy_test/test_conv3d_transpose_part2_op.py @@ -22,8 +22,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestWithSymmetricPad_NHWC(TestConv3DTransposeOp): @@ -175,10 +175,10 @@ def test_case1(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data1": data1_np, "data2": data2_np}, fetch_list=[out1, out2, out3, out4, out5, out6, out7], return_numpy=True, diff --git a/test/legacy_test/test_conv_nn_grad.py b/test/legacy_test/test_conv_nn_grad.py index 0b9a1d5da8625..7fb2ba25372cc 100644 --- a/test/legacy_test/test_conv_nn_grad.py +++ b/test/legacy_test/test_conv_nn_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestConvDoubleGradCheck(unittest.TestCase): @@ -28,12 +28,12 @@ class TestConvDoubleGradCheck(unittest.TestCase): def func(self, place): shape = [2, 4, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d(x, 2, 1, groups=1, bias_attr=False) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -42,10 +42,10 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -55,12 +55,12 @@ class TestConvDoubleGradCheckTest0(unittest.TestCase): def func(self, place): shape = [2, 4, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d(x, 2, 1, bias_attr=False) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -69,9 +69,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -81,12 +81,12 @@ class TestConvDoubleGradCheckTest1(unittest.TestCase): def func(self, place): shape = [2, 3, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d(x, 2, 1, padding=1, bias_attr=False) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -95,9 +95,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -107,12 +107,12 @@ class TestConv3DDoubleGradCheck(unittest.TestCase): def func(self, place): shape = [2, 4, 3, 4, 2] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d(x, 2, 1, bias_attr=False) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -121,10 +121,10 @@ def func(self, place): ) def test_grad(self): - # places = [fluid.CPUPlace()] + # places = [base.CPUPlace()] places = [] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -134,12 +134,12 @@ class TestConv3DDoubleGradCheckTest1(unittest.TestCase): def func(self, place): shape = [2, 4, 5, 3, 2] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d(x, 2, 1, padding=1, bias_attr=False) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -148,9 +148,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -160,7 +160,7 @@ class TestConv2DoubleGradCheck_AsyPadding(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d( input=x, @@ -172,7 +172,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -181,9 +181,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -193,7 +193,7 @@ class TestConv2DoubleGradCheck_PaddingSAME(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d( input=x, @@ -205,7 +205,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -214,9 +214,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -226,7 +226,7 @@ class TestConv2DoubleGradCheck_PaddingVALID(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d( input=x, @@ -238,7 +238,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -247,9 +247,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -259,7 +259,7 @@ class TestConv2DoubleGradCheck_ChannelLast(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d( input=x, @@ -273,7 +273,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -282,9 +282,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -294,7 +294,7 @@ class TestConv2DoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv2d( input=x, @@ -308,7 +308,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -317,9 +317,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -329,7 +329,7 @@ class TestConv3DDoubleGradCheck_AsyPadding(unittest.TestCase): def func(self, place): shape = [2, 2, 2, 2, 2] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d( input=x, @@ -341,7 +341,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -350,9 +350,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -362,7 +362,7 @@ class TestConv3DoubleGradCheck_PaddingSAME(unittest.TestCase): def func(self, place): shape = [2, 2, 2, 2, 2] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d( input=x, @@ -375,7 +375,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -384,9 +384,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -396,7 +396,7 @@ class TestConv3DoubleGradCheck_PaddingVALID(unittest.TestCase): def func(self, place): shape = [2, 2, 3, 3, 2] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d( input=x, @@ -408,7 +408,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -417,9 +417,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -429,7 +429,7 @@ class TestConv3DDoubleGradCheck_ChannelLast(unittest.TestCase): def func(self, place): shape = [2, 2, 2, 2, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d( input=x, @@ -443,7 +443,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -452,9 +452,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -464,7 +464,7 @@ class TestConv3DDoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase): def func(self, place): shape = [2, 2, 2, 2, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) y = paddle.static.nn.conv3d( input=x, @@ -478,7 +478,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -487,9 +487,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -499,7 +499,7 @@ class TestDepthWiseConvDoubleGradCheck(unittest.TestCase): def func(self, place): shape = [2, 4, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', shape, dtype) # condition of depthwise conv: @@ -511,7 +511,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -522,7 +522,7 @@ def func(self, place): def test_grad(self): places = [] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -536,7 +536,7 @@ def func(self, place): x_shape = [2, 4, 3, 3] w_shape = [4, 1, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', x_shape, dtype) w = paddle.static.data('w', w_shape, dtype) @@ -563,7 +563,7 @@ def func(self, place): def test_grad(self): places = [] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -577,7 +577,7 @@ def func(self, place): x_shape = [2, 3, 8, 8, 8] w_shape = [6, 3, 3, 3, 3] eps = 0.005 - dtype = np.float32 if fluid.core.is_compiled_with_rocm() else np.float64 + dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 x = paddle.static.data('x', x_shape, dtype) w = paddle.static.data('w', w_shape, dtype) x.persistable = True @@ -596,7 +596,7 @@ def func(self, place): def test_grad(self): places = [] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_conv_transpose_nn_grad.py b/test/legacy_test/test_conv_transpose_nn_grad.py index 6ea52dcffef26..eacea0637c69b 100644 --- a/test/legacy_test/test_conv_transpose_nn_grad.py +++ b/test/legacy_test/test_conv_transpose_nn_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestConvTransposeDoubleGradCheck(unittest.TestCase): @@ -40,7 +40,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -70,7 +70,7 @@ def test_grad(self): places = [] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -101,7 +101,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -154,7 +154,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -207,7 +207,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) @@ -262,7 +262,7 @@ def func(self, place): ) x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - w = fluid.default_main_program().global_block().all_parameters() + w = base.default_main_program().global_block().all_parameters() w_arr = [] for p in w: w_arr.append(np.random.uniform(-1, 1, p.shape).astype(dtype)) diff --git a/test/legacy_test/test_corr.py b/test/legacy_test/test_corr.py index cc7f7c12abdd5..128a8cf6a1ad3 100644 --- a/test/legacy_test/test_corr.py +++ b/test/legacy_test/test_corr.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base np_minor_version = int((np.__version__).split('.')[1]) @@ -35,9 +35,9 @@ def setUp(self): def test_tensor_corr_default(self): typelist = ['float64', 'float32'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: paddle.set_device('cpu') @@ -60,9 +60,9 @@ def test_tensor_corr_default(self): def test_tensor_corr_rowvar(self): typelist = ['float64', 'float32'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: diff --git a/test/legacy_test/test_cosine_similarity_api.py b/test/legacy_test/test_cosine_similarity_api.py index c4c9e7ec14a8f..7fe78e42c7ab1 100644 --- a/test/legacy_test/test_cosine_similarity_api.py +++ b/test/legacy_test/test_cosine_similarity_api.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import ( +from paddle.base import ( Executor, Program, core, diff --git a/test/legacy_test/test_cost_model.py b/test/legacy_test/test_cost_model.py index c7594f27e5d5c..997a5c0c6c47b 100644 --- a/test/legacy_test/test_cost_model.py +++ b/test/legacy_test/test_cost_model.py @@ -15,8 +15,8 @@ import unittest import paddle +from paddle.base import core from paddle.cost_model import CostModel -from paddle.fluid import core paddle.enable_static() diff --git a/test/legacy_test/test_count_nonzero_api.py b/test/legacy_test/test_count_nonzero_api.py index 741ed0aa0b87d..1b3c78dca3687 100644 --- a/test/legacy_test/test_count_nonzero_api.py +++ b/test/legacy_test/test_count_nonzero_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_cov.py b/test/legacy_test/test_cov.py index f464e3f1cb63f..43beb24cb6d1a 100644 --- a/test/legacy_test/test_cov.py +++ b/test/legacy_test/test_cov.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def numpy_cov(np_arr, rowvar=True, ddof=1, fweights=None, aweights=None): @@ -37,9 +37,9 @@ def setUp(self): def test_tensor_cov_default(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -60,9 +60,9 @@ def test_tensor_cov_default(self): def test_tensor_cov_rowvar(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -87,9 +87,9 @@ def test_tensor_cov_rowvar(self): def test_tensor_cov_ddof(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -114,9 +114,9 @@ def test_tensor_cov_ddof(self): def test_tensor_cov_fweights(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -145,9 +145,9 @@ def test_tensor_cov_fweights(self): def test_tensor_cov_aweights(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -176,9 +176,9 @@ def test_tensor_cov_aweights(self): def test_tensor_cov_weights(self): typelist = ['float64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: diff --git a/test/legacy_test/test_create_global_var.py b/test/legacy_test/test_create_global_var.py index 413666a5c8e30..d051929e84536 100644 --- a/test/legacy_test/test_create_global_var.py +++ b/test/legacy_test/test_create_global_var.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard class TestCreateGlobalVarError(unittest.TestCase): diff --git a/test/legacy_test/test_create_parameter.py b/test/legacy_test/test_create_parameter.py index 0993160ea6a79..895247931ba39 100644 --- a/test/legacy_test/test_create_parameter.py +++ b/test/legacy_test/test_create_parameter.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard class TestCreateParameterError(unittest.TestCase): diff --git a/test/legacy_test/test_cross_entropy_loss.py b/test/legacy_test/test_cross_entropy_loss.py index 6070f5622af51..f0cbc7cae9684 100644 --- a/test/legacy_test/test_cross_entropy_loss.py +++ b/test/legacy_test/test_cross_entropy_loss.py @@ -19,8 +19,8 @@ from test_softmax_with_cross_entropy_op import cross_entropy import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard def log_softmax(x, axis=-1): @@ -177,7 +177,7 @@ def cross_entropy_soft_2d( class CrossEntropyLoss(unittest.TestCase): def setUp(self): self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) # test for deprecated softmax_with_cross_entropy @@ -185,7 +185,7 @@ def test_softmax_with_cross_entropy(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -219,18 +219,18 @@ def test_softmax_with_cross_entropy(self): paddle.disable_static() paddle_loss_swce = paddle.nn.functional.softmax_with_cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, ) paddle_loss_ce = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight) + weight=base.dygraph.to_variable(self.weight) if self.weight is not None else None, reduction=self.reduction, @@ -247,7 +247,7 @@ def test_cross_entropy_loss_soft_1d(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -282,11 +282,11 @@ def test_cross_entropy_loss_soft_1d(self): # 2. dygraph paddle.disable_static() paddle_loss_none_weight = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight) + weight=base.dygraph.to_variable(self.weight) if self.weight is not None else None, reduction=self.reduction, @@ -295,14 +295,14 @@ def test_cross_entropy_loss_soft_1d(self): # 3. static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.C], dtype=self.dtype ) @@ -315,7 +315,7 @@ def test_cross_entropy_loss_soft_1d(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -335,7 +335,7 @@ def test_cross_entropy_loss_soft_1d_weight(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -380,25 +380,25 @@ def test_cross_entropy_loss_soft_1d_weight(self): # 2. dygraph paddle.disable_static() paddle_loss_none_weight = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight), + weight=base.dygraph.to_variable(self.weight), reduction=self.reduction, ) dy_ret_value = paddle_loss_none_weight.numpy() # 3.static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.C], dtype=self.dtype ) @@ -414,7 +414,7 @@ def test_cross_entropy_loss_soft_1d_weight(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -435,7 +435,7 @@ def test_cross_entropy_loss_soft_1d_mean(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -471,8 +471,8 @@ def test_cross_entropy_loss_soft_1d_mean(self): # 2 dygraph paddle.disable_static() paddle_loss_mean = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, weight=self.weight, @@ -482,14 +482,14 @@ def test_cross_entropy_loss_soft_1d_mean(self): # 3. static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.C], dtype=self.dtype ) @@ -502,7 +502,7 @@ def test_cross_entropy_loss_soft_1d_mean(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={'input': self.logits, 'label': self.labels}, @@ -519,7 +519,7 @@ def test_cross_entropy_loss_soft_1d_weight_mean(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -555,25 +555,25 @@ def test_cross_entropy_loss_soft_1d_weight_mean(self): # 2. dygraph paddle_loss_none_weight = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight), + weight=base.dygraph.to_variable(self.weight), reduction=self.reduction, ) dy_ret_value = paddle_loss_none_weight.numpy() # 3. static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.C], dtype=self.dtype ) @@ -588,7 +588,7 @@ def test_cross_entropy_loss_soft_1d_weight_mean(self): weight=weight, reduction=self.reduction, soft_label=True ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -610,7 +610,7 @@ def inner_cross_entropy_loss_soft_2d(soft_label): self.numeric_stable_mode = False self.soft_label = soft_label self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -654,11 +654,11 @@ def inner_cross_entropy_loss_soft_2d(soft_label): # 2. dygraph paddle_loss_none_weight = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight) + weight=base.dygraph.to_variable(self.weight) if self.weight is not None else None, reduction=self.reduction, @@ -667,14 +667,14 @@ def inner_cross_entropy_loss_soft_2d(soft_label): # 3. static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.H, self.W, self.C], @@ -690,7 +690,7 @@ def inner_cross_entropy_loss_soft_2d(soft_label): reduction=self.reduction, soft_label=True ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -714,7 +714,7 @@ def test_cross_entropy_loss_soft_2d_weight_mean(self): self.numeric_stable_mode = False self.soft_label = True self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.axis = -1 self.ignore_index = -100 # should not be changed @@ -754,25 +754,25 @@ def test_cross_entropy_loss_soft_2d_weight_mean(self): # 2. dygraph paddle_loss_none_weight = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(self.logits), - fluid.dygraph.to_variable(self.labels), + base.dygraph.to_variable(self.logits), + base.dygraph.to_variable(self.labels), soft_label=True, axis=self.axis, - weight=fluid.dygraph.to_variable(self.weight), + weight=base.dygraph.to_variable(self.weight), reduction=self.reduction, ) dy_ret_value = paddle_loss_none_weight.numpy() # 3. static paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[self.N, self.H, self.W, self.C], @@ -791,7 +791,7 @@ def test_cross_entropy_loss_soft_2d_weight_mean(self): weight=weight, reduction=self.reduction, soft_label=True ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -814,14 +814,14 @@ def test_cross_entropy_loss_1d_with_mean_ignore(self): input_np = np.random.random([2, 4]).astype(self.dtype) label_np = np.random.randint(0, 4, size=(2)).astype(np.int64) paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 4], dtype=self.dtype ) @@ -829,7 +829,7 @@ def test_cross_entropy_loss_1d_with_mean_ignore(self): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(ignore_index=0) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -841,13 +841,13 @@ def test_cross_entropy_loss_1d_with_mean_ignore(self): self.assertIsNotNone(static_ret) expected = cross_entropy_loss_1d(input_np, label_np)[0] - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( axis=1, ignore_index=0 ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -862,14 +862,14 @@ def test_cross_entropy_loss_1d_with_mean_ignore_negative(self): input_np = np.random.random([N, C]).astype(self.dtype) label_np = -np.ones(N).astype(np.int64) paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[N, C], dtype=self.dtype ) @@ -878,7 +878,7 @@ def test_cross_entropy_loss_1d_with_mean_ignore_negative(self): ignore_index=-1 ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -889,13 +889,13 @@ def test_cross_entropy_loss_1d_with_mean_ignore_negative(self): ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( axis=1, ignore_index=-1 ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -912,14 +912,14 @@ def test_cross_entropy_loss_1d_with_weight_mean_ignore(self): label_np = np.random.randint(0, C, size=(N)).astype(np.int64) weight_np = np.random.random([C]).astype(self.dtype) paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[N, C], dtype=self.dtype ) @@ -932,7 +932,7 @@ def test_cross_entropy_loss_1d_with_weight_mean_ignore(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -944,15 +944,15 @@ def test_cross_entropy_loss_1d_with_weight_mean_ignore(self): ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), + weight=base.dygraph.to_variable(weight_np), axis=1, ignore_index=0, ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -972,13 +972,13 @@ def test_cross_entropy_loss_1d_with_weight_mean_ignore_exceedlabel(self): label_np[0] = 255 weight_np = np.random.random([C]).astype(self.dtype) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), ignore_index=255 + weight=base.dygraph.to_variable(weight_np), ignore_index=255 ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -993,14 +993,14 @@ def test_cross_entropy_loss_1d_with_weight_mean(self): label_np = np.random.randint(0, 4, size=(2)).astype(np.int64) weight_np = np.random.random([4]).astype(self.dtype) # shape:C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 4], dtype=self.dtype ) @@ -1011,7 +1011,7 @@ def test_cross_entropy_loss_1d_with_weight_mean(self): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss(weight=weight) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1026,13 +1026,13 @@ def test_cross_entropy_loss_1d_with_weight_mean(self): 0 ] - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), axis=1 + weight=base.dygraph.to_variable(weight_np), axis=1 ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1048,14 +1048,14 @@ def test_cross_entropy_loss_1d_with_weight_sum(self): label_np = np.random.randint(0, 100, size=(100)).astype(np.int64) # N,1 weight_np = np.random.random([200]).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) @@ -1068,7 +1068,7 @@ def test_cross_entropy_loss_1d_with_weight_sum(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1079,13 +1079,13 @@ def test_cross_entropy_loss_1d_with_weight_sum(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), reduction='sum' + weight=base.dygraph.to_variable(weight_np), reduction='sum' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1102,14 +1102,14 @@ def test_cross_entropy_loss_1d_with_weight_none(self): weight_np = np.random.random([200]).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) @@ -1123,7 +1123,7 @@ def test_cross_entropy_loss_1d_with_weight_none(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1135,13 +1135,13 @@ def test_cross_entropy_loss_1d_with_weight_none(self): ) static_ret = np.squeeze(static_ret) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), reduction='none' + weight=base.dygraph.to_variable(weight_np), reduction='none' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() dy_ret_value = np.squeeze(dy_ret_value) @@ -1158,14 +1158,14 @@ def test_cross_entropy_loss_1d_with_weight_none_func(self): label_np = np.random.randint(0, 100, size=(100)).astype(np.int64) # N weight_np = np.random.random([200]).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) @@ -1177,7 +1177,7 @@ def test_cross_entropy_loss_1d_with_weight_none_func(self): input, label, weight=weight, reduction='none' ) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1189,11 +1189,11 @@ def test_cross_entropy_loss_1d_with_weight_none_func(self): ) static_ret = np.squeeze(static_ret) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): dy_ret = paddle.nn.functional.cross_entropy( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), - weight=fluid.dygraph.to_variable(weight_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), + weight=base.dygraph.to_variable(weight_np), reduction='none', ) dy_ret_value = dy_ret.numpy() @@ -1210,32 +1210,32 @@ def test_cross_entropy_loss_1d_mean(self): input_np = np.random.random([100, 200]).astype(self.dtype) # N,C label_np = np.random.randint(0, 100, size=(100)).astype(np.int64) # N,1 paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) label = paddle.static.data(name='label', shape=[100], dtype='int64') cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss() ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={'input': input_np, 'label': label_np}, fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss() dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1248,14 +1248,14 @@ def test_cross_entropy_loss_1d_sum(self): input_np = np.random.random([100, 200]).astype(self.dtype) # N,C label_np = np.random.randint(0, 100, size=(100)).astype(np.int64) # N,1 paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) @@ -1264,20 +1264,20 @@ def test_cross_entropy_loss_1d_sum(self): reduction='sum' ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={'input': input_np, 'label': label_np}, fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( reduction='sum' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1290,14 +1290,14 @@ def test_cross_entropy_loss_1d_none(self): input_np = np.random.random([100, 200]).astype(self.dtype) # N,C label_np = np.random.randint(0, 100, size=(100)).astype(np.int64) # N,1 paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype=self.dtype ) @@ -1306,7 +1306,7 @@ def test_cross_entropy_loss_1d_none(self): reduction='none' ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={'input': input_np, 'label': label_np}, @@ -1314,13 +1314,13 @@ def test_cross_entropy_loss_1d_none(self): ) static_ret = np.squeeze(static_ret) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( reduction='none' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() dy_ret_value = np.squeeze(dy_ret_value) @@ -1340,14 +1340,14 @@ def test_cross_entropy_loss_2d_with_weight_none(self): weight_np = np.random.random(size=(3,)).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1362,7 +1362,7 @@ def test_cross_entropy_loss_2d_with_weight_none(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1374,13 +1374,13 @@ def test_cross_entropy_loss_2d_with_weight_none(self): ) static_ret = np.squeeze(static_ret) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), reduction='none' + weight=base.dygraph.to_variable(weight_np), reduction='none' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() dy_ret_value = np.squeeze(dy_ret_value) @@ -1402,14 +1402,14 @@ def test_cross_entropy_loss_2d_with_weight_axis_change_mean(self): weight_np = np.random.random(size=(3,)).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 3, 2, 2], dtype=self.dtype ) @@ -1425,7 +1425,7 @@ def test_cross_entropy_loss_2d_with_weight_axis_change_mean(self): # specify the class channels to axis 1 ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1437,15 +1437,15 @@ def test_cross_entropy_loss_2d_with_weight_axis_change_mean(self): ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), + weight=base.dygraph.to_variable(weight_np), reduction='mean', axis=1, ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1468,13 +1468,13 @@ def test_cross_entropy_loss_2d_with_weight_mean_ignore_exceedlabel(self): label_np = np.random.randint(0, C, size=(N, H, W)).astype(np.int64) label_np[0, 0, 0] = 255 weight_np = np.random.random([C]).astype(self.dtype) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), ignore_index=255 + weight=base.dygraph.to_variable(weight_np), ignore_index=255 ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1492,14 +1492,14 @@ def test_cross_entropy_loss_2d_with_weight_mean(self): ) # NHW weight_np = np.random.random(size=(3,)).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1514,7 +1514,7 @@ def test_cross_entropy_loss_2d_with_weight_mean(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1525,13 +1525,13 @@ def test_cross_entropy_loss_2d_with_weight_mean(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), reduction='mean' + weight=base.dygraph.to_variable(weight_np), reduction='mean' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1552,14 +1552,14 @@ def test_cross_entropy_loss_2d_with_weight_sum(self): weight_np = np.random.random(size=(3,)).astype(self.dtype) # C paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1574,7 +1574,7 @@ def test_cross_entropy_loss_2d_with_weight_sum(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1585,13 +1585,13 @@ def test_cross_entropy_loss_2d_with_weight_sum(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( - weight=fluid.dygraph.to_variable(weight_np), reduction='sum' + weight=base.dygraph.to_variable(weight_np), reduction='sum' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1610,14 +1610,14 @@ def test_cross_entropy_loss_2d_none(self): np.int64 ) # NHW paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1628,7 +1628,7 @@ def test_cross_entropy_loss_2d_none(self): reduction='none' ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1639,13 +1639,13 @@ def test_cross_entropy_loss_2d_none(self): ) static_ret = np.squeeze(static_ret) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( reduction='none' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() dy_ret_value = np.squeeze(dy_ret_value) @@ -1663,14 +1663,14 @@ def test_cross_entropy_loss_2d_mean(self): np.int64 ) # NHW paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1682,7 +1682,7 @@ def test_cross_entropy_loss_2d_mean(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1692,13 +1692,13 @@ def test_cross_entropy_loss_2d_mean(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( reduction='mean' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1717,14 +1717,14 @@ def test_cross_entropy_loss_2d_sum(self): np.int64 ) # NHW paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 2, 2, 3], dtype=self.dtype ) @@ -1736,7 +1736,7 @@ def test_cross_entropy_loss_2d_sum(self): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ @@ -1746,13 +1746,13 @@ def test_cross_entropy_loss_2d_sum(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): cross_entropy_loss = paddle.nn.loss.CrossEntropyLoss( reduction='sum' ) dy_ret = cross_entropy_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -1818,14 +1818,14 @@ def static_test_WeightLength_NotEqual(): label_np = np.random.randint(0, 4, size=(2)).astype(np.int64) weight_np = np.random.random([3]).astype('float32') paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[2, 4], dtype='float32' ) @@ -1840,7 +1840,7 @@ def static_test_WeightLength_NotEqual(): ) ret = cross_entropy_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) static_ret = exe.run( prog, feed={ diff --git a/test/legacy_test/test_cross_entropy_op.py b/test/legacy_test/test_cross_entropy_op.py index 0cf4e0a6c2fc8..4ad24ba762b09 100644 --- a/test/legacy_test/test_cross_entropy_op.py +++ b/test/legacy_test/test_cross_entropy_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, paddle_static_guard, randomize_probability import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestCrossEntropyOp(OpTest): @@ -413,11 +413,11 @@ def test_errors(self): def test_Variable(): # the input of cross_entropy must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) - lab1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + lab1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.nn.functional.cross_entropy( x1, lab1, reduction='none', use_softmax=False diff --git a/test/legacy_test/test_cross_op.py b/test/legacy_test/test_cross_op.py index 1114bb0b69ffb..72f77a0adaf3b 100644 --- a/test/legacy_test/test_cross_op.py +++ b/test/legacy_test/test_cross_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestCrossOp(OpTest): @@ -142,7 +142,7 @@ def test_cross_api(self): x = paddle.static.data(name='x', shape=[-1, 3], dtype="float32") y = paddle.static.data(name='y', shape=[-1, 3], dtype="float32") z = paddle.cross(x, y, axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'y': self.data_y}, fetch_list=[z.name], @@ -158,7 +158,7 @@ def test_cross_api(self): x = paddle.static.data(name='x', shape=[-1, 3], dtype="float32") y = paddle.static.data(name='y', shape=[-1, 3], dtype="float32") z = paddle.cross(x, y) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'y': self.data_y}, fetch_list=[z.name], @@ -180,9 +180,9 @@ def test_cross_api(self): def test_dygraph_api(self): self.input_data() # case 1: - # with fluid.dygraph.guard(): - # x = fluid.dygraph.to_variable(self.data_x) - # y = fluid.dygraph.to_variable(self.data_y) + # with base.dygraph.guard(): + # x = base.dygraph.to_variable(self.data_x) + # y = base.dygraph.to_variable(self.data_y) # z = paddle.cross(x, y) # np_z = z.numpy() # expect_out = np.array([[-1.0, -1.0, -1.0], [2.0, 2.0, 2.0], @@ -190,9 +190,9 @@ def test_dygraph_api(self): # np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 2: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - y = fluid.dygraph.to_variable(self.data_y) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + y = base.dygraph.to_variable(self.data_y) z = paddle.cross(x, y, axis=1) np_z = z.numpy() expect_out = np.array( diff --git a/test/legacy_test/test_crypto.py b/test/legacy_test/test_crypto.py index 79777fa53373e..c0c4490e84c4f 100644 --- a/test/legacy_test/test_crypto.py +++ b/test/legacy_test/test_crypto.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.core import CipherFactory, CipherUtils +from paddle.base.core import CipherFactory, CipherUtils class CipherUtilsTestCase(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_graph_static_mode.py b/test/legacy_test/test_cuda_graph_static_mode.py index 3f433e469110a..746a3db02c222 100644 --- a/test/legacy_test/test_cuda_graph_static_mode.py +++ b/test/legacy_test/test_cuda_graph_static_mode.py @@ -18,8 +18,8 @@ from simple_nets import simple_fc_net_with_inputs import paddle +from paddle.base.dygraph.base import switch_to_static_graph from paddle.device.cuda.graphs import CUDAGraph -from paddle.fluid.dygraph.base import switch_to_static_graph def can_use_cuda_graph(): diff --git a/test/legacy_test/test_cuda_graph_static_mode_error.py b/test/legacy_test/test_cuda_graph_static_mode_error.py index 6ee8af1ba69a6..a718f1b7009bd 100644 --- a/test/legacy_test/test_cuda_graph_static_mode_error.py +++ b/test/legacy_test/test_cuda_graph_static_mode_error.py @@ -18,8 +18,8 @@ from test_cuda_graph_static_mode import build_program, can_use_cuda_graph import paddle +from paddle.base.dygraph.base import switch_to_static_graph from paddle.device.cuda.graphs import CUDAGraph -from paddle.fluid.dygraph.base import switch_to_static_graph class TestCUDAGraphInFirstBatch(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_max_memory_allocated.py b/test/legacy_test/test_cuda_max_memory_allocated.py index c7b6828a60c1d..90e016921f8a2 100644 --- a/test/legacy_test/test_cuda_max_memory_allocated.py +++ b/test/legacy_test/test_cuda_max_memory_allocated.py @@ -15,12 +15,12 @@ import unittest import paddle +from paddle.base import core from paddle.device.cuda import ( device_count, max_memory_allocated, memory_allocated, ) -from paddle.fluid import core class TestMaxMemoryAllocated(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_max_memory_reserved.py b/test/legacy_test/test_cuda_max_memory_reserved.py index 07d2b5a9ded19..ac3b2b712e2ff 100644 --- a/test/legacy_test/test_cuda_max_memory_reserved.py +++ b/test/legacy_test/test_cuda_max_memory_reserved.py @@ -15,12 +15,12 @@ import unittest import paddle +from paddle.base import core from paddle.device.cuda import ( device_count, max_memory_reserved, memory_reserved, ) -from paddle.fluid import core class TestMaxMemoryreserved(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_memory_allocated.py b/test/legacy_test/test_cuda_memory_allocated.py index 252dd6f93117c..3e4c258940659 100644 --- a/test/legacy_test/test_cuda_memory_allocated.py +++ b/test/legacy_test/test_cuda_memory_allocated.py @@ -15,8 +15,8 @@ import unittest import paddle +from paddle.base import core from paddle.device.cuda import device_count, memory_allocated -from paddle.fluid import core class TestMemoryAllocated(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_memory_reserved.py b/test/legacy_test/test_cuda_memory_reserved.py index 941ab27ecc5ba..d639eab054ff5 100644 --- a/test/legacy_test/test_cuda_memory_reserved.py +++ b/test/legacy_test/test_cuda_memory_reserved.py @@ -15,8 +15,8 @@ import unittest import paddle +from paddle.base import core from paddle.device.cuda import device_count, memory_reserved -from paddle.fluid import core class TestMemoryreserved(unittest.TestCase): diff --git a/test/legacy_test/test_cuda_random_seed.py b/test/legacy_test/test_cuda_random_seed.py index e14f4c2fd1657..c517bd33b22dd 100644 --- a/test/legacy_test/test_cuda_random_seed.py +++ b/test/legacy_test/test_cuda_random_seed.py @@ -21,8 +21,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core @unittest.skipIf( @@ -36,7 +36,7 @@ class TestGeneratorSeed(unittest.TestCase): def test_gen_dropout_dygraph(self): gen = paddle.seed(12343) - fluid.enable_dygraph() + base.enable_dygraph() gen.manual_seed(111111111) st = paddle.get_cuda_rng_state() @@ -65,7 +65,7 @@ def test_gen_dropout_dygraph(self): def test_generator_gaussian_random_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() st = paddle.get_cuda_rng_state() x1 = paddle.randn([120], dtype="float32") @@ -86,7 +86,7 @@ def test_generator_gaussian_random_dygraph(self): def test_generator_randint_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() paddle.seed(12312321111) x = paddle.randint(low=10, shape=[10], dtype="int32") @@ -106,14 +106,14 @@ def test_generator_randint_dygraph(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_gen_TruncatedNormal_initializer(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) cur_state = paddle.get_cuda_rng_state() - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. x = paddle.uniform(shape=[2, 10]) @@ -132,14 +132,14 @@ def test_gen_TruncatedNormal_initializer(self): ), ) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] ) paddle.seed(123123143) - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): exe.run(startup_program) out2 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -160,7 +160,7 @@ def test_generator_pickle(self): output_dir = tempfile.mkdtemp() random_file = os.path.join(output_dir, "random.pdmodel") - fluid.enable_dygraph() + base.enable_dygraph() x0 = paddle.randn([120], dtype="float32") st = paddle.get_cuda_rng_state() diff --git a/test/legacy_test/test_cummax_op.py b/test/legacy_test/test_cummax_op.py index 1ff5cb2442a63..53e1da6da45f8 100644 --- a/test/legacy_test/test_cummax_op.py +++ b/test/legacy_test/test_cummax_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def cummax_dim2(arr, axis=None): @@ -152,7 +152,7 @@ def run_cases(self): np.testing.assert_array_equal(ind, indices.numpy()) def run_static(self, use_gpu=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data_np = np.random.random((100, 100)).astype(np.float32) x = paddle.static.data('x', [100, 100]) y1, indices1 = paddle.cummax(x) @@ -161,9 +161,9 @@ def run_static(self, use_gpu=False): y4, indices4 = paddle.cummax(x, axis=-2) y5, indices5 = paddle.cummax(x, axis=-2, dtype=np.int32) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) out = exe.run( feed={'x': data_np}, fetch_list=[ @@ -201,22 +201,22 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(ind, out[9], rtol=1e-05) def test_cpu(self): - paddle.disable_static(paddle.fluid.CPUPlace()) + paddle.disable_static(paddle.base.CPUPlace()) self.run_cases() paddle.enable_static() self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(paddle.fluid.CUDAPlace(0)) + paddle.disable_static(paddle.base.CUDAPlace(0)) self.run_cases() paddle.enable_static() self.run_static(use_gpu=True) def test_errors(self): paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_x_type(): data = [1, 2, 3] diff --git a/test/legacy_test/test_cummin_op.py b/test/legacy_test/test_cummin_op.py index dc542ebe90077..95235dc33d442 100644 --- a/test/legacy_test/test_cummin_op.py +++ b/test/legacy_test/test_cummin_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def cummin_dim2(arr, axis=None): @@ -152,7 +152,7 @@ def run_cases(self): np.testing.assert_array_equal(ind, indices.numpy()) def run_static(self, use_gpu=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data_np = np.random.random((100, 100)).astype(np.float32) x = paddle.static.data('x', [100, 100]) y1, indices1 = paddle.cummin(x) @@ -161,9 +161,9 @@ def run_static(self, use_gpu=False): y4, indices4 = paddle.cummin(x, axis=-2) y5, indices5 = paddle.cummin(x, axis=-2, dtype=np.int32) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) out = exe.run( feed={'x': data_np}, fetch_list=[ @@ -201,22 +201,22 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(ind, out[9], rtol=1e-05) def test_cpu(self): - paddle.disable_static(paddle.fluid.CPUPlace()) + paddle.disable_static(paddle.base.CPUPlace()) self.run_cases() paddle.enable_static() self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(paddle.fluid.CUDAPlace(0)) + paddle.disable_static(paddle.base.CUDAPlace(0)) self.run_cases() paddle.enable_static() self.run_static(use_gpu=True) def test_errors(self): paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_x_type(): data = [1, 2, 3] diff --git a/test/legacy_test/test_cumprod_op.py b/test/legacy_test/test_cumprod_op.py index 65b3c8da65870..f0569a7697239 100644 --- a/test/legacy_test/test_cumprod_op.py +++ b/test/legacy_test/test_cumprod_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(0) diff --git a/test/legacy_test/test_cumsum_op.py b/test/legacy_test/test_cumsum_op.py index 4134d649044f4..6422852458138 100644 --- a/test/legacy_test/test_cumsum_op.py +++ b/test/legacy_test/test_cumsum_op.py @@ -21,8 +21,8 @@ import paddle import paddle.inference as paddle_infer -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestCumsumOp(unittest.TestCase): @@ -53,7 +53,7 @@ def run_cases(self): np.testing.assert_array_equal(z, y.numpy()) def run_static(self, use_gpu=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data_np = np.random.random((100, 100)).astype(np.float32) x = paddle.static.data('X', [100, 100]) y = paddle.cumsum(x) @@ -63,9 +63,9 @@ def run_static(self, use_gpu=False): y5 = paddle.cumsum(x, dtype=np.int32) y6 = paddle.cumsum(x, axis=-2) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) out = exe.run( feed={'X': data_np}, fetch_list=[ @@ -90,23 +90,23 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(z, out[5], rtol=1e-05) def test_cpu(self): - paddle.disable_static(paddle.fluid.CPUPlace()) + paddle.disable_static(paddle.base.CPUPlace()) self.run_cases() paddle.enable_static() self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(paddle.fluid.CUDAPlace(0)) + paddle.disable_static(paddle.base.CUDAPlace(0)) self.run_cases() paddle.enable_static() self.run_static(use_gpu=True) def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data('x', [3, 4]) y = paddle.cumsum(x, name='out') self.assertTrue('out' in y.name) @@ -475,7 +475,7 @@ def test_check_grad(self): class BadInputTest(unittest.TestCase): def test_error(self): paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_bad_x(): data = [1, 2, 4] diff --git a/test/legacy_test/test_custom_grad_input.py b/test/legacy_test/test_custom_grad_input.py index cb7b673029fa5..cd16a32bcc7c9 100644 --- a/test/legacy_test/test_custom_grad_input.py +++ b/test/legacy_test/test_custom_grad_input.py @@ -17,7 +17,7 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg class TestTensorBackward(unittest.TestCase): diff --git a/test/legacy_test/test_data.py b/test/legacy_test/test_data.py index 141d9729f61a9..f9dbab12354fd 100644 --- a/test/legacy_test/test_data.py +++ b/test/legacy_test/test_data.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard class TestApiStaticDataError(unittest.TestCase): diff --git a/test/legacy_test/test_data_feeder.py b/test/legacy_test/test_data_feeder.py index 72bacedf97a6b..e8cda8eb45d63 100644 --- a/test/legacy_test/test_data_feeder.py +++ b/test/legacy_test/test_data_feeder.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -24,7 +24,7 @@ class TestDataFeeder(unittest.TestCase): def test_lod_level_0_converter(self): img = paddle.static.data(name='image', shape=[-1, 1, 28, 28]) label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - feeder = fluid.DataFeeder([img, label], fluid.CPUPlace()) + feeder = base.DataFeeder([img, label], base.CPUPlace()) result = feeder.feed([([0] * 784, [9]), ([1] * 784, [1])]) self.assertEqual(result['image'].shape(), [2, 1, 28, 28]) @@ -45,7 +45,7 @@ def test_lod_level_1_converter(self): name='sentences', shape=[-1, 1], dtype='int64', lod_level=1 ) label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - feeder = fluid.DataFeeder([sentences, label], fluid.CPUPlace()) + feeder = base.DataFeeder([sentences, label], base.CPUPlace()) # lod = [[0, 3, 5, 9]] # data = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] @@ -68,7 +68,7 @@ def test_lod_level_2_converter(self): name='paragraphs', shape=[-1, 1], dtype='int64', lod_level=2 ) label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - feeder = fluid.DataFeeder([paragraphs, label], fluid.CPUPlace()) + feeder = base.DataFeeder([paragraphs, label], base.CPUPlace()) # lod = [[0, 2, 3], [0, 3, 5, 9]] # data = [[[1, 2, 3], [4, 5]], [[6, 7, 8, 9]]] diff --git a/test/legacy_test/test_data_norm_op.py b/test/legacy_test/test_data_norm_op.py index 0839a778d30c1..251123e9abdaa 100644 --- a/test/legacy_test/test_data_norm_op.py +++ b/test/legacy_test/test_data_norm_op.py @@ -20,8 +20,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def _reference_testing(x, batch_size, batch_sum, batch_square_sum, slot_dim=-1): @@ -120,21 +120,21 @@ def check_with_place( # create input x_tensor = create_or_get_tensor( - scope, "x_val", OpTest.np_dtype_to_fluid_dtype(x_val), place + scope, "x_val", OpTest.np_dtype_to_base_dtype(x_val), place ) batch_size_tensor = create_or_get_tensor( scope, "batch_size", - OpTest.np_dtype_to_fluid_dtype(batch_size), + OpTest.np_dtype_to_base_dtype(batch_size), place, ) batch_sum_tensor = create_or_get_tensor( - scope, "batch_sum", OpTest.np_dtype_to_fluid_dtype(batch_sum), place + scope, "batch_sum", OpTest.np_dtype_to_base_dtype(batch_sum), place ) batch_square_sum_tensor = create_or_get_tensor( scope, "batch_square_sum", - OpTest.np_dtype_to_fluid_dtype(batch_square_sum), + OpTest.np_dtype_to_base_dtype(batch_square_sum), place, ) @@ -165,10 +165,10 @@ def check_with_place( scale_w = np.ones(scale_shape).astype(np.float32) bias = np.zeros(scale_shape).astype(np.float32) scale_w_tensor = create_or_get_tensor( - scope, "scale_w", OpTest.np_dtype_to_fluid_dtype(scale_w), place + scope, "scale_w", OpTest.np_dtype_to_base_dtype(scale_w), place ) bias_tensor = create_or_get_tensor( - scope, "bias", OpTest.np_dtype_to_fluid_dtype(bias), place + scope, "bias", OpTest.np_dtype_to_base_dtype(bias), place ) data_norm_op = Operator( "data_norm", @@ -528,7 +528,7 @@ class TestDataNormOpErrorr(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): x2 = paddle.static.data(name='x2', shape=[-1, 3, 4], dtype="int32") - # self.assertRaises(TypeError, fluid.data_norm, x2) + # self.assertRaises(TypeError, base.data_norm, x2) paddle.static.nn.data_norm( input=x2, param_attr={}, enable_scale_and_shift=True ) @@ -543,10 +543,10 @@ def test_0_size(): paddle.enable_static() x = paddle.static.data(name='x', shape=[0, 3], dtype='float32') out = paddle.static.nn.data_norm(x, slot_dim=1) - cpu = fluid.core.CPUPlace() - exe = fluid.Executor(cpu) - exe.run(fluid.default_startup_program()) - test_program = fluid.default_main_program().clone(for_test=True) + cpu = base.core.CPUPlace() + exe = base.Executor(cpu) + exe.run(base.default_startup_program()) + test_program = base.default_main_program().clone(for_test=True) exe.run( test_program, fetch_list=out, diff --git a/test/legacy_test/test_dataloader_early_reset.py b/test/legacy_test/test_dataloader_early_reset.py index 1367faa4d94cf..1c826eb9cb89a 100644 --- a/test/legacy_test/test_dataloader_early_reset.py +++ b/test/legacy_test/test_dataloader_early_reset.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def infinite_reader(): @@ -40,30 +40,30 @@ def build_network(self): optimizer.minimize(loss) def get_place(self): - if fluid.is_compiled_with_cuda(): - return fluid.CUDAPlace(0) + if base.is_compiled_with_cuda(): + return base.CUDAPlace(0) else: - return fluid.CPUPlace() + return base.CPUPlace() def create_data_loader(self): self.x = paddle.static.data(name='x', shape=[None, 32], dtype='float32') - return fluid.io.DataLoader.from_generator( + return base.io.DataLoader.from_generator( feed_list=[self.x], capacity=10, iterable=self.iterable ) def test_main(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): self.run_network() def run_network(self): loader = self.create_data_loader() self.build_network() - exe = fluid.Executor(self.get_place()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(self.get_place()) + exe.run(base.default_startup_program()) - prog = fluid.default_main_program() + prog = base.default_main_program() loader.set_batch_generator(infinite_reader, places=self.get_place()) for epoch_id in range(10): diff --git a/test/legacy_test/test_dataloader_keep_order.py b/test/legacy_test/test_dataloader_keep_order.py index 1068093f9c4ae..a37e1b4939770 100644 --- a/test/legacy_test/test_dataloader_keep_order.py +++ b/test/legacy_test/test_dataloader_keep_order.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def create_reader(shape, batch_number): @@ -46,7 +46,7 @@ def build_network(self, places): input_data = paddle.static.data( shape=self.shape, dtype='float32', name="input" ) - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( capacity=16, feed_list=[input_data], iterable=self.iterable ) @@ -83,12 +83,12 @@ def assertInputData(self, batch_id, input_data, dev_cnt): start_val += 1 def get_places(self): - place_list = [fluid.cpu_places(1)] - if fluid.is_compiled_with_cuda(): + place_list = [base.cpu_places(1)] + if base.is_compiled_with_cuda(): if os.name == "nt": - place_list.extend([fluid.cuda_places(0)]) + place_list.extend([base.cuda_places(0)]) else: - place_list.extend([fluid.cuda_places(0)]) + place_list.extend([base.cuda_places(0)]) return place_list def test_main(self): @@ -96,18 +96,18 @@ def test_main(self): self.run_main_with_place(p) def run_main_with_place(self, places): - with fluid.scope_guard(fluid.Scope()): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.scope_guard(base.Scope()): + with base.program_guard(base.Program(), base.Program()): input_data, loss, loader = self.build_network(places) fetch_list = [input_data] - exe = fluid.Executor(places[0]) - exe.run(fluid.default_startup_program()) + exe = base.Executor(places[0]) + exe.run(base.default_startup_program()) dev_cnt = len(places) self.assertTrue(dev_cnt == 1) - main_program = fluid.default_main_program() + main_program = base.default_main_program() max_batch_num = min( self.break_num, int(self.batch_num / dev_cnt) @@ -151,7 +151,7 @@ def run_main_with_place(self, places): batch_id, fetch_val, dev_cnt ) batch_id += 1 - except fluid.core.EOFException: + except base.core.EOFException: loader.reset() self.assertEqual(batch_id, max_batch_num) diff --git a/test/legacy_test/test_dataloader_unkeep_order.py b/test/legacy_test/test_dataloader_unkeep_order.py index 12b5d4e0853bd..17e5257dffc01 100644 --- a/test/legacy_test/test_dataloader_unkeep_order.py +++ b/test/legacy_test/test_dataloader_unkeep_order.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.reader import keep_data_loader_order +from paddle import base +from paddle.base.reader import keep_data_loader_order keep_data_loader_order(False) @@ -52,7 +52,7 @@ def build_network(self, places): input_data = paddle.static.data( shape=self.shape, dtype='float32', name="input" ) - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( capacity=16, feed_list=[input_data], iterable=self.iterable ) @@ -103,12 +103,12 @@ def assertInputData( start_val += 1 def get_places(self): - place_list = [fluid.cpu_places(1)] - if fluid.is_compiled_with_cuda(): + place_list = [base.cpu_places(1)] + if base.is_compiled_with_cuda(): if os.name == "nt": - place_list.extend([fluid.cuda_places(0)]) + place_list.extend([base.cuda_places(0)]) else: - place_list.extend([fluid.cuda_places(0)]) + place_list.extend([base.cuda_places(0)]) return place_list def test_main(self): @@ -116,18 +116,18 @@ def test_main(self): self.run_main_with_place(p) def run_main_with_place(self, places): - with fluid.scope_guard(fluid.Scope()): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.scope_guard(base.Scope()): + with base.program_guard(base.Program(), base.Program()): input_data, loss, loader = self.build_network(places) fetch_list = [input_data] - exe = fluid.Executor(places[0]) - exe.run(fluid.default_startup_program()) + exe = base.Executor(places[0]) + exe.run(base.default_startup_program()) dev_cnt = len(places) self.assertTrue(dev_cnt == 1) - main_program = fluid.default_main_program() + main_program = base.default_main_program() max_batch_num = min( self.break_num, int(self.batch_num / dev_cnt) @@ -178,7 +178,7 @@ def run_main_with_place(self, places): batch_id, fetch_val, dev_cnt ) batch_id += 1 - except fluid.core.EOFException: + except base.core.EOFException: loader.reset() if dev_cnt == 1: diff --git a/test/legacy_test/test_dataset.py b/test/legacy_test/test_dataset.py index 04e38849da281..fd4141c1c8b64 100644 --- a/test/legacy_test/test_dataset.py +++ b/test/legacy_test/test_dataset.py @@ -21,8 +21,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDataset(unittest.TestCase): @@ -60,7 +60,7 @@ def test_config(self): """ Testcase for python config. """ - dataset = fluid.InMemoryDataset() + dataset = base.InMemoryDataset() dataset.set_parse_ins_id(True) dataset.set_parse_content(True) dataset._set_trainer_num(1) @@ -138,7 +138,7 @@ def test_run_with_dump(self): def test_dataset_config(self): """Testcase for dataset configuration.""" - dataset = fluid.core.Dataset("MultiSlotDataset") + dataset = base.core.Dataset("MultiSlotDataset") dataset.set_thread_num(12) dataset.set_filelist(["a.txt", "b.txt", "c.txt"]) dataset.set_trainer_num(4) @@ -212,11 +212,11 @@ def test_set_download_cmd(self): exe = paddle.static.Executor(paddle.CPUPlace()) startup_program = paddle.static.Program() main_program = paddle.static.Program() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): @@ -273,21 +273,19 @@ def test_in_memory_dataset_run(self): dataset.local_shuffle() dataset._set_generate_unique_feasigns(True, 15) dataset._generate_local_tables_unlock(0, 11, 1, 25, 15) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: - exe.train_from_dataset( - fluid.default_main_program(), dataset - ) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) @@ -297,7 +295,7 @@ def test_in_memory_dataset_gpugraph_mode(self): """ Testcase for InMemoryDataset in gpugraph mode. """ - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_feed_type("SlotRecordInMemoryDataFeed") graph_config = { "walk_len": 24, @@ -347,9 +345,9 @@ def test_in_memory_dataset_masterpatch(self): slots = ["slot1", "slot2", "slot3", "slot4"] slots_vars = [] - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): for slot in slots[:2]: var = paddle.static.data( name=slot, shape=[-1, 1], dtype="int64", lod_level=1 @@ -376,7 +374,7 @@ def test_in_memory_dataset_masterpatch(self): dataset.load_into_memory() dataset.local_shuffle() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) for i in range(2): @@ -423,9 +421,9 @@ def test_in_memory_dataset_masterpatch1(self): f.write(data) slots_vars = [] - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): var1 = paddle.static.data( name="slot1", shape=[-1, 1], dtype="int64", lod_level=0 ) @@ -454,7 +452,7 @@ def test_in_memory_dataset_masterpatch1(self): dataset.load_into_memory() dataset.local_shuffle() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) for i in range(2): @@ -512,30 +510,30 @@ def test_in_memory_dataset_run_2(self): dataset.load_into_memory() dataset.local_shuffle() - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) for i in range(2): try: - exe.train_from_dataset(fluid.default_main_program(), dataset) + exe.train_from_dataset(base.default_main_program(), dataset) # exe.train_from_dataset( - # fluid.default_main_program(), dataset, thread=1 + # base.default_main_program(), dataset, thread=1 # ) exe.train_from_dataset( - fluid.default_main_program(), dataset, thread=2 + base.default_main_program(), dataset, thread=2 ) # exe.train_from_dataset( - # fluid.default_main_program(), dataset, thread=2 + # base.default_main_program(), dataset, thread=2 # ) # exe.train_from_dataset( - # fluid.default_main_program(), dataset, thread=3 + # base.default_main_program(), dataset, thread=3 # ) # exe.train_from_dataset( - # fluid.default_main_program(), dataset, thread=4 + # base.default_main_program(), dataset, thread=4 # ) except ImportError as e: pass @@ -543,18 +541,16 @@ def test_in_memory_dataset_run_2(self): self.assertTrue(False) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: - exe.train_from_dataset( - fluid.default_main_program(), dataset - ) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) @@ -586,7 +582,7 @@ def test_in_memory_dataset_run_2(self): fleet_send_sleep_seconds=2, fea_eval=True, ) - fleet_ptr = fluid.core.Fleet() + fleet_ptr = base.core.Fleet() fleet_ptr.set_client2client_config(1, 1, 1) fleet_ptr.get_cache_threshold(0) @@ -626,21 +622,19 @@ def test_queue_dataset_run(self): ) dataset.set_filelist([filename1, filename2]) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: - exe.train_from_dataset( - fluid.default_main_program(), dataset - ) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) @@ -650,7 +644,7 @@ def test_queue_dataset_run(self): ) dataset.set_filelist([]) # try: - # exe.train_from_dataset(fluid.default_main_program(), dataset2) + # exe.train_from_dataset(base.default_main_program(), dataset2) # except ImportError as e: # print("warning: we skip trainer_desc_pb2 import problem in windows") # except Exception as e: @@ -694,25 +688,23 @@ def test_queue_dataset_run_2(self): ) dataset.set_filelist([filename1, filename2]) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: - exe.train_from_dataset( - fluid.default_main_program(), dataset - ) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) @@ -760,25 +752,23 @@ def test_queue_dataset_run_3(self): dataset.set_filelist([filename1, filename2]) dataset.load_into_memory() - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: - exe.train_from_dataset( - fluid.default_main_program(), dataset - ) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) @@ -878,7 +868,7 @@ def test_cuda_in_memory_dataset_run(self): ) slots_vars.append(var) - dataset = fluid.DatasetFactory().create_dataset("InMemoryDataset") + dataset = base.DatasetFactory().create_dataset("InMemoryDataset") dataset.set_feed_type("SlotRecordInMemoryDataFeed") dataset.set_batch_size(1) dataset.set_pipe_command("cat") @@ -905,15 +895,15 @@ def test_cuda_in_memory_dataset_run(self): dataset.get_memory_data_size() - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) for i in range(self.epoch_num): try: - exe.train_from_dataset(fluid.default_main_program(), dataset) + exe.train_from_dataset(base.default_main_program(), dataset) except Exception as e: self.assertTrue(False) temp_dir.cleanup() @@ -1014,12 +1004,12 @@ def test_dataset_none(self): files = [self.filename1, self.filename2] dataset = self.get_dataset(slots_vars, files) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) # test dataset->None try: - exe.train_from_dataset(fluid.default_main_program(), None) + exe.train_from_dataset(base.default_main_program(), None) except ImportError as e: print("warning: we skip trainer_desc_pb2 import problem in windows") except RuntimeError as e: @@ -1036,11 +1026,11 @@ def test_infer_from_dataset(self): files = [self.filename1, self.filename2] dataset = self.get_dataset(slots_vars, files) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) try: - exe.infer_from_dataset(fluid.default_main_program(), dataset) + exe.infer_from_dataset(base.default_main_program(), dataset) except ImportError as e: print("warning: we skip trainer_desc_pb2 import problem in windows") except Exception as e: @@ -1054,15 +1044,15 @@ def test_fetch_handler(self): files = [self.filename1, self.filename2] dataset = self.get_dataset(slots_vars, files) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) - fh = fluid.executor.FetchHandler(out.name) + fh = base.executor.FetchHandler(out.name) fh.help() try: exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, fetch_handler=fh, ) @@ -1110,14 +1100,14 @@ def test_dataset_fleet(self): data += "1 7 2 3 6 4 8 8 8 8 1 7\n" f.write(data) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() from paddle.incubate.distributed.fleet.parameter_server.distribute_transpiler import ( fleet, ) - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"] slots_vars = [] for slot in slots: @@ -1127,9 +1117,9 @@ def test_dataset_fleet(self): slots_vars.append(var) fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) - with fluid.scope_guard(scope): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + with base.scope_guard(scope): + place = base.CPUPlace() + exe = base.Executor(place) try: fleet.init() except ImportError as e: @@ -1182,14 +1172,14 @@ def test_dataset_fleet2(self): data += "1 7 2 3 6 4 8 8 8 8 1 7\n" f.write(data) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"] slots_vars = [] for slot in slots: @@ -1199,9 +1189,9 @@ def test_dataset_fleet2(self): slots_vars.append(var) fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) - with fluid.scope_guard(scope): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + with base.scope_guard(scope): + place = base.CPUPlace() + exe = base.Executor(place) try: fleet.init() except ImportError as e: @@ -1315,14 +1305,14 @@ def test_bosps_dataset_fleet2(self): data += "1 7 2 3 6 4 8 8 8 8 1 7\n" f.write(data) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): slots = ["slot1_ff", "slot2_ff", "slot3_ff", "slot4_ff"] slots_vars = [] for slot in slots: @@ -1332,9 +1322,9 @@ def test_bosps_dataset_fleet2(self): slots_vars.append(var) fake_cost = paddle.subtract(slots_vars[0], slots_vars[-1]) fake_cost = paddle.mean(fake_cost) - with fluid.scope_guard(scope): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + with base.scope_guard(scope): + place = base.CPUPlace() + exe = base.Executor(place) try: fleet.init() except ImportError as e: diff --git a/test/legacy_test/test_dataset_consistency_inspection.py b/test/legacy_test/test_dataset_consistency_inspection.py index 431c354c4b6b4..de9beede4a87b 100644 --- a/test/legacy_test/test_dataset_consistency_inspection.py +++ b/test/legacy_test/test_dataset_consistency_inspection.py @@ -21,12 +21,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet # paddle.enable_static() -# fluid.disable_dygraph() -fluid.disable_dygraph() +# base.disable_dygraph() +base.disable_dygraph() url_schema_len = 5 query_schema = [ 'Q_query_basic', diff --git a/test/legacy_test/test_dataset_dataloader.py b/test/legacy_test/test_dataset_dataloader.py index b17d2089c6d2c..03d27fdced05e 100644 --- a/test/legacy_test/test_dataset_dataloader.py +++ b/test/legacy_test/test_dataset_dataloader.py @@ -20,7 +20,7 @@ from simple_nets import simple_fc_net_with_inputs import paddle -from paddle import fluid +from paddle import base BATCH_SIZE = 32 BATCH_NUM = 10 @@ -31,12 +31,12 @@ def get_place_string(p): - if isinstance(p, (fluid.CPUPlace or fluid.CUDAPlace)): - tmp = fluid.core.Place() + if isinstance(p, (base.CPUPlace or base.CUDAPlace)): + tmp = base.core.Place() tmp.set_place(p) p = tmp - if p._type() == fluid.CPUPlace()._type(): + if p._type() == base.CPUPlace()._type(): return 'CPUPlace()' else: return 'CUDAPlace()' @@ -83,9 +83,9 @@ def tearDown(self): self.temp_dir.cleanup() def build_network(self): - main_prog = fluid.Program() - startup_prog = fluid.Program() - with fluid.program_guard(main_prog, startup_prog): + main_prog = base.Program() + startup_prog = base.Program() + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[-1] + IMAGE_SHAPE, dtype='float32' ) @@ -105,14 +105,14 @@ def check_batch_number(self, place, randomize_batch_num=False): dataset = paddle.distributed.InMemoryDataset() dataset._set_batch_size(BATCH_SIZE) - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): file_num = 1 os.environ['CPU_NUM'] = str(file_num) - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] use_cuda = False else: file_num = 1 - places = [fluid.CUDAPlace(0)] + places = [base.CUDAPlace(0)] use_cuda = True filelist = [] @@ -140,11 +140,11 @@ def check_batch_number(self, place, randomize_batch_num=False): if self.dataset_name == 'InMemoryDataset': dataset.load_into_memory() - dataloader = fluid.io.DataLoader.from_dataset( + dataloader = base.io.DataLoader.from_dataset( dataset=dataset, places=places, drop_last=self.drop_last ) - prog = fluid.CompiledProgram(main_prog) - exe = fluid.Executor(place) + prog = base.CompiledProgram(main_prog) + exe = base.Executor(place) exe.run(startup_prog) @@ -199,19 +199,19 @@ def check_batch_number(self, place, randomize_batch_num=False): self.assertTrue(has_complete_batch) def get_all_places(self): - p = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - p.append(fluid.CUDAPlace(0)) + p = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + p.append(base.CUDAPlace(0)) return p def test_batch_number_with_same_length_files(self): for p in self.get_all_places(): - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): self.check_batch_number(place=p, randomize_batch_num=False) def test_batch_number_with_different_length_files(self): for p in self.get_all_places(): - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): self.check_batch_number(place=p, randomize_batch_num=True) diff --git a/test/legacy_test/test_debugger.py b/test/legacy_test/test_debugger.py index 098e24a78f6eb..2acda1972fcc6 100644 --- a/test/legacy_test/test_debugger.py +++ b/test/legacy_test/test_debugger.py @@ -14,9 +14,9 @@ import unittest +from paddle.base import core +from paddle.base.framework import Program from paddle.distributed.fleet.base.util_factory import draw_block_graphviz -from paddle.fluid import core -from paddle.fluid.framework import Program class TestDrawBlockGraphviz(unittest.TestCase): diff --git a/test/legacy_test/test_decoupled_py_reader.py b/test/legacy_test/test_decoupled_py_reader.py index 38ef00b1ddcf1..a28b2584a5ff6 100644 --- a/test/legacy_test/test_decoupled_py_reader.py +++ b/test/legacy_test/test_decoupled_py_reader.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base EPOCH_NUM = 5 BATCH_SIZE = 16 @@ -37,18 +37,18 @@ def random_reader(): def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - startup_prog = fluid.Program() - main_prog = fluid.Program() + startup_prog = base.Program() + main_prog = base.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[-1, 784], dtype='float32' ) label = paddle.static.data( name='label', shape=[-1, 1], dtype='int64' ) - py_reader = fluid.io.PyReader( + py_reader = base.io.PyReader( feed_list=[image, label], capacity=4, iterable=not use_legacy_py_reader, @@ -60,7 +60,7 @@ def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): hidden, size=hidden_size, activation='tanh', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -89,24 +89,24 @@ def run_main( places, use_double_buffer, ): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, py_reader, loss = simple_fc_net( places, use_legacy_py_reader, use_double_buffer ) reader = paddle.batch(random_reader, batch_size=BATCH_SIZE) - ps = places if use_double_buffer else fluid.cpu_places(len(places)) + ps = places if use_double_buffer else base.cpu_places(len(places)) py_reader.decorate_sample_list_generator( reader, places=ps if py_reader.iterable else None ) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) - prog = fluid.CompiledProgram(main_prog) + prog = base.CompiledProgram(main_prog) step = 0 step_list = [] @@ -125,7 +125,7 @@ def run_main( ) loss_list.append(np.mean(L)) step += 1 - except fluid.core.EOFException: + except base.core.EOFException: py_reader.reset() break step_list.append(step) @@ -161,10 +161,10 @@ def run_main( def prepare_places(self, with_cpu=True, with_gpu=True): places = [] if with_cpu: - places.append([fluid.CPUPlace()]) + places.append([base.CPUPlace()]) - if with_gpu and fluid.core.is_compiled_with_cuda(): - tmp = fluid.cuda_places() + if with_gpu and base.core.is_compiled_with_cuda(): + tmp = base.cuda_places() assert len(tmp) > 0, "no gpu detected" places.append([tmp[0]]) return places diff --git a/test/legacy_test/test_decoupled_py_reader_data_check.py b/test/legacy_test/test_decoupled_py_reader_data_check.py index 1c8742cd895b2..ff5ff423493d3 100644 --- a/test/legacy_test/test_decoupled_py_reader_data_check.py +++ b/test/legacy_test/test_decoupled_py_reader_data_check.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestClass(unittest.TestCase): @@ -42,14 +42,14 @@ def fake_reader(): reader = paddle.reader.cache(fake_reader) batch_reader = paddle.batch(reader, batch_size=batch_size) - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for p in places: - main_prog = fluid.Program() - startup_prog = fluid.Program() - with fluid.program_guard(main_prog, startup_prog): + main_prog = base.Program() + startup_prog = base.Program() + with base.program_guard(main_prog, startup_prog): img = paddle.static.data( shape=[-1] + img_shape, dtype='float32', name='image' ) @@ -57,17 +57,17 @@ def fake_reader(): shape=[-1] + label_shape, dtype='int64', name='label' ) - feeder = fluid.DataFeeder(feed_list=[img, label], place=p) + feeder = base.DataFeeder(feed_list=[img, label], place=p) use_double_buffer = self.use_double_buffer if ( - p._type() != fluid.CPUPlace()._type() + p._type() != base.CPUPlace()._type() and not use_double_buffer ): use_double_buffer = True if self.use_py_reader: - py_reader = fluid.io.PyReader( + py_reader = base.io.PyReader( feed_list=[img, label], capacity=4, iterable=True, @@ -77,7 +77,7 @@ def fake_reader(): batch_reader, places=p ) else: - py_reader = fluid.io.DataLoader.from_generator( + py_reader = base.io.DataLoader.from_generator( feed_list=[img, label], capacity=4, iterable=True, diff --git a/test/legacy_test/test_default_scope_funcs.py b/test/legacy_test/test_default_scope_funcs.py index c6c0e73c5d2ee..779e20d2a7c64 100644 --- a/test/legacy_test/test_default_scope_funcs.py +++ b/test/legacy_test/test_default_scope_funcs.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.default_scope_funcs import ( +from paddle.base.default_scope_funcs import ( enter_local_scope, find_var, get_cur_scope, diff --git a/test/legacy_test/test_deformable_conv_op.py b/test/legacy_test/test_deformable_conv_op.py index 84d0a7d7907da..db01a7c68a48b 100644 --- a/test/legacy_test/test_deformable_conv_op.py +++ b/test/legacy_test/test_deformable_conv_op.py @@ -178,10 +178,10 @@ def setUp(self): output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Offset': OpTest.np_dtype_to_fluid_dtype(offset), - 'Mask': OpTest.np_dtype_to_fluid_dtype(mask), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Offset': OpTest.np_dtype_to_base_dtype(offset), + 'Mask': OpTest.np_dtype_to_base_dtype(mask), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/legacy_test/test_deformable_conv_v1_op.py b/test/legacy_test/test_deformable_conv_v1_op.py index 6a355811194f4..c4354ea9e8a88 100644 --- a/test/legacy_test/test_deformable_conv_v1_op.py +++ b/test/legacy_test/test_deformable_conv_v1_op.py @@ -173,9 +173,9 @@ def setUp(self): ) output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Offset': OpTest.np_dtype_to_fluid_dtype(offset), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Offset': OpTest.np_dtype_to_base_dtype(offset), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/legacy_test/test_deg2rad.py b/test/legacy_test/test_deg2rad.py index 0f038e86f2522..350471f896e69 100644 --- a/test/legacy_test/test_deg2rad.py +++ b/test/legacy_test/test_deg2rad.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -33,22 +33,22 @@ def setUp(self): self.out_np = np.deg2rad(self.x_np) def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(startup_program, train_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(startup_program, train_program): x = paddle.static.data( name='input', dtype=self.x_dtype, shape=self.x_shape ) out = paddle.deg2rad(x) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input': self.x_np}, fetch_list=[out], ) diff --git a/test/legacy_test/test_deprecated_decorator.py b/test/legacy_test/test_deprecated_decorator.py index 1e4bfe50515dd..81ae80a1f9bf7 100755 --- a/test/legacy_test/test_deprecated_decorator.py +++ b/test/legacy_test/test_deprecated_decorator.py @@ -90,7 +90,7 @@ def test_new_multiply(self): def test_ops_elementwise_mul(self): """ Test for new C++ elementwise_op, expected result should be True, - because not matter what fluid.layers.elementwise_mul is deprecated. + because not matter what base.layers.elementwise_mul is deprecated. """ a = np.random.uniform(0.1, 1, [51, 76]).astype(np.float32) @@ -118,7 +118,7 @@ def test_tensor_gradient(self): with warnings.catch_warnings(record=True) as w: grad = x.gradient() assert ( - 'API "paddle.fluid.dygraph.tensor_patch_methods.gradient" is ' + 'API "paddle.base.dygraph.tensor_patch_methods.gradient" is ' 'deprecated since 2.1.0' ) in str(w[-1].message) diff --git a/test/legacy_test/test_deprecated_memory_optimize_interfaces.py b/test/legacy_test/test_deprecated_memory_optimize_interfaces.py index bcb17baaee764..81fa915c9f1dd 100644 --- a/test/legacy_test/test_deprecated_memory_optimize_interfaces.py +++ b/test/legacy_test/test_deprecated_memory_optimize_interfaces.py @@ -17,7 +17,7 @@ from simple_nets import simple_fc_net import paddle -from paddle import fluid +from paddle import base from paddle.distributed import transpiler @@ -26,10 +26,10 @@ def setUp(self): self.method = transpiler.memory_optimize def build_network(self, call_interface): - startup_prog = fluid.Program() - main_prog = fluid.Program() - with fluid.program_guard(main_prog, startup_prog): - with fluid.unique_name.guard(): + startup_prog = base.Program() + main_prog = base.Program() + with base.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): loss = simple_fc_net() opt = paddle.optimizer.Adam(learning_rate=1e-3) opt.minimize(loss) diff --git a/test/legacy_test/test_desc_clone.py b/test/legacy_test/test_desc_clone.py index 10f0134757a79..a106233b896c4 100644 --- a/test/legacy_test/test_desc_clone.py +++ b/test/legacy_test/test_desc_clone.py @@ -19,8 +19,8 @@ import nets import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core SEED = 1 DTYPE = "float32" @@ -29,7 +29,7 @@ # random seed must set before configuring the network. -# fluid.default_startup_program().random_seed = SEED +# base.default_startup_program().random_seed = SEED def cnn_model(data): conv_pool_1 = nets.simple_img_conv_pool( input=data, @@ -60,7 +60,7 @@ def cnn_model(data): x=conv_pool_2, size=SIZE, activation="softmax", - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Normal(loc=0.0, scale=scale) ), ) @@ -87,7 +87,7 @@ def get_model(batch_size): input=predict, label=label, total=batch_size_tensor ) - inference_program = fluid.default_main_program().clone() + inference_program = base.default_main_program().clone() # Optimization opt = paddle.optimizer.Adam(learning_rate=0.001, beta1=0.9, beta2=0.999) @@ -114,7 +114,7 @@ def operator_equal(a, b): raise ValueError("In operator_equal not equal\n") for k, v in a.__dict__.items(): - if isinstance(v, (fluid.framework.Program, fluid.framework.Block)): + if isinstance(v, (base.framework.Program, base.framework.Block)): continue elif isinstance(v, core.OpDesc): @@ -136,7 +136,7 @@ def operator_equal(a, b): def block_equal(a, b): for k, v in a.__dict__.items(): if isinstance( - v, (core.ProgramDesc, fluid.framework.Program, core.BlockDesc) + v, (core.ProgramDesc, base.framework.Program, core.BlockDesc) ): continue elif k == "ops": @@ -177,9 +177,9 @@ def program_equal(a, b): class TestCloneWithStopGradient(unittest.TestCase): def test_clone_with_stop_gradient(self): - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): img = paddle.static.data(name='image', shape=[-1, 784]) hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') hidden1.stop_gradient = True @@ -207,9 +207,9 @@ def test_clone_with_stop_gradient(self): class TestCloneWithStopGradientInSubBlock(unittest.TestCase): def test_clone_with_stop_gradient(self): - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): img = paddle.static.data(name='image', shape=[-1, 784]) true = paddle.ones(shape=[1], dtype="float32") hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') @@ -254,9 +254,9 @@ def false_fn(): class TestCloneWithRaise(unittest.TestCase): def test_clone_with_stop_gradient(self): - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): img = paddle.static.data(name='image', shape=[-1, 784]) true = paddle.ones(shape=[1], dtype="float32") hidden1 = paddle.static.nn.fc(x=img, size=200, activation='relu') diff --git a/test/legacy_test/test_detach.py b/test/legacy_test/test_detach.py index 4b3d25a1cde73..5bb336866733a 100644 --- a/test/legacy_test/test_detach.py +++ b/test/legacy_test/test_detach.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear @@ -31,7 +31,7 @@ def generate_Data(self): def no_detach_multi(self): data = self.generate_Data() - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_w_param_attrs = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(5.0) ) @@ -79,7 +79,7 @@ def no_detach_multi(self): def no_detach_single(self): data = self.generate_Data() - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_w_param_attrs = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(5.0) ) @@ -115,11 +115,11 @@ def no_detach_single(self): def detach_multi(self): data = self.generate_Data() - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_w_param_attrs = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(5.0) ) - linear_b_param_attrs = fluid.ParamAttr( + linear_b_param_attrs = base.ParamAttr( initializer=paddle.nn.initializer.Constant(6.0) ) linear = Linear( @@ -131,7 +131,7 @@ def detach_multi(self): linear1_w_param_attrs = paddle.ParamAttr( initializer=paddle.nn.initializer.Constant(7.0) ) - linear1_b_param_attrs = fluid.ParamAttr( + linear1_b_param_attrs = base.ParamAttr( initializer=paddle.nn.initializer.Constant(8.0) ) linear1 = Linear( @@ -179,7 +179,7 @@ def test_NoDetachSingle_DetachMulti(self): class TestInplace(unittest.TestCase): def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(np.ones((4, 2, 3)).astype(np.float32)) self.assertEqual(var.inplace_version, 0) detach_var_1 = var.detach() @@ -198,7 +198,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones(shape=[4, 2, 3], dtype="float32") var_a.stop_gradient = False diff --git a/test/legacy_test/test_detection.py b/test/legacy_test/test_detection.py index e3aebb946692c..99e0836c4c1f5 100644 --- a/test/legacy_test/test_detection.py +++ b/test/legacy_test/test_detection.py @@ -18,22 +18,22 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph import base -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.dygraph import base as imperative_base +from paddle.base.framework import Program, program_guard paddle.enable_static() @contextlib.contextmanager def new_program_scope(main=None, startup=None, scope=None): - prog = main if main else fluid.Program() - startup_prog = startup if startup else fluid.Program() - scope = scope if scope else fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): - with fluid.unique_name.guard(): + prog = main if main else base.Program() + startup_prog = startup if startup else base.Program() + scope = scope if scope else base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): + with base.unique_name.guard(): yield @@ -58,17 +58,17 @@ def _get_place(self, force_to_use_cpu=False): @contextlib.contextmanager def static_graph(self): with new_program_scope(): - fluid.default_startup_program().random_seed = self.seed - fluid.default_main_program().random_seed = self.seed + base.default_startup_program().random_seed = self.seed + base.default_main_program().random_seed = self.seed yield def get_static_graph_result( self, feed, fetch_list, with_lod=False, force_to_use_cpu=False ): - exe = fluid.Executor(self._get_place(force_to_use_cpu)) - exe.run(fluid.default_startup_program()) + exe = base.Executor(self._get_place(force_to_use_cpu)) + exe.run(base.default_startup_program()) return exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=feed, fetch_list=fetch_list, return_numpy=(not with_lod), @@ -76,11 +76,11 @@ def get_static_graph_result( @contextlib.contextmanager def dynamic_graph(self, force_to_use_cpu=False): - with fluid.dygraph.guard( + with base.dygraph.guard( self._get_place(force_to_use_cpu=force_to_use_cpu) ): - fluid.default_startup_program().random_seed = self.seed - fluid.default_main_program().random_seed = self.seed + base.default_startup_program().random_seed = self.seed + base.default_main_program().random_seed = self.seed yield @@ -137,11 +137,11 @@ def test_generate_proposals(self): ) with self.dynamic_graph(): - scores_dy = base.to_variable(scores_np) - bbox_deltas_dy = base.to_variable(bbox_deltas_np) - im_info_dy = base.to_variable(im_info_np) - anchors_dy = base.to_variable(anchors_np) - variances_dy = base.to_variable(variances_np) + scores_dy = imperative_base.to_variable(scores_np) + bbox_deltas_dy = imperative_base.to_variable(bbox_deltas_np) + im_info_dy = imperative_base.to_variable(im_info_np) + anchors_dy = imperative_base.to_variable(anchors_np) + variances_dy = imperative_base.to_variable(variances_np) rois, roi_probs, rois_num = paddle.vision.ops.generate_proposals( scores_dy, bbox_deltas_dy, @@ -218,8 +218,8 @@ def test_distribute_fpn_proposals(self): output_stat_np.append(output_np) with self.dynamic_graph(): - rois_dy = base.to_variable(rois_np) - rois_num_dy = base.to_variable(rois_num_np) + rois_dy = imperative_base.to_variable(rois_np) + rois_num_dy = imperative_base.to_variable(rois_num_np) ( multi_rois_dy, restore_ind_dy, diff --git a/test/legacy_test/test_device.py b/test/legacy_test/test_device.py index 8396b2a39d699..d054b333cb84e 100644 --- a/test/legacy_test/test_device.py +++ b/test/legacy_test/test_device.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core, framework +from paddle import base +from paddle.base import core, framework class TestStaticDeviceManage(unittest.TestCase): @@ -28,7 +28,7 @@ def _test_device(self, device_name, device_class): out3 = paddle.concat(x=[out1, out2], axis=0) exe = paddle.static.Executor() - exe.run(paddle.fluid.default_startup_program()) + exe.run(paddle.base.default_startup_program()) res = exe.run(fetch_list=[out3]) device = paddle.get_device() @@ -49,7 +49,7 @@ def test_xpu_device(self): class TestImperativeDeviceManage(unittest.TestCase): def test_cpu(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.set_device('cpu') out1 = paddle.zeros(shape=[1, 3], dtype='float32') out2 = paddle.ones(shape=[1, 3], dtype='float32') @@ -63,7 +63,7 @@ def test_cpu(self): def test_gpu(self): if core.is_compiled_with_cuda(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.set_device('gpu:0') out1 = paddle.zeros(shape=[1, 3], dtype='float32') out2 = paddle.ones(shape=[1, 3], dtype='float32') @@ -79,7 +79,7 @@ def test_gpu(self): def test_xpu(self): if core.is_compiled_with_xpu(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out = paddle.to_tensor([1, 2]) device = paddle.get_device() self.assertEqual( diff --git a/test/legacy_test/test_device_guard.py b/test/legacy_test/test_device_guard.py index 76ef6b6799a6a..336437333e692 100644 --- a/test/legacy_test/test_device_guard.py +++ b/test/legacy_test/test_device_guard.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_diag.py b/test/legacy_test/test_diag.py index 429682b84e8a7..96d5f9e53db79 100644 --- a/test/legacy_test/test_diag.py +++ b/test/legacy_test/test_diag.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard class TestDiagOp(OpTest): diff --git a/test/legacy_test/test_diag_embed.py b/test/legacy_test/test_diag_embed.py index 98efcf4b5ca4c..4e9e26d1b4ad4 100644 --- a/test/legacy_test/test_diag_embed.py +++ b/test/legacy_test/test_diag_embed.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDiagEmbedOp(OpTest): @@ -61,9 +61,9 @@ def test_case1(self): out2 = F.diag_embed(data1, offset=1, dim1=-2, dim2=3) place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data1": diag_embed}, fetch_list=[out1, out2], return_numpy=True, diff --git a/test/legacy_test/test_diag_v2.py b/test/legacy_test/test_diag_v2.py index c98daafaa6ead..bbe146245bf14 100644 --- a/test/legacy_test/test_diag_v2.py +++ b/test/legacy_test/test_diag_v2.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestDiagV2Op(OpTest): @@ -226,9 +226,9 @@ def run_static(self, use_gpu=False): result12 = paddle.diag(x5, offset=-1) result13 = paddle.diag(x6, offset=-1) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) ( res0, res1, @@ -285,23 +285,23 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(res13, self.expected12, rtol=1e-05) def test_cpu(self): - paddle.disable_static(place=paddle.fluid.CPUPlace()) + paddle.disable_static(place=paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(place=paddle.fluid.CUDAPlace(0)) + paddle.disable_static(place=paddle.base.CUDAPlace(0)) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static(use_gpu=True) diff --git a/test/legacy_test/test_diagflat.py b/test/legacy_test/test_diagflat.py index db3137ae020e4..c391df1c8eb6c 100644 --- a/test/legacy_test/test_diagflat.py +++ b/test/legacy_test/test_diagflat.py @@ -105,7 +105,7 @@ def test_gpu(self): self.run_static(use_gpu=True) def test_fp16_with_gpu(self, use_gpu=False): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_diagonal_op.py b/test/legacy_test/test_diagonal_op.py index 3cce67889a87f..005a890b6ed07 100644 --- a/test/legacy_test/test_diagonal_op.py +++ b/test/legacy_test/test_diagonal_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_diff_op.py b/test/legacy_test/test_diff_op.py index 96101b58340d9..bf7313ae61237 100644 --- a/test/legacy_test/test_diff_op.py +++ b/test/legacy_test/test_diff_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDiffOp(unittest.TestCase): @@ -79,11 +79,11 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data( name="input", shape=self.input.shape, dtype=self.input.dtype ) @@ -105,12 +105,12 @@ def test_static(self): dtype=self.append.dtype, ) - exe = fluid.Executor(place) + exe = base.Executor(place) out = paddle.diff( x, n=self.n, axis=self.axis, prepend=prepend, append=append ) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "input": self.input, "prepend": self.prepend, @@ -240,7 +240,7 @@ def set_args(self): class TestDiffOpFp16(TestDiffOp): def test_fp16_with_gpu(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_digamma_op.py b/test/legacy_test/test_digamma_op.py index 60eec154620d8..04fbefc1dc8be 100644 --- a/test/legacy_test/test_digamma_op.py +++ b/test/legacy_test/test_digamma_op.py @@ -19,8 +19,8 @@ from scipy.special import psi import paddle -from paddle import fluid, static -from paddle.fluid import core +from paddle import base, static +from paddle.base import core class TestDigammaOp(OpTest): @@ -126,7 +126,7 @@ def test_in_dynamic_mode(self): sc_res = psi(input) for place in self.places: # it is more convenient to use `guard` than `enable/disable_**` here - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_t = paddle.to_tensor(input) res = paddle.digamma(input_t).numpy() np.testing.assert_allclose(res, sc_res, rtol=1e-05) @@ -146,7 +146,7 @@ def test_dtype_error(self): # in dynamic mode with self.assertRaises(RuntimeError): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input = np.random.random(self._shape).astype("int32") input_t = paddle.to_tensor(input) res = paddle.digamma(input_t) diff --git a/test/legacy_test/test_dist_allreduce_op.py b/test/legacy_test/test_dist_allreduce_op.py index afde2ee0ebae5..cb77d5476cfaf 100644 --- a/test/legacy_test/test_dist_allreduce_op.py +++ b/test/legacy_test/test_dist_allreduce_op.py @@ -30,9 +30,9 @@ def _setup_config(self): self._nccl2_reduce_layer = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_allreduce_op.py", delta=1e-5, check_error_log=True ) diff --git a/test/legacy_test/test_dist_base.py b/test/legacy_test/test_dist_base.py index a1ea2d40a4b0f..2e6895b717579 100755 --- a/test/legacy_test/test_dist_base.py +++ b/test/legacy_test/test_dist_base.py @@ -28,11 +28,11 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base +from paddle.base import compiler from paddle.distributed.fleet.meta_optimizers import ( RawProgramOptimizer as RawProgram, ) -from paddle.fluid import compiler from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.collective import ( DistributedStrategy, @@ -149,7 +149,7 @@ def get_transpiler( nccl_comm_num=1, hogwild_mode=False, ): - # NOTE: import fluid until runtime, or else forking processes will cause error. + # NOTE: import base until runtime, or else forking processes will cause error. config = paddle.distributed.transpiler.DistributeTranspilerConfig() config.enable_dc_asgd = dc_asgd config.sync_mode = sync_mode @@ -186,7 +186,7 @@ def run_pserver(self, args): t = self.get_transpiler( trainer_id=args.trainer_id, - main_program=fluid.default_main_program(), + main_program=base.default_main_program(), pserver_endpoints=args.endpoints, trainers=args.trainers, sync_mode=args.sync_mode, @@ -198,8 +198,8 @@ def run_pserver(self, args): args.current_endpoint, pserver_prog ) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) print_to_err(type(self).__name__, "run pserver startup program done.") exe.run(pserver_prog) @@ -223,10 +223,10 @@ def run_pipeline_trainer(self, args): device_id = int(os.getenv("FLAGS_selected_gpus", "0")) eprint(type(self).__name__, "device_id: %d." % device_id) - place = fluid.CUDAPlace(device_id) + place = base.CUDAPlace(device_id) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) eprint(type(self).__name__, "run worker startup program done.") data_loader.set_sample_list_generator(train_reader, place) @@ -234,7 +234,7 @@ def run_pipeline_trainer(self, args): print_to_err(type(self).__name__, "begin to train on trainer") out_losses = [] - main_program = fluid.default_main_program() + main_program = base.default_main_program() lr_scheduler = self.get_lr_scheduler(main_program) for i in range(RUN_STEP): loss = exe.run(main_program, fetch_list=[avg_cost]) @@ -269,24 +269,24 @@ def run_use_fleet_api_20_trainer(self, args): predict, ) = self.get_model(batch_size=args.batch_size) - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - elif fluid.core.is_compiled_with_xpu(): + place = base.CUDAPlace(device_id) + elif base.core.is_compiled_with_xpu(): device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: raise ValueError( "fleet dygraph api must in paddlepaddle-xpu or paddlepaddle-gpu." ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) eprint(type(self).__name__, "run worker startup program done.") feed_var_list = [ var - for var in fluid.default_main_program().global_block().vars.values() + for var in base.default_main_program().global_block().vars.values() if var.is_data ] @@ -295,7 +295,7 @@ def run_use_fleet_api_20_trainer(self, args): if feed_var_list[0].name == 'label': feed_var_list = feed_var_list[::-1] - feeder = fluid.DataFeeder(feed_var_list, place) + feeder = base.DataFeeder(feed_var_list, place) reader_generator = train_reader() def get_data(): @@ -319,7 +319,7 @@ def get_data(): out_losses = [] for i in range(RUN_STEP): (loss,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), fetch_list=[avg_cost.name], feed=feeder.feed(get_data()), ) @@ -335,7 +335,7 @@ def run_use_fleet_api_trainer(self, args): self.lr = args.lr - exec_strategy = fluid.ExecutionStrategy() + exec_strategy = base.ExecutionStrategy() exec_strategy.num_threads = 1 dist_strategy = DistributedStrategy() @@ -369,19 +369,19 @@ def run_use_fleet_api_trainer(self, args): trainer_prog = fleet._origin_program dist_prog = fleet.main_program - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - elif fluid.core.is_compiled_with_xpu(): + place = base.CUDAPlace(device_id) + elif base.core.is_compiled_with_xpu(): device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: raise ValueError( "fleet dygraph api must in paddlepaddle-xpu or paddlepaddle-gpu." ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) eprint(type(self).__name__, "run worker startup program done.") feed_var_list = [ @@ -397,7 +397,7 @@ def run_use_fleet_api_trainer(self, args): if feed_var_list[0].name == 'label': feed_var_list = feed_var_list[::-1] - feeder = fluid.DataFeeder(feed_var_list, place) + feeder = base.DataFeeder(feed_var_list, place) reader_generator = train_reader() def get_data(): @@ -428,37 +428,37 @@ def get_data(): if args.save_model: model_save_dir = "/tmp" if fleet.worker_index() == 0: - model_save_dir_fluid = os.path.join( - model_save_dir, "fluid_persistables" + model_save_dir_base = os.path.join( + model_save_dir, "base_persistables" ) model_save_dir_fleet = os.path.join( model_save_dir, "fleet_persistables" ) - infer_save_dir_fluid = os.path.join( - model_save_dir, "fluid_infer/infer" + infer_save_dir_base = os.path.join( + model_save_dir, "base_infer/infer" ) infer_save_dir_fleet = os.path.join( model_save_dir, "fleet_infer/infer" ) else: - model_save_dir_fluid = os.path.join( - model_save_dir, "fluid_persistables_2" + model_save_dir_base = os.path.join( + model_save_dir, "base_persistables_2" ) model_save_dir_fleet = os.path.join( model_save_dir, "fleet_persistables_2" ) - infer_save_dir_fluid = os.path.join( - model_save_dir, "fluid_infer_2/infer_2" + infer_save_dir_base = os.path.join( + model_save_dir, "base_infer_2/infer_2" ) infer_save_dir_fleet = os.path.join( model_save_dir, "fleet_infer_2/infer_2" ) paddle.distributed.io.save_persistables( - exe, model_save_dir_fluid, fleet._origin_program + exe, model_save_dir_base, fleet._origin_program ) fleet.save_persistables(executor=exe, dirname=model_save_dir_fleet) paddle.static.io.save_inference_model( - path_prefix=infer_save_dir_fluid, + path_prefix=infer_save_dir_base, feed_vars=feed_var_list, fetch_vars=[avg_cost], executor=exe, @@ -474,7 +474,7 @@ def run_trainer(self, args): old_stdout = sys.stdout sys.stdout = StringIO() - build_stra = fluid.BuildStrategy() + build_stra = base.BuildStrategy() # FIXME force disable enable_inplace and memory_optimize build_stra.enable_inplace = False build_stra.memory_optimize = False @@ -491,11 +491,11 @@ def run_trainer(self, args): if args.use_reduce: build_stra.reduce_strategy = ( - fluid.BuildStrategy.ReduceStrategy.Reduce + base.BuildStrategy.ReduceStrategy.Reduce ) else: build_stra.reduce_strategy = ( - fluid.BuildStrategy.ReduceStrategy.AllReduce + base.BuildStrategy.ReduceStrategy.AllReduce ) pass_builder = None if args.batch_merge_repeat > 1: @@ -554,7 +554,7 @@ def run_trainer(self, args): ) t = self.get_transpiler( trainer_id=args.trainer_id, - main_program=fluid.default_main_program(), + main_program=base.default_main_program(), pserver_endpoints=args.endpoints, trainers=args.trainers, sync_mode=args.sync_mode, @@ -589,21 +589,21 @@ def run_trainer(self, args): ) nccl2_t.transpile( args.trainer_id, - program=fluid.default_main_program(), - startup_program=fluid.default_startup_program(), + program=base.default_main_program(), + startup_program=base.default_startup_program(), trainers=args.endpoints, current_endpoint=args.current_endpoint, ) print_to_err( type(self).__name__, "get trainer program done. with nccl2 mode" ) - trainer_prog = fluid.default_main_program() + trainer_prog = base.default_main_program() else: print_to_err( type(self).__name__, "do nothing about main program, just use it", ) - trainer_prog = fluid.default_main_program() + trainer_prog = base.default_main_program() print_to_err(type(self).__name__, "use main program done.") # FIXME(gongwb):wait pserver initialization. @@ -611,15 +611,15 @@ def run_trainer(self, args): if args.use_cuda: device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) + place = base.CUDAPlace(device_id) else: - place = fluid.CPUPlace() + place = base.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) print_to_err(type(self).__name__, "run worker startup program done.") - exec_strategy = fluid.ExecutionStrategy() + exec_strategy = base.ExecutionStrategy() exec_strategy.num_threads = 1 print_to_err(type(self).__name__, "begin to compile with data parallel") @@ -634,7 +634,7 @@ def run_trainer(self, args): if var.is_data ] - feeder = fluid.DataFeeder(feed_var_list, place) + feeder = base.DataFeeder(feed_var_list, place) reader_generator = train_reader() def get_data(): @@ -715,19 +715,19 @@ def _get_data(self, batch, args): def run_trainer(self, args): seed = 90 if args.update_method == 'gloo': - place = fluid.CPUPlace() - elif fluid.core.is_compiled_with_cuda(): + place = base.CPUPlace() + elif base.core.is_compiled_with_cuda(): device_id = int(os.getenv("FLAGS_selected_gpus", "0")) - place = fluid.CUDAPlace(device_id) - elif fluid.core.is_compiled_with_xpu(): + place = base.CUDAPlace(device_id) + elif base.core.is_compiled_with_xpu(): device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) + place = base.XPUPlace(device_id) else: assert "Only support CUDAPlace or XPUPlace or CPU(Gloo) for now." - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + with base.dygraph.guard(place): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed np.random.seed(seed) import random @@ -976,7 +976,7 @@ def _after_setup_config(self): self.__use_xpu = True self._use_dgc = False else: - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.__use_cuda = True else: self.__use_cuda = False diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_async.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_async.py index 94daccf0c5991..96a07d4dc2479 100644 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_async.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_async.py @@ -37,11 +37,11 @@ def test_a_sync_optimizer_trainer(self): os.environ["TRAINING_ROLE"] = "TRAINER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) @@ -58,7 +58,7 @@ def test_a_sync_optimizer_trainer(self): optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) optimizer.minimize(avg_cost) - prog = paddle.fluid.default_main_program() + prog = paddle.base.default_main_program() self.assertEqual(prog.global_block().ops[-1].type, "send_barrier") sends = 0 @@ -75,11 +75,11 @@ def test_a_sync_optimizer_pserver(self): os.environ["TRAINING_ROLE"] = "PSERVER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py index 54a8e7c1de27d..4218e567a33e7 100644 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py @@ -37,11 +37,11 @@ def test_a_sync_optimizer1(self): os.environ["TRAINING_ROLE"] = "TRAINER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) input_x = paddle.static.data(name="x", shape=[-1, 32], dtype='float32') diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py index a5a95370168ac..67807c6673701 100644 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py @@ -39,11 +39,11 @@ def test_a_sync_optimizer3(self): os.environ["TRAINING_ROLE"] = "TRAINER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) input_x = paddle.static.data( @@ -56,7 +56,7 @@ def test_a_sync_optimizer3(self): is_distributed=False, input=input_x, size=[1000000000, 100000], - param_attr=paddle.fluid.ParamAttr( + param_attr=paddle.base.ParamAttr( name="embedding", initializer=paddle.paddle.nn.initializer.Constant(value=0.01), ), diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py index 7bf6733c7c77b..104143d70949e 100644 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py @@ -38,11 +38,11 @@ def test_a_sync_optimizer2(self): os.environ["TRAINING_ROLE"] = "TRAINER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_geo.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_geo.py index 7b565264f995b..475cba5081190 100755 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_geo.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_geo.py @@ -36,11 +36,11 @@ def test_a_sync_optimizer_trainer(self): os.environ["TRAINING_ROLE"] = "TRAINER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) input_x = paddle.static.data(name="x", shape=[-1, 32], dtype='float32') @@ -67,11 +67,11 @@ def test_a_sync_optimizer_pserver(self): os.environ["TRAINING_ROLE"] = "PSERVER" from paddle.distributed import fleet - main_program = paddle.fluid.Program() - startup_program = paddle.fluid.Program() + main_program = paddle.base.Program() + startup_program = paddle.base.Program() - paddle.fluid.framework.switch_main_program(main_program) - paddle.fluid.framework.switch_startup_program(startup_program) + paddle.base.framework.switch_main_program(main_program) + paddle.base.framework.switch_startup_program(startup_program) fleet.init(role_maker.PaddleCloudRoleMaker()) input_x = paddle.static.data(name="x", shape=[-1, 32], dtype='float32') @@ -93,7 +93,7 @@ def test_a_sync_optimizer_pserver(self): optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) optimizer.minimize(avg_cost) - prog = paddle.fluid.default_main_program() + prog = paddle.base.default_main_program() self.assertEqual(prog.global_block().ops[0].type, "listen_and_serv") diff --git a/test/legacy_test/test_dist_fleet_a_sync_optimizer_sync.py b/test/legacy_test/test_dist_fleet_a_sync_optimizer_sync.py index 69e751a04419e..90763b6fa74e2 100644 --- a/test/legacy_test/test_dist_fleet_a_sync_optimizer_sync.py +++ b/test/legacy_test/test_dist_fleet_a_sync_optimizer_sync.py @@ -50,7 +50,7 @@ def test_gradient_merge_optimizer(self): optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) optimizer.minimize(avg_cost) - prog = paddle.fluid.default_main_program() + prog = paddle.base.default_main_program() self.assertEqual(prog.global_block().ops[-1].type, "send_barrier") sends = 0 diff --git a/test/legacy_test/test_dist_fleet_base.py b/test/legacy_test/test_dist_fleet_base.py index 45657b71d9910..ad421c228b0e5 100644 --- a/test/legacy_test/test_dist_fleet_base.py +++ b/test/legacy_test/test_dist_fleet_base.py @@ -28,7 +28,7 @@ from contextlib import closing import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker from paddle.distributed.fleet.utils.ps_util import DistributedInfer @@ -129,8 +129,8 @@ def build_optimizer(self, avg_cost, strategy): optimizer = paddle.optimizer.SGD(scheduler, grad_clip=grad_clip) """ # learning rate decay method before 2.0 - optimizer = fluid.optimizer.SGD( - learning_rate=fluid.layers.exponential_decay( + optimizer = base.optimizer.SGD( + learning_rate=base.layers.exponential_decay( learning_rate=LEARNING_RATE, decay_steps=500, decay_rate=0.969, @@ -160,10 +160,10 @@ def get_executor(self): if self._exe is None: device_env = os.getenv("DEVICE", 'cpu') if device_env == 'cpu': - device = fluid.CPUPlace() + device = base.CPUPlace() elif device_env == 'gpu': - device = fluid.CUDAPlace(0) - self._exe = fluid.Executor(device) + device = base.CUDAPlace(0) + self._exe = base.Executor(device) return self._exe def do_dataset_training(self, fleet): diff --git a/test/legacy_test/test_dist_fleet_heter_program.py b/test/legacy_test/test_dist_fleet_heter_program.py index eeefe24d05191..150be7559906c 100644 --- a/test/legacy_test/test_dist_fleet_heter_program.py +++ b/test/legacy_test/test_dist_fleet_heter_program.py @@ -17,7 +17,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet from paddle.distributed.fleet.base import role_maker @@ -86,7 +86,7 @@ def embedding_layer(input): return paddle.static.nn.sparse_embedding( input=input, size=[100001, 10], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="SparseFeatFactors", initializer=paddle.nn.initializer.Uniform(), ), @@ -96,12 +96,12 @@ def embedding_layer(input): concated = paddle.concat(sparse_embed_seq + inputs[0:1], axis=1) - with fluid.device_guard("gpu"): + with base.device_guard("gpu"): fc1 = paddle.static.nn.fc( x=concated, size=400, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal( std=1 / math.sqrt(concated.shape[1]) ) @@ -109,12 +109,12 @@ def embedding_layer(input): name="fc1", ) - with fluid.device_guard("cpu"): + with base.device_guard("cpu"): fc2 = paddle.static.nn.fc( x=fc1, size=400, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal( std=1 / math.sqrt(fc1.shape[1]) ) @@ -122,12 +122,12 @@ def embedding_layer(input): name="fc2", ) - with fluid.device_guard("gpu"): + with base.device_guard("gpu"): fc3 = paddle.static.nn.fc( x=fc2, size=400, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal( std=1 / math.sqrt(fc2.shape[1]) ) @@ -135,19 +135,19 @@ def embedding_layer(input): name="fc3", ) - with fluid.device_guard("cpu"): + with base.device_guard("cpu"): predict = paddle.static.nn.fc( x=fc3, size=2, activation="softmax", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal( std=1 / math.sqrt(fc3.shape[1]) ) ), ) - with fluid.device_guard("gpu"): + with base.device_guard("gpu"): labels = paddle.cast(inputs[-1], dtype="int64") cost = paddle.nn.functional.cross_entropy( input=predict, label=labels, reduction='none', use_softmax=False diff --git a/test/legacy_test/test_dist_fleet_minimize.py b/test/legacy_test/test_dist_fleet_minimize.py index cc317cd353ead..8313f5d40ecfd 100644 --- a/test/legacy_test/test_dist_fleet_minimize.py +++ b/test/legacy_test/test_dist_fleet_minimize.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -78,7 +78,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -94,7 +94,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -110,7 +110,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -126,12 +126,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -141,7 +141,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -157,12 +157,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps.py b/test/legacy_test/test_dist_fleet_ps.py index 1b6becb18f5ae..66612fa224aa7 100644 --- a/test/legacy_test/test_dist_fleet_ps.py +++ b/test/legacy_test/test_dist_fleet_ps.py @@ -18,7 +18,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -79,7 +79,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=q, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -96,7 +96,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -113,7 +113,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=pt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -130,12 +130,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -146,7 +146,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=nt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -163,12 +163,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps11.py b/test/legacy_test/test_dist_fleet_ps11.py index 44ad3514f64d8..380b0b93ac483 100755 --- a/test/legacy_test/test_dist_fleet_ps11.py +++ b/test/legacy_test/test_dist_fleet_ps11.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -78,7 +78,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -94,7 +94,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -110,7 +110,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -126,12 +126,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -141,7 +141,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -157,12 +157,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps12.py b/test/legacy_test/test_dist_fleet_ps12.py index 7bc2fdcd479ae..122d3c81c26dd 100644 --- a/test/legacy_test/test_dist_fleet_ps12.py +++ b/test/legacy_test/test_dist_fleet_ps12.py @@ -19,7 +19,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -81,7 +81,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -97,7 +97,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -113,7 +113,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -129,12 +129,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -144,7 +144,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -160,12 +160,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps13.py b/test/legacy_test/test_dist_fleet_ps13.py index edced3b5c8ec9..968c978be79d3 100644 --- a/test/legacy_test/test_dist_fleet_ps13.py +++ b/test/legacy_test/test_dist_fleet_ps13.py @@ -19,7 +19,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -82,7 +82,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -98,7 +98,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -114,7 +114,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -130,12 +130,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -145,7 +145,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -161,12 +161,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps2.py b/test/legacy_test/test_dist_fleet_ps2.py index d134a02d1fecb..7feb1e8ca7af6 100644 --- a/test/legacy_test/test_dist_fleet_ps2.py +++ b/test/legacy_test/test_dist_fleet_ps2.py @@ -19,7 +19,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -81,7 +81,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -98,7 +98,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -114,7 +114,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -130,12 +130,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -145,7 +145,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -161,12 +161,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps3.py b/test/legacy_test/test_dist_fleet_ps3.py index 286edccfebda2..d76af1a93059d 100644 --- a/test/legacy_test/test_dist_fleet_ps3.py +++ b/test/legacy_test/test_dist_fleet_ps3.py @@ -18,7 +18,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -79,7 +79,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=q, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -96,7 +96,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -113,7 +113,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=pt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -130,12 +130,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -146,7 +146,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=nt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -163,12 +163,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps4.py b/test/legacy_test/test_dist_fleet_ps4.py index 1f74970707723..2585ab99c7521 100644 --- a/test/legacy_test/test_dist_fleet_ps4.py +++ b/test/legacy_test/test_dist_fleet_ps4.py @@ -18,7 +18,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -78,7 +78,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -94,7 +94,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -110,7 +110,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -126,12 +126,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -141,7 +141,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -157,12 +157,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps5.py b/test/legacy_test/test_dist_fleet_ps5.py index 63290ee6f4d79..914c31134542e 100644 --- a/test/legacy_test/test_dist_fleet_ps5.py +++ b/test/legacy_test/test_dist_fleet_ps5.py @@ -18,7 +18,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -79,7 +79,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=q, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -96,7 +96,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -113,7 +113,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=pt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -130,12 +130,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -146,7 +146,7 @@ def get_loss(cos_q_pt, cos_q_nt): input=nt, is_distributed=is_distributed, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__tmp_", learning_rate=emb_lr, @@ -163,12 +163,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_ps6.py b/test/legacy_test/test_dist_fleet_ps6.py index 8b96ed41a31fd..bbda76ae3f32c 100644 --- a/test/legacy_test/test_dist_fleet_ps6.py +++ b/test/legacy_test/test_dist_fleet_ps6.py @@ -18,7 +18,7 @@ paddle.enable_static() -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -78,7 +78,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -94,7 +94,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -110,7 +110,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -126,12 +126,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -141,7 +141,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -157,12 +157,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) diff --git a/test/legacy_test/test_dist_fleet_raw_program_optimizer.py b/test/legacy_test/test_dist_fleet_raw_program_optimizer.py index c19791a3c33a8..a0fde4c955ecc 100644 --- a/test/legacy_test/test_dist_fleet_raw_program_optimizer.py +++ b/test/legacy_test/test_dist_fleet_raw_program_optimizer.py @@ -34,9 +34,9 @@ def _setup_config(self): self._use_fleet_api_20 = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_fleet_raw_program_optimizer.py", delta=1e-5, @@ -50,9 +50,9 @@ def need_envs(self): return {'FLAGS_sync_before_allreduce': '1'} def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_fleet_raw_program_optimizer.py", delta=1e-5, diff --git a/test/legacy_test/test_dist_fleet_raw_program_optimizer_fuse_allreduce.py b/test/legacy_test/test_dist_fleet_raw_program_optimizer_fuse_allreduce.py index f80207e310c22..68c58ac9a5d90 100644 --- a/test/legacy_test/test_dist_fleet_raw_program_optimizer_fuse_allreduce.py +++ b/test/legacy_test/test_dist_fleet_raw_program_optimizer_fuse_allreduce.py @@ -34,9 +34,9 @@ def _setup_config(self): self._use_fleet_api_20 = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_fleet_raw_program_optimizer_fuse_allreduce.py", delta=1e-5, diff --git a/test/legacy_test/test_dist_fleet_sparse_embedding_ctr.py b/test/legacy_test/test_dist_fleet_sparse_embedding_ctr.py index 1995df11a09d3..c20c1afc0f5db 100644 --- a/test/legacy_test/test_dist_fleet_sparse_embedding_ctr.py +++ b/test/legacy_test/test_dist_fleet_sparse_embedding_ctr.py @@ -24,7 +24,7 @@ from dist_fleet_sparse_embedding_ctr import fake_ctr_reader from test_dist_fleet_base import TestFleetBase -from paddle import fluid +from paddle import base @unittest.skip(reason="Skip unstable ut, need paddle sync mode fix") @@ -218,7 +218,7 @@ def net(): input=dnn_data, size=[dnn_input_dim, dnn_layer_dims[0]], is_test=inference, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="deep_embedding", initializer=init ), ) @@ -231,7 +231,7 @@ def net(): x=dnn_out, size=dim, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01) ), name='dnn-fc-%d' % i, @@ -243,7 +243,7 @@ def net(): input=lr_data, size=[lr_input_dim, 1], is_test=inference, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="wide_embedding", initializer=paddle.nn.initializer.Constant(value=0.01), ), @@ -260,15 +260,15 @@ def net(): reader = paddle.batch(fake_ctr_reader(), batch_size=4) datas, predict = net() - exe = fluid.Executor(fluid.CPUPlace()) - feeder = fluid.DataFeeder(place=fluid.CPUPlace(), feed_list=datas) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + feeder = base.DataFeeder(place=base.CPUPlace(), feed_list=datas) + exe.run(base.default_startup_program()) paddle.distributed.io.load_persistables(exe, model_file) for batch_id, data in enumerate(reader()): score = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=feeder.feed(data), fetch_list=[predict], ) diff --git a/test/legacy_test/test_dist_fleet_spmt.py b/test/legacy_test/test_dist_fleet_spmt.py index d9b5ad66b4fa3..2579e2576227d 100644 --- a/test/legacy_test/test_dist_fleet_spmt.py +++ b/test/legacy_test/test_dist_fleet_spmt.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -76,7 +76,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_emb = paddle.static.nn.sparse_embedding( input=q, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -92,7 +92,7 @@ def get_loss(cos_q_pt, cos_q_nt): q_fc = paddle.static.nn.fc( x=q_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__q_fc__", learning_rate=base_lr, @@ -108,7 +108,7 @@ def get_loss(cos_q_pt, cos_q_nt): pt_emb = paddle.static.nn.sparse_embedding( input=pt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -124,12 +124,12 @@ def get_loss(cos_q_pt, cos_q_nt): pt_fc = paddle.static.nn.fc( x=pt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) # nt nt = paddle.static.data( @@ -139,7 +139,7 @@ def get_loss(cos_q_pt, cos_q_nt): nt_emb = paddle.static.nn.sparse_embedding( input=nt, size=[dict_dim, emb_dim], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__emb__", learning_rate=emb_lr, @@ -155,12 +155,12 @@ def get_loss(cos_q_pt, cos_q_nt): nt_fc = paddle.static.nn.fc( x=nt_ss, size=hid_dim, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.01), name="__fc__", learning_rate=base_lr, ), - bias_attr=fluid.ParamAttr(name="__fc_b__"), + bias_attr=base.ParamAttr(name="__fc_b__"), ) cos_q_pt = paddle.nn.functional.cosine_similarity(q_fc, pt_fc) cos_q_nt = paddle.nn.functional.cosine_similarity(q_fc, nt_fc) @@ -235,10 +235,10 @@ def test_SingleProcessMultiThread(self): os.environ["PADDLE_FUSE_ALLREDUCE"] = "1" os.environ["PADDLE_LOSS_SCALE"] = "1" - startup_program = fluid.Program() - main_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): - with fluid.unique_name.guard(): + startup_program = base.Program() + main_program = base.Program() + with base.program_guard(main_program, startup_program): + with base.unique_name.guard(): loss, acc, _ = self.net() optimizer = paddle.optimizer.Adam(learning_rate=0.01) optimizer.minimize(loss) diff --git a/test/legacy_test/test_dist_hapi_model.py b/test/legacy_test/test_dist_hapi_model.py index 314a7621f07fc..1e5ec1d341f71 100644 --- a/test/legacy_test/test_dist_hapi_model.py +++ b/test/legacy_test/test_dist_hapi_model.py @@ -18,7 +18,7 @@ import time import unittest -from paddle import fluid +from paddle import base from paddle.distributed.utils.launch_utils import ( TrainerProc, find_free_ports, @@ -105,7 +105,7 @@ def start_local_trainers( class TestMultipleGpus(unittest.TestCase): def run_mnist_2gpu(self, target_file_name): - if fluid.core.get_cuda_device_count() == 0: + if base.core.get_cuda_device_count() == 0: return selected_gpus = get_gpus('0,1') diff --git a/test/legacy_test/test_dist_lookup_sparse_table_fuse_ops.py b/test/legacy_test/test_dist_lookup_sparse_table_fuse_ops.py index 1a2df8979f712..b320b887d2298 100644 --- a/test/legacy_test/test_dist_lookup_sparse_table_fuse_ops.py +++ b/test/legacy_test/test_dist_lookup_sparse_table_fuse_ops.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -32,16 +32,16 @@ def test_fuse(self): self.check_with_place(place) def check_with_place(self, place): - scope = fluid.global_scope() + scope = base.global_scope() scope.var("LearningRate").get_tensor().set([0.01], place) scope.var("Ids").get_tensor().set(list(range(100)), place) - init_program = fluid.Program() + init_program = base.Program() lr = init_program.global_block().create_var( name="LearningRate", persistable=True, - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[1], dtype="float32", ) @@ -49,14 +49,14 @@ def check_with_place(self, place): ids = init_program.global_block().create_var( name="Ids", persistable=True, - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[100], dtype="int64", ) output = init_program.global_block().create_var( name="output", - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[100, 8], dtype="float32", ) @@ -98,10 +98,10 @@ def check_with_place(self, place): }, ) - executor = fluid.Executor(place) + executor = base.Executor(place) executor.run(init_program) - training_program = fluid.Program() + training_program = base.Program() scope.var('Beta1Pow').get_tensor().set( np.array([0]).astype("float32"), place @@ -124,7 +124,7 @@ def check_with_place(self, place): lr = training_program.global_block().create_var( name="LearningRate", persistable=True, - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[1], dtype="float32", ) @@ -132,7 +132,7 @@ def check_with_place(self, place): grads = training_program.global_block().create_var( name="Grad", persistable=True, - type=fluid.core.VarDesc.VarType.SELECTED_ROWS, + type=base.core.VarDesc.VarType.SELECTED_ROWS, shape=[100, 8], dtype="float32", ) @@ -140,7 +140,7 @@ def check_with_place(self, place): beta1 = training_program.global_block().create_var( name="Beta1Pow", persistable=True, - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[1], dtype="float32", ) @@ -148,7 +148,7 @@ def check_with_place(self, place): beta2 = training_program.global_block().create_var( name="Beta2Pow", persistable=True, - type=fluid.core.VarDesc.VarType.LOD_TENSOR, + type=base.core.VarDesc.VarType.LOD_TENSOR, shape=[1], dtype="float32", ) diff --git a/test/legacy_test/test_dist_mnist_backward_deps.py b/test/legacy_test/test_dist_mnist_backward_deps.py index 10089293b3b08..5a55891242154 100644 --- a/test/legacy_test/test_dist_mnist_backward_deps.py +++ b/test/legacy_test/test_dist_mnist_backward_deps.py @@ -30,9 +30,9 @@ def _setup_config(self): self._enable_backward_deps = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_mnist.py", delta=1e-5) diff --git a/test/legacy_test/test_dist_mnist_fleet_save.py b/test/legacy_test/test_dist_mnist_fleet_save.py index 0cee8d58cc677..aa25a34b469f2 100644 --- a/test/legacy_test/test_dist_mnist_fleet_save.py +++ b/test/legacy_test/test_dist_mnist_fleet_save.py @@ -33,46 +33,46 @@ def _setup_config(self): self._save_model = True def _rm_temp_files(self, dirname): - fluid_model_path = os.path.join(dirname, 'fluid_persistables') + base_model_path = os.path.join(dirname, 'base_persistables') fleet_model_path = os.path.join(dirname, 'fleet_persistables') - fluid_infer_path = os.path.join(dirname, 'fluid_infer') + base_infer_path = os.path.join(dirname, 'base_infer') fleet_infer_path = os.path.join(dirname, 'fleet_infer') - fluid_model_path_2 = os.path.join(dirname, 'fluid_persistables_2') + base_model_path_2 = os.path.join(dirname, 'base_persistables_2') fleet_model_path_2 = os.path.join(dirname, 'fleet_persistables_2') - fluid_infer_path_2 = os.path.join(dirname, 'fluid_infer_2') + base_infer_path_2 = os.path.join(dirname, 'base_infer_2') fleet_infer_path_2 = os.path.join(dirname, 'fleet_infer_2') - shutil.rmtree(fluid_model_path) + shutil.rmtree(base_model_path) shutil.rmtree(fleet_model_path) - shutil.rmtree(fluid_infer_path) + shutil.rmtree(base_infer_path) shutil.rmtree(fleet_infer_path) - shutil.rmtree(fluid_model_path_2) + shutil.rmtree(base_model_path_2) shutil.rmtree(fleet_model_path_2) - shutil.rmtree(fluid_infer_path_2) + shutil.rmtree(base_infer_path_2) shutil.rmtree(fleet_infer_path_2) def _test_saved_files(self, dirname): - fluid_model_path = os.path.join(dirname, 'fluid_persistables') - fluid_persistables = sorted(os.listdir(fluid_model_path)) + base_model_path = os.path.join(dirname, 'base_persistables') + base_persistables = sorted(os.listdir(base_model_path)) fleet_model_path = os.path.join(dirname, 'fleet_persistables') fleet_persistables = sorted(os.listdir(fleet_model_path)) - fluid_infer_path = os.path.join(dirname, 'fluid_infer') - fluid_infer_files = sorted(os.listdir(fluid_infer_path)) + base_infer_path = os.path.join(dirname, 'base_infer') + base_infer_files = sorted(os.listdir(base_infer_path)) fleet_infer_path = os.path.join(dirname, 'fleet_infer') fleet_infer_files = sorted(os.listdir(fleet_infer_path)) - if len(fluid_persistables) != len(fleet_persistables): + if len(base_persistables) != len(fleet_persistables): self._rm_temp_files(dirname) raise ValueError("Test Failed.") - for i in range(len(fluid_persistables)): - if fluid_persistables[i] != fleet_persistables[i]: + for i in range(len(base_persistables)): + if base_persistables[i] != fleet_persistables[i]: self._rm_temp_files(dirname) raise ValueError("Test Failed.") - if len(fluid_infer_files) != len(fleet_infer_files): + if len(base_infer_files) != len(fleet_infer_files): self._rm_temp_files(dirname) raise ValueError("Test Failed.") - for i in range(len(fluid_infer_files)): - if fluid_infer_files[i] != fleet_infer_files[i]: + for i in range(len(base_infer_files)): + if base_infer_files[i] != fleet_infer_files[i]: self._rm_temp_files(dirname) raise ValueError("Test Failed.") self._rm_temp_files(dirname) @@ -100,9 +100,9 @@ def check_with_place( self._test_saved_files(dirname) def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_mnist.py", delta=1e-5) diff --git a/test/legacy_test/test_dist_mnist_fleetapi.py b/test/legacy_test/test_dist_mnist_fleetapi.py index 96435a8163778..8a79af12a8d54 100644 --- a/test/legacy_test/test_dist_mnist_fleetapi.py +++ b/test/legacy_test/test_dist_mnist_fleetapi.py @@ -31,9 +31,9 @@ def _setup_config(self): self._sync_batch_norm = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist.py", delta=1e-5, @@ -44,14 +44,14 @@ def test_dist_train(self): class FleetCollectiveTest(unittest.TestCase): def test_open_sync_batch_norm(self): - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.collective import ( DistributedStrategy, fleet, ) - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): # Operator "gen_nccl_id" has not been registered return diff --git a/test/legacy_test/test_dist_mnist_fp16_allreduce.py b/test/legacy_test/test_dist_mnist_fp16_allreduce.py index 707f25beed0f8..799799a52598b 100644 --- a/test/legacy_test/test_dist_mnist_fp16_allreduce.py +++ b/test/legacy_test/test_dist_mnist_fp16_allreduce.py @@ -25,9 +25,9 @@ def _setup_config(self): self._nccl2_reduce_layer = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist_fp16_allreduce.py", delta=1e-5, check_error_log=True ) diff --git a/test/legacy_test/test_dist_mnist_hallreduce.py b/test/legacy_test/test_dist_mnist_hallreduce.py index 8fc5181c63cd7..bc056ca3cc382 100644 --- a/test/legacy_test/test_dist_mnist_hallreduce.py +++ b/test/legacy_test/test_dist_mnist_hallreduce.py @@ -33,9 +33,9 @@ def _setup_config(self): self._use_hallreduce = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist.py", delta=1e-5, diff --git a/test/legacy_test/test_dist_mnist_multi_comm.py b/test/legacy_test/test_dist_mnist_multi_comm.py index ae19548be40f9..d57515b000219 100644 --- a/test/legacy_test/test_dist_mnist_multi_comm.py +++ b/test/legacy_test/test_dist_mnist_multi_comm.py @@ -32,9 +32,9 @@ def _setup_config(self): self._nccl_comm_num = 3 def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist.py", delta=1e-5, diff --git a/test/legacy_test/test_dist_mnist_pg.py b/test/legacy_test/test_dist_mnist_pg.py index cf46394f68ddc..38cae5f257bfc 100644 --- a/test/legacy_test/test_dist_mnist_pg.py +++ b/test/legacy_test/test_dist_mnist_pg.py @@ -29,9 +29,9 @@ def _setup_config(self): self._nccl2_mode = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_mnist.py", delta=1, diff --git a/test/legacy_test/test_dist_mnist_ring_allreduce.py b/test/legacy_test/test_dist_mnist_ring_allreduce.py index cb3fb5d34eb5e..fdb65440c8f63 100644 --- a/test/legacy_test/test_dist_mnist_ring_allreduce.py +++ b/test/legacy_test/test_dist_mnist_ring_allreduce.py @@ -29,9 +29,9 @@ def _setup_config(self): self._nccl2_mode = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_mnist.py", delta=1e-5) diff --git a/test/legacy_test/test_dist_mnist_with_program.py b/test/legacy_test/test_dist_mnist_with_program.py index 42b47648ba86a..9c29944cc3777 100644 --- a/test/legacy_test/test_dist_mnist_with_program.py +++ b/test/legacy_test/test_dist_mnist_with_program.py @@ -31,9 +31,9 @@ def _setup_config(self): self._use_local_sgd = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_mnist.py", delta=1e-5) @@ -47,9 +47,9 @@ def _setup_config(self): self._ut4grad_allreduce = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_mnist.py", delta=1e-5) diff --git a/test/legacy_test/test_dist_op.py b/test/legacy_test/test_dist_op.py index 958071cfc62a1..be7f05b638d8a 100644 --- a/test/legacy_test/test_dist_op.py +++ b/test/legacy_test/test_dist_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -246,9 +246,9 @@ def init_data_type(self): def test_api(self): self.init_data_type() - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): x = paddle.static.data( name='x', shape=[2, 3, 4, 5], dtype=self.data_type ) @@ -260,13 +260,13 @@ def test_api(self): y_i = np.random.random((3, 1, 5)).astype(self.data_type) result = paddle.dist(x, y, p) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': x_i, 'y': y_i}, fetch_list=[result], ) diff --git a/test/legacy_test/test_dist_se_resnext_nccl.py b/test/legacy_test/test_dist_se_resnext_nccl.py index 64d63048525f5..a56009d8b9bd2 100644 --- a/test/legacy_test/test_dist_se_resnext_nccl.py +++ b/test/legacy_test/test_dist_se_resnext_nccl.py @@ -30,9 +30,9 @@ def _setup_config(self): self._nccl2_mode = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_se_resnext.py", delta=1e-5, @@ -49,9 +49,9 @@ def _setup_config(self): self._mp_mode = True def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "dist_se_resnext.py", delta=1e-5, diff --git a/test/legacy_test/test_dist_sharding_save.py b/test/legacy_test/test_dist_sharding_save.py index ed994b813b59a..448cc900e3257 100755 --- a/test/legacy_test/test_dist_sharding_save.py +++ b/test/legacy_test/test_dist_sharding_save.py @@ -84,9 +84,9 @@ def check_with_place( self._test_saved_files(dirname) def test_dist_train(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place("dist_sharding_save.py", delta=1e-5) diff --git a/test/legacy_test/test_dist_sparse_load_ps0.py b/test/legacy_test/test_dist_sparse_load_ps0.py index 985aa0d933771..4b697d3db2fef 100644 --- a/test/legacy_test/test_dist_sparse_load_ps0.py +++ b/test/legacy_test/test_dist_sparse_load_ps0.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet from paddle.distributed.fleet.base import role_maker @@ -29,7 +29,7 @@ class SparseLoadOp(unittest.TestCase): """Test load operator.""" def net(self, emb_array, fc_array): - with fluid.unique_name.guard(): + with base.unique_name.guard(): dense_input = paddle.static.data( 'input', shape=[None, 1], dtype="int64" ) @@ -38,7 +38,7 @@ def net(self, emb_array, fc_array): input=dense_input, is_sparse=True, size=[10, 10], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="embedding", initializer=paddle.nn.initializer.Assign(emb_array), ), @@ -48,7 +48,7 @@ def net(self, emb_array, fc_array): x=emb, size=10, activation="relu", - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='fc', initializer=paddle.nn.initializer.Assign(fc_array), ), @@ -57,15 +57,15 @@ def net(self, emb_array, fc_array): return loss def save_origin_model(self, emb_array, fc_array): - startup_program = fluid.framework.Program() - test_program = fluid.framework.Program() - with fluid.framework.program_guard(test_program, startup_program): - with fluid.unique_name.guard(): + startup_program = base.framework.Program() + test_program = base.framework.Program() + with base.framework.program_guard(test_program, startup_program): + with base.unique_name.guard(): loss = self.net(emb_array, fc_array) optimizer = paddle.optimizer.Adam(1e-3) optimizer.minimize(loss) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) model_path = tempfile.mkdtemp() paddle.distributed.io.save_persistables( @@ -112,10 +112,10 @@ def test_2ps_0_load(self): optimizer.minimize(loss) fleet.init_server(model_path) - fc_w = np.array(fluid.global_scope().find_var("fc").get_tensor()) + fc_w = np.array(base.global_scope().find_var("fc").get_tensor()) emb = np.array( - fluid.global_scope().find_var("embedding.block0").get_tensor() + base.global_scope().find_var("embedding.block0").get_tensor() ) assert fc_w.all() == fc_array.all() diff --git a/test/legacy_test/test_dist_sparse_load_ps1.py b/test/legacy_test/test_dist_sparse_load_ps1.py index c5bb9b47c98b9..069eb70560a75 100644 --- a/test/legacy_test/test_dist_sparse_load_ps1.py +++ b/test/legacy_test/test_dist_sparse_load_ps1.py @@ -20,7 +20,7 @@ from test_dist_sparse_load_ps0 import SparseLoadOp import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet from paddle.distributed.fleet.base import role_maker @@ -53,8 +53,8 @@ def test_2ps_0_load(self): fc_array = np.arange(0, 1, 0.1).repeat(10).reshape(10, 10) model_path = self.save_origin_model(emb_array, fc_array) - startup_program = fluid.framework.Program() - test_program = fluid.framework.Program() + startup_program = base.framework.Program() + test_program = base.framework.Program() role = role_maker.PaddleCloudRoleMaker() fleet.init(role) loss = self.net(emb_array, fc_array) @@ -65,7 +65,7 @@ def test_2ps_0_load(self): optimizer.minimize(loss) fleet.init_server(model_path) emb = np.array( - fluid.global_scope().find_var("embedding.block1").get_tensor() + base.global_scope().find_var("embedding.block1").get_tensor() ) assert emb.all() == emb_array[1::2].all() shutil.rmtree(model_path) diff --git a/test/legacy_test/test_dist_sparse_tensor_load_adagrad.py b/test/legacy_test/test_dist_sparse_tensor_load_adagrad.py index 88354c6d797fb..35dd48accd42a 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_adagrad.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_adagrad.py @@ -17,7 +17,7 @@ from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet @@ -28,8 +28,8 @@ class TestSparseLoadProgramAdagrad(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.Adam(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_sparse_tensor_load_adam.py b/test/legacy_test/test_dist_sparse_tensor_load_adam.py index 1a2c60657c1a9..b5eae0e39807e 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_adam.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_adam.py @@ -17,7 +17,7 @@ from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet @@ -28,8 +28,8 @@ class TestSparseLoadProgramAdam(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.Adam(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_sparse_tensor_load_ftrl.py b/test/legacy_test/test_dist_sparse_tensor_load_ftrl.py index b473a6ac08922..6a1f0175b1619 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_ftrl.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_ftrl.py @@ -17,7 +17,7 @@ from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet @@ -28,8 +28,8 @@ class TestSparseLoadProgramFtrl(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.SGD(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_sparse_tensor_load_momentum.py b/test/legacy_test/test_dist_sparse_tensor_load_momentum.py index 6b45d5b72cc21..b7b590cbb3224 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_momentum.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_momentum.py @@ -17,7 +17,7 @@ from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet @@ -28,8 +28,8 @@ class TestSparseLoadProgramMomentum(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.SGD(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_sparse_tensor_load_rmsprop.py b/test/legacy_test/test_dist_sparse_tensor_load_rmsprop.py index 999b9315c83eb..9ce8e211f1e67 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_rmsprop.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_rmsprop.py @@ -17,7 +17,7 @@ from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet @@ -28,8 +28,8 @@ class TestSparseLoadProgramRmsprop(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.SGD(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_sparse_tensor_load_sgd.py b/test/legacy_test/test_dist_sparse_tensor_load_sgd.py index 5444ebdcae4c9..a8da5d52c61ec 100644 --- a/test/legacy_test/test_dist_sparse_tensor_load_sgd.py +++ b/test/legacy_test/test_dist_sparse_tensor_load_sgd.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet import fleet from paddle.distributed.fleet.base import role_maker @@ -40,12 +40,12 @@ def setUp(self): self.strategy.a_sync = True def net(self): - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): - with fluid.unique_name.guard(): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): + with base.unique_name.guard(): inputs = paddle.static.data( 'input', shape=[None, 1], dtype="int64" ) @@ -63,8 +63,8 @@ def net(self): class TestSparseLoadProgramSGD(TestSparseLoadProgram): def test_server_init(self): scope, train_program, startup_program, loss = self.net() - with fluid.scope_guard(scope): - with fluid.program_guard(train_program, startup_program): + with base.scope_guard(scope): + with base.program_guard(train_program, startup_program): optimizer = paddle.optimizer.SGD(1e-3) optimizer = fleet.distributed_optimizer( optimizer, self.strategy diff --git a/test/legacy_test/test_dist_train.py b/test/legacy_test/test_dist_train.py index 968594397c7d5..c1d8e5426db35 100644 --- a/test/legacy_test/test_dist_train.py +++ b/test/legacy_test/test_dist_train.py @@ -22,9 +22,9 @@ from dist_test_utils import remove_ps_flag import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.layers import ops +from paddle import base +from paddle.base import core +from paddle.base.layers import ops from paddle.incubate.nn.layer.io import ListenAndServ, Recv, Send RPC_OP_ROLE_ATTR_NAME = ( @@ -37,7 +37,7 @@ class TestSendOp(unittest.TestCase): def test_send(self): remove_ps_flag(os.getpid()) # Run init_serv in a thread - place = fluid.CPUPlace() + place = base.CPUPlace() # NOTE: python thread will not work here due to GIL. p = Process(target=self.init_serv, args=(place,)) p.daemon = True @@ -71,9 +71,9 @@ def _wait_ps_ready(self, pid): start_left_time -= sleep_time def init_serv(self, place): - main = fluid.Program() + main = base.Program() - with fluid.program_guard(main): + with base.program_guard(main): serv = ListenAndServ("127.0.0.1:0", ["X"], optimizer_mode=False) with serv.do(): out_var = main.global_block().create_var( @@ -92,12 +92,12 @@ def init_serv(self, place): ) ops._scale(x=x, scale=10.0, out=out_var) - self.server_exe = fluid.Executor(place) + self.server_exe = base.Executor(place) self.server_exe.run(main) def init_client(self, place, port): - main = fluid.Program() - with fluid.program_guard(main): + main = base.Program() + with base.program_guard(main): main.global_block().append_op( type="fetch_barrier", inputs={}, @@ -132,16 +132,16 @@ def init_client(self, place, port): Send("127.0.0.1:%d" % port, [x]) o = Recv("127.0.0.1:%d" % port, [get_var]) - exe = fluid.Executor(place) + exe = base.Executor(place) self.dist_out = exe.run(main, fetch_list=o) # o is a list def run_local(self, place): - main = fluid.Program() - with fluid.program_guard(main): + main = base.Program() + with base.program_guard(main): x = paddle.static.data(shape=[32, 32], dtype='float32', name='X') paddle.nn.initializer.Constant(value=2.3)(x, main.global_block()) o = paddle.scale(x=x, scale=10.0) - exe = fluid.Executor(place) + exe = base.Executor(place) self.local_out = exe.run(main, fetch_list=[o]) diff --git a/test/legacy_test/test_dist_transpiler.py b/test/legacy_test/test_dist_transpiler.py index 094bdc2f6cf68..89a306f84045e 100644 --- a/test/legacy_test/test_dist_transpiler.py +++ b/test/legacy_test/test_dist_transpiler.py @@ -22,7 +22,7 @@ gc.set_debug(gc.DEBUG_COLLECTABLE) import paddle -from paddle import fluid +from paddle import base class TranspilerTest(unittest.TestCase): @@ -42,8 +42,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -52,20 +52,20 @@ def net_conf(self): sgd_optimizer.minimize(avg_cost) def get_main_program(self): - main = fluid.Program() + main = base.Program() main.random_seed = 1 - with fluid.program_guard(main): + with base.program_guard(main): self.net_conf() self.origin_prog = main.clone() return main def get_trainer(self, config=None, sync_mode=True): - src = fluid.default_startup_program().clone() + src = base.default_startup_program().clone() t = self._transpiler_instance(config, sync_mode=True) trainer_main = t.get_trainer_program(wait_port=False) - trainer_startup = fluid.default_startup_program() + trainer_startup = base.default_startup_program() assert src.num_blocks == 1 assert trainer_startup.num_blocks == src.num_blocks @@ -100,10 +100,10 @@ def transpiler_test_impl(self): pass def test_transpiler(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.transpiler_test_impl() # NOTE: run gc.collect to eliminate pybind side objects to # prevent random double-deallocate when inherited in python. @@ -300,8 +300,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -356,7 +356,7 @@ def net_conf(self): input=inputs[0], is_sparse=True, size=[dict_size, embedding_size], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='emb', initializer=paddle.nn.initializer.Uniform( -init_width, init_width @@ -368,7 +368,7 @@ def net_conf(self): input=inputs[1], is_sparse=True, size=[dict_size, embedding_size], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='emb_w', initializer=paddle.nn.initializer.Constant(value=0.0), ), @@ -378,7 +378,7 @@ def net_conf(self): input=inputs[1], is_sparse=True, size=[dict_size, 1], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='emb_b', initializer=paddle.nn.initializer.Constant(value=0.0), ), @@ -391,7 +391,7 @@ def net_conf(self): input=neg_word_reshape, is_sparse=True, size=[dict_size, embedding_size], - param_attr=fluid.ParamAttr(name='emb_w', learning_rate=1.0), + param_attr=base.ParamAttr(name='emb_w', learning_rate=1.0), ) neg_emb_w_re = paddle.reshape( @@ -402,7 +402,7 @@ def net_conf(self): input=neg_word_reshape, is_sparse=True, size=[dict_size, 1], - param_attr=fluid.ParamAttr(name='emb_b', learning_rate=1.0), + param_attr=base.ParamAttr(name='emb_b', learning_rate=1.0), ) neg_emb_b_vec = paddle.reshape(neg_emb_b, shape=[-1, neg_num]) @@ -470,8 +470,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -533,10 +533,10 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='fc_w', regularizer=paddle.regularizer.L2Decay() ), - bias_attr=fluid.ParamAttr(name='fc_b'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -571,8 +571,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -646,7 +646,7 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), + weight_attr=base.ParamAttr(name='fc_w'), bias_attr=False, ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') @@ -701,8 +701,8 @@ def emb_pool(ids, table_name, is_distributed): predict = paddle.static.nn.fc( x=fc0, size=2, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') @@ -1088,8 +1088,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -1120,8 +1120,8 @@ def net_conf(self): y_predict = paddle.static.nn.fc( x, size=1000, - weight_attr=fluid.ParamAttr(name='fc_w'), - bias_attr=fluid.ParamAttr(name='fc_b'), + weight_attr=base.ParamAttr(name='fc_w'), + bias_attr=base.ParamAttr(name='fc_b'), ) y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) @@ -1178,10 +1178,10 @@ def transpiler_test_impl(self): class TestNCCL2Transpile(TranspilerTest): def test_nccl2_transpile(self): - if fluid.core.is_compiled_with_cuda(): # test nccl2 only with cuda - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + if base.core.is_compiled_with_cuda(): # test nccl2 only with cuda + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): self.net_conf() config = paddle.distributed.transpiler.DistributeTranspilerConfig() @@ -1290,7 +1290,7 @@ def network_with_table(self, is_sparse, is_distributed): label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") w_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[num_total_classes, 10], @@ -1300,7 +1300,7 @@ def network_with_table(self, is_sparse, is_distributed): ) ) b_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[num_total_classes, 1], @@ -1367,7 +1367,7 @@ def network_with_table(self, is_sparse, is_distributed): name='path_code', shape=[-1, 3], dtype='int64' ) w_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[num_total_classes, 10], @@ -1377,7 +1377,7 @@ def network_with_table(self, is_sparse, is_distributed): ) ) b_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[3, 1], @@ -1391,7 +1391,7 @@ def network_with_table(self, is_sparse, is_distributed): input=input, is_sparse=is_sparse, size=[3, 3], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal( scale=1 / math.sqrt(num_total_classes) ) diff --git a/test/legacy_test/test_dlpack.py b/test/legacy_test/test_dlpack.py index d908b316c065b..dbf9505cebc3a 100644 --- a/test/legacy_test/test_dlpack.py +++ b/test/legacy_test/test_dlpack.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDLPack(unittest.TestCase): @@ -29,7 +29,7 @@ def test_dlpack_dygraph(self): out_from_dlpack = paddle.utils.dlpack.from_dlpack(dlpack) if paddle.in_dynamic_mode(): self.assertTrue( - isinstance(out_from_dlpack, paddle.fluid.core.eager.Tensor) + isinstance(out_from_dlpack, paddle.base.core.eager.Tensor) ) else: self.assertTrue(isinstance(out_from_dlpack, paddle.Tensor)) @@ -48,14 +48,14 @@ def test_dlpack_tensor_larger_than_2dim(self): def test_dlpack_static(self): paddle.enable_static() - tensor = fluid.create_lod_tensor( + tensor = base.create_lod_tensor( np.array([[1], [2], [3], [4]]).astype('int'), [[1, 3]], - fluid.CPUPlace(), + base.CPUPlace(), ) dlpack = paddle.utils.dlpack.to_dlpack(tensor) out_from_dlpack = paddle.utils.dlpack.from_dlpack(dlpack) - self.assertTrue(isinstance(out_from_dlpack, fluid.core.Tensor)) + self.assertTrue(isinstance(out_from_dlpack, base.core.Tensor)) np.testing.assert_array_equal( np.array(out_from_dlpack), np.array([[1], [2], [3], [4]]).astype('int'), @@ -63,14 +63,14 @@ def test_dlpack_static(self): # when build with cuda if core.is_compiled_with_cuda(): - gtensor = fluid.create_lod_tensor( + gtensor = base.create_lod_tensor( np.array([[1], [2], [3], [4]]).astype('int'), [[1, 3]], - fluid.CUDAPlace(0), + base.CUDAPlace(0), ) gdlpack = paddle.utils.dlpack.to_dlpack(gtensor) gout_from_dlpack = paddle.utils.dlpack.from_dlpack(gdlpack) - self.assertTrue(isinstance(gout_from_dlpack, fluid.core.Tensor)) + self.assertTrue(isinstance(gout_from_dlpack, base.core.Tensor)) np.testing.assert_array_equal( np.array(gout_from_dlpack), np.array([[1], [2], [3], [4]]).astype('int'), diff --git a/test/legacy_test/test_dot_op.py b/test/legacy_test/test_dot_op.py index a8b7d4dd53902..2fdaf3dd1598b 100644 --- a/test/legacy_test/test_dot_op.py +++ b/test/legacy_test/test_dot_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class DotOp(OpTest): @@ -30,8 +30,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {} @@ -164,17 +164,17 @@ def test_errors(self): class TestDygraph(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(): - x1 = fluid.dygraph.to_variable(np.array([1, 3]).astype(np.float32)) - y1 = fluid.dygraph.to_variable(np.array([2, 5]).astype(np.float32)) + with base.dygraph.guard(): + x1 = base.dygraph.to_variable(np.array([1, 3]).astype(np.float32)) + y1 = base.dygraph.to_variable(np.array([2, 5]).astype(np.float32)) np.testing.assert_allclose( paddle.dot(x1, y1).numpy(), np.array([17]), rtol=1e-05 ) - x1 = fluid.dygraph.to_variable( + x1 = base.dygraph.to_variable( np.array([[1, 3], [3, 5]]).astype(np.float32) ) - y1 = fluid.dygraph.to_variable( + y1 = base.dygraph.to_variable( np.array([[2, 5], [6, 8]]).astype(np.float32) ) np.testing.assert_array_equal( @@ -225,8 +225,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {} diff --git a/test/legacy_test/test_downpoursgd.py b/test/legacy_test/test_downpoursgd.py index 5bb65133b98a1..c2ae5f54ed4a0 100644 --- a/test/legacy_test/test_downpoursgd.py +++ b/test/legacy_test/test_downpoursgd.py @@ -21,8 +21,8 @@ import paddle import paddle.incubate.distributed.fleet.parameter_server.pslib.ps_pb2 as pslib -from paddle import fluid -from paddle.fluid.trainer_factory import TrainerFactory +from paddle import base +from paddle.base.trainer_factory import TrainerFactory from paddle.incubate.distributed.fleet.parameter_server.pslib.node import ( DownpourServer, DownpourWorker, @@ -67,11 +67,11 @@ def test_device_work_use_cvm(self): with open(f"{cache_path}/fleet_desc.prototxt") as f: text_format.Merge(f.read(), ps_param) fleet_desc = ps_param - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) opt_info = {} - main_program = fluid.default_main_program() + main_program = base.default_main_program() program_id = str(id(avg_cost.block.program)) program_configs = {} program_configs[program_id] = { @@ -131,11 +131,11 @@ def test_device_work(self): with open(f"{cache_path}/fleet_desc.prototxt") as f: text_format.Merge(f.read(), ps_param) fleet_desc = ps_param - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) opt_info = {} - main_program = fluid.default_main_program() + main_program = base.default_main_program() program_id = str(id(avg_cost.block.program)) program_configs = {} program_configs[program_id] = { @@ -193,11 +193,11 @@ def test_downpour_opt_work(self): with open(f"{cache_path}/fleet_desc.prototxt") as f: text_format.Merge(f.read(), ps_param) fleet_desc = ps_param - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) opt_info = {} - main_program = fluid.default_main_program() + main_program = base.default_main_program() program_id = str(id(avg_cost.block.program)) program_configs = {} program_configs[program_id] = { diff --git a/test/legacy_test/test_dropout_nd_op.py b/test/legacy_test/test_dropout_nd_op.py index 070e0f102a8d6..003977fe7ebe2 100644 --- a/test/legacy_test/test_dropout_nd_op.py +++ b/test/legacy_test/test_dropout_nd_op.py @@ -18,11 +18,11 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid import core -from paddle.fluid.data_feeder import check_variable_and_dtype -from paddle.fluid.framework import in_dygraph_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle import _legacy_C_ops, base +from paddle.base import core +from paddle.base.data_feeder import check_variable_and_dtype +from paddle.base.framework import in_dygraph_mode +from paddle.base.layer_helper import LayerHelper from paddle.static import default_main_program @@ -178,14 +178,14 @@ class TestDropoutNdAPI(unittest.TestCase): def setUp(self): paddle.seed(123) np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): paddle.disable_static() for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np = np.random.random([4, 32, 16]).astype("float32") input = paddle.to_tensor(in_np) dropout_1 = paddle.incubate.nn.FusedDropout(p=0.0, axis=[0, 1]) diff --git a/test/legacy_test/test_dropout_op.py b/test/legacy_test/test_dropout_op.py index 088e9ce483014..dbfbfca070c31 100644 --- a/test/legacy_test/test_dropout_op.py +++ b/test/legacy_test/test_dropout_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import _C_ops, fluid, static -from paddle.fluid import Program, core, program_guard +from paddle import _C_ops, base, static +from paddle.base import Program, core, program_guard from paddle.incubate.autograd import primapi @@ -336,7 +336,7 @@ def setUp(self): x = np.random.random(self.input_size).astype("float16") out = x * (1.0 - self.prob) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.attrs = { 'dropout_prob': self.prob, 'fix_seed': self.fix_seed, @@ -457,10 +457,10 @@ def test_seed_cpu_place(self): attrs={'dropout_prob': 0.0}, outputs={'Out': x_out_var, 'Mask': mask_var}, ) - place = fluid.CPUPlace() + place = base.CPUPlace() if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) x_out, mask_out = exe.run( main_program, feed={}, @@ -477,8 +477,8 @@ def test_errors(self): def test_Variable(): # the input of dropout must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.nn.functional.dropout(x1, p=0.5) @@ -498,13 +498,13 @@ def test_dtype(): class TestDropoutFAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[-1, -1], dtype="float32" ) @@ -571,7 +571,7 @@ def check_static_result(self, place): res_np = in_np res_np2 = np.zeros_like(in_np) - exe = fluid.Executor(place) + exe = base.Executor(place) res_list = [ res1, res2, @@ -587,19 +587,19 @@ def check_static_result(self, place): ] for res in res_list: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res], ) np.testing.assert_allclose(fetches[0], res_np, rtol=1e-05) fetches2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res10], ) np.testing.assert_allclose(fetches2[0], res_np2, rtol=1e-05) fetches3 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res13], ) @@ -610,11 +610,11 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np = np.random.random([40, 40]).astype("float32") res_np = in_np res_np2 = np.zeros_like(in_np) - input = fluid.dygraph.to_variable(in_np) + input = base.dygraph.to_variable(in_np) res1 = paddle.nn.functional.dropout( x=input, p=0.0, training=False @@ -722,8 +722,8 @@ def test_errors(self): def test_Variable(): # the input of dropout must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.nn.functional.dropout(x1, p=0.5) @@ -731,8 +731,8 @@ def test_Variable(): def test_Variable2(): # the input of dropout must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.nn.functional.dropout(x1, p=0.5, axis=0) @@ -815,16 +815,16 @@ def test_axis_len(): class TestDropoutCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([40, 40]).astype("float32") result_np = input_np - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) m = paddle.nn.Dropout(p=0.0) m.eval() result = m(input) @@ -836,13 +836,13 @@ def test_dygraph(self): class TestDropout2DFAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 4, 5], dtype="float32" ) @@ -856,11 +856,11 @@ def check_static_result(self, place): in_np = np.random.random([2, 3, 4, 5]).astype("float32") res_np = in_np - exe = fluid.Executor(place) + exe = base.Executor(place) res_list = [res1, res2] for res in res_list: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res], ) @@ -872,10 +872,10 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np = np.random.random([2, 3, 4, 5]).astype("float32") res_np = in_np - input = fluid.dygraph.to_variable(in_np) + input = base.dygraph.to_variable(in_np) res1 = paddle.nn.functional.dropout2d( x=input, p=0.0, training=False, data_format='NCHW' @@ -916,16 +916,16 @@ def test_dataformat(): class TestDropout2DCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 4, 5]).astype("float32") result_np = input_np - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) m = paddle.nn.Dropout2D(p=0.0) m.eval() result = m(input) @@ -934,7 +934,7 @@ def test_dygraph(self): ) def test_static_fp16_with_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) paddle.enable_static() with paddle.static.program_guard( @@ -961,13 +961,13 @@ def test_static_fp16_with_gpu(self): class TestDropout3DFAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 4, 5, 6], dtype="float32" ) @@ -981,11 +981,11 @@ def check_static_result(self, place): in_np = np.random.random([2, 3, 4, 5, 6]).astype("float32") res_np = in_np - exe = fluid.Executor(place) + exe = base.Executor(place) res_list = [res1, res2] for res in res_list: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res], ) @@ -997,10 +997,10 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np = np.random.random([2, 3, 4, 5, 6]).astype("float32") res_np = in_np - input = fluid.dygraph.to_variable(in_np) + input = base.dygraph.to_variable(in_np) res1 = paddle.nn.functional.dropout3d( x=input, p=0.0, training=False, data_format='NCDHW' @@ -1041,16 +1041,16 @@ def test_dataformat(): class TestDropout3DCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 4, 5, 6]).astype("float32") result_np = input_np - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) m = paddle.nn.Dropout3D(p=0.0) m.eval() result = m(input) @@ -1062,12 +1062,12 @@ def test_dygraph(self): class TestAlphaDropoutFAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[40, 40], dtype="float32" ) @@ -1081,17 +1081,17 @@ def check_static_result(self, place): res_np = in_np res_np3 = np.zeros_like(in_np) - exe = fluid.Executor(place) + exe = base.Executor(place) res_list = [res1, res2] for res in res_list: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res], ) np.testing.assert_allclose(fetches[0], res_np, rtol=1e-05) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res3], ) @@ -1103,11 +1103,11 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np = np.random.random([40, 40]).astype("float32") res_np = in_np res_np3 = np.zeros_like(in_np) - input = fluid.dygraph.to_variable(in_np) + input = base.dygraph.to_variable(in_np) res1 = paddle.nn.functional.alpha_dropout(x=input, p=0.0) res2 = paddle.nn.functional.alpha_dropout( @@ -1127,8 +1127,8 @@ def test_errors(self): def test_Variable(): # the input of dropout must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.nn.functional.alpha_dropout(x1, p=0.5) @@ -1165,16 +1165,16 @@ def test_pvalue(): class TestAlphaDropoutCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([40, 40]).astype("float32") result_np = input_np - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) m = paddle.nn.AlphaDropout(p=0.0) m.eval() result = m(input) @@ -1183,7 +1183,7 @@ def test_dygraph(self): ) def test_static_fp16_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -1260,9 +1260,9 @@ def test_static(self): class TestDropoutBackward(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def cal_grad_upscale_train(self, mask, prob): return mask.astype("float32") / (1 - prob) @@ -1272,7 +1272,7 @@ def cal_grad_downscale_in_infer(self, mask): def test_backward_downscale_in_infer(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input = paddle.uniform([40, 40], dtype="float32") input.stop_gradient = False out, mask = _C_ops.dropout( @@ -1287,7 +1287,7 @@ def test_backward_downscale_in_infer(self): def test_backward_upscale_train(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): prob = 0.5 input = paddle.uniform([40, 40], dtype="float32") input.stop_gradient = False @@ -1304,7 +1304,7 @@ def test_backward_upscale_train(self): def test_backward_upscale_train_2(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): prob = 0.3 input = paddle.uniform([40, 40], dtype="float32") input.stop_gradient = False @@ -1356,7 +1356,7 @@ def run_static(self, x): def run_dygraph(self, x): paddle.seed(2022) - with fluid.dygraph.guard(self.place): + with base.dygraph.guard(self.place): out = self.api_case(paddle.to_tensor(x)) return out @@ -1658,9 +1658,9 @@ def setUp(self): def get_eager_desire(self, place): paddle.disable_static() paddle.seed(self.seed) - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") core.set_prim_eager_enabled(False) input_ = paddle.to_tensor( @@ -1682,7 +1682,7 @@ def test_static_comp(self): fwd_actual = [] rev_actual = [] mps = [] - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): for place in self.places: paddle.seed(self.seed) mp, sp = paddle.static.Program(), paddle.static.Program() @@ -1738,9 +1738,9 @@ def test_jit_comp(self): rev_actual = [] paddle.disable_static() for place in self.places: - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") paddle.seed(self.seed) input_ = paddle.to_tensor( @@ -1779,7 +1779,7 @@ def test_jit_comp_with_cinn(self): rev_actual = [] paddle.disable_static() for place in self.places: - if not isinstance(place, fluid.CUDAPlace): + if not isinstance(place, base.CUDAPlace): continue paddle.set_device("gpu") paddle.seed(self.seed) @@ -1802,7 +1802,7 @@ def test_jit_comp_with_cinn(self): rev_actual.append(grad[0].numpy()) i = 0 for place in self.places: - if not isinstance(self.places[i], fluid.CUDAPlace): + if not isinstance(self.places[i], base.CUDAPlace): continue np.testing.assert_allclose( self.fwd_desire[i].sum(), diff --git a/test/legacy_test/test_dygraph_mnist_fp16.py b/test/legacy_test/test_dygraph_mnist_fp16.py index 6b95be0a5404d..0e2fb6570376b 100644 --- a/test/legacy_test/test_dygraph_mnist_fp16.py +++ b/test/legacy_test/test_dygraph_mnist_fp16.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn import Linear @@ -120,14 +120,14 @@ def forward(self, inputs, label): class TestMnist(unittest.TestCase): def func_mnist_fp16(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): return x = np.random.randn(1, 3, 224, 224).astype("float32") y = np.random.randint(10, size=[1, 1], dtype="int64") - with fluid.dygraph.guard(fluid.CUDAPlace(0)): + with base.dygraph.guard(base.CUDAPlace(0)): model = MNIST(dtype="float32") - x = fluid.dygraph.to_variable(x) - y = fluid.dygraph.to_variable(y) + x = base.dygraph.to_variable(x) + y = base.dygraph.to_variable(y) # using amp.auto_cast because paddle.nn.Conv2D doesn't suppport setting dtype with paddle.amp.auto_cast(dtype='float16'): diff --git a/test/legacy_test/test_dygraph_multi_forward.py b/test/legacy_test/test_dygraph_multi_forward.py index afcfe12c30f53..42729091d93b3 100644 --- a/test/legacy_test/test_dygraph_multi_forward.py +++ b/test/legacy_test/test_dygraph_multi_forward.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear SEED = 123123111 @@ -108,7 +108,7 @@ class TestDygraphMultiForward(unittest.TestCase): def test_mnist_forward_float32(self): epoch_num = 1 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) mnist = MNIST() @@ -151,10 +151,10 @@ def test_mnist_forward_float32(self): with new_program_scope(): paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) mnist = MNIST() @@ -182,7 +182,7 @@ def test_mnist_forward_float32(self): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -202,7 +202,7 @@ def test_mnist_forward_float32(self): fetch_list = [avg_loss.name] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_dygraph_weight_norm.py b/test/legacy_test/test_dygraph_weight_norm.py index 433f31d19e873..c8ac9db557e95 100644 --- a/test/legacy_test/test_dygraph_weight_norm.py +++ b/test/legacy_test/test_dygraph_weight_norm.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn.utils import remove_weight_norm, weight_norm @@ -121,7 +121,7 @@ def weight_normalize(self, w, dim=None): return g, v def test_check_output(self): - fluid.enable_imperative() + base.enable_imperative() linear = paddle.nn.Conv2D(2, 3, 3) before_weight = linear.weight.numpy() if self.dim is None: @@ -132,7 +132,7 @@ def test_check_output(self): wn = weight_norm(linear, dim=self.dim) outputs = [] for name, data in self.data.items(): - output = linear(fluid.dygraph.to_variable(data)) + output = linear(base.dygraph.to_variable(data)) outputs.append(output.numpy()) after_weight = linear.weight self.actual_outputs = [linear.weight_g.numpy(), linear.weight_v.numpy()] @@ -183,7 +183,7 @@ def init_test_case(self): self.dim = None def test_check_output(self): - fluid.enable_imperative() + base.enable_imperative() linear = paddle.nn.Conv2D(2, 3, 3) before_weight = linear.weight wn = weight_norm(linear, dim=self.dim) diff --git a/test/legacy_test/test_dynamic_rnn_stop_gradient.py b/test/legacy_test/test_dynamic_rnn_stop_gradient.py index 4f5048fd48391..5e61be6117ef1 100644 --- a/test/legacy_test/test_dynamic_rnn_stop_gradient.py +++ b/test/legacy_test/test_dynamic_rnn_stop_gradient.py @@ -17,15 +17,15 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.tensor.manipulation import tensor_array_to_tensor paddle.enable_static() def build_and_run_program(place, batch_size, beam_size, stop_gradient=False): - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 np.random.seed(2) x = paddle.assign( @@ -64,7 +64,7 @@ def build_and_run_program(place, batch_size, beam_size, stop_gradient=False): loss = paddle.mean(out) opt = paddle.optimizer.Adam(0.01) opt.minimize(loss) - exe = fluid.Executor(place) + exe = base.Executor(place) data = np.random.random_integers( low=0, high=beam_size - 1, size=(batch_size, beam_size) ).astype("int64") @@ -79,8 +79,8 @@ def setUp(self): self.beam_size = 64 def run_main(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): value1 = build_and_run_program( place, self.batch_size, self.beam_size, False ) @@ -91,9 +91,9 @@ def run_main(self, place): np.testing.assert_array_equal(value1, value2) def test_check_main(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for p in places: self.run_main(p) diff --git a/test/legacy_test/test_eager_deletion_conditional_block.py b/test/legacy_test/test_eager_deletion_conditional_block.py index 998c02db814eb..eb18a3f4133a8 100644 --- a/test/legacy_test/test_eager_deletion_conditional_block.py +++ b/test/legacy_test/test_eager_deletion_conditional_block.py @@ -14,9 +14,9 @@ import unittest -from paddle import fluid +from paddle import base -fluid.core._set_eager_deletion_mode(0.0, 1.0, True) +base.core._set_eager_deletion_mode(0.0, 1.0, True) if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_eager_deletion_delete_vars.py b/test/legacy_test/test_eager_deletion_delete_vars.py index 065de1feb0455..7420e15981c27 100644 --- a/test/legacy_test/test_eager_deletion_delete_vars.py +++ b/test/legacy_test/test_eager_deletion_delete_vars.py @@ -23,11 +23,11 @@ from functools import reduce import paddle -from paddle import fluid +from paddle import base paddle.enable_static() -fluid.core._set_eager_deletion_mode(0.0, 1.0, True) +base.core._set_eager_deletion_mode(0.0, 1.0, True) def simple_fc_net(): @@ -39,7 +39,7 @@ def simple_fc_net(): hidden, size=200, activation='tanh', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -70,15 +70,15 @@ def get_persistables_and_non_persistables(prog, fetch_list): class TestExecutor(unittest.TestCase): def test_executor_main(self): - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for p in places: self.place = p - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): - with fluid.unique_name.guard(): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): + with base.unique_name.guard(): self.executor_main() def prepare_feed(self, image, label, dev_cnt=1): @@ -124,7 +124,7 @@ def assertScopeVar(self, scope, persitables, non_persistables): self.assertEqual(len(outline_np_vars), 0) def assert_gc_vars(self, program, skip_vars, non_persistable_vars): - gc_vars = fluid.core._get_eager_deletion_vars(program.desc, skip_vars) + gc_vars = base.core._get_eager_deletion_vars(program.desc, skip_vars) self.assertEqual(len(gc_vars), program.num_blocks) gc_vars = reduce(lambda x, y: x + y, gc_vars[0]) self.assertEqual(set(gc_vars), set(non_persistable_vars)) @@ -133,42 +133,42 @@ def executor_main(self): image, label, loss = simple_fc_net() loss.persistable = False persistables, non_persistables = get_persistables_and_non_persistables( - fluid.default_main_program(), [loss.name] + base.default_main_program(), [loss.name] ) print(f'Non-persistable var number {len(non_persistables)}') print(non_persistables) self.assert_gc_vars( - fluid.default_main_program(), [loss.name], non_persistables + base.default_main_program(), [loss.name], non_persistables ) - exe = fluid.Executor(self.place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(self.place) + exe.run(base.default_startup_program()) - p = fluid.core.Place() + p = base.core.Place() p.set_place(self.place) - exe = fluid.core.Executor(p) + exe = base.core.Executor(p) for _ in range(10): image_np, label_np = self.prepare_feed(image, label) - fluid.global_scope().var(image.name).get_tensor().set( + base.global_scope().var(image.name).get_tensor().set( image_np, self.place ) - fluid.global_scope().var(label.name).get_tensor().set( + base.global_scope().var(label.name).get_tensor().set( label_np, self.place ) # exe.run would not create local scope # so that we can detect whether gc clears temporary variables exe.run( - fluid.default_main_program().desc, - fluid.global_scope(), + base.default_main_program().desc, + base.global_scope(), 0, False, True, [loss.name], ) self.assertScopeVar( - fluid.global_scope(), persistables, non_persistables + base.global_scope(), persistables, non_persistables ) diff --git a/test/legacy_test/test_eager_deletion_dynamic_rnn_base.py b/test/legacy_test/test_eager_deletion_dynamic_rnn_base.py index 5716eaa8b03ea..aac329502abd6 100644 --- a/test/legacy_test/test_eager_deletion_dynamic_rnn_base.py +++ b/test/legacy_test/test_eager_deletion_dynamic_rnn_base.py @@ -21,8 +21,8 @@ from fake_reader import fake_imdb_reader import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def train(network, use_cuda, batch_size=32, pass_num=2): @@ -45,16 +45,16 @@ def train(network, use_cuda, batch_size=32, pass_num=2): optimizer = paddle.optimizer.Adagrad(learning_rate=0.2) optimizer.minimize(cost) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=[data, label], place=place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + feeder = base.DataFeeder(feed_list=[data, label], place=place) reader = feeder.feed(train_reader()) - exe = fluid.Executor(place) - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 + exe.run(base.default_startup_program()) - train_cp = fluid.default_main_program() + train_cp = base.default_main_program() fetch_list = [cost] for pass_id in range(pass_num): @@ -80,6 +80,6 @@ def test_network(self): for use_cuda in [True, False]: print(f'network: {self.net.__name__}, use_cuda: {use_cuda}') - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(core.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(core.Scope()): train(self.net, use_cuda) diff --git a/test/legacy_test/test_eager_deletion_mnist.py b/test/legacy_test/test_eager_deletion_mnist.py index e9fbd2de4961b..888f1800a0635 100644 --- a/test/legacy_test/test_eager_deletion_mnist.py +++ b/test/legacy_test/test_eager_deletion_mnist.py @@ -14,9 +14,9 @@ import unittest -from paddle import fluid +from paddle import base -fluid.core._set_eager_deletion_mode(0.0, 1.0, True) +base.core._set_eager_deletion_mode(0.0, 1.0, True) if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_eager_deletion_padding_rnn.py b/test/legacy_test/test_eager_deletion_padding_rnn.py index 874731e35d27a..0bc835a86368f 100644 --- a/test/legacy_test/test_eager_deletion_padding_rnn.py +++ b/test/legacy_test/test_eager_deletion_padding_rnn.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base.executor import Executor os.environ["CPU_NUM"] = "1" @@ -245,7 +245,7 @@ def encoder_static( size=[vocab_size, hidden_size], dtype='float32', is_sparse=False, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -325,20 +325,20 @@ def setUp(self): # The default exec_strategy used for PaddingRNN. # You can change it in set_customed_config. - self.exec_strategy = fluid.ExecutionStrategy() + self.exec_strategy = base.ExecutionStrategy() self.exec_strategy.num_threads = self.device_count self.exec_strategy.num_iteration_per_drop_scope = 100 # The default build_strategy used for PaddingRNN. # You can change it in set_customed_config. - self.build_strategy = fluid.BuildStrategy() + self.build_strategy = base.BuildStrategy() self.build_strategy.enable_inplace = True self.build_strategy.memory_optimize = False self.build_strategy.fuse_all_optimizer_ops = True # CPU executor is used for PaddingRNN default. # You can change to CUDA executor in set_customed_config. - self.exe = Executor(fluid.CPUPlace()) + self.exe = Executor(base.CPUPlace()) def set_customed_config(self): # This function will be called before training. @@ -347,10 +347,10 @@ def set_customed_config(self): def _prepare_program(self, config): paddle.seed(config.random_seed) - self.main_program = fluid.Program() - self.startup_program = fluid.Program() - with fluid.program_guard(self.main_program, self.startup_program): - with fluid.unique_name.guard(): + self.main_program = base.Program() + self.startup_program = base.Program() + with base.program_guard(self.main_program, self.startup_program): + with base.unique_name.guard(): res_vars = lm_model( config.hidden_size, config.vocab_size, diff --git a/test/legacy_test/test_eager_deletion_recurrent_op.py b/test/legacy_test/test_eager_deletion_recurrent_op.py index 7f98c7dbe387f..ef47eba277c70 100644 --- a/test/legacy_test/test_eager_deletion_recurrent_op.py +++ b/test/legacy_test/test_eager_deletion_recurrent_op.py @@ -18,13 +18,13 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.enable_static() np.random.seed(123) os.environ["CPU_NUM"] = "1" -fluid.core._set_eager_deletion_mode(0.0, 1.0, True) +base.core._set_eager_deletion_mode(0.0, 1.0, True) class RecurrentNet(paddle.nn.Layer): diff --git a/test/legacy_test/test_eager_deletion_while_op.py b/test/legacy_test/test_eager_deletion_while_op.py index 55d0e4ae15039..b909946d9704b 100644 --- a/test/legacy_test/test_eager_deletion_while_op.py +++ b/test/legacy_test/test_eager_deletion_while_op.py @@ -21,12 +21,12 @@ import numpy import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import core +from paddle.base.executor import Executor paddle.enable_static() -fluid.core._set_eager_deletion_mode(0.0, 1.0, True) +base.core._set_eager_deletion_mode(0.0, 1.0, True) class TestEagerDeletionWhileOpBase(unittest.TestCase): @@ -38,8 +38,8 @@ def test_main(self): places.append(core.CUDAPlace(0)) for p in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): self.run_main(p) def run_main(self, place): @@ -121,14 +121,14 @@ def run_main(self, place): optim.minimize(loss) gc_vars = core._get_eager_deletion_vars( - fluid.default_main_program().desc, [loss.name] + base.default_main_program().desc, [loss.name] ) self.assertEqual(len(gc_vars), 3) exe = Executor(self.place) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) - prog = fluid.default_main_program() + prog = base.default_main_program() for _ in range(5): d = [] diff --git a/test/legacy_test/test_eager_run_program.py b/test/legacy_test/test_eager_run_program.py index e110383a42658..3014b791b47e1 100644 --- a/test/legacy_test/test_eager_run_program.py +++ b/test/legacy_test/test_eager_run_program.py @@ -18,9 +18,9 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid import core -from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.framework import Variable +from paddle.base import core +from paddle.base.dygraph.base import switch_to_static_graph +from paddle.base.framework import Variable def _append_backward_desc(main_program, outs): @@ -32,7 +32,7 @@ def _append_backward_desc(main_program, outs): targets.append(program.global_block().var(out.name)) if targets: - paddle.fluid.backward.gradients(targets=targets, inputs=[]) + paddle.base.backward.gradients(targets=targets, inputs=[]) return program @@ -76,7 +76,7 @@ def _add_build_strategy_for(input_program, start_op_index, end_op_index): compiled_program._compile( core.Scope(), paddle.framework._current_expected_place() ) - ir_graph = paddle.fluid.framework.IrGraph(compiled_program._graph) + ir_graph = paddle.base.framework.IrGraph(compiled_program._graph) builded_program = ir_graph.to_program() return builded_program diff --git a/test/legacy_test/test_eager_trace_op.py b/test/legacy_test/test_eager_trace_op.py index 91e95ff04a1f4..ee3ed9bb17989 100644 --- a/test/legacy_test/test_eager_trace_op.py +++ b/test/legacy_test/test_eager_trace_op.py @@ -24,18 +24,18 @@ def test_branches(self): data = np.random.random([1, 1]).astype(np.float32) x = paddle.to_tensor(data) - paddle.fluid.framework._dygraph_tracer().trace_op( + paddle.base.framework._dygraph_tracer().trace_op( 'broadcast_tensors', {'X': [x, x], 'Out': [x, x]}, {'Out': [x, x]}, {}, ) - paddle.fluid.framework._dygraph_tracer().trace_op( + paddle.base.framework._dygraph_tracer().trace_op( 'scale', {'X': x}, {'Out': x}, {'scale': 0.5} ) scale = paddle.to_tensor(np.random.random([1]).astype(np.float32)) - paddle.fluid.framework._dygraph_tracer().trace_op( + paddle.base.framework._dygraph_tracer().trace_op( 'instance_norm', {'Scale': [scale], 'X': [x]}, {'Y': [x]}, {} ) diff --git a/test/legacy_test/test_egr_python_api.py b/test/legacy_test/test_egr_python_api.py index 10f86571af101..9082956887e2b 100644 --- a/test/legacy_test/test_egr_python_api.py +++ b/test/legacy_test/test_egr_python_api.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle.base import core +from paddle.base.framework import ( EagerParamBase, _current_expected_place, in_dygraph_mode, @@ -167,7 +167,7 @@ def constructor(self, place): self.assertEqual(egr_tensor3.stop_gradient, True) self.assertTrue( egr_tensor3.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) np.testing.assert_array_equal(egr_tensor3.numpy(), arr2) @@ -181,7 +181,7 @@ def constructor(self, place): self.assertEqual(egr_tensor4.stop_gradient, True) self.assertTrue( egr_tensor4.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) np.testing.assert_array_equal(egr_tensor4.numpy(), egr_tensor3.numpy()) @@ -233,8 +233,8 @@ def constructor(self, place): np.testing.assert_array_equal(egr_tensor9.numpy(), arr4) x = np.random.rand(3, 3).astype('float32') - t = paddle.fluid.Tensor() - t.set(x, paddle.fluid.CPUPlace()) + t = paddle.base.Tensor() + t.set(x, paddle.base.CPUPlace()) egr_tensor10 = core.eager.Tensor(t, place) self.assertEqual(egr_tensor10.persistable, False) self.assertTrue("generated_tensor" in egr_tensor10.name) @@ -259,7 +259,7 @@ def constructor(self, place): self.assertEqual(egr_tensor12.shape, [3, 3]) self.assertEqual(egr_tensor12.dtype, core.VarDesc.VarType.FP32) self.assertEqual(egr_tensor12.stop_gradient, True) - self.assertTrue(egr_tensor12.place._equals(paddle.fluid.CPUPlace())) + self.assertTrue(egr_tensor12.place._equals(paddle.base.CPUPlace())) np.testing.assert_array_equal(egr_tensor12.numpy(), x) zero_dim_param = EagerParamBase(shape=[], dtype="float32") @@ -321,7 +321,7 @@ def constructor_with_kwargs(self, place): self.assertEqual(egr_tensor0.shape, [4, 16, 16, 32]) self.assertTrue( egr_tensor0.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) self.assertEqual(egr_tensor0.dtype, core.VarDesc.VarType.FP32) @@ -507,7 +507,7 @@ def constructor_with_kwargs(self, place): self.assertEqual(egr_tensor15.stop_gradient, True) self.assertTrue( egr_tensor15.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) np.testing.assert_array_equal(egr_tensor15.numpy(), egr_tensor4.numpy()) @@ -522,7 +522,7 @@ def constructor_with_kwargs(self, place): self.assertEqual(egr_tensor16.stop_gradient, True) self.assertTrue( egr_tensor16.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) np.testing.assert_array_equal(egr_tensor16.numpy(), egr_tensor4.numpy()) @@ -568,8 +568,8 @@ def constructor_with_kwargs(self, place): # init eager tensor by framework tensor x = np.random.rand(3, 3).astype('float32') - t = paddle.fluid.Tensor() - t.set(x, paddle.fluid.CPUPlace()) + t = paddle.base.Tensor() + t.set(x, paddle.base.CPUPlace()) egr_tensor20 = core.eager.Tensor(value=t) self.assertEqual(egr_tensor20.persistable, False) self.assertTrue("generated_tensor" in egr_tensor20.name) @@ -578,7 +578,7 @@ def constructor_with_kwargs(self, place): self.assertEqual(egr_tensor20.stop_gradient, True) self.assertTrue( egr_tensor20.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) np.testing.assert_array_equal(egr_tensor20.numpy(), x) @@ -800,13 +800,13 @@ def test_global_properties(self): def test_place_guard(self): if core.is_compiled_with_cuda(): paddle.set_device("gpu:0") - with paddle.fluid.framework._dygraph_place_guard(core.CPUPlace()): + with paddle.base.framework._dygraph_place_guard(core.CPUPlace()): self.assertTrue( isinstance(_current_expected_place(), type(core.CPUPlace())) ) else: paddle.set_device("cpu") - with paddle.fluid.framework._dygraph_place_guard(core.CPUPlace()): + with paddle.base.framework._dygraph_place_guard(core.CPUPlace()): self.assertTrue( isinstance(_current_expected_place(), type(core.CPUPlace())) ) @@ -820,7 +820,7 @@ def test_value(self): self.assertEqual(egr_tensor0.shape, [4, 16, 16, 32]) self.assertTrue( egr_tensor0.place._equals( - paddle.fluid.framework._current_expected_place() + paddle.base.framework._current_expected_place() ) ) self.assertEqual(egr_tensor0.dtype, core.VarDesc.VarType.FP64) @@ -831,7 +831,7 @@ def test_value(self): ) self.assertTrue( egr_tensor0.value().get_tensor()._place(), - paddle.fluid.framework._current_expected_place(), + paddle.base.framework._current_expected_place(), ) self.assertTrue(egr_tensor0.value().get_tensor()._is_initialized()) @@ -936,13 +936,13 @@ def func_fp16_initilaizer(self): return res def func_layer_helper_base(self, value): - base = paddle.fluid.layer_helper_base.LayerHelperBase( + base = paddle.base.layer_helper_base.LayerHelperBase( "test_layer", "test_layer" ) return base.to_variable(value).numpy() def func_base_to_variable(self, value): - paddle.fluid.dygraph.base.to_variable(value) + paddle.base.dygraph.base.to_variable(value) def test_backward_with_single_tensor(self): arr4 = np.random.rand(4, 16, 16, 32).astype('float32') @@ -954,7 +954,7 @@ def test_backward_with_single_tensor(self): self.assertEqual(egr_tensor12.shape, [4, 16, 16, 32]) self.assertEqual(egr_tensor12.dtype, core.VarDesc.VarType.FP32) self.assertEqual(egr_tensor12.stop_gradient, True) - self.assertTrue(egr_tensor12.place._equals(paddle.fluid.CPUPlace())) + self.assertTrue(egr_tensor12.place._equals(paddle.base.CPUPlace())) np.testing.assert_array_equal(egr_tensor12.numpy(), arr4) np.testing.assert_array_equal(egr_tensor12.gradient(), None) egr_tensor12.stop_gradient = False diff --git a/test/legacy_test/test_egr_string_tensor_api.py b/test/legacy_test/test_egr_string_tensor_api.py index 16388ee645808..2fad942cf3e58 100644 --- a/test/legacy_test/test_egr_string_tensor_api.py +++ b/test/legacy_test/test_egr_string_tensor_api.py @@ -16,7 +16,7 @@ import numpy as np -from paddle.fluid import core +from paddle.base import core class EagerStringTensorTestCase(unittest.TestCase): diff --git a/test/legacy_test/test_eig_op.py b/test/legacy_test/test_eig_op.py index 5e804be327ec6..da28ca66bc000 100644 --- a/test/legacy_test/test_eig_op.py +++ b/test/legacy_test/test_eig_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core # cast output to complex for numpy.linalg.eig @@ -66,7 +66,7 @@ def setUp(self): self.python_api = paddle.linalg.eig self.__class__.op_type = self.op_type self.init_input() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Eigenvalues': self.out[0], 'Eigenvectors': self.out[1]} def init_input(self): @@ -243,15 +243,15 @@ def test_check_output_with_place(self): place = core.CPUPlace() input_np = np.random.random([3, 3]).astype('complex') expect_val, expect_vec = np.linalg.eig(input_np) - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[3, 3], dtype='complex' ) act_val, act_vec = paddle.linalg.eig(input) - exe = fluid.Executor(place) + exe = base.Executor(place) fetch_val, fetch_vec = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[act_val, act_vec], ) @@ -326,8 +326,8 @@ def test_check_grad(self): grad_v = np.ones(real_v.shape, test_type) grad_x = eig_backward(real_w, real_v, grad_w, grad_v) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(input_np) + with base.dygraph.guard(): + x = base.dygraph.to_variable(input_np) x.stop_gradient = False w, v = paddle.linalg.eig(x) (w.sum() + v.sum()).backward() diff --git a/test/legacy_test/test_eigvals_op.py b/test/legacy_test/test_eigvals_op.py index 8c367c31c340c..811eee672e1a5 100644 --- a/test/legacy_test/test_eigvals_op.py +++ b/test/legacy_test/test_eigvals_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core np.set_printoptions(threshold=np.inf) diff --git a/test/legacy_test/test_einsum.py b/test/legacy_test/test_einsum.py index a689c5f1e42fe..830474de90f31 100644 --- a/test/legacy_test/test_einsum.py +++ b/test/legacy_test/test_einsum.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core os.environ['FLAGS_new_einsum'] = "0" @@ -179,14 +179,12 @@ def test_forward(self): expected_result = np.einsum(self.sample["paradigm"], *operands) equation = self.sample["paradigm"] - with paddle.fluid.dygraph.guard( - self._get_place(force_to_use_cpu=False) - ): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=False)): pd_operands = [paddle.to_tensor(operand) for operand in operands] result = paddle.einsum(equation, *pd_operands) self.check_output_equal(result.numpy(), expected_result) - with paddle.fluid.dygraph.guard(self._get_place(force_to_use_cpu=True)): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=True)): pd_operands = [paddle.to_tensor(operand) for operand in operands] result = paddle.einsum(equation, *pd_operands) self.check_output_equal(result.numpy(), expected_result) @@ -348,9 +346,7 @@ def check_output_equal(self, actual, expect, rtol=1.0e-5, atol=1.0e-8): def check_output(self, eqn, *ops): expect = np.einsum(eqn, *ops) - with paddle.fluid.dygraph.guard( - self._get_place(force_to_use_cpu=False) - ): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=False)): pd_operands = [paddle.to_tensor(op) for op in ops] actual = paddle.einsum(eqn, *pd_operands) self.check_output_equal(actual.numpy(), expect) @@ -449,14 +445,14 @@ def test_large_nops(self): def test_static_graph(self): paddle.enable_static() - fluid = paddle.fluid - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + base = paddle.base + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + self.place = base.CPUPlace() + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): a = paddle.static.data( name='a', shape=[3, None, None, None], dtype='float' ) @@ -479,7 +475,7 @@ def test_static_graph(self): outs.append(paddle.einsum('...kj, ...ik', d, e)) outs.append(paddle.einsum('ijk..., ikj', c, e)) outs.append(paddle.einsum('ijk..., ikj->...ij', c, e)) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) a = np.arange(72).reshape(3, 2, 3, 4).astype('float') b = np.arange(48).reshape(2, 2, 3, 4).astype('float') diff --git a/test/legacy_test/test_einsum_op.py b/test/legacy_test/test_einsum_op.py index b69bdcf075124..8595094ee0fed 100644 --- a/test/legacy_test/test_einsum_op.py +++ b/test/legacy_test/test_einsum_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def einsum_wrapper(a, b): diff --git a/test/legacy_test/test_einsum_v2.py b/test/legacy_test/test_einsum_v2.py index 6b4bf6958b946..4ce8b7519bc8a 100644 --- a/test/legacy_test/test_einsum_v2.py +++ b/test/legacy_test/test_einsum_v2.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core os.environ['FLAGS_new_einsum'] = "1" @@ -181,14 +181,12 @@ def test_forward(self): expected_result = np.einsum(self.sample["paradigm"], *operands) equation = self.sample["paradigm"] - with paddle.fluid.dygraph.guard( - self._get_place(force_to_use_cpu=False) - ): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=False)): pd_operands = [paddle.to_tensor(operand) for operand in operands] result = paddle.einsum(equation, *pd_operands) self.check_output_equal(result.numpy(), expected_result) - with paddle.fluid.dygraph.guard(self._get_place(force_to_use_cpu=True)): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=True)): pd_operands = [paddle.to_tensor(operand) for operand in operands] result = paddle.einsum(equation, *pd_operands) self.check_output_equal(result.numpy(), expected_result) @@ -390,9 +388,7 @@ def check_output_equal(self, actual, expect, rtol=1.0e-5, atol=1.0e-8): def check_output(self, eqn, *ops): expect = np.einsum(eqn, *ops) - with paddle.fluid.dygraph.guard( - self._get_place(force_to_use_cpu=False) - ): + with paddle.base.dygraph.guard(self._get_place(force_to_use_cpu=False)): pd_operands = [paddle.to_tensor(op) for op in ops] actual = paddle.einsum(eqn, *pd_operands) self.check_output_equal(actual.numpy(), expect) @@ -471,14 +467,14 @@ def test_sums(self): def test_static_graph(self): paddle.enable_static() - fluid = paddle.fluid - if fluid.core.is_compiled_with_cuda(): - self.place = fluid.CUDAPlace(0) + base = paddle.base + if base.core.is_compiled_with_cuda(): + self.place = base.CUDAPlace(0) else: - self.place = fluid.CPUPlace() - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + self.place = base.CPUPlace() + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): a = paddle.static.data( name='a', shape=[3, None, None, None], dtype='float' ) @@ -501,7 +497,7 @@ def test_static_graph(self): outs.append(paddle.einsum('...kj, ...ik', d, e)) outs.append(paddle.einsum('ijk..., ikj', c, e)) outs.append(paddle.einsum('ijk..., ikj->...ij', c, e)) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) a = np.arange(72).reshape(3, 2, 3, 4).astype('float') b = np.arange(48).reshape(2, 2, 3, 4).astype('float') diff --git a/test/legacy_test/test_elementwise_add_op.py b/test/legacy_test/test_elementwise_add_op.py index 5eb25c977e646..8630ef593be64 100644 --- a/test/legacy_test/test_elementwise_add_op.py +++ b/test/legacy_test/test_elementwise_add_op.py @@ -21,9 +21,9 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.layer_helper import LayerHelper class TestElementwiseAddOp(OpTest): @@ -43,8 +43,8 @@ def setUp(self): self.if_enable_cinn() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} @@ -187,12 +187,8 @@ def setUp(self): self.axis = -1 self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype( - convert_float_to_uint16(self.x) - ), - 'Y': OpTest.np_dtype_to_fluid_dtype( - convert_float_to_uint16(self.y) - ), + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(self.x)), + 'Y': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(self.y)), } self.attrs = {'axis': self.axis, 'use_mkldnn': False} self.outputs = {'Out': convert_float_to_uint16(self.out)} @@ -581,7 +577,7 @@ def _executed_api(self, x, y, name=None): return paddle.add(x, y, name) def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") y = paddle.static.data(name='y', shape=[2, 3], dtype='float32') @@ -589,7 +585,7 @@ def test_name(self): self.assertEqual(('add_res' in y_1.name), True) def test_declarative(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def gen_data(): return { @@ -601,18 +597,18 @@ def gen_data(): y = paddle.static.data(name="y", shape=[3], dtype='float32') z = self._executed_api(x, y) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) z_value = exe.run(feed=gen_data(), fetch_list=[z.name]) z_expected = np.array([3.0, 8.0, 6.0]) self.assertEqual((z_value == z_expected).all(), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 4]).astype('float64') np_y = np.array([1, 5, 2]).astype('float64') - x = fluid.dygraph.to_variable(np_x) - y = fluid.dygraph.to_variable(np_y) + x = base.dygraph.to_variable(np_x) + y = base.dygraph.to_variable(np_y) z = self._executed_api(x, y) np_z = z.numpy() z_expected = np.array([3.0, 8.0, 6.0]) @@ -691,8 +687,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -877,7 +873,7 @@ def _floa32_bfloat16_or_float16_add(self, y_dtype): class TestTensorFloa32Bfloat16Add(TestTensorFloa32Bfloat16OrFloat16Add): def test_floa32_bfloat16_add(self): place = core.CUDAPlace(0) - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): self._floa32_bfloat16_or_float16_add(y_dtype=paddle.bfloat16) @@ -888,7 +884,7 @@ def test_floa32_bfloat16_add(self): class TestTensorFloa32Float16Add(TestTensorFloa32Bfloat16OrFloat16Add): def test_floa32_float16_add(self): place = core.CUDAPlace(0) - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): self._floa32_bfloat16_or_float16_add(y_dtype=paddle.float16) diff --git a/test/legacy_test/test_elementwise_div_op.py b/test/legacy_test/test_elementwise_div_op.py index 7fb7ec87e4501..8a2adb65908bb 100644 --- a/test/legacy_test/test_elementwise_div_op.py +++ b/test/legacy_test/test_elementwise_div_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def broadcast_wrapper(shape=[1, 10, 12, 1]): @@ -472,13 +472,13 @@ def test_check_gradient(self): class TestElementwiseDivBroadcast(unittest.TestCase): def test_shape_with_batch_sizes(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x_var = paddle.static.data( name='x', dtype='float32', shape=[None, 3, None, None] ) one = 2.0 out = one / x_var - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) x = np.random.uniform(0.1, 0.6, (1, 3, 32, 32)).astype("float32") (out_result,) = exe.run(feed={'x': x}, fetch_list=[out]) self.assertEqual((out_result == (2 / x)).all(), True) @@ -486,7 +486,7 @@ def test_shape_with_batch_sizes(self): class TestDivideOp(unittest.TestCase): def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") y = paddle.static.data(name='y', shape=[2, 3], dtype='float32') @@ -494,7 +494,7 @@ def test_name(self): self.assertEqual(('div_res' in y_1.name), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 4]).astype('float64') np_y = np.array([1, 5, 2]).astype('float64') x = paddle.to_tensor(np_x) @@ -513,8 +513,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} diff --git a/test/legacy_test/test_elementwise_floordiv_op.py b/test/legacy_test/test_elementwise_floordiv_op.py index 79f5c00deabed..17a5d0f2ed91a 100644 --- a/test/legacy_test/test_elementwise_floordiv_op.py +++ b/test/legacy_test/test_elementwise_floordiv_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid +from paddle import base class TestElementwiseModOp(OpTest): @@ -38,8 +38,8 @@ def setUp(self): self.init_axis() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} @@ -106,7 +106,7 @@ def device_guard(device=None): class TestFloorDivideOp(unittest.TestCase): def test_name(self): with paddle_static_guard(): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="int64") y = paddle.static.data(name='y', shape=[2, 3], dtype='int64') @@ -115,9 +115,9 @@ def test_name(self): def test_dygraph(self): paddle.disable_static() - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for p in places: for dtype in ( 'uint8', diff --git a/test/legacy_test/test_elementwise_floormod_op.py b/test/legacy_test/test_elementwise_floormod_op.py index 42082a3b7b2c8..9a3bb665f80fb 100644 --- a/test/legacy_test/test_elementwise_floormod_op.py +++ b/test/legacy_test/test_elementwise_floormod_op.py @@ -16,12 +16,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestFloorModOp(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): # mod by zero x = paddle.to_tensor([59], dtype='int32') y = paddle.to_tensor([0], dtype='int32') diff --git a/test/legacy_test/test_elementwise_gradient_op.py b/test/legacy_test/test_elementwise_gradient_op.py index 2e518a2afb389..e06ac89200a7c 100644 --- a/test/legacy_test/test_elementwise_gradient_op.py +++ b/test/legacy_test/test_elementwise_gradient_op.py @@ -16,8 +16,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestElementWiseAddOp(unittest.TestCase): @@ -45,8 +45,8 @@ def test_with_place(place): var_names = ['x', 'y', 'out', 'y@GRAD', 'x@GRAD', 'out@GRAD'] ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -83,7 +83,7 @@ def test_with_place(place): grad_var = block.desc.find_var(arg.encode("ascii")) grad_var.set_dtype(core.VarDesc.VarType.FP32) - exe = fluid.Executor(place) + exe = base.Executor(place) out = exe.run( program, feed={ diff --git a/test/legacy_test/test_elementwise_heaviside_op.py b/test/legacy_test/test_elementwise_heaviside_op.py index a034c74fb8415..5e02e6cd0a945 100644 --- a/test/legacy_test/test_elementwise_heaviside_op.py +++ b/test/legacy_test/test_elementwise_heaviside_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def Heaviside_grad(x, y, dout, astype="float16", is_bfloat16=False): diff --git a/test/legacy_test/test_elementwise_max_op.py b/test/legacy_test/test_elementwise_max_op.py index 1283cfe287232..4c94992cec075 100644 --- a/test/legacy_test/test_elementwise_max_op.py +++ b/test/legacy_test/test_elementwise_max_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core class TestElementwiseOp(OpTest): diff --git a/test/legacy_test/test_elementwise_min_op.py b/test/legacy_test/test_elementwise_min_op.py index 9ba527ef8035a..9d2e5b76b9a48 100644 --- a/test/legacy_test/test_elementwise_min_op.py +++ b/test/legacy_test/test_elementwise_min_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() @@ -344,7 +344,7 @@ def test_check_output(self): def test_check_grad_normal(self): places = self._get_places() for place in places: - if type(place) is paddle.fluid.libpaddle.CPUPlace: + if type(place) is paddle.base.libpaddle.CPUPlace: check_prim = False else: check_prim = True @@ -369,7 +369,7 @@ def test_check_grad_normal(self): def test_check_grad_ingore_x(self): places = self._get_places() for place in places: - if isinstance(place, paddle.fluid.libpaddle.CPUPlace): + if isinstance(place, paddle.base.libpaddle.CPUPlace): check_prim = False else: check_prim = True @@ -394,7 +394,7 @@ def test_check_grad_ingore_x(self): def test_check_grad_ingore_y(self): places = self._get_places() for place in places: - if isinstance(place, paddle.fluid.libpaddle.CPUPlace): + if isinstance(place, paddle.base.libpaddle.CPUPlace): check_prim = False else: check_prim = True diff --git a/test/legacy_test/test_elementwise_mod_op.py b/test/legacy_test/test_elementwise_mod_op.py index 8f34328241fef..eb3ba740ea5e2 100644 --- a/test/legacy_test/test_elementwise_mod_op.py +++ b/test/legacy_test/test_elementwise_mod_op.py @@ -23,8 +23,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestElementwiseModOp(OpTest): @@ -41,8 +41,8 @@ def setUp(self): self.init_axis() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} @@ -177,12 +177,8 @@ def setUp(self): self.init_kernel_type() self.init_axis() self.inputs = { - 'X': convert_float_to_uint16( - OpTest.np_dtype_to_fluid_dtype(self.x) - ), - 'Y': convert_float_to_uint16( - OpTest.np_dtype_to_fluid_dtype(self.y) - ), + 'X': convert_float_to_uint16(OpTest.np_dtype_to_base_dtype(self.x)), + 'Y': convert_float_to_uint16(OpTest.np_dtype_to_base_dtype(self.y)), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': convert_float_to_uint16(self.out)} @@ -217,7 +213,7 @@ def _executed_api(self, x, y, name=None): return paddle.remainder(x, y, name) def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="int64") y = paddle.static.data(name='y', shape=[2, 3], dtype='int64') @@ -225,7 +221,7 @@ def test_name(self): self.assertEqual(('div_res' in y_1.name), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 8, 7]).astype('int64') np_y = np.array([1, 5, 3, 3]).astype('int64') x = paddle.to_tensor(np_x) diff --git a/test/legacy_test/test_elementwise_mul_op.py b/test/legacy_test/test_elementwise_mul_op.py index 987d15419109c..6da50f02c163d 100644 --- a/test/legacy_test/test_elementwise_mul_op.py +++ b/test/legacy_test/test_elementwise_mul_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core class ElementwiseMulOp(OpTest): @@ -39,8 +39,8 @@ def setUp(self): self.if_enable_cinn() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} @@ -107,8 +107,8 @@ def setUp(self): self.if_enable_cinn() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis} @@ -182,12 +182,8 @@ def setUp(self): self.axis = -1 self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype( - convert_float_to_uint16(self.x) - ), - 'Y': OpTest.np_dtype_to_fluid_dtype( - convert_float_to_uint16(self.y) - ), + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(self.x)), + 'Y': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(self.y)), } self.outputs = {'Out': convert_float_to_uint16(self.out)} self.attrs = {'axis': self.axis, 'use_mkldnn': False} @@ -302,8 +298,8 @@ def init_input_attr_output(self): self.y = np.random.uniform(0.1, 1, [17, 17]).astype(self.dtype) self.out = np.multiply(self.x, self.y) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} @@ -327,8 +323,8 @@ def init_input_attr_output(self): self.y = np.random.rand(100).astype(self.dtype) self.out = self.x * self.y.reshape(100, 1, 1) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} @@ -500,8 +496,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} diff --git a/test/legacy_test/test_elementwise_nn_grad.py b/test/legacy_test/test_elementwise_nn_grad.py index 9a4baa7c526c3..2f7b7c5fa41d0 100644 --- a/test/legacy_test/test_elementwise_nn_grad.py +++ b/test/legacy_test/test_elementwise_nn_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestElementwiseMulDoubleGradCheck(unittest.TestCase): @@ -45,9 +45,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -74,9 +74,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -103,9 +103,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -132,9 +132,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -171,9 +171,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -200,9 +200,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -241,9 +241,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -271,9 +271,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -300,9 +300,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -329,9 +329,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -368,9 +368,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -397,9 +397,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_elementwise_pow_op.py b/test/legacy_test/test_elementwise_pow_op.py index 1f7566df1bb19..ce8a63355c5c7 100644 --- a/test/legacy_test/test_elementwise_pow_op.py +++ b/test/legacy_test/test_elementwise_pow_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def pow_grad(x, y, dout): @@ -217,13 +217,13 @@ def setUp(self): ).astype("int") def test_grad(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for place in places: - with fluid.dygraph.guard(place): - x = fluid.dygraph.to_variable(self.x, zero_copy=False) - y = fluid.dygraph.to_variable(self.y, zero_copy=False) + with base.dygraph.guard(place): + x = base.dygraph.to_variable(self.x, zero_copy=False) + y = base.dygraph.to_variable(self.y, zero_copy=False) x.stop_gradient = False y.stop_gradient = False res = x**y diff --git a/test/legacy_test/test_elementwise_sub_op.py b/test/legacy_test/test_elementwise_sub_op.py index a2f199fb4b21d..87abcc2b07e57 100644 --- a/test/legacy_test/test_elementwise_sub_op.py +++ b/test/legacy_test/test_elementwise_sub_op.py @@ -20,9 +20,9 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.layer_helper import LayerHelper class TestElementwiseOp(OpTest): @@ -789,8 +789,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -862,7 +862,7 @@ def _executed_api(self, x, y, name=None): return paddle.subtract(x, y, name) def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") y = paddle.static.data(name='y', shape=[2, 3], dtype=np.float32) @@ -870,7 +870,7 @@ def test_name(self): self.assertEqual(('subtract_res' in y_1.name), True) def test_declarative(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def gen_data(): return { @@ -881,18 +881,18 @@ def gen_data(): x = paddle.static.data(name="x", shape=[3], dtype=np.float32) y = paddle.static.data(name="y", shape=[3], dtype=np.float32) z = self._executed_api(x, y) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) z_value = exe.run(feed=gen_data(), fetch_list=[z.name]) z_expected = np.array([1.0, -2.0, 2.0]) self.assertEqual((z_value == z_expected).all(), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 4]).astype('float64') np_y = np.array([1, 5, 2]).astype('float64') - x = fluid.dygraph.to_variable(np_x) - y = fluid.dygraph.to_variable(np_y) + x = base.dygraph.to_variable(np_x) + y = base.dygraph.to_variable(np_y) z = self._executed_api(x, y) np_z = z.numpy(False) z_expected = np.array([1.0, -2.0, 2.0]) diff --git a/test/legacy_test/test_ema.py b/test/legacy_test/test_ema.py index 54625de715e45..acfd4479fe096 100644 --- a/test/legacy_test/test_ema.py +++ b/test/legacy_test/test_ema.py @@ -17,21 +17,21 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestExponentialMovingAverage(unittest.TestCase): def setUp(self): - self._places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self._places.append(fluid.CUDAPlace(0)) + self._places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self._places.append(base.CUDAPlace(0)) self._ema_decay = 0.999 self._param_name = "fc.weight" - self._train_program = fluid.Program() - self._startup_prog = fluid.Program() - with fluid.program_guard(self._train_program, self._startup_prog): - with fluid.unique_name.guard(): + self._train_program = base.Program() + self._startup_prog = base.Program() + with base.program_guard(self._train_program, self._startup_prog): + with base.unique_name.guard(): data = paddle.static.data( name='x', shape=[-1, 5], dtype='float32' ) @@ -40,7 +40,7 @@ def setUp(self): ) cost = paddle.mean(hidden) - self._test_program = fluid.default_main_program().clone( + self._test_program = base.default_main_program().clone( for_test=True ) @@ -53,7 +53,7 @@ def setUp(self): self._ema.update() def train(self, place): - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(self._startup_prog) params = [] @@ -61,17 +61,17 @@ def train(self, place): for batch_id in range(3): data = np.random.random(size=(10, 5)).astype('float32') tmp_param = np.array( - fluid.global_scope().find_var(self._param_name).get_tensor() + base.global_scope().find_var(self._param_name).get_tensor() ) exe.run(program=self._train_program, feed={'x': data}) tmp_param = np.array( - fluid.global_scope().find_var(self._param_name).get_tensor() + base.global_scope().find_var(self._param_name).get_tensor() ) params.append(tmp_param) with self._ema.apply(exe): final_ema = np.array( - fluid.global_scope().find_var(self._param_name).get_tensor() + base.global_scope().find_var(self._param_name).get_tensor() ) data = np.random.random(size=(10, 5)).astype('float32') exe.run(program=self._test_program, feed={'x': data}) diff --git a/test/legacy_test/test_embedding_id_stop_gradient.py b/test/legacy_test/test_embedding_id_stop_gradient.py index eef33dd44ec5f..e39d9c4674c67 100644 --- a/test/legacy_test/test_embedding_id_stop_gradient.py +++ b/test/legacy_test/test_embedding_id_stop_gradient.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestEmbeddingIdStopGradientBase(unittest.TestCase): @@ -26,9 +26,9 @@ def setUp(self): self.iteration = 10 def get_places(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) return places @@ -43,12 +43,12 @@ def run_program(self, place, stop_gradient=False): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - startup_program = fluid.Program() - main_program = fluid.Program() + startup_program = base.Program() + main_program = base.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): x_1 = paddle.static.data(name='x1', shape=[4, 1], dtype='int64') x_2 = paddle.static.data(name='x2', shape=[4, 1], dtype='int64') x = paddle.concat([x_1, x_2], axis=-1) @@ -65,7 +65,7 @@ def run_program(self, place, stop_gradient=False): optim = paddle.optimizer.SGD(learning_rate=0.001) optim.minimize(avg_cost) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) x1_data = np.random.randint(0, 9, x_1.shape).astype('int64') diff --git a/test/legacy_test/test_empty_like_op.py b/test/legacy_test/test_empty_like_op.py index 164275b1a7d83..6074e74ae30f1 100644 --- a/test/legacy_test/test_empty_like_op.py +++ b/test/legacy_test/test_empty_like_op.py @@ -18,8 +18,8 @@ from eager_op_test import convert_uint16_to_float import paddle -from paddle.fluid import core -from paddle.fluid.data_feeder import convert_dtype +from paddle.base import core +from paddle.base.data_feeder import convert_dtype from paddle.static import Program, program_guard @@ -214,7 +214,7 @@ def init_config(self): def test_static_graph(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -247,7 +247,7 @@ def init_config(self): def test_static_graph(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_empty_op.py b/test/legacy_test/test_empty_op.py index bfd3184c4bb2b..19980e55d4718 100644 --- a/test/legacy_test/test_empty_op.py +++ b/test/legacy_test/test_empty_op.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle import base +from paddle.base import core +from paddle.base.framework import convert_np_dtype_to_dtype_ # Situation 1: Attr(shape) is a list(without tensor) @@ -263,7 +263,7 @@ def test_static_graph(self): place = paddle.CPUPlace() exe = paddle.static.Executor(place) res_1, res_2, res_3, res_4, res_5, res_6 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "shape_tensor_int32": np.array([200, 3]).astype("int32"), "shape_tensor_int64": np.array([200, 3]).astype("int64"), diff --git a/test/legacy_test/test_entry_attr.py b/test/legacy_test/test_entry_attr.py index a475adca2bc5c..fc73bdf9c399a 100644 --- a/test/legacy_test/test_entry_attr.py +++ b/test/legacy_test/test_entry_attr.py @@ -18,7 +18,7 @@ import unittest -from paddle import fluid +from paddle import base from paddle.distributed import ( CountFilterEntry, ProbabilityEntry, @@ -62,11 +62,11 @@ def showclick_entry(self): self.assertEqual("show_click_entry:show:click", ss) def spaese_layer(self): - prog = fluid.Program() - scope = fluid.core.Scope() + prog = base.Program() + scope = base.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog): + with base.scope_guard(scope): + with base.program_guard(prog): input = paddle.static.data( name="dnn_data", shape=[-1, 1], dtype="int64", lod_level=1 ) @@ -76,7 +76,7 @@ def spaese_layer(self): size=[100, 10], is_test=False, entry=prob, - param_attr=fluid.ParamAttr(name="deep_embedding"), + param_attr=base.ParamAttr(name="deep_embedding"), ) pool = paddle.static.nn.sequence_lod.sequence_pool( diff --git a/test/legacy_test/test_entry_attr2.py b/test/legacy_test/test_entry_attr2.py index 358e43c088cd2..c00013bb746e8 100644 --- a/test/legacy_test/test_entry_attr2.py +++ b/test/legacy_test/test_entry_attr2.py @@ -18,16 +18,16 @@ import unittest -from paddle import fluid +from paddle import base class EntryAttrChecks(unittest.TestCase): def embedding_layer(self): - prog = fluid.Program() - scope = fluid.core.Scope() + prog = base.Program() + scope = base.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog): + with base.scope_guard(scope): + with base.program_guard(prog): input = paddle.static.data( name="dnn_data", shape=[-1, 1], dtype="int64", lod_level=1 ) @@ -36,7 +36,7 @@ def embedding_layer(self): size=[100, 10], is_sparse=True, is_distributed=True, - param_attr=fluid.ParamAttr(name="deep_embedding"), + param_attr=base.ParamAttr(name="deep_embedding"), ) pool = paddle.static.nn.sequence_lod.sequence_pool( diff --git a/test/legacy_test/test_erf_op.py b/test/legacy_test/test_erf_op.py index 23ccec74c2386..e7eb19b156a70 100644 --- a/test/legacy_test/test_erf_op.py +++ b/test/legacy_test/test_erf_op.py @@ -19,8 +19,8 @@ from scipy.special import erf import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base class TestErfOp(OpTest): @@ -65,14 +65,14 @@ def _test_case(self, place): np.testing.assert_allclose(y_ref, y_test, rtol=1e-05) def test_case(self): - with paddle.fluid.framework._static_guard(): - self._test_case(fluid.CPUPlace()) - if fluid.is_compiled_with_cuda(): - self._test_case(fluid.CUDAPlace(0)) + with paddle.base.framework._static_guard(): + self._test_case(base.CPUPlace()) + if base.is_compiled_with_cuda(): + self._test_case(base.CUDAPlace(0)) def test_name(self): - with paddle.fluid.framework._static_guard(): - with fluid.program_guard(fluid.Program()): + with paddle.base.framework._static_guard(): + with base.program_guard(base.Program()): x = paddle.static.data('x', [3, 4]) y = paddle.erf(x, name='erf') self.assertTrue('erf' in y.name) @@ -99,9 +99,9 @@ def test_check_grad(self): @unittest.skipIf( - not paddle.fluid.core.is_compiled_with_cuda() - or not paddle.fluid.core.is_bfloat16_supported( - paddle.fluid.core.CUDAPlace(0) + not paddle.base.core.is_compiled_with_cuda() + or not paddle.base.core.is_bfloat16_supported( + paddle.base.core.CUDAPlace(0) ), "core is not complied with CUDA and not support the bfloat16", ) @@ -119,11 +119,11 @@ def setUp(self): self.outputs = {'Out': convert_float_to_uint16(y_ref)} def test_check_output(self): - place = paddle.fluid.core.CUDAPlace(0) + place = paddle.base.core.CUDAPlace(0) self.check_output_with_place(place) def test_check_grad(self): - place = paddle.fluid.core.CUDAPlace(0) + place = paddle.base.core.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out', check_prim=True) diff --git a/test/legacy_test/test_erfinv_op.py b/test/legacy_test/test_erfinv_op.py index f6a51285de6e2..1a4f54230991a 100644 --- a/test/legacy_test/test_erfinv_op.py +++ b/test/legacy_test/test_erfinv_op.py @@ -23,7 +23,7 @@ from scipy.special import erfinv import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(0) diff --git a/test/legacy_test/test_error_clip.py b/test/legacy_test/test_error_clip.py index 1fba67df22677..754410aeb3b72 100644 --- a/test/legacy_test/test_error_clip.py +++ b/test/legacy_test/test_error_clip.py @@ -15,16 +15,16 @@ import sys import paddle -from paddle import fluid +from paddle import base BATCH_SIZE = 128 CLIP_MAX = 2e-6 CLIP_MIN = -1e-6 paddle.enable_static() -prog = fluid.framework.Program() +prog = base.framework.Program() -with fluid.program_guard(main_program=prog): +with base.program_guard(main_program=prog): image = paddle.static.data(name='x', shape=[-1, 784], dtype='float32') hidden1 = paddle.static.nn.fc(x=image, size=128, activation='relu') @@ -44,8 +44,8 @@ ) avg_cost_clip = prog_clip.block(0).var(avg_cost.name) -fluid.backward.append_backward(loss=avg_cost) -fluid.backward.append_backward( +base.backward.append_backward(loss=avg_cost) +base.backward.append_backward( loss=avg_cost_clip, callbacks=[paddle.nn.clip.error_clip_callback] ) @@ -60,10 +60,10 @@ batch_size=BATCH_SIZE, ) -place = fluid.CPUPlace() -exe = fluid.Executor(place) -feeder = fluid.DataFeeder(feed_list=[image, label], place=place) -exe.run(fluid.default_startup_program()) +place = base.CPUPlace() +exe = base.Executor(place) +feeder = base.DataFeeder(feed_list=[image, label], place=place) +exe.run(base.default_startup_program()) count = 0 for data in train_reader(): diff --git a/test/legacy_test/test_exception.py b/test/legacy_test/test_exception.py index 1c8e40cf94ed7..d8e998bf7f9eb 100644 --- a/test/legacy_test/test_exception.py +++ b/test/legacy_test/test_exception.py @@ -17,8 +17,8 @@ import numpy import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestException(unittest.TestCase): @@ -54,7 +54,7 @@ class TestExceptionNoCStack(unittest.TestCase): def setUp(self): paddle.enable_static() # test no C++ stack format - fluid.set_flags({'FLAGS_call_stack_level': 1}) + base.set_flags({'FLAGS_call_stack_level': 1}) def test_exception_in_static_mode(self): x = paddle.static.data(name='X', shape=[-1, 13], dtype='float32') @@ -65,26 +65,26 @@ def test_exception_in_static_mode(self): paddle.optimizer.SGD(learning_rate=0.01).minimize(avg_loss) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) x = numpy.random.random(size=(8, 12)).astype('float32') y = numpy.random.random(size=(8, 1)).astype('float32') with self.assertRaises(ValueError): exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'X': x, 'Y': y}, fetch_list=[avg_loss.name], ) def test_exception_in_dynamic_mode(self): - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): x = numpy.random.random(size=(10, 2)).astype('float32') linear = paddle.nn.Linear(1, 10) - data = fluid.dygraph.to_variable(x) + data = base.dygraph.to_variable(x) with self.assertRaises(ValueError): res = linear(data) diff --git a/test/legacy_test/test_executor_and_mul.py b/test/legacy_test/test_executor_and_mul.py index b7770c17e206c..1b577289e8718 100644 --- a/test/legacy_test/test_executor_and_mul.py +++ b/test/legacy_test/test_executor_and_mul.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.executor import Executor +from paddle.base.executor import Executor from paddle.static import data from paddle.tensor import array_write diff --git a/test/legacy_test/test_executor_and_use_program_cache.py b/test/legacy_test/test_executor_and_use_program_cache.py index f209407fcf2f2..3e6536e277e69 100644 --- a/test/legacy_test/test_executor_and_use_program_cache.py +++ b/test/legacy_test/test_executor_and_use_program_cache.py @@ -18,14 +18,14 @@ from test_eager_deletion_padding_rnn import PaddingRNNTestBase, RNNConfig import paddle -from paddle import fluid +from paddle import base class TestExecutor(unittest.TestCase): def test_mul(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): a = paddle.static.data(name='a', shape=[-1, 784], dtype='float32') b = paddle.static.data(name='b', shape=[784, 100], dtype='float32') a.desc.set_need_check_feed(False) @@ -38,7 +38,7 @@ def test_mul(self): out_np = np.dot(a_np, b_np) place = paddle.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) def _train(use_program_cache, max_iters=1): import time @@ -86,7 +86,7 @@ def train_and_save_inference_program( self, rnn_model="static", use_program_cache=True ): config = RNNConfig("test", rnn_model) - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): self.train(config, use_program_cache) paddle.static.io.save_inference_model( path_prefix="padding_rnn." + rnn_model + ".inference_model", @@ -125,7 +125,7 @@ def test_inference_output(self): ).astype("float32") for use_program_cache in [False, True]: - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): save_dirname = ( "padding_rnn." + rnn_model + ".inference_model" ) diff --git a/test/legacy_test/test_executor_check_feed.py b/test/legacy_test/test_executor_check_feed.py index 02529bce96479..78fb383885ec4 100644 --- a/test/legacy_test/test_executor_check_feed.py +++ b/test/legacy_test/test_executor_check_feed.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestExecutor(unittest.TestCase): @@ -34,13 +34,13 @@ def net(self): return paddle.to_tensor(lr), avg_cost def test_program_check_feed(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): + cpu = base.CPUPlace() + exe = base.Executor(cpu) lr, cost = self.net() exe.run(startup_program) train_data = [[1.0], [2.0], [3.0], [4.0]] @@ -55,16 +55,16 @@ def test_program_check_feed(self): ) def test_compiled_program_check_feed(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): + cpu = base.CPUPlace() + exe = base.Executor(cpu) lr, cost = self.net() exe.run(startup_program) - compiled_prog = fluid.CompiledProgram(main_program) + compiled_prog = base.CompiledProgram(main_program) train_data = [[1.0], [2.0], [3.0], [4.0]] y_true = [[2.0], [4.0], [6.0], [8.0]] a = 0 diff --git a/test/legacy_test/test_executor_feed_non_tensor.py b/test/legacy_test/test_executor_feed_non_tensor.py index d92b7370482c0..f6bec1de5d6b5 100644 --- a/test/legacy_test/test_executor_feed_non_tensor.py +++ b/test/legacy_test/test_executor_feed_non_tensor.py @@ -17,7 +17,7 @@ import numpy import paddle -from paddle import fluid +from paddle import base class TestExecutor(unittest.TestCase): @@ -36,13 +36,13 @@ def net(self): return paddle.to_tensor(lr), avg_cost def test_program_feed_float(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): + cpu = base.CPUPlace() + exe = base.Executor(cpu) lr, cost = self.net() exe.run(startup_program) train_data = numpy.array([[1.0], [2.0], [3.0], [4.0]]).astype( @@ -58,17 +58,17 @@ def test_program_feed_float(self): return_numpy=False, ) self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), fluid.core.VarDesc.VarType.FP32) + self.assertEqual(_lr._dtype(), base.core.VarDesc.VarType.FP32) self.assertEqual(type(a), float) def test_program_feed_int(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): + cpu = base.CPUPlace() + exe = base.Executor(cpu) lr, cost = self.net() exe.run(startup_program) train_data = numpy.array([[1.0], [2.0], [3.0], [4.0]]).astype( @@ -84,17 +84,17 @@ def test_program_feed_int(self): return_numpy=False, ) self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), fluid.core.VarDesc.VarType.FP32) + self.assertEqual(_lr._dtype(), base.core.VarDesc.VarType.FP32) self.assertEqual(type(a), int) def test_program_feed_list(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): + cpu = base.CPUPlace() + exe = base.Executor(cpu) lr, cost = self.net() exe.run(startup_program) train_data = [[1.0], [2.0], [3.0], [4.0]] @@ -106,20 +106,20 @@ def test_program_feed_list(self): return_numpy=False, ) self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), fluid.core.VarDesc.VarType.FP32) + self.assertEqual(_lr._dtype(), base.core.VarDesc.VarType.FP32) self.assertEqual(type(y_true), list) def test_compiled_program_feed_scalar(self): - main_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(main_program, startup_program): - with fluid.scope_guard(scope): + main_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(main_program, startup_program): + with base.scope_guard(scope): lr, cost = self.net() - cpu = fluid.CPUPlace() - exe = fluid.Executor(cpu) + cpu = base.CPUPlace() + exe = base.Executor(cpu) exe.run(startup_program) - compiled_prog = fluid.CompiledProgram(main_program) + compiled_prog = base.CompiledProgram(main_program) train_data = numpy.array([[1.0], [2.0], [3.0], [4.0]]).astype( 'float32' ) @@ -134,61 +134,61 @@ def test_compiled_program_feed_scalar(self): return_numpy=False, ) self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), fluid.core.VarDesc.VarType.FP32) + self.assertEqual(_lr._dtype(), base.core.VarDesc.VarType.FP32) self.assertEqual(type(a), float) class TestAsLodTensor(unittest.TestCase): def test_as_lodtensor_int32(self): - cpu = fluid.CPUPlace() - tensor = fluid.executor._as_lodtensor( - 1.0, cpu, fluid.core.VarDesc.VarType.INT32 + cpu = base.CPUPlace() + tensor = base.executor._as_lodtensor( + 1.0, cpu, base.core.VarDesc.VarType.INT32 ) - self.assertEqual(tensor._dtype(), fluid.core.VarDesc.VarType.INT32) + self.assertEqual(tensor._dtype(), base.core.VarDesc.VarType.INT32) def test_as_lodtensor_fp64(self): - cpu = fluid.CPUPlace() - tensor = fluid.executor._as_lodtensor( - 1, cpu, fluid.core.VarDesc.VarType.FP64 + cpu = base.CPUPlace() + tensor = base.executor._as_lodtensor( + 1, cpu, base.core.VarDesc.VarType.FP64 ) - self.assertEqual(tensor._dtype(), fluid.core.VarDesc.VarType.FP64) + self.assertEqual(tensor._dtype(), base.core.VarDesc.VarType.FP64) def test_as_lodtensor_assertion_error(self): - cpu = fluid.CPUPlace() - self.assertRaises(AssertionError, fluid.executor._as_lodtensor, 1, cpu) + cpu = base.CPUPlace() + self.assertRaises(AssertionError, base.executor._as_lodtensor, 1, cpu) def test_as_lodtensor_type_error(self): - cpu = fluid.CPUPlace() + cpu = base.CPUPlace() self.assertRaises( TypeError, - fluid.executor._as_lodtensor, + base.executor._as_lodtensor, {"a": 1}, cpu, - fluid.core.VarDesc.VarType.INT32, + base.core.VarDesc.VarType.INT32, ) def test_as_lodtensor_list(self): - cpu = fluid.CPUPlace() - tensor = fluid.executor._as_lodtensor( - [1, 2], cpu, fluid.core.VarDesc.VarType.FP64 + cpu = base.CPUPlace() + tensor = base.executor._as_lodtensor( + [1, 2], cpu, base.core.VarDesc.VarType.FP64 ) - self.assertEqual(tensor._dtype(), fluid.core.VarDesc.VarType.FP64) + self.assertEqual(tensor._dtype(), base.core.VarDesc.VarType.FP64) def test_as_lodtensor_tuple(self): - cpu = fluid.CPUPlace() - tensor = fluid.executor._as_lodtensor( - (1, 2), cpu, fluid.core.VarDesc.VarType.FP64 + cpu = base.CPUPlace() + tensor = base.executor._as_lodtensor( + (1, 2), cpu, base.core.VarDesc.VarType.FP64 ) - self.assertEqual(tensor._dtype(), fluid.core.VarDesc.VarType.FP64) + self.assertEqual(tensor._dtype(), base.core.VarDesc.VarType.FP64) def test_as_lodtensor_nested_list(self): - cpu = fluid.CPUPlace() + cpu = base.CPUPlace() self.assertRaises( TypeError, - fluid.executor._as_lodtensor, + base.executor._as_lodtensor, [{1.2, 1.2}, {1, 2}], cpu, - fluid.core.VarDesc.VarType.INT32, + base.core.VarDesc.VarType.INT32, ) diff --git a/test/legacy_test/test_executor_return_tensor_not_overwriting.py b/test/legacy_test/test_executor_return_tensor_not_overwriting.py index bcffb9ef5abfb..66fa0001ef030 100644 --- a/test/legacy_test/test_executor_return_tensor_not_overwriting.py +++ b/test/legacy_test/test_executor_return_tensor_not_overwriting.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -33,8 +33,8 @@ def calc_add_out(self, place=None, parallel=None): self.y = np.random.random((2, 5)).astype(np.float32) self.out = np.add(self.x, self.y) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.op_type = "elementwise_add" @@ -47,8 +47,8 @@ def calc_mul_out(self, place=None, parallel=None): self.y = np.random.random((5, 2)).astype(np.float32) self.out = np.dot(self.x, self.y) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.op_type = "mul" @@ -57,9 +57,9 @@ def calc_mul_out(self, place=None, parallel=None): return outs def test_executor_run_twice(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for place in places: for parallel in [True, False]: @@ -78,8 +78,8 @@ def calc_add_out(self, place=None): x = paddle.ones(shape=[3, 3], dtype='float32') y = paddle.ones(shape=[3, 3], dtype='float32') out = paddle.add(x=x, y=y) - program = fluid.default_main_program() - exe = fluid.Executor(place) + program = base.default_main_program() + exe = base.Executor(place) out = exe.run(program, fetch_list=[out], return_numpy=False) return out @@ -87,15 +87,15 @@ def calc_sub_out(self, place=None): x = paddle.ones(shape=[2, 2], dtype='float32') y = paddle.ones(shape=[2, 2], dtype='float32') out = paddle.subtract(x=x, y=y) - program = fluid.default_main_program() - exe = fluid.Executor(place) + program = base.default_main_program() + exe = base.Executor(place) out = exe.run(program, fetch_list=[out], return_numpy=False) return out def test_executor_run_twice(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for place in places: add_out = self.calc_add_out(place) diff --git a/test/legacy_test/test_expand_as_v2_op.py b/test/legacy_test/test_expand_as_v2_op.py index 68c9801acb4d8..0f9d9d3da6477 100755 --- a/test/legacy_test/test_expand_as_v2_op.py +++ b/test/legacy_test/test_expand_as_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestExpandAsBasic(OpTest): @@ -250,7 +250,7 @@ def test_check_grad(self): class TestExpandAsV2Error(unittest.TestCase): def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x1 = paddle.static.data(name='x1', shape=[-1, 4], dtype="uint8") x2 = paddle.static.data(name='x2', shape=[-1, 4], dtype="int32") self.assertRaises(TypeError, paddle.tensor.expand_as, x1, x2) @@ -274,9 +274,9 @@ def test_api(self): out_1 = paddle.expand_as(x, y=y) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": input1, "target_tensor": input2}, fetch_list=[out_1], ) diff --git a/test/legacy_test/test_expand_op.py b/test/legacy_test/test_expand_op.py index f4d6258d82025..90fb5222dfae3 100644 --- a/test/legacy_test/test_expand_op.py +++ b/test/legacy_test/test_expand_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle import fluid +from paddle import base # Situation 1: expand_times is a list(without tensor) @@ -26,7 +26,7 @@ def setUp(self): self.op_type = "expand" self.init_data() self.dtype = ( - "float32" if fluid.core.is_compiled_with_rocm() else "float64" + "float32" if base.core.is_compiled_with_rocm() else "float64" ) self.inputs = {'X': np.random.random(self.ori_shape).astype(self.dtype)} @@ -81,7 +81,7 @@ def setUp(self): self.op_type = "expand" self.init_data() self.dtype = ( - "float32" if fluid.core.is_compiled_with_rocm() else "float64" + "float32" if base.core.is_compiled_with_rocm() else "float64" ) expand_times_tensor = [] @@ -130,7 +130,7 @@ def setUp(self): self.op_type = "expand" self.init_data() self.dtype = ( - "float32" if fluid.core.is_compiled_with_rocm() else "float64" + "float32" if base.core.is_compiled_with_rocm() else "float64" ) self.inputs = { diff --git a/test/legacy_test/test_expand_v2_op.py b/test/legacy_test/test_expand_v2_op.py index 9b9f6b631a2bf..9352b34f69aa0 100644 --- a/test/legacy_test/test_expand_v2_op.py +++ b/test/legacy_test/test_expand_v2_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard # Situation 1: shape is a list(without tensor) @@ -278,8 +278,8 @@ def test_check_grad(self): class TestExpandV2Error(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) shape = [2, 2] self.assertRaises(TypeError, paddle.tensor.expand, x1, shape) @@ -307,11 +307,11 @@ def test_api(self): out_2 = paddle.expand(x, shape=[positive_2, 14]) out_3 = paddle.expand(x, shape=expand_shape) - g0 = fluid.backward.calc_gradient(out_2, x) + g0 = base.backward.calc_gradient(out_2, x) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": input, "expand_shape": np.array([12, 14]).astype("int32"), @@ -340,7 +340,7 @@ def test_shape_with_var(self): # Test python Dygraph API class TestExpandV2DygraphAPI(unittest.TestCase): def test_expand_times_is_tensor(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(1) a = paddle.rand([2, 5]) expand_1 = paddle.expand(a, shape=[2, 5]) @@ -373,9 +373,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -404,9 +404,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_exponential_op.py b/test/legacy_test/test_exponential_op.py index 2974e2d4e513b..4debe069b8235 100644 --- a/test/legacy_test/test_exponential_op.py +++ b/test/legacy_test/test_exponential_op.py @@ -22,7 +22,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core class TestExponentialOp1(OpTest): diff --git a/test/legacy_test/test_eye_op.py b/test/legacy_test/test_eye_op.py index b7ef848b42fad..a0340dc2c5f56 100644 --- a/test/legacy_test/test_eye_op.py +++ b/test/legacy_test/test_eye_op.py @@ -20,9 +20,9 @@ from test_attribute_var import UnittestBase import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core, framework +from paddle.base.framework import Program, program_guard class TestEyeOp(OpTest): @@ -92,7 +92,7 @@ class API_TestTensorEye(unittest.TestCase): def test_out(self): with paddle.static.program_guard(paddle.static.Program()): data = paddle.eye(10) - place = fluid.CPUPlace() + place = base.CPUPlace() exe = paddle.static.Executor(place) (result,) = exe.run(fetch_list=[data]) expected_result = np.eye(10, dtype="float32") diff --git a/test/legacy_test/test_fake_dequantize_op.py b/test/legacy_test/test_fake_dequantize_op.py index a7a5801a4a06e..d47ba3fd91b55 100644 --- a/test/legacy_test/test_fake_dequantize_op.py +++ b/test/legacy_test/test_fake_dequantize_op.py @@ -319,11 +319,11 @@ def setUp(self): def _get_places(self): import paddle - from paddle.fluid import core + from paddle.base import core if core.is_compiled_with_cuda(): - place = paddle.fluid.core.CUDAPlace(0) - if paddle.fluid.core.is_float16_supported(place): + place = paddle.base.core.CUDAPlace(0) + if paddle.base.core.is_float16_supported(place): return [place] else: return [] diff --git a/test/legacy_test/test_fake_init_op.py b/test/legacy_test/test_fake_init_op.py index 30b934a7c93ce..4a8fe0b4ac997 100644 --- a/test/legacy_test/test_fake_init_op.py +++ b/test/legacy_test/test_fake_init_op.py @@ -16,7 +16,7 @@ from op import Operator -from paddle.fluid import core +from paddle.base import core class TestFakeInitOpSelectedRows(unittest.TestCase): diff --git a/test/legacy_test/test_fc_op.py b/test/legacy_test/test_fc_op.py index 44b8e2f599d8e..a59720e9145c8 100644 --- a/test/legacy_test/test_fc_op.py +++ b/test/legacy_test/test_fc_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard SEED = 2020 @@ -158,11 +158,11 @@ def run_program(num_flatten_dims): ) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe = fluid.Executor(place=place) + exe = base.Executor(place=place) exe.run(startup_program) out = exe.run(main_program, feed={"x": input}, fetch_list=[out]) return out diff --git a/test/legacy_test/test_feed_data_check_shape_type.py b/test/legacy_test/test_feed_data_check_shape_type.py index c8a85e0556f1a..54607fb2575e4 100644 --- a/test/legacy_test/test_feed_data_check_shape_type.py +++ b/test/legacy_test/test_feed_data_check_shape_type.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core os.environ['CPU_NUM'] = str(4) np.random.seed(123) @@ -203,16 +203,16 @@ def _test_feed_lod_tensor(self, use_cuda): feed_in_data = np.random.uniform(size=[sum_length, 3, 4, 5]).astype( np.float32 ) - feed_data_tensor = fluid.LoDTensor() - feed_data_tensor.set(feed_in_data, fluid.CPUPlace()) + feed_data_tensor = base.LoDTensor() + feed_data_tensor.set(feed_in_data, base.CPUPlace()) feed_data_tensor.set_recursive_sequence_lengths(sequence_lengths) label_size = [device_count, 1] - feed_label_tensor = fluid.LoDTensor() + feed_label_tensor = base.LoDTensor() feed_label = np.random.randint( low=0, high=self.class_num, size=[sum_length, 1] ).astype(np.int64) - feed_label_tensor.set(feed_label, fluid.CPUPlace()) + feed_label_tensor.set(feed_label, base.CPUPlace()) feed_label_tensor.set_recursive_sequence_lengths(sequence_lengths) self._feed_data_in_executor( @@ -231,17 +231,17 @@ def _feed_data_in_executor( feed_label, use_cuda, ): - startup_program = fluid.Program() - main_program = fluid.Program() + startup_program = base.Program() + main_program = base.Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): in_data, label, loss = self._simple_fc_net( in_size, label_size, self.class_num, self.hidden_sizes ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) train_program = main_program diff --git a/test/legacy_test/test_feed_fetch_method.py b/test/legacy_test/test_feed_fetch_method.py index 1ef4946b217f0..6f72928a05812 100644 --- a/test/legacy_test/test_feed_fetch_method.py +++ b/test/legacy_test/test_feed_fetch_method.py @@ -16,7 +16,7 @@ import numpy as np -from paddle.fluid import core +from paddle.base import core class TestFeedFetch(unittest.TestCase): diff --git a/test/legacy_test/test_fetch_handler.py b/test/legacy_test/test_fetch_handler.py index 9a43432930089..1d588a5d0339b 100644 --- a/test/legacy_test/test_fetch_handler.py +++ b/test/legacy_test/test_fetch_handler.py @@ -17,9 +17,9 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import Program +from paddle import base +from paddle.base import core +from paddle.base.framework import Program class TestFetchHandler(unittest.TestCase): @@ -35,26 +35,24 @@ def test_fetch_handler(self): var_emb = block.create_var(name='emb', type=core.VarDesc.VarType.FP32) var_emb3 = block.create_var(name='emb3', type=core.VarDesc.VarType.FP32) - class FH(fluid.executor.FetchHandler): + class FH(base.executor.FetchHandler): def handler(self, fetch_dict): assert len(fetch_dict) == 1 table_var = scope.var('emb').get_tensor() table_var.set(table, place) fh = FH(var_dict={'emb': var_emb}, period_secs=2) - fm = fluid.trainer_factory.FetchHandlerMonitor(scope, fh) + fm = base.trainer_factory.FetchHandlerMonitor(scope, fh) fm.start() time.sleep(3) fm.stop() - default_fh = fluid.executor.FetchHandler( + default_fh = base.executor.FetchHandler( var_dict={'emb': var_emb, 'emb2': None, 'emb3': var_emb3}, period_secs=1, ) - default_fm = fluid.trainer_factory.FetchHandlerMonitor( - scope, default_fh - ) + default_fm = base.trainer_factory.FetchHandlerMonitor(scope, default_fh) default_fm.start() time.sleep(5) default_fm.stop() diff --git a/test/legacy_test/test_fetch_lod_tensor_array.py b/test/legacy_test/test_fetch_lod_tensor_array.py index 8a8288476dd43..f9abc882f3af3 100644 --- a/test/legacy_test/test_fetch_lod_tensor_array.py +++ b/test/legacy_test/test_fetch_lod_tensor_array.py @@ -18,13 +18,13 @@ from simple_nets import simple_fc_net, simple_fc_net_with_inputs import paddle -from paddle import fluid +from paddle import base class TestFetchLoDTensorArray(unittest.TestCase): def build_program(self, main_program, startup_program): - with fluid.unique_name.guard(): - with fluid.program_guard(main_program, startup_program): + with base.unique_name.guard(): + with base.program_guard(main_program, startup_program): i = paddle.zeros(shape=[1], dtype='int64') img = paddle.static.data( name='image', shape=[-1, 784], dtype='float32' @@ -46,8 +46,8 @@ def build_program(self, main_program, startup_program): return loss, array def check_network(self, use_cuda=True): - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() loss, array = self.build_program(main_program, startup_program) @@ -55,13 +55,13 @@ def check_network(self, use_cuda=True): image = np.random.normal(size=(batch_size, 784)).astype('float32') label = np.random.randint(0, 10, (batch_size, 1), dtype="int64") - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) feed_dict = {'image': image, 'label': label} - build_strategy = fluid.BuildStrategy() - binary = fluid.CompiledProgram( + build_strategy = base.BuildStrategy() + binary = base.CompiledProgram( main_program, build_strategy=build_strategy ) @@ -76,7 +76,7 @@ def check_network(self, use_cuda=True): np.testing.assert_allclose(loss_v, array_v[2], rtol=1e-05) def test_fetch_lod_tensor_array(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_network(use_cuda=True) self.check_network(use_cuda=False) diff --git a/test/legacy_test/test_fetch_var.py b/test/legacy_test/test_fetch_var.py index eb1bdcc4878c7..6978be07405de 100644 --- a/test/legacy_test/test_fetch_var.py +++ b/test/legacy_test/test_fetch_var.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestFetchVar(unittest.TestCase): @@ -30,9 +30,9 @@ def test_fetch_var(self): dtype="int32", persistable=True, name="x" ) paddle.assign(self.val, output=x) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_main_program(), feed={}, fetch_list=[]) - fetched_x = fluid.executor._fetch_var("x") + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_main_program(), feed={}, fetch_list=[]) + fetched_x = base.executor._fetch_var("x") np.testing.assert_array_equal(fetched_x, self.val) self.assertEqual(fetched_x.dtype, self.val.dtype) diff --git a/test/legacy_test/test_fill_any_like_op.py b/test/legacy_test/test_fill_any_like_op.py index 31a3fa3836323..f32f07f39d07b 100644 --- a/test/legacy_test/test_fill_any_like_op.py +++ b/test/legacy_test/test_fill_any_like_op.py @@ -19,7 +19,7 @@ import paddle import paddle.framework.dtype as dtypes -from paddle.fluid import core +from paddle.base import core def fill_any_like_wrapper(x, value, out_dtype=None, name=None): diff --git a/test/legacy_test/test_fill_any_op.py b/test/legacy_test/test_fill_any_op.py index 2137cb1e22ea4..b2325d06dfa37 100644 --- a/test/legacy_test/test_fill_any_op.py +++ b/test/legacy_test/test_fill_any_op.py @@ -73,7 +73,7 @@ def init(self): class TestFillAnyInplace(unittest.TestCase): def test_fill_any_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(np.ones((4, 2, 3)).astype(np.float32)) self.assertEqual(var.inplace_version, 0) @@ -87,7 +87,7 @@ def test_fill_any_version(self): self.assertEqual(var.inplace_version, 3) def test_fill_any_eqaul(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): tensor = paddle.to_tensor( np.random.random((20, 30)).astype(np.float32) ) @@ -98,7 +98,7 @@ def test_fill_any_eqaul(self): self.assertEqual((tensor.numpy() == target).all().item(), True) def test_backward(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = paddle.full([10, 10], -1.0, dtype='float32') x.stop_gradient = False y = 2 * x diff --git a/test/legacy_test/test_fill_constant_op.py b/test/legacy_test/test_fill_constant_op.py index 614cd29668d88..ee9d5e058c1e0 100644 --- a/test/legacy_test/test_fill_constant_op.py +++ b/test/legacy_test/test_fill_constant_op.py @@ -19,8 +19,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def fill_wrapper(shape, value=0.0): @@ -328,9 +328,9 @@ def test_api(self): shape=shape_tensor_int64, dtype=np.float32, value=val2 ) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7, res_8 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "shape_tensor_int32": np.array([1, 2]).astype("int32"), "shape_tensor_int64": np.array([1, 2]).astype("int64"), @@ -366,13 +366,13 @@ def test_api(self): class TestFillConstantImperative(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): data1 = np.array([1, 2]).astype('int32') data2 = np.array([1.1]).astype('float32') data3 = np.array([88]).astype('int32') - shape = fluid.dygraph.to_variable(data1) - val = fluid.dygraph.to_variable(data2) - value = fluid.dygraph.to_variable(data3) + shape = base.dygraph.to_variable(data1) + val = base.dygraph.to_variable(data2) + value = base.dygraph.to_variable(data3) res1 = paddle.tensor.fill_constant( shape=[1, 2], dtype='float32', value=1.1 ) @@ -399,17 +399,17 @@ def test_api(self): ) def test_nan(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = paddle.tensor.fill_constant([1], 'float32', np.nan) self.assertTrue(np.isnan(res.numpy().item(0))) def test_inf(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = paddle.tensor.fill_constant([1], 'float32', np.inf) self.assertTrue(np.isinf(res.numpy().item(0))) def test_ninf(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): res = paddle.tensor.fill_constant([1], 'float32', np.NINF) self.assertTrue(np.isinf(res.numpy().item(0))) self.assertEqual(np.NINF, res.numpy().item(0)) diff --git a/test/legacy_test/test_fill_diagonal_tensor_op.py b/test/legacy_test/test_fill_diagonal_tensor_op.py index a1217efee9a5b..be5f435b001ea 100644 --- a/test/legacy_test/test_fill_diagonal_tensor_op.py +++ b/test/legacy_test/test_fill_diagonal_tensor_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def fill_diagonal_ndarray(x, value, offset=0, dim1=0, dim2=1): diff --git a/test/legacy_test/test_fill_op.py b/test/legacy_test/test_fill_op.py index bd7dc4dccf878..88686999b2d91 100644 --- a/test/legacy_test/test_fill_op.py +++ b/test/legacy_test/test_fill_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from op import Operator -from paddle.fluid import core +from paddle.base import core class TestFillOp1(OpTest): diff --git a/test/legacy_test/test_fill_zeros_like2_op.py b/test/legacy_test/test_fill_zeros_like2_op.py index e6320ed0ab832..cbacc81d3985b 100644 --- a/test/legacy_test/test_fill_zeros_like2_op.py +++ b/test/legacy_test/test_fill_zeros_like2_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestFillZerosLike2Op(OpTest): diff --git a/test/legacy_test/test_flash_attention.py b/test/legacy_test/test_flash_attention.py index 979217d7d221a..245e51a36a0d0 100644 --- a/test/legacy_test/test_flash_attention.py +++ b/test/legacy_test/test_flash_attention.py @@ -20,8 +20,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional.flash_attention import ( flash_attention, flash_attn_unpadded, @@ -177,7 +177,7 @@ def test_unpadded(self): self.return_softmax, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) fetches_result = exe.run( feed={ "q": query.astype('float16'), @@ -293,7 +293,7 @@ def test_all(self): qs, ks, vs, self.dropout, self.causal, self.return_softmax ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) fetches_result = exe.run( feed={ "q": query.astype('float16'), diff --git a/test/legacy_test/test_flatten_contiguous_range_op.py b/test/legacy_test/test_flatten_contiguous_range_op.py index 658f03979a9c4..f8b76771b4e65 100644 --- a/test/legacy_test/test_flatten_contiguous_range_op.py +++ b/test/legacy_test/test_flatten_contiguous_range_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core class TestFlattenOp(OpTest): diff --git a/test/legacy_test/test_flatten_op.py b/test/legacy_test/test_flatten_op.py index 0803db60c73c0..d3756946b1d89 100644 --- a/test/legacy_test/test_flatten_op.py +++ b/test/legacy_test/test_flatten_op.py @@ -68,7 +68,7 @@ def init_test_case(self): class TestFlattenOpFP16(unittest.TestCase): def test_fp16_with_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): with paddle_static_guard(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( diff --git a/test/legacy_test/test_fleet.py b/test/legacy_test/test_fleet.py index 52d6df39b92ee..0e9eb0579cc98 100644 --- a/test/legacy_test/test_fleet.py +++ b/test/legacy_test/test_fleet.py @@ -33,7 +33,7 @@ def setUp(self): def test_pslib_1(self): """Test cases for pslib.""" import paddle - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) @@ -49,13 +49,13 @@ def test_pslib_1(self): os.environ["PADDLE_TRAINER_ID"] = "0" role_maker = GeneralRoleMaker() # role_maker.generate_role() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # fleet.init(role_maker) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], @@ -67,7 +67,7 @@ def test_pslib_1(self): size=[1, 1], is_sparse=True, is_distributed=True, - param_attr=fluid.ParamAttr(name="embedding"), + param_attr=base.ParamAttr(name="embedding"), ) bow = paddle.static.nn.sequence_lod.sequence_pool( input=emb, pool_type='sum' diff --git a/test/legacy_test/test_fleet_base.py b/test/legacy_test/test_fleet_base.py index 2164c8fade4d1..6a20b425f9d45 100644 --- a/test/legacy_test/test_fleet_base.py +++ b/test/legacy_test/test_fleet_base.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -161,7 +161,7 @@ def setUp(self): def test_dygraph_method(self): paddle.disable_static() value = np.arange(26).reshape(2, 13).astype("float32") - a = fluid.dygraph.to_variable(value) + a = base.dygraph.to_variable(value) layer = paddle.nn.Linear(13, 5) adam = paddle.optimizer.Adam( learning_rate=0.01, parameters=layer.parameters() @@ -216,8 +216,8 @@ def test_single_error(): # in non_distributed mode(use `python` to launch), raise error if has multi cards if ( - fluid.core.is_compiled_with_cuda() - and fluid.core.get_cuda_device_count() > 1 + base.core.is_compiled_with_cuda() + and base.core.get_cuda_device_count() > 1 ): self.assertRaises(ValueError, test_single_error) else: diff --git a/test/legacy_test/test_fleet_base_2.py b/test/legacy_test/test_fleet_base_2.py index 667de8759f6b6..836f9486f8667 100644 --- a/test/legacy_test/test_fleet_base_2.py +++ b/test/legacy_test/test_fleet_base_2.py @@ -21,7 +21,7 @@ import os -from paddle import fluid +from paddle import base class TestFleetBase(unittest.TestCase): @@ -66,11 +66,11 @@ def test_ps_minimize(self): optimizer = fleet.distributed_optimizer(optimizer, strategy=strategy) optimizer.minimize(avg_cost) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(paddle.static.default_startup_program()) - compiled_prog = fluid.compiler.CompiledProgram( - fluid.default_main_program() + compiled_prog = base.compiler.CompiledProgram( + base.default_main_program() ) temp_dir = tempfile.TemporaryDirectory() diff --git a/test/legacy_test/test_fleet_base_single.py b/test/legacy_test/test_fleet_base_single.py index 352b64b19155f..4efc7e240661f 100644 --- a/test/legacy_test/test_fleet_base_single.py +++ b/test/legacy_test/test_fleet_base_single.py @@ -24,7 +24,7 @@ import unittest import paddle -from paddle import fluid, nn +from paddle import base, nn from paddle.distributed import fleet @@ -95,12 +95,12 @@ def test_single_run_collective_minimize(self): optimizer.minimize(avg_cost) place = ( - fluid.CUDAPlace(0) - if paddle.fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(paddle.static.default_startup_program()) for i in range(10): @@ -139,13 +139,13 @@ def test_single_run_ps_minimize(self): fleet.init_server() fleet.run_server() elif fleet.is_worker(): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(paddle.static.default_startup_program()) step = 10 for i in range(step): cost_val = exe.run( - program=fluid.default_main_program(), + program=base.default_main_program(), feed=self.gen_data(), fetch_list=[avg_cost.name], ) diff --git a/test/legacy_test/test_fleet_exe_dist_model_run.py b/test/legacy_test/test_fleet_exe_dist_model_run.py index ebc790c403463..9f2d0a09da20a 100644 --- a/test/legacy_test/test_fleet_exe_dist_model_run.py +++ b/test/legacy_test/test_fleet_exe_dist_model_run.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_fleet_exe_dist_model_tensor.py b/test/legacy_test/test_fleet_exe_dist_model_tensor.py index b5c4af9fc763f..728ab1f5681f0 100644 --- a/test/legacy_test/test_fleet_exe_dist_model_tensor.py +++ b/test/legacy_test/test_fleet_exe_dist_model_tensor.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.core import DistModelDataType, DistModelTensor +from paddle.base.core import DistModelDataType, DistModelTensor paddle.enable_static() diff --git a/test/legacy_test/test_fleet_executor.py b/test/legacy_test/test_fleet_executor.py index ef13706b89c63..9402602083a9b 100644 --- a/test/legacy_test/test_fleet_executor.py +++ b/test/legacy_test/test_fleet_executor.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -44,7 +44,7 @@ def fake_fleet_opt(self): def run_fleet_executor(self, place, x_data, y_data): exe = paddle.static.Executor(place) empty_program = paddle.static.Program() - with fluid.program_guard(empty_program, empty_program): + with base.program_guard(empty_program, empty_program): x = paddle.static.data( name='x', shape=[-1] + list(x_data.shape), dtype=x_data.dtype ) @@ -82,13 +82,13 @@ def run_fleet_executor(self, place, x_data, y_data): return res def test_executor_on_single_device(self): - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): shape = (10000, 3462) x_data = np.random.rand(*shape) y_data = np.random.rand(*shape) z_data = x_data + y_data a_data = 2 * x_data + 3 * y_data - res = self.run_fleet_executor(fluid.CUDAPlace(0), x_data, y_data) + res = self.run_fleet_executor(base.CUDAPlace(0), x_data, y_data) np.testing.assert_allclose(res[0], z_data, rtol=1e-05) np.testing.assert_allclose(res[1], a_data, rtol=1e-05) diff --git a/test/legacy_test/test_fleet_executor_cond_interceptor.py b/test/legacy_test/test_fleet_executor_cond_interceptor.py index 32fd99fef514f..463a7f2983879 100644 --- a/test/legacy_test/test_fleet_executor_cond_interceptor.py +++ b/test/legacy_test/test_fleet_executor_cond_interceptor.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.fleet.fleet_executor_utils import TaskNode -from paddle.fluid import core paddle.enable_static() @@ -58,7 +58,7 @@ def test_cond_interceptor(self): ) # loop length data = paddle.static.data(name='x', shape=[1]) - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[data], capacity=num_micro_batches * 4, iterable=False ) loader.set_batch_generator( diff --git a/test/legacy_test/test_fleet_executor_multi_devices.py b/test/legacy_test/test_fleet_executor_multi_devices.py index 871dd68042cc9..6f68e0d9d2763 100644 --- a/test/legacy_test/test_fleet_executor_multi_devices.py +++ b/test/legacy_test/test_fleet_executor_multi_devices.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet paddle.enable_static() @@ -26,7 +26,7 @@ class TestFleetExecutor(unittest.TestCase): def run_fleet_executor(self, place, fleet_opt={}): exe = paddle.static.Executor(place) empty_program = paddle.static.Program() - with fluid.program_guard(empty_program, empty_program): + with base.program_guard(empty_program, empty_program): x = paddle.static.data( name='x', shape=[-1, 1], dtype=paddle.float32 ) @@ -52,7 +52,7 @@ def test_dist_executor_on_multi_devices(self): "dist_strategy": strategy.sharding_configs, "num_micro_batches": strategy.pipeline_configs["accumulate_steps"], } - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): # TODO: Distribute test case is not supported for executor can not stop pass diff --git a/test/legacy_test/test_fleet_executor_origin_scheduler.py b/test/legacy_test/test_fleet_executor_origin_scheduler.py index a017f3283b0ba..8cb5941ba4407 100644 --- a/test/legacy_test/test_fleet_executor_origin_scheduler.py +++ b/test/legacy_test/test_fleet_executor_origin_scheduler.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -44,7 +44,7 @@ def fake_fleet_opt(self): def run_fleet_executor(self, place, x_data, y_data): exe = paddle.static.Executor(place) empty_program = paddle.static.Program() - with fluid.program_guard(empty_program, empty_program): + with base.program_guard(empty_program, empty_program): x = paddle.static.data( name='x', shape=[-1] + list(x_data.shape), dtype=x_data.dtype ) @@ -82,13 +82,13 @@ def run_fleet_executor(self, place, x_data, y_data): return res def test_executor_on_single_device(self): - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): shape = (10000, 3462) x_data = np.random.rand(*shape) y_data = np.random.rand(*shape) z_data = x_data + y_data a_data = 2 * x_data + 3 * y_data - res = self.run_fleet_executor(fluid.CUDAPlace(0), x_data, y_data) + res = self.run_fleet_executor(base.CUDAPlace(0), x_data, y_data) np.testing.assert_allclose(res[0], z_data, rtol=1e-05) np.testing.assert_allclose(res[1], a_data, rtol=1e-05) diff --git a/test/legacy_test/test_fleet_executor_task_node.py b/test/legacy_test/test_fleet_executor_task_node.py index 41795aa51a936..1a2fa9be9bb46 100644 --- a/test/legacy_test/test_fleet_executor_task_node.py +++ b/test/legacy_test/test_fleet_executor_task_node.py @@ -15,8 +15,8 @@ import unittest import paddle +from paddle.base import core from paddle.distributed.fleet.fleet_executor_utils import TaskNode -from paddle.fluid import core paddle.enable_static() diff --git a/test/legacy_test/test_fleet_executor_with_task_nodes.py b/test/legacy_test/test_fleet_executor_with_task_nodes.py index 27b66a862c6ea..f7cad5f89362f 100644 --- a/test/legacy_test/test_fleet_executor_with_task_nodes.py +++ b/test/legacy_test/test_fleet_executor_with_task_nodes.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed.fleet.fleet_executor_utils import TaskNode paddle.enable_static() @@ -27,7 +27,7 @@ class TestFleetExecutor(unittest.TestCase): def run_fleet_executor(self, place, x_data, y_data): exe = paddle.static.Executor(place) empty_program = paddle.static.Program() - with fluid.program_guard(empty_program, empty_program): + with base.program_guard(empty_program, empty_program): x = paddle.static.data( name='x', shape=[-1] + list(x_data.shape), dtype=x_data.dtype ) @@ -76,13 +76,13 @@ def run_fleet_executor(self, place, x_data, y_data): return res def test_executor_on_single_device(self): - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): shape = (10000, 3462) x_data = np.random.rand(*shape) y_data = np.random.rand(*shape) z_data = x_data + y_data a_data = 2 * x_data + 3 * y_data - res = self.run_fleet_executor(fluid.CUDAPlace(0), x_data, y_data) + res = self.run_fleet_executor(base.CUDAPlace(0), x_data, y_data) np.testing.assert_allclose(res[0], z_data, rtol=1e-05) np.testing.assert_allclose(res[1], a_data, rtol=1e-05) diff --git a/test/legacy_test/test_fleet_gradient_scale.py b/test/legacy_test/test_fleet_gradient_scale.py index ef7741883c9d1..26483fee7433a 100644 --- a/test/legacy_test/test_fleet_gradient_scale.py +++ b/test/legacy_test/test_fleet_gradient_scale.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet @@ -52,8 +52,8 @@ def test_single_gpu(self): startup_program = paddle.static.Program() strategy = fleet.DistributedStrategy() strategy.gradient_scale_configs = {'scale_strategy': 'sum'} - with fluid.program_guard(main_program, startup_program): - with fluid.unique_name.guard(): + with base.program_guard(main_program, startup_program): + with base.unique_name.guard(): input_x = paddle.static.data( name="x", shape=[None, 32], dtype='float32' ) diff --git a/test/legacy_test/test_fleet_metric.py b/test/legacy_test/test_fleet_metric.py index 4adaab8e90e93..d29bb9590edbd 100644 --- a/test/legacy_test/test_fleet_metric.py +++ b/test/legacy_test/test_fleet_metric.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base.util_factory import UtilBase from paddle.distributed.fleet.metrics import metric @@ -54,7 +54,7 @@ class FakeFleet: def __init__(self): """Init.""" - self.gloo = fluid.core.Gloo() + self.gloo = base.core.Gloo() self.gloo.set_rank(0) self.gloo.set_size(1) self.gloo.set_prefix("123") @@ -76,9 +76,9 @@ def _barrier(self, comm_world="worker"): def test_metric_1(self): """Test cases for metrics.""" - train = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train, startup): + train = base.Program() + startup = base.Program() + with base.program_guard(train, startup): t = paddle.static.create_global_var( shape=[1, 1], value=1, @@ -93,10 +93,10 @@ def test_metric_1(self): persistable=True, force_cpu=True, ) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - scope = fluid.Scope() - with fluid.scope_guard(scope): + place = base.CPUPlace() + exe = base.Executor(place) + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup) metric.sum(t, scope, self.util) metric.max(t, scope, self.util) diff --git a/test/legacy_test/test_fleet_nocvm_1.py b/test/legacy_test/test_fleet_nocvm_1.py index f4014613525f5..d9962c1a27b38 100644 --- a/test/legacy_test/test_fleet_nocvm_1.py +++ b/test/legacy_test/test_fleet_nocvm_1.py @@ -33,7 +33,7 @@ def setUp(self): def test_pslib_1(self): """Test cases for pslib.""" - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) @@ -49,13 +49,13 @@ def test_pslib_1(self): os.environ["PADDLE_TRAINER_ID"] = "0" role_maker = GeneralRoleMaker() # role_maker.generate_role() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # fleet.init(role_maker) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], @@ -67,7 +67,7 @@ def test_pslib_1(self): size=[1, 1], is_sparse=True, is_distributed=True, - param_attr=fluid.ParamAttr(name="embedding"), + param_attr=base.ParamAttr(name="embedding"), ) fc = paddle.static.nn.fc(x=emb, size=1, activation=None) label = paddle.static.data( diff --git a/test/legacy_test/test_fleet_ps.py b/test/legacy_test/test_fleet_ps.py index 75cb4b595b309..941ef3776a32e 100644 --- a/test/legacy_test/test_fleet_ps.py +++ b/test/legacy_test/test_fleet_ps.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.incubate.distributed.fleet.parameter_server.ir.pserver_pass import ( _get_optimizer_input_shape, ) diff --git a/test/legacy_test/test_fleet_pyramid_hash.py b/test/legacy_test/test_fleet_pyramid_hash.py index 9c86ea8f770b9..cfd02ee72ced9 100644 --- a/test/legacy_test/test_fleet_pyramid_hash.py +++ b/test/legacy_test/test_fleet_pyramid_hash.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.incubate.distributed.fleet import role_maker from paddle.incubate.distributed.fleet.parameter_server.distribute_transpiler import ( fleet, @@ -47,11 +47,11 @@ def test_dist_geo_server_transpiler(self): black_list_len=2800, seed=3, lr=0.002, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="PyramidHash_emb_0", learning_rate=0, ), - param_attr_wl=fluid.ParamAttr( + param_attr_wl=base.ParamAttr( name="Filter", learning_rate=0, ), diff --git a/test/legacy_test/test_fleet_rolemaker.py b/test/legacy_test/test_fleet_rolemaker.py index b9af57199fc36..7caf6452bfb14 100644 --- a/test/legacy_test/test_fleet_rolemaker.py +++ b/test/legacy_test/test_fleet_rolemaker.py @@ -62,7 +62,7 @@ def test_training_role(self): def test_pslib_1(self): """Test cases for pslib.""" - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) @@ -79,13 +79,13 @@ def test_pslib_1(self): role_maker = GeneralRoleMaker() # print("init rolemaker") # role_maker.generate_role() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # fleet.init(role_maker) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], dtype="float32", lod_level=1 ) diff --git a/test/legacy_test/test_fleet_rolemaker_2.py b/test/legacy_test/test_fleet_rolemaker_2.py index a43cebd4c66c3..b7ee8ed7a3049 100644 --- a/test/legacy_test/test_fleet_rolemaker_2.py +++ b/test/legacy_test/test_fleet_rolemaker_2.py @@ -34,7 +34,7 @@ def tearDown(self): def test_pslib_2(self): """Test cases for pslib.""" - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.distribute_transpiler import ( fleet, ) @@ -52,17 +52,17 @@ def test_pslib_2(self): os.environ["PADDLE_PSERVERS_IP_PORT_LIST"] = "127.0.0.1:36002" os.environ["PADDLE_TRAINER_ID"] = "0" os.environ["PADDLE_TRAINERS_NUM"] = "1" - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) try: fleet.init(None) except: print("no mpi4py, skip test_pslib_2") return - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], dtype="float32", lod_level=1 ) diff --git a/test/legacy_test/test_fleet_rolemaker_3.py b/test/legacy_test/test_fleet_rolemaker_3.py index 88541d58b24c3..205b25c3b3911 100644 --- a/test/legacy_test/test_fleet_rolemaker_3.py +++ b/test/legacy_test/test_fleet_rolemaker_3.py @@ -33,7 +33,7 @@ def setUp(self): def test_pslib_1(self): """Test cases for pslib.""" - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) @@ -53,13 +53,13 @@ def test_pslib_1(self): http_ip_port="127.0.0.1:36003", ) # role_maker.generate_role() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # fleet.init(role_maker) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], dtype="float32", lod_level=1 ) diff --git a/test/legacy_test/test_fleet_unitaccessor.py b/test/legacy_test/test_fleet_unitaccessor.py index 8146bf90efd25..f6e33ed1ee6b3 100644 --- a/test/legacy_test/test_fleet_unitaccessor.py +++ b/test/legacy_test/test_fleet_unitaccessor.py @@ -33,7 +33,7 @@ def setUp(self): def test_pslib_1(self): """Test cases for pslib.""" - from paddle import fluid + from paddle import base from paddle.incubate.distributed.fleet.parameter_server.pslib import ( fleet, ) @@ -49,13 +49,13 @@ def test_pslib_1(self): os.environ["PADDLE_TRAINER_ID"] = "0" role_maker = GeneralRoleMaker() # role_maker.generate_role() - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # fleet.init(role_maker) - train_program = fluid.Program() - startup_program = fluid.Program() - scope = fluid.Scope() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + scope = base.Scope() + with base.program_guard(train_program, startup_program): show = paddle.static.data( name="show", shape=[-1, 1], dtype="int64", lod_level=1 ) @@ -64,7 +64,7 @@ def test_pslib_1(self): size=[1, 1], is_sparse=True, is_distributed=True, - param_attr=fluid.ParamAttr(name="embedding"), + param_attr=base.ParamAttr(name="embedding"), ) fc = paddle.static.nn.fc(x=emb, size=1, activation=None) label = paddle.static.data( diff --git a/test/legacy_test/test_flip.py b/test/legacy_test/test_flip.py index e899511b150c1..d4125049f90eb 100644 --- a/test/legacy_test/test_flip.py +++ b/test/legacy_test/test_flip.py @@ -20,17 +20,17 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestFlipOp_API(unittest.TestCase): """Test flip api.""" def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): axis = [0] input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] @@ -38,10 +38,10 @@ def test_static_graph(self): output = paddle.flip(input, axis) output = paddle.flip(output, -1) output = output.flip(0) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) res = exe.run( @@ -56,8 +56,8 @@ def test_static_graph(self): def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) - with fluid.dygraph.guard(): - inputs = fluid.dygraph.to_variable(img) + with base.dygraph.guard(): + inputs = base.dygraph.to_variable(img) ret = paddle.flip(inputs, [0]) ret = ret.flip(0) ret = paddle.flip(ret, 1) @@ -247,9 +247,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -278,9 +278,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_fmax_op.py b/test/legacy_test/test_fmax_op.py index 0271854aebb72..0ea78cb41cc5e 100644 --- a/test/legacy_test/test_fmax_op.py +++ b/test/legacy_test/test_fmax_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core class ApiFMaxTest(unittest.TestCase): diff --git a/test/legacy_test/test_fmin_op.py b/test/legacy_test/test_fmin_op.py index 1956d5b4fc433..26e2c585355ca 100644 --- a/test/legacy_test/test_fmin_op.py +++ b/test/legacy_test/test_fmin_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_fold_op.py b/test/legacy_test/test_fold_op.py index 8fdb37deadf21..62f3a05311e63 100644 --- a/test/legacy_test/test_fold_op.py +++ b/test/legacy_test/test_fold_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -108,7 +108,7 @@ def calc_fold(self): def set_data(self): self.init_data() self.calc_fold() - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.attrs = { 'kernel_sizes': self.kernel_sizes, 'paddings': self.paddings, @@ -151,13 +151,13 @@ def setUp(self): self.op_type = 'fold' self.python_api = paddle.nn.functional.fold self.set_data() - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_api(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input = paddle.to_tensor(self.x) m = paddle.nn.Fold(**self.attrs) m.eval() @@ -172,7 +172,7 @@ def test_info(self): class TestFoldOpError(unittest.TestCase): def test_errors(self): - from paddle.fluid.framework import Program, program_guard + from paddle.base.framework import Program, program_guard from paddle.nn.functional import fold with program_guard(Program(), Program()): diff --git a/test/legacy_test/test_frac_api.py b/test/legacy_test/test_frac_api.py index 9b32a0a4686cc..26bc74225e54b 100644 --- a/test/legacy_test/test_frac_api.py +++ b/test/legacy_test/test_frac_api.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def ref_frac(x): @@ -45,10 +45,10 @@ def test_api_static(self): with program_guard(Program()): input = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = paddle.frac(input) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) (res,) = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_frac(self.x_np) np.testing.assert_allclose(out_ref, res, rtol=1e-05) diff --git a/test/legacy_test/test_frame_op.py b/test/legacy_test/test_frame_op.py index 8f166f443462b..6f96d952cc63e 100644 --- a/test/legacy_test/test_frame_op.py +++ b/test/legacy_test/test_frame_op.py @@ -19,7 +19,7 @@ from numpy.lib.stride_tricks import as_strided import paddle -from paddle.fluid import core +from paddle.base import core def frame_from_librosa(x, frame_length, hop_length, axis=-1): diff --git a/test/legacy_test/test_framework_debug_str.py b/test/legacy_test/test_framework_debug_str.py index ef9f294055b46..24bc2d62c8632 100644 --- a/test/legacy_test/test_framework_debug_str.py +++ b/test/legacy_test/test_framework_debug_str.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.framework import Program +from paddle.base.framework import Program class TestDebugStringFramework(unittest.TestCase): diff --git a/test/legacy_test/test_frexp_api.py b/test/legacy_test/test_frexp_api.py index f14216ef718c6..520b6a90785a4 100644 --- a/test/legacy_test/test_frexp_api.py +++ b/test/legacy_test/test_frexp_api.py @@ -16,7 +16,7 @@ import numpy as np import paddle -import paddle.fluid +import paddle.base class TestFrexpAPI(unittest.TestCase): diff --git a/test/legacy_test/test_ftrl_op.py b/test/legacy_test/test_ftrl_op.py index 4493f428aaf68..2659eb423905f 100644 --- a/test/legacy_test/test_ftrl_op.py +++ b/test/legacy_test/test_ftrl_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from op import Operator -from paddle.fluid import core +from paddle.base import core def ftrl_step(param, grad, rows, sq_accum, lin_accum, lr, l1, l2, lr_power): diff --git a/test/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py index d0c326d7b19b1..38e2b426e4775 100644 --- a/test/legacy_test/test_full_like_op.py +++ b/test/legacy_test/test_full_like_op.py @@ -19,8 +19,8 @@ import paddle import paddle.framework.dtype as dtypes -from paddle.fluid import core -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base import core +from paddle.base.framework import convert_np_dtype_to_dtype_ from paddle.static import Program, program_guard @@ -114,7 +114,7 @@ def setUp(self): bf16_flag = self.dtype == np.uint16 x = np.zeros(self.shape).astype(np.float32 if bf16_flag else self.dtype) - x = OpTest.np_dtype_to_fluid_dtype(x) + x = OpTest.np_dtype_to_base_dtype(x) out = np.full_like(x, self.fill_value, self.dtype) diff --git a/test/legacy_test/test_full_op.py b/test/legacy_test/test_full_op.py index 9a5c95044927f..74e928e58a52a 100644 --- a/test/legacy_test/test_full_op.py +++ b/test/legacy_test/test_full_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard # Test python API @@ -64,9 +64,9 @@ def test_api(self): shape=shape_tensor_int64, dtype=np.float32, fill_value=val ) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "shape_tensor_int32": np.array([1, 2]).astype("int32"), "shape_tensor_int64": np.array([1, 2]).astype("int64"), @@ -97,7 +97,7 @@ def test_api(self): ) def test_api_eager(self): - with fluid.dygraph.base.guard(): + with base.dygraph.base.guard(): positive_2_int32 = paddle.tensor.fill_constant([1], "int32", 2) positive_2_int64 = paddle.tensor.fill_constant([1], "int64", 2) positive_4_int64 = paddle.tensor.fill_constant( diff --git a/test/legacy_test/test_functional_conv1d.py b/test/legacy_test/test_functional_conv1d.py index d050c6163900b..5100385497b50 100644 --- a/test/legacy_test/test_functional_conv1d.py +++ b/test/legacy_test/test_functional_conv1d.py @@ -18,7 +18,7 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F diff --git a/test/legacy_test/test_functional_conv1d_transpose.py b/test/legacy_test/test_functional_conv1d_transpose.py index 865c848f8ba1d..1f1dbd05940e4 100644 --- a/test/legacy_test/test_functional_conv1d_transpose.py +++ b/test/legacy_test/test_functional_conv1d_transpose.py @@ -18,7 +18,7 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py index 2d8484cbee1e2..5b68ef2d46c0b 100644 --- a/test/legacy_test/test_functional_conv2d.py +++ b/test/legacy_test/test_functional_conv2d.py @@ -18,9 +18,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestFunctionalConv2D(TestCase): @@ -72,10 +72,10 @@ def prepare(self): ) def static_graph_case_1(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = paddle.static.data( "input", @@ -103,16 +103,16 @@ def static_graph_case_1(self): act=self.act, data_format=self.data_format, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out def static_graph_case_2(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = x = paddle.static.data( "input", @@ -146,7 +146,7 @@ def static_graph_case_2(self): if self.act == 'sigmoid': y = F.sigmoid(y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) feed_dict = {"input": self.input, "weight": self.weight} if not self.no_bias: @@ -185,14 +185,14 @@ def _test_identity(self): np.testing.assert_array_almost_equal(out2, out3) def test_identity_cpu(self): - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self._test_identity() @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) def test_identity_gpu(self): - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self._test_identity() @@ -230,10 +230,10 @@ def prepare(self): self.bias_shape = (self.out_channels,) def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): self.channel_last = self.data_format == "NHWC" if self.channel_last: x = x = paddle.static.data( @@ -505,10 +505,10 @@ def setUp(self): self.data_format = "NCHW" def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data( "input", self.input.shape, dtype=paddle.float32 ) @@ -527,7 +527,7 @@ def static_graph_case(self): act=None, data_format=self.data_format, ) - exe = fluid.Executor() + exe = base.Executor() exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out diff --git a/test/legacy_test/test_functional_conv2d_transpose.py b/test/legacy_test/test_functional_conv2d_transpose.py index dd708614b8818..3bd999962ef09 100644 --- a/test/legacy_test/test_functional_conv2d_transpose.py +++ b/test/legacy_test/test_functional_conv2d_transpose.py @@ -18,9 +18,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestFunctionalConv2D(TestCase): @@ -73,10 +73,10 @@ def prepare(self): ) def static_graph_case_1(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = paddle.static.data( "input", @@ -104,16 +104,16 @@ def static_graph_case_1(self): else paddle.nn.initializer.Assign(self.bias), data_format=self.data_format, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out def static_graph_case_2(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = x = paddle.static.data( "input", @@ -144,7 +144,7 @@ def static_graph_case_2(self): groups=self.groups, data_format=self.data_format, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) feed_dict = {"input": self.input, "weight": self.weight} if not self.no_bias: @@ -180,14 +180,14 @@ def _test_identity(self): np.testing.assert_array_almost_equal(out2, out3) def test_identity_cpu(self): - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self._test_identity() @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) def test_identity_gpu(self): - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self._test_identity() @@ -226,10 +226,10 @@ def prepare(self): self.bias_shape = (self.out_channels,) def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): self.channel_last = self.data_format == "NHWC" if self.channel_last: x = x = paddle.static.data( @@ -513,10 +513,10 @@ def setUp(self): self.data_format = "NCHW" def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data( "input", self.input.shape, dtype=paddle.float32 ) @@ -535,7 +535,7 @@ def static_graph_case(self): act=None, data_format=self.data_format, ) - exe = fluid.Executor() + exe = base.Executor() exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py index 3f3415afdbf11..0a5eb925f5835 100644 --- a/test/legacy_test/test_functional_conv3d.py +++ b/test/legacy_test/test_functional_conv3d.py @@ -18,9 +18,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestFunctionalConv3D(TestCase): @@ -72,10 +72,10 @@ def prepare(self): ) def static_graph_case_1(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = paddle.static.data( "input", @@ -103,16 +103,16 @@ def static_graph_case_1(self): act=self.act, data_format=self.data_format, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out def static_graph_case_2(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = x = paddle.static.data( "input", @@ -146,7 +146,7 @@ def static_graph_case_2(self): if self.act == 'sigmoid': y = F.sigmoid(y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) feed_dict = {"input": self.input, "weight": self.weight} if not self.no_bias: @@ -185,14 +185,14 @@ def _test_identity(self): np.testing.assert_array_almost_equal(out2, out3) def test_identity_cpu(self): - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self._test_identity() @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) def test_identity_gpu(self): - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self._test_identity() @@ -230,10 +230,10 @@ def prepare(self): self.bias_shape = (self.out_channels,) def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): self.channel_last = self.data_format == "NDHWC" if self.channel_last: x = x = paddle.static.data( @@ -480,10 +480,10 @@ def setUp(self): self.data_format = "NCDHW" def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data( "input", self.input.shape, dtype=paddle.float32 ) @@ -502,7 +502,7 @@ def static_graph_case(self): act=None, data_format=self.data_format, ) - exe = fluid.Executor() + exe = base.Executor() exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out diff --git a/test/legacy_test/test_functional_conv3d_transpose.py b/test/legacy_test/test_functional_conv3d_transpose.py index 22aaeb02a92f5..b0bd104d2935b 100644 --- a/test/legacy_test/test_functional_conv3d_transpose.py +++ b/test/legacy_test/test_functional_conv3d_transpose.py @@ -18,9 +18,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestFunctionalConv3DTranspose(TestCase): @@ -73,10 +73,10 @@ def prepare(self): ) def static_graph_case_1(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = paddle.static.data( "input", @@ -105,16 +105,16 @@ def static_graph_case_1(self): act=self.act, data_format=self.data_format, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out def static_graph_case_2(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): if self.channel_last: x = x = paddle.static.data( "input", @@ -147,7 +147,7 @@ def static_graph_case_2(self): ) if self.act == 'sigmoid': y = F.sigmoid(y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(start) feed_dict = {"input": self.input, "weight": self.weight} if not self.no_bias: @@ -185,14 +185,14 @@ def _test_identity(self): np.testing.assert_array_almost_equal(out2, out3) def test_identity_cpu(self): - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() self._test_identity() @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) def test_identity_gpu(self): - self.place = fluid.CUDAPlace(0) + self.place = base.CUDAPlace(0) self._test_identity() @@ -231,10 +231,10 @@ def prepare(self): self.bias_shape = (self.out_channels,) def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): self.channel_last = self.data_format == "NDHWC" if self.channel_last: x = x = paddle.static.data( @@ -538,10 +538,10 @@ def setUp(self): self.data_format = "NCDHW" def static_graph_case(self): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data( "input", self.input.shape, dtype=paddle.float32 ) @@ -560,7 +560,7 @@ def static_graph_case(self): act=None, data_format=self.data_format, ) - exe = fluid.Executor() + exe = base.Executor() exe.run(start) (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out diff --git a/test/legacy_test/test_fuse_all_reduce_pass.py b/test/legacy_test/test_fuse_all_reduce_pass.py index b7ea594522b9f..0745844bda323 100644 --- a/test/legacy_test/test_fuse_all_reduce_pass.py +++ b/test/legacy_test/test_fuse_all_reduce_pass.py @@ -21,8 +21,8 @@ from simple_nets import bow_net, fc_with_batchnorm, init_data, simple_fc_net import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -142,8 +142,8 @@ def setUpClass(cls): cls.train_data = next(reader) def get_data_from_feeder(self): - place = fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=["words", "label"], place=place) + place = base.CPUPlace() + feeder = base.DataFeeder(feed_list=["words", "label"], place=place) return feeder.feed(self.train_data) def _decorate_compare_fused_all_reduce(self, model, use_device): diff --git a/test/legacy_test/test_fuse_bn_act_pass.py b/test/legacy_test/test_fuse_bn_act_pass.py index 711b4061506d5..6faaff59b51ef 100644 --- a/test/legacy_test/test_fuse_bn_act_pass.py +++ b/test/legacy_test/test_fuse_bn_act_pass.py @@ -15,12 +15,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestFuseBatchNormActPass(unittest.TestCase): def build_program(self, main_program, startup_program, use_cuda, seed=1): - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): x = paddle.static.data( name='x', shape=[-1, 1, 28, 28], dtype='float32' ) @@ -35,11 +35,11 @@ def build_program(self, main_program, startup_program, use_cuda, seed=1): bias_attr=False, data_format='NHWC', ) - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( name='batch_norm_w', initializer=paddle.nn.initializer.Constant(value=1.0), ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( name='batch_norm_b', initializer=paddle.nn.initializer.Constant(value=0.0), ) @@ -72,26 +72,26 @@ def build_program(self, main_program, startup_program, use_cuda, seed=1): def check(self, place, use_cuda): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() x, y, loss = self.build_program(main_program, startup_program, use_cuda) - exe = fluid.Executor(place) + exe = base.Executor(place) iters = 8 batch_size = 16 - feeder = fluid.DataFeeder(feed_list=[x, y], place=place) + feeder = base.DataFeeder(feed_list=[x, y], place=place) # close fused_bn_act_ops - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.fuse_bn_act_ops = False - binary = fluid.CompiledProgram( + binary = base.CompiledProgram( main_program, build_strategy=build_strategy ) train_reader = paddle.batch( paddle.dataset.mnist.train(), batch_size=batch_size ) loss_vals = [] - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) for _ in range(iters): data = next(train_reader()) @@ -101,17 +101,17 @@ def check(self, place, use_cuda): loss_vals.append(loss_v[0]) # open fused_bn_act_ops - build_strategy_fused = fluid.BuildStrategy() + build_strategy_fused = base.BuildStrategy() build_strategy_fused.fuse_bn_act_ops = True - binary_fused = fluid.CompiledProgram( + binary_fused = base.CompiledProgram( main_program, build_strategy=build_strategy_fused ) train_reader_fused = paddle.batch( paddle.dataset.mnist.train(), batch_size=batch_size ) loss_vals_fused = [] - scope_fused = fluid.Scope() - with fluid.scope_guard(scope_fused): + scope_fused = base.Scope() + with base.scope_guard(scope_fused): exe.run(startup_program) for _ in range(iters): data = next(train_reader_fused()) @@ -125,12 +125,12 @@ def check(self, place, use_cuda): self.assertAlmostEqual(loss_vals[i], loss_vals_fused[i], delta=1e-5) def test_fuse_bn_act_pass_cpu(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self.check(place, use_cuda=False) def test_fuse_bn_act_pass_cuda(self): - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check(place, use_cuda=True) diff --git a/test/legacy_test/test_fuse_bn_add_act_pass.py b/test/legacy_test/test_fuse_bn_add_act_pass.py index f0b11655916de..d121a211e7afd 100644 --- a/test/legacy_test/test_fuse_bn_add_act_pass.py +++ b/test/legacy_test/test_fuse_bn_add_act_pass.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -29,33 +29,33 @@ ) class TestFusedBnAddActAPI(unittest.TestCase): def setUp(self): - self.conv_param_attr1 = fluid.ParamAttr( + self.conv_param_attr1 = base.ParamAttr( name='conv2d_1.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - self.conv_param_attr2 = fluid.ParamAttr( + self.conv_param_attr2 = base.ParamAttr( name='conv2d_2.weight', initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - self.bn_param_attr1 = fluid.ParamAttr( + self.bn_param_attr1 = base.ParamAttr( name='batch_norm_w_1', initializer=paddle.nn.initializer.Constant(value=1.0), ) - self.bn_bias_attr1 = fluid.ParamAttr( + self.bn_bias_attr1 = base.ParamAttr( name='batch_norm_b_1', initializer=paddle.nn.initializer.Constant(value=0.0), ) - self.bn_param_attr2 = fluid.ParamAttr( + self.bn_param_attr2 = base.ParamAttr( name='batch_norm_w_2', initializer=paddle.nn.initializer.Constant(value=1.0), ) - self.bn_bias_attr2 = fluid.ParamAttr( + self.bn_bias_attr2 = base.ParamAttr( name='batch_norm_b_2', initializer=paddle.nn.initializer.Constant(value=0.0), ) - self.fc_param_attr = fluid.ParamAttr( + self.fc_param_attr = base.ParamAttr( name='fc.weight', initializer=paddle.nn.initializer.XavierNormal(), ) @@ -63,7 +63,7 @@ def setUp(self): def build_fused_program( self, main_program, startup_program, use_cuda, seed=1 ): - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): x = paddle.static.data( name='x', shape=[-1, 1, 28, 28], dtype='float32' ) @@ -124,7 +124,7 @@ def build_fused_program( def build_origin_program( self, main_program, startup_program, use_cuda, seed=1 ): - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): x = paddle.static.data( name='x', shape=[-1, 1, 28, 28], dtype='float32' ) @@ -191,22 +191,22 @@ def check(self, place, use_cuda): batch_size = 16 # build_fused_program: turn on fuse_bn_add_act_ops - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() loss = self.build_origin_program( main_program, startup_program, use_cuda ) - build_strategy_fused = fluid.BuildStrategy() + build_strategy_fused = base.BuildStrategy() build_strategy_fused.fuse_bn_add_act_ops = True - binary_fused = fluid.CompiledProgram( + binary_fused = base.CompiledProgram( main_program, build_strategy=build_strategy_fused ) - exe = fluid.Executor(place) + exe = base.Executor(place) loss_vals_fused = [] x_data = [] y_data = [] - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) for _ in range(iters): x = np.random.random((batch_size, 1, 28, 28)).astype("float32") @@ -219,14 +219,14 @@ def check(self, place, use_cuda): loss_vals_fused.append(loss_v[0]) # build_origin_program: turn off fused_bn_act_ops - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.fuse_bn_add_act_ops = False - binary = fluid.CompiledProgram( + binary = base.CompiledProgram( main_program, build_strategy=build_strategy_fused ) loss_vals = [] - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) for i in range(iters): loss_v = exe.run( @@ -241,20 +241,20 @@ def check(self, place, use_cuda): self.assertAlmostEqual(loss_vals[i], loss_vals_fused[i], delta=1e-5) def test_fuse_bn_add_act(self): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) self.check(place, use_cuda=True) def test_fuse_bn_add_act_API(self): # build_fused_program: use fused_bn_add_act python API - main_program = fluid.Program() - startup_program = fluid.Program() - place = fluid.CUDAPlace(0) + main_program = base.Program() + startup_program = base.Program() + place = base.CUDAPlace(0) x, y, loss = self.build_fused_program( main_program, startup_program, use_cuda=True ) - exe = fluid.Executor(place) - scope = fluid.Scope() - with fluid.scope_guard(scope): + exe = base.Executor(place) + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) for _ in range(5): x = np.random.random((4, 1, 28, 28)).astype("float32") diff --git a/test/legacy_test/test_fuse_elewise_add_act_pass.py b/test/legacy_test/test_fuse_elewise_add_act_pass.py index 02348fae60a0e..b9237a14bd108 100644 --- a/test/legacy_test/test_fuse_elewise_add_act_pass.py +++ b/test/legacy_test/test_fuse_elewise_add_act_pass.py @@ -21,8 +21,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestMNIST(TestParallelExecutorBase): @@ -111,23 +111,23 @@ def check(self, place): paddle.seed(1) numpy.random.seed(1) paddle.framework.random._manual_program_seed(1) - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() X, Y, loss = self.build_program(main_program, startup_program) - exe = fluid.Executor(place) + exe = base.Executor(place) x = numpy.random.random(size=(3, 3)).astype('float32') y = numpy.random.random(size=(3, 3)).astype('float32') label = numpy.random.random(size=(3, 3)).astype('float32') # open fused_pass - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.fuse_elewise_add_act_ops = True compiled_prog_fused = paddle.static.CompiledProgram( main_program, build_strategy=build_strategy ) - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) loss_data_fused = exe.run( compiled_prog_fused, @@ -136,13 +136,13 @@ def check(self, place): ) # close fused_pass - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.fuse_elewise_add_act_ops = False compiled_prog = paddle.static.CompiledProgram( main_program, build_strategy=build_strategy ) - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) loss_data = exe.run( compiled_prog, feed={"X": x, "Y": y}, fetch_list=[loss.name] @@ -151,12 +151,12 @@ def check(self, place): self.assertEqual(loss_data_fused, loss_data) def test_fuse_act_add_grad_pass_cpu(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self.check(place) def test_fuse_act_add_grad_pass_cuda(self): - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check(place) diff --git a/test/legacy_test/test_fuse_gemm_epilogue_pass.py b/test/legacy_test/test_fuse_gemm_epilogue_pass.py index 13480e3d75ded..177ebfa6b1819 100644 --- a/test/legacy_test/test_fuse_gemm_epilogue_pass.py +++ b/test/legacy_test/test_fuse_gemm_epilogue_pass.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def compare(ref, res, atol, rtol): diff --git a/test/legacy_test/test_fuse_optimizer_pass.py b/test/legacy_test/test_fuse_optimizer_pass.py index d85355306dfc3..3fa7f3d999a61 100644 --- a/test/legacy_test/test_fuse_optimizer_pass.py +++ b/test/legacy_test/test_fuse_optimizer_pass.py @@ -21,8 +21,8 @@ from simple_nets import bow_net, fc_with_batchnorm, init_data import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestFuseOptimizationOps(TestParallelExecutorBase): @@ -124,8 +124,8 @@ def setUpClass(cls): cls.train_data = next(reader) def _get_data_from_feeder(self): - place = fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=["words", "label"], place=place) + place = base.CPUPlace() + feeder = base.DataFeeder(feed_list=["words", "label"], place=place) return feeder.feed(self.train_data) def _decorate_compare_fused_optimizer_ops( diff --git a/test/legacy_test/test_fuse_relu_depthwise_conv_pass.py b/test/legacy_test/test_fuse_relu_depthwise_conv_pass.py index 70487e22448f4..fd294535d55d7 100644 --- a/test/legacy_test/test_fuse_relu_depthwise_conv_pass.py +++ b/test/legacy_test/test_fuse_relu_depthwise_conv_pass.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def norm(*args, **kargs): diff --git a/test/legacy_test/test_fused_attention_no_dropout.py b/test/legacy_test/test_fused_attention_no_dropout.py index 4f18abd79e0fe..ca9b4d90f1dde 100644 --- a/test/legacy_test/test_fused_attention_no_dropout.py +++ b/test/legacy_test/test_fused_attention_no_dropout.py @@ -195,7 +195,7 @@ def set_configs(self): class TestFusedAttentionAPIError(unittest.TestCase): def test_invalid_x_rank(self): def test_x_rank_1(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): layer = FusedMultiHeadAttention(embed_dim=1, num_heads=1) array = np.array([1.9], dtype=np.float32) x = paddle.to_tensor(np.reshape(array, [1]), dtype='float32') diff --git a/test/legacy_test/test_fused_attention_op.py b/test/legacy_test/test_fused_attention_op.py index 1feed2cccce3a..271b4ab6bec33 100644 --- a/test/legacy_test/test_fused_attention_op.py +++ b/test/legacy_test/test_fused_attention_op.py @@ -21,7 +21,7 @@ import paddle.incubate.nn.functional as incubate_f import paddle.nn.functional as F from paddle import tensor -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.nn.layer.common import Dropout, Linear from paddle.nn.layer.norm import LayerNorm from paddle.nn.layer.transformer import _convert_attention_mask diff --git a/test/legacy_test/test_fused_attention_pass.py b/test/legacy_test/test_fused_attention_pass.py index 3387662d75827..37a356ea64b70 100644 --- a/test/legacy_test/test_fused_attention_pass.py +++ b/test/legacy_test/test_fused_attention_pass.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F +from paddle.base import core from paddle.distributed.passes import PassManager, new_pass -from paddle.fluid import core paddle.enable_static() diff --git a/test/legacy_test/test_fused_bias_act_op.py b/test/legacy_test/test_fused_bias_act_op.py index 53ed9cc330693..054932f13f57d 100644 --- a/test/legacy_test/test_fused_bias_act_op.py +++ b/test/legacy_test/test_fused_bias_act_op.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def round_type_1_process(val): diff --git a/test/legacy_test/test_fused_bias_dropout_residual_layer_norm_op.py b/test/legacy_test/test_fused_bias_dropout_residual_layer_norm_op.py index 8d9295c27276e..87c7bcf4db908 100644 --- a/test/legacy_test/test_fused_bias_dropout_residual_layer_norm_op.py +++ b/test/legacy_test/test_fused_bias_dropout_residual_layer_norm_op.py @@ -19,7 +19,7 @@ import paddle import paddle.incubate.nn.functional as incubate_f -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.nn.layer.common import Dropout from paddle.nn.layer.norm import LayerNorm diff --git a/test/legacy_test/test_fused_dropout_add_op.py b/test/legacy_test/test_fused_dropout_add_op.py index efd1833a713cb..6466775f432da 100644 --- a/test/legacy_test/test_fused_dropout_add_op.py +++ b/test/legacy_test/test_fused_dropout_add_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.nn.functional import fused_dropout_add from paddle.incubate.nn.layer.fused_dropout_add import FusedDropoutAdd @@ -152,7 +152,7 @@ def test_static_op(self): outs = fused_dropout_add(xs, ys, p=0.5, training=True) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "xs": x_data.astype('float16'), diff --git a/test/legacy_test/test_fused_ec_moe_op.py b/test/legacy_test/test_fused_ec_moe_op.py index 87b065a9017ec..3ac780806c911 100644 --- a/test/legacy_test/test_fused_ec_moe_op.py +++ b/test/legacy_test/test_fused_ec_moe_op.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.incubate.nn.functional import fused_ec_moe from paddle.nn.layer.common import Linear diff --git a/test/legacy_test/test_fused_elemwise_activation_op.py b/test/legacy_test/test_fused_elemwise_activation_op.py index 44a4588a9ad34..28cffdcabd579 100644 --- a/test/legacy_test/test_fused_elemwise_activation_op.py +++ b/test/legacy_test/test_fused_elemwise_activation_op.py @@ -18,7 +18,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core # TestFusedElementwiseActivationOp # TestFusedElementwiseActivationOp_scalar @@ -51,8 +51,8 @@ def setUp(self): self.intermediate_out = self.intermediate_out.astype(self.dtype) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } if self.attrs["save_intermediate_out"]: self.outputs = { diff --git a/test/legacy_test/test_fused_emb_seq_pool_op.py b/test/legacy_test/test_fused_emb_seq_pool_op.py index 9a8e1f3913f22..4d2e774e5465c 100644 --- a/test/legacy_test/test_fused_emb_seq_pool_op.py +++ b/test/legacy_test/test_fused_emb_seq_pool_op.py @@ -108,7 +108,7 @@ class TestFusedEmbeddingSeqPoolApi(unittest.TestCase): def test_api(self): with paddle_static_guard(): if ver.mkl() == "ON" and 'Linux' in platform.platform(): - from paddle import fluid + from paddle import base dict_size = 20 data_t = paddle.static.data( @@ -123,11 +123,11 @@ def test_api(self): is_sparse=False, ) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) # prepare input words' idx - x_tensor = fluid.core.LoDTensor() + x_tensor = base.core.LoDTensor() idxs = np.random.randint(1, 10, (8)).astype("int64") x_tensor.set(idxs, place) diff --git a/test/legacy_test/test_fused_fc_elementwise_layernorm_op.py b/test/legacy_test/test_fused_fc_elementwise_layernorm_op.py index f4812116d9385..330931fdc4170 100644 --- a/test/legacy_test/test_fused_fc_elementwise_layernorm_op.py +++ b/test/legacy_test/test_fused_fc_elementwise_layernorm_op.py @@ -19,7 +19,7 @@ from test_fc_op import MatrixGenerate, fc_refer from test_layer_norm_op import _reference_layer_norm_naive -from paddle.fluid import core +from paddle.base import core np.random.random(123) diff --git a/test/legacy_test/test_fused_feedforward_op.py b/test/legacy_test/test_fused_feedforward_op.py index c8739c38e7699..6aba4784ff6f6 100644 --- a/test/legacy_test/test_fused_feedforward_op.py +++ b/test/legacy_test/test_fused_feedforward_op.py @@ -19,7 +19,7 @@ import paddle import paddle.incubate.nn.functional as incubate_f import paddle.nn.functional as F -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.nn.layer import transformer from paddle.nn.layer.common import Dropout, Linear from paddle.nn.layer.norm import LayerNorm diff --git a/test/legacy_test/test_fused_feedforward_pass.py b/test/legacy_test/test_fused_feedforward_pass.py index 107e3f319d3d7..03240d88d2d8b 100644 --- a/test/legacy_test/test_fused_feedforward_pass.py +++ b/test/legacy_test/test_fused_feedforward_pass.py @@ -18,8 +18,8 @@ import paddle from paddle import nn +from paddle.base import core from paddle.distributed.passes import PassManager, new_pass -from paddle.fluid import core paddle.enable_static() diff --git a/test/legacy_test/test_fused_gate_attention_op.py b/test/legacy_test/test_fused_gate_attention_op.py index e88b923f287b0..7bb5790f0c766 100644 --- a/test/legacy_test/test_fused_gate_attention_op.py +++ b/test/legacy_test/test_fused_gate_attention_op.py @@ -30,7 +30,7 @@ import paddle import paddle.incubate.nn.functional as F from paddle import _legacy_C_ops, nn -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/legacy_test/test_fused_gemm_epilogue_grad_op.py b/test/legacy_test/test_fused_gemm_epilogue_grad_op.py index f7a7d8dbf6608..0014f5a440cc9 100644 --- a/test/legacy_test/test_fused_gemm_epilogue_grad_op.py +++ b/test/legacy_test/test_fused_gemm_epilogue_grad_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core def get_outputs(DOut, X, Y): diff --git a/test/legacy_test/test_fused_gemm_epilogue_op.py b/test/legacy_test/test_fused_gemm_epilogue_op.py index 5aeae6671882e..6064536ec4a90 100644 --- a/test/legacy_test/test_fused_gemm_epilogue_op.py +++ b/test/legacy_test/test_fused_gemm_epilogue_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest, skip_check_grad_ci, skip_check_inplace_ci import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.nn.functional import fused_linear_activation diff --git a/test/legacy_test/test_fused_layernorm_op.py b/test/legacy_test/test_fused_layernorm_op.py index a50f216c67315..cf8b73c57475a 100644 --- a/test/legacy_test/test_fused_layernorm_op.py +++ b/test/legacy_test/test_fused_layernorm_op.py @@ -16,8 +16,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def quant_helper( @@ -433,7 +433,7 @@ def check_layernorm(self, x_np, gamma_np, beta_np, dtype): self.epsilon, begin_norm_axis=1, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -483,7 +483,7 @@ def check_layernorm_int8(self, x_np, gamma_np, beta_np, dtype): quant_max_bound=self.quant_max_bound, quant_min_bound=self.quant_min_bound, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -532,7 +532,7 @@ def check_residual_bias_add(self, x_np, residual_np, bias_np, dtype): quant_min_bound=self.quant_min_bound, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -590,7 +590,7 @@ def check_residual_bias_layernorm( residual=residual_static, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -664,7 +664,7 @@ def check_residual_bias_layernorm_int8( quant_min_bound=self.quant_min_bound, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), diff --git a/test/legacy_test/test_fused_matmul_bias.py b/test/legacy_test/test_fused_matmul_bias.py index 6210ad56cd847..85666710b0e45 100644 --- a/test/legacy_test/test_fused_matmul_bias.py +++ b/test/legacy_test/test_fused_matmul_bias.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.nn import FusedLinear from paddle.incubate.nn.functional import fused_linear, fused_matmul_bias diff --git a/test/legacy_test/test_fused_multi_transformer_int8_op.py b/test/legacy_test/test_fused_multi_transformer_int8_op.py index d54eff322b64d..ba4be8f9831a4 100644 --- a/test/legacy_test/test_fused_multi_transformer_int8_op.py +++ b/test/legacy_test/test_fused_multi_transformer_int8_op.py @@ -20,8 +20,8 @@ import paddle import paddle.nn.functional as F from paddle import _legacy_C_ops, tensor -from paddle.fluid import core -from paddle.fluid.framework import default_main_program +from paddle.base import core +from paddle.base.framework import default_main_program from paddle.nn.layer.common import Dropout from paddle.nn.layer.norm import LayerNorm from paddle.nn.layer.transformer import _convert_attention_mask diff --git a/test/legacy_test/test_fused_multi_transformer_op.py b/test/legacy_test/test_fused_multi_transformer_op.py index 5ae564dcdb0ff..dbe4fa6f014e8 100644 --- a/test/legacy_test/test_fused_multi_transformer_op.py +++ b/test/legacy_test/test_fused_multi_transformer_op.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F from paddle import tensor -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.incubate.nn import FusedMultiTransformer from paddle.incubate.nn.functional import fused_multi_transformer from paddle.nn.layer.common import Dropout, Linear @@ -57,7 +57,7 @@ def setUp(self): # use autograd to check grad in this unittest. self.__class__.no_need_check_grad = False - bias_attr = paddle.fluid.ParamAttr( + bias_attr = paddle.base.ParamAttr( initializer=paddle.paddle.nn.initializer.Constant(value=0.0005) ) self.q_proj = Linear( @@ -996,13 +996,13 @@ def GetFusedMultiTransformerOutStatic(self): } if self.has_pre_cache: out = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed=feed_data, fetch_list=[final_out[0].name], ) else: out = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed=feed_data, fetch_list=[final_out.name], ) diff --git a/test/legacy_test/test_fused_multihead_matmul_op.py b/test/legacy_test/test_fused_multihead_matmul_op.py index d600aef0b98b4..ebf73fd6353fb 100644 --- a/test/legacy_test/test_fused_multihead_matmul_op.py +++ b/test/legacy_test/test_fused_multihead_matmul_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core np.random.random(123) diff --git a/test/legacy_test/test_fused_rotary_position_embedding.py b/test/legacy_test/test_fused_rotary_position_embedding.py index de6355d56a5ee..5be92f6f9b705 100644 --- a/test/legacy_test/test_fused_rotary_position_embedding.py +++ b/test/legacy_test/test_fused_rotary_position_embedding.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.nn.functional import fused_rotary_position_embedding diff --git a/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py b/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py index cbed4e5b33fcf..f8cbc8c387dc7 100644 --- a/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py +++ b/test/legacy_test/test_fused_scale_bias_relu_conv_bnstats_op.py @@ -20,7 +20,7 @@ import paddle from paddle import nn -from paddle.fluid import core +from paddle.base import core def skip_unit_test(): diff --git a/test/legacy_test/test_fused_transformer_encoder_layer.py b/test/legacy_test/test_fused_transformer_encoder_layer.py index c4b0a47420a1a..ed73401252828 100644 --- a/test/legacy_test/test_fused_transformer_encoder_layer.py +++ b/test/legacy_test/test_fused_transformer_encoder_layer.py @@ -16,7 +16,7 @@ import numpy as np import paddle -from paddle.fluid.framework import default_main_program, in_dygraph_mode +from paddle.base.framework import default_main_program, in_dygraph_mode from paddle.incubate.nn import FusedTransformerEncoderLayer from paddle.nn import TransformerEncoderLayer diff --git a/test/legacy_test/test_fusion_transpose_flatten_concat_op.py b/test/legacy_test/test_fusion_transpose_flatten_concat_op.py index 873687963faba..cb461ec073651 100644 --- a/test/legacy_test/test_fusion_transpose_flatten_concat_op.py +++ b/test/legacy_test/test_fusion_transpose_flatten_concat_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/legacy_test/test_gather_nd_op.py b/test/legacy_test/test_gather_nd_op.py index 6102a0a8fcc69..d934b047ae149 100644 --- a/test/legacy_test/test_gather_nd_op.py +++ b/test/legacy_test/test_gather_nd_op.py @@ -22,8 +22,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestGatherNdOpWithEmptyIndex(OpTest): @@ -554,14 +554,14 @@ def test_index_dtype(): class TestGatherNdAPI2(unittest.TestCase): def test_static(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data('data1', shape=[-1, 2], dtype='float64') data1.desc.set_need_check_feed(False) index = paddle.static.data('index', shape=[-1, 1], dtype='int32') index.desc.set_need_check_feed(False) out = paddle.gather_nd(data1, index) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input = np.array([[1, 2], [3, 4], [5, 6]]) index_1 = np.array([[1]]).astype('int32') (result,) = exe.run( @@ -571,7 +571,7 @@ def test_static(self): np.testing.assert_allclose(result, expected_output, rtol=1e-05) def test_static_fp16_with_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -607,8 +607,8 @@ def test_imperative(self): paddle.disable_static() input_1 = np.array([[1, 2], [3, 4], [5, 6]]) index_1 = np.array([[1]]) - input = fluid.dygraph.to_variable(input_1) - index = fluid.dygraph.to_variable(index_1) + input = base.dygraph.to_variable(input_1) + index = base.dygraph.to_variable(index_1) output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([[3, 4]]) diff --git a/test/legacy_test/test_gather_op.py b/test/legacy_test/test_gather_op.py index ff67a85484a86..8bc012883fc65 100644 --- a/test/legacy_test/test_gather_op.py +++ b/test/legacy_test/test_gather_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import base +from paddle.base.dygraph.base import switch_to_static_graph from paddle.framework import core @@ -419,14 +419,14 @@ def config_dtype(self): class API_TestGather(unittest.TestCase): def test_out1(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data('data1', shape=[-1, 2], dtype='float64') data1.desc.set_need_check_feed(False) index = paddle.static.data('index', shape=[-1, 1], dtype='int32') index.desc.set_need_check_feed(False) out = paddle.gather(data1, index) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input = np.array([[1, 2], [3, 4], [5, 6]]) index_1 = np.array([1, 2]) (result,) = exe.run( @@ -500,7 +500,7 @@ def test_large_data(self): index = np.random.randint(0, 22682, size=(8859027)) def test_dygraph(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): gpu_out = paddle.gather( paddle.to_tensor(x), paddle.to_tensor(index) ) @@ -560,7 +560,7 @@ def test_axis_dtype1(): self.assertRaises(TypeError, test_axis_dtype1) def test_error2(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): shape = [8, 9, 6] x = paddle.static.data(shape=shape, dtype='int8', name='x') index = paddle.static.data(shape=shape, dtype='int32', name='mask') diff --git a/test/legacy_test/test_gather_tree_op.py b/test/legacy_test/test_gather_tree_op.py index 3bc12a1a40954..b59e30098aecc 100644 --- a/test/legacy_test/test_gather_tree_op.py +++ b/test/legacy_test/test_gather_tree_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid.framework import Program, program_guard +from paddle.base.framework import Program, program_guard class TestGatherTreeOp(OpTest): diff --git a/test/legacy_test/test_gaussian_nll_loss.py b/test/legacy_test/test_gaussian_nll_loss.py index 1480c83eb26ae..4a33a19fb766a 100644 --- a/test/legacy_test/test_gaussian_nll_loss.py +++ b/test/legacy_test/test_gaussian_nll_loss.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_gaussian_random_op.py b/test/legacy_test/test_gaussian_random_op.py index d134d8578cbda..293e79d05fcea 100644 --- a/test/legacy_test/test_gaussian_random_op.py +++ b/test/legacy_test/test_gaussian_random_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_uint16_to_float, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.tensor import random @@ -75,7 +75,7 @@ def setUp(self): "mean": self.mean, "std": self.std, "seed": 10, - "dtype": paddle.fluid.core.VarDesc.VarType.FP16, + "dtype": paddle.base.core.VarDesc.VarType.FP16, "use_mkldnn": self.use_mkldnn, } paddle.seed(10) @@ -118,7 +118,7 @@ def setUp(self): "mean": self.mean, "std": self.std, "seed": 10, - "dtype": paddle.fluid.core.VarDesc.VarType.BF16, + "dtype": paddle.base.core.VarDesc.VarType.BF16, "use_mkldnn": self.use_mkldnn, } paddle.seed(10) @@ -308,9 +308,9 @@ def test_api(self): seed=10, ) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "shape_tensor_int32": np.array([2000, 500]).astype("int32"), "shape_tensor_int64": np.array([2000, 500]).astype("int64"), @@ -335,17 +335,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) if paddle.is_compiled_with_cuda(): paddle.set_device('gpu') @@ -359,17 +359,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) if paddle.is_compiled_with_cuda(): paddle.set_device('gpu') diff --git a/test/legacy_test/test_gcd.py b/test/legacy_test/test_gcd.py index 738c040ea9890..a7ec34eca42c7 100644 --- a/test/legacy_test/test_gcd.py +++ b/test/legacy_test/test_gcd.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,9 +31,9 @@ def setUp(self): self.y_shape = [1] def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(startup_program, train_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(startup_program, train_program): x = paddle.static.data( name='input1', dtype='int32', shape=self.x_shape ) @@ -43,13 +43,13 @@ def test_static_graph(self): out = paddle.gcd(x, y) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input1': self.x_np, 'input2': self.y_np}, fetch_list=[out], ) diff --git a/test/legacy_test/test_gelu_op.py b/test/legacy_test/test_gelu_op.py index d0ab6be7d89f0..acd3ef5aed796 100644 --- a/test/legacy_test/test_gelu_op.py +++ b/test/legacy_test/test_gelu_op.py @@ -18,9 +18,9 @@ from scipy.special import erf import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base def gelu(x, approximate): @@ -43,7 +43,7 @@ def _test_case1_cpu(self, approximate): x = np.random.uniform(-1, 1, size=(11, 17)).astype(np.float32) y_ref = gelu(x, approximate) - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place) as g: x_var = dg.to_variable(x) y_var = F.gelu(x_var, approximate) @@ -54,7 +54,7 @@ def _test_case1_gpu(self, approximate): x = np.random.uniform(-1, 1, size=(11, 17)).astype(np.float32) y_ref = gelu(x, approximate) - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) with dg.guard(place) as g: x_var = dg.to_variable(x) y_var = F.gelu(x_var, approximate) @@ -64,7 +64,7 @@ def _test_case1_gpu(self, approximate): def test_cases(self): for approximate in [True, False]: self._test_case1_cpu(approximate) - if fluid.is_compiled_with_cuda(): + if base.is_compiled_with_cuda(): self._test_case1_gpu(approximate) def test_fast_math(self): diff --git a/test/legacy_test/test_generator_dataloader.py b/test/legacy_test/test_generator_dataloader.py index 9d89553852190..7de57eb3eb5ca 100644 --- a/test/legacy_test/test_generator_dataloader.py +++ b/test/legacy_test/test_generator_dataloader.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.reader import DataLoaderBase +from paddle import base +from paddle.base.reader import DataLoaderBase EPOCH_NUM = 20 BATCH_SIZE = 32 @@ -38,18 +38,18 @@ def random_reader(): def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - startup_prog = fluid.Program() - main_prog = fluid.Program() + startup_prog = base.Program() + main_prog = base.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[-1, 784], dtype='float32' ) label = paddle.static.data( name='label', shape=[-1, 1], dtype='int64' ) - py_reader = fluid.io.DataLoader.from_generator( + py_reader = base.io.DataLoader.from_generator( feed_list=[image, label], capacity=4, iterable=not use_legacy_py_reader, @@ -61,7 +61,7 @@ def simple_fc_net(places, use_legacy_py_reader, use_double_buffer): hidden, size=hidden_size, activation='tanh', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -90,24 +90,24 @@ def run_main( places, use_double_buffer, ): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, py_reader, loss = simple_fc_net( places, use_legacy_py_reader, use_double_buffer ) reader = paddle.batch(random_reader, batch_size=BATCH_SIZE) - ps = places if use_double_buffer else fluid.cpu_places(len(places)) + ps = places if use_double_buffer else base.cpu_places(len(places)) py_reader.set_sample_list_generator( reader, places=ps if py_reader.iterable else None ) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) - prog = fluid.CompiledProgram(main_prog) + prog = base.CompiledProgram(main_prog) step = 0 step_list = [] @@ -126,7 +126,7 @@ def run_main( ) loss_list.append(np.mean(L)) step += 1 - except fluid.core.EOFException: + except base.core.EOFException: py_reader.reset() break step_list.append(step) @@ -164,10 +164,10 @@ def run_main( def prepare_places(self, with_cpu=True, with_gpu=True): places = [] if with_cpu: - places.append([fluid.CPUPlace()]) + places.append([base.CPUPlace()]) - if with_gpu and fluid.core.is_compiled_with_cuda(): - tmp = fluid.cuda_places() + if with_gpu and base.core.is_compiled_with_cuda(): + tmp = base.cuda_places() assert len(tmp) > 0, "no gpu detected" places.append([tmp[0]]) return places diff --git a/test/legacy_test/test_get_all_op_or_kernel_names.py b/test/legacy_test/test_get_all_op_or_kernel_names.py index 8ee2d2cb0f569..55f0162ec041b 100644 --- a/test/legacy_test/test_get_all_op_or_kernel_names.py +++ b/test/legacy_test/test_get_all_op_or_kernel_names.py @@ -14,18 +14,18 @@ import unittest -from paddle.fluid import core +from paddle.base import core class TestGetAllRegisteredOpKernels(unittest.TestCase): - # reshape kernel is in fluid while not in phi + # reshape kernel is in base while not in phi def test_phi_kernels(self): self.assertTrue(core._get_all_register_op_kernels('phi')['sign']) with self.assertRaises(KeyError): core._get_all_register_op_kernels('phi')['reshape'] - # sign kernel is removed from fluid and added into phi - def test_fluid_kernels(self): + # sign kernel is removed from base and added into phi + def test_base_kernels(self): self.assertTrue(core._get_all_register_op_kernels('fluid')['reshape']) with self.assertRaises(KeyError): core._get_all_register_op_kernels('fluid')['sign'] @@ -49,7 +49,6 @@ def test_get_all_op_names(self): > len(set(all_op_with_phi_kernels) | set(all_op_with_fluid_kernels)) ) self.assertTrue("scale" in all_op_with_phi_kernels) - self.assertTrue("scale" in all_op_with_phi_kernels) if __name__ == '__main__': diff --git a/test/legacy_test/test_get_device_properties.py b/test/legacy_test/test_get_device_properties.py index aa3cac5d9f34a..41b7f94ad764c 100644 --- a/test/legacy_test/test_get_device_properties.py +++ b/test/legacy_test/test_get_device_properties.py @@ -14,8 +14,8 @@ import unittest +from paddle.base import core from paddle.device.cuda import device_count, get_device_properties -from paddle.fluid import core class TestGetDeviceProperties(unittest.TestCase): diff --git a/test/legacy_test/test_get_set_flags.py b/test/legacy_test/test_get_set_flags.py index 4b4cb9a7f7155..d732feb6c5f1d 100644 --- a/test/legacy_test/test_get_set_flags.py +++ b/test/legacy_test/test_get_set_flags.py @@ -14,7 +14,7 @@ import unittest -from paddle import fluid +from paddle import base class TestGetAndSetFlags(unittest.TestCase): @@ -24,13 +24,13 @@ def test_api(self): 'FLAGS_check_nan_inf': True, } - fluid.set_flags(flags) + base.set_flags(flags) flags_list = ['FLAGS_eager_delete_tensor_gb', 'FLAGS_check_nan_inf'] flag = 'FLAGS_eager_delete_tensor_gb' - res_list = fluid.get_flags(flags_list) - res = fluid.get_flags(flag) + res_list = base.get_flags(flags_list) + res = base.get_flags(flag) self.assertTrue(res_list['FLAGS_eager_delete_tensor_gb'], 1.0) self.assertTrue(res_list['FLAGS_check_nan_inf'], True) @@ -45,25 +45,25 @@ def test_errors(self): # flags type of set_flags should be dict. def test_set_flags_input_type(): - fluid.set_flags(flags_list) + base.set_flags(flags_list) self.assertRaises(TypeError, test_set_flags_input_type) # flags in set_flags should be public flags. def test_set_private_flag(): - fluid.set_flags(flag_private) + base.set_flags(flag_private) self.assertRaises(ValueError, test_set_private_flag) # flags type of set_flags should be list, tuple or string def test_get_flags_input_type(): - fluid.get_flags(flag) + base.get_flags(flag) self.assertRaises(TypeError, test_get_flags_input_type) # flags in get_flags should be public flags. def test_get_private_flag(): - fluid.get_flags('FLAGS_free_idle_chunk') + base.get_flags('FLAGS_free_idle_chunk') self.assertRaises(ValueError, test_get_private_flag) diff --git a/test/legacy_test/test_get_tensor_from_selected_rows_op.py b/test/legacy_test/test_get_tensor_from_selected_rows_op.py index dbbef2ce2cc74..e60b4711543ad 100644 --- a/test/legacy_test/test_get_tensor_from_selected_rows_op.py +++ b/test/legacy_test/test_get_tensor_from_selected_rows_op.py @@ -18,7 +18,7 @@ from op import Operator import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard from paddle.nn import clip diff --git a/test/legacy_test/test_global_var_getter_setter.py b/test/legacy_test/test_global_var_getter_setter.py index 3da1e85dc22cf..5ad33b36d0a2b 100644 --- a/test/legacy_test/test_global_var_getter_setter.py +++ b/test/legacy_test/test_global_var_getter_setter.py @@ -14,7 +14,7 @@ import unittest -from paddle import fluid +from paddle import base class VarInfo: @@ -31,7 +31,7 @@ def test_main(self): VarInfo("FLAGS_eager_delete_tensor_gb", float, True), ] - g = fluid.core.globals() + g = base.core.globals() for var in var_infos: self.assertTrue(var.name in g) self.assertTrue(var.name in g.keys()) diff --git a/test/legacy_test/test_glu.py b/test/legacy_test/test_glu.py index 91fe30651bb54..9ffbc5a706181 100644 --- a/test/legacy_test/test_glu.py +++ b/test/legacy_test/test_glu.py @@ -17,8 +17,8 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid +import paddle.base.dygraph as dg +from paddle import base from paddle.nn import functional as F @@ -47,9 +47,9 @@ def check_identity(self, place): np.testing.assert_allclose(y_np, self.out) def test_case(self): - self.check_identity(fluid.CPUPlace()) - if fluid.is_compiled_with_cuda(): - self.check_identity(fluid.CUDAPlace(0)) + self.check_identity(base.CPUPlace()) + if base.is_compiled_with_cuda(): + self.check_identity(base.CUDAPlace(0)) class TestGlu(unittest.TestCase): diff --git a/test/legacy_test/test_gpu_package_without_gpu_device.py b/test/legacy_test/test_gpu_package_without_gpu_device.py index 59db731117766..2429ff6c095f0 100644 --- a/test/legacy_test/test_gpu_package_without_gpu_device.py +++ b/test/legacy_test/test_gpu_package_without_gpu_device.py @@ -18,7 +18,7 @@ import tempfile import unittest -from paddle.fluid import core +from paddle.base import core class TestGPUPackagePaddle(unittest.TestCase): diff --git a/test/legacy_test/test_grad_clip_minimize.py b/test/legacy_test/test_grad_clip_minimize.py index 168eb701644bd..c6434d96e43dc 100644 --- a/test/legacy_test/test_grad_clip_minimize.py +++ b/test/legacy_test/test_grad_clip_minimize.py @@ -16,8 +16,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base.dygraph.base import to_variable from paddle.nn import ClipGradByGlobalNorm, ClipGradByNorm, ClipGradByValue @@ -60,7 +60,7 @@ def get_numpy_global_norm_result(self): return new_np_p_g def get_dygrap_global_norm_result(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): gloabl_norm_clip = ClipGradByGlobalNorm(self.max_global_norm) p_g_var = [] for p, g in self.para_and_grad: @@ -132,7 +132,7 @@ def get_numpy_norm_result(self): return new_p_g def get_dygrap_norm_result(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): norm_clip = ClipGradByNorm(self.max_norm) p_g_var = [] for p, g in self.para_and_grad: @@ -200,7 +200,7 @@ def get_numpy_clip_result(self): return new_p_g def get_dygrap_clip_result(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value_clip = ClipGradByValue(max=self.max_value, min=self.min_value) p_g_var = [] for p, g in self.para_and_grad: diff --git a/test/legacy_test/test_gradient_clip.py b/test/legacy_test/test_gradient_clip.py index dae8b7b47d884..96c5de1bfe3a3 100644 --- a/test/legacy_test/test_gradient_clip.py +++ b/test/legacy_test/test_gradient_clip.py @@ -18,8 +18,8 @@ from fake_reader import fake_imdb_reader import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.clip import _allow_pure_fp16_global_norm_clip paddle.enable_static() @@ -31,7 +31,7 @@ def bow_net( """ BOW net This model is from https://github.com/PaddlePaddle/models: - fluid/PaddleNLP/text_classification/nets.py + base/PaddleNLP/text_classification/nets.py """ emb = paddle.static.nn.embedding( input=data, is_sparse=True, size=[dict_dim, emb_dim] @@ -66,18 +66,18 @@ def init(self): pass def get_places(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) return places def check_clip_result(self, out, out_clip): pass def check_gradient_clip(self, place, dtype='float32'): - prog = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard( + prog = base.Program() + startup_program = base.Program() + with base.program_guard( main_program=prog, startup_program=startup_program ): image = paddle.static.data( @@ -105,12 +105,12 @@ def check_gradient_clip(self, place, dtype='float32'): prog_clip = prog.clone() avg_cost_clip = prog_clip.block(0).var(avg_cost.name) - p_g = fluid.backward.append_backward(loss=avg_cost) - p_g_clip = fluid.backward.append_backward(loss=avg_cost_clip) + p_g = base.backward.append_backward(loss=avg_cost) + p_g_clip = base.backward.append_backward(loss=avg_cost_clip) p_g = sorted(p_g, key=lambda x: x[0].name) p_g_clip = sorted(p_g_clip, key=lambda x: x[0].name) - with fluid.program_guard( + with base.program_guard( main_program=prog_clip, startup_program=startup_program ): p_g_clip = self.clip_gradient(p_g_clip) @@ -119,8 +119,8 @@ def check_gradient_clip(self, place, dtype='float32'): grad_clip_list = [elem[1] for elem in p_g_clip] train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=3) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[image, label], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[image, label], place=place) exe.run(startup_program) data = next(train_reader()) @@ -131,9 +131,9 @@ def check_gradient_clip(self, place, dtype='float32'): self.check_clip_result(out, out_clip) def check_sparse_gradient_clip(self, place): - prog = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard( + prog = base.Program() + startup_program = base.Program() + with base.program_guard( main_program=prog, startup_program=startup_program ): data = paddle.static.data( @@ -146,8 +146,8 @@ def check_sparse_gradient_clip(self, place): self.backward_and_optimize(cost) - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=[data, label], place=place) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=[data, label], place=place) exe.run(startup_program) data = next(self.train_data()) @@ -192,7 +192,7 @@ def func(params_grads): return paddle.nn.clip.append_gradient_clip_ops(params_grads) self.clip_gradient = func - self.check_gradient_clip(fluid.CPUPlace()) + self.check_gradient_clip(base.CPUPlace()) # test whether the output is right when use grad_clip def test_new_gradient_clip(self): @@ -201,7 +201,7 @@ def func(params_grads): return clip(params_grads) self.clip_gradient = func - self.check_gradient_clip(fluid.CPUPlace()) + self.check_gradient_clip(base.CPUPlace()) # test whether the output is right when use grad_clip under float64 def test_new_gradient_clip_fp64(self): @@ -210,7 +210,7 @@ def func(params_grads): return clip(params_grads) self.clip_gradient = func - self.check_gradient_clip(fluid.CPUPlace(), "float64") + self.check_gradient_clip(base.CPUPlace(), "float64") # invoke 'set_gradient_clip' in a wrong order def test_wrong_API_order(self): @@ -276,19 +276,19 @@ def test_none_grad_fp16(self): ) def _test_none_grad_helper(self, dtype): - prog = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard( + prog = base.Program() + startup_program = base.Program() + with base.program_guard( main_program=prog, startup_program=startup_program ): clip = paddle.nn.ClipGradByGlobalNorm(self.clip_norm) x = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter(name="x", shape=[2, 3], dtype=dtype) ) y = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter(name="y", shape=[2, 3], dtype=dtype) ) @@ -329,20 +329,20 @@ def func(params_grads): return clip(params_grads) self.clip_gradient = func - self.check_gradient_clip(fluid.CPUPlace()) + self.check_gradient_clip(base.CPUPlace()) # if grad is None or not need clip def test_none_grad(self): clip = paddle.nn.ClipGradByNorm(self.clip_norm) x = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( name="x", shape=[2, 3], dtype="float32", need_clip=False ) ) y = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( name="y", shape=[2, 3], dtype="float32", need_clip=False @@ -387,20 +387,20 @@ def func(params_grads): return clip(params_grads) self.clip_gradient = func - self.check_gradient_clip(fluid.CPUPlace()) + self.check_gradient_clip(base.CPUPlace()) # if grad is None or not need clip def test_none_grad(self): clip = paddle.nn.ClipGradByValue(self.max, self.min) x = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( name="x", shape=[2, 3], dtype="float32", need_clip=False ) ) y = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( name="y", shape=[2, 3], dtype="float32", need_clip=False @@ -422,10 +422,10 @@ def test_none_grad(self): class TestDygraphGradientClip(unittest.TestCase): def test_gradient_clip(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear = paddle.nn.Linear(5, 5) inputs = paddle.uniform([16, 5], min=-10, max=10).astype('float32') - out = linear(fluid.dygraph.to_variable(inputs)) + out = linear(base.dygraph.to_variable(inputs)) loss = paddle.mean(out) loss.backward() sgd_optimizer = paddle.optimizer.SGD( @@ -447,10 +447,10 @@ def setUp(self): def check_clip_result(self, loss, optimizer): # if grad is None - x = fluid.dygraph.to_variable( + x = base.dygraph.to_variable( np.array([2, 3]).astype("float32"), name="x" ) - y = fluid.dygraph.to_variable( + y = base.dygraph.to_variable( np.array([3, 4]).astype("float32"), name="y" ) assert len(self.clip1([(x, x), (x, y), (x, None)])) == 2 @@ -487,10 +487,10 @@ def setUp(self): def check_clip_result(self, loss, optimizer): # if grad is None - x = fluid.dygraph.to_variable(np.array([2, 3]).astype("float32")) + x = base.dygraph.to_variable(np.array([2, 3]).astype("float32")) assert len(self.clip([(x, None)])) == 0 # get params and grads from network - self.clip([(fluid.dygraph.to_variable(np.array([2, 3])), None)]) + self.clip([(base.dygraph.to_variable(np.array([2, 3])), None)]) opt, params_grads = optimizer.minimize(loss) _, grads = zip(*params_grads) params_grads = self.clip(params_grads) @@ -516,7 +516,7 @@ def setUp(self): def check_clip_result(self, loss, optimizer): # if grad is None - x = fluid.dygraph.to_variable(np.array([2, 3]).astype("float32")) + x = base.dygraph.to_variable(np.array([2, 3]).astype("float32")) assert len(self.clip([(x, None)])) == 0 # get params and grads from network opt, params_grads = optimizer.minimize(loss) @@ -549,8 +549,8 @@ def forward(self, x): class TestDygraphGradientClipFP16(unittest.TestCase): def test_gradient_clip(self): - if fluid.core.is_compiled_with_cuda(): - with fluid.dygraph.guard(): + if base.core.is_compiled_with_cuda(): + with base.dygraph.guard(): paddle.seed(10) model = SimpleNet() sgd_optimizer = paddle.optimizer.SGD( @@ -564,7 +564,7 @@ def test_gradient_clip(self): 'float32' ) with paddle.amp.auto_cast(level='O2'): - out = model(fluid.dygraph.to_variable(inputs)) + out = model(base.dygraph.to_variable(inputs)) loss = paddle.mean(out) scaled = scaler.scale(loss) scaled.backward() @@ -606,10 +606,10 @@ def test_gradient_clip(self): class TestDygraphGradientClipFP64(unittest.TestCase): def test_gradient_clip(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): inputs = paddle.uniform([16, 5], min=-10, max=10).astype('float32') linear = paddle.nn.Linear(5, 5) - out = linear(fluid.dygraph.to_variable(inputs)) + out = linear(base.dygraph.to_variable(inputs)) loss = paddle.mean(out) loss.backward() # before clip diff --git a/test/legacy_test/test_graph_khop_sampler.py b/test/legacy_test/test_graph_khop_sampler.py index 4070ad72a9442..cee848d549e72 100644 --- a/test/legacy_test/test_graph_khop_sampler.py +++ b/test/legacy_test/test_graph_khop_sampler.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestGraphKhopSampler(unittest.TestCase): @@ -91,20 +91,20 @@ def test_sample_result(self): def test_uva_sample_result(self): paddle.disable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): row = None - if fluid.framework.in_dygraph_mode(): - row = paddle.fluid.core.eager.to_uva_tensor( + if base.framework.in_dygraph_mode(): + row = paddle.base.core.eager.to_uva_tensor( self.row.astype(self.row.dtype), 0 ) - sorted_eid = paddle.fluid.core.eager.to_uva_tensor( + sorted_eid = paddle.base.core.eager.to_uva_tensor( self.sorted_eid.astype(self.sorted_eid.dtype), 0 ) else: - row = paddle.fluid.core.to_uva_tensor( + row = paddle.base.core.to_uva_tensor( self.row.astype(self.row.dtype) ) - sorted_eid = paddle.fluid.core.to_uva_tensor( + sorted_eid = paddle.base.core.to_uva_tensor( self.sorted_eid.astype(self.sorted_eid.dtype) ) colptr = paddle.to_tensor(self.colptr) diff --git a/test/legacy_test/test_graph_sample_neighbors.py b/test/legacy_test/test_graph_sample_neighbors.py index 1f3829721e9ea..8616b88374bc4 100644 --- a/test/legacy_test/test_graph_sample_neighbors.py +++ b/test/legacy_test/test_graph_sample_neighbors.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestGraphSampleNeighbors(unittest.TestCase): @@ -81,7 +81,7 @@ def test_sample_result(self): def test_sample_result_fisher_yates_sampling(self): paddle.disable_static() - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): row = paddle.to_tensor(self.row) colptr = paddle.to_tensor(self.colptr) nodes = paddle.to_tensor(self.nodes) @@ -322,7 +322,7 @@ def test_sample_result(self): def test_sample_result_fisher_yates_sampling(self): paddle.disable_static() - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): row = paddle.to_tensor(self.row) colptr = paddle.to_tensor(self.colptr) nodes = paddle.to_tensor(self.nodes) diff --git a/test/legacy_test/test_graph_send_ue_recv_op.py b/test/legacy_test/test_graph_send_ue_recv_op.py index abfabb3870b6f..10ce31514df36 100644 --- a/test/legacy_test/test_graph_send_ue_recv_op.py +++ b/test/legacy_test/test_graph_send_ue_recv_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core def get_broadcast_shape(shp1, shp2): diff --git a/test/legacy_test/test_greater_equal_op.py b/test/legacy_test/test_greater_equal_op.py index 9ef5827e1440c..52b6e24e7d78f 100644 --- a/test/legacy_test/test_greater_equal_op.py +++ b/test/legacy_test/test_greater_equal_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import core +from paddle.base import core class Test_Greater_Equal_Op_Fp16(unittest.TestCase): diff --git a/test/legacy_test/test_grid_sample_function.py b/test/legacy_test/test_grid_sample_function.py index 4f8042c8bad66..fff624c9bd95a 100644 --- a/test/legacy_test/test_grid_sample_function.py +++ b/test/legacy_test/test_grid_sample_function.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class GridSampleTestCase(unittest.TestCase): @@ -46,10 +46,10 @@ def setUp(self): self.grid = np.random.uniform(-1, 1, self.grid_shape).astype(self.dtype) def static_functional(self, place): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data("x", self.x_shape, dtype=self.dtype) grid = paddle.static.data( "grid", self.grid_shape, dtype=self.dtype @@ -62,7 +62,7 @@ def static_functional(self, place): align_corners=self.align_corners, ) feed_dict = {"x": self.x, "grid": self.grid} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -87,17 +87,17 @@ def _test_equivalence(self, place): np.testing.assert_array_almost_equal(result1, result2) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class GridSampleErrorTestCase(GridSampleTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with self.assertRaises(ValueError): self.static_functional(place) diff --git a/test/legacy_test/test_grid_sampler_op.py b/test/legacy_test/test_grid_sampler_op.py index 3670d3dd22e37..cbc40b12f59ba 100644 --- a/test/legacy_test/test_grid_sampler_op.py +++ b/test/legacy_test/test_grid_sampler_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_group_norm_op.py b/test/legacy_test/test_group_norm_op.py index 543d98943c4a0..ef0c030f8f93e 100644 --- a/test/legacy_test/test_group_norm_op.py +++ b/test/legacy_test/test_group_norm_op.py @@ -26,8 +26,8 @@ from testsuite import create_op import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def group_norm_naive(x, scale, bias, epsilon, groups, data_layout): @@ -50,7 +50,7 @@ def group_norm_naive(x, scale, bias, epsilon, groups, data_layout): class TestGroupNormOpError(unittest.TestCase): def test_errors(self): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): def test_x_type(): input = np.random.random(2, 100, 3, 5).astype('float32') @@ -106,9 +106,9 @@ def setUp(self): ) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(input), - 'Scale': OpTest.np_dtype_to_fluid_dtype(scale), - 'Bias': OpTest.np_dtype_to_fluid_dtype(bias), + 'X': OpTest.np_dtype_to_base_dtype(input), + 'Scale': OpTest.np_dtype_to_base_dtype(scale), + 'Bias': OpTest.np_dtype_to_base_dtype(bias), } self.outputs = {'Y': output, 'Mean': mean, 'Variance': var} self.attrs['data_layout'] = self.data_format @@ -474,9 +474,9 @@ def test_case1(self): bias = np.array([0]).astype("float64") place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data1": data1_np, "data2": data2_np}, fetch_list=[out1, out2], return_numpy=True, @@ -526,13 +526,13 @@ def test_dygraph_api(self): self.shape = (8, 32, 32) input = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - tensor_1 = fluid.dygraph.to_variable(input) + with base.dygraph.guard(): + tensor_1 = base.dygraph.to_variable(input) tensor_1.stop_gradient = False groupNorm = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret1 = groupNorm(tensor_1) ret1.backward() - tensor_eager_1 = fluid.dygraph.to_variable(input) + tensor_eager_1 = base.dygraph.to_variable(input) tensor_eager_1.stop_gradient = False groupNorm_eager = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret2 = groupNorm_eager(tensor_eager_1) @@ -546,13 +546,13 @@ def test_dygraph_api(self): self.shape = (8, 32, 32) input = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - tensor_1 = fluid.dygraph.to_variable(input) + with base.dygraph.guard(): + tensor_1 = base.dygraph.to_variable(input) tensor_1.stop_gradient = False groupNorm = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret1 = groupNorm(tensor_1) ret1.backward() - tensor_eager_1 = fluid.dygraph.to_variable(input) + tensor_eager_1 = base.dygraph.to_variable(input) tensor_eager_1.stop_gradient = False groupNorm_eager = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret2 = groupNorm_eager(tensor_eager_1) @@ -572,13 +572,13 @@ def test_dygraph_api(self): self.shape = (8, 32, 32) input = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - tensor_1 = fluid.dygraph.to_variable(input) + with base.dygraph.guard(): + tensor_1 = base.dygraph.to_variable(input) tensor_1.stop_gradient = False groupNorm = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret1 = groupNorm(tensor_1) ret1.backward() - tensor_eager_1 = fluid.dygraph.to_variable(input) + tensor_eager_1 = base.dygraph.to_variable(input) tensor_eager_1.stop_gradient = False groupNorm_eager = paddle.nn.GroupNorm(num_channels=32, num_groups=4) ret2 = groupNorm_eager(tensor_eager_1) @@ -1024,9 +1024,9 @@ def setUp(self): self.static_rev_desire[-1].append(rev[2]) def get_eager_desire(self, place): - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") core.set_prim_eager_enabled(False) paddle.disable_static() @@ -1061,9 +1061,9 @@ def get_eager_desire(self, place): def get_static_desire(self, place): core._set_prim_all_enabled(False) paddle.enable_static() - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") mp, sp = paddle.static.Program(), paddle.static.Program() @@ -1154,7 +1154,7 @@ def test_static_comp(self): if len(self.places) < 1: return - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): for place in self.places: fwd_actual.append([]) rev_actual.append([]) @@ -1394,7 +1394,7 @@ def test_jit_comp_with_cinn(self): fwd_actual = [] rev_actual = [] for place in self.places: - if not isinstance(place, fluid.CUDAPlace): + if not isinstance(place, base.CUDAPlace): continue input_ = paddle.to_tensor( data=self.x, dtype=self.dtype, place=place, stop_gradient=False @@ -1436,7 +1436,7 @@ def test_jit_comp_with_cinn(self): i = 0 for place in self.places: - if not isinstance(place, fluid.CUDAPlace): + if not isinstance(place, base.CUDAPlace): continue atol = self.threshold_list[i][2] rtol = self.threshold_list[i][2] diff --git a/test/legacy_test/test_group_norm_op_v2.py b/test/legacy_test/test_group_norm_op_v2.py index 0b3beee6797d7..94232e7f70a00 100644 --- a/test/legacy_test/test_group_norm_op_v2.py +++ b/test/legacy_test/test_group_norm_op_v2.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def group_norm_naive_for_general_dimension(x, scale, bias, epsilon, groups): @@ -48,9 +48,9 @@ def test_numerical_accuracy(self): (2, 6, 6, 6, 2, 3), ] np.random.seed(10) - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("group_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: for shape in shapes: @@ -87,9 +87,9 @@ def test_numerical_accuracy(self): (2, 6, 6, 6, 256, 3), ] np.random.seed(10) - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("group_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: for shape in shapes: diff --git a/test/legacy_test/test_gru_rnn_op.py b/test/legacy_test/test_gru_rnn_op.py index 450cb89dbd220..4a4420a8c335d 100644 --- a/test/legacy_test/test_gru_rnn_op.py +++ b/test/legacy_test/test_gru_rnn_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core sys.path.append("../../test/rnn") from convert import get_params_for_net diff --git a/test/legacy_test/test_gru_unit_op.py b/test/legacy_test/test_gru_unit_op.py index f62163aaf56af..1359583ae1ade 100644 --- a/test/legacy_test/test_gru_unit_op.py +++ b/test/legacy_test/test_gru_unit_op.py @@ -18,7 +18,7 @@ import numpy as np from eager_op_test import OpTest -from paddle import fluid +from paddle import base class GRUActivationType(OpTest): @@ -118,7 +118,7 @@ def set_outputs(self, origin_mode=False): def setUp(self): self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.set_inputs() self.set_outputs() @@ -136,7 +136,7 @@ def test_check_grad(self): class TestGRUUnitOpOriginMode(TestGRUUnitOp): def setUp(self): self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.set_inputs(origin_mode=True) self.set_outputs(origin_mode=True) @@ -176,7 +176,7 @@ def test_check_grad_ingore_input(self): class TestGRUUnitOpWithBiasOriginMode(TestGRUUnitOpWithBias): def setUp(self): self.dtype = ( - 'float32' if fluid.core.is_compiled_with_rocm() else 'float64' + 'float32' if base.core.is_compiled_with_rocm() else 'float64' ) self.set_inputs(origin_mode=True) self.set_outputs(origin_mode=True) diff --git a/test/legacy_test/test_gumbel_softmax_op.py b/test/legacy_test/test_gumbel_softmax_op.py index c0396a7a02929..608b73739c902 100644 --- a/test/legacy_test/test_gumbel_softmax_op.py +++ b/test/legacy_test/test_gumbel_softmax_op.py @@ -17,7 +17,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base paddle.enable_static() @@ -42,7 +42,7 @@ def setUp(self): np.random.seed(0) x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.zeros(self.shape).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def test_check_output(self): @@ -172,7 +172,7 @@ def setUp(self): batch_x = np.ones(self.shape) * single_x out = np.zeros(self.shape).astype(self.dtype) self.probs = self.softmax(single_x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(batch_x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(batch_x)} self.outputs = {'Out': out} def test_check_output(self): @@ -228,7 +228,7 @@ def setUp(self): self.count_expected = 24 self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) @@ -243,7 +243,7 @@ def test_check_api(self): self.assertEqual(out_np.sum(), self.count_expected) # test dygrapg api - with paddle.fluid.dygraph.base.guard(): + with paddle.base.dygraph.base.guard(): x = paddle.to_tensor(self.x) y = paddle.nn.functional.gumbel_softmax(x, hard=True) out_np = np.array(y) @@ -255,8 +255,8 @@ def test_errors(self): paddle.disable_static() def test_Variable(): - x1 = fluid.create_lod_tensor( - np.zeros((100, 784)), [[10, 10, 10, 70]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.zeros((100, 784)), [[10, 10, 10, 70]], base.CPUPlace() ) paddle.nn.functional.gumbel_softmax(x1) diff --git a/test/legacy_test/test_hapi_amp.py b/test/legacy_test/test_hapi_amp.py index 654f1351f3661..5f91b9a86baf4 100644 --- a/test/legacy_test/test_hapi_amp.py +++ b/test/legacy_test/test_hapi_amp.py @@ -23,7 +23,7 @@ import paddle import paddle.vision.transforms as T -from paddle import Model, fluid +from paddle import Model, base from paddle.nn.layer.loss import CrossEntropyLoss from paddle.static import InputSpec from paddle.vision.datasets import MNIST @@ -31,7 +31,7 @@ @unittest.skipIf( - not fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestHapiWithAmp(unittest.TestCase): def get_model(self, amp_config): @@ -100,7 +100,7 @@ def test_save_load(self): lenet_amp_path = os.path.join(temp_dir.name, './lenet_amp') model.save(lenet_amp_path) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): paddle.seed(2021) new_model = self.get_model(amp_level) train_dataset = MNIST(mode='train', transform=transform) @@ -143,7 +143,7 @@ def test_dynamic_check_input(self): {"level": "O1", "use_fp16_guard": True}, "O3", ] - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): self.skipTest('module not tested when ONLY_CPU compling') paddle.set_device('gpu') net = LeNet() @@ -170,7 +170,7 @@ def test_dynamic_check_input(self): def test_static_check_input(self): paddle.enable_static() amp_configs = {"level": "O2", "use_pure_fp16": True} - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): self.skipTest('module not tested when ONLY_CPU compling') paddle.set_device('gpu') diff --git a/test/legacy_test/test_histogram_op.py b/test/legacy_test/test_histogram_op.py index 36df3209ac76e..8ca5b4dd500da 100644 --- a/test/legacy_test/test_histogram_op.py +++ b/test/legacy_test/test_histogram_op.py @@ -18,25 +18,25 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard class TestHistogramOpAPI(unittest.TestCase): """Test histogram api.""" def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): inputs = paddle.static.data( name='input', dtype='int64', shape=[2, 3] ) output = paddle.histogram(inputs, bins=5, min=1, max=5) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[2, 4, 2], [2, 5, 4]]).astype(np.int64) res = exe.run( @@ -50,9 +50,9 @@ def test_static_graph(self): ) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): inputs_np = np.array([[2, 4, 2], [2, 5, 4]]).astype(np.int64) - inputs = fluid.dygraph.to_variable(inputs_np) + inputs = base.dygraph.to_variable(inputs_np) actual = paddle.histogram(inputs, bins=5, min=1, max=5) expected = np.array([0, 3, 0, 2, 1]).astype(np.int64) self.assertTrue( @@ -73,11 +73,11 @@ class TestHistogramOpError(unittest.TestCase): """Test histogram op error.""" def run_network(self, net_func): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): net_func() - exe = fluid.Executor() + exe = base.Executor() exe.run(main_program) def test_bins_error(self): diff --git a/test/legacy_test/test_host_memory_stats.py b/test/legacy_test/test_host_memory_stats.py index a1ae645244094..35da81454dba2 100644 --- a/test/legacy_test/test_host_memory_stats.py +++ b/test/legacy_test/test_host_memory_stats.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core paddle.set_device('cpu') diff --git a/test/legacy_test/test_hsigmoid_op.py b/test/legacy_test/test_hsigmoid_op.py index b9c4338fa4c05..6144285586055 100644 --- a/test/legacy_test/test_hsigmoid_op.py +++ b/test/legacy_test/test_hsigmoid_op.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base paddle.enable_static() np.random.seed(100) @@ -298,7 +298,7 @@ def hs_net_conf(self, is_sparse): input=input_word, is_sparse=is_sparse, size=[3, 3], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Normal(std=1 / math.sqrt(3)) ), ) @@ -323,9 +323,9 @@ def hs_net_conf(self, is_sparse): return avg_cost, data_list def training_test(self, is_sparse): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle.seed(1) - start_up = fluid.default_startup_program() + start_up = base.default_startup_program() x = np.arange(6).reshape(6) path_table = np.array([(1, 2, -1), (1, 2, -1)]).astype('int64') path_code = np.array([(1, 0, -1), (0, 0, -1)]).astype('int64') @@ -335,10 +335,10 @@ def training_test(self, is_sparse): optimizer = paddle.optimizer.SGD(learning_rate=1e-3) optimizer.minimize(loss) - main_program = fluid.default_main_program() - place = fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=data_list, place=place) - exe = fluid.Executor(place) + main_program = base.default_main_program() + place = base.CPUPlace() + feeder = base.DataFeeder(feed_list=data_list, place=place) + exe = base.Executor(place) exe.run(start_up) result = [] @@ -619,10 +619,10 @@ def test_static_api(self): for ret in [ret1, ret2]: np.testing.assert_allclose(self.out_np, ret, rtol=1e-05) - def test_fluid_api(self): - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + def test_base_api(self): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): x = paddle.static.data('x', [-1, self.feature_size]) labels = paddle.static.data('labels', [-1, 1], 'int64') path_table = None @@ -647,7 +647,7 @@ def test_fluid_api(self): path_code=path_code, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup_program) feed_dict = {'x': self.x_np, 'labels': self.labels_np} if self.is_custom: diff --git a/test/legacy_test/test_huber_loss_op.py b/test/legacy_test/test_huber_loss_op.py index 318e2bf68fc32..5b74de590dab9 100644 --- a/test/legacy_test/test_huber_loss_op.py +++ b/test/legacy_test/test_huber_loss_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def huber_loss_forward(val, delta): diff --git a/test/legacy_test/test_i0_op.py b/test/legacy_test/test_i0_op.py index a30f67312c8d0..6019a868cf0d2 100644 --- a/test/legacy_test/test_i0_op.py +++ b/test/legacy_test/test_i0_op.py @@ -19,7 +19,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(100) paddle.seed(100) diff --git a/test/legacy_test/test_i0e_op.py b/test/legacy_test/test_i0e_op.py index 0543d53effec8..8a3df4ab77afd 100644 --- a/test/legacy_test/test_i0e_op.py +++ b/test/legacy_test/test_i0e_op.py @@ -19,7 +19,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(100) paddle.seed(100) diff --git a/test/legacy_test/test_i1_op.py b/test/legacy_test/test_i1_op.py index b5846d6e15647..d4ab42cd4b4fa 100644 --- a/test/legacy_test/test_i1_op.py +++ b/test/legacy_test/test_i1_op.py @@ -19,7 +19,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(42) paddle.seed(42) diff --git a/test/legacy_test/test_i1e_op.py b/test/legacy_test/test_i1e_op.py index a4c360ae9a36f..49379918b2666 100644 --- a/test/legacy_test/test_i1e_op.py +++ b/test/legacy_test/test_i1e_op.py @@ -19,7 +19,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(42) paddle.seed(42) diff --git a/test/legacy_test/test_identity_loss_op.py b/test/legacy_test/test_identity_loss_op.py index ce8fa7dbfd825..6fc0ce658ff41 100644 --- a/test/legacy_test/test_identity_loss_op.py +++ b/test/legacy_test/test_identity_loss_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard class TestIdentityLossOp(OpTest): @@ -114,7 +114,7 @@ class TestIdentityLossAPI(unittest.TestCase): def setUp(self): self.x_shape = [2, 3, 4, 5] self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32) - self.place = fluid.CPUPlace() + self.place = base.CPUPlace() def identity_loss_ref(self, input, reduction): if reduction == 0 or reduction == "sum": diff --git a/test/legacy_test/test_image_classification_layer.py b/test/legacy_test/test_image_classification_layer.py index 4abb4312eb61b..75ca22fb28c97 100644 --- a/test/legacy_test/test_image_classification_layer.py +++ b/test/legacy_test/test_image_classification_layer.py @@ -17,8 +17,8 @@ import nets import paddle -from paddle import fluid -from paddle.fluid.framework import Program +from paddle import base +from paddle.base.framework import Program def conv_block(input, num_filter, groups, dropouts): @@ -39,7 +39,7 @@ class TestLayer(unittest.TestCase): def test_batch_norm_layer(self): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): images = paddle.static.data( name='pixel', shape=[-1, 3, 48, 48], dtype='float32' ) @@ -54,7 +54,7 @@ def test_batch_norm_layer(self): def test_dropout_layer(self): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): images = paddle.static.data( name='pixel', shape=[-1, 3, 48, 48], dtype='float32' ) @@ -66,7 +66,7 @@ def test_img_conv_group(self): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): images = paddle.static.data( name='pixel', shape=[-1, 3, 48, 48], dtype='float32' ) @@ -78,7 +78,7 @@ def test_img_conv_group(self): def test_elementwise_add_with_act(self): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): image1 = paddle.static.data( name='pixel1', shape=[-1, 3, 48, 48], dtype='float32' ) diff --git a/test/legacy_test/test_imperative_auto_prune.py b/test/legacy_test/test_imperative_auto_prune.py index 4491bf705a3d5..59f8da20287ce 100644 --- a/test/legacy_test/test_imperative_auto_prune.py +++ b/test/legacy_test/test_imperative_auto_prune.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn import Embedding from paddle.tensor import random @@ -167,24 +167,24 @@ def embed_linear0(self, x): class TestImperativeAutoPrune(unittest.TestCase): def test_auto_prune(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): case1 = AutoPruneLayer0(input_size=5) value1 = np.arange(25).reshape(5, 5).astype("float32") value2 = np.arange(25).reshape(5, 5).astype("float32") - v1 = fluid.dygraph.to_variable(value1) - v2 = fluid.dygraph.to_variable(value2) + v1 = base.dygraph.to_variable(value1) + v2 = base.dygraph.to_variable(value2) loss = case1(v1, v2) loss.backward() self.assertIsNotNone(case1.linear2.weight._grad_ivar()) self.assertIsNotNone(case1.linear1.weight._grad_ivar()) def test_auto_prune2(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): case2 = AutoPruneLayer1(input_size=5) value1 = np.arange(25).reshape(5, 5).astype("float32") value2 = np.arange(25).reshape(5, 5).astype("float32") - v1 = fluid.dygraph.to_variable(value1) - v2 = fluid.dygraph.to_variable(value2) + v1 = base.dygraph.to_variable(value1) + v2 = base.dygraph.to_variable(value2) loss = case2(v1, v2) loss.backward() @@ -193,12 +193,12 @@ def test_auto_prune2(self): # TODO(jiabin): Support this when we support better split tensor def test_auto_prune3(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): case3 = AutoPruneLayer3(input_size=784) value1 = np.arange(784).reshape(1, 784).astype("float32") value2 = np.arange(1).reshape(1, 1).astype("int64") - v1 = fluid.dygraph.to_variable(value1) - v2 = fluid.dygraph.to_variable(value2) + v1 = base.dygraph.to_variable(value1) + v2 = base.dygraph.to_variable(value2) loss, part2 = case3(v1, v2, 1) part2.retain_grads() loss.backward() @@ -206,12 +206,12 @@ def test_auto_prune3(self): self.assertTrue((part2.gradient() == 0).all()) def test_auto_prune4(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): case4 = AutoPruneLayer3(input_size=784) value1 = np.arange(784).reshape(1, 784).astype("float32") value2 = np.arange(1).reshape(1, 1).astype("int64") - v1 = fluid.dygraph.to_variable(value1) - v2 = fluid.dygraph.to_variable(value2) + v1 = base.dygraph.to_variable(value1) + v2 = base.dygraph.to_variable(value2) loss, part2 = case4(v1, v2, 1) part2.retain_grads() part2.backward() @@ -219,12 +219,12 @@ def test_auto_prune4(self): self.assertTrue((part2.gradient() == 1).all()) def test_auto_prune5(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): case4 = AutoPruneLayer3(input_size=784) value1 = np.arange(784).reshape(1, 784).astype("float32") value2 = np.arange(1).reshape(1, 1).astype("int64") - v1 = fluid.dygraph.to_variable(value1) - v2 = fluid.dygraph.to_variable(value2) + v1 = base.dygraph.to_variable(value1) + v2 = base.dygraph.to_variable(value2) loss, part1, part2 = case4(v1, v2, 2) part2.retain_grads() part1.backward() @@ -232,15 +232,15 @@ def test_auto_prune5(self): self.assertTrue((part2.gradient() == 0).all()) def test_auto_prune6(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = paddle.nn.Linear(13, 5) linear2 = paddle.nn.Linear(3, 3) - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) out2 = linear2(b) out1.stop_gradient = True @@ -250,15 +250,15 @@ def test_auto_prune6(self): self.assertIsNone(out1.gradient()) def test_auto_prune7(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = paddle.nn.Linear(13, 5) linear2 = paddle.nn.Linear(3, 3) - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) out2 = linear2(b) out1.stop_gradient = True @@ -268,15 +268,15 @@ def test_auto_prune7(self): self.assertIsNone(out1.gradient()) def test_auto_prune8(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = paddle.nn.Linear(13, 5) linear2 = paddle.nn.Linear(5, 3) - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) linear_origin = linear.weight.numpy() out2 = linear2(out1) @@ -296,15 +296,15 @@ def test_auto_prune8(self): ) def test_auto_prune9(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = paddle.nn.Linear(13, 5) linear2 = paddle.nn.Linear(5, 3) - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) linear_origin = linear.weight.numpy() out2 = linear2(out1) @@ -326,21 +326,21 @@ def test_auto_prune9(self): assert type(e) == ValueError def test_auto_prune10(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value0 = np.arange(26).reshape(2, 13).astype("float32") value1 = np.arange(6).reshape(2, 3).astype("float32") value2 = np.arange(10).reshape(2, 5).astype("float32") linear = paddle.nn.Linear(13, 5) linear2 = paddle.nn.Linear(3, 3) - a = fluid.dygraph.to_variable(value0) - b = fluid.dygraph.to_variable(value1) - c = fluid.dygraph.to_variable(value2) + a = base.dygraph.to_variable(value0) + b = base.dygraph.to_variable(value1) + c = base.dygraph.to_variable(value2) out1 = linear(a) out2 = linear2(b) out1.stop_gradient = True out = paddle.concat([out1, out2, c], axis=1) - # TODO(jiabin): In Eager Mode we don't actually need sort_sum_gradient, this test should be removed when we don't support fluid anymore. - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + # TODO(jiabin): In Eager Mode we don't actually need sort_sum_gradient, this test should be removed when we don't support base anymore. + base.set_flags({'FLAGS_sort_sum_gradient': True}) out.backward() self.assertIsNone(linear.weight.gradient()) self.assertIsNone(out1.gradient()) @@ -355,15 +355,15 @@ def test_auto_prune_with_optimizer(self): ).astype("int64") embed = np.random.randn(batch_size, size).astype("float32") - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): model = MyLayer(size, vocab_size, size) grad_clip = paddle.nn.ClipGradByGlobalNorm(0.001) optimizer = paddle.optimizer.Adam( 0.001, parameters=model.parameters(), grad_clip=grad_clip ) - indices = fluid.dygraph.to_variable(indices) - embed = fluid.dygraph.to_variable(embed) + indices = base.dygraph.to_variable(indices) + embed = base.dygraph.to_variable(embed) dummy_loss = model(embed) loss = model.embed_linear0(indices) @@ -375,15 +375,15 @@ def test_auto_prune_with_optimizer(self): assert model.embed1.weight._grad_ivar() is None assert model.linear_1.weight._grad_ivar() is None - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): model = MyLayer2(size, vocab_size, size) grad_clip = paddle.nn.ClipGradByGlobalNorm(0.001) optimizer = paddle.optimizer.Adam( 0.001, parameters=model.parameters(), grad_clip=grad_clip ) - indices = fluid.dygraph.to_variable(indices) - emebd = fluid.dygraph.to_variable(embed) + indices = base.dygraph.to_variable(indices) + emebd = base.dygraph.to_variable(embed) dummy_loss = model(indices) loss = model.embed_linear0(indices) @@ -396,11 +396,11 @@ def test_auto_prune_with_optimizer(self): assert model.linear_1.weight._grad_ivar() is None def test_case2_prune_no_grad_branch(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value1 = np.arange(784).reshape(1, 784) value2 = np.arange(1).reshape(1, 1) - v1 = fluid.dygraph.to_variable(value1).astype("float32") - v2 = fluid.dygraph.to_variable(value2).astype("float32") + v1 = base.dygraph.to_variable(value1).astype("float32") + v2 = base.dygraph.to_variable(value2).astype("float32") case3 = AutoPruneLayer2(input_size=784) loss = case3(v1, v2) loss.backward() @@ -408,10 +408,10 @@ def test_case2_prune_no_grad_branch(self): self.assertIsNotNone(case3.linear.weight._grad_ivar()) def test_case3_prune_no_grad_branch2(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): value1 = np.arange(1).reshape(1, 1) linear = paddle.nn.Linear(1, 1) - label = fluid.dygraph.to_variable(value1).astype("float32") + label = base.dygraph.to_variable(value1).astype("float32") label = linear(label) label = paddle.cast(label, dtype="float32") label = paddle.cast(label, dtype='int64') @@ -421,7 +421,7 @@ def test_case3_prune_no_grad_branch2(self): self.assertIsNone(linear.weight._grad_ivar()) def test_case4_with_no_grad_op_maker(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out = random.gaussian(shape=[20, 30]) loss = paddle.mean(out) loss.backward() diff --git a/test/legacy_test/test_imperative_base.py b/test/legacy_test/test_imperative_base.py index 9cd0a61bf35aa..f3a2d0dc503ff 100644 --- a/test/legacy_test/test_imperative_base.py +++ b/test/legacy_test/test_imperative_base.py @@ -14,15 +14,15 @@ import contextlib -from paddle import fluid +from paddle import base @contextlib.contextmanager def new_program_scope(main=None, startup=None, scope=None): - prog = main if main else fluid.Program() - startup_prog = startup if startup else fluid.Program() - scope = scope if scope else fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): - with fluid.unique_name.guard(): + prog = main if main else base.Program() + startup_prog = startup if startup else base.Program() + scope = scope if scope else base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): + with base.unique_name.guard(): yield diff --git a/test/legacy_test/test_imperative_container_layerlist.py b/test/legacy_test/test_imperative_container_layerlist.py index d920d2705bfeb..371e46ce1849d 100644 --- a/test/legacy_test/test_imperative_container_layerlist.py +++ b/test/legacy_test/test_imperative_container_layerlist.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class MyLayer(paddle.nn.Layer): @@ -37,10 +37,10 @@ def paddle_imperative_list(self): [paddle.nn.Linear(2**i, 2 ** (i + 1)) for i in range(6)] ) - def layer_list(self, use_fluid_api): + def layer_list(self, use_base_api): data_np = np.random.uniform(-1, 1, [5, 1]).astype('float32') - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(data_np) + with base.dygraph.guard(): + x = base.dygraph.to_variable(data_np) layerlist = self.paddle_imperative_list() size = len(layerlist) diff --git a/test/legacy_test/test_imperative_container_parameterlist.py b/test/legacy_test/test_imperative_container_parameterlist.py index c6304f6a55e64..87cf1a6a2c8ef 100644 --- a/test/legacy_test/test_imperative_container_parameterlist.py +++ b/test/legacy_test/test_imperative_container_parameterlist.py @@ -17,11 +17,11 @@ import numpy as np import paddle -from paddle import _legacy_C_ops, fluid +from paddle import _legacy_C_ops, base class MyLayer(paddle.nn.Layer): - def __init__(self, num_stacked_param, use_fluid_api): + def __init__(self, num_stacked_param, use_base_api): super().__init__() # create ParameterList with iterable Parameters self.params = self.paddle_imperative_ParameterList(num_stacked_param) @@ -39,12 +39,12 @@ def forward(self, x): class TestImperativeContainerParameterList(unittest.TestCase): - def paramter_list(self, use_fluid_api): + def paramter_list(self, use_base_api): data_np = np.random.uniform(-1, 1, [5, 2]).astype('float32') - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(data_np) + with base.dygraph.guard(): + x = base.dygraph.to_variable(data_np) num_stacked_param = 4 - model = MyLayer(num_stacked_param, use_fluid_api) + model = MyLayer(num_stacked_param, use_base_api) self.assertEqual(len(model.params), num_stacked_param) res = model(x) self.assertListEqual(res.shape, [5, 2]) diff --git a/test/legacy_test/test_imperative_container_sequential.py b/test/legacy_test/test_imperative_container_sequential.py index d0ea6bd79a7fb..834fce4af4d78 100644 --- a/test/legacy_test/test_imperative_container_sequential.py +++ b/test/legacy_test/test_imperative_container_sequential.py @@ -17,15 +17,15 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn import Linear class TestImperativeContainerSequential(unittest.TestCase): def test_sequential(self): data = np.random.uniform(-1, 1, [5, 10]).astype('float32') - with fluid.dygraph.guard(): - data = fluid.dygraph.to_variable(data) + with base.dygraph.guard(): + data = base.dygraph.to_variable(data) model1 = paddle.nn.Sequential(Linear(10, 1), Linear(1, 2)) res1 = model1(data) self.assertListEqual(res1.shape, [5, 2]) @@ -58,8 +58,8 @@ def test_sequential(self): def test_sequential_list_params(self): data = np.random.uniform(-1, 1, [5, 10]).astype('float32') - with fluid.dygraph.guard(): - data = fluid.dygraph.to_variable(data) + with base.dygraph.guard(): + data = base.dygraph.to_variable(data) model1 = paddle.nn.Sequential(Linear(10, 1), Linear(1, 2)) res1 = model1(data) self.assertListEqual(res1.shape, [5, 2]) diff --git a/test/legacy_test/test_imperative_data_loader_base.py b/test/legacy_test/test_imperative_data_loader_base.py index 6104ebbb67485..11d0524744b2c 100644 --- a/test/legacy_test/test_imperative_data_loader_base.py +++ b/test/legacy_test/test_imperative_data_loader_base.py @@ -17,8 +17,8 @@ import numpy as np import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.reader import use_pinned_memory +from paddle import base +from paddle.base.reader import use_pinned_memory def get_random_images_and_labels(image_shape, label_shape): @@ -52,32 +52,32 @@ def iter_loader_data(self, loader): self.assertEqual(relu.shape, [self.batch_size, 784]) def test_single_process_loader(self): - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.capacity, iterable=False, use_multiprocess=False ) loader.set_sample_generator( sample_generator_creator(self.batch_size, self.batch_num), batch_size=self.batch_size, - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) self.iter_loader_data(loader) def test_multi_process_loader(self): - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.capacity, use_multiprocess=True ) loader.set_sample_generator( sample_generator_creator(self.batch_size, self.batch_num), batch_size=self.batch_size, - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) self.iter_loader_data(loader) def test_generator_no_places(self): - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator(capacity=self.capacity) + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator(capacity=self.capacity) loader.set_sample_generator( sample_generator_creator(self.batch_size, self.batch_num), batch_size=self.batch_size, @@ -85,15 +85,15 @@ def test_generator_no_places(self): self.iter_loader_data(loader) def test_set_pin_memory(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): use_pinned_memory(False) - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( capacity=self.capacity, iterable=False, use_multiprocess=False ) loader.set_sample_generator( sample_generator_creator(self.batch_size, self.batch_num), batch_size=self.batch_size, - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) self.iter_loader_data(loader) use_pinned_memory(True) diff --git a/test/legacy_test/test_imperative_data_loader_exception.py b/test/legacy_test/test_imperative_data_loader_exception.py index 22717c0f03a25..af196e33d2cdb 100644 --- a/test/legacy_test/test_imperative_data_loader_exception.py +++ b/test/legacy_test/test_imperative_data_loader_exception.py @@ -18,8 +18,8 @@ import numpy as np import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def get_random_images_and_labels(image_shape, label_shape): @@ -36,11 +36,11 @@ def setUp(self): self.capacity = 5 def test_not_capacity(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): with self.assertRaisesRegex( ValueError, "Please give value to capacity." ): - fluid.io.DataLoader.from_generator() + base.io.DataLoader.from_generator() def test_single_process_with_thread_expection(self): def error_sample_genarator(batch_num): @@ -50,12 +50,12 @@ def __reader__(): return __reader__ - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.capacity, iterable=False, use_multiprocess=False ) loader.set_batch_generator( - error_sample_genarator(self.batch_num), places=fluid.CPUPlace() + error_sample_genarator(self.batch_num), places=base.CPUPlace() ) exception = None try: @@ -74,12 +74,12 @@ def __reader__(): return __reader__ - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.capacity, use_multiprocess=True ) loader.set_batch_generator( - error_sample_genarator(self.batch_num), places=fluid.CPUPlace() + error_sample_genarator(self.batch_num), places=base.CPUPlace() ) exception = None try: @@ -101,13 +101,13 @@ def __reader__(): return __reader__ - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.capacity, use_multiprocess=True ) loader.set_batch_generator( slow_batch_generator_creator(self.batch_size, self.batch_num), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) exception = None try: diff --git a/test/legacy_test/test_imperative_data_loader_exit_func.py b/test/legacy_test/test_imperative_data_loader_exit_func.py index 7dd95299639e4..82505099a4b0f 100644 --- a/test/legacy_test/test_imperative_data_loader_exit_func.py +++ b/test/legacy_test/test_imperative_data_loader_exit_func.py @@ -18,7 +18,7 @@ import time import unittest -from paddle.fluid.reader import ( +from paddle.base.reader import ( CleanupFuncRegistrar, _cleanup, multiprocess_queue_set, diff --git a/test/legacy_test/test_imperative_data_loader_fds_clear.py b/test/legacy_test/test_imperative_data_loader_fds_clear.py index 5950ecb58235d..31b2306761af4 100644 --- a/test/legacy_test/test_imperative_data_loader_fds_clear.py +++ b/test/legacy_test/test_imperative_data_loader_fds_clear.py @@ -17,7 +17,7 @@ import numpy as np import paddle.nn.functional as F -from paddle import fluid +from paddle import base from paddle.io import DataLoader, Dataset @@ -60,12 +60,12 @@ def setUp(self): self.capacity = 50 def prepare_data_loader(self): - loader = fluid.io.DataLoader.from_generator( + loader = base.io.DataLoader.from_generator( capacity=self.capacity, use_multiprocess=True ) loader.set_batch_generator( batch_generator_creator(self.batch_size, self.batch_num), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) return loader @@ -80,14 +80,14 @@ def run_one_epoch_with_break(self, loader): break def test_data_loader_break(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): loader = self.prepare_data_loader() for _ in range(self.epoch_num): self.run_one_epoch_with_break(loader) break def test_data_loader_continue_break(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): loader = self.prepare_data_loader() for _ in range(self.epoch_num): self.run_one_epoch_with_break(loader) @@ -95,8 +95,8 @@ def test_data_loader_continue_break(self): class TestMultiProcessDataLoaderMmapFdsClear(TestDygraphDataLoaderMmapFdsClear): def prepare_data_loader(self): - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): dataset = RandomDataset(self.batch_size * self.batch_num) loader = DataLoader( dataset, diff --git a/test/legacy_test/test_imperative_data_loader_process.py b/test/legacy_test/test_imperative_data_loader_process.py index c610050acfd7b..f1fbc1471a0b4 100644 --- a/test/legacy_test/test_imperative_data_loader_process.py +++ b/test/legacy_test/test_imperative_data_loader_process.py @@ -18,8 +18,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid.reader import _reader_process_loop +from paddle import base +from paddle.base.reader import _reader_process_loop def get_random_images_and_labels(image_shape, label_shape): @@ -57,13 +57,13 @@ def __clear_process__(util_queue): except queue.Empty: break - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.batch_num + 1, use_multiprocess=True ) loader.set_batch_generator( batch_generator_creator(self.batch_size, self.batch_num), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) loader._data_queue = queue.Queue(self.batch_num + 1) _reader_process_loop(loader._batch_reader, loader._data_queue) @@ -87,12 +87,12 @@ def __reader__(): return __reader__ - with fluid.dygraph.guard(): - loader = fluid.io.DataLoader.from_generator( + with base.dygraph.guard(): + loader = base.io.DataLoader.from_generator( capacity=self.batch_num + 1, use_multiprocess=True ) loader.set_batch_generator( - none_sample_genarator(self.batch_num), places=fluid.CPUPlace() + none_sample_genarator(self.batch_num), places=base.CPUPlace() ) loader._data_queue = queue.Queue(self.batch_num + 1) exception = None diff --git a/test/legacy_test/test_imperative_data_parallel.py b/test/legacy_test/test_imperative_data_parallel.py index c9ee7f1342cc8..7184a9e640357 100644 --- a/test/legacy_test/test_imperative_data_parallel.py +++ b/test/legacy_test/test_imperative_data_parallel.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn import Linear @@ -37,7 +37,7 @@ def forward(self, inputs): class TestDataParallelStateDict(unittest.TestCase): def test_data_parallel_state_dict(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.distributed.init_parallel_env() mlp = MLP() parallel_mlp = paddle.DataParallel(mlp) @@ -47,9 +47,9 @@ def test_data_parallel_state_dict(self): base_para = {} place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) for k, v in single_state.items(): self.assertTrue(k in parallel_state) diff --git a/test/legacy_test/test_imperative_decorator.py b/test/legacy_test/test_imperative_decorator.py index d02819ff2937c..6e4eb4af3cb14 100644 --- a/test/legacy_test/test_imperative_decorator.py +++ b/test/legacy_test/test_imperative_decorator.py @@ -18,8 +18,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework class TestTracerMode(unittest.TestCase): @@ -29,7 +29,7 @@ def setUp(self): def get_tracer_mode(self): assert framework.in_dygraph_mode(), "Dygraph mode must be enabled" - @fluid.dygraph.no_grad + @base.dygraph.no_grad def no_grad_func(self, a): self.assertEqual(self.tracer._has_grad, False) return a @@ -47,7 +47,7 @@ def check_not_support_rlt(self, ans): self.assertEqual(rlt, ans) def test_main(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.tracer = framework._dygraph_tracer() self.tracer._train_mode = self.init_mode @@ -57,7 +57,7 @@ def test_main(self): def need_no_grad_func(a, b=1): return a + b - decorated_func = fluid.dygraph.no_grad(need_no_grad_func) + decorated_func = base.dygraph.no_grad(need_no_grad_func) self.assertTrue( str(inspect.getfullargspec(decorated_func)) == str(inspect.getfullargspec(need_no_grad_func)) @@ -65,7 +65,7 @@ def need_no_grad_func(a, b=1): self.assertEqual(self.tracer._train_mode, self.init_mode) - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.check_not_support_rlt(False) paddle.enable_static() diff --git a/test/legacy_test/test_imperative_deepcf.py b/test/legacy_test/test_imperative_deepcf.py index 7827e40974658..e80f9d2974dd8 100644 --- a/test/legacy_test/test_imperative_deepcf.py +++ b/test/legacy_test/test_imperative_deepcf.py @@ -21,9 +21,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear @@ -117,7 +117,7 @@ def __init__(self, num_users, num_items, matrix): self._num_users = num_users self._num_items = num_items self._rating_matrix = self.create_parameter( - attr=fluid.ParamAttr(trainable=False), + attr=base.ParamAttr(trainable=False), shape=matrix.shape, dtype=matrix.dtype, is_bias=False, @@ -257,10 +257,10 @@ def test_deefcf(self): ) = self.get_data() paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - startup = fluid.Program() - main = fluid.Program() + startup = base.Program() + main = base.Program() - scope = fluid.core.Scope() + scope = base.core.Scope() with new_program_scope(main=main, startup=startup, scope=scope): users = paddle.static.data('users', [-1, 1], dtype='int32') items = paddle.static.data('items', [-1, 1], dtype='int32') @@ -272,10 +272,10 @@ def test_deefcf(self): adam = paddle.optimizer.Adam(0.01) adam.minimize(loss) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) exe.run(startup) for e in range(self.num_epoches): @@ -302,7 +302,7 @@ def test_deefcf(self): )[0] sys.stderr.write('static loss %s\n' % static_loss) - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -333,13 +333,13 @@ def test_deefcf(self): dy_loss = loss.numpy() sys.stderr.write(f'dynamic loss: {slice} {dy_loss}\n') - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) deepcf2 = DeepCF(num_users, num_items, matrix) adam2 = paddle.optimizer.Adam(0.01, parameters=deepcf2.parameters()) - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + base.set_flags({'FLAGS_sort_sum_gradient': True}) for e in range(self.num_epoches): sys.stderr.write('epoch %d\n' % e) for slice in range( @@ -365,11 +365,11 @@ def test_deefcf(self): dy_loss2 = loss2.numpy() sys.stderr.write(f'dynamic loss: {slice} {dy_loss2}\n') - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed deepcf = DeepCF(num_users, num_items, matrix) adam = paddle.optimizer.Adam(0.01, parameters=deepcf.parameters()) diff --git a/test/legacy_test/test_imperative_double_grad.py b/test/legacy_test/test_imperative_double_grad.py index 069a99b55875b..2f8d81d2e0ed2 100644 --- a/test/legacy_test/test_imperative_double_grad.py +++ b/test/legacy_test/test_imperative_double_grad.py @@ -19,17 +19,17 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle import base +from paddle.base.wrapped_decorator import wrap_decorator from paddle.vision.models import resnet50, resnet101 def _dygraph_guard_(func): def __impl__(*args, **kwargs): - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): return func(*args, **kwargs) else: - with fluid.dygraph.guard(): + with base.dygraph.guard(): return func(*args, **kwargs) return __impl__ @@ -40,7 +40,7 @@ def __impl__(*args, **kwargs): def random_var(size, low=-1, high=1, dtype='float32'): x_np = np.random.uniform(low=low, high=high, size=size).astype(dtype) - return fluid.dygraph.to_variable(x_np) + return base.dygraph.to_variable(x_np) class TestEagerGrad(TestCase): @@ -52,7 +52,7 @@ def test_simple_example_eager_grad(self): x = paddle.to_tensor(np_x, dtype="float64", stop_gradient=False) y = paddle.to_tensor(np_y, dtype="float64", stop_gradient=False) out = paddle.matmul(x, y) - dx = fluid.dygraph.grad(out, x) + dx = base.dygraph.grad(out, x) dout = np.ones_like(np_y) expected_dx = np.matmul(dout, np.transpose(np_y)) @@ -73,7 +73,7 @@ def test_simple_example_eager_grad_allow_unused(self): out_z = paddle.nn.functional.sigmoid(z) out = paddle.matmul(x, y) - dx = fluid.dygraph.grad(out, [x, z], allow_unused=True) + dx = base.dygraph.grad(out, [x, z], allow_unused=True) dout = np.ones_like(np_y) expected_dx = np.matmul(dout, np.transpose(np_y)) np.testing.assert_allclose(dx[0].numpy(), expected_dx, rtol=1e-05) @@ -96,7 +96,7 @@ def test_simple_example_eager_grad_not_allow_unused(self): try: # allow_unused is false in default - dx = fluid.dygraph.grad(out, [x, z]) + dx = base.dygraph.grad(out, [x, z]) except ValueError as e: error_msg = str(e) assert error_msg.find("allow_unused") > 0 @@ -115,7 +115,7 @@ def test_simple_example_eager_grad_duplicate_input(self): try: # duplicate input will arise RuntimeError errors - dx = fluid.dygraph.grad(out, [x, x]) + dx = base.dygraph.grad(out, [x, x]) except RuntimeError as e: error_msg = str(e) assert error_msg.find("duplicate") > 0 @@ -134,7 +134,7 @@ def test_simple_example_eager_grad_duplicate_output(self): try: # duplicate output will arise RuntimeError errors - dx = fluid.dygraph.grad([out, out], [x]) + dx = base.dygraph.grad([out, out], [x]) except RuntimeError as e: error_msg = str(e) assert error_msg.find("duplicate") > 0 @@ -194,8 +194,8 @@ def grad( create_graph=False, allow_unused=False, ): - fluid.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) - return fluid.dygraph.grad( + base.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) + return base.dygraph.grad( outputs=outputs, inputs=inputs, grad_outputs=grad_outputs, @@ -322,7 +322,7 @@ def test_none_one_initial_gradient(self): ) np.random.shuffle(x_np) - x = fluid.dygraph.to_variable(x_np) + x = base.dygraph.to_variable(x_np) x.stop_gradient = False alpha = 0.2 @@ -533,17 +533,17 @@ def model_f(input): out = out + linear(input) return out - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + base.set_flags({'FLAGS_sort_sum_gradient': True}) - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(123) paddle.framework.random._manual_program_seed(123) - a = fluid.dygraph.to_variable(value) + a = base.dygraph.to_variable(value) a.stop_gradient = False out = model_f(a) - dx = fluid.dygraph.grad( + dx = base.dygraph.grad( outputs=[out], inputs=[a], create_graph=False, @@ -553,10 +553,10 @@ def model_f(input): grad_1 = dx[0].numpy() - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(123) paddle.framework.random._manual_program_seed(123) - a = fluid.dygraph.to_variable(value) + a = base.dygraph.to_variable(value) a.stop_gradient = False out = model_f(a) @@ -569,12 +569,12 @@ def model_f(input): class TestRaiseNoDoubleGradOp(TestCase): def test_no_grad_op(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.ones(shape=[2, 3, 2, 2], dtype='float32') x.stop_gradient = False y = paddle.static.nn.group_norm(x, groups=1) - dx = fluid.dygraph.grad( + dx = base.dygraph.grad( outputs=[y], inputs=[x], create_graph=True, retain_graph=True )[0] diff --git a/test/legacy_test/test_imperative_framework.py b/test/legacy_test/test_imperative_framework.py index 3c81da2c6d59f..33f53c8d3ab66 100644 --- a/test/legacy_test/test_imperative_framework.py +++ b/test/legacy_test/test_imperative_framework.py @@ -18,7 +18,7 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid +from paddle import base class MLP(paddle.nn.Layer): @@ -68,6 +68,6 @@ def test_dygraph_backward(self): def test_dygraph_to_string(self): np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) - with fluid.dygraph.guard(): - var_inp = fluid.dygraph.to_variable(np_inp) + with base.dygraph.guard(): + var_inp = base.dygraph.to_variable(np_inp) print(str(var_inp)) diff --git a/test/legacy_test/test_imperative_gan.py b/test/legacy_test/test_imperative_gan.py index 524ed9ec35f08..60261972956bd 100644 --- a/test/legacy_test/test_imperative_gan.py +++ b/test/legacy_test/test_imperative_gan.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.nn import Linear @@ -58,11 +58,11 @@ def test_gan_float32(self): seed = 90 paddle.seed(1) paddle.framework.random._manual_program_seed(1) - startup = fluid.Program() - discriminate_p = fluid.Program() - generate_p = fluid.Program() + startup = base.Program() + discriminate_p = base.Program() + generate_p = base.Program() - scope = fluid.core.Scope() + scope = base.core.Scope() with new_program_scope( main=discriminate_p, startup=startup, scope=scope ): @@ -116,13 +116,13 @@ def test_gan_float32(self): sgd = paddle.optimizer.SGD(learning_rate=1e-3) sgd.minimize(g_loss) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) static_params = {} - with fluid.scope_guard(scope): + with base.scope_guard(scope): img = np.ones([2, 1], np.float32) noise = np.ones([2, 2], np.float32) exe.run(startup) @@ -142,7 +142,7 @@ def test_gan_float32(self): ) dy_params = {} - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(1) paddle.framework.random._manual_program_seed(1) @@ -197,8 +197,8 @@ def test_gan_float32(self): dy_d_loss = d_loss.numpy() dy_params2 = {} - with fluid.dygraph.guard(): - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(): + base.set_flags({'FLAGS_sort_sum_gradient': True}) paddle.seed(1) paddle.framework.random._manual_program_seed(1) discriminator2 = Discriminator() diff --git a/test/legacy_test/test_imperative_gnn.py b/test/legacy_test/test_imperative_gnn.py index 225dbe83b1df2..b68aa6926bf63 100644 --- a/test/legacy_test/test_imperative_gnn.py +++ b/test/legacy_test/test_imperative_gnn.py @@ -20,9 +20,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.optimizer import Adam @@ -66,10 +66,10 @@ class TestDygraphGNN(unittest.TestCase): def test_gnn_float32(self): paddle.seed(90) paddle.framework.random._manual_program_seed(90) - startup = fluid.Program() - main = fluid.Program() + startup = base.Program() + main = base.Program() - scope = fluid.core.Scope() + scope = base.core.Scope() with new_program_scope(main=main, startup=startup, scope=scope): features = paddle.static.data( name='features', shape=[1, 100, 50], dtype='float32' @@ -94,10 +94,10 @@ def test_gnn_float32(self): adam = Adam(learning_rate=1e-3) adam.minimize(loss) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) exe.run(startup) static_loss = exe.run( @@ -113,7 +113,7 @@ def test_gnn_float32(self): scope.find_var(model.gc.weight.name).get_tensor() ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(90) paddle.framework.random._manual_program_seed(90) @@ -139,7 +139,7 @@ def test_gnn_float32(self): loss_value = loss.numpy() model_gc_weight_value = model.gc.weight.numpy() - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(90) paddle.framework.random._manual_program_seed(90) diff --git a/test/legacy_test/test_imperative_group.py b/test/legacy_test/test_imperative_group.py index 2a1fdad88bb87..1e28d94e0b10d 100644 --- a/test/legacy_test/test_imperative_group.py +++ b/test/legacy_test/test_imperative_group.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle.fluid import core -from paddle.fluid.framework import in_dygraph_mode +from paddle.base import core +from paddle.base.framework import in_dygraph_mode class TestDataParallelGroup(unittest.TestCase): diff --git a/test/legacy_test/test_imperative_hook_for_layer.py b/test/legacy_test/test_imperative_hook_for_layer.py index 3b0fc95fadc11..d5ce15c58233a 100644 --- a/test/legacy_test/test_imperative_hook_for_layer.py +++ b/test/legacy_test/test_imperative_hook_for_layer.py @@ -17,9 +17,9 @@ import numpy as np from test_imperative_lod_tensor_to_selected_rows import SimpleNet -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph import base +from paddle import base +from paddle.base import core +from paddle.base.dygraph import base as imperative_base call_forward_post_hook = False call_forward_pre_hook = False @@ -49,15 +49,15 @@ class Test_Forward_Hook(unittest.TestCase): def test_forward_hook_return_value(self): seed = 90 - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(place): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed + base.set_flags({'FLAGS_sort_sum_gradient': True}) input_word = ( np.array( @@ -78,9 +78,9 @@ def test_forward_hook_return_value(self): ) y_data = y_data.reshape((-1, 1)) - input = base.to_variable(input_word) - input1 = base.to_variable(input_word1) - y = base.to_variable(y_data) + input = imperative_base.to_variable(input_word) + input1 = imperative_base.to_variable(input_word1) + y = imperative_base.to_variable(y_data) simplenet = SimpleNet( hidden_size=20, @@ -131,15 +131,15 @@ def test_forward_hook_return_value(self): def test_forward_hook(self): seed = 90 - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(place): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed + base.set_flags({'FLAGS_sort_sum_gradient': True}) global call_forward_post_hook global call_forward_pre_hook @@ -161,8 +161,8 @@ def test_forward_hook(self): ) y_data = y_data.reshape((-1, 1)) - input = base.to_variable(input_word) - y = base.to_variable(y_data) + input = imperative_base.to_variable(input_word) + y = imperative_base.to_variable(y_data) simplenet = SimpleNet( hidden_size=20, diff --git a/test/legacy_test/test_imperative_layer_apply.py b/test/legacy_test/test_imperative_layer_apply.py index 402d2351a0308..140cd8676ba14 100644 --- a/test/legacy_test/test_imperative_layer_apply.py +++ b/test/legacy_test/test_imperative_layer_apply.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn class LeNetDygraph(paddle.nn.Layer): @@ -73,7 +73,7 @@ def init_weights(layer): class TestLayerApply(unittest.TestCase): def test_apply_init_weight(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = LeNetDygraph() net.apply(init_weights) diff --git a/test/legacy_test/test_imperative_layer_children.py b/test/legacy_test/test_imperative_layer_children.py index bf440d511c566..38d4291d1a973 100644 --- a/test/legacy_test/test_imperative_layer_children.py +++ b/test/legacy_test/test_imperative_layer_children.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn class LeNetDygraph(paddle.nn.Layer): @@ -39,7 +39,7 @@ def forward(self, inputs): class TestLayerChildren(unittest.TestCase): def func_apply_init_weight(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = LeNetDygraph() net.eval() diff --git a/test/legacy_test/test_imperative_layer_trainable.py b/test/legacy_test/test_imperative_layer_trainable.py index 449b448da31f7..a436dfb6e0e02 100644 --- a/test/legacy_test/test_imperative_layer_trainable.py +++ b/test/legacy_test/test_imperative_layer_trainable.py @@ -17,13 +17,13 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import dygraph +from paddle import base +from paddle.base import dygraph class TestImperativeLayerTrainable(unittest.TestCase): def test_set_trainable(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): label = np.random.uniform(-1, 1, [10, 10]).astype(np.float32) label = dygraph.to_variable(label) diff --git a/test/legacy_test/test_imperative_load_static_param.py b/test/legacy_test/test_imperative_load_static_param.py index 0e956c55ae685..83dc19a3945cb 100644 --- a/test/legacy_test/test_imperative_load_static_param.py +++ b/test/legacy_test/test_imperative_load_static_param.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework from paddle.nn import BatchNorm, Linear paddle.enable_static() @@ -147,12 +147,12 @@ def testLoadStaticModel(self): [20, 200], 'float32', name="weight_test_2" ) - para_list = fluid.default_main_program().list_vars() + para_list = base.default_main_program().list_vars() - exe = fluid.Executor( - fluid.CPUPlace() - if not fluid.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + exe = base.Executor( + base.CPUPlace() + if not base.is_compiled_with_cuda() + else base.CUDAPlace(0) ) out = exe.run(framework.default_startup_program()) @@ -174,7 +174,7 @@ def testLoadStaticModel(self): else: new_dict[k] = v - with fluid.dygraph.guard(): + with base.dygraph.guard(): class MyTest(paddle.nn.Layer): def __init__(self): diff --git a/test/legacy_test/test_imperative_lod_tensor_to_selected_rows.py b/test/legacy_test/test_imperative_lod_tensor_to_selected_rows.py index 52e378265547e..f2c54048e6d28 100644 --- a/test/legacy_test/test_imperative_lod_tensor_to_selected_rows.py +++ b/test/legacy_test/test_imperative_lod_tensor_to_selected_rows.py @@ -19,9 +19,9 @@ from utils import DyGraphProgramDescTracerTestHelper import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable class SimpleNet(paddle.nn.Layer): @@ -44,7 +44,7 @@ def __init__( vocab_size, hidden_size, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -52,7 +52,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype=dtype, default_initializer=paddle.nn.initializer.Uniform( @@ -87,9 +87,9 @@ def test_simple_net(self): self.simple_net_float32(is_sparse, dtype) def simple_net_float32(self, is_sparse, dtype): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: seed = 90 @@ -101,7 +101,7 @@ def simple_net_float32(self, is_sparse, dtype): batch_num = 200 for is_sort_sum_gradient in [True, False]: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -123,7 +123,7 @@ def simple_net_float32(self, is_sparse, dtype): dy_loss = None helper = DyGraphProgramDescTracerTestHelper(self) - fluid.set_flags( + base.set_flags( {'FLAGS_sort_sum_gradient': is_sort_sum_gradient} ) @@ -160,7 +160,7 @@ def simple_net_float32(self, is_sparse, dtype): dtype=dtype, ) - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = paddle.optimizer.SGD(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -177,7 +177,7 @@ def simple_net_float32(self, is_sparse, dtype): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) for i in range(len(static_param_name_list)): @@ -191,7 +191,7 @@ def simple_net_float32(self, is_sparse, dtype): fetch_list = [static_loss] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_data, "y": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_mnist.py b/test/legacy_test/test_imperative_mnist.py index ff436db8d8084..b2e8779b22193 100644 --- a/test/legacy_test/test_imperative_mnist.py +++ b/test/legacy_test/test_imperative_mnist.py @@ -19,8 +19,8 @@ from utils import DyGraphProgramDescTracerTestHelper import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn import Linear @@ -119,23 +119,23 @@ def test_mnist_float32(self): traced_layer = None - with fluid.dygraph.guard(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + with base.dygraph.guard(): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed mnist = MNIST() sgd = paddle.optimizer.SGD( learning_rate=1e-3, parameters=mnist.parameters() ) - batch_py_reader = fluid.io.PyReader(capacity=1) + batch_py_reader = base.io.PyReader(capacity=1) batch_py_reader.decorate_sample_list_generator( paddle.batch( self.reader_decorator(paddle.dataset.mnist.train()), batch_size=batch_size, drop_last=True, ), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) mnist.train() @@ -177,13 +177,13 @@ def test_mnist_float32(self): dy_param_value[param.name] = param.numpy() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) mnist = MNIST() @@ -214,7 +214,7 @@ def test_mnist_float32(self): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -241,7 +241,7 @@ def test_mnist_float32(self): traced_layer([static_x_data]) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_mnist_sorted_gradient.py b/test/legacy_test/test_imperative_mnist_sorted_gradient.py index a85bdae98cdeb..9629858b2d8bf 100644 --- a/test/legacy_test/test_imperative_mnist_sorted_gradient.py +++ b/test/legacy_test/test_imperative_mnist_sorted_gradient.py @@ -19,9 +19,9 @@ from test_imperative_mnist import MNIST import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable class TestImperativeMnistSortGradient(unittest.TestCase): @@ -29,10 +29,10 @@ def test_mnist_sort_gradient_float32(self): seed = 90 epoch_num = 1 - with fluid.dygraph.guard(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed + base.set_flags({'FLAGS_sort_sum_gradient': True}) mnist2 = MNIST() sgd2 = paddle.optimizer.SGD( @@ -82,13 +82,13 @@ def test_mnist_sort_gradient_float32(self): break with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) mnist = MNIST() @@ -117,7 +117,7 @@ def test_mnist_sort_gradient_float32(self): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -138,7 +138,7 @@ def test_mnist_sort_gradient_float32(self): fetch_list = [avg_loss.name] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_named_members.py b/test/legacy_test/test_imperative_named_members.py index 89a3834ca4009..4680447469928 100644 --- a/test/legacy_test/test_imperative_named_members.py +++ b/test/legacy_test/test_imperative_named_members.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class MyLayer(paddle.nn.Layer): @@ -34,7 +34,7 @@ def forward(self, x): class TestImperativeNamedSubLayers(unittest.TestCase): def test_named_sublayers(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False) custom = MyLayer(3, 10) @@ -64,7 +64,7 @@ def test_named_sublayers(self): class TestImperativeNamedParameters(unittest.TestCase): def test_named_parameters(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): fc1 = paddle.nn.Linear(10, 3) fc2 = paddle.nn.Linear(3, 10, bias_attr=False) custom = MyLayer(3, 10) @@ -82,7 +82,7 @@ def test_named_parameters(self): self.assertListEqual(expected_named_parameters, named_parameters) def test_dir_layer(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): class Mymodel(paddle.nn.Layer): def __init__(self): @@ -91,12 +91,12 @@ def __init__(self): self.linear2 = paddle.nn.Linear(5, 5) self.conv2d = paddle.nn.Conv2D(3, 2, 3) self.embedding = paddle.nn.Embedding(128, 16) - self.h_0 = fluid.dygraph.to_variable( + self.h_0 = base.dygraph.to_variable( np.zeros([10, 10]).astype('float32') ) self.weight = self.create_parameter( shape=[2, 3], - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), dtype="float32", is_bias=False, ) diff --git a/test/legacy_test/test_imperative_numpy_bridge.py b/test/legacy_test/test_imperative_numpy_bridge.py index 0adb69adcf878..31c286a234792 100644 --- a/test/legacy_test/test_imperative_numpy_bridge.py +++ b/test/legacy_test/test_imperative_numpy_bridge.py @@ -17,29 +17,29 @@ import numpy as np -from paddle import fluid +from paddle import base class TestImperativeNumpyBridge(unittest.TestCase): def test_tensor_from_numpy(self): data_np = np.array([[2, 3, 1]]).astype('float32') - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") - var = fluid.dygraph.to_variable(data_np, zero_copy=True) + var = base.dygraph.to_variable(data_np, zero_copy=True) assert ( "Currently, zero_copy is not supported, and it will be discarded." in str(w[-1].message) ) # Temporally diable zero_copy - # var = fluid.dygraph.to_variable(data_np, zero_copy=True) + # var = base.dygraph.to_variable(data_np, zero_copy=True) # np.testing.assert_array_equal(var.numpy(), data_np) # data_np[0][0] = 4 # self.assertEqual(data_np[0][0], 4) # self.assertEqual(var[0][0].numpy()[0], 4) # np.testing.assert_array_equal(var.numpy(), data_np) - var2 = fluid.dygraph.to_variable(data_np, zero_copy=False) + var2 = base.dygraph.to_variable(data_np, zero_copy=False) np.testing.assert_array_equal(var2.numpy(), data_np) data_np[0][0] = -1 self.assertEqual(data_np[0][0], -1) diff --git a/test/legacy_test/test_imperative_ocr_attention_model.py b/test/legacy_test/test_imperative_ocr_attention_model.py index 8b07c7652fad9..f39fc790d55c2 100644 --- a/test/legacy_test/test_imperative_ocr_attention_model.py +++ b/test/legacy_test/test_imperative_ocr_attention_model.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.nn import BatchNorm, Linear @@ -76,12 +76,12 @@ def __init__( filter_size = 3 conv_std_0 = (2.0 / (filter_size**2 * channels[0])) ** 0.5 - conv_param_0 = fluid.ParamAttr( + conv_param_0 = base.ParamAttr( initializer=paddle.nn.initializer.Normal(0.0, conv_std_0) ) conv_std_1 = (2.0 / (filter_size**2 * channels[1])) ** 0.5 - conv_param_1 = fluid.ParamAttr( + conv_param_1 = base.ParamAttr( initializer=paddle.nn.initializer.Normal(0.0, conv_std_1) ) @@ -199,14 +199,14 @@ def __init__( ): super().__init__() self.rnn_hidden_size = rnn_hidden_size - para_attr = fluid.ParamAttr( + para_attr = base.ParamAttr( initializer=paddle.nn.initializer.Normal(0.0, 0.02) ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( initializer=paddle.nn.initializer.Normal(0.0, 0.02), learning_rate=2.0, ) - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): h_0 = np.zeros( (Config.batch_size, rnn_hidden_size), dtype="float32" ) @@ -247,7 +247,7 @@ def __init__( def forward(self, inputs): conv_features = self.ocr_convs(inputs) - # sliced_feature = fluid.layers.im2sequence( + # sliced_feature = base.layers.im2sequence( # input=conv_features, # stride=[1, 1], # filter_size=[conv_features.shape[2], 1]) @@ -445,7 +445,7 @@ def test_ocr_test(self): ) def run_dygraph(): - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + base.set_flags({'FLAGS_sort_sum_gradient': True}) paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) ocr_attention = OCRAttention() @@ -506,10 +506,10 @@ def run_dygraph(): return dy_out, dy_param_init_value, dy_param_value - with fluid.dygraph.guard(): + with base.dygraph.guard(): dy_out, dy_param_init_value, dy_param_value = run_dygraph() - with fluid.dygraph.guard(): + with base.dygraph.guard(): ( eager_out, eager_param_init_value, @@ -519,10 +519,10 @@ def run_dygraph(): with new_program_scope(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) ocr_attention = OCRAttention() @@ -564,7 +564,7 @@ def run_dygraph(): use_softmax=False, ) static_avg_loss = paddle.sum(cost) - # param_grad_list = fluid.backward.append_backward(static_avg_loss) + # param_grad_list = base.backward.append_backward(static_avg_loss) optimizer.minimize(static_avg_loss) static_param_init_value = {} @@ -578,7 +578,7 @@ def run_dygraph(): ) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -594,7 +594,7 @@ def run_dygraph(): static_label_out = label_out_np static_label_out = static_label_out.reshape((-1, 1)) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "pixel": image_np, "label_in": static_label_in, diff --git a/test/legacy_test/test_imperative_optimizer.py b/test/legacy_test/test_imperative_optimizer.py index 170a3b7d18585..a9af79fca6afa 100644 --- a/test/legacy_test/test_imperative_optimizer.py +++ b/test/legacy_test/test_imperative_optimizer.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer -from paddle.fluid import core # Note(wangzhongpu) # In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer. @@ -63,12 +63,12 @@ def _check_exception(self, exception_message, place=None): batch_size = 128 if place is None: place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): try: paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -85,12 +85,12 @@ def _check_mlp(self, place=None): if place is None: place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -99,14 +99,14 @@ def _check_mlp(self, place=None): parameter_list=mlp.parameters() ) - batch_py_reader = fluid.io.PyReader(capacity=1) + batch_py_reader = base.io.PyReader(capacity=1) batch_py_reader.decorate_sample_list_generator( paddle.batch( self.reader_decorator(paddle.dataset.mnist.train()), batch_size=batch_size, drop_last=True, ), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) dy_param_init_value = {} @@ -140,12 +140,12 @@ def _check_mlp(self, place=None): if place is None: place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe = fluid.Executor(place) + exe = base.Executor(place) mlp = MLP() optimizer = self.get_optimizer() @@ -171,7 +171,7 @@ def _check_mlp(self, place=None): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -194,7 +194,7 @@ def _check_mlp(self, place=None): fetch_list = [avg_loss.name] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) @@ -229,12 +229,12 @@ def _check_mlp(self, place=None): class TestOptimizerLearningRate(unittest.TestCase): def test_constant_lr(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -253,12 +253,12 @@ def test_constant_lr(self): np.testing.assert_allclose(lr, 0.001, rtol=1e-06, atol=0.0) def test_lr_decay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -284,12 +284,12 @@ def test_lr_decay(self): np.testing.assert_allclose(lr, ret[i], rtol=1e-06, atol=0.0) def test_lr_decay_natural_exp(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -319,12 +319,12 @@ def test_lr_decay_natural_exp(self): np.testing.assert_allclose(lr, ret[i], rtol=1e-06, atol=0.0) def test_set_lr(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) diff --git a/test/legacy_test/test_imperative_optimizer_v2.py b/test/legacy_test/test_imperative_optimizer_v2.py index e7e0f1c4a4782..0000bd49ccb08 100644 --- a/test/legacy_test/test_imperative_optimizer_v2.py +++ b/test/legacy_test/test_imperative_optimizer_v2.py @@ -19,9 +19,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.distributed.fleet.meta_optimizers import DGCMomentumOptimizer -from paddle.fluid import core # Note(wangzhongpu) # In dygraph, don't support ModelAverage, DGCMomentumOptimizer, ExponentialMovingAverage, PipelineOptimizer, LookaheadOptimizer, RecomputeOptimizer. @@ -64,9 +64,9 @@ def _check_exception(self, exception_message, place=None): batch_size = 128 if place is None: place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) try: @@ -88,9 +88,9 @@ def _check_mlp(self, place=None): if place is None: place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) paddle.disable_static(place) @@ -100,14 +100,14 @@ def _check_mlp(self, place=None): mlp = MLP() optimizer = self.get_optimizer_dygraph(parameter_list=mlp.parameters()) - batch_py_reader = fluid.io.PyReader(capacity=1) + batch_py_reader = base.io.PyReader(capacity=1) batch_py_reader.decorate_sample_list_generator( paddle.batch( self.reader_decorator(paddle.dataset.mnist.train()), batch_size=batch_size, drop_last=True, ), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) dy_param_init_value = {} @@ -153,12 +153,12 @@ def _check_mlp(self, place=None): if place is None: place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) - exe = fluid.Executor(place) + exe = base.Executor(place) mlp = MLP() optimizer = self.get_optimizer() @@ -184,7 +184,7 @@ def _check_mlp(self, place=None): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -207,7 +207,7 @@ def _check_mlp(self, place=None): fetch_list = [avg_loss.name] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) @@ -527,12 +527,12 @@ def test_sgd(self): class TestOptimizerLearningRate(unittest.TestCase): def test_constant_lr(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -551,12 +551,12 @@ def test_constant_lr(self): np.testing.assert_allclose(lr, 0.001, rtol=1e-06, atol=0.0) def test_lr_decay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -580,11 +580,11 @@ def test_lr_decay(self): scheduler.step() def test_lr_scheduler_natural_exp(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) loss = paddle.mean(b) @@ -605,12 +605,12 @@ def test_lr_scheduler_natural_exp(self): scheduler.step() def test_set_lr(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -641,12 +641,12 @@ def test_set_lr(self): adam.set_lr(0.01) def test_set_lr_scheduler(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - a = fluid.dygraph.to_variable(a) + a = base.dygraph.to_variable(a) b = linear(a) @@ -865,7 +865,7 @@ def test_recompute(self): class TestImperativeOptimizerList(unittest.TestCase): def test_parameter_list(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear_1 = paddle.nn.Linear(10, 10) linear_2 = paddle.nn.Linear(10, 10) @@ -877,7 +877,7 @@ def test_parameter_list(self): ) in_np = np.random.uniform(-0.1, 0.1, [10, 10]).astype("float32") - in_data = fluid.dygraph.to_variable(in_np) + in_data = base.dygraph.to_variable(in_np) y = linear_1(in_data) y = linear_2(y) diff --git a/test/legacy_test/test_imperative_parallel_coalesce_split.py b/test/legacy_test/test_imperative_parallel_coalesce_split.py index 4ae37b2588849..db9a2e56a766e 100644 --- a/test/legacy_test/test_imperative_parallel_coalesce_split.py +++ b/test/legacy_test/test_imperative_parallel_coalesce_split.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable class MyLayer(paddle.nn.Layer): @@ -42,7 +42,7 @@ def test_coalesce_split(self): _split_tensors, ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): test_layer = MyLayer("test_layer") strategy = core.ParallelStrategy() test_layer = paddle.DataParallel(test_layer, strategy) @@ -73,7 +73,7 @@ def test_coalesce_split(self): def test_reshape_inplace(self): from paddle.distributed.parallel import _reshape_inplace - with fluid.dygraph.guard(): + with base.dygraph.guard(): test_layer = MyLayer("test_layer") strategy = core.ParallelStrategy() test_layer = paddle.DataParallel(test_layer, strategy) diff --git a/test/legacy_test/test_imperative_partitial_backward.py b/test/legacy_test/test_imperative_partitial_backward.py index 77d531574a471..e61f24a36d27c 100644 --- a/test/legacy_test/test_imperative_partitial_backward.py +++ b/test/legacy_test/test_imperative_partitial_backward.py @@ -17,14 +17,14 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestImperativePartitialBackward(unittest.TestCase): def test_partitial_backward(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = np.random.randn(2, 4, 5).astype("float32") - x = fluid.dygraph.to_variable(x) + x = base.dygraph.to_variable(x) linear1 = paddle.nn.Linear(5, 10) linear2 = paddle.nn.Linear(5, 10) diff --git a/test/legacy_test/test_imperative_ptb_rnn.py b/test/legacy_test/test_imperative_ptb_rnn.py index e3790fa2b1931..1847b13e92840 100644 --- a/test/legacy_test/test_imperative_ptb_rnn.py +++ b/test/legacy_test/test_imperative_ptb_rnn.py @@ -19,9 +19,9 @@ from utils import DyGraphProgramDescTracerTestHelper import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core, framework +from paddle.base.dygraph.base import to_variable from paddle.nn import Embedding @@ -48,7 +48,7 @@ def _create_parameter(self): for i in range(self._num_layers): weight_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -61,7 +61,7 @@ def _create_parameter(self): ) self.weight_1_arr.append(self.add_parameter('w_%d' % i, weight_1)) bias_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -172,7 +172,7 @@ def __init__( vocab_size, hidden_size, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -180,7 +180,7 @@ def __init__( ), ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -188,7 +188,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -250,7 +250,7 @@ def ptb_rnn_cpu_float32(self, is_sparse): batch_num = 200 traced_layer = None - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -320,10 +320,10 @@ def ptb_rnn_cpu_float32(self, is_sparse): is_sparse=is_sparse, ) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) sgd = paddle.optimizer.SGD(learning_rate=1e-3) x = paddle.static.data( @@ -374,7 +374,7 @@ def ptb_rnn_cpu_float32(self, is_sparse): fetch_list = [static_loss, static_last_hidden, static_last_cell] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, diff --git a/test/legacy_test/test_imperative_ptb_rnn_sorted_gradient.py b/test/legacy_test/test_imperative_ptb_rnn_sorted_gradient.py index 972c38f4b6fa5..c79abac4c757d 100644 --- a/test/legacy_test/test_imperative_ptb_rnn_sorted_gradient.py +++ b/test/legacy_test/test_imperative_ptb_rnn_sorted_gradient.py @@ -19,9 +19,9 @@ from test_imperative_ptb_rnn import PtbModel import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core, framework +from paddle.base.dygraph.base import to_variable class TestDygraphPtbRnnSortGradient(unittest.TestCase): @@ -39,8 +39,8 @@ def ptb_rnn_sort_gradient_cpu_float32(self, is_sparse): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(): + base.set_flags({'FLAGS_sort_sum_gradient': True}) paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -108,10 +108,10 @@ def ptb_rnn_sort_gradient_cpu_float32(self, is_sparse): is_sparse=is_sparse, ) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) sgd = paddle.optimizer.SGD(learning_rate=1e-3) x = paddle.static.data( @@ -162,7 +162,7 @@ def ptb_rnn_sort_gradient_cpu_float32(self, is_sparse): fetch_list = [static_loss, static_last_hidden, static_last_cell] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, diff --git a/test/legacy_test/test_imperative_recurrent_usage.py b/test/legacy_test/test_imperative_recurrent_usage.py index a399a9266e116..a17bf58472bdd 100644 --- a/test/legacy_test/test_imperative_recurrent_usage.py +++ b/test/legacy_test/test_imperative_recurrent_usage.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable class RecurrentTest(paddle.nn.Layer): @@ -38,9 +38,9 @@ def test_recurrent_feed(self): seed = 90 original_np1 = np.arange(1, 5).reshape(2, 2).astype("float32") original_np2 = np.arange(5, 9).reshape(2, 2).astype("float32") - with fluid.dygraph.guard(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + with base.dygraph.guard(): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed original_in1 = to_variable(original_np1) original_in2 = to_variable(original_np2) original_in1.stop_gradient = False @@ -57,9 +57,9 @@ def test_recurrent_feed(self): original_in1.stop_gradient = True rt.clear_gradients() - with fluid.dygraph.guard(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + with base.dygraph.guard(): + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed original_in1 = to_variable(original_np1) original_in2 = to_variable(original_np2) original_in1.stop_gradient = False @@ -77,30 +77,30 @@ def test_recurrent_feed(self): rt.clear_gradients() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed in1 = paddle.static.data(name="inp1", shape=[2, 2]) in1.stop_gradient = False in2 = paddle.static.data(name="inp2", shape=[2, 2]) in2.stop_gradient = False rt1 = RecurrentTest("RecurrentTest") static_sum_out, static_out = rt1(in1, in2) - fluid.backward.append_backward(static_sum_out) - exe = fluid.Executor( - fluid.CPUPlace() + base.backward.append_backward(static_sum_out) + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) static_dout = ( - fluid.default_main_program() + base.default_main_program() .block(0) ._find_var_recursive(static_out.name + "@GRAD") ) fetch_list = [static_sum_out, static_out, static_dout] for i in range(3): out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"inp1": original_np1, "inp2": original_np2}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_reinforcement.py b/test/legacy_test/test_imperative_reinforcement.py index 481b78d613491..2959071cf6a9f 100644 --- a/test/legacy_test/test_imperative_reinforcement.py +++ b/test/legacy_test/test_imperative_reinforcement.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class Policy(paddle.nn.Layer): @@ -63,18 +63,18 @@ def run_dygraph(): policy = Policy(input_size=4) - dy_state = fluid.dygraph.base.to_variable(state) + dy_state = base.dygraph.base.to_variable(state) dy_state.stop_gradient = True loss_probs = policy(dy_state) - dy_mask = fluid.dygraph.base.to_variable(mask) + dy_mask = base.dygraph.base.to_variable(mask) dy_mask.stop_gradient = True loss_probs = paddle.log(loss_probs) loss_probs = paddle.multiply(loss_probs, dy_mask) loss_probs = paddle.sum(loss_probs, axis=-1) - dy_reward = fluid.dygraph.base.to_variable(reward) + dy_reward = base.dygraph.base.to_variable(reward) dy_reward.stop_gradient = True loss_probs = paddle.multiply(dy_reward, loss_probs) @@ -101,10 +101,10 @@ def run_dygraph(): return dy_out, dy_param_init_value, dy_param_value - with fluid.dygraph.guard(): + with base.dygraph.guard(): dy_out, dy_param_init_value, dy_param_value = run_dygraph() - with fluid.dygraph.guard(): + with base.dygraph.guard(): ( eager_out, eager_param_init_value, @@ -115,10 +115,10 @@ def run_dygraph(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) policy = Policy(input_size=4) @@ -153,7 +153,7 @@ def run_dygraph(): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -164,7 +164,7 @@ def run_dygraph(): fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"st_state": state, "st_reward": reward, "st_mask": mask}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_resnet.py b/test/legacy_test/test_imperative_resnet.py index 41e270c67958a..b91f840a57bf0 100644 --- a/test/legacy_test/test_imperative_resnet.py +++ b/test/legacy_test/test_imperative_resnet.py @@ -19,10 +19,10 @@ from utils import DyGraphProgramDescTracerTestHelper import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable +from paddle.base.layer_helper import LayerHelper from paddle.nn import BatchNorm # NOTE(zhiqiu): run with FLAGS_cudnn_deterministic=1 @@ -58,14 +58,14 @@ def optimizer_setting(params, parameter_list=None): base_lr = params["lr"] lr = [] lr = [base_lr * (0.1**i) for i in range(len(bd) + 1)] - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): optimizer = paddle.optimizer.SGD( learning_rate=0.01, parameters=parameter_list ) else: optimizer = paddle.optimizer.SGD(learning_rate=0.01) # TODO(minqiyang): Add learning rate scheduler support to dygraph mode - # optimizer = fluid.optimizer.Momentum( + # optimizer = base.optimizer.Momentum( # learning_rate=params["lr"], # learning_rate=paddle.optimizer.lr.piecewise_decay( # boundaries=bd, values=lr), @@ -225,7 +225,7 @@ def __init__(self, layers=50, class_dim=102, use_cudnn=True): self.out = paddle.nn.Linear( self.pool2d_avg_output, class_dim, - weight_attr=fluid.param_attr.ParamAttr( + weight_attr=base.param_attr.ParamAttr( initializer=paddle.nn.initializer.Uniform(-stdv, stdv) ), ) @@ -260,7 +260,7 @@ def test_resnet_float32(self): traced_layer = None - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -346,10 +346,10 @@ def test_resnet_float32(self): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) resnet = ResNet() @@ -387,7 +387,7 @@ def test_resnet_float32(self): ) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -414,7 +414,7 @@ def test_resnet_float32(self): fetch_list.extend(static_param_name_list) fetch_list.extend(static_grad_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_resnet_sorted_gradient.py b/test/legacy_test/test_imperative_resnet_sorted_gradient.py index ba71a803fb650..08805d6e6f990 100644 --- a/test/legacy_test/test_imperative_resnet_sorted_gradient.py +++ b/test/legacy_test/test_imperative_resnet_sorted_gradient.py @@ -19,9 +19,9 @@ from test_imperative_resnet import ResNet import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable batch_size = 8 train_parameters = { @@ -54,14 +54,14 @@ def optimizer_setting(params, parameter_list=None): base_lr = params["lr"] lr = [] lr = [base_lr * (0.1**i) for i in range(len(bd) + 1)] - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): optimizer = paddle.optimizer.SGD( learning_rate=0.01, parameters=parameter_list ) else: optimizer = paddle.optimizer.SGD(learning_rate=0.01) # TODO(minqiyang): Add learning rate scheduler support to dygraph mode - # optimizer = fluid.optimizer.Momentum( + # optimizer = base.optimizer.Momentum( # learning_rate=params["lr"], # learning_rate=paddle.optimizer.lr.piecewise_decay( # boundaries=bd, values=lr), @@ -77,8 +77,8 @@ def test_resnet_sort_gradient_float32(self): batch_size = train_parameters["batch_size"] batch_num = 10 - with fluid.dygraph.guard(): - fluid.set_flags({'FLAGS_sort_sum_gradient': True}) + with base.dygraph.guard(): + base.set_flags({'FLAGS_sort_sum_gradient': True}) paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -152,10 +152,10 @@ def test_resnet_sort_gradient_float32(self): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) resnet = ResNet() @@ -196,7 +196,7 @@ def test_resnet_sort_gradient_float32(self): ) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -220,7 +220,7 @@ def test_resnet_sort_gradient_float32(self): fetch_list.extend(static_param_name_list) fetch_list.extend(static_grad_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_save_load_v2.py b/test/legacy_test/test_imperative_save_load_v2.py index 11f3386df3461..473872f1aadb3 100644 --- a/test/legacy_test/test_imperative_save_load_v2.py +++ b/test/legacy_test/test_imperative_save_load_v2.py @@ -19,9 +19,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable from paddle.nn import Embedding from paddle.optimizer import Adam from paddle.optimizer.lr import LRScheduler @@ -47,7 +47,7 @@ def __init__( for i in range(self._num_layers): weight_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -60,7 +60,7 @@ def __init__( ) self.weight_1_arr.append(self.add_parameter('w_%d' % i, weight_1)) bias_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -170,7 +170,7 @@ def __init__( vocab_size, hidden_size, sparse=False, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -179,7 +179,7 @@ def __init__( ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -187,7 +187,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -251,7 +251,7 @@ def func_setUp(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -272,9 +272,9 @@ def func_setUp(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) scheduler = paddle.optimizer.lr.PiecewiseDecay( boundaries=bd, values=lr_arr @@ -354,7 +354,7 @@ def func_testLoadAndSetVarBase(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -375,9 +375,9 @@ def func_testLoadAndSetVarBase(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) scheduler = paddle.optimizer.lr.PiecewiseDecay( boundaries=bd, values=lr_arr @@ -476,7 +476,7 @@ def func_testSetVariable(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -497,9 +497,9 @@ def func_testSetVariable(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) scheduler = paddle.optimizer.lr.PiecewiseDecay( boundaries=bd, values=lr_arr @@ -594,7 +594,7 @@ def func_testSetNumpy(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -615,9 +615,9 @@ def func_testSetNumpy(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) scheduler = paddle.optimizer.lr.PiecewiseDecay( boundaries=bd, values=lr_arr @@ -718,7 +718,7 @@ def func_testSetVariableBeforeTrain(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -731,9 +731,9 @@ def func_testSetVariableBeforeTrain(self): ) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) adam = Adam( learning_rate=0.0, @@ -806,7 +806,7 @@ def func_testLoadAndSetVarBaseBeforeTrain(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -828,9 +828,9 @@ def func_testLoadAndSetVarBaseBeforeTrain(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) adam = Adam( learning_rate=0.0, @@ -908,7 +908,7 @@ def func_testSetNumpyBeforeTrain(self): batch_size = 4 batch_num = 200 - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) # TODO: marsyang1993 Change seed to @@ -930,9 +930,9 @@ def func_testSetNumpyBeforeTrain(self): lr_arr.append(new_lr) place = ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) scheduler = paddle.optimizer.lr.PiecewiseDecay( boundaries=bd, values=lr_arr @@ -1013,7 +1013,7 @@ def func_testSetNumpyBeforeTrain(self): np.testing.assert_array_equal(new_t, base_t) def func_testOnlyLoadParams(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): emb = paddle.nn.Embedding(10, 10) state_dict = emb.state_dict() paddle.save( @@ -1026,7 +1026,7 @@ def func_testOnlyLoadParams(self): ) def func_test_no_state_in_input_dict(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): emb = paddle.nn.Embedding(10, 10) state_dict = emb.state_dict() paddle.save( @@ -1042,7 +1042,7 @@ def func_test_no_state_in_input_dict(self): emb.set_state_dict(para_state_dict) def func_test_state_shape_mismatch(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): emb = paddle.nn.Embedding(10, 10) state_dict = emb.state_dict() paddle.save( diff --git a/test/legacy_test/test_imperative_se_resnext.py b/test/legacy_test/test_imperative_se_resnext.py index 09feff3444edd..24bc9e56d0e1d 100644 --- a/test/legacy_test/test_imperative_se_resnext.py +++ b/test/legacy_test/test_imperative_se_resnext.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.layer_helper import LayerHelper from paddle.nn import BatchNorm batch_size = 8 @@ -54,7 +54,7 @@ def optimizer_setting(params, parameter_list=None): # bd = [step * e for e in ls["epochs"]] # base_lr = params["lr"] # lr = [base_lr * (0.1**i) for i in range(len(bd) + 1)] - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): optimizer = paddle.optimizer.SGD( learning_rate=0.01, parameters=parameter_list ) @@ -339,7 +339,7 @@ def run_dygraph(): ) np.random.seed(seed) - batch_py_reader = fluid.io.PyReader(capacity=1) + batch_py_reader = base.io.PyReader(capacity=1) batch_py_reader.decorate_sample_list_generator( paddle.batch( self.reader_decorator( @@ -348,7 +348,7 @@ def run_dygraph(): batch_size=batch_size, drop_last=True, ), - places=fluid.CPUPlace(), + places=base.CPUPlace(), ) dy_param_init_value = {} @@ -406,7 +406,7 @@ def run_dygraph(): dy_grad_value, ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): ( dy_out, dy_param_init_value, @@ -414,7 +414,7 @@ def run_dygraph(): dy_grad_value, ) = run_dygraph() - with fluid.dygraph.guard(): + with base.dygraph.guard(): ( eager_out, eager_param_init_value, @@ -426,10 +426,10 @@ def run_dygraph(): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) se_resnext = SeResNeXt() @@ -472,7 +472,7 @@ def run_dygraph(): ) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) @@ -496,7 +496,7 @@ def run_dygraph(): fetch_list.extend(static_param_name_list) fetch_list.extend(static_grad_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"pixel": static_x_data, "label": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_selected_rows.py b/test/legacy_test/test_imperative_selected_rows.py index 5f04e8d28a19f..8d97666e0f862 100644 --- a/test/legacy_test/test_imperative_selected_rows.py +++ b/test/legacy_test/test_imperative_selected_rows.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import to_variable class SimpleNet(paddle.nn.Layer): @@ -39,15 +39,15 @@ def forward(self, input): class TestSimpleNet(unittest.TestCase): def test_selectedrows_gradient1(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: for dtype in ["float32", "float64"]: for sort_sum_gradient in [True, False]: paddle.disable_static(place) - fluid.set_flags( + base.set_flags( {'FLAGS_sort_sum_gradient': sort_sum_gradient} ) # grad_clip = paddle.nn.ClipGradByGlobalNorm(5.0) @@ -78,14 +78,14 @@ def test_selectedrows_gradient1(self): paddle.enable_static() def test_selectedrows_gradient2(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: for sort_sum_gradient in [True, False]: - with fluid.dygraph.guard(place): - fluid.set_flags( + with base.dygraph.guard(place): + base.set_flags( {'FLAGS_sort_sum_gradient': sort_sum_gradient} ) grad_clip = paddle.nn.ClipGradByGlobalNorm(5.0) diff --git a/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py b/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py index 4f7e4780e9336..19be1a8b7ee3f 100644 --- a/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py +++ b/test/legacy_test/test_imperative_selected_rows_to_lod_tensor.py @@ -18,9 +18,9 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.dygraph.base import to_variable +from paddle import base +from paddle.base import core, framework +from paddle.base.dygraph.base import to_variable from paddle.nn import Embedding @@ -44,7 +44,7 @@ def __init__( vocab_size, hidden_size, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -52,7 +52,7 @@ def __init__( ), ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.hidden_size], dtype=dtype, default_initializer=paddle.nn.initializer.Uniform( @@ -60,7 +60,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size], dtype=dtype, default_initializer=paddle.nn.initializer.Uniform( @@ -96,9 +96,9 @@ def test_simple_net(self): self.simple_net_float(is_sparse, dtype) def simple_net_float(self, is_sparse, dtype): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: seed = 90 @@ -111,7 +111,7 @@ def simple_net_float(self, is_sparse, dtype): for is_sort_sum_gradient in [True, False]: traced_layer = None - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -132,7 +132,7 @@ def simple_net_float(self, is_sparse, dtype): dy_param_init = {} dy_loss = None - fluid.set_flags( + base.set_flags( {'FLAGS_sort_sum_gradient': is_sort_sum_gradient} ) @@ -170,7 +170,7 @@ def simple_net_float(self, is_sparse, dtype): dtype=dtype, ) - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = paddle.optimizer.SGD(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -201,7 +201,7 @@ def simple_net_float(self, is_sparse, dtype): fetch_list = [static_loss] fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_data, "y": y_data}, fetch_list=fetch_list, ) diff --git a/test/legacy_test/test_imperative_signal_handler.py b/test/legacy_test/test_imperative_signal_handler.py index a5e44f953df77..8ed10104b1cd8 100644 --- a/test/legacy_test/test_imperative_signal_handler.py +++ b/test/legacy_test/test_imperative_signal_handler.py @@ -19,7 +19,7 @@ import time import unittest -from paddle.fluid import core +from paddle.base import core def set_child_signal_handler(self, child_pid): diff --git a/test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py b/test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py index 62476046e8ab2..63a8e160d7112 100644 --- a/test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py +++ b/test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py @@ -17,18 +17,18 @@ import numpy as np import paddle -from paddle import _legacy_C_ops, fluid +from paddle import _legacy_C_ops, base from paddle.tensor import random -if fluid.is_compiled_with_cuda(): - fluid.core.globals()['FLAGS_cudnn_deterministic'] = True +if base.is_compiled_with_cuda(): + base.core.globals()['FLAGS_cudnn_deterministic'] = True class Config: def __init__(self, place, sort_sum_gradient=True): self.place = place - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): # CPU cases are extremely slow self.g_base_dims = 1 self.d_base_dims = 1 @@ -114,7 +114,7 @@ def __init__(self, num_channels, epsilon=1e-5): self.bias = self.create_parameter(shape=[num_channels], is_bias=True) def forward(self, input): - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): out, _, _ = _legacy_C_ops.instance_norm( input, self.scale, self.bias, 'epsilon', self.epsilon ) @@ -123,8 +123,8 @@ def forward(self, input): return paddle.static.nn.instance_norm( input, epsilon=self.epsilon, - param_attr=fluid.ParamAttr(self.scale.name), - bias_attr=fluid.ParamAttr(self.bias.name), + param_attr=base.ParamAttr(self.scale.name), + bias_attr=base.ParamAttr(self.bias.name), ) @@ -387,15 +387,15 @@ def loss_cls(cls, label, cfg): def calc_gradients(outputs, inputs, no_grad_set): - if fluid.in_dygraph_mode(): - return fluid.dygraph.grad( + if base.in_dygraph_mode(): + return base.dygraph.grad( outputs=outputs, inputs=inputs, no_grad_vars=no_grad_set, create_graph=True, ) else: - return fluid.gradients( + return base.gradients( targets=outputs, inputs=inputs, no_grad_set=no_grad_set ) @@ -481,7 +481,7 @@ def build_optimizer(layer, cfg, loss=None): learning_rate = 1e-3 beta1 = 0.5 beta2 = 0.999 - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): return paddle.optimizer.Adam( learning_rate=learning_rate, beta1=beta1, @@ -510,7 +510,7 @@ def __init__(self, cfg): self.cfg = cfg - fluid.set_flags({'FLAGS_sort_sum_gradient': cfg.sort_sum_gradient}) + base.set_flags({'FLAGS_sort_sum_gradient': cfg.sort_sum_gradient}) def clear_gradients(self): if self.g_optimizer: @@ -520,9 +520,9 @@ def clear_gradients(self): self.d_optimizer.clear_gradients() def run(self, image_real, label_org, label_trg): - image_real = fluid.dygraph.to_variable(image_real) - label_org = fluid.dygraph.to_variable(label_org) - label_trg = fluid.dygraph.to_variable(label_trg) + image_real = base.dygraph.to_variable(image_real) + label_org = base.dygraph.to_variable(label_org) + label_trg = base.dygraph.to_variable(label_trg) g_loss = get_generator_loss( image_real, @@ -575,11 +575,11 @@ def create_data_layer(): paddle.seed(cfg.seed) paddle.framework.random._manual_program_seed(cfg.seed) - self.gen_program = fluid.Program() - gen_startup_program = fluid.Program() + self.gen_program = base.Program() + gen_startup_program = base.Program() - with fluid.program_guard(self.gen_program, gen_startup_program): - with fluid.unique_name.guard(): + with base.program_guard(self.gen_program, gen_startup_program): + with base.unique_name.guard(): image_real, label_org, label_trg = create_data_layer() generator = Generator(cfg) discriminator = Discriminator(cfg) @@ -593,10 +593,10 @@ def create_data_layer(): ) build_optimizer(generator, cfg, loss=g_loss) - self.dis_program = fluid.Program() - dis_startup_program = fluid.Program() - with fluid.program_guard(self.dis_program, dis_startup_program): - with fluid.unique_name.guard(): + self.dis_program = base.Program() + dis_startup_program = base.Program() + with base.program_guard(self.dis_program, dis_startup_program): + with base.unique_name.guard(): image_real, label_org, label_trg = create_data_layer() generator = Generator(cfg) discriminator = Discriminator(cfg) @@ -610,10 +610,10 @@ def create_data_layer(): ) build_optimizer(discriminator, cfg, loss=d_loss) - self.executor = fluid.Executor(cfg.place) - self.scope = fluid.Scope() + self.executor = base.Executor(cfg.place) + self.scope = base.Scope() - with fluid.scope_guard(self.scope): + with base.scope_guard(self.scope): self.executor.run(gen_startup_program) self.executor.run(dis_startup_program) @@ -626,7 +626,7 @@ def run(self, image_real, label_org, label_trg): 'label_org': label_org, 'label_trg': label_trg, } - with fluid.scope_guard(self.scope): + with base.scope_guard(self.scope): g_loss_val = self.executor.run( self.gen_program, feed=feed, fetch_list=[self.g_loss] )[0] @@ -638,10 +638,10 @@ def run(self, image_real, label_org, label_trg): class TestStarGANWithGradientPenalty(unittest.TestCase): def func_main(self): - self.place_test(fluid.CPUPlace()) + self.place_test(base.CPUPlace()) - if fluid.is_compiled_with_cuda(): - self.place_test(fluid.CUDAPlace(0)) + if base.is_compiled_with_cuda(): + self.place_test(base.CUDAPlace(0)) def place_test(self, place): cfg = Config(place, False) @@ -649,17 +649,17 @@ def place_test(self, place): dataset = create_mnist_dataset(cfg) dataset = paddle.reader.cache(dataset) - fluid_dygraph_loss = [] - with fluid.dygraph.guard(cfg.place): - fluid_dygraph_model = DyGraphTrainModel(cfg) + base_dygraph_loss = [] + with base.dygraph.guard(cfg.place): + base_dygraph_model = DyGraphTrainModel(cfg) for batch_id, (image_real, label_org, label_trg) in enumerate( dataset() ): - loss = fluid_dygraph_model.run(image_real, label_org, label_trg) - fluid_dygraph_loss.append(loss) + loss = base_dygraph_model.run(image_real, label_org, label_trg) + base_dygraph_loss.append(loss) eager_dygraph_loss = [] - with fluid.dygraph.guard(cfg.place): + with base.dygraph.guard(cfg.place): eager_dygraph_model = DyGraphTrainModel(cfg) for batch_id, (image_real, label_org, label_trg) in enumerate( dataset() diff --git a/test/legacy_test/test_imperative_tensor_clear_gradient.py b/test/legacy_test/test_imperative_tensor_clear_gradient.py index 11864a5902724..fe947c5a55cd3 100644 --- a/test/legacy_test/test_imperative_tensor_clear_gradient.py +++ b/test/legacy_test/test_imperative_tensor_clear_gradient.py @@ -18,16 +18,16 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle import base +from paddle.base.wrapped_decorator import wrap_decorator def _dygraph_guard_(func): def __impl__(*args, **kwargs): - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): return func(*args, **kwargs) else: - with fluid.dygraph.guard(): + with base.dygraph.guard(): return func(*args, **kwargs) return __impl__ @@ -46,7 +46,7 @@ def test_tensor_method_clear_gradient_case1(self): linear = paddle.nn.Linear(2, 3) out = linear(input) out.backward() - if not fluid.framework.in_dygraph_mode(): + if not base.framework.in_dygraph_mode(): linear.weight.clear_gradient() else: linear.weight._zero_grads() @@ -67,13 +67,13 @@ def test_tensor_method_clear_gradient_case2(self): linear.weight.clear_gradient(False) # before ._gradient_set_empty(False), # the return of ._is_gradient_set_empty() should be True - if not fluid.framework.in_dygraph_mode(): + if not base.framework.in_dygraph_mode(): self.assertTrue(linear.weight._is_gradient_set_empty()) else: self.assertIsNone(linear.weight.grad) # reset, because ClearGradient will call SetIsEmpty(True), but this is not our expectation. - if not fluid.framework.in_dygraph_mode(): + if not base.framework.in_dygraph_mode(): linear.weight._gradient_set_empty(False) # after ._gradient_set_empty(False), # the return of ._is_gradient_set_empty() should be False diff --git a/test/legacy_test/test_imperative_trace_non_persistable_inputs.py b/test/legacy_test/test_imperative_trace_non_persistable_inputs.py index b90b5d47b8994..a30064c60f93f 100644 --- a/test/legacy_test/test_imperative_trace_non_persistable_inputs.py +++ b/test/legacy_test/test_imperative_trace_non_persistable_inputs.py @@ -18,14 +18,14 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class SimpleFCLayer(paddle.nn.Layer): def __init__(self, feature_size, batch_size, fc_size): super().__init__() self._linear = paddle.nn.Linear(feature_size, fc_size) - self._offset = fluid.dygraph.to_variable( + self._offset = base.dygraph.to_variable( np.random.random((batch_size, fc_size)).astype('float32') ) @@ -36,10 +36,10 @@ def forward(self, x): class TestTracedLayerRecordNonPersistableInput(unittest.TestCase): def test_main(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return traced_layer = None - with fluid.dygraph.guard(): + with base.dygraph.guard(): feature_size = 3 batch_size = 4 fc_size = 2 @@ -55,13 +55,13 @@ def test_main(self): } for _ in range(10): - in_x = fluid.dygraph.to_variable( + in_x = base.dygraph.to_variable( np.random.random((batch_size, feature_size)).astype( 'float32' ) ) if traced_layer is None: - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( layer, [in_x] ) else: diff --git a/test/legacy_test/test_imperative_transformer_sorted_gradient.py b/test/legacy_test/test_imperative_transformer_sorted_gradient.py index f85986fab3584..ea4fb2bb142fb 100644 --- a/test/legacy_test/test_imperative_transformer_sorted_gradient.py +++ b/test/legacy_test/test_imperative_transformer_sorted_gradient.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph import guard, to_variable +from paddle import base +from paddle.base import core +from paddle.base.dygraph import guard, to_variable from paddle.nn import Layer, Linear np.set_printoptions(suppress=True) @@ -398,10 +398,10 @@ def __init__(self, d_model, process_cmd, shape_len=None): if cmd == "n": self._layer_norm = paddle.nn.LayerNorm( normalized_shape=d_model, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.0) ), ) @@ -658,7 +658,7 @@ def __init__( src_vocab_size, src_emb_dim, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=word_emb_param_name, initializer=paddle.nn.initializer.Normal( 0.0, src_emb_dim**-0.5 @@ -674,7 +674,7 @@ def __init__( self._src_max_len, src_emb_dim, sparse=is_sparse, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name=pos_enc_param_name, initializer=paddle.nn.initializer.Assign(pos_inp), trainable=False, @@ -1112,7 +1112,7 @@ def transformer_sort_gradient_float32(self, is_sparse): def run_dygraph(): # NOTE(xiongkun03): In new executor, the inplace strategy is on by default, which will cause result of sumop have some differences. So we disable inplace. - fluid.set_flags({'FLAGS_new_executor_use_inplace': False}) + base.set_flags({'FLAGS_new_executor_use_inplace': False}) paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) transformer = TransFormer( @@ -1140,7 +1140,7 @@ def run_dygraph(): lr_decay = paddle.optimizer.lr.noam_decay( ModelHyperParams.d_model, TrainTaskConfig.warmup_steps ) - with fluid.default_main_program()._lr_schedule_guard(): + with base.default_main_program()._lr_schedule_guard(): learning_rate = lr_decay * TrainTaskConfig.learning_rate optimizer = paddle.optimizer.Adam( learning_rate=learning_rate, @@ -1215,10 +1215,10 @@ def run_dygraph(): is_test=False, is_sparse=is_sparse, ) - exe = fluid.Executor( - fluid.CPUPlace() + exe = base.Executor( + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) optimizer = paddle.optimizer.SGD(learning_rate=0.003) @@ -1249,7 +1249,7 @@ def run_dygraph(): for param in transformer.parameters(): static_param_name_list.append(param.name) out = exe.run( - fluid.default_startup_program(), + base.default_startup_program(), fetch_list=static_param_name_list, ) for i in range(len(static_param_name_list)): @@ -1269,7 +1269,7 @@ def run_dygraph(): fetch_list.extend(static_param_name_list) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=feed_dict, fetch_list=fetch_list, ) @@ -1285,7 +1285,7 @@ def run_dygraph(): # compare eager result with imperative result with guard(): - fluid.set_flags({'FLAGS_sort_sum_gradient': False}) + base.set_flags({'FLAGS_sort_sum_gradient': False}) ( dy_avg_cost_value, dy_sum_cost_value, diff --git a/test/legacy_test/test_imperative_triple_grad.py b/test/legacy_test/test_imperative_triple_grad.py index 72e2689da774b..2ca8f40de1dae 100644 --- a/test/legacy_test/test_imperative_triple_grad.py +++ b/test/legacy_test/test_imperative_triple_grad.py @@ -18,16 +18,16 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle import base +from paddle.base.wrapped_decorator import wrap_decorator def _dygraph_guard_(func): def __impl__(*args, **kwargs): - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): return func(*args, **kwargs) else: - with fluid.dygraph.guard(): + with base.dygraph.guard(): return func(*args, **kwargs) return __impl__ @@ -39,7 +39,7 @@ def __impl__(*args, **kwargs): def random_var(size, low=-1, high=1, dtype='float32'): np.random.seed(2021) x_np = np.random.uniform(low=low, high=high, size=size).astype(dtype) - return fluid.dygraph.to_variable(x_np) + return base.dygraph.to_variable(x_np) class TestDygraphTripleGradMatmul(TestCase): @@ -118,8 +118,8 @@ def grad( create_graph=False, allow_unused=False, ): - fluid.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) - return fluid.dygraph.grad( + base.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) + return base.dygraph.grad( outputs=outputs, inputs=inputs, grad_outputs=grad_outputs, @@ -244,8 +244,8 @@ def grad( create_graph=False, allow_unused=False, ): - fluid.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) - return fluid.dygraph.grad( + base.set_flags({'FLAGS_sort_sum_gradient': self.sort_sum_gradient}) + return base.dygraph.grad( outputs=outputs, inputs=inputs, grad_outputs=grad_outputs, diff --git a/test/legacy_test/test_imperative_using_non_zero_gpu.py b/test/legacy_test/test_imperative_using_non_zero_gpu.py index b83bd2fcaf4b7..887a04320e225 100644 --- a/test/legacy_test/test_imperative_using_non_zero_gpu.py +++ b/test/legacy_test/test_imperative_using_non_zero_gpu.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.dygraph import guard, to_variable +from paddle import base +from paddle.base.dygraph import guard, to_variable class TestImperativeUsingNonZeroGpu(unittest.TestCase): @@ -28,15 +28,15 @@ def run_main(self, np_arr, place): np.testing.assert_array_equal(np_arr, var.numpy()) def test_non_zero_gpu(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): return np_arr = np.random.random([11, 13]).astype('float32') if paddle.device.cuda.device_count() > 1: # should use non zero gpu if there are more than 1 gpu - self.run_main(np_arr, fluid.CUDAPlace(1)) + self.run_main(np_arr, base.CUDAPlace(1)) else: - self.run_main(np_arr, fluid.CUDAPlace(0)) + self.run_main(np_arr, base.CUDAPlace(0)) if __name__ == '__main__': diff --git a/test/legacy_test/test_increment.py b/test/legacy_test/test_increment.py index 5112d780bf58a..4887564e9b9bb 100755 --- a/test/legacy_test/test_increment.py +++ b/test/legacy_test/test_increment.py @@ -17,23 +17,23 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestIncrement(unittest.TestCase): def test_api(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.tensor.fill_constant( shape=[1], dtype='int64', value=5 ) expected_result = np.array([8], dtype='int64') output = paddle.tensor.math.increment(input, value=3) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) result = exe.run(fetch_list=[output]) self.assertEqual((result == expected_result).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): input = paddle.ones(shape=[1], dtype='int64') expected_result = np.array([2], dtype='int64') output = paddle.tensor.math.increment(input, value=1) @@ -42,11 +42,11 @@ def test_api(self): class TestInplaceApiWithDataTransform(unittest.TestCase): def test_increment(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): paddle.enable_static() - with paddle.fluid.device_guard("gpu:0"): + with paddle.base.device_guard("gpu:0"): x = paddle.tensor.fill_constant([1], "float32", 0) - with paddle.fluid.device_guard("cpu"): + with paddle.base.device_guard("cpu"): x = paddle.increment(x) exe = paddle.static.Executor(paddle.CUDAPlace(0)) (a,) = exe.run(paddle.static.default_main_program(), fetch_list=[x]) diff --git a/test/legacy_test/test_index_add_op.py b/test/legacy_test/test_index_add_op.py index 0e9b15f45438f..39279305a15c7 100644 --- a/test/legacy_test/test_index_add_op.py +++ b/test/legacy_test/test_index_add_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import Program, core +from paddle.base import Program, core def compute_index_add_ref( diff --git a/test/legacy_test/test_index_put_op.py b/test/legacy_test/test_index_put_op.py index 44e50c1567721..9ab02298f94dd 100644 --- a/test/legacy_test/test_index_put_op.py +++ b/test/legacy_test/test_index_put_op.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import Program +from paddle.base import Program def compute_index_put_ref(x_np, indices_np, value_np, accumulate=False): diff --git a/test/legacy_test/test_index_sample_op.py b/test/legacy_test/test_index_sample_op.py index b2fee3e3fd648..569e27203dd24 100755 --- a/test/legacy_test/test_index_sample_op.py +++ b/test/legacy_test/test_index_sample_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestIndexSampleOp(OpTest): @@ -211,9 +211,9 @@ def test_shape(self): index = paddle.static.data(name='index', shape=[-1, 3], dtype='int32') output = paddle.index_sample(x=x, index=index) - place = fluid.CPUPlace() - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place=place) + exe.run(base.default_startup_program()) feed = {'x': x_np, 'index': index_np} res = exe.run(feed=feed, fetch_list=[output]) @@ -221,7 +221,7 @@ def test_shape(self): class TestIndexSampleDynamic(unittest.TestCase): def test_result(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor( [ [1.0, 2.0, 3.0, 4.0], diff --git a/test/legacy_test/test_index_select_op.py b/test/legacy_test/test_index_select_op.py index 7bd578dcf68aa..7f6eac1a9423d 100644 --- a/test/legacy_test/test_index_select_op.py +++ b/test/legacy_test/test_index_select_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard np.random.seed(1024) @@ -88,8 +88,8 @@ def init_dtype_type(self): class TestIndexSelectOpCaseSingleThread(TestIndexSelectOp): def init_dtype_type(self): - if fluid.is_compiled_with_cuda(): - fluid.set_flags({'FLAGS_cudnn_deterministic': True}) + if base.is_compiled_with_cuda(): + base.set_flags({'FLAGS_cudnn_deterministic': True}) self.x_type = np.float32 self.index_type = np.int32 self.dim = -2 @@ -195,7 +195,7 @@ def test_index_select_api(self): x = paddle.static.data(name='x', shape=[-1, 4]) index = paddle.static.data(name='index', shape=[3], dtype='int32') z = paddle.index_select(x, index, axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'index': self.data_index}, fetch_list=[z.name], @@ -211,7 +211,7 @@ def test_index_select_api(self): x = paddle.static.data(name='x', shape=[-1, 4]) index = paddle.static.data(name='index', shape=[3], dtype='int32') z = paddle.index_select(x, index) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'index': self.data_index}, fetch_list=[z.name], @@ -226,9 +226,9 @@ def test_dygraph_api(self): paddle.disable_static() self.input_data() # case 1: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.index_select(x, index) np_z = z.numpy() expect_out = np.array( @@ -237,9 +237,9 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 2: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.index_select(x, index, axis=1) np_z = z.numpy() expect_out = np.array( diff --git a/test/legacy_test/test_infer_no_need_buffer_slots.py b/test/legacy_test/test_infer_no_need_buffer_slots.py index 53f9212c7db72..1ba17a9270c50 100644 --- a/test/legacy_test/test_infer_no_need_buffer_slots.py +++ b/test/legacy_test/test_infer_no_need_buffer_slots.py @@ -15,19 +15,19 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core, framework +from paddle import base +from paddle.base import core, framework class TestInferNoNeedBufferSlots(unittest.TestCase): def net(self): x1 = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_var(dtype="float32", shape=[1], lod_level=0, name="x1") ) x2 = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_var(dtype="float32", shape=[1], lod_level=0, name="x2") ) @@ -37,7 +37,7 @@ def net(self): def test_infer_no_need_buffer_slots(self): program = framework.Program() startup_program = framework.Program() - with fluid.program_guard(program, startup_program): + with base.program_guard(program, startup_program): loss = self.net() sgd = paddle.optimizer.SGD(learning_rate=0.01) sgd.minimize(loss) diff --git a/test/legacy_test/test_infer_shape.py b/test/legacy_test/test_infer_shape.py index 2b4f50a82646b..ba554f99086d5 100644 --- a/test/legacy_test/test_infer_shape.py +++ b/test/legacy_test/test_infer_shape.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core class TestInferShape(unittest.TestCase): diff --git a/test/legacy_test/test_inference_api.py b/test/legacy_test/test_inference_api.py index 4f8cb527f1c6b..b6f5456ee4796 100644 --- a/test/legacy_test/test_inference_api.py +++ b/test/legacy_test/test_inference_api.py @@ -19,8 +19,8 @@ paddle.enable_static() import numpy as np -from paddle import fluid -from paddle.fluid.core import PaddleDType, PaddleTensor +from paddle import base +from paddle.base.core import PaddleDType, PaddleTensor from paddle.framework import core from paddle.inference import ( Config, @@ -74,12 +74,12 @@ def test_inference_api(self): def get_sample_model(): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype="float32" ) @@ -103,12 +103,12 @@ def get_sample_model(): def get_sample_model_cuda(data_type): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): data = paddle.static.data( name="data", shape=[-1, 6, 64, 64], dtype=data_type ) @@ -181,10 +181,10 @@ def test_lod_tensor(): predictor = create_predictor(config) in_names = predictor.get_input_names() in_handle = predictor.get_input_handle(in_names[0]) - in_data = paddle.fluid.create_lod_tensor( + in_data = paddle.base.create_lod_tensor( np.full((1, 6, 32, 32), 1.0, "float32"), [[1]], - paddle.fluid.CPUPlace(), + paddle.base.CPUPlace(), ) in_handle.share_external_data(in_data) predictor.run() diff --git a/test/legacy_test/test_inference_model_io.py b/test/legacy_test/test_inference_model_io.py index 7db7a113b5284..2e179cf90276e 100644 --- a/test/legacy_test/test_inference_model_io.py +++ b/test/legacy_test/test_inference_model_io.py @@ -21,14 +21,14 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base +from paddle.base import core, executor +from paddle.base.compiler import CompiledProgram +from paddle.base.framework import Program, program_guard from paddle.distributed.io import ( load_inference_model_distributed, save_persistables, ) -from paddle.fluid import core, executor -from paddle.fluid.compiler import CompiledProgram -from paddle.fluid.framework import Program, program_guard from paddle.static.io import load_inference_model, save_inference_model paddle.enable_static() @@ -264,8 +264,8 @@ class TestSaveInferenceModelNew(unittest.TestCase): def test_save_and_load_inference_model(self): root_path = tempfile.TemporaryDirectory() MODEL_DIR = os.path.join(root_path.name, "inference_model5") - init_program = fluid.default_startup_program() - program = fluid.default_main_program() + init_program = base.default_startup_program() + program = base.default_main_program() # fake program without feed/fetch with program_guard(program, init_program): @@ -443,8 +443,8 @@ def test_save_and_load_inference_model(self): self.assertRaises(TypeError, paddle.static.io._get_valid_program, cp) def test_serialize_program_and_persistables(self): - init_program = fluid.default_startup_program() - program = fluid.default_main_program() + init_program = base.default_startup_program() + program = base.default_main_program() # fake program without feed/fetch with program_guard(program, init_program): @@ -492,8 +492,8 @@ def test_serialize_program_and_persistables(self): ) def test_normalize_program(self): - init_program = fluid.default_startup_program() - program = fluid.default_main_program() + init_program = base.default_startup_program() + program = base.default_main_program() # fake program without feed/fetch with program_guard(program, init_program): diff --git a/test/legacy_test/test_initializer.py b/test/legacy_test/test_initializer.py index 79b4782e6b6a3..52b2e4d5024dd 100644 --- a/test/legacy_test/test_initializer.py +++ b/test/legacy_test/test_initializer.py @@ -18,9 +18,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import framework -from paddle.fluid.core import VarDesc +from paddle import base +from paddle.base import framework +from paddle.base.core import VarDesc from paddle.regularizer import L2Decay DELTA = 0.00001 @@ -664,10 +664,10 @@ def test_set_global_weight_initilizer(self): """Test Set Global Param initilizer with UniformInitializer""" main_prog = framework.Program() startup_prog = framework.Program() - fluid.set_global_initializer( + base.set_global_initializer( paddle.nn.initializer.Uniform(low=-0.5, high=0.5) ) - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) # default initilizer of param in layers.conv2d is NormalInitializer conv = paddle.static.nn.conv2d(x, 5, 3) @@ -685,17 +685,17 @@ def test_set_global_weight_initilizer(self): self.assertAlmostEqual(param_init_op.attr('min'), -0.5, delta=DELTA) self.assertAlmostEqual(param_init_op.attr('max'), 0.5, delta=DELTA) self.assertEqual(param_init_op.attr('seed'), 0) - fluid.set_global_initializer(None) + base.set_global_initializer(None) def test_set_global_bias_initilizer(self): """Test Set Global Bias initilizer with NormalInitializer""" main_prog = framework.Program() startup_prog = framework.Program() - fluid.set_global_initializer( + base.set_global_initializer( paddle.nn.initializer.Uniform(low=-0.5, high=0.5), bias_init=paddle.nn.initializer.Normal(0.0, 2.0), ) - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) # default initilizer of bias in layers.conv2d is ConstantInitializer conv = paddle.static.nn.conv2d(x, 5, 3) @@ -715,7 +715,7 @@ def test_set_global_bias_initilizer(self): self.assertAlmostEqual(param_init_op.attr('min'), -0.5, delta=DELTA) self.assertAlmostEqual(param_init_op.attr('max'), 0.5, delta=DELTA) self.assertEqual(param_init_op.attr('seed'), 0) - fluid.set_global_initializer(None) + base.set_global_initializer(None) class TestUniformInitializerDygraph(unittest.TestCase): diff --git a/test/legacy_test/test_initializer_nn.py b/test/legacy_test/test_initializer_nn.py index e6fd87c497c66..b0b0e0bef268d 100644 --- a/test/legacy_test/test_initializer_nn.py +++ b/test/legacy_test/test_initializer_nn.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid, nn -from paddle.fluid import framework -from paddle.fluid.core import VarDesc +from paddle import base, nn +from paddle.base import framework +from paddle.base.core import VarDesc from paddle.nn import initializer DELTA = 0.00001 @@ -71,7 +71,7 @@ def test_constant_initializer_default_value_static(self, dtype="float32"): def test_constant_initializer_default_value_dygraph(self, dtype="float32"): """Test constant initializer with supplied value in dygraph""" - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear = nn.Linear(2, 4, weight_attr=nn.initializer.Constant()) mat_target = np.ones((2, 4), dtype=dtype) * 0.0 mat_linear = linear.weight.numpy() @@ -89,7 +89,7 @@ def test_constant_initializer_static(self, dtype="float32"): def test_constant_initializer_dygraph(self, dtype="float32"): """Test constant initializer with supplied value in dygraph""" - with fluid.dygraph.guard(): + with base.dygraph.guard(): linear = nn.Linear( 2, 4, weight_attr=nn.initializer.Constant(value=2.0) ) diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 047db61ec0c77..3dd1213d38d9e 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -22,7 +22,7 @@ class TestInplace(unittest.TestCase): def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(np.ones((4, 2, 3)).astype(np.float32)) self.assertEqual(var.inplace_version, 0) @@ -41,7 +41,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones(shape=[4, 2, 3], dtype="float32") var_a.stop_gradient = False @@ -65,7 +65,7 @@ def test_backward_error(self): def test_backward_success_1(self): # var_b is modified inplace before using it, the inplace operator doesn't result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones(shape=[4, 2, 3], dtype="float32") var_a.stop_gradient = False @@ -80,7 +80,7 @@ def test_backward_success_1(self): def test_backward_success_2(self): # Although var_b is modified inplace after using it, it does not used in gradient computation. # The inplace operator doesn't result in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones(shape=[4, 2, 3], dtype="float32") var_a.stop_gradient = False @@ -133,7 +133,7 @@ def test_forward_result(self): ) def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -147,7 +147,7 @@ def test_forward_version(self): self.assertEqual(var.inplace_version, 3) def test_leaf_inplace_var_error(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var.stop_gradient = False @@ -159,7 +159,7 @@ def leaf_inplace_error(): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -183,7 +183,7 @@ def test_backward_success_1(self): # var_b is modified inplace before using it, the inplace operator doesn't result # in incorrect gradient computation. grad_var_a, grad_var_a_inplace = 0, 1 - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -199,7 +199,7 @@ def test_backward_success_1(self): loss.backward() grad_var_a_inplace = var_a.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -216,7 +216,7 @@ def test_backward_success_2(self): # Although var_b is modified inplace after using it, it does not used in gradient computation. # The inplace operator doesn't result in incorrect gradient computation. grad_var_a, grad_var_a_inplace = 0, 1 - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -235,7 +235,7 @@ def test_backward_success_2(self): loss.backward() grad_var_a_inplace = var_a.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -275,7 +275,7 @@ def test_continuous_inplace_backward(self): # The api that only relies on input to calculate the gradient will copy input before # the inpalce calculation, so here supports continuous inpalce backward calculation. grad_var_a, grad_var_a_inplace = 0, 1 - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -287,7 +287,7 @@ def test_continuous_inplace_backward(self): loss.backward() grad_var_a_inplace = var_a.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -518,7 +518,7 @@ def test_backward_success_2(self): class TestLossIsInplaceVar(unittest.TestCase): def test_loss_is_inplace_var(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones((2, 2)) var_a.stop_gradient = False @@ -528,7 +528,7 @@ def test_loss_is_inplace_var(self): loss.backward() inplace_grad_var_a = var_a.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones((2, 2)) var_a.stop_gradient = False @@ -861,7 +861,7 @@ def set_np_compare_func(self): self.np_compare = np_array_equal_with_nan def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -877,7 +877,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -920,7 +920,7 @@ def non_inplace_api_processing(self, var): return paddle.lcm(var, self.y) def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -961,7 +961,7 @@ def test_leaf_inplace_var_error(self): pass def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -977,7 +977,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype("float64") var_a.stop_gradient = False @@ -1039,7 +1039,7 @@ def non_inplace_api_processing(self, var): return paddle.where(var > self.y, var, self.y) def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -1055,7 +1055,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -1344,7 +1344,7 @@ def non_inplace_api_processing(self, var): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype("float64") var_a.stop_gradient = False @@ -1364,7 +1364,7 @@ def test_backward_error(self): loss.backward() def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) @@ -1388,7 +1388,7 @@ def non_inplace_api_processing(self, var): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) var_a.stop_gradient = False @@ -1408,7 +1408,7 @@ def test_backward_error(self): loss.backward() def test_forward_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(self.input_var_numpy).astype(self.dtype) self.assertEqual(var.inplace_version, 0) diff --git a/test/legacy_test/test_inplace_abn_op.py b/test/legacy_test/test_inplace_abn_op.py index 94c04ba41e4e1..d56a467a2ed79 100644 --- a/test/legacy_test/test_inplace_abn_op.py +++ b/test/legacy_test/test_inplace_abn_op.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestInplaceANBOpTraining(unittest.TestCase): @@ -42,12 +42,12 @@ def build_program( use_cuda=False, inplace=False, ): - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() main.random_seed = seed startup.random_seed = seed - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + with base.unique_name.guard(): + with base.program_guard(main, startup): data = paddle.static.data( name='input', shape=self.dshape, @@ -58,8 +58,8 @@ def build_program( bn = paddle.static.nn.batch_norm( data, - param_attr=fluid.ParamAttr(name='bn_scale'), - bias_attr=fluid.ParamAttr(name='bn_bias'), + param_attr=base.ParamAttr(name='bn_scale'), + bias_attr=base.ParamAttr(name='bn_bias'), moving_mean_name='bn_moving_mean', moving_variance_name='bn_moving_variance', data_layout=layout, @@ -103,7 +103,7 @@ def test_all_branches(self): use_cuda, False, ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) exe.run(program=main, feed={'input': data}) diff --git a/test/legacy_test/test_inplace_addto_strategy.py b/test/legacy_test/test_inplace_addto_strategy.py index 225b460f1ae4a..657b870c1c097 100644 --- a/test/legacy_test/test_inplace_addto_strategy.py +++ b/test/legacy_test/test_inplace_addto_strategy.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class ConvBNLayer(paddle.nn.Layer): @@ -54,9 +54,9 @@ def forward(self, inputs): def create_program(data_format="NCHW"): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): x = paddle.static.data(name='img', shape=[-1, 3, 224, 224]) x.stop_gradient = False if data_format == "NHWC": @@ -84,20 +84,20 @@ def run_program(enable_addto): np.random.seed(10) paddle.seed(10) paddle.framework.random._manual_program_seed(10) - if fluid.core.is_compiled_with_cuda(): - fluid.set_flags({"FLAGS_cudnn_deterministic": True}) - fluid.set_flags({"FLAGS_max_inplace_grad_add": 2}) + if base.core.is_compiled_with_cuda(): + base.set_flags({"FLAGS_cudnn_deterministic": True}) + base.set_flags({"FLAGS_max_inplace_grad_add": 2}) loss, main, startup, w = create_program(data_format=data_format) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) - strategy = fluid.BuildStrategy() + strategy = base.BuildStrategy() strategy.enable_addto = enable_addto - compiled = fluid.CompiledProgram(main, build_strategy=strategy) + compiled = base.CompiledProgram(main, build_strategy=strategy) exe.run(startup) img = np.random.uniform(-128, 128, [8, 3, 224, 224]).astype( diff --git a/test/legacy_test/test_inplace_softmax_with_cross_entropy.py b/test/legacy_test/test_inplace_softmax_with_cross_entropy.py index 8f2263ee74c58..64bb0fbc669b4 100644 --- a/test/legacy_test/test_inplace_softmax_with_cross_entropy.py +++ b/test/legacy_test/test_inplace_softmax_with_cross_entropy.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestSoftmaxWithXe(unittest.TestCase): @@ -35,8 +35,8 @@ def softmax_with_xe( self, x, y, place, inplace=True, numeric_stable_mode=True ): m, n = x.shape - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.Scope()): x_d = paddle.static.data( name='x', shape=[m, n], @@ -57,14 +57,14 @@ def softmax_with_xe( numeric_stable_mode=numeric_stable_mode, ) - exe = fluid.Executor(place) + exe = base.Executor(place) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.enable_inplace = inplace - prog = fluid.CompiledProgram( - fluid.default_main_program(), build_strategy=build_strategy + prog = base.CompiledProgram( + base.default_main_program(), build_strategy=build_strategy ) fetch_list = [z_d.name, s_d.name] @@ -113,9 +113,9 @@ def main_with_place(self, place): self.assertTrue((s1 == s2).all()) def test_main(self): - self.main_with_place(fluid.CPUPlace()) - if fluid.core.is_compiled_with_cuda(): - self.main_with_place(fluid.CUDAPlace(0)) + self.main_with_place(base.CPUPlace()) + if base.core.is_compiled_with_cuda(): + self.main_with_place(base.CUDAPlace(0)) class TestSoftmaxWithXe1(TestSoftmaxWithXe): diff --git a/test/legacy_test/test_input_spec.py b/test/legacy_test/test_input_spec.py index 7234e6414c8d7..47c461a2a1eab 100644 --- a/test/legacy_test/test_input_spec.py +++ b/test/legacy_test/test_input_spec.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base import core +from paddle.base.framework import convert_np_dtype_to_dtype_ from paddle.jit.dy2static.utils import _compatible_non_tensor_spec from paddle.static import InputSpec diff --git a/test/legacy_test/test_instance_norm_op.py b/test/legacy_test/test_instance_norm_op.py index fe8ed7bf150c1..b4bd1401ddea9 100644 --- a/test/legacy_test/test_instance_norm_op.py +++ b/test/legacy_test/test_instance_norm_op.py @@ -19,9 +19,9 @@ from eager_op_test import OpTest import paddle -from paddle import fluid, nn -from paddle.fluid import Program, core, program_guard -from paddle.fluid.dygraph import to_variable +from paddle import base, nn +from paddle.base import Program, core, program_guard +from paddle.base.dygraph import to_variable def _reference_instance_norm_naive(x, scale, bias, epsilon, mean, var): @@ -325,9 +325,9 @@ def setUp(self): self.static_rev_desire[-1].append(rev[2]) def get_eager_desire(self, place): - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") core.set_prim_eager_enabled(False) paddle.disable_static() @@ -350,9 +350,9 @@ def get_eager_desire(self, place): def get_static_desire(self, place): core._set_prim_all_enabled(False) paddle.enable_static() - if isinstance(place, fluid.CPUPlace): + if isinstance(place, base.CPUPlace): paddle.set_device("cpu") - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): paddle.set_device("gpu") mp, sp = paddle.static.Program(), paddle.static.Program() @@ -428,7 +428,7 @@ def test_static_comp(self): if len(self.places) < 1: return - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): for place in self.places: fwd_actual.append([]) rev_actual.append([]) @@ -783,8 +783,8 @@ def test_with_place(place, shape): ] ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -827,7 +827,7 @@ def test_with_place(place, shape): program._sync_with_cpp() - exe = fluid.Executor(place) + exe = base.Executor(place) out = exe.run( program, feed={ @@ -871,8 +871,8 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # the input of instance_norm must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.static.nn.instance_norm, x1) @@ -918,7 +918,7 @@ def test_norm(self): ) for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): instance_norm = paddle.nn.InstanceNorm2D( 5, weight_attr=False, bias_attr=False ) @@ -952,7 +952,7 @@ def test_norm(self): ) for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): instance_norm = paddle.nn.InstanceNorm2D( 3, weight_attr=True, bias_attr=True ) diff --git a/test/legacy_test/test_instance_norm_op_v2.py b/test/legacy_test/test_instance_norm_op_v2.py index 8b6745b17bb1d..9b59b2d813616 100644 --- a/test/legacy_test/test_instance_norm_op_v2.py +++ b/test/legacy_test/test_instance_norm_op_v2.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def instance_norm_wrapper( @@ -79,27 +79,27 @@ def _reference_instance_norm_grad(x, scale, mean, var): class TestInstanceNorm(unittest.TestCase): def test_error(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu( "instance_norm" ): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: def error1d(): x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') instance_norm1d = paddle.nn.InstanceNorm1D(1) - instance_norm1d(fluid.dygraph.to_variable(x_data_4)) + instance_norm1d(base.dygraph.to_variable(x_data_4)) def error2d(): x_data_3 = np.random.random(size=(2, 1, 3)).astype('float32') instance_norm2d = paddle.nn.InstanceNorm2D(1) - instance_norm2d(fluid.dygraph.to_variable(x_data_3)) + instance_norm2d(base.dygraph.to_variable(x_data_3)) def error3d(): x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') instance_norm3d = paddle.nn.InstanceNorm3D(1) - instance_norm3d(fluid.dygraph.to_variable(x_data_4)) + instance_norm3d(base.dygraph.to_variable(x_data_4)) def weight_bias_false(): x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') @@ -107,31 +107,31 @@ def weight_bias_false(): 1, weight_attr=False, bias_attr=False ) - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): weight_bias_false() self.assertRaises(ValueError, error1d) self.assertRaises(ValueError, error2d) self.assertRaises(ValueError, error3d) def test_dygraph(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu( "instance_norm" ): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v1(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.InstanceNorm2D(shape[1]) - y = bn(fluid.dygraph.to_variable(x)) + y = bn(base.dygraph.to_variable(x)) return y.numpy() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): bn = paddle.nn.InstanceNorm2D(shape[1]) - y = bn(fluid.dygraph.to_variable(x)) + y = bn(base.dygraph.to_variable(x)) return y.numpy() x = np.random.randn(*shape).astype("float32") @@ -140,14 +140,14 @@ def compute_v2(x): np.testing.assert_allclose(y1, y2, rtol=1e-05) def test_static(self): - with paddle.fluid.framework._static_guard(): - places = [fluid.CPUPlace()] + with paddle.base.framework._static_guard(): + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu( "instance_norm" ): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: - exe = fluid.Executor(p) + exe = base.Executor(p) shape = [4, 10, 16, 16] def compute_v1(x_np): @@ -157,7 +157,7 @@ def compute_v1(x_np): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = ins(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -168,7 +168,7 @@ def compute_v2(x_np): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = ins(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -392,7 +392,7 @@ def train(self, use_amp, data_layout="NCHW"): def test_amp_nchw(self): if not isinstance( - paddle.fluid.framework._current_expected_place(), core.CPUPlace + paddle.base.framework._current_expected_place(), core.CPUPlace ): expected = self.train(False) actual = self.train(True) diff --git a/test/legacy_test/test_inverse_op.py b/test/legacy_test/test_inverse_op.py index de0527e9661be..da4c74e62814f 100644 --- a/test/legacy_test/test_inverse_op.py +++ b/test/legacy_test/test_inverse_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestInverseOp(OpTest): @@ -90,12 +90,12 @@ def config(self): class TestInverseAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float64" ) @@ -103,9 +103,9 @@ def check_static_result(self, place): input_np = np.random.random([4, 4]).astype("float64") result_np = np.linalg.inv(input_np) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -119,9 +119,9 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([4, 4]).astype("float64") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = paddle.inverse(input) np.testing.assert_allclose( result.numpy(), np.linalg.inv(input_np), rtol=1e-05 @@ -156,12 +156,12 @@ def test_errors(self): class TestInverseSingularAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float64" ) @@ -169,10 +169,10 @@ def check_static_result(self, place): input_np = np.zeros([4, 4]).astype("float64") - exe = fluid.Executor(place) + exe = base.Executor(place) try: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -187,9 +187,9 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.ones([4, 4]).astype("float64") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) try: result = paddle.inverse(input) except RuntimeError as ex: diff --git a/test/legacy_test/test_io_save_load.py b/test/legacy_test/test_io_save_load.py index a6e125d3d3298..b108f63e75e54 100644 --- a/test/legacy_test/test_io_save_load.py +++ b/test/legacy_test/test_io_save_load.py @@ -17,8 +17,8 @@ import unittest import paddle -from paddle import fluid, static -from paddle.fluid import core +from paddle import base, static +from paddle.base import core class TestSaveLoadAPIError(unittest.TestCase): @@ -32,7 +32,7 @@ def tearDown(self): def test_get_valid_program_error(self): # case 1: CompiledProgram no program graph = core.Graph(core.ProgramDesc()) - compiled_program = fluid.CompiledProgram(graph) + compiled_program = base.CompiledProgram(graph) with self.assertRaises(TypeError): paddle.static.io._get_valid_program(compiled_program) @@ -41,8 +41,8 @@ def test_get_valid_program_error(self): paddle.static.io._get_valid_program("program") def test_load_vars_error(self): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) # case 1: main_program type error when vars None with self.assertRaises(TypeError): static.io.load_vars( @@ -67,14 +67,14 @@ def tearDown(self): self.temp_dir.cleanup() def test_useless_feeded_var_names(self): - start_prog = fluid.Program() - main_prog = fluid.Program() - with fluid.program_guard(main_prog, start_prog): + start_prog = base.Program() + main_prog = base.Program() + with base.program_guard(main_prog, start_prog): x = paddle.static.data(name='x', shape=[10, 16], dtype='float32') y = paddle.static.data(name='y', shape=[10, 16], dtype='float32') z = paddle.static.nn.fc(x, 4) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(start_prog) with self.assertRaisesRegex( ValueError, "not involved in the target_vars calculation" diff --git a/test/legacy_test/test_ir_graph.py b/test/legacy_test/test_ir_graph.py index b563024e5bf2a..79db30cfd16b2 100644 --- a/test/legacy_test/test_ir_graph.py +++ b/test/legacy_test/test_ir_graph.py @@ -14,7 +14,7 @@ import unittest -from paddle import fluid +from paddle import base class TestIRGraph(unittest.TestCase): @@ -51,23 +51,23 @@ def test_erase(self): self.assertFalse(graph.has("test")) def test_create_var_node(self): - prog = fluid.core.ProgramDesc() + prog = base.core.ProgramDesc() block = prog.block(0) shape = [10, 20] x1 = block.var(b'x1') - x1.set_type(fluid.core.VarDesc.VarType.LOD_TENSOR) + x1.set_type(base.core.VarDesc.VarType.LOD_TENSOR) x1.set_shape(shape) - graph = fluid.core.Graph(prog) + graph = base.core.Graph(prog) node = graph.create_var_node(x1) - self.assertTrue(node.node_type() == fluid.core.Node.Type.Variable) + self.assertTrue(node.node_type() == base.core.Node.Type.Variable) def test_create_op_node(self): - prog = fluid.core.ProgramDesc() + prog = base.core.ProgramDesc() block = prog.block(0) sum_op_desc = block.append_op() - graph = fluid.core.Graph(prog) + graph = base.core.Graph(prog) node = graph.create_op_node(sum_op_desc) - self.assertTrue(node.node_type() == fluid.core.Node.Type.Operation) + self.assertTrue(node.node_type() == base.core.Node.Type.Operation) def test_create_control_dep_var(self): graph = build_graph() @@ -76,11 +76,11 @@ def test_create_control_dep_var(self): self.assertTrue(node.name() == name) def test_create_empty_node(self): - prog = fluid.core.ProgramDesc() - graph = fluid.core.Graph(prog) - n1 = graph.create_empty_node('x', fluid.core.Node.Type.Operation) + prog = base.core.ProgramDesc() + graph = base.core.Graph(prog) + n1 = graph.create_empty_node('x', base.core.Node.Type.Operation) self.assertTrue(n1.name() == 'x') - n2 = graph.create_empty_node('y', fluid.core.Node.Type.Variable) + n2 = graph.create_empty_node('y', base.core.Node.Type.Variable) self.assertTrue(n2.name() == 'y') def test_release_nodes(self): @@ -117,21 +117,21 @@ def resolve_hazard(self): def build_graph(): - prog = fluid.core.ProgramDesc() + prog = base.core.ProgramDesc() block = prog.block(0) shape = [10, 20] # prepare input/output x1 = block.var(b'x1') - x1.set_type(fluid.core.VarDesc.VarType.LOD_TENSOR) + x1.set_type(base.core.VarDesc.VarType.LOD_TENSOR) x1.set_shape(shape) x2 = block.var(b'x2') - x2.set_type(fluid.core.VarDesc.VarType.LOD_TENSOR) + x2.set_type(base.core.VarDesc.VarType.LOD_TENSOR) x2.set_shape(shape) out = block.var(b'out') - out.set_type(fluid.core.VarDesc.VarType.LOD_TENSOR) + out.set_type(base.core.VarDesc.VarType.LOD_TENSOR) sum_op_desc = block.append_op() sum_op_desc.set_type("sum") @@ -140,7 +140,7 @@ def build_graph(): sum_op_desc.check_attrs() sum_op_desc.infer_shape(block) - graph = fluid.core.Graph(prog) + graph = base.core.Graph(prog) return graph diff --git a/test/legacy_test/test_ir_inplace_pass.py b/test/legacy_test/test_ir_inplace_pass.py index 4aed1bc642fef..c5a5be1168f87 100644 --- a/test/legacy_test/test_ir_inplace_pass.py +++ b/test/legacy_test/test_ir_inplace_pass.py @@ -19,8 +19,8 @@ from parallel_executor_test_base import DeviceType, TestParallelExecutorBase import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def fc_with_batchnorm(use_feed): @@ -33,7 +33,7 @@ def fc_with_batchnorm(use_feed): hidden, size=200, activation='tanh', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) diff --git a/test/legacy_test/test_ir_memory_optimize_pass.py b/test/legacy_test/test_ir_memory_optimize_pass.py index 4fff201519ca2..6112d0aedd7ad 100644 --- a/test/legacy_test/test_ir_memory_optimize_pass.py +++ b/test/legacy_test/test_ir_memory_optimize_pass.py @@ -18,7 +18,7 @@ from parallel_executor_test_base import DeviceType, TestParallelExecutorBase import paddle -from paddle.fluid import core +from paddle.base import core def _feed_data_helper(): diff --git a/test/legacy_test/test_ir_memory_optimize_transformer.py b/test/legacy_test/test_ir_memory_optimize_transformer.py index 6c87727c4bb30..b3dc82c12e636 100644 --- a/test/legacy_test/test_ir_memory_optimize_transformer.py +++ b/test/legacy_test/test_ir_memory_optimize_transformer.py @@ -15,7 +15,7 @@ import os import unittest -from paddle.fluid import core +from paddle.base import core os.environ['FLAGS_eager_delete_tensor_gb'] = "0.0" diff --git a/test/legacy_test/test_isclose_op.py b/test/legacy_test/test_isclose_op.py index 2074a160c5b3d..5f17a9b6e7055 100644 --- a/test/legacy_test/test_isclose_op.py +++ b/test/legacy_test/test_isclose_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core class TestIscloseOp(OpTest): @@ -118,9 +118,9 @@ def test_api_case(self): paddle.enable_static() x_data = np.random.rand(10, 10) y_data = np.random.rand(10, 10) - places = [paddle.fluid.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): - places.append(paddle.fluid.CUDAPlace(0)) + places = [paddle.base.CPUPlace()] + if paddle.base.core.is_compiled_with_cuda(): + places.append(paddle.base.CUDAPlace(0)) for place in places: with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -132,9 +132,9 @@ def test_api_case(self): name='y', shape=[10, 10], dtype='float64' ) result = paddle.isclose(x, y) - exe = paddle.fluid.Executor(place) + exe = paddle.base.Executor(place) fetches = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={"x": x_data, "y": y_data}, fetch_list=[result], ) @@ -145,7 +145,7 @@ def test_api_case(self): class TestIscloseDygraph(unittest.TestCase): def test_api_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() diff --git a/test/legacy_test/test_isfinite_op.py b/test/legacy_test/test_isfinite_op.py index d5a409489d866..74bb329a8df39 100755 --- a/test/legacy_test/test_isfinite_op.py +++ b/test/legacy_test/test_isfinite_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest, convert_float_to_uint16 -from paddle.fluid import core +from paddle.base import core class TestInf(OpTest): diff --git a/test/legacy_test/test_isfinite_v2_op.py b/test/legacy_test/test_isfinite_v2_op.py index 6c3a780aeda03..5c9faa507897d 100644 --- a/test/legacy_test/test_isfinite_v2_op.py +++ b/test/legacy_test/test_isfinite_v2_op.py @@ -17,18 +17,18 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def run_static(x_np, dtype, op_str, use_gpu=False): paddle.enable_static() - startup_program = fluid.Program() - main_program = fluid.Program() + startup_program = base.Program() + main_program = base.Program() place = paddle.CPUPlace() - if use_gpu and fluid.core.is_compiled_with_cuda(): + if use_gpu and base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) - exe = fluid.Executor(place) - with fluid.program_guard(main_program, startup_program): + exe = base.Executor(place) + with base.program_guard(main_program, startup_program): x = paddle.static.data(name='x', shape=x_np.shape, dtype=dtype) res = getattr(paddle.tensor, op_str)(x) exe.run(startup_program) @@ -40,7 +40,7 @@ def run_static(x_np, dtype, op_str, use_gpu=False): def run_dygraph(x_np, op_str, use_gpu=True): place = paddle.CPUPlace() - if use_gpu and fluid.core.is_compiled_with_cuda(): + if use_gpu and base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) paddle.disable_static(place) x = paddle.to_tensor(x_np) @@ -49,9 +49,9 @@ def run_dygraph(x_np, op_str, use_gpu=True): def run_eager(x_np, op_str, use_gpu=True): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): place = paddle.CPUPlace() - if use_gpu and fluid.core.is_compiled_with_cuda(): + if use_gpu and base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) x = paddle.to_tensor(x_np) @@ -152,7 +152,7 @@ def test_finite(self): class TestError(unittest.TestCase): def test_bad_input(self): paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_isinf_bad_x(): x = [1, 2, 3] diff --git a/test/legacy_test/test_jit_layer.py b/test/legacy_test/test_jit_layer.py index e36cea92de780..e488995764539 100644 --- a/test/legacy_test/test_jit_layer.py +++ b/test/legacy_test/test_jit_layer.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid.framework import _dygraph_place_guard +from paddle.base.framework import _dygraph_place_guard from paddle.jit.layer import Layer from paddle.static import InputSpec diff --git a/test/legacy_test/test_jit_save_load.py b/test/legacy_test/test_jit_save_load.py index 40bf389b5f190..e2df76f475194 100644 --- a/test/legacy_test/test_jit_save_load.py +++ b/test/legacy_test/test_jit_save_load.py @@ -22,8 +22,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import unique_name +from paddle import base +from paddle.base import unique_name from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_PARAMS_INFO_SUFFIX from paddle.nn import Linear @@ -304,7 +304,7 @@ def train(layer, input_size=784, label_size=1): learning_rate=0.01, parameter_list=layer.parameters() ) # create data loader - train_loader = fluid.io.DataLoader.from_generator(capacity=5) + train_loader = base.io.DataLoader.from_generator(capacity=5) train_loader.set_batch_generator( random_batch_reader(input_size, label_size) ) @@ -332,7 +332,7 @@ def train_with_label(layer, input_size=784, label_size=1): learning_rate=0.01, parameters=layer.parameters() ) # create data loader - train_loader = fluid.io.DataLoader.from_generator(capacity=5) + train_loader = base.io.DataLoader.from_generator(capacity=5) train_loader.set_batch_generator( random_batch_reader(input_size, label_size) ) @@ -356,7 +356,7 @@ def setUp(self): self.temp_dir.name, "test_jit_save_load/model" ) # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() # config seed paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -389,7 +389,7 @@ def load_and_inference(self, train_layer, infer_layer): train_layer.eval() infer_layer.eval() # inference & compare - x = fluid.dygraph.to_variable( + x = base.dygraph.to_variable( np.random.random((1, 784)).astype('float32') ) np.testing.assert_array_equal( @@ -417,7 +417,7 @@ def load_dygraph_state_dict(self, train_layer): new_layer.set_state_dict(load_state_dict) new_layer.eval() # inference & compare - x = fluid.dygraph.to_variable( + x = base.dygraph.to_variable( np.random.random((1, 784)).astype('float32') ) np.testing.assert_array_equal( @@ -442,16 +442,14 @@ def test_jit_load_no_path(self): class TestSaveLoadWithNestOut(unittest.TestCase): def setUp(self): # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() self.temp_dir = tempfile.TemporaryDirectory() def tearDown(self): self.temp_dir.cleanup() def test_nest_output(self): - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) net = LinearNetWithNestOut(8, 8) dy_outs = paddle.utils.flatten(net(x)) @@ -478,8 +476,8 @@ def test_dict_input(self): net = LinearNetWithDictInput(8, 8) # net.forward.concrete_program.inputs: # (<__main__.LinearNetWithDictInput object at 0x7f2655298a98>, - # {'img': var img : fluid.VarType.LOD_TENSOR.shape(-1, 8).astype(VarType.FP32)}, - # {'label': var label : fluid.VarType.LOD_TENSOR.shape(-1, 1).astype(VarType.INT64)}) + # {'img': var img : base.VarType.LOD_TENSOR.shape(-1, 8).astype(VarType.FP32)}, + # {'label': var label : base.VarType.LOD_TENSOR.shape(-1, 1).astype(VarType.INT64)}) self.assertEqual(len(net.forward.concrete_program.inputs), 3) temp_dir = tempfile.TemporaryDirectory() path = os.path.join( @@ -539,7 +537,7 @@ def test_dict_input(self): class TestSaveLoadWithInputSpec(unittest.TestCase): def setUp(self): # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() self.temp_dir = tempfile.TemporaryDirectory() def tearDown(self): @@ -567,9 +565,7 @@ def test_with_input_spec(self): # 2. load to infer infer_layer = paddle.jit.load(model_path) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) pred = infer_layer(x) def test_multi_in_out(self): @@ -591,12 +587,8 @@ def test_multi_in_out(self): # 3. load to infer infer_layer = paddle.jit.load(model_path) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) - y = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) + y = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) # 4. predict pred_x, pred_y = infer_layer(x, y) @@ -633,12 +625,8 @@ def test_multi_in_out1(self): # 3. load to infer infer_layer = paddle.jit.load(model_path) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) - y = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) + y = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) # 4. predict pred_x, pred_y = infer_layer(x, y) @@ -666,7 +654,7 @@ def test_multi_in_out1(self): class TestJitSaveLoadConfig(unittest.TestCase): def setUp(self): # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() # config seed paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -680,9 +668,7 @@ def test_output_spec(self): adam = paddle.optimizer.Adam( learning_rate=0.1, parameters=train_layer.parameters() ) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) for i in range(10): out, loss = train_layer(x) loss.backward() @@ -702,9 +688,7 @@ def test_output_spec(self): train_layer.eval() infer_layer = paddle.jit.load(model_path) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) np.testing.assert_array_equal( train_layer(x)[0].numpy(), infer_layer(x).numpy() ) @@ -739,7 +723,7 @@ def setUp(self): self.temp_dir.name, "jit_multi_load/model" ) # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() # config seed paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -775,7 +759,7 @@ def setUp(self): self.temp_dir.name, "jit_prune_model_and_load/model" ) # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() # config seed paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) @@ -788,9 +772,7 @@ def train_and_save(self): adam = paddle.optimizer.Adam( learning_rate=0.1, parameters=train_layer.parameters() ) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) for i in range(10): hidden, loss = train_layer(x) loss.backward() @@ -813,9 +795,7 @@ def test_load_pruned_model(self): infer_layer = paddle.jit.load(self.model_path) - x = fluid.dygraph.to_variable( - np.random.random((4, 8)).astype('float32') - ) + x = base.dygraph.to_variable(np.random.random((4, 8)).astype('float32')) np.testing.assert_array_equal( train_layer(x)[0].numpy(), infer_layer(x).numpy() ) @@ -838,7 +818,7 @@ def test_load_var_not_in_extra_var_info(self): class TestJitSaveMultiCases(unittest.TestCase): def setUp(self): # enable dygraph mode - fluid.enable_dygraph() + base.enable_dygraph() # config seed paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) diff --git a/test/legacy_test/test_kldiv_loss_op.py b/test/legacy_test/test_kldiv_loss_op.py index f16cd781c2bbe..02dc6151d32b8 100644 --- a/test/legacy_test/test_kldiv_loss_op.py +++ b/test/legacy_test/test_kldiv_loss_op.py @@ -89,7 +89,7 @@ def run_kl_loss(self, reduction, shape=(5, 20)): target = np.random.uniform(-10, 10, shape).astype('float64') gt_loss = kldiv_loss(x, target, reduction) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): kldiv_criterion = paddle.nn.KLDivLoss(reduction) pred_loss = kldiv_criterion( paddle.to_tensor(x), paddle.to_tensor(target) @@ -123,7 +123,7 @@ def test_kl_loss_static_api(self): class TestKLDivLossTypePromotion(unittest.TestCase): def test_kl_div_promotion(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x1 = paddle.rand([5, 20], dtype='float32') target1 = paddle.rand([5, 20], dtype='float64') diff --git a/test/legacy_test/test_kron_op.py b/test/legacy_test/test_kron_op.py index 735e8bd1e3203..d5eac58233f12 100644 --- a/test/legacy_test/test_kron_op.py +++ b/test/legacy_test/test_kron_op.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -import paddle.fluid.dygraph as dg -from paddle import fluid -from paddle.fluid import core +import paddle.base.dygraph as dg +from paddle import base +from paddle.base import core class TestKronOp(OpTest): @@ -123,7 +123,7 @@ def test_case(self): a = np.random.randn(10, 10).astype(np.float64) b = np.random.randn(10, 10).astype(np.float64) - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): a_var = dg.to_variable(a) b_var = dg.to_variable(b) @@ -134,16 +134,16 @@ def test_case_with_output(self): a = np.random.randn(10, 10).astype(np.float64) b = np.random.randn(10, 10).astype(np.float64) - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): a_var = paddle.static.data("a", [-1, -1], dtype="float64") b_var = paddle.static.data("b", [-1, -1], dtype="float64") out_var = paddle.kron(a_var, b_var) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(start) (c,) = exe.run(main, feed={'a': a, 'b': b}, fetch_list=[out_var]) np.testing.assert_allclose(c, np.kron(a, b)) @@ -160,8 +160,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} diff --git a/test/legacy_test/test_kthvalue_op.py b/test/legacy_test/test_kthvalue_op.py index e2fa225fd8f7e..463ee34b880e8 100644 --- a/test/legacy_test/test_kthvalue_op.py +++ b/test/legacy_test/test_kthvalue_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def cal_kthvalue(x, k, axis, keepdim=False): @@ -141,7 +141,7 @@ def test_gpu_kernel(): ) test_cpu_kernel() - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): test_gpu_kernel() @@ -168,7 +168,7 @@ def test_nan_in_gpu_kernel(): self.assertEqual(inds[0, 2].numpy(), nan_position) test_nan_in_cpu_kernel() - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): test_nan_in_gpu_kernel() diff --git a/test/legacy_test/test_l1_loss.py b/test/legacy_test/test_l1_loss.py index 20295d1a9a92b..651d55977b34c 100644 --- a/test/legacy_test/test_l1_loss.py +++ b/test/legacy_test/test_l1_loss.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestFunctionalL1Loss(unittest.TestCase): @@ -55,9 +55,9 @@ def run_static(self, use_gpu=False): result2 = paddle.nn.functional.l1_loss(input, label, reduction='none') y = paddle.nn.functional.l1_loss(input, label, name='aaa') - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) static_result = exe.run( feed={"input": self.input_np, "label": self.label_np}, fetch_list=[result0, result1, result2], @@ -73,22 +73,22 @@ def run_static(self, use_gpu=False): self.assertTrue('aaa' in y.name) def test_cpu(self): - paddle.disable_static(place=paddle.fluid.CPUPlace()) + paddle.disable_static(place=paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(place=paddle.fluid.CUDAPlace(0)) + paddle.disable_static(place=paddle.base.CUDAPlace(0)) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static(use_gpu=True) # test case the raise message @@ -149,9 +149,9 @@ def run_static(self, use_gpu=False): l1_loss = paddle.nn.loss.L1Loss(name='aaa') result3 = l1_loss(input, label) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) static_result = exe.run( feed={"input": self.input_np, "label": self.label_np}, fetch_list=[result0, result1, result2], @@ -166,22 +166,22 @@ def run_static(self, use_gpu=False): self.assertTrue('aaa' in result3.name) def test_cpu(self): - paddle.disable_static(place=paddle.fluid.CPUPlace()) + paddle.disable_static(place=paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(place=paddle.fluid.CUDAPlace(0)) + paddle.disable_static(place=paddle.base.CUDAPlace(0)) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static(use_gpu=True) # test case the raise message diff --git a/test/legacy_test/test_label_smooth_functional.py b/test/legacy_test/test_label_smooth_functional.py index 621559286ea6d..81f868c83c895 100644 --- a/test/legacy_test/test_label_smooth_functional.py +++ b/test/legacy_test/test_label_smooth_functional.py @@ -17,9 +17,9 @@ import numpy as np import paddle -import paddle.fluid.dygraph as dg +import paddle.base.dygraph as dg import paddle.nn.functional as F -from paddle import fluid +from paddle import base class LabelSmoothTestCase(unittest.TestCase): @@ -41,12 +41,12 @@ def __init__( def setUp(self): self.label = np.random.randn(*(self.label_shape)).astype(self.dtype) - def fluid_layer(self, place): + def base_layer(self, place): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): label_var = paddle.static.data( "input", self.label_shape, dtype=self.dtype ) @@ -56,17 +56,17 @@ def fluid_layer(self, place): epsilon=self.epsilon, ) feed_dict = {"input": self.label} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np def functional(self, place): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): label_var = paddle.static.data( "input", self.label_shape, dtype=self.dtype ) @@ -74,7 +74,7 @@ def functional(self, place): label_var, prior_dist=self.prior_dist, epsilon=self.epsilon ) feed_dict = {"input": self.label} - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (y_np,) = exe.run(main, feed=feed_dict, fetch_list=[y_var]) return y_np @@ -89,24 +89,24 @@ def paddle_dygraph_layer(self): return y_np def _test_equivalence(self, place): - place = fluid.CPUPlace() - result1 = self.fluid_layer(place) + place = base.CPUPlace() + result1 = self.base_layer(place) result2 = self.functional(place) result3 = self.paddle_dygraph_layer() np.testing.assert_array_almost_equal(result1, result2) np.testing.assert_array_almost_equal(result2, result3) def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self._test_equivalence(place) - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self._test_equivalence(place) class LabelSmoothErrorTestCase(LabelSmoothTestCase): def runTest(self): - place = fluid.CPUPlace() + place = base.CPUPlace() with dg.guard(place): with self.assertRaises(ValueError): self.paddle_dygraph_layer() diff --git a/test/legacy_test/test_label_smooth_op.py b/test/legacy_test/test_label_smooth_op.py index 5c45b6a9d89e2..d7cd2169a8ba7 100644 --- a/test/legacy_test/test_label_smooth_op.py +++ b/test/legacy_test/test_label_smooth_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core class TestLabelSmoothOp(OpTest): diff --git a/test/legacy_test/test_lamb_op.py b/test/legacy_test/test_lamb_op.py index 33b2ab65a8372..535b09bd8a448 100644 --- a/test/legacy_test/test_lamb_op.py +++ b/test/legacy_test/test_lamb_op.py @@ -19,7 +19,7 @@ from op import Operator import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_lambv2_op.py b/test/legacy_test/test_lambv2_op.py index b10eab7997055..b55d6dceb1319 100644 --- a/test/legacy_test/test_lambv2_op.py +++ b/test/legacy_test/test_lambv2_op.py @@ -17,14 +17,14 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import switch_to_static_graph class LAMBOptimizer(paddle.optimizer.Lamb): def _append_optimize_op(self, block, param_and_grad): - assert isinstance(block, fluid.framework.Block) + assert isinstance(block, base.framework.Block) block.program._use_lamb = True m = moment1 = self._get_accumulator( @@ -119,7 +119,7 @@ def test_lamb_op_with_multi_steps(self): paddle.enable_static() def _build_static_model(main, startup, seed=100): - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): main.random_seed = seed startup.random_seed = seed x = paddle.static.data( @@ -133,21 +133,21 @@ def _build_static_model(main, startup, seed=100): avg_loss = paddle.mean(loss) return avg_loss - place = fluid.CPUPlace() + place = base.CPUPlace() num_steps = 10 for i in range(num_steps): feed_x = np.random.random(size=(10, 13)).astype('float32') feed_y = np.random.random(size=(10, 1)).astype('float32') - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): avg_loss = _build_static_model(main_program, startup_program) lamb_kernel = paddle.optimizer.Lamb(learning_rate=0.2) lamb_kernel.minimize(avg_loss) - executor = fluid.Executor(place) + executor = base.Executor(place) executor.run(startup_program) output = executor.run( program=main_program, @@ -155,14 +155,14 @@ def _build_static_model(main, startup, seed=100): fetch_list=[avg_loss.name], ) - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): loss = _build_static_model(main, startup) lamb = LAMBOptimizer(learning_rate=0.2) lamb.minimize(loss) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) out = exe.run( program=main, diff --git a/test/legacy_test/test_layer_norm_op.py b/test/legacy_test/test_layer_norm_op.py index 0572ec64fe37d..76ce8bb2934b5 100644 --- a/test/legacy_test/test_layer_norm_op.py +++ b/test/legacy_test/test_layer_norm_op.py @@ -25,8 +25,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard from paddle.static.amp.fp16_utils import _keep_layer_norm_scale_bias_to_fp32 paddle.enable_static() @@ -584,8 +584,8 @@ def test_with_place( var_names += ['bias'] ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -638,7 +638,7 @@ def test_with_place( grad_var.set_dtype(core.VarDesc.VarType.FP32) program._sync_with_cpp() - exe = fluid.Executor(place) + exe = base.Executor(place) out = exe.run( program, feed={ @@ -859,7 +859,7 @@ def check_main(self, x_np, weight_np, bias_np, dtype): bias = paddle.to_tensor(bias_np) if dtype == "bfloat16": - x = x.cast(paddle.fluid.core.VarDesc.VarType.BF16) + x = x.cast(paddle.base.core.VarDesc.VarType.BF16) x.stop_gradient = False weight.stop_gradient = False @@ -919,7 +919,7 @@ def check_layer_norm( x = paddle.to_tensor(x_np) if dtype == "bfloat16": - x = x.cast(paddle.fluid.core.VarDesc.VarType.BF16) + x = x.cast(paddle.base.core.VarDesc.VarType.BF16) x.stop_gradient = True bias = paddle.to_tensor(bias_np) if has_scale else None diff --git a/test/legacy_test/test_layer_norm_op_v2.py b/test/legacy_test/test_layer_norm_op_v2.py index 9087fe267808a..6e8cdfe728cfe 100644 --- a/test/legacy_test/test_layer_norm_op_v2.py +++ b/test/legacy_test/test_layer_norm_op_v2.py @@ -17,26 +17,26 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestDygraphLayerNormv2(unittest.TestCase): def test_dygraph(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v1(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[1:]) y = ln(paddle.to_tensor(x)) return y.numpy() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[1:]) y = ln(paddle.to_tensor(x)) return y.numpy() @@ -47,14 +47,14 @@ def compute_v2(x): np.testing.assert_allclose(y1, y2, rtol=1e-05) def test_eager(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v1(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[1:]) x1 = paddle.to_tensor(x) x1.stop_gradient = False @@ -63,7 +63,7 @@ def compute_v1(x): return y.numpy(), x1.gradient() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[1:]) x1 = paddle.to_tensor(x) x1.stop_gradient = False @@ -79,11 +79,11 @@ def compute_v2(x): def test_static(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: - exe = fluid.Executor(p) + exe = base.Executor(p) shape = [4, 10, 16, 16] def compute_v1(x_np): @@ -93,7 +93,7 @@ def compute_v1(x_np): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = ln(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -104,7 +104,7 @@ def compute_v2(x_np): name='x', shape=x_np.shape, dtype=x_np.dtype ) y = ln(x) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] return r @@ -116,38 +116,38 @@ def compute_v2(x_np): class TestLayerNormFunction(unittest.TestCase): def test_dygraph(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda() and core.op_support_gpu("layer_norm"): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: shape = [4, 10, 4, 4] def compute_v0(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[1:]) y = ln(paddle.to_tensor(x)) return y.numpy() def compute_v1(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.to_tensor(x) y = paddle.nn.functional.layer_norm(x, shape[1:]) return y.numpy() def compute_v2(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.to_tensor(x) y = paddle.nn.functional.layer_norm(x, tuple(shape[1:])) return y.numpy() def compute_v3(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): ln = paddle.nn.LayerNorm(shape[-1]) y = ln(paddle.to_tensor(x)) return y.numpy() def compute_v4(x): - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.to_tensor(x) y = paddle.nn.functional.layer_norm(x, shape[-1]) return y.numpy() diff --git a/test/legacy_test/test_layers.py b/test/legacy_test/test_layers.py index 25d5ad42bea48..33f9af2b1d163 100644 --- a/test/legacy_test/test_layers.py +++ b/test/legacy_test/test_layers.py @@ -23,10 +23,10 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph import base, to_variable -from paddle.fluid.framework import Program, default_main_program, program_guard +from paddle import base +from paddle.base import core, dygraph +from paddle.base.dygraph import to_variable +from paddle.base.framework import Program, default_main_program, program_guard from paddle.incubate.layers.nn import ( batch_fc, partial_concat, @@ -65,10 +65,10 @@ def static_graph(self): def get_static_graph_result( self, feed, fetch_list, with_lod=False, force_to_use_cpu=False ): - exe = fluid.Executor(self._get_place(force_to_use_cpu)) - exe.run(fluid.default_startup_program()) + exe = base.Executor(self._get_place(force_to_use_cpu)) + exe.run(base.default_startup_program()) return exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=feed, fetch_list=fetch_list, return_numpy=(not with_lod), @@ -76,7 +76,7 @@ def get_static_graph_result( @contextlib.contextmanager def dynamic_graph(self, force_to_use_cpu=False): - with fluid.dygraph.guard( + with base.dygraph.guard( self._get_place(force_to_use_cpu=force_to_use_cpu) ): paddle.seed(self.seed) @@ -104,7 +104,7 @@ def forward(self, x, do_linear2=False): with self.dynamic_graph(): inp = np.ones([3, 3], dtype='float32') - x = base.to_variable(inp) + x = to_variable(inp) custom = CustomLayer(input_size=3, linear1_size=2) ret = custom(x, do_linear2=False) np.testing.assert_array_equal(ret.numpy().shape, [3, 2]) @@ -126,7 +126,7 @@ def test_dropout(self): feed={'data': inp}, fetch_list=[ret, ret2] ) with self.dynamic_graph(): - t = base.to_variable(inp) + t = to_variable(inp) dropout = paddle.nn.Dropout(p=0.35) dy_ret = dropout(t) dy_ret2 = paddle.nn.functional.dropout(t, p=0.35) @@ -153,7 +153,7 @@ def test_linear(self): feed={'data': inp}, fetch_list=[ret] )[0] with self.dynamic_graph(): - t = base.to_variable(inp) + t = to_variable(inp) linear = paddle.nn.Linear( 32, 4, @@ -242,7 +242,7 @@ def test_Flatten(self): feed={'data': inp}, fetch_list=[ret] )[0] with self.dynamic_graph(): - t = base.to_variable(inp) + t = to_variable(inp) flatten = paddle.nn.Flatten() dy_ret = flatten(t) dy_ret_value = dy_ret.numpy() @@ -291,7 +291,7 @@ def test_SyncBatchNorm(self): with self.dynamic_graph(): t = np.ones([3, 3, 5, 5], dtype='float32') my_syncbn = paddle.nn.SyncBatchNorm(3) - dy_ret = my_syncbn(base.to_variable(t)) + dy_ret = my_syncbn(to_variable(t)) dy_ret_value = dy_ret.numpy() np.testing.assert_array_equal(static_ret, dy_ret_value) @@ -305,7 +305,7 @@ def test_relu(self): with self.dynamic_graph(): t = np.ones([3, 3], dtype='float32') - dy_ret = F.relu(base.to_variable(t)) + dy_ret = F.relu(to_variable(t)) dy_ret_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_ret_value, rtol=1e-05) @@ -328,7 +328,7 @@ def test_matmul(self): with self.dynamic_graph(): t = np.ones([3, 3], dtype='float32') t2 = np.ones([3, 3], dtype='float32') - dy_ret = paddle.matmul(base.to_variable(t), base.to_variable(t2)) + dy_ret = paddle.matmul(to_variable(t), to_variable(t2)) dy_ret_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_ret_value, rtol=1e-05) @@ -431,7 +431,7 @@ def test_conv2d_transpose(self): 27, bias_attr=paddle.nn.initializer.Constant(value=1), ) - dy_rlt = conv2d_transpose(base.to_variable(inp_np)) + dy_rlt = conv2d_transpose(to_variable(inp_np)) dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) dy_rlt_value = dy_rlt.numpy() np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) @@ -440,7 +440,7 @@ def test_conv2d_transpose(self): with self.dynamic_graph(): images = np.ones([2, 3, 5, 5], dtype='float32') custom_weight = np.random.randn(3, 3, 2, 2).astype("float32") - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( initializer=paddle.nn.initializer.Assign(custom_weight) ) conv2d1 = paddle.nn.Conv2DTranspose(3, 3, [2, 2]) @@ -450,8 +450,8 @@ def test_conv2d_transpose(self): [2, 2], weight_attr=weight_attr, ) - dy_ret1 = conv2d1(base.to_variable(images)) - dy_ret2 = conv2d2(base.to_variable(images)) + dy_ret1 = conv2d1(to_variable(images)) + dy_ret2 = conv2d2(to_variable(images)) self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) conv2d1_weight_np = conv2d1.weight.numpy() @@ -464,8 +464,8 @@ def test_conv2d_transpose(self): conv2d1_weight_np, conv2d2.weight.numpy() ) conv2d2.bias.set_value(conv2d1_bias) - dy_ret1 = conv2d1(base.to_variable(images)) - dy_ret2 = conv2d2(base.to_variable(images)) + dy_ret1 = conv2d1(to_variable(images)) + dy_ret2 = conv2d2(to_variable(images)) np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) conv2d2.weight = conv2d1.weight @@ -537,14 +537,18 @@ def test_bilinear_tensor_product(self): 6, bias_attr=paddle.nn.initializer.Constant(value=1), ) - dy_rlt = btp(base.to_variable(inp_np_x), base.to_variable(inp_np_y)) + dy_rlt = btp( + to_variable(inp_np_x), + to_variable(inp_np_y), + ) dy_rlt = paddle.nn.functional.sigmoid(dy_rlt) dy_rlt_value = dy_rlt.numpy() with self.dynamic_graph(): btp2 = paddle.nn.Bilinear(3, 3, 6) dy_rlt2 = btp2( - base.to_variable(inp_np_x), base.to_variable(inp_np_y) + to_variable(inp_np_x), + to_variable(inp_np_y), ) dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) dy_rlt2_value = dy_rlt2.numpy() @@ -570,27 +574,31 @@ def test_bilinear_tensor_product(self): with self.dynamic_graph(): custom_weight = np.random.randn(6, 3, 3).astype("float32") - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( initializer=paddle.nn.initializer.Assign(custom_weight) ) btp1 = paddle.nn.Bilinear(3, 3, 6) btp2 = paddle.nn.Bilinear(3, 3, 6, weight_attr=weight_attr) dy_rlt1 = btp1( - base.to_variable(inp_np_x), base.to_variable(inp_np_y) + to_variable(inp_np_x), + to_variable(inp_np_y), ) dy_rlt1 = paddle.nn.functional.sigmoid(dy_rlt1) dy_rlt2 = btp2( - base.to_variable(inp_np_x), base.to_variable(inp_np_y) + to_variable(inp_np_x), + to_variable(inp_np_y), ) dy_rlt2 = paddle.nn.functional.sigmoid(dy_rlt2) self.assertFalse(np.array_equal(dy_rlt1.numpy(), dy_rlt2.numpy())) btp2.weight.set_value(btp1.weight.numpy()) btp2.bias.set_value(btp1.bias) dy_rlt1 = btp1( - base.to_variable(inp_np_x), base.to_variable(inp_np_y) + to_variable(inp_np_x), + to_variable(inp_np_y), ) dy_rlt2 = btp2( - base.to_variable(inp_np_x), base.to_variable(inp_np_y) + to_variable(inp_np_x), + to_variable(inp_np_y), ) np.testing.assert_array_equal(dy_rlt1.numpy(), dy_rlt2.numpy()) @@ -634,7 +642,7 @@ def test_embeding(self): emb2 = paddle.nn.Embedding( dict_size, 32, weight_attr='emb.w', sparse=False ) - dy_rlt = emb2(base.to_variable(inp_word)) + dy_rlt = emb2(to_variable(inp_word)) dy_rlt_value = dy_rlt.numpy() np.testing.assert_allclose(static_rlt2[0], static_rlt) @@ -642,20 +650,20 @@ def test_embeding(self): with self.dynamic_graph(): custom_weight = np.random.randn(dict_size, 32).astype("float32") - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( initializer=paddle.nn.initializer.Assign(custom_weight) ) emb1 = paddle.nn.Embedding(dict_size, 32, sparse=False) emb2 = paddle.nn.Embedding( dict_size, 32, weight_attr=weight_attr, sparse=False ) - rep1 = emb1(base.to_variable(inp_word)) - rep2 = emb2(base.to_variable(inp_word)) + rep1 = emb1(to_variable(inp_word)) + rep2 = emb2(to_variable(inp_word)) self.assertFalse(np.array_equal(emb1.weight.numpy(), custom_weight)) np.testing.assert_array_equal(emb2.weight.numpy(), custom_weight) self.assertFalse(np.array_equal(rep1.numpy(), rep2.numpy())) emb2.weight.set_value(emb1.weight.numpy()) - rep2 = emb2(base.to_variable(inp_word)) + rep2 = emb2(to_variable(inp_word)) np.testing.assert_array_equal(rep1.numpy(), rep2.numpy()) emb2.weight = emb1.weight @@ -665,10 +673,10 @@ def test_embeding(self): def test_one_hot(self): with self.dynamic_graph(): - label = fluid.dygraph.to_variable(np.array([[1], [1], [3], [0]])) + label = base.dygraph.to_variable(np.array([[1], [1], [3], [0]])) one_hot_label1 = paddle.nn.functional.one_hot(label, 4) one_hot_label2 = paddle.nn.functional.one_hot( - label, fluid.dygraph.to_variable(np.array([4])) + label, base.dygraph.to_variable(np.array([4])) ) np.testing.assert_array_equal( one_hot_label1.numpy(), one_hot_label2.numpy() @@ -676,22 +684,22 @@ def test_one_hot(self): def test_split(self): with self.dynamic_graph(): - input = fluid.dygraph.to_variable(np.random.random((3, 8, 5))) + input = base.dygraph.to_variable(np.random.random((3, 8, 5))) x0, x1 = paddle.split(input, num_or_sections=2, axis=1) x00, x11 = paddle.split( input, num_or_sections=2, - axis=fluid.dygraph.to_variable(np.array([1])), + axis=base.dygraph.to_variable(np.array([1])), ) np.testing.assert_array_equal(x0.numpy(), x00.numpy()) np.testing.assert_array_equal(x1.numpy(), x11.numpy()) def test_topk(self): with self.dynamic_graph(): - input = fluid.dygraph.to_variable(np.random.random((13, 11))) + input = base.dygraph.to_variable(np.random.random((13, 11))) top5_values1, top5_indices1 = paddle.topk(input, k=5) top5_values2, top5_indices2 = paddle.topk( - input, k=fluid.dygraph.to_variable(np.array([5])) + input, k=base.dygraph.to_variable(np.array([5])) ) np.testing.assert_array_equal( top5_values1.numpy(), top5_values2.numpy() @@ -731,7 +739,7 @@ def test_conv3d(self): conv3d = paddle.nn.Conv3D( in_channels=3, out_channels=3, kernel_size=2 ) - dy_ret = conv3d(base.to_variable(images)) + dy_ret = conv3d(to_variable(images)) dy_rlt_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) @@ -740,7 +748,7 @@ def test_conv3d(self): with self.dynamic_graph(): images = np.ones([2, 3, 6, 6, 6], dtype='float32') custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( initializer=paddle.nn.initializer.Assign(custom_weight) ) conv3d1 = paddle.nn.Conv3D( @@ -752,8 +760,8 @@ def test_conv3d(self): kernel_size=2, weight_attr=weight_attr, ) - dy_ret1 = conv3d1(base.to_variable(images)) - dy_ret2 = conv3d2(base.to_variable(images)) + dy_ret1 = conv3d1(to_variable(images)) + dy_ret2 = conv3d2(to_variable(images)) self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) conv3d1_weight_np = conv3d1.weight.numpy() @@ -766,8 +774,8 @@ def test_conv3d(self): conv3d1_weight_np, conv3d2.weight.numpy() ) conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(base.to_variable(images)) - dy_ret2 = conv3d2(base.to_variable(images)) + dy_ret1 = conv3d1(to_variable(images)) + dy_ret2 = conv3d2(to_variable(images)) np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) conv3d2.weight = conv3d1.weight @@ -801,7 +809,7 @@ def test_group_norm(self): ) static_ret = self.get_static_graph_result( feed={ - 'X': fluid.create_lod_tensor( + 'X': base.create_lod_tensor( data=input, recursive_seq_lens=[[1, 1]], place=place ) }, @@ -822,7 +830,7 @@ def test_group_norm(self): ret = groupNorm(X) static_ret2 = self.get_static_graph_result( feed={ - 'X': fluid.create_lod_tensor( + 'X': base.create_lod_tensor( data=input, recursive_seq_lens=[[1, 1]], place=place ) }, @@ -837,7 +845,7 @@ def test_group_norm(self): weight_attr=paddle.nn.initializer.Uniform(low=-0.5, high=0.5), bias_attr=paddle.nn.initializer.Constant(value=1), ) - dy_ret = groupNorm(base.to_variable(input)) + dy_ret = groupNorm(to_variable(input)) dy_rlt_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) @@ -870,12 +878,12 @@ def test_instance_norm(self): with self.dynamic_graph(): instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(base.to_variable(input)) + dy_ret = instanceNorm(to_variable(input)) dy_rlt_value = dy_ret.numpy() with self.dynamic_graph(): instanceNorm = paddle.nn.InstanceNorm2D(num_features=shape[1]) - dy_ret = instanceNorm(base.to_variable(input)) + dy_ret = instanceNorm(to_variable(input)) dy_rlt_value2 = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) @@ -917,7 +925,7 @@ def test_spectral_norm(self): ) static_ret = self.get_static_graph_result( feed={ - 'Weight': fluid.create_lod_tensor( + 'Weight': base.create_lod_tensor( data=input, recursive_seq_lens=[[1, 1]], place=place ), }, @@ -933,7 +941,7 @@ def test_spectral_norm(self): ret = spectralNorm(Weight) static_ret2 = self.get_static_graph_result( feed={ - 'Weight': fluid.create_lod_tensor( + 'Weight': base.create_lod_tensor( data=input, recursive_seq_lens=[[1, 1]], place=place ) }, @@ -943,7 +951,7 @@ def test_spectral_norm(self): with self.dynamic_graph(): spectralNorm = paddle.nn.SpectralNorm(shape, dim=1, power_iters=2) - dy_ret = spectralNorm(base.to_variable(input)) + dy_ret = spectralNorm(to_variable(input)) dy_rlt_value = dy_ret.numpy() np.testing.assert_allclose(static_ret, dy_rlt_value, rtol=1e-05) @@ -979,7 +987,7 @@ def test_conv3d_transpose(self): conv3d_transpose = paddle.nn.Conv3DTranspose( in_channels=3, out_channels=12, kernel_size=12 ) - dy_rlt = conv3d_transpose(base.to_variable(input_array)) + dy_rlt = conv3d_transpose(to_variable(input_array)) dy_rlt_value = dy_rlt.numpy() np.testing.assert_allclose(static_rlt2, static_rlt, rtol=1e-05) np.testing.assert_allclose(dy_rlt_value, static_rlt, rtol=1e-05) @@ -987,7 +995,7 @@ def test_conv3d_transpose(self): with self.dynamic_graph(): images = np.ones([2, 3, 6, 6, 6], dtype='float32') custom_weight = np.random.randn(3, 3, 2, 2, 2).astype("float32") - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( initializer=paddle.nn.initializer.Assign(custom_weight) ) conv3d1 = paddle.nn.Conv3DTranspose( @@ -1003,8 +1011,8 @@ def test_conv3d_transpose(self): weight_attr=weight_attr, bias_attr='conv3d2_b', ) - dy_ret1 = conv3d1(base.to_variable(images)) - dy_ret2 = conv3d2(base.to_variable(images)) + dy_ret1 = conv3d1(to_variable(images)) + dy_ret2 = conv3d2(to_variable(images)) self.assertFalse(np.array_equal(dy_ret1.numpy(), dy_ret2.numpy())) conv3d1_weight_np = conv3d1.weight.numpy() @@ -1017,8 +1025,8 @@ def test_conv3d_transpose(self): conv3d1_weight_np, conv3d2.weight.numpy() ) conv3d1.bias.set_value(conv3d1_bias) - dy_ret1 = conv3d1(base.to_variable(images)) - dy_ret2 = conv3d2(base.to_variable(images)) + dy_ret1 = conv3d1(to_variable(images)) + dy_ret2 = conv3d2(to_variable(images)) np.testing.assert_array_equal(dy_ret1.numpy(), dy_ret2.numpy()) conv3d2.weight = conv3d1.weight @@ -1083,8 +1091,8 @@ def test_compare(self): feed={"a": value_a, "b": value_b}, fetch_list=[cond] )[0] with self.dynamic_graph(): - da = base.to_variable(value_a) - db = base.to_variable(value_b) + da = to_variable(value_a) + db = to_variable(value_b) dcond = paddle.less_than(x=da, y=db) for i in range(len(static_ret)): @@ -1099,8 +1107,8 @@ def test_compare(self): feed={"a1": value_a, "b1": value_b}, fetch_list=[cond1] )[0] with self.dynamic_graph(): - da1 = base.to_variable(value_a) - db1 = base.to_variable(value_b) + da1 = to_variable(value_a) + db1 = to_variable(value_b) dcond1 = paddle.less_equal(x=da1, y=db1) for i in range(len(static_ret1)): @@ -1115,8 +1123,8 @@ def test_compare(self): feed={"a2": value_a, "b2": value_b}, fetch_list=[cond2] )[0] with self.dynamic_graph(): - da2 = base.to_variable(value_a) - db2 = base.to_variable(value_b) + da2 = to_variable(value_a) + db2 = to_variable(value_b) dcond2 = paddle.greater_than(x=da2, y=db2) for i in range(len(static_ret2)): @@ -1131,8 +1139,8 @@ def test_compare(self): feed={"a3": value_a, "b3": value_b}, fetch_list=[cond3] )[0] with self.dynamic_graph(): - da3 = base.to_variable(value_a) - db3 = base.to_variable(value_b) + da3 = to_variable(value_a) + db3 = to_variable(value_b) dcond3 = paddle.greater_equal(x=da3, y=db3) for i in range(len(static_ret3)): @@ -1147,8 +1155,8 @@ def test_compare(self): feed={"a4": value_a, "b4": value_b}, fetch_list=[cond4] )[0] with self.dynamic_graph(): - da4 = base.to_variable(value_a) - db4 = base.to_variable(value_b) + da4 = to_variable(value_a) + db4 = to_variable(value_b) dcond4 = paddle.equal(x=da4, y=db4) for i in range(len(static_ret4)): @@ -1163,8 +1171,8 @@ def test_compare(self): feed={"a5": value_a, "b5": value_b}, fetch_list=[cond5] )[0] with self.dynamic_graph(): - da5 = base.to_variable(value_a) - db5 = base.to_variable(value_b) + da5 = to_variable(value_a) + db5 = to_variable(value_b) dcond5 = paddle.equal(x=da5, y=db5) for i in range(len(static_ret5)): @@ -1190,17 +1198,17 @@ def greater_equal_branch(a, b): lambda: less_than_branch(a, b), ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(fetch_list=[out]) static_res = ret[0] with self.dynamic_graph(): - a = fluid.dygraph.to_variable(np.array([0.1]).astype('float32')) - b = fluid.dygraph.to_variable(np.array([0.23]).astype('float32')) + a = base.dygraph.to_variable(np.array([0.1]).astype('float32')) + b = base.dygraph.to_variable(np.array([0.23]).astype('float32')) out = paddle.static.nn.cond( a < b, lambda: less_than_branch(a, b), @@ -1260,11 +1268,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) static_res1, static_res2 = exe.run(fetch_list=[out_1, out_2]) with self.dynamic_graph(): @@ -1334,11 +1342,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) static_res1, static_res2, static_res3 = exe.run( fetch_list=[out_1, out_2, out_3] ) @@ -1424,10 +1432,10 @@ def test_accuracy(self): fc_out = paddle.nn.Linear(32 * 32, 10)(data_new) predict = paddle.nn.functional.softmax(fc_out) result = paddle.static.accuracy(input=predict, label=label, k=5) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) # x = np.random.rand(3, 32, 32).astype("float32") # y = np.array([[1], [0], [1]]) @@ -1436,8 +1444,8 @@ def test_accuracy(self): ) with self.dynamic_graph(force_to_use_cpu=True): - data = base.to_variable(x) - label = base.to_variable(y) + data = to_variable(x) + label = to_variable(y) data_new = paddle.reshape(data, [3, 32 * 32]) fc_out = paddle.nn.Linear(32 * 32, 10)(data_new) predict = paddle.nn.functional.softmax(fc_out) @@ -1536,8 +1544,8 @@ def _get_np_data(self, shape, dtype, append_batch_size=True): def _get_data( self, name, shape, dtype, set_feed_dict=True, append_batch_size=True ): - if base.enabled(): - return base.to_variable( + if dygraph.base.enabled(): + return to_variable( value=self._get_np_data(shape, dtype, append_batch_size), name=name, zero_copy=False, @@ -1559,8 +1567,8 @@ def _get_data( def make_fit_a_line(self): with program_guard( - fluid.default_main_program(), - startup_program=fluid.default_startup_program(), + base.default_main_program(), + startup_program=base.default_startup_program(), ): x = self._get_data(name='x', shape=[13], dtype='float32') y_predict = paddle.nn.Linear(13, 1)(x) @@ -1573,7 +1581,7 @@ def make_fit_a_line(self): def make_recognize_digits_mlp(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): # Change g_program, so the rest layers use `g_program` images = self._get_data(name='pixel', shape=[784], dtype='float32') @@ -1594,7 +1602,7 @@ def make_recognize_digits_mlp(self): def make_conv2d_transpose(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): img = self._get_data(name='pixel', shape=[3, 2, 2], dtype='float32') return paddle.static.nn.conv2d_transpose( @@ -1603,7 +1611,7 @@ def make_conv2d_transpose(self): def make_recognize_digits_conv(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): images = self._get_data( name='pixel', shape=[1, 28, 28], dtype='float32' @@ -1650,7 +1658,7 @@ def make_recognize_digits_conv(self): def make_word_embedding(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): dict_size = 10000 embed_size = 32 @@ -1710,7 +1718,7 @@ def make_word_embedding(self): def make_pool2d(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name='x', shape=[3, 224, 224], dtype='float32') return paddle.nn.functional.max_pool2d( @@ -1719,7 +1727,7 @@ def make_pool2d(self): def make_pool2d_infershape(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): theta = self._get_data("theta", shape=[2, 3], dtype='float32') x = paddle.nn.functional.affine_grid( @@ -1731,7 +1739,7 @@ def make_pool2d_infershape(self): def make_softmax(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): data = self._get_data(name='data', shape=[10], dtype='float32') hid = paddle.nn.Linear(10, 20)(data) @@ -1776,7 +1784,7 @@ def make_nce(self): def make_multiplex(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x1 = self._get_data(name='x1', shape=[4], dtype='float32') x2 = self._get_data(name='x2', shape=[4], dtype='float32') @@ -1786,7 +1794,7 @@ def make_multiplex(self): def make_softmax_with_cross_entropy(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name='x', shape=[16], dtype='float32') y = self._get_data(name='label', shape=[1], dtype='int64') @@ -1823,7 +1831,7 @@ def make_softmax_with_cross_entropy(self): def make_scatter(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data( name='x', shape=[3, 3], append_batch_size=False, dtype='float32' @@ -1841,7 +1849,7 @@ def make_scatter(self): return out def make_one_hot(self): - with fluid.framework._dygraph_place_guard(place=fluid.CPUPlace()): + with base.framework._dygraph_place_guard(place=base.CPUPlace()): label = self._get_data(name="label", shape=[1], dtype="int32") one_hot_label = paddle.nn.functional.one_hot(label, 10) return one_hot_label @@ -1849,7 +1857,7 @@ def make_one_hot(self): def make_label_smooth(self): # TODO(minqiyang): support gpu ut self._force_to_use_cpu = True - with fluid.framework._dygraph_place_guard(place=fluid.CPUPlace()): + with base.framework._dygraph_place_guard(place=base.CPUPlace()): label = self._get_data(name="label", shape=[1], dtype="int32") one_hot_label = paddle.nn.functional.one_hot(label, 10) smooth_label = F.label_smooth(label=one_hot_label, epsilon=0.1) @@ -1857,7 +1865,7 @@ def make_label_smooth(self): def make_topk(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): data = self._get_data(name="label", shape=[200], dtype="float32") values, indices = paddle.topk(data, k=5) @@ -1866,7 +1874,7 @@ def make_topk(self): def make_l2_normalize(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name='x', shape=[8, 7, 10], dtype="float32") output = paddle.nn.functional.normalize(x, axis=1) @@ -1874,7 +1882,7 @@ def make_l2_normalize(self): def make_shape(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[3, 100, 100], dtype="float32" @@ -1884,7 +1892,7 @@ def make_shape(self): def make_pad2d(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[3, 100, 100], dtype="float32" @@ -1901,7 +1909,7 @@ def make_pad2d(self): def make_mish(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data(name="input", shape=[16], dtype="float32") out = paddle.nn.functional.mish(input, name='mish') @@ -1909,7 +1917,7 @@ def make_mish(self): def make_cross_entropy(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name="x", shape=[30, 10], dtype="float32") label = self._get_data(name="label", shape=[30, 1], dtype="int64") @@ -1926,7 +1934,7 @@ def make_cross_entropy(self): def make_uniform_random_batch_size_like(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[13, 11], dtype='float32' @@ -1936,14 +1944,14 @@ def make_uniform_random_batch_size_like(self): def make_gaussian_random(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): out = random.gaussian(shape=[20, 30]) return out def make_sum(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[13, 11], dtype='float32' @@ -1958,7 +1966,7 @@ def make_slice(self): axes = [0, 1, 2] with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[3, 4, 5, 6], dtype='float32' @@ -1969,7 +1977,7 @@ def make_slice(self): def make_scale_variable(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = self._get_data( name="input", shape=[3, 4, 5, 6], dtype='float32' @@ -1985,7 +1993,7 @@ def make_scale_variable(self): def make_bilinear_tensor_product_layer(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): data = self._get_data(name='data', shape=[4], dtype="float32") @@ -1997,7 +2005,7 @@ def make_bilinear_tensor_product_layer(self): def make_batch_norm(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): data = self._get_data( name='data', shape=[32, 128, 128], dtype="float32" @@ -2007,7 +2015,7 @@ def make_batch_norm(self): def make_batch_norm_momentum_variable(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): data = self._get_data( name='data', shape=[32, 128, 128], dtype="float32" @@ -2023,7 +2031,7 @@ def make_batch_norm_momentum_variable(self): def make_range(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): paddle.arange(0, 10, 2, 'int32') paddle.arange(0.1, 10.0, 0.2, 'float32') @@ -2042,7 +2050,7 @@ def make_range(self): def make_spectral_norm(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): weight = self._get_data( name='weight', @@ -2055,7 +2063,7 @@ def make_spectral_norm(self): def make_kldiv_loss(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data( name='x', @@ -2076,7 +2084,7 @@ def make_kldiv_loss(self): def make_pixel_shuffle(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name="X", shape=[9, 4, 4], dtype="float32") out = paddle.nn.functional.pixel_shuffle(x, upscale_factor=3) @@ -2084,7 +2092,7 @@ def make_pixel_shuffle(self): def make_mse_loss(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name="X", shape=[1], dtype="float32") y = self._get_data(name="Y", shape=[1], dtype="float32") @@ -2093,7 +2101,7 @@ def make_mse_loss(self): def make_square_error_cost(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): x = self._get_data(name="X", shape=[1], dtype="float32") y = self._get_data(name="Y", shape=[1], dtype="float32") @@ -2161,13 +2169,13 @@ def test_batch_fc(self): out = batch_fc( input=input, param_size=[16, 3, 10], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( learning_rate=1.0, name="w_0", initializer=paddle.nn.initializer.XavierNormal(), ), bias_size=[16, 10], - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( learning_rate=1.0, name="b_0", initializer=paddle.nn.initializer.XavierNormal(), @@ -2188,7 +2196,7 @@ def test_rank_attention(self): input=input, rank_offset=rank_offset, rank_param_shape=[18, 3], - rank_param_attr=fluid.ParamAttr( + rank_param_attr=base.ParamAttr( learning_rate=1.0, name="ubm_rank_param.w_0", initializer=paddle.nn.initializer.XavierNormal(), @@ -2261,7 +2269,7 @@ def test_partial_concat(self): def test_addmm(self): with program_guard( - fluid.default_main_program(), fluid.default_startup_program() + base.default_main_program(), base.default_startup_program() ): input = paddle.static.data( name='input_data', @@ -2313,14 +2321,14 @@ def forward(self): class TestLayerParameterTrainableSet(unittest.TestCase): def test_layer_parameter_set(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = ExampleNet() self.assertFalse(net.weight.trainable) class TestLayerTrainingAttribute(unittest.TestCase): def test_set_train_eval_in_dynamic_mode(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): net = paddle.nn.Dropout() net.train() self.assertTrue(net.training) @@ -2359,7 +2367,7 @@ def forward(self, input): class TestSubLayerCount(unittest.TestCase): def test_sublayer(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): mySuperlayer = MySuperLayer() self.assertTrue(len(mySuperlayer.sublayers()) == 3) self.assertTrue(len(mySuperlayer.sublayers(include_self=True)) == 4) diff --git a/test/legacy_test/test_layout_autotune.py b/test/legacy_test/test_layout_autotune.py index b4297e98817a8..68e85bc866e84 100644 --- a/test/legacy_test/test_layout_autotune.py +++ b/test/legacy_test/test_layout_autotune.py @@ -44,11 +44,11 @@ def forward(self, image): class LayoutAutoTune(unittest.TestCase): def test_config(self): - paddle.fluid.core.enable_layout_autotune() + paddle.base.core.enable_layout_autotune() if self.use_autoune(): - self.assertEqual(paddle.fluid.core.use_layout_autotune(), True) - paddle.fluid.core.disable_layout_autotune() - self.assertEqual(paddle.fluid.core.use_layout_autotune(), False) + self.assertEqual(paddle.base.core.use_layout_autotune(), True) + paddle.base.core.disable_layout_autotune() + self.assertEqual(paddle.base.core.use_layout_autotune(), False) self.use_autoune() def setUp(self): @@ -59,7 +59,7 @@ def use_autoune(self): paddle.incubate.autotune.set_config( config={"layout": {"enable": True}} ) - return paddle.fluid.core.use_layout_autotune() + return paddle.base.core.use_layout_autotune() else: config = {"layout": {"enable": False}} tfile = tempfile.NamedTemporaryFile(mode="w+", delete=False) @@ -67,7 +67,7 @@ def use_autoune(self): tfile.close() paddle.incubate.autotune.set_config(tfile.name) os.remove(tfile.name) - return paddle.fluid.core.use_layout_autotune() + return paddle.base.core.use_layout_autotune() def train(self, data_format): model = SimpleNet(data_format="NCHW", class_num=2) diff --git a/test/legacy_test/test_lazy_init.py b/test/legacy_test/test_lazy_init.py index 8b2bef91c00e8..37bf8b6fe19ae 100644 --- a/test/legacy_test/test_lazy_init.py +++ b/test/legacy_test/test_lazy_init.py @@ -18,7 +18,7 @@ import paddle from paddle import LazyGuard -from paddle.fluid import unique_name +from paddle.base import unique_name from paddle.nn import Layer, Linear from paddle.nn.initializer import ( Constant, diff --git a/test/legacy_test/test_lcm.py b/test/legacy_test/test_lcm.py index 478853d8bab8f..706c2dc23c32e 100644 --- a/test/legacy_test/test_lcm.py +++ b/test/legacy_test/test_lcm.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -31,9 +31,9 @@ def setUp(self): self.y_shape = [] def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(startup_program, train_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(startup_program, train_program): x1 = paddle.static.data( name='input1', dtype='int32', shape=self.x_shape ) @@ -43,13 +43,13 @@ def test_static_graph(self): out = paddle.lcm(x1, x2) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input1': self.x_np, 'input2': self.y_np}, fetch_list=[out], ) diff --git a/test/legacy_test/test_ldexp.py b/test/legacy_test/test_ldexp.py index b2a9976dd9c18..76e1b454a2e2c 100644 --- a/test/legacy_test/test_ldexp.py +++ b/test/legacy_test/test_ldexp.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard DYNAMIC = 1 diff --git a/test/legacy_test/test_learning_rate_scheduler.py b/test/legacy_test/test_learning_rate_scheduler.py index 8898fb59b87b1..fd92acaf719b1 100644 --- a/test/legacy_test/test_learning_rate_scheduler.py +++ b/test/legacy_test/test_learning_rate_scheduler.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core, framework +from paddle import base +from paddle.base import core, framework def exponential_decay( @@ -118,10 +118,10 @@ def lambda_decay(global_step, learning_rate, lr_lambda): class TestLearningRateDecayDygraph(unittest.TestCase): def test_LR_state_dict(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = np.random.uniform(-1, 1, [3, 10]).astype("float32") linear = paddle.nn.Linear(10, 10) - input = fluid.dygraph.to_variable(x) + input = base.dygraph.to_variable(x) Exponential_scheduler = paddle.optimizer.lr.ExponentialDecay( learning_rate=0.1, @@ -235,7 +235,7 @@ def test_LR_state_dict(self): ) def test_NoamDecay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): d_model = 0.01 warmup_steps = 200 learning_rate = 2.0 @@ -248,18 +248,18 @@ def test_NoamDecay(self): step, d_model, warmup_steps, learning_rate ) lr.step() - fluid_result = lr() + base_result = lr() self.assertAlmostEqual( right_result, - fluid_result, + base_result, msg='Failed lr scheduler in step {}, Python result is {}, Fluid result is {}'.format( - step, right_result, fluid_result + step, right_result, base_result ), ) def test_LinearLrWarmup(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): lr = paddle.optimizer.lr.PolynomialDecay( learning_rate=1.0, decay_steps=10, @@ -288,7 +288,7 @@ def test_LinearLrWarmup(self): ) def test_MultiStepDecay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): learning_rate = 0.5 milestones = [2, 4, 8] decay_rate = 0.2 @@ -305,14 +305,14 @@ def test_MultiStepDecay(self): right_result = multi_step_decay( epoch, learning_rate, milestones, decay_rate ) - fluid_result = adam.get_lr() + base_result = adam.get_lr() adam.step() scheduler.step() self.assertAlmostEqual( right_result, - fluid_result, + base_result, msg='Failed lr scheduler in epoch {}, Python result is {}, Fluid result is {}'.format( - epoch, right_result, fluid_result + epoch, right_result, base_result ), ) @@ -333,7 +333,7 @@ def test_MultiStepDecay(self): lr = paddle.optimizer.lr.MultiStepDecay(-1, [20, 30, 50]) def test_StepDecay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): learning_rate = 0.5 step_size = 3 decay_rate = 0.2 @@ -344,14 +344,14 @@ def test_StepDecay(self): right_result = step_decay( epoch, learning_rate, step_size, decay_rate ) - fluid_result = scheduler() + base_result = scheduler() scheduler.get_lr() scheduler.step() self.assertAlmostEqual( right_result, - fluid_result, + base_result, msg='Failed lr scheduler in epoch {}, Python result is {}, Fluid result is {}'.format( - epoch, right_result, fluid_result + epoch, right_result, base_result ), ) @@ -362,7 +362,7 @@ def test_StepDecay(self): lr = paddle.optimizer.lr.StepDecay(learning_rate, 20, 2) def test_LambdaDecay(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): learning_rate = 0.5 lr_lambda = lambda x: 0.95**x scheduler = paddle.optimizer.lr.LambdaDecay( @@ -376,14 +376,14 @@ def test_LambdaDecay(self): for epoch in range(30): right_result = lambda_decay(epoch, learning_rate, lr_lambda) - fluid_result = scheduler() + base_result = scheduler() scheduler.get_lr() scheduler.step() self.assertAlmostEqual( right_result, - fluid_result, + base_result, msg='Failed lr scheduler in epoch {}, Python result is {}, Fluid result is {}'.format( - epoch, right_result, fluid_result + epoch, right_result, base_result ), ) @@ -392,26 +392,26 @@ def test_LambdaDecay(self): class TestLearningRateDecay(unittest.TestCase): - def check_decay(self, python_decay_fn, fluid_decay_fn, kwargs): - places = [fluid.CPUPlace()] + def check_decay(self, python_decay_fn, base_decay_fn, kwargs): + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: self.check_decay_with_place( - place, python_decay_fn, fluid_decay_fn, kwargs + place, python_decay_fn, base_decay_fn, kwargs ) def check_decay_with_place( - self, place, python_decay_fn, fluid_decay_fn, kwargs + self, place, python_decay_fn, base_decay_fn, kwargs ): - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() - with fluid.program_guard(main_prog, startup_prog): - decayed_lr = fluid_decay_fn(**kwargs) + with base.program_guard(main_prog, startup_prog): + decayed_lr = base_decay_fn(**kwargs) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) @@ -502,7 +502,7 @@ def test_decay(self): ), ] - for py_decay_fn, fluid_decay_fn, kwargs in decay_fns: + for py_decay_fn, base_decay_fn, kwargs in decay_fns: print( "class=" + self.__class__.__name__ @@ -514,32 +514,32 @@ def test_decay(self): main_program = framework.Program() startup_program = framework.Program() with framework.program_guard(main_program, startup_program): - self.check_decay(py_decay_fn, fluid_decay_fn, kwargs) + self.check_decay(py_decay_fn, base_decay_fn, kwargs) class TestLinearWamrupLearningRateDecay(unittest.TestCase): def check_decay_with_place( - self, place, python_decay_fn, fluid_decay_fn, kwargs + self, place, python_decay_fn, base_decay_fn, kwargs ): - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() warmup_steps = 10 start_lr = 0.1 / 3.0 end_lr = 0.1 - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): decayed_lr = paddle.optimizer.lr.linear_lr_warmup( - fluid_decay_fn(**kwargs), warmup_steps, start_lr, end_lr + base_decay_fn(**kwargs), warmup_steps, start_lr, end_lr ) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) for step in range(20): # Step of NoamDecay starts from 1. - if fluid_decay_fn.__name__ == 'noam_decay': + if base_decay_fn.__name__ == 'noam_decay': step += 1 (lr_val,) = exe.run(main_prog, feed={}, fetch_list=[decayed_lr]) if step < warmup_steps: @@ -564,17 +564,17 @@ def check_decay_with_place( class TestLinearWamrupLearningRateDecayWithScalarInput(unittest.TestCase): def run_scalar_lr(self, place, lr, start_lr, end_lr): - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() warmup_steps = 10 - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): decayed_lr = paddle.optimizer.lr.linear_lr_warmup( lr, warmup_steps, start_lr, end_lr ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_prog) for step in range(20): @@ -595,9 +595,9 @@ def run_scalar_lr(self, place, lr, start_lr, end_lr): def test_scalar_lr(self): def run_places(lr, start_lr, end_lr): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.run_scalar_lr(p, lr, start_lr, end_lr) diff --git a/test/legacy_test/test_lerp_op.py b/test/legacy_test/test_lerp_op.py index 7966e9a4b98f5..f85062940e31d 100644 --- a/test/legacy_test/test_lerp_op.py +++ b/test/legacy_test/test_lerp_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(0) diff --git a/test/legacy_test/test_less_equal_op.py b/test/legacy_test/test_less_equal_op.py index 98b25bac4ea47..12473936c7085 100644 --- a/test/legacy_test/test_less_equal_op.py +++ b/test/legacy_test/test_less_equal_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import core +from paddle.base import core class Test_Less_Equal_Op_Fp16(unittest.TestCase): diff --git a/test/legacy_test/test_less_than_op.py b/test/legacy_test/test_less_than_op.py index ee5adcb97470a..dccb4576db60b 100644 --- a/test/legacy_test/test_less_than_op.py +++ b/test/legacy_test/test_less_than_op.py @@ -19,7 +19,7 @@ import paddle from paddle import static -from paddle.fluid import core +from paddle.base import core class Test_Less_Than_Op_Fp16(unittest.TestCase): diff --git a/test/legacy_test/test_lgamma_op.py b/test/legacy_test/test_lgamma_op.py index 52c079f8a62a1..3fbbb45f235d6 100644 --- a/test/legacy_test/test_lgamma_op.py +++ b/test/legacy_test/test_lgamma_op.py @@ -20,7 +20,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_limit_by_capacity_op.py b/test/legacy_test/test_limit_by_capacity_op.py index 00fbf9b8dc68a..066345d584824 100644 --- a/test/legacy_test/test_limit_by_capacity_op.py +++ b/test/legacy_test/test_limit_by_capacity_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.models.moe import utils -from paddle.fluid import core def limit_by_capacity(expert_count, _capacity, n_worker): diff --git a/test/legacy_test/test_linalg_lstsq_op.py b/test/legacy_test/test_linalg_lstsq_op.py index 69792b368cf6f..ca9ec7dfb26d5 100644 --- a/test/legacy_test/test_linalg_lstsq_op.py +++ b/test/legacy_test/test_linalg_lstsq_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class LinalgLstsqTestCase(unittest.TestCase): @@ -95,8 +95,8 @@ def test_static(self): paddle.enable_static() for dev in self.devices: paddle.set_device(dev) - place = fluid.CPUPlace() if dev == "cpu" else fluid.CUDAPlace(0) - with fluid.program_guard(fluid.Program(), fluid.Program()): + place = base.CPUPlace() if dev == "cpu" else base.CUDAPlace(0) + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data( name="x", shape=self._input_shape_1, @@ -110,9 +110,9 @@ def test_static(self): results = paddle.linalg.lstsq( x, y, rcond=self.rcond, driver=self.driver ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self._input_data_1, "y": self._input_data_2}, fetch_list=[results], ) diff --git a/test/legacy_test/test_linalg_pinv_op.py b/test/legacy_test/test_linalg_pinv_op.py index 4420d5b1faff3..fecd97e2f0b24 100644 --- a/test/legacy_test/test_linalg_pinv_op.py +++ b/test/legacy_test/test_linalg_pinv_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class LinalgPinvTestCase(unittest.TestCase): @@ -63,11 +63,11 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data( name="input", shape=self._input_shape, @@ -76,9 +76,9 @@ def test_static(self): out = paddle.linalg.pinv( x, rcond=self.rcond, hermitian=self.hermitian ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": self._input_data}, fetch_list=[out], ) diff --git a/test/legacy_test/test_linear.py b/test/legacy_test/test_linear.py index 8823a4f40be2e..016cb50e453d8 100644 --- a/test/legacy_test/test_linear.py +++ b/test/legacy_test/test_linear.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class LinearTestCase(unittest.TestCase): @@ -45,14 +45,14 @@ def functional(self, place): def paddle_nn_layer(self, place): paddle.disable_static(place) input = paddle.to_tensor(self.input) - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( name="linear_weight", learning_rate=1.0, trainable=False, regularizer=None, initializer=paddle.nn.initializer.Constant(value=1.0), ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( name="linear_bias", learning_rate=1.0, trainable=False, diff --git a/test/legacy_test/test_linear_interp_op.py b/test/legacy_test/test_linear_interp_op.py index 34f0eb7c25f43..d7b6848c3cbe5 100755 --- a/test/legacy_test/test_linear_interp_op.py +++ b/test/legacy_test/test_linear_interp_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def linear_interp_np( @@ -255,8 +255,8 @@ def test_case(self): align_corners=False, data_format='NCW', ) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(x_data) + with base.dygraph.guard(): + x = base.dygraph.to_variable(x_data) interp = us_1(x) expect = linear_interp_np( diff --git a/test/legacy_test/test_linear_interp_v2_op.py b/test/legacy_test/test_linear_interp_v2_op.py index 389b0dee1d22f..87cf56e3eb1b4 100755 --- a/test/legacy_test/test_linear_interp_v2_op.py +++ b/test/legacy_test/test_linear_interp_v2_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard from paddle.nn.functional import interpolate @@ -326,8 +326,8 @@ def test_case(self): align_corners=False, data_format='NCW', ) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(x_data) + with base.dygraph.guard(): + x = base.dygraph.to_variable(x_data) interp = us_1(x) expect = linear_interp_np( @@ -531,7 +531,7 @@ def out_shape_error(): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestLinearInterpOpForFloat16(unittest.TestCase): def init_test_case(self): diff --git a/test/legacy_test/test_linspace.py b/test/legacy_test/test_linspace.py index 6468ad08c8fb5..88a2ef57acc5d 100644 --- a/test/legacy_test/test_linspace.py +++ b/test/legacy_test/test_linspace.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestLinspaceOpCommonCase(OpTest): @@ -147,8 +147,8 @@ def test_variable_input1(self): stop = paddle.full(shape=[1], fill_value=10, dtype='float32') num = paddle.full(shape=[1], fill_value=5, dtype='int32') out = paddle.linspace(start, stop, num, dtype='float32') - exe = fluid.Executor(place=fluid.CPUPlace()) - res = exe.run(fluid.default_main_program(), fetch_list=[out]) + exe = base.Executor(place=base.CPUPlace()) + res = exe.run(base.default_main_program(), fetch_list=[out]) np_res = np.linspace(0, 10, 5, dtype='float32') self.assertEqual((res == np_res).all(), True) @@ -165,9 +165,9 @@ def test_dtype(self): out_1 = paddle.linspace(0, 10, 5, dtype='float32') out_2 = paddle.linspace(0, 10, 5, dtype=np.float32) out_3 = paddle.linspace(0, 10, 5, dtype=core.VarDesc.VarType.FP32) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3 = exe.run( - fluid.default_main_program(), fetch_list=[out_1, out_2, out_3] + base.default_main_program(), fetch_list=[out_1, out_2, out_3] ) np.testing.assert_array_equal(res_1, res_2) diff --git a/test/legacy_test/test_listen_and_serv_op.py b/test/legacy_test/test_listen_and_serv_op.py index 2ff2bddfcf2bf..121490c8ae4fc 100644 --- a/test/legacy_test/test_listen_and_serv_op.py +++ b/test/legacy_test/test_listen_and_serv_op.py @@ -24,7 +24,7 @@ from multiprocessing import Process import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -43,8 +43,8 @@ def run_pserver(use_cuda, sync_mode, ip, port, trainers, trainer_id): sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) sgd_optimizer.minimize(avg_cost) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) pserver_endpoints = ip + ":" + port current_endpoint = ip + ":" + port @@ -80,8 +80,8 @@ def run_pserver_with_empty_block( sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) sgd_optimizer.minimize(avg_cost) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) ps1 = ip + ":" + str(int(port) + 1) ps2 = ip + ":" + port diff --git a/test/legacy_test/test_load_state_dict_from_old_format.py b/test/legacy_test/test_load_state_dict_from_old_format.py index e107a03baff6c..3b36afe0ea35f 100644 --- a/test/legacy_test/test_load_state_dict_from_old_format.py +++ b/test/legacy_test/test_load_state_dict_from_old_format.py @@ -21,8 +21,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def convolutional_neural_network(img): @@ -78,8 +78,8 @@ def tearDown(self): def train_and_save_model(self): with new_program_scope(): - startup_program = fluid.default_startup_program() - main_program = fluid.default_main_program() + startup_program = base.default_startup_program() + main_program = base.default_main_program() img = paddle.static.data( name='img', shape=[None, 1, 28, 28], dtype='float32' @@ -91,14 +91,14 @@ def train_and_save_model(self): prediction, avg_loss = static_train_net(img, label) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) - feeder = fluid.DataFeeder(feed_list=[img, label], place=place) + feeder = base.DataFeeder(feed_list=[img, label], place=place) exe.run(startup_program) train_reader = paddle.batch( @@ -120,8 +120,8 @@ def train_and_save_model(self): break static_param_dict = {} - for param in fluid.default_main_program().all_parameters(): - static_param_dict[param.name] = fluid.executor._fetch_var( + for param in base.default_main_program().all_parameters(): + static_param_dict[param.name] = base.executor._fetch_var( param.name ) diff --git a/test/legacy_test/test_lod_array_length_op.py b/test/legacy_test/test_lod_array_length_op.py index f357043e0c8fb..110493a99940b 100644 --- a/test/legacy_test/test_lod_array_length_op.py +++ b/test/legacy_test/test_lod_array_length_op.py @@ -17,8 +17,8 @@ import numpy import paddle -from paddle.fluid import Program, core, program_guard -from paddle.fluid.executor import Executor +from paddle.base import Program, core, program_guard +from paddle.base.executor import Executor class TestLoDArrayLength(unittest.TestCase): diff --git a/test/legacy_test/test_lod_tensor.py b/test/legacy_test/test_lod_tensor.py index b3f34dccd1c03..4bfe899787e8b 100644 --- a/test/legacy_test/test_lod_tensor.py +++ b/test/legacy_test/test_lod_tensor.py @@ -16,9 +16,9 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.lod_tensor import ( +from paddle import base +from paddle.base import core +from paddle.base.lod_tensor import ( create_lod_tensor, create_random_int_lodtensor, ) @@ -26,7 +26,7 @@ class TestLoDTensor(unittest.TestCase): def test_pybind_recursive_seq_lens(self): - tensor = fluid.LoDTensor() + tensor = base.LoDTensor() recursive_seq_lens = [] tensor.set_recursive_sequence_lengths(recursive_seq_lens) recursive_seq_lens = [[], [1], [3]] @@ -43,9 +43,9 @@ def test_pybind_recursive_seq_lens(self): self.assertEqual( tensor.recursive_sequence_lengths(), recursive_seq_lens ) - tensor.set(np.random.random([6, 1]), fluid.CPUPlace()) + tensor.set(np.random.random([6, 1]), base.CPUPlace()) self.assertTrue(tensor.has_valid_recursive_sequence_lengths()) - tensor.set(np.random.random([9, 1]), fluid.CPUPlace()) + tensor.set(np.random.random([9, 1]), base.CPUPlace()) self.assertFalse(tensor.has_valid_recursive_sequence_lengths()) # Each level's sum should be equal to the number of items in the next level @@ -55,12 +55,12 @@ def test_pybind_recursive_seq_lens(self): self.assertEqual( tensor.recursive_sequence_lengths(), recursive_seq_lens ) - tensor.set(np.random.random([8, 1]), fluid.CPUPlace()) + tensor.set(np.random.random([8, 1]), base.CPUPlace()) self.assertFalse(tensor.has_valid_recursive_sequence_lengths()) recursive_seq_lens = [[2, 3], [1, 3, 1, 2, 1]] tensor.set_recursive_sequence_lengths(recursive_seq_lens) self.assertTrue(tensor.has_valid_recursive_sequence_lengths()) - tensor.set(np.random.random([9, 1]), fluid.CPUPlace()) + tensor.set(np.random.random([9, 1]), base.CPUPlace()) self.assertFalse(tensor.has_valid_recursive_sequence_lengths()) def test_create_lod_tensor(self): @@ -76,10 +76,10 @@ def test_create_lod_tensor(self): create_lod_tensor, data, wrong_recursive_seq_lens, - fluid.CPUPlace(), + base.CPUPlace(), ) tensor = create_lod_tensor( - data, correct_recursive_seq_lens, fluid.CPUPlace() + data, correct_recursive_seq_lens, base.CPUPlace() ) self.assertEqual( tensor.recursive_sequence_lengths(), correct_recursive_seq_lens @@ -94,7 +94,7 @@ def test_create_lod_tensor(self): # Create LoDTensor from numpy array data = np.random.random([10, 1]).astype('float64') recursive_seq_lens = [[2, 1], [3, 3, 4]] - tensor = create_lod_tensor(data, recursive_seq_lens, fluid.CPUPlace()) + tensor = create_lod_tensor(data, recursive_seq_lens, base.CPUPlace()) self.assertEqual( tensor.recursive_sequence_lengths(), recursive_seq_lens ) @@ -105,7 +105,7 @@ def test_create_lod_tensor(self): # Create LoDTensor from another LoDTensor, they are differnt instances new_recursive_seq_lens = [[2, 2, 1], [1, 2, 2, 3, 2]] new_tensor = create_lod_tensor( - tensor, new_recursive_seq_lens, fluid.CPUPlace() + tensor, new_recursive_seq_lens, base.CPUPlace() ) self.assertEqual( tensor.recursive_sequence_lengths(), recursive_seq_lens @@ -122,7 +122,7 @@ def test_create_random_int_lodtensor(self): low = 0 high = dict_size - 1 tensor = create_random_int_lodtensor( - recursive_seq_lens, shape, fluid.CPUPlace(), low, high + recursive_seq_lens, shape, base.CPUPlace(), low, high ) self.assertEqual( tensor.recursive_sequence_lengths(), recursive_seq_lens @@ -136,51 +136,51 @@ def test_print_lodtensor(self): low = 0 high = dict_size - 1 tensor = create_random_int_lodtensor( - recursive_seq_lens, shape, fluid.CPUPlace(), low, high + recursive_seq_lens, shape, base.CPUPlace(), low, high ) print(tensor) self.assertTrue(isinstance(str(tensor), str)) if core.is_compiled_with_cuda(): gtensor = create_random_int_lodtensor( - recursive_seq_lens, shape, fluid.CUDAPlace(0), low, high + recursive_seq_lens, shape, base.CUDAPlace(0), low, high ) print(gtensor) self.assertTrue(isinstance(str(gtensor), str)) def test_dlpack_support(self): - tensor = fluid.create_lod_tensor( + tensor = base.create_lod_tensor( np.array([[1], [2], [3], [4]]).astype('int'), [[1, 3]], - fluid.CPUPlace(), + base.CPUPlace(), ) dltensor = tensor._to_dlpack() - tensor_from_dlpack = fluid.core.from_dlpack(dltensor) - self.assertTrue(isinstance(tensor_from_dlpack, fluid.core.Tensor)) + tensor_from_dlpack = base.core.from_dlpack(dltensor) + self.assertTrue(isinstance(tensor_from_dlpack, base.core.Tensor)) np.testing.assert_array_equal( np.array(tensor_from_dlpack), np.array([[1], [2], [3], [4]]).astype('int'), ) # when build with cuda if core.is_compiled_with_cuda(): - gtensor = fluid.create_lod_tensor( + gtensor = base.create_lod_tensor( np.array([[1], [2], [3], [4]]).astype('int'), [[1, 3]], - fluid.CUDAPlace(0), + base.CUDAPlace(0), ) gdltensor = gtensor._to_dlpack() - gtensor_from_dlpack = fluid.core.from_dlpack(gdltensor) - self.assertTrue(isinstance(gtensor_from_dlpack, fluid.core.Tensor)) + gtensor_from_dlpack = base.core.from_dlpack(gdltensor) + self.assertTrue(isinstance(gtensor_from_dlpack, base.core.Tensor)) np.testing.assert_array_equal( np.array(gtensor_from_dlpack), np.array([[1], [2], [3], [4]]).astype('int'), ) def test_as_type(self): - tensor = fluid.create_lod_tensor( + tensor = base.create_lod_tensor( np.array([[1], [2], [3], [4]]).astype('int'), [[1, 3]], - fluid.CPUPlace(), + base.CPUPlace(), ) fp32_tensor = tensor._as_type(core.VarDesc.VarType.FP32) print(fp32_tensor) diff --git a/test/legacy_test/test_lod_tensor_array.py b/test/legacy_test/test_lod_tensor_array.py index c9d2f1554bda5..cafa79717e8ac 100644 --- a/test/legacy_test/test_lod_tensor_array.py +++ b/test/legacy_test/test_lod_tensor_array.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class TestLoDTensorArray(unittest.TestCase): diff --git a/test/legacy_test/test_log_softmax.py b/test/legacy_test/test_log_softmax.py index 8c951ca8c8965..70da5577485ff 100644 --- a/test/legacy_test/test_log_softmax.py +++ b/test/legacy_test/test_log_softmax.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core np.random.seed(10) @@ -167,7 +167,7 @@ def setUp(self): self.x = np.random.uniform(-1.0, 1.0, self.x_shape).astype(np.float32) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) @@ -201,7 +201,7 @@ def setUp(self): self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) diff --git a/test/legacy_test/test_logaddexp.py b/test/legacy_test/test_logaddexp.py index 1c9e66fb129ff..a55727b774dd9 100644 --- a/test/legacy_test/test_logaddexp.py +++ b/test/legacy_test/test_logaddexp.py @@ -33,7 +33,7 @@ class TestLogsumexpAPI(unittest.TestCase): def setUp(self): self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) diff --git a/test/legacy_test/test_logcumsumexp_op.py b/test/legacy_test/test_logcumsumexp_op.py index 2643b19f68e24..ab0df6cf73a19 100644 --- a/test/legacy_test/test_logcumsumexp_op.py +++ b/test/legacy_test/test_logcumsumexp_op.py @@ -24,8 +24,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def np_naive_logcumsumexp(x: np.ndarray, axis: Optional[int] = None): @@ -149,7 +149,7 @@ def run_imperative(self): np.testing.assert_allclose(z, y.numpy(), rtol=1e-05) def run_static(self, use_gpu=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data_np = np.random.random((5, 4)).astype(np.float32) x = paddle.static.data('X', [5, 4]) y = paddle.logcumsumexp(x) @@ -158,9 +158,9 @@ def run_static(self, use_gpu=False): y4 = paddle.logcumsumexp(x, dtype='float64') y5 = paddle.logcumsumexp(x, axis=-2) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) out = exe.run( feed={'X': data_np}, fetch_list=[ @@ -183,37 +183,37 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(z, out[4], rtol=1e-05) def test_cpu(self): - paddle.disable_static(paddle.fluid.CPUPlace()) + paddle.disable_static(paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(paddle.fluid.CUDAPlace(0)) + paddle.disable_static(paddle.base.CUDAPlace(0)) self.run_imperative() paddle.enable_static() self.run_static(use_gpu=True) def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data('x', [3, 4]) y = paddle.logcumsumexp(x, name='out') self.assertTrue('out' in y.name) def test_type_error(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): with self.assertRaises(TypeError): data_np = np.random.random((100, 100), dtype=np.int32) x = paddle.static.data('X', [100, 100], dtype='int32') y = paddle.logcumsumexp(x) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) + exe = base.Executor(place) + exe.run(base.default_startup_program()) out = exe.run(feed={'X': data_np}, fetch_list=[y.name]) diff --git a/test/legacy_test/test_logit_op.py b/test/legacy_test/test_logit_op.py index edec9fb1d309a..e2123bfa8f9c5 100644 --- a/test/legacy_test/test_logit_op.py +++ b/test/legacy_test/test_logit_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) @@ -148,7 +148,7 @@ def setUp(self): self.x = np.random.uniform(0.0, 1.0, self.x_shape).astype(np.float32) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) diff --git a/test/legacy_test/test_logspace.py b/test/legacy_test/test_logspace.py index 0587846bc4841..402442ca4bc06 100644 --- a/test/legacy_test/test_logspace.py +++ b/test/legacy_test/test_logspace.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core class TestLogspaceOpCommonCase(OpTest): diff --git a/test/legacy_test/test_logsumexp.py b/test/legacy_test/test_logsumexp.py index a5184650f36d7..44c958fef6323 100644 --- a/test/legacy_test/test_logsumexp.py +++ b/test/legacy_test/test_logsumexp.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def ref_logsumexp(x, axis=None, keepdim=False, reduce_all=False): @@ -125,7 +125,7 @@ def set_attrs(self): self.axis = [0, 1, 2, 3] def set_attrs_addition(self): - if paddle.fluid.core.is_compiled_with_rocm(): + if paddle.base.core.is_compiled_with_rocm(): self.user_defined_grads = [self.calc_grad()] self.user_defined_grad_outputs = [np.ones(1, dtype=self.dtype)] @@ -140,7 +140,7 @@ def set_attrs(self): self.reduce_all = True def set_attrs_addition(self): - if paddle.fluid.core.is_compiled_with_rocm(): + if paddle.base.core.is_compiled_with_rocm(): self.user_defined_grads = [self.calc_grad()] self.user_defined_grad_outputs = [np.ones(1, dtype=self.dtype)] @@ -239,7 +239,7 @@ def setUp(self): self.x = np.random.uniform(-1, 1, self.shape).astype(np.float32) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) @@ -281,7 +281,7 @@ def test_alias(self): # Test logsumexp bug class TestLogZeroError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_lookahead.py b/test/legacy_test/test_lookahead.py index bfcd7afd4b795..f9eeee2f651a4 100644 --- a/test/legacy_test/test_lookahead.py +++ b/test/legacy_test/test_lookahead.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn LOOKAHEAD_K = 5 LOOKAHEAD_ALPHA = 0.2 @@ -27,13 +27,13 @@ class TestLookAhead(unittest.TestCase): def test_lookahead_static(self): paddle.enable_static() - place = fluid.CPUPlace() + place = base.CPUPlace() shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_program = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_program, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_program = base.Program() + startup = base.Program() + with base.program_guard(train_program, startup): + with base.unique_name.guard(): data = paddle.static.data( name='X', shape=[None, 1], dtype='float32' ) diff --git a/test/legacy_test/test_lookup_table_bf16_op.py b/test/legacy_test/test_lookup_table_bf16_op.py index 48cb484f0d81d..942a9cc3ccd74 100644 --- a/test/legacy_test/test_lookup_table_bf16_op.py +++ b/test/legacy_test/test_lookup_table_bf16_op.py @@ -24,8 +24,8 @@ from op import Operator import paddle -from paddle import enable_static, fluid -from paddle.fluid import core +from paddle import base, enable_static +from paddle.base import core def _lookup(weights, ids, flat_ids, op_version="lookup_table"): @@ -227,25 +227,25 @@ def setUp(self): self.flat_ids = self.ids.flatten() self.value = 3.0 self.w_fp32 = np.full(self.w_shape, self.value) - self.place = fluid.CPUPlace() - self.prog = fluid.Program() - self.startup_prog = fluid.Program() + self.place = base.CPUPlace() + self.prog = base.Program() + self.startup_prog = base.Program() self.set_initializer() - with fluid.program_guard(self.prog, self.startup_prog): + with base.program_guard(self.prog, self.startup_prog): x = paddle.static.data( name='x', shape=self.ids_shape, dtype='int64' ) self.emb = paddle.static.nn.embedding( input=x, size=self.w_shape, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="emb_weight", initializer=self.initializer ), is_sparse=False, dtype="uint16", ) # bfloat16 - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(self.startup_prog) self.result = exe.run( self.prog, feed={'x': self.ids}, fetch_list=['emb_weight', self.emb] diff --git a/test/legacy_test/test_lookup_table_op.py b/test/legacy_test/test_lookup_table_op.py index 04ac09bdce996..8a3eb0ef2da1f 100644 --- a/test/legacy_test/test_lookup_table_op.py +++ b/test/legacy_test/test_lookup_table_op.py @@ -25,7 +25,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard class TestLookupTableOp(OpTest): diff --git a/test/legacy_test/test_lookup_table_v2_bf16_op.py b/test/legacy_test/test_lookup_table_v2_bf16_op.py index 54cf40210c2b6..13b15053a6e67 100644 --- a/test/legacy_test/test_lookup_table_v2_bf16_op.py +++ b/test/legacy_test/test_lookup_table_v2_bf16_op.py @@ -25,8 +25,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestLookupTableV2BF16Op(TestLookupTableBF16Op): @@ -101,25 +101,25 @@ def setUp(self): self.flat_ids = self.ids.flatten() self.value = 3.0 self.w_fp32 = np.full(self.w_shape, self.value) - self.place = fluid.CPUPlace() - self.prog = fluid.Program() - self.startup_prog = fluid.Program() + self.place = base.CPUPlace() + self.prog = base.Program() + self.startup_prog = base.Program() self.set_initializer() - with fluid.program_guard(self.prog, self.startup_prog): + with base.program_guard(self.prog, self.startup_prog): x = paddle.static.data( name='x', shape=[-1] + self.ids_shape, dtype='int64' ) self.emb = paddle.static.nn.embedding( input=x, size=self.w_shape, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="emb_weight", initializer=self.initializer ), is_sparse=False, dtype="uint16", ) # bfloat16 - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(self.startup_prog) self.result = exe.run( self.prog, feed={'x': self.ids}, fetch_list=['emb_weight', self.emb] diff --git a/test/legacy_test/test_lookup_table_v2_op.py b/test/legacy_test/test_lookup_table_v2_op.py index c02ce3f95be84..5000c3486b5e6 100644 --- a/test/legacy_test/test_lookup_table_v2_op.py +++ b/test/legacy_test/test_lookup_table_v2_op.py @@ -19,8 +19,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestStaticGraphSupportMultipleInt(unittest.TestCase): @@ -204,14 +204,14 @@ def init_data(self): def get_w_grad(self, is_sparse): self.init_data() - main_program = fluid.Program() - with fluid.program_guard(main_program, fluid.Program()): + main_program = base.Program() + with base.program_guard(main_program, base.Program()): x = paddle.static.data(name='x', shape=[-1, 5], dtype='int64') y_ = paddle.static.data(name='y_', shape=[-1, 5], dtype='float32') emb = paddle.static.nn.embedding( input=x, size=[10, 16], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="emb_weight", learning_rate=10, initializer=paddle.nn.initializer.Assign(self.w_data), @@ -226,9 +226,9 @@ def get_w_grad(self, is_sparse): sgd_optimizer = paddle.optimizer.SGD(learning_rate=1e-4) sgd_optimizer.minimize(loss) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={'x': self.x_data, 'y_': self.y_data}, fetch_list=['emb_weight'], @@ -253,11 +253,11 @@ def test_api(self): x = paddle.static.data(name='x', shape=[-1, 20], dtype='int64') emb = paddle.static.nn.embedding(input=x, size=[128, 64]) - place = fluid.CPUPlace() + place = base.CPUPlace() x_data = np.random.randint(0, 127, [2, 20]).astype("int64") - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={ 'x': x_data, diff --git a/test/legacy_test/test_lr_scheduler.py b/test/legacy_test/test_lr_scheduler.py index 5a5ce8aa166dc..54484ecc6ad2c 100644 --- a/test/legacy_test/test_lr_scheduler.py +++ b/test/legacy_test/test_lr_scheduler.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def reduce_lr_on_plateau( diff --git a/test/legacy_test/test_lrn_op.py b/test/legacy_test/test_lrn_op.py index df9b1ebccf481..2e65b92ebe532 100644 --- a/test/legacy_test/test_lrn_op.py +++ b/test/legacy_test/test_lrn_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestLRNOp(OpTest): @@ -110,13 +110,13 @@ def init_test_case(self): class TestLocalResponseNormFAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_3d_input(self, place): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): in_np1 = np.random.random([3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 1)) @@ -132,9 +132,9 @@ def check_static_3d_input(self, place): res2 = paddle.nn.functional.local_response_norm( x=input2, size=5, data_format='NLC' ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input1": in_np1, "input2": in_np2}, fetch_list=[res1, res2], ) @@ -146,7 +146,7 @@ def check_static_3d_input(self, place): def check_static_4d_input(self, place): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input1 = paddle.static.data( name="input1", shape=[3, 3, 40, 40], dtype="float32" ) @@ -164,9 +164,9 @@ def check_static_4d_input(self, place): in_np1 = np.random.random([3, 3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 3, 1)) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input1": in_np1, "input2": in_np2}, fetch_list=[res1, res2], ) @@ -178,7 +178,7 @@ def check_static_4d_input(self, place): def check_static_5d_input(self, place): with paddle_static_guard(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input1 = paddle.static.data( name="input1", shape=[3, 3, 3, 40, 40], dtype="float32" ) @@ -195,9 +195,9 @@ def check_static_5d_input(self, place): in_np1 = np.random.random([3, 3, 3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 3, 4, 1)) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input1": in_np1, "input2": in_np2}, fetch_list=[res1, res2], ) @@ -215,7 +215,7 @@ def test_static(self): self.check_static_5d_input(place=place) def check_dygraph_3d_input(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np1 = np.random.random([3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 1)) @@ -233,7 +233,7 @@ def check_dygraph_3d_input(self, place): np.testing.assert_allclose(res1.numpy(), res2_tran, rtol=1e-05) def check_dygraph_4d_input(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np1 = np.random.random([3, 3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 3, 1)) @@ -251,7 +251,7 @@ def check_dygraph_4d_input(self, place): np.testing.assert_allclose(res1.numpy(), res2_tran, rtol=1e-05) def check_dygraph_5d_input(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in_np1 = np.random.random([3, 3, 3, 40, 40]).astype("float32") in_np2 = np.transpose(in_np1, (0, 2, 3, 4, 1)) @@ -282,10 +282,10 @@ def test_errors(self): def test_Variable(): # the input of lrn must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], - fluid.CPUPlace(), + base.CPUPlace(), ) paddle.nn.functional.local_response_norm(x1, size=5) @@ -327,13 +327,13 @@ def test_shape(): class TestLocalResponseNormCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): in1 = paddle.rand(shape=(3, 3, 40, 40), dtype="float32") in2 = paddle.transpose(in1, [0, 2, 3, 1]) @@ -347,7 +347,7 @@ def test_dygraph(self): np.testing.assert_allclose(res1.numpy(), res2_tran, rtol=1e-05) def test_static_fp16_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle_static_guard(): with paddle.static.program_guard( diff --git a/test/legacy_test/test_lstm_cudnn_op.py b/test/legacy_test/test_lstm_cudnn_op.py index 2e171913915aa..20b0066dd55a7 100644 --- a/test/legacy_test/test_lstm_cudnn_op.py +++ b/test/legacy_test/test_lstm_cudnn_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core random.seed(2) np.set_printoptions(threshold=np.inf) diff --git a/test/legacy_test/test_lu_op.py b/test/legacy_test/test_lu_op.py index 27f6505641c63..f12d4ae6c893a 100644 --- a/test/legacy_test/test_lu_op.py +++ b/test/legacy_test/test_lu_op.py @@ -22,8 +22,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def scipy_lu(A, pivot): @@ -202,9 +202,9 @@ def run_lu_dygraph(shape, dtype): min_mn = min(m, n) pivot = True - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: paddle.disable_static(place) batch_size = a.size // (a.shape[-1] * a.shape[-2]) @@ -253,11 +253,11 @@ def run_lu_static(shape, dtype): pivot = True places = [] - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): batch_size = a.size // (a.shape[-1] * a.shape[-2]) sP, sl, sU = scipy_lu(a, pivot) sL = np.tril(sl, -1) @@ -282,9 +282,9 @@ def run_lu_static(shape, dtype): name="input", shape=shape, dtype=dtype ) lu, p = paddle.linalg.lu(x, pivot=pivot) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": a}, fetch_list=[lu, p], ) @@ -310,7 +310,7 @@ def run_lu_static(shape, dtype): class TestLUAPIError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in lu should not be 0. def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_lu_unpack_op.py b/test/legacy_test/test_lu_unpack_op.py index 06cc404c8846c..bfac8781076bf 100644 --- a/test/legacy_test/test_lu_unpack_op.py +++ b/test/legacy_test/test_lu_unpack_op.py @@ -22,8 +22,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def scipy_lu_unpack(A): @@ -138,17 +138,17 @@ def setUp(self): lu = lu.numpy() pivots = pivots.numpy() else: - with fluid.program_guard(fluid.Program(), fluid.Program()): - place = fluid.CPUPlace() + with base.program_guard(base.Program(), base.Program()): + place = base.CPUPlace() if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) xv = paddle.static.data( name="input", shape=self.x_shape, dtype=self.dtype ) lu, p = paddle.linalg.lu(xv) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": x}, fetch_list=[lu, p], ) @@ -228,9 +228,9 @@ def run_lu_unpack_dygraph(shape, dtype): n = a.shape[-1] min_mn = min(m, n) - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: paddle.disable_static(place) @@ -271,11 +271,11 @@ def run_lu_static(shape, dtype): n = a.shape[-1] min_mn = min(m, n) - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): sP, sL, sU = scipy_lu_unpack(a) x = paddle.static.data( @@ -283,9 +283,9 @@ def run_lu_static(shape, dtype): ) lu, p = paddle.linalg.lu(x) pP, pL, pU = paddle.linalg.lu_unpack(lu, p) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": a}, fetch_list=[pP, pL, pU], ) @@ -317,7 +317,7 @@ def run_lu_static(shape, dtype): class TestLU_UnpackAPIError(unittest.TestCase): def test_errors_1(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in lu should not be 0. def test_x_size(): x = paddle.to_tensor( @@ -337,7 +337,7 @@ def test_x_size(): self.assertRaises(ValueError, test_x_size) def test_errors_2(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in lu should not be 0. def test_y_size(): x = paddle.to_tensor( @@ -357,7 +357,7 @@ def test_y_size(): self.assertRaises(ValueError, test_y_size) def test_errors_3(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in lu should not be 0. def test_y_data(): x = paddle.to_tensor( diff --git a/test/legacy_test/test_manual_seed.py b/test/legacy_test/test_manual_seed.py index 7af57833312e4..dccf88ae29565 100644 --- a/test/legacy_test/test_manual_seed.py +++ b/test/legacy_test/test_manual_seed.py @@ -17,13 +17,13 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.tensor import random class TestManualSeed(unittest.TestCase): def test_seed(self): - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = random.gaussian([10], dtype="float32") @@ -38,7 +38,7 @@ def test_seed(self): x2_np = x2.numpy() x3_np = x3.numpy() - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): np.testing.assert_allclose(x1_np, x2_np, rtol=1e-05) np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) diff --git a/test/legacy_test/test_margin_cross_entropy_op.py b/test/legacy_test/test_margin_cross_entropy_op.py index 439a449b58788..9c27be97fc577 100644 --- a/test/legacy_test/test_margin_cross_entropy_op.py +++ b/test/legacy_test/test_margin_cross_entropy_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard def stable_softmax_comm(x): @@ -322,7 +322,7 @@ def setUp(self): paddle.framework.random._manual_program_seed(self.seed) self.places = [] if core.is_compiled_with_cuda(): - self.places.append(paddle.fluid.CUDAPlace(0)) + self.places.append(paddle.base.CUDAPlace(0)) def initParams(self): self.python_out_sig = ["Loss"] @@ -402,9 +402,9 @@ def check_static_result(self, place): reduction=self.reduction, ) - exe = paddle.fluid.Executor(place) + exe = paddle.base.Executor(place) [loss_res, softmax_res] = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={'logits': logits_np, 'label': labels_np}, fetch_list=[loss, softmax], ) @@ -416,7 +416,7 @@ def test_dynamic(self): self.check_dynamic_result(place=place) def check_dynamic_result(self, place): - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): datas = np.random.uniform( -0.99, 0.99, [self.batch_dim, self.feat_dim] ).astype(self.dtype) @@ -492,7 +492,7 @@ def setUp(self): paddle.framework.random._manual_program_seed(self.seed) self.places = [] if core.is_compiled_with_cuda(): - self.places.append(paddle.fluid.CUDAPlace(0)) + self.places.append(paddle.base.CUDAPlace(0)) def initParams(self): self.python_api = python_api @@ -517,7 +517,7 @@ def init_dtype(self): def test_dynamic_errors(self): def test_dim(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): labels_np = np.random.randint( 0, self.num_class, (self.batch_dim, 2), dtype="int64" ) @@ -540,7 +540,7 @@ def test_dim(): def test_label_type(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): labels_np = np.random.uniform( 0, self.num_class, (self.batch_dim, 1) ).astype(self.dtype) @@ -563,7 +563,7 @@ def test_label_type(): def test_group_value(): for place in self.places: - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): labels_np = np.random.randint( 0, self.num_class, (self.batch_dim,), dtype="int64" ) diff --git a/test/legacy_test/test_margin_rank_loss_op.py b/test/legacy_test/test_margin_rank_loss_op.py index 2b3c3b51ff383..ad40b546b81a5 100644 --- a/test/legacy_test/test_margin_rank_loss_op.py +++ b/test/legacy_test/test_margin_rank_loss_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid +from paddle import base class TestMarginRankLossOp(OpTest): @@ -75,19 +75,19 @@ def setUp(self): self.loss = loss def test_identity(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self.check_identity(place) - if fluid.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check_identity(place) def check_identity(self, place): with paddle_static_guard(): - main = fluid.Program() - start = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): label = paddle.static.data( "label", (self.batch_size, 1), "float32" ) @@ -101,7 +101,7 @@ def check_identity(self, place): x1, x2, label, self.margin, 'none' ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(start) (out_np,) = exe.run( main, diff --git a/test/legacy_test/test_masked_select_op.py b/test/legacy_test/test_masked_select_op.py index 6d89ced3ecc07..572e0ecb17583 100644 --- a/test/legacy_test/test_masked_select_op.py +++ b/test/legacy_test/test_masked_select_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def np_masked_select(x, mask): diff --git a/test/legacy_test/test_math_op_patch.py b/test/legacy_test/test_math_op_patch.py index 7eab8dcf4fd4b..55b3f55c45451 100644 --- a/test/legacy_test/test_math_op_patch.py +++ b/test/legacy_test/test_math_op_patch.py @@ -19,7 +19,7 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid +from paddle import base class TestMathOpPatches(unittest.TestCase): @@ -36,11 +36,11 @@ def test_add_scalar(self): c = ab + 10 d = ab + a # e = a + ab - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') b_np, c_np, d_np = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b, c, d] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b, c, d] ) np.testing.assert_allclose(a_np + 10, b_np, rtol=1e-05) ab_np = np.concatenate([a_np, b_np], axis=1) @@ -52,11 +52,11 @@ def test_add_scalar(self): def test_radd_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = 10 + a - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(a_np + 10, b_np, rtol=1e-05) @@ -64,11 +64,11 @@ def test_radd_scalar(self): def test_sub_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = a - 10 - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(a_np - 10, b_np, rtol=1e-05) @@ -76,11 +76,11 @@ def test_sub_scalar(self): def test_rsub_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = 10 - a - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(10 - a_np, b_np, rtol=1e-05) @@ -88,11 +88,11 @@ def test_rsub_scalar(self): def test_mul_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = a * 10 - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(a_np * 10, b_np, rtol=1e-05) @@ -100,11 +100,11 @@ def test_mul_scalar(self): def test_rmul_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = 10 * a - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(10 * a_np, b_np, rtol=1e-05) @@ -112,11 +112,11 @@ def test_rmul_scalar(self): def test_div_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = a / 10 - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(a_np / 10, b_np, rtol=1e-05) @@ -124,12 +124,12 @@ def test_div_scalar(self): def test_rdiv_scalar(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = 10 / a - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') + 1e-2 (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(10 / a_np, b_np, rtol=1e-05) @@ -138,12 +138,12 @@ def test_div_two_tensor(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = paddle.static.data(name="b", shape=[-1, 1]) c = a / b - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') b_np = np.random.random(size=[10, 1]).astype('float32') + 1e-2 (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c], ) @@ -154,12 +154,12 @@ def test_mul_two_tensor(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = paddle.static.data(name="b", shape=[-1, 1]) c = a * b - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') b_np = np.random.random(size=[10, 1]).astype('float32') (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c], ) @@ -170,12 +170,12 @@ def test_add_two_tensor(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = paddle.static.data(name="b", shape=[-1, 1]) c = a + b - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') b_np = np.random.random(size=[10, 1]).astype('float32') (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c], ) @@ -186,12 +186,12 @@ def test_sub_two_tensor(self): a = paddle.static.data(name="a", shape=[-1, 1]) b = paddle.static.data(name="b", shape=[-1, 1]) c = a - b - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.random(size=[10, 1]).astype('float32') b_np = np.random.random(size=[10, 1]).astype('float32') (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, 'b': b_np}, fetch_list=[c], ) @@ -201,11 +201,11 @@ def test_sub_two_tensor(self): def test_integer_div(self): a = paddle.static.data(name="a", shape=[-1, 1], dtype='int64') b = a / 7 - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.array([3, 4, 10, 14, 9, 18]).astype('int64') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) b_np_actual = (a_np / 7).astype('float32') @@ -217,19 +217,19 @@ def test_equal(self): b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32') c = a == b - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float32') b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float32') (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, "b": b_np}, fetch_list=[c], ) np.testing.assert_array_equal(c_np, a_np == b_np) - self.assertEqual(c.dtype, fluid.core.VarDesc.VarType.BOOL) + self.assertEqual(c.dtype, base.core.VarDesc.VarType.BOOL) @prog_scope() def test_equal_and_cond(self): @@ -242,12 +242,12 @@ def test_equal_and_cond(self): cond = one == zero c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.array([3, 4, 10, 14, 9, 18]).astype('float32') b_np = np.array([3, 4, 11, 15, 8, 18]).astype('float32') (c_np,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"a": a_np, "b": b_np}, fetch_list=[c], ) @@ -259,12 +259,12 @@ def test_neg(self): a = paddle.static.data(name="a", shape=[-1, 10, 1], dtype='float32') a.desc.set_need_check_feed(False) b = -a - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float32') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(-a_np, b_np, rtol=1e-05) @@ -273,12 +273,12 @@ def test_astype(self): a = paddle.static.data(name="a", shape=[-1, 10, 1]) a.desc.set_need_check_feed(False) b = a.astype('float32') - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) a_np = np.random.uniform(-1, 1, size=[10, 1]).astype('float64') (b_np,) = exe.run( - fluid.default_main_program(), feed={"a": a_np}, fetch_list=[b] + base.default_main_program(), feed={"a": a_np}, fetch_list=[b] ) np.testing.assert_allclose(a_np.astype('float32'), b_np, rtol=1e-05) @@ -291,9 +291,9 @@ def test_bitwise_and(self): y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") z = x & y - exe = fluid.Executor() + exe = base.Executor() out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "y": y_np}, fetch_list=[z], ) @@ -309,9 +309,9 @@ def test_bitwise_or(self): y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") z = x | y - exe = fluid.Executor() + exe = base.Executor() out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "y": y_np}, fetch_list=[z], ) @@ -327,9 +327,9 @@ def test_bitwise_xor(self): y = paddle.static.data(name="y", shape=[2, 3, 5], dtype="int32") z = x ^ y - exe = fluid.Executor() + exe = base.Executor() out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "y": y_np}, fetch_list=[z], ) @@ -343,9 +343,9 @@ def test_bitwise_not(self): x = paddle.static.data(name="x", shape=[2, 3, 5], dtype="int32") z = ~x - exe = fluid.Executor() + exe = base.Executor() out = exe.run( - fluid.default_main_program(), feed={"x": x_np}, fetch_list=[z] + base.default_main_program(), feed={"x": x_np}, fetch_list=[z] ) np.testing.assert_array_equal(out[0], out_np) @@ -357,9 +357,9 @@ def test_T(self): x = paddle.static.data(name="x", shape=[2, 8, 5, 3], dtype="int32") z = x.T - exe = fluid.Executor() + exe = base.Executor() out = exe.run( - fluid.default_main_program(), feed={"x": x_np}, fetch_list=[z] + base.default_main_program(), feed={"x": x_np}, fetch_list=[z] ) np.testing.assert_array_equal(out[0], out_np) diff --git a/test/legacy_test/test_math_op_patch_var_base.py b/test/legacy_test/test_math_op_patch_var_base.py index c392fb972697d..7a2970289fcd2 100644 --- a/test/legacy_test/test_math_op_patch_var_base.py +++ b/test/legacy_test/test_math_op_patch_var_base.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestMathOpPatchesVarBase(unittest.TestCase): @@ -29,76 +29,76 @@ def setUp(self): def test_add(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a + b np.testing.assert_array_equal(res.numpy(), a_np + b_np) def test_sub(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a - b np.testing.assert_array_equal(res.numpy(), a_np - b_np) def test_mul(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a * b np.testing.assert_array_equal(res.numpy(), a_np * b_np) def test_div(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a / b # NOTE: Not sure why array_equal fails on windows, allclose is acceptable np.testing.assert_allclose(res.numpy(), a_np / b_np, rtol=1e-05) def test_add_scalar(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = a + b np.testing.assert_array_equal(res.numpy(), a_np + b) def test_add_scalar_reverse(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = b + a np.testing.assert_array_equal(res.numpy(), b + a_np) def test_sub_scalar(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = a - b np.testing.assert_array_equal(res.numpy(), a_np - b) def test_sub_scalar_reverse(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = b - a np.testing.assert_array_equal(res.numpy(), b - a_np) def test_mul_scalar(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = a * b np.testing.assert_array_equal(res.numpy(), a_np * b) @@ -106,8 +106,8 @@ def test_mul_scalar(self): # div_scalar, not equal def test_div_scalar(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) b = 0.1 res = a / b np.testing.assert_allclose(res.numpy(), a_np / b, rtol=1e-05) @@ -116,27 +116,27 @@ def test_div_scalar(self): def test_pow(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a**b np.testing.assert_allclose(res.numpy(), a_np**b_np, rtol=1e-05) def test_floor_div(self): a_np = np.random.randint(1, 100, size=self.shape) b_np = np.random.randint(1, 100, size=self.shape) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a // b np.testing.assert_array_equal(res.numpy(), a_np // b_np) def test_mod(self): a_np = np.random.randint(1, 100, size=self.shape) b_np = np.random.randint(1, 100, size=self.shape) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a % b np.testing.assert_array_equal(res.numpy(), a_np % b_np) @@ -170,10 +170,10 @@ def test_equal(self): a_np = np.asarray([1, 2, 3, 4, 5]) b_np = np.asarray([1, 2, 3, 4, 5]) c_np = np.asarray([1, 2, 2, 4, 5]) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) - c = fluid.dygraph.to_variable(c_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) + c = base.dygraph.to_variable(c_np) res1 = a == b res2 = a == c np.testing.assert_array_equal(res1.numpy(), a_np == b_np) @@ -183,10 +183,10 @@ def test_not_equal(self): a_np = np.asarray([1, 2, 3, 4, 5]) b_np = np.asarray([1, 2, 3, 4, 5]) c_np = np.asarray([1, 2, 2, 4, 5]) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) - c = fluid.dygraph.to_variable(c_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) + c = base.dygraph.to_variable(c_np) res1 = a != b res2 = a != c np.testing.assert_array_equal(res1.numpy(), a_np != b_np) @@ -195,49 +195,49 @@ def test_not_equal(self): def test_less_than(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a < b np.testing.assert_array_equal(res.numpy(), a_np < b_np) def test_less_equal(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a <= b np.testing.assert_array_equal(res.numpy(), a_np <= b_np) def test_greater_than(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a > b np.testing.assert_array_equal(res.numpy(), a_np > b_np) def test_greater_equal(self): a_np = np.random.random(self.shape).astype(self.dtype) b_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a >= b np.testing.assert_array_equal(res.numpy(), a_np >= b_np) def test_neg(self): a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) res = -a np.testing.assert_array_equal(res.numpy(), -a_np) def test_float_int_long(self): - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(np.array([100.1])) + with base.dygraph.guard(): + a = base.dygraph.to_variable(np.array([100.1])) self.assertTrue(float(a) == 100.1) self.assertTrue(int(a) == 100) self.assertTrue(int(a) == 100) @@ -249,13 +249,13 @@ def test_float_int_long(self): def test_len(self): a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) self.assertTrue(len(a) == 10) def test_index(self): - with fluid.dygraph.guard(): - var1 = fluid.dygraph.to_variable(np.array([2])) + with base.dygraph.guard(): + var1 = base.dygraph.to_variable(np.array([2])) i_tmp = 0 for i in range(var1): self.assertTrue(i == i_tmp) @@ -276,7 +276,7 @@ def test_index(self): self.assertTrue(str1[var1] == 's') def test_np_left_mul(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): t = np.sqrt(2.0 * np.pi) x = paddle.ones((2, 2), dtype="float32") y = t * x @@ -291,16 +291,16 @@ def test_np_left_mul(self): def test_add_different_dtype(self): a_np = np.random.random(self.shape).astype(np.float32) b_np = np.random.random(self.shape).astype(np.float16) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) res = a + b np.testing.assert_array_equal(res.numpy(), a_np + b_np) def test_floordiv_different_dtype(self): a_np = np.full(self.shape, 10, np.int64) b_np = np.full(self.shape, 2, np.int32) - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = paddle.to_tensor(a_np) b = paddle.to_tensor(b_np) res = a // b @@ -308,11 +308,11 @@ def test_floordiv_different_dtype(self): def test_astype(self): a_np = np.random.uniform(-1, 1, self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) res1 = a.astype(np.float16) res2 = a.astype('float16') - res3 = a.astype(fluid.core.VarDesc.VarType.FP16) + res3 = a.astype(base.core.VarDesc.VarType.FP16) self.assertEqual(res1.dtype, res2.dtype) self.assertEqual(res1.dtype, res3.dtype) @@ -323,11 +323,11 @@ def test_astype(self): def test_conpare_op_broadcast(self): a_np = np.random.uniform(-1, 1, [10, 1, 10]).astype(self.dtype) b_np = np.random.uniform(-1, 1, [1, 1, 10]).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) - self.assertEqual((a != b).dtype, fluid.core.VarDesc.VarType.BOOL) + self.assertEqual((a != b).dtype, base.core.VarDesc.VarType.BOOL) np.testing.assert_array_equal((a != b).numpy(), a_np != b_np) def test_tensor_patch_method(self): @@ -400,7 +400,7 @@ def test_tensor_patch_method(self): ) d = d.matmul(d.t()) # ROCM not support cholesky - if not fluid.core.is_compiled_with_rocm(): + if not base.core.is_compiled_with_rocm(): np.testing.assert_array_equal( d.cholesky().numpy(), paddle.cholesky(d).numpy() ) @@ -611,8 +611,8 @@ def test_tensor_patch_method(self): def test_complex_scalar(self): a_np = np.random.random(self.shape).astype(self.dtype) - with fluid.dygraph.guard(): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(): + a = base.dygraph.to_variable(a_np) res = 1j * a np.testing.assert_array_equal(res.numpy(), 1j * a_np) @@ -621,7 +621,7 @@ def test_matmul(self): y_np = np.random.uniform(-1, 1, [3, 2]).astype(self.dtype) except_out = x_np @ y_np - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor(x_np) y = paddle.to_tensor(y_np) out = x @ y diff --git a/test/legacy_test/test_matmul_int8_op.py b/test/legacy_test/test_matmul_int8_op.py index b1aa9d3286845..f482f7b194446 100644 --- a/test/legacy_test/test_matmul_int8_op.py +++ b/test/legacy_test/test_matmul_int8_op.py @@ -18,7 +18,7 @@ from test_sparse_attention_op import get_cuda_version import paddle -from paddle.fluid import core +from paddle.base import core paddle.disable_static() diff --git a/test/legacy_test/test_matmul_op.py b/test/legacy_test/test_matmul_op.py index b5ac23f3e38ad..cf2fd10d5dcf2 100644 --- a/test/legacy_test/test_matmul_op.py +++ b/test/legacy_test/test_matmul_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid +from paddle import base def generate_compatible_shapes(dim_X, dim_Y, transpose_X, transpose_Y): @@ -168,11 +168,11 @@ def generate_compatible_shapes_ndim(dim, transpose_X, transpose_Y): class API_TestMm(unittest.TestCase): def test_out(self): with paddle_static_guard(): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2], dtype="float64") y = paddle.static.data(name='y', shape=[2], dtype='float64') result = paddle.mm(x, y) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) data1 = np.random.rand(2) data2 = np.random.rand(2) np_res = exe.run( @@ -191,12 +191,12 @@ def test_out(self): ) def test_dygraph_without_out(self): - device = fluid.CPUPlace() - with fluid.dygraph.guard(device): + device = base.CPUPlace() + with base.dygraph.guard(device): input_array1 = np.random.rand(3, 4).astype("float64") input_array2 = np.random.rand(4, 3).astype("float64") - data1 = fluid.dygraph.to_variable(input_array1) - data2 = fluid.dygraph.to_variable(input_array2) + data1 = base.dygraph.to_variable(input_array1) + data2 = base.dygraph.to_variable(input_array2) out = paddle.mm(data1, data2) expected_result = np.matmul(input_array1, input_array2) np.testing.assert_allclose(expected_result, out.numpy(), rtol=1e-05) @@ -204,12 +204,12 @@ def test_dygraph_without_out(self): class Test_API_Matmul(unittest.TestCase): def test_dygraph_without_out(self): - device = fluid.CPUPlace() - with fluid.dygraph.guard(device): + device = base.CPUPlace() + with base.dygraph.guard(device): input_array1 = np.random.rand(3, 4).astype("float64") input_array2 = np.random.rand(4, 3).astype("float64") - data1 = fluid.dygraph.to_variable(input_array1) - data2 = fluid.dygraph.to_variable(input_array2) + data1 = base.dygraph.to_variable(input_array1) + data2 = base.dygraph.to_variable(input_array2) out = paddle.matmul(data1, data2) expected_result = np.matmul(input_array1, input_array2) np.testing.assert_allclose(expected_result, out.numpy(), rtol=1e-05) @@ -220,7 +220,7 @@ def test_errors(self): with paddle_static_guard(): def test_error1(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[10, 2], dtype="float32" ) @@ -232,7 +232,7 @@ def test_error1(): self.assertRaises(ValueError, test_error1) def test_error2(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[-1, 10, 2], dtype="float32" ) @@ -244,7 +244,7 @@ def test_error2(): test_error2() def test_error3(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[10, 10, 2], dtype="float32" ) diff --git a/test/legacy_test/test_matmul_v2_op.py b/test/legacy_test/test_matmul_v2_op.py index fa939fd29d48e..681a24fd243c4 100644 --- a/test/legacy_test/test_matmul_v2_op.py +++ b/test/legacy_test/test_matmul_v2_op.py @@ -19,8 +19,8 @@ from testsuite import create_op import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def reference_matmul(X, Y, transpose_X=False, transpose_Y=False): @@ -494,12 +494,12 @@ def test_check_grad(self): class TestMatMulV2API(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = paddle.static.data( name="input_x", shape=[4, 3], dtype="float32" ) @@ -512,9 +512,9 @@ def check_static_result(self, place): x_np = np.random.random([4, 3]).astype("float32") y_np = np.random.random([3, 4]).astype("float32") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": x_np, "input_y": y_np}, fetch_list=[result], ) @@ -525,7 +525,7 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x = np.random.random([4, 3]).astype("float64") input_y = np.random.random([3, 4]).astype("float64") x = paddle.to_tensor(input_x) @@ -536,7 +536,7 @@ def test_dygraph_fp16(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x = np.random.random([4, 3]).astype("float16") input_y = np.random.random([3, 4]).astype("float16") x = paddle.to_tensor(input_x) @@ -547,7 +547,7 @@ def test_compute_type_fp32(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.set_flags( {'FLAGS_gemm_use_half_precision_compute_type': False} ) @@ -573,7 +573,7 @@ def test_compute_type_fp16_nan(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): paddle.set_flags( {'FLAGS_gemm_use_half_precision_compute_type': True} ) @@ -605,8 +605,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -658,8 +658,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -720,8 +720,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -746,8 +746,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -772,8 +772,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} @@ -798,8 +798,8 @@ def setUp(self): self.init_input_output() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': -1, 'use_mkldnn': False} self.outputs = {'Out': self.out} diff --git a/test/legacy_test/test_matrix_nms_op.py b/test/legacy_test/test_matrix_nms_op.py index 80ac3aa4d1775..9500e6fbf6f06 100644 --- a/test/legacy_test/test_matrix_nms_op.py +++ b/test/legacy_test/test_matrix_nms_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard def python_matrix_nms( diff --git a/test/legacy_test/test_matrix_power_op.py b/test/legacy_test/test_matrix_power_op.py index cc4be16fdfaf9..e9967a7ec3e1c 100644 --- a/test/legacy_test/test_matrix_power_op.py +++ b/test/legacy_test/test_matrix_power_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -243,12 +243,12 @@ def config(self): class TestMatrixPowerAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = paddle.static.data( name="input_x", shape=[4, 4], dtype="float64" ) @@ -256,9 +256,9 @@ def check_static_result(self, place): input_np = np.random.random([4, 4]).astype("float64") result_np = np.linalg.matrix_power(input_np, -2) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": input_np}, fetch_list=[result], ) @@ -272,7 +272,7 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([4, 4]).astype("float64") input = paddle.to_tensor(input_np) result = paddle.linalg.matrix_power(input, -2) @@ -338,12 +338,12 @@ def test_errors(self): class TestMatrixPowerSingularAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float64" ) @@ -351,10 +351,10 @@ def check_static_result(self, place): input_np = np.zeros([4, 4]).astype("float64") - exe = fluid.Executor(place) + exe = base.Executor(place) try: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -371,9 +371,9 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.ones([4, 4]).astype("float64") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) try: result = paddle.linalg.matrix_power(input, -2) except RuntimeError as ex: diff --git a/test/legacy_test/test_matrix_rank_op.py b/test/legacy_test/test_matrix_rank_op.py index 860447cb556b3..1a8bbb5b4636c 100644 --- a/test/legacy_test/test_matrix_rank_op.py +++ b/test/legacy_test/test_matrix_rank_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() SEED = 2049 @@ -167,12 +167,12 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x_np = np.random.rand(3, 4, 7, 7).astype(np.float64) tol_np = np.random.random([3, 4]).astype(np.float32) x_pd = paddle.static.data( @@ -185,41 +185,41 @@ def test_static(self): rank_pd = paddle.linalg.matrix_rank( x_pd, tol_pd, hermitian=False ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"X": x_np, "TolTensor": tol_np}, fetch_list=[rank_pd], ) np.testing.assert_allclose(fetches[0], rank_np, rtol=1e-05) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x_np = np.random.rand(3, 4, 7, 7).astype(np.float64) x_pd = paddle.static.data( name="X", shape=[3, 4, 7, 7], dtype='float64' ) rank_np = np.linalg.matrix_rank(x_np, hermitian=True) rank_pd = paddle.linalg.matrix_rank(x_pd, hermitian=True) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"X": x_np}, fetch_list=[rank_pd], ) np.testing.assert_allclose(fetches[0], rank_np, rtol=1e-05) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x_np = np.random.rand(3, 4, 7, 7).astype(np.float64) x_pd = paddle.static.data( name="X", shape=[3, 4, 7, 7], dtype='float64' ) rank_np = np.linalg.matrix_rank(x_np, 0.1, hermitian=False) rank_pd = paddle.linalg.matrix_rank(x_pd, 0.1, hermitian=False) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"X": x_np}, fetch_list=[rank_pd], ) diff --git a/test/legacy_test/test_max_min_amax_amin_op.py b/test/legacy_test/test_max_min_amax_amin_op.py index 400a0c1eeb776..b5184bd3acd20 100644 --- a/test/legacy_test/test_max_min_amax_amin_op.py +++ b/test/legacy_test/test_max_min_amax_amin_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -28,9 +28,9 @@ def setUp(self): self.init_case() self.cal_np_out_and_gradient() self.place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) def init_case(self): @@ -92,18 +92,18 @@ def _choose_paddle_func(self, func, x): # We check the output between paddle API and numpy in static graph. def test_static_graph(self): def _test_static_graph(func): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(startup_program, train_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(startup_program, train_program): x = paddle.static.data( name='input', dtype=self.dtype, shape=self.shape ) x.stop_gradient = False out = self._choose_paddle_func(func, x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input': self.x_np}, fetch_list=[out], ) diff --git a/test/legacy_test/test_max_op.py b/test/legacy_test/test_max_op.py index 90fe8561b866a..580b8423bffef 100644 --- a/test/legacy_test/test_max_op.py +++ b/test/legacy_test/test_max_op.py @@ -19,7 +19,7 @@ from test_sum_op import TestReduceOPTensorAxisBase import paddle -from paddle.fluid import core +from paddle.base import core class ApiMaxTest(unittest.TestCase): diff --git a/test/legacy_test/test_maximum_op.py b/test/legacy_test/test_maximum_op.py index 544f2c090096b..818bdb65fee68 100644 --- a/test/legacy_test/test_maximum_op.py +++ b/test/legacy_test/test_maximum_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class ApiMaximumTest(unittest.TestCase): diff --git a/test/legacy_test/test_maxout_op.py b/test/legacy_test/test_maxout_op.py index b6d339c3aab28..06b855ce2364a 100644 --- a/test/legacy_test/test_maxout_op.py +++ b/test/legacy_test/test_maxout_op.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(1) diff --git a/test/legacy_test/test_mean_op.py b/test/legacy_test/test_mean_op.py index 5f56926304183..023a8aceddde3 100644 --- a/test/legacy_test/test_mean_op.py +++ b/test/legacy_test/test_mean_op.py @@ -21,8 +21,8 @@ from test_sum_op import TestReduceOPTensorAxisBase import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard np.random.seed(10) @@ -106,7 +106,7 @@ def test_check_output(self): def test_checkout_grad(self): place = core.CUDAPlace(0) if core.is_float16_supported(place): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_np = np.random.random((10, 10)).astype(self.dtype) x = paddle.to_tensor(x_np) x.stop_gradient = False @@ -461,19 +461,19 @@ def test_case(x, axis=None, keepdim=False): test_case(self.x, [0, 1, 2, 3]) paddle.enable_static() - def test_fluid_api(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + def test_base_api(self): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data("x", shape=[10, 10], dtype="float32") out = paddle.mean(x=x, axis=1) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) x_np = np.random.rand(10, 10).astype(np.float32) res = exe.run(feed={"x": x_np}, fetch_list=[out]) np.testing.assert_allclose(res[0], np.mean(x_np, axis=1), rtol=1e-05) - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_np = np.random.rand(10, 10).astype(np.float32) - x = fluid.dygraph.to_variable(x_np) + x = base.dygraph.to_variable(x_np) out = paddle.mean(x=x, axis=1) np.testing.assert_allclose( out.numpy(), np.mean(x_np, axis=1), rtol=1e-05 @@ -537,9 +537,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -568,9 +568,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_memcpy_op.py b/test/legacy_test/test_memcpy_op.py index 8dd8e363af792..768c1bec79c9d 100755 --- a/test/legacy_test/test_memcpy_op.py +++ b/test/legacy_test/test_memcpy_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestMemcpy_FillConstant(unittest.TestCase): @@ -72,8 +72,8 @@ def test_gpu_copy_to_pinned(self): outputs={'Out': pinned_var}, attrs={'dst_place_type': 2}, ) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) gpu_, pinned_ = exe.run( main_program, feed={}, fetch_list=[gpu_var.name, pinned_var.name] ) @@ -88,8 +88,8 @@ def test_pinned_copy_gpu(self): outputs={'Out': gpu_var}, attrs={'dst_place_type': 1}, ) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) gpu_, pinned_ = exe.run( main_program, feed={}, fetch_list=[gpu_var.name, pinned_var.name] ) @@ -144,8 +144,8 @@ def test_hip_copy_bool_value(self): outputs={'Out': gpu_var}, attrs={'dst_place_type': 1}, ) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) gpu_, pinned_ = exe.run( main_program, feed={}, @@ -187,7 +187,7 @@ def test_SELECTED_ROWS(self): name="selected_row_0", dtype="float32", persistable=False, - type=fluid.core.VarDesc.VarType.SELECTED_ROWS, + type=base.core.VarDesc.VarType.SELECTED_ROWS, stop_gradient=True, ) main_program.global_block().append_op( @@ -207,8 +207,8 @@ def test_SELECTED_ROWS(self): outputs={'Out': pinned_var}, attrs={'dst_place_type': 2}, ) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CUDAPlace(0) + exe = base.Executor(place) selected_row_var_, pinned_ = exe.run( main_program, feed={}, diff --git a/test/legacy_test/test_memory_efficient_attention.py b/test/legacy_test/test_memory_efficient_attention.py index c4220fe5b8077..24e1d5115f44f 100644 --- a/test/legacy_test/test_memory_efficient_attention.py +++ b/test/legacy_test/test_memory_efficient_attention.py @@ -23,7 +23,7 @@ import paddle import paddle.incubate.nn.attn_bias as ab import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.nn.memory_efficient_attention import ( memory_efficient_attention, ) diff --git a/test/legacy_test/test_memory_reuse_exclude_feed_var.py b/test/legacy_test/test_memory_reuse_exclude_feed_var.py index 205369bc820c5..de0f08efc99e0 100644 --- a/test/legacy_test/test_memory_reuse_exclude_feed_var.py +++ b/test/legacy_test/test_memory_reuse_exclude_feed_var.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TestMemoryReuseExcludeFeedVar(unittest.TestCase): @@ -33,18 +33,18 @@ def main_impl(self, place): relu_image = F.relu(image) loss = paddle.mean(relu_image) - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.enable_inplace = True build_strategy.memory_optimize = True - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) - compiled_prog = fluid.CompiledProgram( - fluid.default_main_program(), build_strategy=build_strategy + compiled_prog = base.CompiledProgram( + base.default_main_program(), build_strategy=build_strategy ) - image_tensor = fluid.LoDTensor() + image_tensor = base.LoDTensor() np_image = np.random.uniform( low=-10, high=10, size=self.image_shape ).astype('float32') @@ -57,14 +57,14 @@ def main_impl(self, place): np.testing.assert_array_equal(np.array(image_tensor), np_image) def test_main(self): - places = [fluid.CPUPlace()] - if fluid.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for p in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.unique_name.guard(): - with fluid.scope_guard(fluid.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.unique_name.guard(): + with base.scope_guard(base.Scope()): self.main_impl(p) diff --git a/test/legacy_test/test_merge_selectedrows_op.py b/test/legacy_test/test_merge_selectedrows_op.py index 7c7f3a86da88c..023fadf927523 100644 --- a/test/legacy_test/test_merge_selectedrows_op.py +++ b/test/legacy_test/test_merge_selectedrows_op.py @@ -17,7 +17,7 @@ import numpy as np from op import Operator -from paddle.fluid import core +from paddle.base import core class TestMergeSelectedRows(unittest.TestCase): diff --git a/test/legacy_test/test_merged_adam_op.py b/test/legacy_test/test_merged_adam_op.py index 6ff2cb90bc039..9f254a89d6ee8 100644 --- a/test/legacy_test/test_merged_adam_op.py +++ b/test/legacy_test/test_merged_adam_op.py @@ -18,7 +18,7 @@ import paddle from paddle import _C_ops, _legacy_C_ops -from paddle.fluid.framework import in_dygraph_mode +from paddle.base.framework import in_dygraph_mode def run_adam_op( @@ -47,15 +47,15 @@ def run_adam_op( paddle.disable_static() paddle.set_device(place) - param_vars = [paddle.fluid.dygraph.to_variable(p) for p in params] - grad_vars = [paddle.fluid.dygraph.to_variable(g) for g in grads] - lr_vars = [paddle.fluid.dygraph.to_variable(l) for l in lrs] - moment1_vars = [paddle.fluid.dygraph.to_variable(m) for m in moment1s] - moment2_vars = [paddle.fluid.dygraph.to_variable(m) for m in moment2s] - beta1_pow_vars = [paddle.fluid.dygraph.to_variable(b) for b in beta1_pows] - beta2_pow_vars = [paddle.fluid.dygraph.to_variable(b) for b in beta2_pows] + param_vars = [paddle.base.dygraph.to_variable(p) for p in params] + grad_vars = [paddle.base.dygraph.to_variable(g) for g in grads] + lr_vars = [paddle.base.dygraph.to_variable(l) for l in lrs] + moment1_vars = [paddle.base.dygraph.to_variable(m) for m in moment1s] + moment2_vars = [paddle.base.dygraph.to_variable(m) for m in moment2s] + beta1_pow_vars = [paddle.base.dygraph.to_variable(b) for b in beta1_pows] + beta2_pow_vars = [paddle.base.dygraph.to_variable(b) for b in beta2_pows] master_param_vars = [ - paddle.fluid.dygraph.to_variable(m_p) for m_p in master_params + paddle.base.dygraph.to_variable(m_p) for m_p in master_params ] if not use_merged: diff --git a/test/legacy_test/test_merged_momentum_op.py b/test/legacy_test/test_merged_momentum_op.py index 08d200a911f3c..289c86fef3b4e 100644 --- a/test/legacy_test/test_merged_momentum_op.py +++ b/test/legacy_test/test_merged_momentum_op.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper def run_momentum_op( diff --git a/test/legacy_test/test_meshgrid_op.py b/test/legacy_test/test_meshgrid_op.py index d2f7b0c2eca89..97c896e7cb8da 100644 --- a/test/legacy_test/test_meshgrid_op.py +++ b/test/legacy_test/test_meshgrid_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def meshgrid_wrapper(x): @@ -155,10 +155,10 @@ def test_api(self): out_2 = np.reshape(input_2, [1, 200]) out_2 = np.broadcast_to(out_2, [100, 200]) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) grid_x, grid_y = paddle.tensor.meshgrid(x, y) res_1, res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': input_1, 'y': input_2}, fetch_list=[grid_x, grid_y], ) @@ -191,10 +191,10 @@ def test_list_input(self): out_2 = np.reshape(input_2, [1, 200]) out_2 = np.broadcast_to(out_2, [100, 200]) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) grid_x, grid_y = paddle.tensor.meshgrid([x, y]) res_1, res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': input_1, 'y': input_2}, fetch_list=[grid_x, grid_y], ) @@ -228,10 +228,10 @@ def test_tuple_input(self): out_2 = np.reshape(input_2, [1, 200]) out_2 = np.broadcast_to(out_2, [100, 200]) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) grid_x, grid_y = paddle.tensor.meshgrid((x, y)) res_1, res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': input_1, 'y': input_2}, fetch_list=[grid_x, grid_y], ) @@ -257,9 +257,9 @@ def test_api_with_dygraph(self): ], ).astype('int32') - with fluid.dygraph.guard(): - tensor_3 = fluid.dygraph.to_variable(input_3) - tensor_4 = fluid.dygraph.to_variable(input_4) + with base.dygraph.guard(): + tensor_3 = base.dygraph.to_variable(input_3) + tensor_4 = base.dygraph.to_variable(input_4) res_3, res_4 = paddle.tensor.meshgrid(tensor_3, tensor_4) np.testing.assert_array_equal(res_3.shape, [100, 200]) @@ -283,9 +283,9 @@ def test_api_with_dygraph_list_input(self): ], ).astype('int32') - with fluid.dygraph.guard(): - tensor_3 = fluid.dygraph.to_variable(input_3) - tensor_4 = fluid.dygraph.to_variable(input_4) + with base.dygraph.guard(): + tensor_3 = base.dygraph.to_variable(input_3) + tensor_4 = base.dygraph.to_variable(input_4) res_3, res_4 = paddle.tensor.meshgrid([tensor_3, tensor_4]) np.testing.assert_array_equal(res_3.shape, [100, 200]) @@ -309,9 +309,9 @@ def test_api_with_dygraph_tuple_input(self): ], ).astype('int32') - with fluid.dygraph.guard(): - tensor_3 = fluid.dygraph.to_variable(input_3) - tensor_4 = fluid.dygraph.to_variable(input_4) + with base.dygraph.guard(): + tensor_3 = base.dygraph.to_variable(input_3) + tensor_4 = base.dygraph.to_variable(input_4) res_3, res_4 = paddle.tensor.meshgrid((tensor_3, tensor_4)) np.testing.assert_array_equal(res_3.shape, [100, 200]) @@ -360,16 +360,16 @@ def test_dygraph_api(self): ], ).astype('int32') - with fluid.dygraph.guard(): - tensor_1 = fluid.dygraph.to_variable(input_1) - tensor_2 = fluid.dygraph.to_variable(input_2) + with base.dygraph.guard(): + tensor_1 = base.dygraph.to_variable(input_1) + tensor_2 = base.dygraph.to_variable(input_2) tensor_1.stop_gradient = False tensor_2.stop_gradient = False res_1, res_2 = paddle.tensor.meshgrid((tensor_1, tensor_2)) sum = paddle.add_n([res_1, res_2]) sum.backward() - tensor_eager_1 = fluid.dygraph.to_variable(input_1) - tensor_eager_2 = fluid.dygraph.to_variable(input_2) + tensor_eager_1 = base.dygraph.to_variable(input_1) + tensor_eager_2 = base.dygraph.to_variable(input_2) tensor_eager_1.stop_gradient = False tensor_eager_2.stop_gradient = False res_eager_1, res_eager_2 = paddle.tensor.meshgrid( diff --git a/test/legacy_test/test_metrics.py b/test/legacy_test/test_metrics.py index eb7af9365af3b..805ac0bad28b0 100644 --- a/test/legacy_test/test_metrics.py +++ b/test/legacy_test/test_metrics.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.hapi.model import to_list @@ -158,7 +158,7 @@ def random_pred_label(self): return label, pred_one_hot def test_main(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): acc = paddle.metric.Accuracy(topk=self.topk, name=self.name) for _ in range(10): label, pred = self.random_pred_label() @@ -200,11 +200,11 @@ def setUp(self): def test_main(self): paddle.enable_static() - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() main_prog.random_seed = 1024 startup_prog.random_seed = 1024 - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): pred = paddle.static.data( name='pred', shape=[None, self.class_num], dtype='float32' ) @@ -214,8 +214,8 @@ def test_main(self): acc = paddle.metric.Accuracy(topk=self.topk, name=self.name) state = acc.compute(pred, label) - exe = fluid.Executor(fluid.CPUPlace()) - compiled_main_prog = fluid.CompiledProgram(main_prog) + exe = base.Executor(base.CPUPlace()) + compiled_main_prog = base.CompiledProgram(main_prog) for _ in range(10): label, pred = self.random_pred_label() diff --git a/test/legacy_test/test_min_op.py b/test/legacy_test/test_min_op.py index 5495f44f12dee..4d51ce3fbbf28 100644 --- a/test/legacy_test/test_min_op.py +++ b/test/legacy_test/test_min_op.py @@ -19,8 +19,8 @@ from test_sum_op import TestReduceOPTensorAxisBase import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class ApiMinTest(unittest.TestCase): @@ -120,7 +120,7 @@ def init_data(self): class TestMinAPIWithEmptyTensor(unittest.TestCase): def test_empty_tensor(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): with self.assertRaises(ValueError): data = np.array([], dtype=np.float32) data = np.reshape(data, [0, 0, 0, 0, 0, 0, 0]) diff --git a/test/legacy_test/test_minimum_op.py b/test/legacy_test/test_minimum_op.py index f1de0f0f10d85..6267b78b4cf9d 100644 --- a/test/legacy_test/test_minimum_op.py +++ b/test/legacy_test/test_minimum_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class ApiMinimumTest(unittest.TestCase): diff --git a/test/legacy_test/test_mix_precision_all_reduce_fuse.py b/test/legacy_test/test_mix_precision_all_reduce_fuse.py index ef81e5cc3bf71..6887b2d0de631 100644 --- a/test/legacy_test/test_mix_precision_all_reduce_fuse.py +++ b/test/legacy_test/test_mix_precision_all_reduce_fuse.py @@ -20,7 +20,7 @@ from simple_nets import init_data import paddle -from paddle.fluid import core +from paddle.base import core batch_size = 12 img_shape = [1, 28, 28] diff --git a/test/legacy_test/test_mode_op.py b/test/legacy_test/test_mode_op.py index 15c376f57abb6..63e67882e23e1 100644 --- a/test/legacy_test/test_mode_op.py +++ b/test/legacy_test/test_mode_op.py @@ -22,8 +22,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def _mode1D(a): @@ -226,7 +226,7 @@ def test_gpu_kernel(): paddle.disable_static() test_cpu_kernel() - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): test_gpu_kernel() @@ -267,7 +267,7 @@ def test_run_static(self): class TestModeZeroError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_model.py b/test/legacy_test/test_model.py index 35649cce6bc54..683b002e05815 100644 --- a/test/legacy_test/test_model.py +++ b/test/legacy_test/test_model.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle import Model, fluid, jit, to_tensor +from paddle import Model, base, jit, to_tensor from paddle.hapi.model import prepare_distributed_context from paddle.io import Dataset, DistributedBatchSampler from paddle.metric import Accuracy @@ -163,7 +163,7 @@ def dynamic_train(model, dataloader): def dynamic_evaluate(model, dataloader): - with fluid.dygraph.no_grad(): + with base.dygraph.no_grad(): model.eval() cnt = 0 for inputs, labels in dataloader: @@ -182,15 +182,15 @@ def dynamic_evaluate(model, dataloader): @unittest.skipIf( - not fluid.is_compiled_with_cuda(), 'CPU testing is not supported' + not base.is_compiled_with_cuda(), 'CPU testing is not supported' ) class TestModel(unittest.TestCase): @classmethod def setUpClass(cls): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): cls().skipTest('module not tested when ONLY_CPU compling') cls.device = paddle.set_device('gpu') - fluid.enable_dygraph(cls.device) + base.enable_dygraph(cls.device) sp_num = 1280 cls.train_dataset = MnistDataset(mode='train', sample_num=sp_num) @@ -228,7 +228,7 @@ def setUpClass(cls): cls.weight_path = os.path.join(cls.save_dir, 'lenet') paddle.save(dy_lenet.state_dict(), cls.weight_path + '.pdparams') - fluid.disable_dygraph() + base.disable_dygraph() @classmethod def tearDownClass(cls): @@ -274,7 +274,7 @@ def test_prepare_context(self): prepare_distributed_context() def fit(self, dynamic, num_replicas=None, rank=None, num_iters=None): - fluid.enable_dygraph(self.device) if dynamic else None + base.enable_dygraph(self.device) if dynamic else None seed = 555 paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -337,10 +337,10 @@ def fit(self, dynamic, num_replicas=None, rank=None, num_iters=None): ) model.fit(train_loader, val_loader) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def fit_with_tuple_input(self, dynamic, num_replicas=None, rank=None): - fluid.enable_dygraph(self.device) if dynamic else None + base.enable_dygraph(self.device) if dynamic else None seed = 555 paddle.seed(seed) paddle.framework.random._manual_program_seed(seed) @@ -390,10 +390,10 @@ def fit_with_tuple_input(self, dynamic, num_replicas=None, rank=None): ) model.fit(train_loader, val_loader) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def evaluate(self, dynamic): - fluid.enable_dygraph(self.device) if dynamic else None + base.enable_dygraph(self.device) if dynamic else None model = Model(LeNet(), self.inputs, self.labels) model.prepare(metrics=Accuracy()) model.load(self.weight_path) @@ -413,10 +413,10 @@ def evaluate(self, dynamic): model.evaluate(val_loader) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def predict(self, dynamic): - fluid.enable_dygraph(self.device) if dynamic else None + base.enable_dygraph(self.device) if dynamic else None model = Model(LeNet(), self.inputs) model.prepare() model.load(self.weight_path) @@ -441,10 +441,10 @@ def predict(self, dynamic): model.evaluate(test_loader) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def test_predict_without_inputs(self): - fluid.enable_dygraph(self.device) + base.enable_dygraph(self.device) model = Model(LeNet()) model.prepare() model.load(self.weight_path) @@ -453,7 +453,7 @@ def test_predict_without_inputs(self): self.test_dataset, batch_size=64, stack_outputs=True ) np.testing.assert_equal(output[0].shape[0], len(self.test_dataset)) - fluid.disable_dygraph() + base.disable_dygraph() def test_summary_gpu(self): paddle.disable_static(self.device) @@ -494,7 +494,7 @@ def test_train_batch(self, dynamic=True): label = np.random.randint(0, 10, size=(4, 1)).astype(np.int64) def get_expect(): - fluid.enable_dygraph(fluid.CPUPlace()) + base.enable_dygraph(base.CPUPlace()) self.set_seed() m = MyModel() optim = paddle.optimizer.SGD( @@ -507,13 +507,13 @@ def get_expect(): avg_loss.backward() optim.minimize(avg_loss) m.clear_gradients() - fluid.disable_dygraph() + base.disable_dygraph() return avg_loss.numpy() ref = get_expect() for dynamic in [True, False]: device = paddle.set_device('cpu') - fluid.enable_dygraph(device) if dynamic else None + base.enable_dygraph(device) if dynamic else None self.set_seed() net = MyModel() @@ -527,25 +527,25 @@ def get_expect(): model.prepare(optim2, loss=CrossEntropyLoss(reduction="sum")) (loss,) = model.train_batch([data], [label]) np.testing.assert_allclose(loss.flatten(), ref.flatten()) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def test_test_batch(self): dim = 20 data = np.random.random(size=(4, dim)).astype(np.float32) def get_expect(): - fluid.enable_dygraph(fluid.CPUPlace()) + base.enable_dygraph(base.CPUPlace()) self.set_seed() m = MyModel() m.eval() output = m(to_tensor(data)) - fluid.disable_dygraph() + base.disable_dygraph() return output.numpy() ref = get_expect() for dynamic in [True, False]: device = paddle.set_device('cpu') - fluid.enable_dygraph(device) if dynamic else None + base.enable_dygraph(device) if dynamic else None self.set_seed() net = MyModel() inputs = [InputSpec([None, dim], 'float32', 'x')] @@ -554,7 +554,7 @@ def get_expect(): (out,) = model.predict_batch([data]) np.testing.assert_allclose(out, ref, rtol=1e-6) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def test_save_load(self): path = os.path.join(tempfile.mkdtemp(), '.cache_test_save_load') @@ -562,7 +562,7 @@ def test_save_load(self): os.makedirs(path) for dynamic in [True, False]: device = paddle.set_device('cpu') - fluid.enable_dygraph(device) if dynamic else None + base.enable_dygraph(device) if dynamic else None net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] labels = [InputSpec([None, 1], 'int64', 'label')] @@ -575,7 +575,7 @@ def test_save_load(self): ) model.save(path) model.load(path) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None shutil.rmtree(path) def test_dynamic_load(self): @@ -616,14 +616,14 @@ def test_dynamic_save_static_load(self): os.makedirs(path) # dynamic saving device = paddle.set_device('cpu') - fluid.enable_dygraph(device) + base.enable_dygraph(device) model = Model(MyModel()) optim = paddle.optimizer.SGD( learning_rate=0.001, parameters=model.parameters() ) model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum")) model.save(path) - fluid.disable_dygraph() + base.disable_dygraph() inputs = [InputSpec([None, 20], 'float32', 'x')] labels = [InputSpec([None, 1], 'int64', 'label')] @@ -652,7 +652,7 @@ def test_static_save_dynamic_load(self): model.save(path) device = paddle.set_device('cpu') - fluid.enable_dygraph(device) # if dynamic else None + base.enable_dygraph(device) # if dynamic else None net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] @@ -664,12 +664,12 @@ def test_static_save_dynamic_load(self): model.prepare(optimizer=optim, loss=CrossEntropyLoss(reduction="sum")) model.load(path) shutil.rmtree(path) - fluid.disable_dygraph() + base.disable_dygraph() def test_parameters(self): for dynamic in [True, False]: device = paddle.set_device('cpu') - fluid.enable_dygraph(device) if dynamic else None + base.enable_dygraph(device) if dynamic else None net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] model = Model(net, inputs) @@ -677,7 +677,7 @@ def test_parameters(self): params = model.parameters() self.assertTrue(params[0].shape[0] == 20) self.assertTrue(params[0].shape[1] == 10) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None def test_summary(self): def _get_param_from_state_dict(state_dict): @@ -688,7 +688,7 @@ def _get_param_from_state_dict(state_dict): for dynamic in [True, False]: device = paddle.set_device('cpu') - fluid.enable_dygraph(device) if dynamic else None + base.enable_dygraph(device) if dynamic else None net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] model = Model(net, inputs) @@ -837,16 +837,16 @@ def test_export_deploy_model(self): model.save(save_dir, training=False) ori_results = model.predict_batch(tensor_img) - fluid.disable_dygraph() if dynamic else None + base.disable_dygraph() if dynamic else None place = ( - fluid.CPUPlace() - if not fluid.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not base.is_compiled_with_cuda() + else base.CUDAPlace(0) ) - new_scope = fluid.Scope() - with fluid.scope_guard(new_scope): - exe = fluid.Executor(place) + new_scope = base.Scope() + with base.scope_guard(new_scope): + exe = base.Executor(place) [ inference_program, feed_target_names, @@ -984,7 +984,7 @@ def make_optimizer(parameters=None): # dynamic test device = paddle.set_device('cpu') - fluid.enable_dygraph(device) + base.enable_dygraph(device) net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] labels = [InputSpec([None, 1], 'int64', 'label')] @@ -1047,7 +1047,7 @@ def make_optimizer(parameters=None): # dynamic test device = paddle.set_device('cpu') - fluid.enable_dygraph(device) + base.enable_dygraph(device) net = MyModel() inputs = [InputSpec([None, 20], 'float32', 'x')] labels = [InputSpec([None, 1], 'int64', 'label')] diff --git a/test/legacy_test/test_modelaverage.py b/test/legacy_test/test_modelaverage.py index 920167906f70d..29d192eeebad9 100644 --- a/test/legacy_test/test_modelaverage.py +++ b/test/legacy_test/test_modelaverage.py @@ -17,20 +17,20 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn class TestModelAverage(unittest.TestCase): def test_model_average_static(self): paddle.enable_static() - place = fluid.CPUPlace() + place = base.CPUPlace() shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_program = fluid.Program() - startup = fluid.Program() - test_program = fluid.Program() - with fluid.program_guard(train_program, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_program = base.Program() + startup = base.Program() + test_program = base.Program() + with base.program_guard(train_program, startup): + with base.unique_name.guard(): data = paddle.static.data( name='X', shape=[None, 1], dtype='float32' ) diff --git a/test/legacy_test/test_momentum_op.py b/test/legacy_test/test_momentum_op.py index b23183996c025..67296e516ab78 100644 --- a/test/legacy_test/test_momentum_op.py +++ b/test/legacy_test/test_momentum_op.py @@ -20,8 +20,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def calculate_momentum_by_numpy( @@ -249,7 +249,7 @@ def setUp(self): def test_check_output(self): paddle.enable_static() if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) if core.is_float16_supported(place): self.check_output_with_place(place, check_dygraph=False) @@ -528,7 +528,7 @@ def init_args(self): def test_sparse_momentum(self): if core.is_compiled_with_cuda(): - self.check_with_place(fluid.CUDAPlace(0)) + self.check_with_place(base.CUDAPlace(0)) class TestSparseMomentumOpWithMultiPrecision2( @@ -555,9 +555,9 @@ def test_momentum_dygraph(self): def test_momentum(self): paddle.enable_static() - place = fluid.CPUPlace() - main = fluid.Program() - with fluid.program_guard(main): + place = base.CPUPlace() + main = base.Program() + with base.program_guard(main): x = paddle.static.data(name='x', shape=[-1, 13], dtype='float32') y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') y_predict = paddle.static.nn.fc(x, size=1, activation=None) @@ -575,9 +575,9 @@ def test_momentum(self): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) @@ -692,9 +692,9 @@ def test_momentum_dygraph_1(self): def test_momentum_static(self): paddle.enable_static() - place = fluid.CPUPlace() - main = fluid.Program() - with fluid.program_guard(main): + place = base.CPUPlace() + main = base.Program() + with base.program_guard(main): x = paddle.static.data(name='x', shape=[-1, 13], dtype='float32') y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') y_predict = paddle.static.nn.fc(x, size=1, activation=None) @@ -712,9 +712,9 @@ def test_momentum_static(self): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) @@ -810,7 +810,7 @@ def __update_params(self, momentum, linear): momentum.minimize(loss) linear.clear_gradients() - def __test_vs(self, place=fluid.CPUPlace()): + def __test_vs(self, place=base.CPUPlace()): paddle.disable_static(place=place) linear_old = paddle.nn.Linear( @@ -847,10 +847,10 @@ def __test_vs(self, place=fluid.CPUPlace()): 'the param weight updated by two Momentum optimizers should equal', ) - def test_vs(self, place=fluid.CPUPlace()): - places = [fluid.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + def test_vs(self, place=base.CPUPlace()): + places = [base.CPUPlace()] + if paddle.base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for place in places: self.__test_vs(place=place) diff --git a/test/legacy_test/test_monitor.py b/test/legacy_test/test_monitor.py index 37fb9cb898a63..8f856c2ea1aae 100644 --- a/test/legacy_test/test_monitor.py +++ b/test/legacy_test/test_monitor.py @@ -23,8 +23,8 @@ import tempfile import unittest -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestDatasetWithStat(unittest.TestCase): @@ -76,21 +76,21 @@ def test_dataset_run_with_stat(self): dataset._set_fea_eval(1, True) dataset.slots_shuffle(["slot1"]) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) if self.use_data_loader: - data_loader = fluid.io.DataLoader.from_dataset( - dataset, fluid.cpu_places(), self.drop_last + data_loader = base.io.DataLoader.from_dataset( + dataset, base.cpu_places(), self.drop_last ) for i in range(self.epoch_num): for data in data_loader(): - exe.run(fluid.default_main_program(), feed=data) + exe.run(base.default_main_program(), feed=data) else: for i in range(self.epoch_num): try: exe.train_from_dataset( - fluid.default_main_program(), + base.default_main_program(), dataset, fetch_list=[embs[0], embs[1]], fetch_info=["emb0", "emb1"], diff --git a/test/legacy_test/test_mse_loss.py b/test/legacy_test/test_mse_loss.py index 37fb0a6a95883..688895240a374 100644 --- a/test/legacy_test/test_mse_loss.py +++ b/test/legacy_test/test_mse_loss.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import core +from paddle.base.executor import Executor class TestMseLoss(unittest.TestCase): @@ -41,10 +41,10 @@ def test_mse_loss(self): for use_cuda in ( [False, True] if core.is_compiled_with_cuda() else [False] ): - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() exe = Executor(place) (result,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_val, "label": label_val}, fetch_list=[output], ) @@ -79,14 +79,14 @@ def test_NNMseLoss_mean(self): input_np = np.random.uniform(0.1, 0.5, dim).astype("float32") label_np = np.random.uniform(0.1, 0.5, dim).astype("float32") paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[-1] + dim, dtype='float32' ) @@ -98,18 +98,18 @@ def test_NNMseLoss_mean(self): mse_loss = paddle.nn.loss.MSELoss() ret = mse_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[ret], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): mse_loss = paddle.nn.loss.MSELoss() dy_ret = mse_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_result = dy_ret.numpy() @@ -125,14 +125,14 @@ def test_NNMseLoss_sum(self): input_np = np.random.uniform(0.1, 0.5, dim).astype("float32") label_np = np.random.uniform(0.1, 0.5, dim).astype("float32") paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[-1] + dim, dtype='float32' ) @@ -144,18 +144,18 @@ def test_NNMseLoss_sum(self): mse_loss = paddle.nn.loss.MSELoss(reduction='sum') ret = mse_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[ret], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): mse_loss = paddle.nn.loss.MSELoss(reduction='sum') dy_ret = mse_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_result = dy_ret.numpy() @@ -171,14 +171,14 @@ def test_NNMseLoss_none(self): input_np = np.random.uniform(0.1, 0.5, dim).astype("float32") label_np = np.random.uniform(0.1, 0.5, dim).astype("float32") paddle.enable_static() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[-1] + dim, dtype='float32' ) @@ -190,18 +190,18 @@ def test_NNMseLoss_none(self): mse_loss = paddle.nn.loss.MSELoss(reduction='none') ret = mse_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[ret], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): mse_loss = paddle.nn.loss.MSELoss(reduction='none') dy_ret = mse_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_result = dy_ret.numpy() diff --git a/test/legacy_test/test_mul_nn_grad.py b/test/legacy_test/test_mul_nn_grad.py index 10ea654c84305..eb30a0e1a5068 100644 --- a/test/legacy_test/test_mul_nn_grad.py +++ b/test/legacy_test/test_mul_nn_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -57,9 +57,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_mul_op.py b/test/legacy_test/test_mul_op.py index 9ce95d354d7af..d1b5d5f02843b 100644 --- a/test/legacy_test/test_mul_op.py +++ b/test/legacy_test/test_mul_op.py @@ -17,7 +17,7 @@ import numpy as np -from paddle.fluid import core +from paddle.base import core sys.path.append("..") from eager_op_test import OpTest, convert_float_to_uint16 diff --git a/test/legacy_test/test_multi_dot_op.py b/test/legacy_test/test_multi_dot_op.py index dc6f5c4810cf9..8a3557b6d7f14 100644 --- a/test/legacy_test/test_multi_dot_op.py +++ b/test/legacy_test/test_multi_dot_op.py @@ -19,7 +19,7 @@ from numpy.linalg import multi_dot import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_multi_label_soft_margin_loss.py b/test/legacy_test/test_multi_label_soft_margin_loss.py index af8481df1719c..c2e68191ffc36 100644 --- a/test/legacy_test/test_multi_label_soft_margin_loss.py +++ b/test/legacy_test/test_multi_label_soft_margin_loss.py @@ -96,7 +96,7 @@ def test_static( def test_dygraph( place, input_np, label_np, weight=None, reduction='mean', functional=False ): - with paddle.fluid.dygraph.base.guard(): + with paddle.base.dygraph.base.guard(): input = paddle.to_tensor(input_np) label = paddle.to_tensor(label_np) if weight is not None: diff --git a/test/legacy_test/test_multiclass_nms_op.py b/test/legacy_test/test_multiclass_nms_op.py index 6878513ce8e2b..2059eff7d738b 100644 --- a/test/legacy_test/test_multiclass_nms_op.py +++ b/test/legacy_test/test_multiclass_nms_op.py @@ -20,8 +20,8 @@ import paddle from paddle import _C_ops -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core +from paddle.base.layer_helper import LayerHelper def multiclass_nms3( diff --git a/test/legacy_test/test_multinomial_op.py b/test/legacy_test/test_multinomial_op.py index 882437f73fa68..de58d6e8dd903 100644 --- a/test/legacy_test/test_multinomial_op.py +++ b/test/legacy_test/test_multinomial_op.py @@ -20,8 +20,8 @@ from test_attribute_var import UnittestBase import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def sample_output_one_dimension(out, dim): @@ -306,16 +306,16 @@ def test_dygraph4(self): def test_static(self): paddle.enable_static() - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): x = paddle.static.data('x', shape=[4], dtype='float32') out = paddle.multinomial(x, num_samples=100000, replacement=True) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) x_np = np.random.rand(4).astype('float32') diff --git a/test/legacy_test/test_multiplex_op.py b/test/legacy_test/test_multiplex_op.py index 64886a232f68f..c860825c66e40 100644 --- a/test/legacy_test/test_multiplex_op.py +++ b/test/legacy_test/test_multiplex_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base class TestMultiplexOp(OpTest): @@ -62,7 +62,7 @@ def test_check_grad_ignore_x3(self): class TestMultiplexOpError(unittest.TestCase): def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x1 = paddle.static.data(name='x1', shape=[None, 2], dtype='int64') x2 = paddle.static.data(name='x2', shape=[None, 2], dtype='int64') index = paddle.static.data( @@ -111,7 +111,7 @@ def test_multiplex_dygraph(self): paddle.enable_static() def test_dygraph_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): img1 = np.array([[1, 2], [3, 4]]).astype(np.float32) img2 = np.array([[5, 6], [7, 8]]).astype(np.float32) inputs = [paddle.to_tensor(img1), paddle.to_tensor(img2)] diff --git a/test/legacy_test/test_multiprocess_dataloader_dataset.py b/test/legacy_test/test_multiprocess_dataloader_dataset.py index 7eb99ef7006e7..d10d51d6a0241 100755 --- a/test/legacy_test/test_multiprocess_dataloader_dataset.py +++ b/test/legacy_test/test_multiprocess_dataloader_dataset.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.io import ( ChainDataset, ComposeDataset, @@ -61,7 +61,7 @@ def run_main(self, num_workers, places): paddle.static.default_startup_program().random_seed = 1 paddle.static.default_main_program().random_seed = 1 place = paddle.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([16, 3, 4]).astype('float32') input = paddle.to_tensor(input_np) label_np = np.random.random([16, 1]).astype('int32') @@ -82,8 +82,8 @@ def run_main(self, num_workers, places): assert len(label) == 1 assert input.shape == [1, 3, 4] assert label.shape == [1, 1] - assert isinstance(input, fluid.core.eager.Tensor) - assert isinstance(label, fluid.core.eager.Tensor) + assert isinstance(input, base.core.eager.Tensor) + assert isinstance(label, base.core.eager.Tensor) np.testing.assert_allclose(input.numpy(), input_np[i]) np.testing.assert_allclose(label.numpy(), label_np[i]) @@ -180,8 +180,8 @@ def assert_basic(input, label): assert len(label) == 1 assert input.shape == [1, 3, 4] assert label.shape == [1, 1] - assert isinstance(input, fluid.core.eager.Tensor) - assert isinstance(label, fluid.core.eager.Tensor) + assert isinstance(input, base.core.eager.Tensor) + assert isinstance(label, base.core.eager.Tensor) elements_list = [] for _, (input, label) in enumerate(dataloader()): @@ -261,7 +261,7 @@ def run_main(self, num_workers, places): paddle.static.default_startup_program().random_seed = 1 paddle.static.default_main_program().random_seed = 1 place = paddle.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): dataset = NumpyMixTensorDataset(16) assert len(dataset) == 16 dataloader = DataLoader( @@ -277,8 +277,8 @@ def run_main(self, num_workers, places): assert len(label) == 1 assert input.shape == [1, IMAGE_SIZE] assert label.shape == [1, 1] - assert isinstance(input, fluid.core.eager.Tensor) - assert isinstance(label, fluid.core.eager.Tensor) + assert isinstance(input, base.core.eager.Tensor) + assert isinstance(label, base.core.eager.Tensor) class ComplextDataset(Dataset): @@ -306,7 +306,7 @@ def run_main(self, num_workers): paddle.static.default_startup_program().random_seed = 1 paddle.static.default_main_program().random_seed = 1 place = paddle.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): dataset = ComplextDataset(16) assert len(dataset) == 16 dataloader = DataLoader( @@ -362,7 +362,7 @@ def run_main(self, num_workers): paddle.static.default_startup_program().random_seed = 1 paddle.static.default_main_program().random_seed = 1 place = paddle.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): self.init_dataset() dataloader = DataLoader( self.dataset, @@ -373,7 +373,7 @@ def run_main(self, num_workers): ) for i, data in enumerate(dataloader()): - assert isinstance(data, fluid.core.eager.Tensor) + assert isinstance(data, base.core.eager.Tensor) assert data.shape == [2, 2, 3] def test_main(self): diff --git a/test/legacy_test/test_multiprocess_dataloader_dynamic.py b/test/legacy_test/test_multiprocess_dataloader_dynamic.py index debc1cbf20630..c60d92153e530 100644 --- a/test/legacy_test/test_multiprocess_dataloader_dynamic.py +++ b/test/legacy_test/test_multiprocess_dataloader_dynamic.py @@ -29,7 +29,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.io import DataLoader from paddle.nn import Linear @@ -76,9 +76,9 @@ def forward(self, image): class TestDygraphDataLoader(unittest.TestCase): def run_main(self, num_workers, places, persistent_workers): - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 - with fluid.dygraph.guard(places[0]): + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 + with base.dygraph.guard(places[0]): fc_net = SimpleFCNet() optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters()) @@ -147,9 +147,9 @@ def test_main(self): class TestDygraphDataLoaderWithBatchedDataset(TestDygraphDataLoader): def run_main(self, num_workers, places, persistent_workers): - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 - with fluid.dygraph.guard(places[0]): + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 + with base.dygraph.guard(places[0]): fc_net = SimpleFCNet() optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters()) diff --git a/test/legacy_test/test_multiprocess_dataloader_exception.py b/test/legacy_test/test_multiprocess_dataloader_exception.py index bfd08f703c4f6..398e3bf4b99be 100644 --- a/test/legacy_test/test_multiprocess_dataloader_exception.py +++ b/test/legacy_test/test_multiprocess_dataloader_exception.py @@ -17,8 +17,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.io import BatchSampler, DataLoader, Dataset, IterableDataset from paddle.io.dataloader.worker import _worker_loop @@ -39,8 +39,8 @@ def __len__(self): class TestDataLoaderAssert(unittest.TestCase): def test_main(self): - place = fluid.cpu_places()[0] - with fluid.dygraph.guard(place): + place = base.cpu_places()[0] + with base.dygraph.guard(place): dataset = RandomDataset(100) batch_sampler = BatchSampler(dataset=dataset, batch_size=4) @@ -147,8 +147,8 @@ def test_main(self): class TestDataLoaderWorkerLoop(unittest.TestCase): def run_without_worker_done(self, use_shared_memory=True): try: - place = fluid.cpu_places()[0] - with fluid.dygraph.guard(place): + place = base.cpu_places()[0] + with base.dygraph.guard(place): dataset = RandomDataset(800) # test init_fn @@ -204,8 +204,8 @@ def _collate_fn(sample_list): def run_with_worker_done(self, use_shared_memory=True): try: - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): dataset = RandomDataset(800) # test init_fn diff --git a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_dynamic.py b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_dynamic.py index 5ee06b58674c3..164a6531d3ecc 100644 --- a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_dynamic.py +++ b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_dynamic.py @@ -29,7 +29,7 @@ ) import paddle -from paddle import fluid +from paddle import base from paddle.io import DataLoader from paddle.nn import Linear @@ -77,9 +77,9 @@ def forward(self, image): class TestDygraphDataLoader(unittest.TestCase): def run_main(self, num_workers, places, persistent_workers): - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 - with fluid.dygraph.guard(places[0]): + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 + with base.dygraph.guard(places[0]): fc_net = SimpleFCNet() optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters()) @@ -146,9 +146,9 @@ def test_main(self): class TestDygraphDataLoaderWithBatchedDataset(TestDygraphDataLoader): def run_main(self, num_workers, places, persistent_workers): - fluid.default_startup_program().random_seed = 1 - fluid.default_main_program().random_seed = 1 - with fluid.dygraph.guard(places[0]): + base.default_startup_program().random_seed = 1 + base.default_main_program().random_seed = 1 + with base.dygraph.guard(places[0]): fc_net = SimpleFCNet() optimizer = paddle.optimizer.Adam(parameters=fc_net.parameters()) diff --git a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_split.py b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_split.py index 94d25c82ac541..a158194992060 100644 --- a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_split.py +++ b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_split.py @@ -17,7 +17,7 @@ import numpy as np -from paddle import fluid +from paddle import base from paddle.io import DataLoader, IterableDataset, get_worker_info @@ -47,8 +47,8 @@ def __iter__(self): class TestDynamicDataLoaderIterSplit(unittest.TestCase): def test_main(self): - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): dataset = RangeIterableDatasetSplit(0, 10) dataloader = DataLoader( dataset, @@ -77,8 +77,8 @@ def __iter__(self): class TestDynamicDataLoaderIterInitFuncSplit(unittest.TestCase): def test_main(self): - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CPUPlace() + with base.dygraph.guard(place): dataset = RangeIterableDataset(0, 10) def worker_spliter(worker_id): diff --git a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_static.py b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_static.py index a2c0ec547fbcb..cf7ff971ca711 100644 --- a/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_static.py +++ b/test/legacy_test/test_multiprocess_dataloader_iterable_dataset_static.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.io import DataLoader, IterableDataset EPOCH_NUM = 2 @@ -45,13 +45,13 @@ def __iter__(self): def simple_fc_net_static(): - startup_prog = fluid.Program() - main_prog = fluid.Program() + startup_prog = base.Program() + main_prog = base.Program() startup_prog.random_seed = 1 main_prog.random_seed = 1 - with fluid.unique_name.guard(): - with fluid.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[None, IMAGE_SIZE], dtype='float32' ) @@ -59,10 +59,10 @@ def simple_fc_net_static(): name='label', shape=[None, 1], dtype='int64' ) hidden = image - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.8) ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ) for hidden_size in [10, 20, 30]: @@ -98,10 +98,10 @@ def simple_fc_net_static(): def prepare_places(with_cpu=False, with_gpu=True): places = [] if with_cpu: - places.append([fluid.CPUPlace()]) + places.append([base.CPUPlace()]) - if with_gpu and fluid.core.is_compiled_with_cuda(): - tmp = fluid.cuda_places()[:2] + if with_gpu and base.core.is_compiled_with_cuda(): + tmp = base.cuda_places()[:2] assert len(tmp) > 0, "no gpu detected" places.append([tmp[0]]) return places @@ -109,8 +109,8 @@ def prepare_places(with_cpu=False, with_gpu=True): class TestStaticDataLoader(unittest.TestCase): def run_main(self, num_workers, places, persistent_workers): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, image, label, loss = simple_fc_net_static() dataset = RandomDataset(SAMPLE_NUM, CLASS_NUM) @@ -126,10 +126,10 @@ def run_main(self, num_workers, places, persistent_workers): ) # assert len(dataloader) == int(SAMPLE_NUM / BATCH_SIZE) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) - prog = fluid.CompiledProgram(main_prog) + prog = base.CompiledProgram(main_prog) step_list = [] loss_list = [] @@ -212,8 +212,8 @@ def __iter__(self): class TestStaticDataLoaderWithBatchedDataset(TestStaticDataLoader): def run_main(self, num_workers, places, persistent_workers): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, image, label, loss = simple_fc_net_static() dataset = RandomBatchedDataset(SAMPLE_NUM, CLASS_NUM) @@ -228,7 +228,7 @@ def run_main(self, num_workers, places, persistent_workers): persistent_workers=persistent_workers, ) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) prog = main_prog diff --git a/test/legacy_test/test_multiprocess_dataloader_static.py b/test/legacy_test/test_multiprocess_dataloader_static.py index 478aec47f7f25..fed4534c52a02 100644 --- a/test/legacy_test/test_multiprocess_dataloader_static.py +++ b/test/legacy_test/test_multiprocess_dataloader_static.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.io import DataLoader, Dataset EPOCH_NUM = 3 @@ -45,13 +45,13 @@ def __len__(self): def simple_fc_net_static(): - startup_prog = fluid.Program() - main_prog = fluid.Program() + startup_prog = base.Program() + main_prog = base.Program() startup_prog.random_seed = 1 main_prog.random_seed = 1 - with fluid.unique_name.guard(): - with fluid.program_guard(main_prog, startup_prog): + with base.unique_name.guard(): + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[None, IMAGE_SIZE], dtype='float32' ) @@ -59,10 +59,10 @@ def simple_fc_net_static(): name='label', shape=[None, 1], dtype='int64' ) hidden = image - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.8) ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ) for hidden_size in [10, 20, 30]: @@ -98,10 +98,10 @@ def simple_fc_net_static(): def prepare_places(with_cpu=False, with_gpu=True): places = [] if with_cpu: - places.append([fluid.CPUPlace()]) + places.append([base.CPUPlace()]) - if with_gpu and fluid.core.is_compiled_with_cuda(): - tmp = fluid.cuda_places()[:2] + if with_gpu and base.core.is_compiled_with_cuda(): + tmp = base.cuda_places()[:2] assert len(tmp) > 0, "no gpu detected" places.append([tmp[0]]) return places @@ -109,8 +109,8 @@ def prepare_places(with_cpu=False, with_gpu=True): class TestStaticDataLoader(unittest.TestCase): def run_main(self, num_workers, places, persistent_workers): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, image, label, loss = simple_fc_net_static() dataset = RandomDataset(SAMPLE_NUM, CLASS_NUM) @@ -126,7 +126,7 @@ def run_main(self, num_workers, places, persistent_workers): ) assert len(dataloader) == int(SAMPLE_NUM / BATCH_SIZE) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) prog = main_prog @@ -193,12 +193,12 @@ def test_main(self): class TestStaticDataLoaderReturnList(unittest.TestCase): def run_single_place(self, num_workers): - scope = fluid.Scope() + scope = base.Scope() image = paddle.static.data( name='image', shape=[None, IMAGE_SIZE], dtype='float32' ) label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - with fluid.scope_guard(scope): + with base.scope_guard(scope): dataset = RandomDataset(SAMPLE_NUM, CLASS_NUM) dataloader = DataLoader( dataset, @@ -216,19 +216,19 @@ def run_single_place(self, num_workers): assert not isinstance(d[1], list) def run_multi_place(self, num_workers): - scope = fluid.Scope() + scope = base.Scope() image = paddle.static.data( name='image', shape=[None, IMAGE_SIZE], dtype='float32' ) label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - with fluid.scope_guard(scope): + with base.scope_guard(scope): dataset = RandomDataset(SAMPLE_NUM, CLASS_NUM) dataloader = DataLoader( dataset, feed_list=[image, label], num_workers=num_workers, batch_size=BATCH_SIZE, - places=[fluid.CPUPlace()] * 2, + places=[base.CPUPlace()] * 2, drop_last=True, return_list=True, ) @@ -270,8 +270,8 @@ def __len__(self): class TestStaticDataLoaderWithBatchedDataset(TestStaticDataLoader): def run_main(self, num_workers, places, persistent_workers): - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): startup_prog, main_prog, image, label, loss = simple_fc_net_static() dataset = RandomBatchedDataset(SAMPLE_NUM, CLASS_NUM) @@ -287,10 +287,10 @@ def run_main(self, num_workers, places, persistent_workers): ) assert len(dataloader) == int(SAMPLE_NUM / BATCH_SIZE) - exe = fluid.Executor(place=places[0]) + exe = base.Executor(place=places[0]) exe.run(startup_prog) - prog = fluid.CompiledProgram(main_prog) + prog = base.CompiledProgram(main_prog) step_list = [] loss_list = [] diff --git a/test/legacy_test/test_multiprocess_reader_exception.py b/test/legacy_test/test_multiprocess_reader_exception.py index a32552c282d69..e885cffcb4fb0 100644 --- a/test/legacy_test/test_multiprocess_reader_exception.py +++ b/test/legacy_test/test_multiprocess_reader_exception.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.reader import multiprocess_reader @@ -31,10 +31,10 @@ def setUp(self): self.raise_exception = False def places(self): - if fluid.is_compiled_with_cuda(): - return [fluid.CPUPlace(), fluid.CUDAPlace(0)] + if base.is_compiled_with_cuda(): + return [base.CPUPlace(), base.CUDAPlace(0)] else: - return [fluid.CPUPlace()] + return [base.CPUPlace()] def main_impl(self, place, iterable): sample_num = 40 @@ -52,11 +52,11 @@ def __impl__(): return __impl__ - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): image = paddle.static.data( name='image', dtype='float32', shape=[None, 10] ) - reader = fluid.io.DataLoader.from_generator( + reader = base.io.DataLoader.from_generator( feed_list=[image], capacity=2, iterable=iterable ) @@ -66,21 +66,21 @@ def __impl__(): [fake_reader(), fake_reader()], use_pipe=self.use_pipe ) - if isinstance(place, fluid.CUDAPlace): + if isinstance(place, base.CUDAPlace): reader.set_sample_generator( decorated_reader, batch_size=batch_size, - places=fluid.cuda_places(0), + places=base.cuda_places(0), ) else: reader.set_sample_generator( decorated_reader, batch_size=batch_size, - places=fluid.cpu_places(1), + places=base.cpu_places(1), ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) batch_num = int(sample_num * 2 / batch_size) @@ -103,7 +103,7 @@ def __impl__(): while True: exe.run(fetch_list=[image_p_1]) num += 1 - except fluid.core.EOFException: + except base.core.EOFException: reader.reset() self.assertFalse(self.raise_exception) self.assertEqual(num, batch_num) @@ -116,7 +116,7 @@ def test_main(self): for p in self.places(): for iterable in [False, True]: try: - with fluid.scope_guard(fluid.Scope()): + with base.scope_guard(base.Scope()): self.main_impl(p, iterable) self.assertTrue(not self.raise_exception) diff --git a/test/legacy_test/test_naive_best_fit_gpu_memory_limit.py b/test/legacy_test/test_naive_best_fit_gpu_memory_limit.py index dba09d70a6e07..de601cb89c484 100644 --- a/test/legacy_test/test_naive_best_fit_gpu_memory_limit.py +++ b/test/legacy_test/test_naive_best_fit_gpu_memory_limit.py @@ -16,34 +16,34 @@ import numpy as np -from paddle import fluid +from paddle import base -fluid.core.globals()['FLAGS_allocator_strategy'] = 'naive_best_fit' +base.core.globals()['FLAGS_allocator_strategy'] = 'naive_best_fit' -if fluid.is_compiled_with_cuda(): - fluid.core.globals()['FLAGS_gpu_memory_limit_mb'] = 10 +if base.is_compiled_with_cuda(): + base.core.globals()['FLAGS_gpu_memory_limit_mb'] = 10 class TestBase(unittest.TestCase): def setUp(self): - if fluid.is_compiled_with_cuda(): - self._limit = fluid.core.globals()['FLAGS_gpu_memory_limit_mb'] + if base.is_compiled_with_cuda(): + self._limit = base.core.globals()['FLAGS_gpu_memory_limit_mb'] def test_allocate(self): - if not fluid.is_compiled_with_cuda(): + if not base.is_compiled_with_cuda(): return other_dim = int(1024 * 1024 / 4) - place = fluid.CUDAPlace(0) - t = fluid.LoDTensor() + place = base.CUDAPlace(0) + t = base.LoDTensor() t.set( np.ndarray([int(self._limit / 2), other_dim], dtype='float32'), place, ) del t - t = fluid.LoDTensor() + t = base.LoDTensor() large_np = np.ndarray([2 * self._limit, other_dim], dtype='float32') try: diff --git a/test/legacy_test/test_name_scope.py b/test/legacy_test/test_name_scope.py index af8617cb99908..4b3e5dd0ff9df 100644 --- a/test/legacy_test/test_name_scope.py +++ b/test/legacy_test/test_name_scope.py @@ -15,24 +15,24 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestNameScope(unittest.TestCase): def test_name_scope(self): - with fluid.name_scope("s1"): + with base.name_scope("s1"): a = paddle.static.data(name='data', shape=[-1, 1], dtype='int32') b = a + 1 - with fluid.name_scope("s2"): + with base.name_scope("s2"): c = b * 1 - with fluid.name_scope("s3"): + with base.name_scope("s3"): d = c / 1 - with fluid.name_scope("s1"): + with base.name_scope("s1"): f = paddle.pow(d, 2.0) - with fluid.name_scope("s4"): + with base.name_scope("s4"): g = f - 1 - for op in fluid.default_main_program().block(0).ops: + for op in base.default_main_program().block(0).ops: if op.type == 'elementwise_add': self.assertEqual(op.desc.attr("op_namescope"), '/s1/') elif op.type == 'elementwise_mul': diff --git a/test/legacy_test/test_nan_inf.py b/test/legacy_test/test_nan_inf.py index cf5ad32daf195..e6f6ddb770144 100644 --- a/test/legacy_test/test_nan_inf.py +++ b/test/legacy_test/test_nan_inf.py @@ -117,7 +117,7 @@ def test_nan_inf_dynamic(self): self.run_check_nan_inf(cmd, self.dygraph_expected_op_count) # Test on GPU. - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): cmd = f"{self._python_interp} {filepath} --use_cuda --check_nan_inf_level {self.check_nan_inf_level}" self.run_check_nan_inf(cmd, self.dygraph_expected_op_count) @@ -233,7 +233,7 @@ def _check_num_nan_inf(use_cuda): {"FLAGS_check_nan_inf": 1, "FLAGS_check_nan_inf_level": 0} ) _check_num_nan_inf(use_cuda=False) - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): _check_num_nan_inf(use_cuda=True) def run_check_nan_inf_level(self, use_cuda, dtype, level): @@ -257,7 +257,7 @@ def test_check_nan_inf_level_float32(self): self.run_check_nan_inf_level( use_cuda=False, dtype="float32", level=level ) - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.run_check_nan_inf_level( use_cuda=True, dtype="float32", level=level ) @@ -267,7 +267,7 @@ def test_check_nan_inf_level_float16(self): self.run_check_nan_inf_level( use_cuda=False, dtype="float32", level=level ) - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.run_check_nan_inf_level( use_cuda=True, dtype="float16", level=level ) @@ -279,7 +279,7 @@ def test_eager(self): x_np, y_np = self.generate_inputs(shape, "float32") device_list = ["cpu"] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): device_list.append("gpu:0") for device in device_list: diff --git a/test/legacy_test/test_nan_inf_dir.py b/test/legacy_test/test_nan_inf_dir.py index 122ddb74f4123..180e84044b8b0 100644 --- a/test/legacy_test/test_nan_inf_dir.py +++ b/test/legacy_test/test_nan_inf_dir.py @@ -110,7 +110,7 @@ def test_num_nan_inf(self): self.check_num_nan_inf( x_np, use_cuda=False, subdir="check_nan_inf_dir_cpu" ) - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): self.check_num_nan_inf( x_np, use_cuda=True, subdir="check_nan_inf_dir_gpu" ) diff --git a/test/legacy_test/test_nan_to_num_op.py b/test/legacy_test/test_nan_to_num_op.py index a5e0bbe62f4e2..c8a6d4fc67bef 100644 --- a/test/legacy_test/test_nan_to_num_op.py +++ b/test/legacy_test/test_nan_to_num_op.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core # from eager_op_test import OpTest @@ -87,7 +87,7 @@ def test_static(self): def test_dygraph(self): paddle.disable_static(place=self.place) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # NOTE(tiancaishaonvjituizi): float64 input fails the test x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( np.float32 diff --git a/test/legacy_test/test_nanmean_api.py b/test/legacy_test/test_nanmean_api.py index f89756e7d8f2b..58c47a77702f3 100644 --- a/test/legacy_test/test_nanmean_api.py +++ b/test/legacy_test/test_nanmean_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_nanmedian.py b/test/legacy_test/test_nanmedian.py index ba3fe7f5039bd..9a55a91ac625b 100644 --- a/test/legacy_test/test_nanmedian.py +++ b/test/legacy_test/test_nanmedian.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(102) @@ -175,7 +175,7 @@ def test_duplicated_axis(): def test_dygraph(self): paddle.disable_static(place=self.place) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): data = self.fake_data["col_nan_odd"] out = paddle.nanmedian(paddle.to_tensor(data), keepdim=True) np_res = np.nanmedian(data, keepdims=True) diff --git a/test/legacy_test/test_nansum_api.py b/test/legacy_test/test_nansum_api.py index f0f255fe6e120..050ff6be2c8b2 100644 --- a/test/legacy_test/test_nansum_api.py +++ b/test/legacy_test/test_nansum_api.py @@ -17,15 +17,15 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class API_Test_Nansum(unittest.TestCase): def test_static_graph(self): paddle.enable_static() - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 4] ) @@ -33,10 +33,10 @@ def test_static_graph(self): out2 = paddle.nansum(input, axis=0) out3 = paddle.nansum(input, axis=-1) out4 = paddle.nansum(input, axis=1, keepdim=True) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) x = np.array( @@ -87,7 +87,7 @@ def test_static_graph_fp16(self): out2 = paddle.nansum(input, axis=0) out3 = paddle.nansum(input, axis=-1) out4 = paddle.nansum(input, axis=1, keepdim=True) - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) exe = paddle.static.Executor(place) exe.run(startup_program) @@ -131,8 +131,8 @@ def test_dygraph(self): x = np.array( [[float('nan'), 3, 5, 9], [1, 2, float('-nan'), 7]] ).astype(np.float32) - with fluid.dygraph.guard(): - inputs = fluid.dygraph.to_variable(x) + with base.dygraph.guard(): + inputs = base.dygraph.to_variable(x) out = paddle.nansum(inputs) out_ref = np.array([27]).astype(np.float32) diff --git a/test/legacy_test/test_nce.py b/test/legacy_test/test_nce.py index f91abbff55248..8332f07b85c3a 100644 --- a/test/legacy_test/test_nce.py +++ b/test/legacy_test/test_nce.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard def nce( @@ -161,7 +161,7 @@ def setUp(self): @staticmethod def get_place(): - place = fluid.core.CPUPlace() + place = base.core.CPUPlace() return place @staticmethod @@ -195,7 +195,7 @@ def train_network( ) w_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[num_total_classes, 10], @@ -205,7 +205,7 @@ def train_network( ) ) b_param = ( - fluid.default_main_program() + base.default_main_program() .global_block() .create_parameter( shape=[num_total_classes, 1], @@ -238,7 +238,7 @@ def train_network( def test_input_is_selected_rows(self): with paddle_static_guard(): place = self.get_place() - exe = fluid.Executor(place) + exe = base.Executor(place) data = self.get_train_data(self.batch_size) nid_freq_arr = np.random.dirichlet(np.ones(20) * 1000).astype( @@ -247,17 +247,17 @@ def test_input_is_selected_rows(self): rets = [] # for dense - dense_scope = fluid.core.Scope() - dense_startup_program = fluid.framework.Program() - dense_train_program = fluid.framework.Program() - with fluid.scope_guard(dense_scope): - with fluid.program_guard( + dense_scope = base.core.Scope() + dense_startup_program = base.framework.Program() + dense_train_program = base.framework.Program() + with base.scope_guard(dense_scope): + with base.program_guard( dense_train_program, dense_startup_program ): cost, feeds = self.train_network( 20, 5, "custom_dist", nid_freq_arr.tolist(), False ) - feeder = fluid.DataFeeder(feed_list=feeds, place=place) + feeder = base.DataFeeder(feed_list=feeds, place=place) paddle.enable_static() exe.run(dense_startup_program) loss_val = exe.run( @@ -268,17 +268,17 @@ def test_input_is_selected_rows(self): rets.append(np.mean(loss_val)) # for sparse - sparse_scope = fluid.core.Scope() - sparse_startup_program = fluid.framework.Program() - sparse_train_program = fluid.framework.Program() - with fluid.scope_guard(sparse_scope): - with fluid.program_guard( + sparse_scope = base.core.Scope() + sparse_startup_program = base.framework.Program() + sparse_train_program = base.framework.Program() + with base.scope_guard(sparse_scope): + with base.program_guard( sparse_train_program, sparse_startup_program ): cost, feeds = self.train_network( 20, 5, "custom_dist", nid_freq_arr.tolist(), True ) - feeder = fluid.DataFeeder(feed_list=feeds, place=place) + feeder = base.DataFeeder(feed_list=feeds, place=place) paddle.enable_static() exe.run(sparse_startup_program) loss_val = exe.run( @@ -295,10 +295,10 @@ class TestNCE_OpError(unittest.TestCase): def test_errors(self): with paddle_static_guard(): with program_guard(Program(), Program()): - input1 = fluid.create_lod_tensor( + input1 = base.create_lod_tensor( np.array([0.0, 3.0, 2.0, 4.0]), [[1, 1, 2]], - fluid.CPUPlace(), + base.CPUPlace(), ) label1 = paddle.static.data( name='label1', shape=[-1, 4], dtype="int64" @@ -311,10 +311,10 @@ def test_errors(self): input2 = paddle.static.data( name='input2', shape=[-1, 4], dtype="float32" ) - label2 = fluid.create_lod_tensor( + label2 = base.create_lod_tensor( np.array([0.0, 3.0, 2.0, 4.0]), [[1, 1, 2]], - fluid.CPUPlace(), + base.CPUPlace(), ) # the input(label) of nce layer must be Variable. self.assertRaises( diff --git a/test/legacy_test/test_nearest_interp_op.py b/test/legacy_test/test_nearest_interp_op.py index f1cad2f66899b..6ef7c23589ed3 100755 --- a/test/legacy_test/test_nearest_interp_op.py +++ b/test/legacy_test/test_nearest_interp_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core def nearest_neighbor_interp_np( diff --git a/test/legacy_test/test_nearest_interp_v2_op.py b/test/legacy_test/test_nearest_interp_v2_op.py index 72b371b9e3469..15902efc28e53 100755 --- a/test/legacy_test/test_nearest_interp_v2_op.py +++ b/test/legacy_test/test_nearest_interp_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional import interpolate paddle.enable_static() @@ -969,7 +969,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 3, 6, 6)).astype("int64") scale_np = np.array([2, 2]).astype("int64") input_x = paddle.to_tensor(input_data) @@ -994,7 +994,7 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.random.random((2, 2, 6, 6, 6)).astype("int64") scale_np = np.array([2, 2, 2]).astype("int64") input_x = paddle.to_tensor(input_data) @@ -1013,7 +1013,7 @@ def test_case(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestNearestInterp3DOpForFloat16(unittest.TestCase): def init_test_case(self): @@ -1054,7 +1054,7 @@ def test_main(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestNearestInterpOpForFloat16(unittest.TestCase): def init_test_case(self): diff --git a/test/legacy_test/test_neg_op.py b/test/legacy_test/test_neg_op.py index ea748a57aeb84..8e17344dfe1b9 100644 --- a/test/legacy_test/test_neg_op.py +++ b/test/legacy_test/test_neg_op.py @@ -57,7 +57,7 @@ def test_cpu(self): self.run_static() def test_gpu(self): - if not paddle.fluid.core.is_compiled_with_cuda(): + if not paddle.base.core.is_compiled_with_cuda(): return paddle.disable_static(place=paddle.CUDAPlace(0)) diff --git a/test/legacy_test/test_network_with_dtype.py b/test/legacy_test/test_network_with_dtype.py index 07f38a1e80d9c..69d3bfb7f9d7f 100644 --- a/test/legacy_test/test_network_with_dtype.py +++ b/test/legacy_test/test_network_with_dtype.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core BATCH_SIZE = 20 @@ -27,9 +27,9 @@ def setUp(self): self.init_dtype() def run_net_on_place(self, place): - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): x = paddle.static.data(name='x', shape=[-1, 13], dtype=self.dtype) y = paddle.static.data(name='y', shape=[-1, 1], dtype=self.dtype) y_predict = paddle.static.nn.fc(x, size=1, activation=None) @@ -44,8 +44,8 @@ def run_net_on_place(self, place): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=BATCH_SIZE ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) exe.run(startup) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) @@ -56,13 +56,13 @@ def init_dtype(self): pass def test_cpu(self): - place = fluid.CPUPlace() + place = base.CPUPlace() self.run_net_on_place(place) def test_gpu(self): if not core.is_compiled_with_cuda(): return - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) self.run_net_on_place(place) diff --git a/test/legacy_test/test_nll_loss.py b/test/legacy_test/test_nll_loss.py index e0bb3882806c1..e225bbebd9a0d 100644 --- a/test/legacy_test/test_nll_loss.py +++ b/test/legacy_test/test_nll_loss.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base def nll_loss_1d( @@ -82,15 +82,15 @@ def test_NLLLoss_1D_mean(self): input_np = np.random.random(size=(10, 10)).astype(np.float64) np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -98,21 +98,21 @@ def test_NLLLoss_1D_mean(self): nll_loss = paddle.nn.loss.NLLLoss() res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss() dy_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) ) dy_result = dy_res.numpy() - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss() eager_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) @@ -130,15 +130,15 @@ def test_NLLLoss_1D_sum(self): input_np = np.random.random(size=(10, 10)).astype(np.float64) np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -146,14 +146,14 @@ def test_NLLLoss_1D_sum(self): nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') dy_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) @@ -181,15 +181,15 @@ def test_NLLLoss_1D_with_weight_mean(self): np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) weight_np = np.random.random(size=(10,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -200,7 +200,7 @@ def test_NLLLoss_1D_with_weight_mean(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight) res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -211,7 +211,7 @@ def test_NLLLoss_1D_with_weight_mean(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np) ) @@ -243,15 +243,15 @@ def test_NLLLoss_1D_with_weight_sum(self): np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) weight_np = np.random.random(size=(10,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -262,7 +262,7 @@ def test_NLLLoss_1D_with_weight_sum(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -273,7 +273,7 @@ def test_NLLLoss_1D_with_weight_sum(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='sum' ) @@ -295,10 +295,10 @@ def test_NLLLoss_1D_with_weight_mean_cpu(self): np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) weight_np = np.random.random(size=(10,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() - place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -309,7 +309,7 @@ def test_NLLLoss_1D_with_weight_mean_cpu(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight) res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -320,7 +320,7 @@ def test_NLLLoss_1D_with_weight_mean_cpu(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np) ) @@ -340,10 +340,10 @@ def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): np.random.seed(200) label_np = np.random.randint(0, 10, size=(10,)).astype(np.int64) weight_np = np.random.random(size=(10,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() - place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[10, 10], dtype='float64' ) @@ -354,7 +354,7 @@ def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -365,7 +365,7 @@ def test_NLLLoss_1D_with_weight_no_reduce_cpu(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='none' ) @@ -386,15 +386,15 @@ def test_NLLLoss_2D_mean(self): input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5], dtype='float64' ) @@ -404,14 +404,14 @@ def test_NLLLoss_2D_mean(self): nll_loss = paddle.nn.loss.NLLLoss() res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss() dy_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) @@ -429,15 +429,15 @@ def test_NLLLoss_2D_sum(self): input_np = np.random.random(size=(5, 3, 5, 5)).astype(np.float64) np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5], dtype='float64' ) @@ -447,14 +447,14 @@ def test_NLLLoss_2D_sum(self): nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss(reduction='sum') dy_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) @@ -473,15 +473,15 @@ def test_NLLLoss_2D_with_weight_mean(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5], dtype='float64' ) @@ -495,7 +495,7 @@ def test_NLLLoss_2D_with_weight_mean(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight) res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -506,7 +506,7 @@ def test_NLLLoss_2D_with_weight_mean(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np) ) @@ -527,10 +527,10 @@ def test_NLLLoss_2D_with_weight_mean_cpu(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() - place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5], dtype='float64' ) @@ -544,7 +544,7 @@ def test_NLLLoss_2D_with_weight_mean_cpu(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight) res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -555,7 +555,7 @@ def test_NLLLoss_2D_with_weight_mean_cpu(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np) ) @@ -576,14 +576,14 @@ def test_NLLLoss_2D_with_weight_sum(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5], dtype='float64' ) @@ -597,7 +597,7 @@ def test_NLLLoss_2D_with_weight_sum(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -608,7 +608,7 @@ def test_NLLLoss_2D_with_weight_sum(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='sum' ) @@ -630,15 +630,15 @@ def test_NLLLoss_in_dims_not_2or4_mean(self): input_np = np.random.random(size=(5, 3, 5, 5, 5)).astype(np.float64) np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5, 5], dtype='float64' ) @@ -648,14 +648,14 @@ def test_NLLLoss_in_dims_not_2or4_mean(self): nll_loss = paddle.nn.loss.NLLLoss() res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={"input": input_np, "label": label_np}, fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss() dy_res = nll_loss( paddle.to_tensor(input_np), paddle.to_tensor(label_np) @@ -680,15 +680,15 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5, 5], dtype='float64' ) @@ -701,7 +701,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight) res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -712,7 +712,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_mean(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np) ) @@ -741,15 +741,15 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5, 5], dtype='float64' ) @@ -762,7 +762,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='sum') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -773,7 +773,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_sum(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='sum' ) @@ -805,15 +805,15 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - # place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + # place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5, 5], dtype='float64' ) @@ -826,7 +826,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -837,7 +837,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='none' ) @@ -870,10 +870,10 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): np.random.seed(200) label_np = np.random.randint(0, 3, size=(5, 5, 5, 5)).astype(np.int64) weight_np = np.random.random(size=(3,)).astype(np.float64) - prog = fluid.Program() - startup_prog = fluid.Program() - place = fluid.CPUPlace() - with fluid.program_guard(prog, startup_prog): + prog = base.Program() + startup_prog = base.Program() + place = base.CPUPlace() + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[5, 3, 5, 5, 5], dtype='float64' ) @@ -886,7 +886,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): nll_loss = paddle.nn.loss.NLLLoss(weight=weight, reduction='none') res = nll_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_result,) = exe.run( prog, feed={ @@ -897,7 +897,7 @@ def test_NLLLoss_in_dims_not_2or4_with_weight_no_reduce_cpu(self): fetch_list=[res], ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): nll_loss = paddle.nn.loss.NLLLoss( weight=paddle.to_tensor(weight_np), reduction='none' ) @@ -966,10 +966,10 @@ def test_check_output_with_weight(self): def test_check_grad(self): self.with_weight = True - place = fluid.CPUPlace() + place = base.CPUPlace() self.check_grad_with_place(place, ['X'], 'Out') - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out') def init_test_case(self): @@ -1017,10 +1017,10 @@ def test_check_output_with_weight(self): def test_check_grad(self): self.with_weight = True - place = fluid.CPUPlace() + place = base.CPUPlace() self.check_grad_with_place(place, ['X'], 'Out') - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out') def init_test_case(self): @@ -1067,10 +1067,10 @@ def test_check_output_with_weight(self): def test_check_grad(self): self.with_weight = True - place = fluid.CPUPlace() + place = base.CPUPlace() self.check_grad_with_place(place, ['X'], 'Out') - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out') def init_test_case(self): @@ -1118,10 +1118,10 @@ def test_check_output_with_weight(self): def test_check_grad(self): self.with_weight = True - place = fluid.CPUPlace() + place = base.CPUPlace() self.check_grad_with_place(place, ['X'], 'Out') - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) self.check_grad_with_place(place, ['X'], 'Out') def init_test_case(self): @@ -1169,7 +1169,7 @@ def test_x_dim_lt_2(): self.assertRaises(ValueError, test_x_dim_lt_2) def test_x_dim_imperative_lt_2(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_np = np.random.random(size=(5,)).astype(np.float64) label_np = np.random.randint(0, 10, size=(5,)).astype(np.int64) x = paddle.to_tensor(x_np) @@ -1228,7 +1228,7 @@ def test_NLLLoss_reduction_not_sum_mean_none(): self.assertRaises(ValueError, test_NLLLoss_reduction_not_sum_mean_none) def test_NLLLoss_reduction_imperative_not_sum_mean_none(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_np = np.random.random(size=(5, 3)).astype(np.float64) label_np = np.random.randint(0, 3, size=(5,)).astype(np.int64) x = paddle.to_tensor(x_np) @@ -1258,7 +1258,7 @@ def test_nll_loss_function_reduction_not_sum_mean_none(): ) def test_nll_loss_function_reduction_imperative_not_sum_mean_none(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_np = np.random.random(size=(5, 3)).astype(np.float64) label_np = np.random.randint(0, 3, size=(5,)).astype(np.int64) x = paddle.to_tensor(x_np) diff --git a/test/legacy_test/test_nn_functional_embedding_static.py b/test/legacy_test/test_nn_functional_embedding_static.py index 1cf9198504738..49935b1b1ec41 100644 --- a/test/legacy_test/test_nn_functional_embedding_static.py +++ b/test/legacy_test/test_nn_functional_embedding_static.py @@ -17,21 +17,21 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn import functional class EmbeddingStatic(unittest.TestCase): def test_1(self): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): def test_bad_x(): initializer = paddle.nn.initializer.Assign( np.random.random(size=(128, 100)) ) - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( name="emb_weight", learning_rate=0.5, initializer=initializer, @@ -55,15 +55,15 @@ def test_bad_x(): test_bad_x() def test_2(self): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): def test_bad_x(): initializer = paddle.nn.initializer.Assign( np.random.random(size=(128, 100)) ) - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( name="emb_weight", learning_rate=0.5, initializer=initializer, diff --git a/test/legacy_test/test_nn_functional_hot_op.py b/test/legacy_test/test_nn_functional_hot_op.py index 0a4fb0942f8c1..e320a991c74bc 100644 --- a/test/legacy_test/test_nn_functional_hot_op.py +++ b/test/legacy_test/test_nn_functional_hot_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn import functional @@ -131,9 +131,9 @@ def test_api_with_dygraph(self): label = np.array( [np.random.randint(0, num_classes - 1) for i in range(6)] ).reshape([6, 1]) - with fluid.dygraph.guard(): + with base.dygraph.guard(): one_hot_label = functional.one_hot( - x=fluid.dygraph.to_variable(label), num_classes=num_classes + x=base.dygraph.to_variable(label), num_classes=num_classes ) def _run(self, num_classes): @@ -141,13 +141,13 @@ def _run(self, num_classes): label.desc.set_need_check_feed(False) one_hot_label = functional.one_hot(x=label, num_classes=num_classes) - place = fluid.CPUPlace() + place = base.CPUPlace() label_data = np.array( [np.random.randint(0, 10 - 1) for i in range(6)] ).reshape([6, 1]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={ 'label': label_data, @@ -159,7 +159,7 @@ def _run(self, num_classes): class BadInputTestOnehotV2(unittest.TestCase): def test_error(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_bad_x(): label = paddle.static.data( diff --git a/test/legacy_test/test_nn_grad.py b/test/legacy_test/test_nn_grad.py index 398b3d9c888e0..592f4d8c0c922 100644 --- a/test/legacy_test/test_nn_grad.py +++ b/test/legacy_test/test_nn_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -47,9 +47,9 @@ def config(self): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: self.func(place) @@ -82,9 +82,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -106,9 +106,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -131,9 +131,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -162,9 +162,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -193,9 +193,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -225,9 +225,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -257,9 +257,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -284,9 +284,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -306,9 +306,9 @@ def func(self, place): gradient_checker.double_grad_check([x], out, x_init=x_arr, place=place) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -328,9 +328,9 @@ def func(self, place): gradient_checker.double_grad_check([x], out, x_init=x_arr, place=place) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -361,9 +361,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -413,9 +413,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -438,9 +438,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -474,9 +474,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -509,9 +509,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -540,9 +540,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_nn_margin_rank_loss.py b/test/legacy_test/test_nn_margin_rank_loss.py index f125c1cbe878f..35967c9390936 100644 --- a/test/legacy_test/test_nn_margin_rank_loss.py +++ b/test/legacy_test/test_nn_margin_rank_loss.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import Program, program_guard @@ -42,7 +42,7 @@ def setUp(self): "float64" ) self.places = [] - self.places.append(fluid.CPUPlace()) + self.places.append(base.CPUPlace()) if core.is_compiled_with_cuda(): self.places.append(paddle.CUDAPlace(0)) diff --git a/test/legacy_test/test_nn_matmul_v2_grad.py b/test/legacy_test/test_nn_matmul_v2_grad.py index 1924045c0e88c..62350f9532ea1 100644 --- a/test/legacy_test/test_nn_matmul_v2_grad.py +++ b/test/legacy_test/test_nn_matmul_v2_grad.py @@ -19,8 +19,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -57,9 +57,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -72,9 +72,9 @@ def init_test(self): self.transpose_y = True def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -111,9 +111,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -150,9 +150,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -190,9 +190,9 @@ def func(self, place): def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -229,9 +229,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -268,9 +268,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -307,9 +307,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -346,9 +346,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -385,9 +385,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -424,9 +424,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -463,9 +463,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -502,9 +502,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -541,9 +541,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -580,9 +580,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -619,9 +619,9 @@ def func(self, place): ) def test_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_nn_sigmoid_op.py b/test/legacy_test/test_nn_sigmoid_op.py index 298e5cf1f4a6b..a038ff7b94b8f 100644 --- a/test/legacy_test/test_nn_sigmoid_op.py +++ b/test/legacy_test/test_nn_sigmoid_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid, nn -from paddle.fluid import core +from paddle import base, nn +from paddle.base import core from paddle.nn import functional @@ -45,7 +45,7 @@ def check_static_api(self, place): x = paddle.static.data(name='x', shape=self.x_shape) x.stop_gradient = False y = mysigmoid(x) - fluid.backward.append_backward(paddle.mean(y)) + base.backward.append_backward(paddle.mean(y)) exe = paddle.static.Executor(place) out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y]) np.testing.assert_allclose(out[0], self.y, rtol=1e-05) @@ -59,9 +59,9 @@ def check_dynamic_api(self, place): np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05) def test_check_api(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: self.check_dynamic_api(place) self.check_static_api(place) @@ -85,7 +85,7 @@ def check_static_api(self, place): with paddle.static.program_guard(main_program): x = paddle.static.data(name='x', shape=self.x_shape) y = functional.sigmoid(x, name="api_sigmoid") - exe = paddle.static.Executor(fluid.CPUPlace()) + exe = paddle.static.Executor(base.CPUPlace()) out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y]) np.testing.assert_allclose(out[0], self.y, rtol=1e-05) @@ -96,9 +96,9 @@ def check_dynamic_api(self): np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05) def test_check_api(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: self.check_static_api(place) self.check_dynamic_api() diff --git a/test/legacy_test/test_noamdecay_op.py b/test/legacy_test/test_noamdecay_op.py index 62312c7a8b9f0..aafa50e0e74af 100644 --- a/test/legacy_test/test_noamdecay_op.py +++ b/test/legacy_test/test_noamdecay_op.py @@ -19,7 +19,7 @@ class TestSparseEmbeddingAPIError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in sparse_embedding should not be 0. def test_0_d_model(): schedular = paddle.optimizer.lr.NoamDecay( diff --git a/test/legacy_test/test_nonzero_api.py b/test/legacy_test/test_nonzero_api.py index c78de5a24f6be..6dbe9cfed9de1 100644 --- a/test/legacy_test/test_nonzero_api.py +++ b/test/legacy_test/test_nonzero_api.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard def call_nonzero(x): @@ -37,7 +37,7 @@ def test_nonzero_api_as_tuple(self): self.assertEqual(type(y), tuple) self.assertEqual(len(y), 2) z = paddle.concat(list(y), axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[z.name], return_numpy=False @@ -53,7 +53,7 @@ def test_nonzero_api_as_tuple(self): self.assertEqual(type(y), tuple) self.assertEqual(len(y), 1) z = paddle.concat(list(y), axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[z.name], return_numpy=False ) @@ -66,7 +66,7 @@ def test_nonzero_api(self): x = paddle.static.data(name='x', shape=[-1, 2], dtype='float32') x.desc.set_need_check_feed(False) y = paddle.nonzero(x) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[y.name], return_numpy=False ) @@ -78,7 +78,7 @@ def test_nonzero_api(self): x = paddle.static.data(name='x', shape=[-1], dtype='float32') x.desc.set_need_check_feed(False) y = paddle.nonzero(x) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[y.name], return_numpy=False ) @@ -87,8 +87,8 @@ def test_nonzero_api(self): def test_dygraph_api(self): data_x = np.array([[True, False], [False, True]]) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(data_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(data_x) z = paddle.nonzero(x) np_z = z.numpy() expect_out = np.array([[0, 0], [1, 1]]) diff --git a/test/legacy_test/test_norm_all.py b/test/legacy_test/test_norm_all.py index 75441aa5dfab4..44001abdea81f 100644 --- a/test/legacy_test/test_norm_all.py +++ b/test/legacy_test/test_norm_all.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import _C_ops, fluid -from paddle.fluid import core -from paddle.fluid.framework import in_dygraph_mode +from paddle import _C_ops, base +from paddle.base import core +from paddle.base.framework import in_dygraph_mode # hack method for test p_norm final state @@ -412,11 +412,11 @@ def calc_gradient(self): def run_fro(self, p, axis, shape_x, dtype, keep_dim, check_dim=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data = paddle.static.data(name="X", shape=shape_x, dtype=dtype) out = paddle.norm(x=data, p=p, axis=axis, keepdim=keep_dim) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) np_input = (np.random.rand(*shape_x) + 1.0).astype(dtype) expected_result = numpy_frobenius_norm( np_input, axis=axis, keepdims=keep_dim @@ -434,11 +434,11 @@ def run_fro(self, p, axis, shape_x, dtype, keep_dim, check_dim=False): def run_pnorm(self, p, axis, shape_x, dtype, keep_dim, check_dim=False): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data = paddle.static.data(name="X", shape=shape_x, dtype=dtype) out = paddle.norm(x=data, p=p, axis=axis, keepdim=keep_dim) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) np_input = (np.random.rand(*shape_x) + 1.0).astype(dtype) expected_result = p_norm( np_input, porder=p, axis=axis, keepdims=keep_dim @@ -637,7 +637,7 @@ def test_dygraph(self): run_graph(self, p='fro', axis=None, shape_x=[2, 3, 4], dtype="float32") def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[10, 10], dtype="float32") y_1 = paddle.norm(x, p='fro', name='frobenius_name') y_2 = paddle.norm(x, p=2, name='pnorm_name') @@ -645,7 +645,7 @@ def test_name(self): self.assertEqual(('pnorm_name' in y_2.name), True) def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): def err_dtype(p, shape_x, xdtype, out=None): data = paddle.static.data(shape=shape_x, dtype=xdtype) @@ -674,7 +674,7 @@ def err_dtype(p, shape_x, xdtype, out=None): ValueError, paddle.norm, data, p='unspport', axis=[-3, -2, -1] ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): # The size of input in Norm should not be 0. def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_norm_nn_grad.py b/test/legacy_test/test_norm_nn_grad.py index 361929d49b8e0..3bd8f1b61ee05 100644 --- a/test/legacy_test/test_norm_nn_grad.py +++ b/test/legacy_test/test_norm_nn_grad.py @@ -19,15 +19,15 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestInstanceNormDoubleGradCheck(unittest.TestCase): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed() shape = [2, 3, 4, 5] dtype = "float32" @@ -42,9 +42,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -54,8 +54,8 @@ class TestInstanceNormDoubleGradCheckWithoutParamBias( ): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed() shape = [2, 3, 4, 5] dtype = "float32" @@ -77,8 +77,8 @@ def instance_norm_wrapper(self, x): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed() shape = [2, 3, 4, 5] dtype = "float32" @@ -103,9 +103,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -119,8 +119,8 @@ def instance_norm_wrapper(self, x): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed() shape = [2, 3, 4, 5] dtype = "float32" @@ -164,8 +164,8 @@ def batch_norm_wrapper(self, x): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed() dtype = "float32" eps = 0.005 @@ -191,9 +191,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -241,8 +241,8 @@ def batch_norm_wrapper(self, x): class TestBatchNormDoubleGradCheckCase5(TestBatchNormDoubleGradCheck): @prog_scope() def func(self, place): - prog = fluid.Program() - with fluid.program_guard(prog): + prog = base.Program() + with base.program_guard(prog): np.random.seed(37) dtype = "float32" eps = 0.005 diff --git a/test/legacy_test/test_norm_op.py b/test/legacy_test/test_norm_op.py index 3144ec189ed4e..a486fba065e10 100644 --- a/test/legacy_test/test_norm_op.py +++ b/test/legacy_test/test_norm_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def l2_norm(x, axis, epsilon): @@ -113,7 +113,7 @@ def test_check_grad(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestNormOp7(TestNormOp): def init_dtype(self): @@ -121,12 +121,12 @@ def init_dtype(self): def test_check_output(self): self.check_output_with_place( - fluid.core.CUDAPlace(0), atol=5e-2, check_cinn=True + base.core.CUDAPlace(0), atol=5e-2, check_cinn=True ) def test_check_grad(self): self.check_grad_with_place( - fluid.core.CUDAPlace(0), + base.core.CUDAPlace(0), ['X'], 'Out', max_relative_error=0.05, @@ -203,7 +203,7 @@ def init_test_case(self): class API_NormTest(unittest.TestCase): def test_errors(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_norm_x_type(): data = paddle.static.data(name="x", shape=[3, 3], dtype="int64") diff --git a/test/legacy_test/test_normal.py b/test/legacy_test/test_normal.py index 6009580a8cde7..d03e311f8c1c3 100644 --- a/test/legacy_test/test_normal.py +++ b/test/legacy_test/test_normal.py @@ -33,7 +33,7 @@ def setUp(self): self.dtype = self.get_dtype() self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) diff --git a/test/legacy_test/test_normalization_wrapper.py b/test/legacy_test/test_normalization_wrapper.py index 4b58a4b21564b..dccade9412e5c 100644 --- a/test/legacy_test/test_normalization_wrapper.py +++ b/test/legacy_test/test_normalization_wrapper.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestNormalization(unittest.TestCase): @@ -43,7 +43,7 @@ def set_program(self, axis, epsilon): ) out = paddle.sum(l2_norm, axis=None) - fluid.backward.append_backward(loss=out) + base.backward.append_backward(loss=out) self.fetch_list = [l2_norm] def run_program(self): @@ -54,10 +54,10 @@ def run_program(self): for place in places: self.set_inputs(place) - exe = fluid.Executor(place) + exe = base.Executor(place) (output,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=self.inputs, fetch_list=self.fetch_list, return_numpy=True, @@ -67,7 +67,7 @@ def run_program(self): def set_inputs(self, place): """Set the randomly generated data to the test program.""" self.inputs = {} - tensor = fluid.Tensor() + tensor = base.Tensor() tensor.set(self.data, place) self.inputs[self.data_desc["name"]] = tensor diff --git a/test/legacy_test/test_normalize.py b/test/legacy_test/test_normalize.py index b9e34e599eb39..9c0c482f767b5 100644 --- a/test/legacy_test/test_normalize.py +++ b/test/legacy_test/test_normalize.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base def p_normalize(x, axis=1, p=2, epsilon=1e-12, keepdims=True): @@ -63,9 +63,9 @@ def run_static(self, use_gpu=False): result3 = F.normalize(x, name='aaa') result4 = F.normalize(x2, axis=0) - place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_gpu else base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) static_result = exe.run( feed={"input": self.input_np, "input2": self.input_np2}, fetch_list=[result0, result1, result2, result4], @@ -79,26 +79,26 @@ def run_static(self, use_gpu=False): self.assertRaises(ValueError, F.normalize, x2) def test_cpu(self): - paddle.disable_static(place=paddle.fluid.CPUPlace()) + paddle.disable_static(place=paddle.base.CPUPlace()) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static() def test_gpu(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return - paddle.disable_static(place=paddle.fluid.CUDAPlace(0)) + paddle.disable_static(place=paddle.base.CUDAPlace(0)) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static(use_gpu=True) def test_errors(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): # The size of input in Normalize should not be 0. def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_npair_loss_op.py b/test/legacy_test/test_npair_loss_op.py index b49cdf0547c47..79a003762e35a 100755 --- a/test/legacy_test/test_npair_loss_op.py +++ b/test/legacy_test/test_npair_loss_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard def npairloss(anchor, positive, labels, l2_reg=0.002): @@ -63,8 +63,8 @@ def test_npair_loss(self): num_data, feat_dim, num_classes = 18, 6, 3 place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) embeddings_anchor = np.random.rand(num_data, feat_dim).astype( np.float32 @@ -199,7 +199,7 @@ def test_labels_type(): class TestNpairLossZeroError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_anchor_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_number_count_op.py b/test/legacy_test/test_number_count_op.py index 78acc7948e15e..2ea68fd82d7ec 100644 --- a/test/legacy_test/test_number_count_op.py +++ b/test/legacy_test/test_number_count_op.py @@ -18,8 +18,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.models.moe import utils -from paddle.fluid import core def count(x, upper_num): diff --git a/test/legacy_test/test_numel_op.py b/test/legacy_test/test_numel_op.py index 57f07fa86981a..d04326971ed00 100644 --- a/test/legacy_test/test_numel_op.py +++ b/test/legacy_test/test_numel_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestNumelOp(OpTest): @@ -149,9 +149,9 @@ def init(self): class TestNumelAPI(unittest.TestCase): def test_numel_static(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): shape1 = [2, 1, 4, 5] shape2 = [1, 4, 5] x_1 = paddle.static.data(shape=shape1, dtype='int32', name='x_1') @@ -188,9 +188,9 @@ def test_numel_imperative(self): paddle.enable_static() def test_error(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): def test_x_type(): shape = [1, 4, 5] diff --git a/test/legacy_test/test_one_hot_v2_op.py b/test/legacy_test/test_one_hot_v2_op.py index 8bb28ed4e3f86..f8a7c3a59140e 100644 --- a/test/legacy_test/test_one_hot_v2_op.py +++ b/test/legacy_test/test_one_hot_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def one_hot_wrapper(x, depth_tensor, **keargs): @@ -141,9 +141,9 @@ def test_api_with_dygraph(self): label = np.array( [np.random.randint(0, depth - 1) for i in range(6)] ).reshape([6, 1]) - with fluid.dygraph.guard(): + with base.dygraph.guard(): one_hot_label = paddle.nn.functional.one_hot( - fluid.dygraph.to_variable(label), depth + base.dygraph.to_variable(label), depth ) one_hot_label = paddle.nn.functional.one_hot( paddle.to_tensor(label), depth @@ -154,13 +154,13 @@ def _run(self, depth): label.desc.set_need_check_feed(False) one_hot_label = paddle.nn.functional.one_hot(x=label, num_classes=depth) - place = fluid.CPUPlace() + place = base.CPUPlace() label_data = np.array( [np.random.randint(0, 10 - 1) for i in range(6)] ).reshape([6, 1]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={ 'label': label_data, @@ -172,7 +172,7 @@ def _run(self, depth): class BadInputTestOnehotV2(unittest.TestCase): def test_error(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_bad_x(): label = paddle.static.data( diff --git a/test/legacy_test/test_ones_like.py b/test/legacy_test/test_ones_like.py index fad132e6093cc..17048414c3896 100644 --- a/test/legacy_test/test_ones_like.py +++ b/test/legacy_test/test_ones_like.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import _C_ops, fluid, ones_like -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle import _C_ops, base, ones_like +from paddle.base import Program, core, program_guard +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestOnesLikeAPIError(unittest.TestCase): @@ -45,11 +45,11 @@ def test_api(self): out5 = ones_like(x, 'int64') place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) outs = exe.run( train_program, feed={'X': np.ones(shape).astype('float32')}, @@ -67,9 +67,9 @@ class TestOnesAPI(unittest.TestCase): def test_api(self): shape = [3, 4] place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) paddle.disable_static(place) diff --git a/test/legacy_test/test_ones_op.py b/test/legacy_test/test_ones_op.py index 22e3d3d737d44..4dff895aceb8a 100644 --- a/test/legacy_test/test_ones_op.py +++ b/test/legacy_test/test_ones_op.py @@ -45,7 +45,7 @@ def test_paddle_ones(self): expected_result = np.ones(10, dtype="int64") self.assertEqual((result == expected_result).all(), True) - def test_fluid_ones(self): + def test_base_ones(self): with paddle.static.program_guard(paddle.static.Program()): ones = paddle.ones(shape=[10], dtype="int64") place = paddle.CPUPlace() diff --git a/test/legacy_test/test_op_function_generator.py b/test/legacy_test/test_op_function_generator.py index 0ad5a2e4a6692..e5e3d3957a739 100644 --- a/test/legacy_test/test_op_function_generator.py +++ b/test/legacy_test/test_op_function_generator.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle import _legacy_C_ops, fluid +from paddle import _legacy_C_ops, base class TestTracedLayer(paddle.nn.Layer): @@ -36,11 +36,11 @@ def setUp(self): self.array = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) def test_elementwise_add(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) b = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) - x = fluid.dygraph.to_variable(a) - y = fluid.dygraph.to_variable(b) + x = base.dygraph.to_variable(a) + y = base.dygraph.to_variable(b) x.stop_gradient = False res1 = paddle.add(x, y) @@ -49,11 +49,11 @@ def test_elementwise_add(self): np.testing.assert_array_equal(res1.numpy(), res2.numpy()) def test_elementwise_mul(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) b = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) - x = fluid.dygraph.to_variable(a) - y = fluid.dygraph.to_variable(b) + x = base.dygraph.to_variable(a) + y = base.dygraph.to_variable(b) res1 = paddle.multiply(x, y) res2 = _legacy_C_ops.elementwise_mul(x, y) @@ -61,9 +61,9 @@ def test_elementwise_mul(self): np.testing.assert_array_equal(res1.numpy(), res2.numpy()) def test_relu(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(-1, 1, self.shape).astype(self.dtype) - x = fluid.dygraph.to_variable(a) + x = base.dygraph.to_variable(a) res1 = F.relu(x) res2 = _legacy_C_ops.relu(x) @@ -71,11 +71,11 @@ def test_relu(self): np.testing.assert_array_equal(res1.numpy(), res2.numpy()) def test_trace_backward(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): a = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) b = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) - x = fluid.dygraph.to_variable(a) - y = fluid.dygraph.to_variable(b) + x = base.dygraph.to_variable(a) + y = base.dygraph.to_variable(b) x.stop_gradient = False y.stop_gradient = False x.retain_grads() diff --git a/test/legacy_test/test_op_name_conflict.py b/test/legacy_test/test_op_name_conflict.py index 5114e81c24c37..fbb717eda9609 100644 --- a/test/legacy_test/test_op_name_conflict.py +++ b/test/legacy_test/test_op_name_conflict.py @@ -17,24 +17,24 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestOpNameConflict(unittest.TestCase): def test_conflict(self): paddle.enable_static() - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): x = paddle.static.data(name="x", shape=[1], dtype='float32') y = paddle.static.data(name="y", shape=[1], dtype='float32') m = paddle.log2(x, name="log2") n = paddle.log2(y, name="log2") - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) m_v, n_v = exe.run( feed={ "x": np.ones((1), "float32") * 1, diff --git a/test/legacy_test/test_op_support_gpu.py b/test/legacy_test/test_op_support_gpu.py index 88bb417e0911c..46561b4014df2 100644 --- a/test/legacy_test/test_op_support_gpu.py +++ b/test/legacy_test/test_op_support_gpu.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core class TestOpSupportGPU(unittest.TestCase): diff --git a/test/legacy_test/test_op_version.py b/test/legacy_test/test_op_version.py index 5fadf6b24f4c4..56953aaf4a545 100644 --- a/test/legacy_test/test_op_version.py +++ b/test/legacy_test/test_op_version.py @@ -14,7 +14,7 @@ import unittest -from paddle import fluid, utils +from paddle import base, utils class OpLastCheckpointCheckerTest(unittest.TestCase): @@ -24,7 +24,7 @@ def __init__(self, methodName='runTest'): self.fake_op = 'for_pybind_test__' def test_op_attr_info(self): - update_type = fluid.core.OpUpdateType.kNewAttr + update_type = base.core.OpUpdateType.kNewAttr info_list = self.checker.filter_updates( self.fake_op, update_type, 'STRINGS' ) @@ -34,7 +34,7 @@ def test_op_attr_info(self): self.assertEqual(info_list[0].remark(), 'std::vector') def test_op_input_output_info(self): - update_type = fluid.core.OpUpdateType.kNewInput + update_type = base.core.OpUpdateType.kNewInput info_list = self.checker.filter_updates( self.fake_op, update_type, 'NewInput' ) @@ -43,7 +43,7 @@ def test_op_input_output_info(self): self.assertEqual(info_list[0].remark(), 'NewInput_') def test_op_bug_fix_info(self): - update_type = fluid.core.OpUpdateType.kBugfixWithBehaviorChanged + update_type = base.core.OpUpdateType.kBugfixWithBehaviorChanged info_list = self.checker.filter_updates(self.fake_op, update_type) self.assertTrue(info_list) self.assertEqual(info_list[0].remark(), 'BugfixWithBehaviorChanged_') @@ -52,7 +52,7 @@ def test_op_bug_fix_info(self): class OpVersionTest(unittest.TestCase): def __init__(self, methodName='runTest'): super().__init__(methodName) - self.vmap = fluid.core.get_op_version_map() + self.vmap = base.core.get_op_version_map() self.fake_op = 'for_pybind_test__' def test_checkpoints(self): diff --git a/test/legacy_test/test_operator.py b/test/legacy_test/test_operator.py index da08e898ed82e..19f75afd74757 100644 --- a/test/legacy_test/test_operator.py +++ b/test/legacy_test/test_operator.py @@ -17,7 +17,7 @@ import numpy as np import op -from paddle.fluid.proto import framework_pb2 +from paddle.base.proto import framework_pb2 class TestGetAllProtos(unittest.TestCase): diff --git a/test/legacy_test/test_operator_desc.py b/test/legacy_test/test_operator_desc.py index e79ef1e951eb5..d0ee95854cd94 100644 --- a/test/legacy_test/test_operator_desc.py +++ b/test/legacy_test/test_operator_desc.py @@ -14,8 +14,8 @@ import unittest -from paddle.fluid import core -from paddle.fluid.framework import Program, default_startup_program +from paddle.base import core +from paddle.base.framework import Program, default_startup_program main_program = default_startup_program() diff --git a/test/legacy_test/test_optimizer.py b/test/legacy_test/test_optimizer.py index 568d50eab22bd..c7e6d21124176 100644 --- a/test/legacy_test/test_optimizer.py +++ b/test/legacy_test/test_optimizer.py @@ -20,10 +20,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import ( +from paddle import base +from paddle.base import core, framework +from paddle.base.backward import append_backward +from paddle.base.framework import ( Program, convert_np_dtype_to_dtype_, program_guard, @@ -830,8 +830,8 @@ def mlp(input_x, input_y): main_program = Program() startup_program = Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): with program_guard(main_program, startup_program): input_x = paddle.static.data( name="x", shape=[-1, 3], dtype='float32' @@ -845,13 +845,13 @@ def mlp(input_x, input_y): sgd._set_checkpoints([prediction]) sgd.minimize(cost) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) feed_data = gen_data() drop_vec = exe.run( feed=feed_data, - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[ "dropout_with_seed_cpu.tmp_1", "dropout_with_seed_cpu.tmp_1.subprog_0", @@ -895,8 +895,8 @@ def mlp(input_x, input_y): main_program = Program() startup_program = Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): with program_guard(main_program, startup_program): input_x = paddle.static.data( name="x", shape=[-1, 3], dtype='float32' @@ -910,13 +910,13 @@ def mlp(input_x, input_y): sgd._set_checkpoints([prediction]) sgd.minimize(cost) - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) + exe = base.Executor(place) + exe.run(base.default_startup_program()) feed_data = gen_data() drop_vec = exe.run( feed=feed_data, - program=fluid.default_main_program(), + program=base.default_main_program(), fetch_list=[ "dropout_with_seed_gpu.tmp_1", "dropout_with_seed_gpu.tmp_1.subprog_0", @@ -1028,7 +1028,7 @@ def __init__(self, dtype): def forward(self, x): return x * self._w + self._b - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): model = MyLayer(dtype) x = paddle.rand([10, 2, 3], dtype=dtype) loss = model(x) @@ -1138,7 +1138,7 @@ def __len__(self): def test_with_state_dict(self): if core.is_compiled_with_cuda(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): out_use_state_dict = self.check_with_opt_state_dict( use_save_load=True ) diff --git a/test/legacy_test/test_optimizer_for_varbase.py b/test/legacy_test/test_optimizer_for_varbase.py index a8e4b03341837..abeebc2ec3e04 100644 --- a/test/legacy_test/test_optimizer_for_varbase.py +++ b/test/legacy_test/test_optimizer_for_varbase.py @@ -88,7 +88,7 @@ def test_optimizer_with_varbase_input(self): optimizer.Adam(learning_rate=self.lr, parameters=x) def test_create_param_lr_with_1_for_coverage(self): - x = paddle.fluid.framework.EagerParamBase( + x = paddle.base.framework.EagerParamBase( dtype="float32", shape=[5, 10], lod_level=0, @@ -97,7 +97,7 @@ def test_create_param_lr_with_1_for_coverage(self): ) x.value().get_tensor().set( np.random.random((5, 10)).astype('float32'), - paddle.fluid.framework._current_expected_place(), + paddle.base.framework._current_expected_place(), ) y = paddle.ones([5, 10]) @@ -107,7 +107,7 @@ def test_create_param_lr_with_1_for_coverage(self): opt.step() def test_create_param_lr_with_no_1_value_for_coverage(self): - x = paddle.fluid.framework.EagerParamBase( + x = paddle.base.framework.EagerParamBase( dtype="float32", shape=[5, 10], lod_level=0, @@ -116,7 +116,7 @@ def test_create_param_lr_with_no_1_value_for_coverage(self): ) x.value().get_tensor().set( np.random.random((5, 10)).astype('float32'), - paddle.fluid.framework._current_expected_place(), + paddle.base.framework._current_expected_place(), ) y = paddle.ones([5, 10]) diff --git a/test/legacy_test/test_optimizer_grad.py b/test/legacy_test/test_optimizer_grad.py index 842c5d0766326..dad2e783124ab 100644 --- a/test/legacy_test/test_optimizer_grad.py +++ b/test/legacy_test/test_optimizer_grad.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.backward import _append_grad_suffix_ +from paddle import base +from paddle.base.backward import _append_grad_suffix_ paddle.enable_static() @@ -79,20 +79,20 @@ def build_net(self, cond_i, use_bf16=False): param_x = paddle.create_parameter( dtype="float32", shape=self.shape, - attr=fluid.ParamAttr(learning_rate=self.param_lr, name="param_x"), + attr=base.ParamAttr(learning_rate=self.param_lr, name="param_x"), default_initializer=paddle.nn.initializer.Assign(self.x), ) param_y = paddle.create_parameter( dtype="float32", shape=self.shape, - attr=fluid.ParamAttr(learning_rate=self.param_lr, name="param_y"), + attr=base.ParamAttr(learning_rate=self.param_lr, name="param_y"), default_initializer=paddle.nn.initializer.Assign(self.y), ) param_z = paddle.create_parameter( dtype="float32", shape=self.shape, - attr=fluid.ParamAttr(learning_rate=self.param_lr, name="param_z"), + attr=base.ParamAttr(learning_rate=self.param_lr, name="param_z"), default_initializer=paddle.nn.initializer.Assign(self.z), ) @@ -193,9 +193,9 @@ def _check_grads(self, use_bf16=False): """ main logic code to check the validity of apply_optimize. """ - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) # test on CPU and GPU for place in places: for param_lr in self.param_lr: @@ -206,9 +206,9 @@ def _check_grads(self, use_bf16=False): ) self._init_param_attr() - main_program = fluid.Program() - init_program = fluid.Program() - with fluid.program_guard(main_program, init_program): + main_program = base.Program() + init_program = base.Program() + with base.program_guard(main_program, init_program): # reset optimizer._accumulators to avoid duplicate name in loop. self.optimizer._accumulators = defaultdict( lambda: {} @@ -223,7 +223,7 @@ def _check_grads(self, use_bf16=False): if use_bf16: self.optimizer = decorated_optimizer - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(init_program) if use_bf16: self.optimizer.amp_init(exe.place) @@ -245,7 +245,7 @@ def _check_grads(self, use_bf16=False): @unittest.skipIf( - not fluid.core.supports_bfloat16(), "place does not support BF16 evaluation" + not base.core.supports_bfloat16(), "place does not support BF16 evaluation" ) class TestSGDOptimizer(TestOptimizer): def test_optimizer_multiblock_except(self): diff --git a/test/legacy_test/test_optimizer_in_control_flow.py b/test/legacy_test/test_optimizer_in_control_flow.py index 5e1fca418b7fe..84adcf75cfef8 100644 --- a/test/legacy_test/test_optimizer_in_control_flow.py +++ b/test/legacy_test/test_optimizer_in_control_flow.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.framework import Program, program_guard BATCH_SIZE = 1 INPUT_SIZE = 784 @@ -47,10 +47,10 @@ def double_fc_net(image): image, size=FC_SIZE, activation='relu', - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.99) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.5) ), name="hidden", @@ -60,10 +60,10 @@ def double_fc_net(image): hidden, size=CLASS_NUM, activation='softmax', - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.2) ), - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.8) ), name="prediction", @@ -121,8 +121,8 @@ def fn_2(opt, avg_loss=None, pred=None, label=None): lambda: fn_2(sgd, avg_loss_2), ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) for epoch in range(EPOCH_NUM): @@ -173,10 +173,10 @@ def forward(self, inputs): def dynamic(train_data, use_cuda=False, use_parallel_exe=False): - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - with fluid.dygraph.guard(place): - fluid.default_startup_program().random_seed = SEED - fluid.default_main_program().random_seed = SEED + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + with base.dygraph.guard(place): + base.default_startup_program().random_seed = SEED + base.default_main_program().random_seed = SEED dy_layer = DygraphLayer() adam = paddle.optimizer.Adam( learning_rate=LR, parameters=dy_layer.parameters() @@ -187,8 +187,8 @@ def dynamic(train_data, use_cuda=False, use_parallel_exe=False): for epoch in range(EPOCH_NUM): image_data, label = train_data[epoch] - var_input = fluid.dygraph.to_variable(image_data) - var_label = fluid.dygraph.to_variable(label) + var_input = base.dygraph.to_variable(image_data) + var_label = base.dygraph.to_variable(label) hidden, prediction = dy_layer(var_input) if epoch % 2 == 0: diff --git a/test/legacy_test/test_overlap_add_op.py b/test/legacy_test/test_overlap_add_op.py index 98d4ce10aaabb..90b10c6b9a6c0 100644 --- a/test/legacy_test/test_overlap_add_op.py +++ b/test/legacy_test/test_overlap_add_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def overlap_add(x, hop_length, axis=-1): diff --git a/test/legacy_test/test_pad3d_op.py b/test/legacy_test/test_pad3d_op.py index 7e6cefcc3132d..cbd8d142e6467 100644 --- a/test/legacy_test/test_pad3d_op.py +++ b/test/legacy_test/test_pad3d_op.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import ( +from paddle.base import ( Executor, Program, core, diff --git a/test/legacy_test/test_pad_op.py b/test/legacy_test/test_pad_op.py index 3cec8719e137c..f33a317e944c6 100644 --- a/test/legacy_test/test_pad_op.py +++ b/test/legacy_test/test_pad_op.py @@ -20,7 +20,7 @@ from test_attribute_var import UnittestBase import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard def pad_wrapper(x, paddings, pad_value): @@ -115,7 +115,7 @@ def test_check_grad_normal(self): class TestPadOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program(), Program()): input_data = np.random.random((2, 2)).astype("float32") @@ -136,7 +136,7 @@ def init_info(self): self.save_path = os.path.join(self.temp_dir.name, self.path_prefix()) def test_static(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): main_prog = Program() starup_prog = Program() with program_guard(main_prog, starup_prog): @@ -196,7 +196,7 @@ def call_func(self, x): class TestPaddingValueTensor3(unittest.TestCase): def test_static(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): np_x = np.random.random((16, 16)).astype('float32') main_prog = Program() starup_prog = Program() diff --git a/test/legacy_test/test_paddle_imperative_double_grad.py b/test/legacy_test/test_paddle_imperative_double_grad.py index 99bb5bf714413..32e65fff99f54 100644 --- a/test/legacy_test/test_paddle_imperative_double_grad.py +++ b/test/legacy_test/test_paddle_imperative_double_grad.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid.wrapped_decorator import wrap_decorator +from paddle import base +from paddle.base.wrapped_decorator import wrap_decorator def _dygraph_guard_(func): @@ -28,7 +28,7 @@ def __impl__(*args, **kwargs): if paddle.in_dynamic_mode(): return func(*args, **kwargs) else: - with fluid.dygraph.guard(): + with base.dygraph.guard(): return func(*args, **kwargs) return __impl__ @@ -39,7 +39,7 @@ def __impl__(*args, **kwargs): def random_var(size, low=-1, high=1, dtype='float32'): x_np = np.random.uniform(low=low, high=high, size=size).astype(dtype) - return fluid.dygraph.to_variable(x_np) + return base.dygraph.to_variable(x_np) class TestDygraphDoubleGrad(TestCase): @@ -152,7 +152,7 @@ def test_none_one_initial_gradient(self): ) np.random.shuffle(x_np) - x = fluid.dygraph.to_variable(x_np) + x = base.dygraph.to_variable(x_np) x.stop_gradient = False alpha = 0.2 diff --git a/test/legacy_test/test_paddle_multiprocessing.py b/test/legacy_test/test_paddle_multiprocessing.py index 9117a2d86a09d..f3b74596b3326 100644 --- a/test/legacy_test/test_paddle_multiprocessing.py +++ b/test/legacy_test/test_paddle_multiprocessing.py @@ -198,7 +198,7 @@ def test_pass_empty(self): class TestMultiprocessingGpu(TestMultiprocessingBase): @unittest.skipIf( - not paddle.fluid.core.is_compiled_with_cuda(), + not paddle.base.core.is_compiled_with_cuda(), "core is not compiled with CUDA", ) def func_test_pass_tensor(self): diff --git a/test/legacy_test/test_paddle_save_load.py b/test/legacy_test/test_paddle_save_load.py index be7eb0febeb62..cb2e7e47afe86 100644 --- a/test/legacy_test/test_paddle_save_load.py +++ b/test/legacy_test/test_paddle_save_load.py @@ -22,8 +22,8 @@ import paddle import paddle.optimizer as opt -from paddle import fluid, nn -from paddle.fluid import framework +from paddle import base, nn +from paddle.base import framework from paddle.optimizer import Adam from paddle.optimizer.lr import LRScheduler @@ -168,7 +168,7 @@ def tearDown(self): def set_zero(self, prog, place, scope=None): if scope is None: - scope = fluid.global_scope() + scope = base.global_scope() for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: ten = scope.find_var(var.name).get_tensor() @@ -225,31 +225,31 @@ def test_replace_static_save_load(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_map[var.name] = t path = os.path.join( self.temp_dir.name, "test_replace_static_save_load", "model" ) - # paddle.save, legacy paddle.fluid.load + # paddle.save, legacy paddle.base.load self.replace_static_save(prog, path) self.set_zero(prog, place) paddle.static.load(prog, path) for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, np.array(base_t)) - # legacy paddle.fluid.save, paddle.load + # legacy paddle.base.save, paddle.load paddle.static.save(prog, path) self.set_zero(prog, place) self.replace_static_load(prog, path) for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -257,13 +257,13 @@ def test_replace_static_save_load(self): path_vars = 'test_replace_save_load_return_tensor_static/model' for var in prog.list_vars(): if var.persistable: - tensor = var.get_value(fluid.global_scope()) + tensor = var.get_value(base.global_scope()) paddle.save( tensor, os.path.join(self.temp_dir.name, path_vars, var.name), ) with self.assertRaises(TypeError): - var.get_value('fluid.global_scope()') + var.get_value('base.global_scope()') with self.assertRaises(ValueError): x.get_value() with self.assertRaises(TypeError): @@ -285,7 +285,7 @@ def test_replace_static_save_load(self): ) var.set_value(tensor) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -360,7 +360,7 @@ def test_single_pickle_var_dygraph(self): self.assertTrue( isinstance( t_dygraph, - paddle.fluid.core.eager.Tensor, + paddle.base.core.eager.Tensor, ) ) np.testing.assert_array_equal(tensor.numpy(), np_dygraph) @@ -368,7 +368,7 @@ def test_single_pickle_var_dygraph(self): paddle.enable_static() lod_static = paddle.load(path) np_static = paddle.load(path, return_numpy=True) - self.assertTrue(isinstance(lod_static, paddle.fluid.core.LoDTensor)) + self.assertTrue(isinstance(lod_static, paddle.base.core.LoDTensor)) np.testing.assert_array_equal(tensor.numpy(), np_static) np.testing.assert_array_equal(tensor.numpy(), np.array(lod_static)) @@ -383,9 +383,9 @@ def test_single_pickle_var_static(self): z = paddle.static.nn.fc(x, 128) loss = paddle.mean(z) place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) exe = paddle.static.Executor(place) exe.run(paddle.static.default_startup_program()) @@ -394,7 +394,7 @@ def test_single_pickle_var_static(self): if list(var.shape) == [IMAGE_SIZE, 128]: tensor = var.get_value() break - scope = fluid.global_scope() + scope = base.global_scope() origin_tensor = np.array(tensor) path = os.path.join( self.temp_dir.name, 'test_single_pickle_var_static/var' @@ -438,9 +438,9 @@ def test_dygraph_save_static_load(self): y_static = layer(data) program = paddle.static.default_main_program() place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) exe = paddle.static.Executor(paddle.CPUPlace()) exe.run(paddle.static.default_startup_program()) @@ -570,9 +570,7 @@ def test_save_load_complex_object_dygraph_save(self): ) self.assertTrue(load_tensor2['epoch'] == 123) - self.assertTrue( - isinstance(load_tensor3[0], paddle.fluid.core.LoDTensor) - ) + self.assertTrue(isinstance(load_tensor3[0], paddle.base.core.LoDTensor)) np.testing.assert_array_equal( np.array(load_tensor3[0]), obj3[0].numpy() ) @@ -582,7 +580,7 @@ def test_save_load_complex_object_dygraph_save(self): self.assertTrue( isinstance( load_tensor3[2]["state_dict"][k], - paddle.fluid.core.LoDTensor, + paddle.base.core.LoDTensor, ) ) np.testing.assert_array_equal( @@ -592,14 +590,14 @@ def test_save_load_complex_object_dygraph_save(self): for k, v in state_dict.items(): self.assertTrue( isinstance( - load_tensor3[2]["opt"][k], paddle.fluid.core.LoDTensor + load_tensor3[2]["opt"][k], paddle.base.core.LoDTensor ) ) np.testing.assert_array_equal( np.array(load_tensor3[2]['opt'][k]), v.numpy() ) - self.assertTrue(load_tensor4[0], paddle.fluid.core.LoDTensor) + self.assertTrue(load_tensor4[0], paddle.base.core.LoDTensor) np.testing.assert_array_equal(np.array(load_tensor4[0]), obj4[0]) load_array1 = paddle.load(path1, return_numpy=True) @@ -641,9 +639,9 @@ def test_save_load_complex_object_static_save(self): z = paddle.static.nn.fc(z, 128, bias_attr=False) loss = paddle.mean(z) place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) prog = paddle.static.default_main_program() exe = paddle.static.Executor(place) @@ -705,15 +703,15 @@ def test_save_load_complex_object_static_save(self): ) self.assertTrue(load_tensor2['epoch'] == 123) - self.assertTrue(isinstance(load_tensor3[0], fluid.core.LoDTensor)) + self.assertTrue(isinstance(load_tensor3[0], base.core.LoDTensor)) np.testing.assert_array_equal(np.array(load_tensor3[0]), obj3[0]) - self.assertTrue(isinstance(load_tensor3[1], fluid.core.LoDTensor)) + self.assertTrue(isinstance(load_tensor3[1], base.core.LoDTensor)) np.testing.assert_array_equal(np.array(load_tensor3[1]), obj3[1]) for k, v in state_dict.items(): self.assertTrue( isinstance( - load_tensor3[2]["state_dict"][k], fluid.core.LoDTensor + load_tensor3[2]["state_dict"][k], base.core.LoDTensor ) ) np.testing.assert_array_equal( @@ -722,13 +720,13 @@ def test_save_load_complex_object_static_save(self): for k, v in state_dict.items(): self.assertTrue( - isinstance(load_tensor3[2]["opt"][k], fluid.core.LoDTensor) + isinstance(load_tensor3[2]["opt"][k], base.core.LoDTensor) ) np.testing.assert_array_equal( np.array(load_tensor3[2]['opt'][k]), np.array(v) ) - self.assertTrue(isinstance(load_tensor4[0], fluid.core.LoDTensor)) + self.assertTrue(isinstance(load_tensor4[0], base.core.LoDTensor)) np.testing.assert_array_equal(np.array(load_tensor4[0]), obj4[0]) load_array1 = paddle.load(path1, return_numpy=True) @@ -788,14 +786,14 @@ def test_save_load_complex_object_static_save(self): self.assertTrue( isinstance( load_tensor3[0], - fluid.core.eager.Tensor, + base.core.eager.Tensor, ) ) np.testing.assert_array_equal(load_tensor3[0].numpy(), obj3[0]) self.assertTrue( isinstance( load_tensor3[1], - fluid.core.eager.Tensor, + base.core.eager.Tensor, ) ) np.testing.assert_array_equal(load_tensor3[1].numpy(), obj3[1]) @@ -804,7 +802,7 @@ def test_save_load_complex_object_static_save(self): self.assertTrue( isinstance( load_tensor3[2]["state_dict"][k], - fluid.core.eager.Tensor, + base.core.eager.Tensor, ) ) np.testing.assert_array_equal( @@ -815,7 +813,7 @@ def test_save_load_complex_object_static_save(self): self.assertTrue( isinstance( load_tensor3[2]["opt"][k], - fluid.core.eager.Tensor, + base.core.eager.Tensor, ) ) np.testing.assert_array_equal( @@ -825,7 +823,7 @@ def test_save_load_complex_object_static_save(self): self.assertTrue( isinstance( load_tensor4[0], - fluid.core.eager.Tensor, + base.core.eager.Tensor, ) ) np.testing.assert_array_equal(load_tensor4[0].numpy(), obj4[0]) @@ -874,8 +872,8 @@ def test_varbase_binary_var(self): load_tensor = paddle.load(path, return_numpy=False) origin_array = varbase.numpy() load_tensor_array = load_tensor.numpy() - if paddle.fluid.core.is_compiled_with_cuda(): - fluid.core._cuda_synchronize(paddle.CUDAPlace(0)) + if paddle.base.core.is_compiled_with_cuda(): + base.core._cuda_synchronize(paddle.CUDAPlace(0)) np.testing.assert_array_equal(origin_array, load_array) np.testing.assert_array_equal(origin_array, load_tensor_array) @@ -916,9 +914,9 @@ def test_static_save_to_memory(self): z = paddle.static.nn.fc(z, 128, bias_attr=False) loss = paddle.mean(z) place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) prog = paddle.static.default_main_program() exe = paddle.static.Executor(place) diff --git a/test/legacy_test/test_paddle_save_load_binary.py b/test/legacy_test/test_paddle_save_load_binary.py index a4f3709c556be..df7304cf1d19e 100644 --- a/test/legacy_test/test_paddle_save_load_binary.py +++ b/test/legacy_test/test_paddle_save_load_binary.py @@ -22,8 +22,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework IMAGE_SIZE = 784 @@ -39,7 +39,7 @@ def tearDown(self): def set_zero(self, prog, place, scope=None): if scope is None: - scope = fluid.global_scope() + scope = base.global_scope() for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: ten = scope.find_var(var.name).get_tensor() @@ -81,9 +81,9 @@ def test_replace_save_load_vars(self): z = paddle.static.nn.fc(z, 128, bias_attr=False) loss = paddle.mean(z) place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) exe = paddle.static.Executor(place) exe.run(paddle.static.default_startup_program()) @@ -92,7 +92,7 @@ def test_replace_save_load_vars(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -114,7 +114,7 @@ def test_replace_save_load_vars(self): for var in prog.list_vars(): if var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] @@ -131,7 +131,7 @@ def test_replace_save_load_vars(self): for var in prog.list_vars(): if var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] @@ -149,15 +149,15 @@ def test_save_load_lod_tensor(self): OUTPUT_NUM, name='fc_vars', ) - prog = fluid.default_main_program() + prog = base.default_main_program() place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) - exe = fluid.Executor(place) + exe = base.Executor(place) prog = paddle.static.default_main_program() - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) dirname = os.path.join( self.temp_dir.name, 'test_save_load_lod_tensor1/tensor_' @@ -178,7 +178,7 @@ def test_save_load_lod_tensor(self): is_zeros = np.array(var.get_value()) loaded_tensor = paddle.load(dirname + 'fc_vars.w_0') - self.assertTrue(isinstance(loaded_tensor, fluid.core.LoDTensor)) + self.assertTrue(isinstance(loaded_tensor, base.core.LoDTensor)) self.assertTrue( list(loaded_tensor.shape()) == [IMAGE_SIZE, OUTPUT_NUM] ) @@ -199,11 +199,11 @@ def test_save_load_lod_tensor(self): paddle.load(path) with self.assertRaises(ValueError): - temp_lod = fluid.core.LoDTensor() + temp_lod = base.core.LoDTensor() paddle.save(temp_lod, path, use_binary_format=True) with self.assertRaises(RuntimeError): - fluid.core.save_lod_tensor( + base.core.save_lod_tensor( temp_lod, os.path.join( self.temp_dir.name, @@ -212,7 +212,7 @@ def test_save_load_lod_tensor(self): ) with self.assertRaises(RuntimeError): - fluid.core.load_lod_tensor( + base.core.load_lod_tensor( temp_lod, os.path.join( self.temp_dir.name, @@ -237,14 +237,14 @@ def test_save_load_lod_tensor(self): def test_save_load_selected_rows(self): paddle.enable_static() place = ( - fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else base.CUDAPlace(0) ) height = 10 rows = [0, 4, 7] row_numel = 12 - selected_rows = fluid.core.SelectedRows(rows, height) + selected_rows = base.core.SelectedRows(rows, height) path = os.path.join( self.temp_dir.name, 'test_paddle_save_load_selected_rows/sr.pdsr' ) @@ -259,13 +259,13 @@ def test_save_load_selected_rows(self): paddle.save(selected_rows, path, use_binary_format=True) load_sr = paddle.load(path) - self.assertTrue(isinstance(load_sr, fluid.core.SelectedRows)) + self.assertTrue(isinstance(load_sr, base.core.SelectedRows)) self.assertTrue(list(load_sr.rows()) == rows) self.assertTrue(load_sr.height() == height) np.testing.assert_array_equal(np.array(load_sr.get_tensor()), np_array) with self.assertRaises(RuntimeError): - fluid.core.save_selected_rows( + base.core.save_selected_rows( selected_rows, os.path.join( self.temp_dir.name, @@ -273,7 +273,7 @@ def test_save_load_selected_rows(self): ), ) with self.assertRaises(RuntimeError): - fluid.core.load_selected_rows( + base.core.load_selected_rows( selected_rows, os.path.join( self.temp_dir.name, @@ -288,7 +288,7 @@ def test_save_load_selected_rows(self): # load from memory selected_rows_mem = paddle.load(byio) to_array_mem = np.array(selected_rows_mem) - self.assertTrue(isinstance(selected_rows_mem, fluid.core.SelectedRows)) + self.assertTrue(isinstance(selected_rows_mem, base.core.SelectedRows)) self.assertTrue(list(selected_rows_mem.rows()) == rows) self.assertTrue(selected_rows_mem.height() == height) np.testing.assert_array_equal( diff --git a/test/legacy_test/test_paddlescience.py b/test/legacy_test/test_paddlescience.py index e3fbe0c25dbe7..06c5abe80df89 100644 --- a/test/legacy_test/test_paddlescience.py +++ b/test/legacy_test/test_paddlescience.py @@ -15,10 +15,10 @@ import unittest import paddle -from paddle import fluid, jit, nn +from paddle import base, jit, nn paddle.jit.enable_to_static(True) -fluid.core._set_prim_all_enabled(True) +base.core._set_prim_all_enabled(True) x = paddle.randn([4, 1]) y = paddle.randn([4, 1]) diff --git a/test/legacy_test/test_pairwise_distance.py b/test/legacy_test/test_pairwise_distance.py index e89a713282db2..2f9199f48c04a 100644 --- a/test/legacy_test/test_pairwise_distance.py +++ b/test/legacy_test/test_pairwise_distance.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def np_pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False): @@ -48,9 +48,9 @@ def test_static( prog = paddle.static.Program() startup_prog = paddle.static.Program() place = ( - fluid.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if paddle.base.core.is_compiled_with_cuda() + else base.CPUPlace() ) paddle.enable_static() with paddle.static.program_guard(prog, startup_prog): diff --git a/test/legacy_test/test_parallel_dygraph_dataparallel.py b/test/legacy_test/test_parallel_dygraph_dataparallel.py index aedf97b168381..de3160e9c6f9c 100644 --- a/test/legacy_test/test_parallel_dygraph_dataparallel.py +++ b/test/legacy_test/test_parallel_dygraph_dataparallel.py @@ -18,7 +18,7 @@ import time import unittest -from paddle import fluid +from paddle import base from paddle.distributed.utils.launch_utils import ( TrainerProc, find_free_ports, @@ -160,8 +160,8 @@ def run_mnist_2gpu( allocator_strategy="auto_growth", ): if ( - not fluid.core.is_compiled_with_cuda() - or fluid.core.get_cuda_device_count() == 0 + not base.core.is_compiled_with_cuda() + or base.core.get_cuda_device_count() == 0 ): return diff --git a/test/legacy_test/test_parallel_dygraph_dataparallel_cpuonly.py b/test/legacy_test/test_parallel_dygraph_dataparallel_cpuonly.py index b2f7831489100..5a944284414bf 100644 --- a/test/legacy_test/test_parallel_dygraph_dataparallel_cpuonly.py +++ b/test/legacy_test/test_parallel_dygraph_dataparallel_cpuonly.py @@ -103,8 +103,8 @@ def start_local_trainers( class TestMultipleGpus(unittest.TestCase): def run_mnist_2gpu(self, target_file_name): - # if not fluid.core.is_compiled_with_cuda( - # ) or fluid.core.get_cuda_device_count() == 0: + # if not base.core.is_compiled_with_cuda( + # ) or base.core.get_cuda_device_count() == 0: # return selected_gpus = get_gpus('0,1') diff --git a/test/legacy_test/test_parallel_dygraph_transformer_gloo.py b/test/legacy_test/test_parallel_dygraph_transformer_gloo.py index 7dee57cb29ff0..714da4b720696 100644 --- a/test/legacy_test/test_parallel_dygraph_transformer_gloo.py +++ b/test/legacy_test/test_parallel_dygraph_transformer_gloo.py @@ -17,7 +17,7 @@ from test_dist_base import TestDistBase -from paddle import fluid +from paddle import base flag_name = os.path.splitext(__file__)[0] @@ -46,7 +46,7 @@ def _setup_config(self): self._find_unused_parameters = False def test_transformer(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "parallel_dygraph_transformer.py", delta=1e-5, diff --git a/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_cpu.py b/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_cpu.py index 8b5e892364c0c..75d3d85e20e5b 100644 --- a/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_cpu.py +++ b/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_cpu.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import fluid +from paddle import base -fluid.core._set_fuse_parameter_group_size(3) -fluid.core._set_fuse_parameter_memory_size(131072) +base.core._set_fuse_parameter_group_size(3) +base.core._set_fuse_parameter_memory_size(131072) import unittest from functools import partial diff --git a/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_gpu.py b/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_gpu.py index 0a39002275edc..752538efaa059 100644 --- a/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_gpu.py +++ b/test/legacy_test/test_parallel_executor_seresnext_with_fuse_all_reduce_gpu.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle import fluid +from paddle import base -fluid.core._set_fuse_parameter_group_size(3) -fluid.core._set_fuse_parameter_memory_size(131072) +base.core._set_fuse_parameter_group_size(3) +base.core._set_fuse_parameter_memory_size(131072) import unittest from functools import partial diff --git a/test/legacy_test/test_parallel_executor_seresnext_with_reduce_cpu.py b/test/legacy_test/test_parallel_executor_seresnext_with_reduce_cpu.py index d1e3d9f6d0c9a..9dead36622763 100644 --- a/test/legacy_test/test_parallel_executor_seresnext_with_reduce_cpu.py +++ b/test/legacy_test/test_parallel_executor_seresnext_with_reduce_cpu.py @@ -17,7 +17,7 @@ import seresnext_net from parallel_executor_test_base import DeviceType, TestParallelExecutorBase -from paddle.fluid import core +from paddle.base import core class TestResnetWithReduceBase(TestParallelExecutorBase): diff --git a/test/legacy_test/test_parallel_executor_transformer.py b/test/legacy_test/test_parallel_executor_transformer.py index 3b72fde560dd5..2cca066c2a9c7 100644 --- a/test/legacy_test/test_parallel_executor_transformer.py +++ b/test/legacy_test/test_parallel_executor_transformer.py @@ -21,8 +21,8 @@ from parallel_executor_test_base import DeviceType, TestParallelExecutorBase import paddle +from paddle.base import core from paddle.dataset import wmt16 -from paddle.fluid import core os.environ['CPU_NUM'] = str(4) diff --git a/test/legacy_test/test_parameter.py b/test/legacy_test/test_parameter.py index 30bf9dc0d8280..415167e31fe61 100644 --- a/test/legacy_test/test_parameter.py +++ b/test/legacy_test/test_parameter.py @@ -18,10 +18,10 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.dygraph import guard -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Variable, default_main_program +from paddle.base import core +from paddle.base.dygraph import guard +from paddle.base.executor import Executor +from paddle.base.framework import Variable, default_main_program paddle.enable_static() main_program = default_main_program() diff --git a/test/legacy_test/test_partial_eager_deletion_transformer.py b/test/legacy_test/test_partial_eager_deletion_transformer.py index 2d78f886963a9..acfc9d02567d8 100644 --- a/test/legacy_test/test_partial_eager_deletion_transformer.py +++ b/test/legacy_test/test_partial_eager_deletion_transformer.py @@ -15,9 +15,9 @@ import unittest import paddle -from paddle import fluid +from paddle import base -fluid.core._set_eager_deletion_mode(0.0, 0.55, True) +base.core._set_eager_deletion_mode(0.0, 0.55, True) if __name__ == '__main__': paddle.enable_static() diff --git a/test/legacy_test/test_pass_builder.py b/test/legacy_test/test_pass_builder.py index 19912b6df065f..b976d29ca0db3 100644 --- a/test/legacy_test/test_pass_builder.py +++ b/test/legacy_test/test_pass_builder.py @@ -22,16 +22,16 @@ from simple_nets import simple_fc_net import paddle -from paddle import fluid -from paddle.fluid import compiler, core +from paddle import base +from paddle.base import compiler, core class TestPassBuilder(unittest.TestCase): def check_network_convergence(self, use_cuda, build_strategy=None): os.environ['CPU_NUM'] = str(4) - main = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.program_guard(main, startup): loss = simple_fc_net() test_program = main.clone(for_test=True) @@ -42,8 +42,8 @@ def check_network_convergence(self, use_cuda, build_strategy=None): image = np.random.normal(size=(batch_size, 784)).astype('float32') label = np.random.randint(0, 10, (batch_size, 1), dtype="int64") - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup) feed_dict = {'image': image, 'label': label} @@ -83,7 +83,7 @@ def check_network_convergence(self, use_cuda, build_strategy=None): ) def test_parallel_testing_with_new_strategy(self): - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() self.assertFalse(build_strategy.fuse_elewise_add_act_ops) build_strategy.fuse_elewise_add_act_ops = True # FIXME: currently fuse_elewise_add_act_ops not compatible with below options diff --git a/test/legacy_test/test_pixel_shuffle_op.py b/test/legacy_test/test_pixel_shuffle_op.py index 7c6f18479fd11..7ab983476c5d6 100644 --- a/test/legacy_test/test_pixel_shuffle_op.py +++ b/test/legacy_test/test_pixel_shuffle_op.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def pixel_shuffle_np(x, up_factor, data_format="NCHW"): @@ -182,14 +182,14 @@ def test_static_graph_functional(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, )[0] res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -222,13 +222,13 @@ def test_api_fp16(self): out_2_np = pixel_shuffle_np(self.x_2_np, 3, "NHWC") exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, )[0] res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -260,14 +260,14 @@ def test_static_graph_layer(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, )[0] res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -319,21 +319,21 @@ def test_dygraph2(self): class TestPixelShuffleError(unittest.TestCase): def test_error_functional(self): def error_upscale_factor(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") pixel_shuffle = F.pixel_shuffle(paddle.to_tensor(x), 3.33) self.assertRaises(TypeError, error_upscale_factor) def error_0_upscale_factor(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = paddle.uniform([1, 1, 1, 1], dtype='float64') pixel_shuffle = F.pixel_shuffle(x, 0) self.assertRaises(ValueError, error_0_upscale_factor) def error_data_format(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") pixel_shuffle = F.pixel_shuffle(paddle.to_tensor(x), 3, "WOW") @@ -341,14 +341,14 @@ def error_data_format(): def test_error_layer(self): def error_upscale_factor_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") ps = paddle.nn.PixelShuffle(3.33) self.assertRaises(TypeError, error_upscale_factor_layer) def error_data_format_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 9, 4, 4]).astype("float64") ps = paddle.nn.PixelShuffle(3, "MEOW") diff --git a/test/legacy_test/test_pixel_unshuffle.py b/test/legacy_test/test_pixel_unshuffle.py index 2353ca0192c7e..dc982ded5b4ee 100644 --- a/test/legacy_test/test_pixel_unshuffle.py +++ b/test/legacy_test/test_pixel_unshuffle.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def pixel_unshuffle_np(x, down_factor, data_format="NCHW"): @@ -221,14 +221,14 @@ def test_static_graph_functional(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, )[0] res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -263,14 +263,14 @@ def test_static_graph_layer(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_1_np}, fetch_list=out_1, use_prune=True, )[0] res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_2_np}, fetch_list=out_2, use_prune=True, @@ -337,28 +337,28 @@ def test_error_functional(self): '''test_error_functional''' def error_input(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([4, 12, 12]).astype("float64") pixel_unshuffle = F.pixel_unshuffle(paddle.to_tensor(x), 2) self.assertRaises(ValueError, error_input) def error_downscale_factor_1(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") pixel_unshuffle = F.pixel_unshuffle(paddle.to_tensor(x), 3.33) self.assertRaises(TypeError, error_downscale_factor_1) def error_downscale_factor_2(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") pixel_unshuffle = F.pixel_unshuffle(paddle.to_tensor(x), -1) self.assertRaises(ValueError, error_downscale_factor_2) def error_data_format(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") pixel_unshuffle = F.pixel_unshuffle( paddle.to_tensor(x), 3, "WOW" @@ -370,7 +370,7 @@ def test_error_layer(self): '''test_error_layer''' def error_input_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([4, 12, 12]).astype("float64") ps = paddle.nn.PixelUnshuffle(2) ps(paddle.to_tensor(x)) @@ -378,21 +378,21 @@ def error_input_layer(): self.assertRaises(ValueError, error_input_layer) def error_downscale_factor_layer_1(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") ps = paddle.nn.PixelUnshuffle(3.33) self.assertRaises(TypeError, error_downscale_factor_layer_1) def error_downscale_factor_layer_2(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") ps = paddle.nn.PixelUnshuffle(-1) self.assertRaises(ValueError, error_downscale_factor_layer_2) def error_data_format_layer(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 1, 12, 12]).astype("float64") ps = paddle.nn.PixelUnshuffle(3, "MEOW") diff --git a/test/legacy_test/test_poisson_nll_loss.py b/test/legacy_test/test_poisson_nll_loss.py index 14ad375519914..4cfa517856780 100644 --- a/test/legacy_test/test_poisson_nll_loss.py +++ b/test/legacy_test/test_poisson_nll_loss.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core np.random.seed(100) diff --git a/test/legacy_test/test_poisson_op.py b/test/legacy_test/test_poisson_op.py index 84edf6a322189..f6667407ed92e 100644 --- a/test/legacy_test/test_poisson_op.py +++ b/test/legacy_test/test_poisson_op.py @@ -23,7 +23,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() paddle.seed(100) @@ -106,7 +106,7 @@ def test_static(self): self.assertTrue(np.min(y_np) >= 0) def test_dygraph(self): - with paddle.fluid.dygraph.base.guard(): + with paddle.base.dygraph.base.guard(): x = paddle.randn([10, 10], dtype='float32') y = paddle.poisson(x) self.assertTrue(np.min(y.numpy()) >= 0) diff --git a/test/legacy_test/test_polar.py b/test/legacy_test/test_polar.py index 21b72c97d5879..5336fe38037a4 100644 --- a/test/legacy_test/test_polar.py +++ b/test/legacy_test/test_polar.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_polygamma_op.py b/test/legacy_test/test_polygamma_op.py index 9b5cf3062f3d2..88736f89173e0 100644 --- a/test/legacy_test/test_polygamma_op.py +++ b/test/legacy_test/test_polygamma_op.py @@ -19,7 +19,7 @@ from scipy import special import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(100) paddle.seed(100) diff --git a/test/legacy_test/test_pool1d_api.py b/test/legacy_test/test_pool1d_api.py index 871c1817202fd..1381470481c53 100644 --- a/test/legacy_test/test_pool1d_api.py +++ b/test/legacy_test/test_pool1d_api.py @@ -18,8 +18,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -117,12 +117,12 @@ def avg_pool1D_forward_naive( class TestPool1D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_avg_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -133,9 +133,9 @@ def check_avg_static_results(self, place): input_np, ksize=[2], strides=[2], paddings=[0], ceil_mode=False ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -168,9 +168,9 @@ def check_avg_static_results_fp16(self, place): np.testing.assert_allclose(fetches[0], result_np, rtol=1e-03) def check_avg_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.avg_pool1d(input, kernel_size=2, stride=2, padding=[0]) result_np = avg_pool1D_forward_naive( @@ -186,9 +186,9 @@ def check_avg_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_padding_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.avg_pool1d( input, kernel_size=2, stride=2, padding=[1], exclusive=True ) @@ -207,7 +207,7 @@ def check_avg_dygraph_padding_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32], dtype="float32" ) @@ -218,18 +218,18 @@ def check_max_static_results(self, place): input_np, ksize=[2], strides=[2], paddings=[0] ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) def check_max_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.max_pool1d(input, kernel_size=2, stride=2, padding=0) result_np = max_pool1D_forward_naive( @@ -245,9 +245,9 @@ def check_max_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_return_index_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result, index = F.max_pool1d( input, kernel_size=2, stride=2, padding=0, return_mask=True ) @@ -265,9 +265,9 @@ def check_max_dygraph_return_index_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_padding_same(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.max_pool1d( input, kernel_size=2, stride=2, padding="SAME" ) @@ -279,9 +279,9 @@ def check_max_dygraph_padding_same(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_padding_same(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = F.avg_pool1d( input, kernel_size=2, stride=2, padding="SAME" ) @@ -307,11 +307,11 @@ def test_pool1d(self): class TestPool1DError_API(unittest.TestCase): def test_error_api(self): def run1(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[2]] res_pd = F.max_pool1d( input_pd, kernel_size=2, stride=2, padding=padding @@ -320,11 +320,11 @@ def run1(): self.assertRaises(ValueError, run1) def run2(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[2]] res_pd = F.max_pool1d( input_pd, kernel_size=2, stride=2, padding=padding @@ -333,11 +333,11 @@ def run2(): self.assertRaises(ValueError, run2) def run3(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "padding" res_pd = F.max_pool1d( input_pd, kernel_size=2, stride=2, padding=padding @@ -346,11 +346,11 @@ def run3(): self.assertRaises(ValueError, run3) def run4(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = F.max_pool1d( input_pd, @@ -363,11 +363,11 @@ def run4(): self.assertRaises(ValueError, run4) def run5(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = F.max_pool1d( input_pd, @@ -380,11 +380,11 @@ def run5(): self.assertRaises(ValueError, run5) def run6(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = F.avg_pool1d( input_pd, @@ -397,11 +397,11 @@ def run6(): self.assertRaises(ValueError, run6) def run7(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "paddle" res_pd = F.avg_pool1d( input_pd, @@ -414,11 +414,11 @@ def run7(): self.assertRaises(ValueError, run7) def run_kernel_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = 0 res_pd = F.avg_pool1d( input_pd, @@ -431,11 +431,11 @@ def run_kernel_out_of_range(): self.assertRaises(ValueError, run_kernel_out_of_range) def run_stride_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = 0 res_pd = F.avg_pool1d( input_pd, @@ -448,7 +448,7 @@ def run_stride_out_of_range(): self.assertRaises(ValueError, run_stride_out_of_range) def run_zero_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1]), dtype='float32' @@ -460,7 +460,7 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) def run_zero_tuple_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1]), dtype='float32' diff --git a/test/legacy_test/test_pool2d_api.py b/test/legacy_test/test_pool2d_api.py index a4971a399ae64..fcca5381fa4f0 100644 --- a/test/legacy_test/test_pool2d_api.py +++ b/test/legacy_test/test_pool2d_api.py @@ -22,20 +22,20 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional import avg_pool2d, max_pool2d class TestPool2D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_avg_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32, 32], dtype="float32" ) @@ -50,18 +50,18 @@ def check_avg_static_results(self, place): pool_type='avg', ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) def check_avg_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool2d(input, kernel_size=2, stride=2, padding=0) result_np = pool2D_forward_naive( @@ -80,9 +80,9 @@ def check_avg_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_padding_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool2d( input, kernel_size=2, stride=2, padding=1, ceil_mode=False ) @@ -104,9 +104,9 @@ def check_avg_dygraph_padding_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_ceilmode_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool2d( input, kernel_size=2, stride=2, padding=0, ceil_mode=True ) @@ -127,7 +127,7 @@ def check_avg_dygraph_ceilmode_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32, 32], dtype="float32" ) @@ -142,18 +142,18 @@ def check_max_static_results(self, place): pool_type='max', ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) def check_max_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool2d( input, kernel_size=2, stride=2, padding=0, return_mask=False ) @@ -174,9 +174,9 @@ def check_max_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_nhwc_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable( + input = base.dygraph.to_variable( np.transpose(input_np, [0, 2, 3, 1]) ) result = max_pool2d( @@ -202,9 +202,9 @@ def check_max_dygraph_nhwc_results(self, place): ) def check_max_dygraph_padding_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool2d( input, kernel_size=2, stride=2, padding=1, ceil_mode=False ) @@ -226,9 +226,9 @@ def check_max_dygraph_padding_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_ceilmode_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool2d( input, kernel_size=2, stride=2, padding=0, ceil_mode=True ) @@ -249,9 +249,9 @@ def check_max_dygraph_ceilmode_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_stride_is_none(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result, indices = max_pool2d( input, kernel_size=2, @@ -277,9 +277,9 @@ def check_max_dygraph_stride_is_none(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_stride_is_none(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool2d( input, kernel_size=2, stride=None, padding="SAME" ) @@ -301,9 +301,9 @@ def check_avg_dygraph_stride_is_none(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_padding(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) padding = [[0, 0], [0, 0], [0, 0], [0, 0]] result = max_pool2d( input, @@ -329,9 +329,9 @@ def check_max_dygraph_padding(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_divisor(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) padding = [[0, 0], [0, 0], [0, 0], [0, 0]] result = avg_pool2d( input, @@ -374,11 +374,11 @@ def test_pool2d(self): class TestPool2DError_API(unittest.TestCase): def test_error_api(self): def run1(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[0, 1], [0, 0], [0, 0], [0, 0]] res_pd = max_pool2d( input_pd, kernel_size=2, stride=2, padding=padding @@ -387,11 +387,11 @@ def run1(): self.assertRaises(ValueError, run1) def run2(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[0, 1], [0, 0], [0, 0], [0, 0]] res_pd = max_pool2d( input_pd, @@ -404,11 +404,11 @@ def run2(): self.assertRaises(ValueError, run2) def run3(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "padding" res_pd = max_pool2d( input_pd, @@ -421,11 +421,11 @@ def run3(): self.assertRaises(ValueError, run3) def run3_avg(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "padding" res_pd = avg_pool2d( input_pd, @@ -438,11 +438,11 @@ def run3_avg(): self.assertRaises(ValueError, run3_avg) def run4(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = max_pool2d( input_pd, @@ -456,11 +456,11 @@ def run4(): self.assertRaises(ValueError, run4) def run4_avg(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = avg_pool2d( input_pd, @@ -474,11 +474,11 @@ def run4_avg(): self.assertRaises(ValueError, run4_avg) def run5(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "padding" res_pd = avg_pool2d( input_pd, @@ -491,11 +491,11 @@ def run5(): self.assertRaises(ValueError, run5) def run6(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = avg_pool2d( input_pd, @@ -509,11 +509,11 @@ def run6(): self.assertRaises(ValueError, run6) def run7(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = avg_pool2d( input_pd, @@ -527,11 +527,11 @@ def run7(): self.assertRaises(ValueError, run7) def run8(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = "VALID" res_pd = max_pool2d( input_pd, @@ -545,11 +545,11 @@ def run8(): self.assertRaises(ValueError, run8) def run9(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = max_pool2d( input_pd, kernel_size=2, @@ -563,11 +563,11 @@ def run9(): self.assertRaises(ValueError, run9) def run_kernel_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool2d( input_pd, kernel_size=[-1, 2], @@ -580,11 +580,11 @@ def run_kernel_out_of_range(): self.assertRaises(ValueError, run_kernel_out_of_range) def run_stride_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool2d( input_pd, kernel_size=3, @@ -597,7 +597,7 @@ def run_stride_out_of_range(): self.assertRaises(ValueError, run_stride_out_of_range) def run_zero_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1, 1]), dtype='float32' @@ -609,7 +609,7 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) def run_zero_tuple_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1, 1]), dtype='float32' diff --git a/test/legacy_test/test_pool2d_op.py b/test/legacy_test/test_pool2d_op.py index 73c6050228bc3..f55b1a9a9d02c 100644 --- a/test/legacy_test/test_pool2d_op.py +++ b/test/legacy_test/test_pool2d_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -389,7 +389,7 @@ def setUp(self): self.inputs = {'X': convert_float_to_uint16(input)} else: output = output.astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(input)} self.attrs = { 'strides': self.strides, diff --git a/test/legacy_test/test_pool3d_api.py b/test/legacy_test/test_pool3d_api.py index a9e849fb91d41..a2286740391e4 100644 --- a/test/legacy_test/test_pool3d_api.py +++ b/test/legacy_test/test_pool3d_api.py @@ -22,20 +22,20 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional import avg_pool3d, max_pool3d class TestPool3D_API(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_avg_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32, 32, 32], dtype="float32" ) @@ -50,18 +50,18 @@ def check_avg_static_results(self, place): pool_type='avg', ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) def check_avg_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool3d(input, kernel_size=2, stride=2, padding="SAME") result_np = pool3D_forward_naive( @@ -82,9 +82,9 @@ def check_avg_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_padding_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool3d( input, kernel_size=2, @@ -116,9 +116,9 @@ def check_avg_dygraph_padding_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_dygraph_ceilmode_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = avg_pool3d( input, kernel_size=2, stride=2, padding=0, ceil_mode=True ) @@ -140,7 +140,7 @@ def check_avg_dygraph_ceilmode_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_static_results(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 32, 32, 32], dtype="float32" ) @@ -155,18 +155,18 @@ def check_max_static_results(self, place): pool_type='max', ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) np.testing.assert_allclose(fetches[0], result_np, rtol=1e-05) def check_max_dygraph_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool3d(input, kernel_size=2, stride=2, padding=0) result_np = pool3D_forward_naive( @@ -185,9 +185,9 @@ def check_max_dygraph_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_ndhwc_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable( + input = base.dygraph.to_variable( np.transpose(input_np, [0, 2, 3, 4, 1]) ) result = max_pool3d( @@ -214,9 +214,9 @@ def check_max_dygraph_ndhwc_results(self, place): ) def check_max_dygraph_ceilmode_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool3d( input, kernel_size=2, stride=2, padding=0, ceil_mode=True ) @@ -238,9 +238,9 @@ def check_max_dygraph_ceilmode_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_padding_results(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result = max_pool3d( input, kernel_size=2, stride=2, padding=1, ceil_mode=False ) @@ -262,9 +262,9 @@ def check_max_dygraph_padding_results(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_stride_is_none(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) result, indices = max_pool3d( input, kernel_size=2, @@ -290,9 +290,9 @@ def check_max_dygraph_stride_is_none(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_max_dygraph_padding(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) padding = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]] result = max_pool3d(input, kernel_size=2, stride=2, padding=padding) @@ -316,9 +316,9 @@ def check_max_dygraph_padding(self, place): np.testing.assert_allclose(result.numpy(), result_np, rtol=1e-05) def check_avg_divisor(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([2, 3, 32, 32, 32]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) padding = 0 result = avg_pool3d( input, @@ -368,7 +368,7 @@ def test_pool3d(self): def test_static_fp16_gpu(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -396,8 +396,8 @@ def test_static_fp16_gpu(self): def test_static_bf16_gpu(self): paddle.enable_static() if ( - paddle.fluid.core.is_compiled_with_cuda() - and paddle.fluid.core.is_bfloat16_supported(core.CUDAPlace(0)) + paddle.base.core.is_compiled_with_cuda() + and paddle.base.core.is_bfloat16_supported(core.CUDAPlace(0)) ): place = paddle.CUDAPlace(0) with paddle.static.program_guard( @@ -427,11 +427,11 @@ def test_static_bf16_gpu(self): class TestPool3DError_API(unittest.TestCase): def test_error_api(self): def run1(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]] res_pd = avg_pool3d( input_pd, kernel_size=2, stride=2, padding=padding @@ -440,11 +440,11 @@ def run1(): self.assertRaises(ValueError, run1) def run2(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]] res_pd = avg_pool3d( input_pd, @@ -457,11 +457,11 @@ def run2(): self.assertRaises(ValueError, run2) def run3(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) padding = [[0, 1], [0, 0], [0, 0], [0, 0], [0, 0]] res_pd = avg_pool3d( input_pd, @@ -474,11 +474,11 @@ def run3(): self.assertRaises(ValueError, run3) def run4(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool3d( input_pd, kernel_size=2, @@ -490,11 +490,11 @@ def run4(): self.assertRaises(ValueError, run4) def run5(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = max_pool3d( input_pd, kernel_size=2, @@ -506,11 +506,11 @@ def run5(): self.assertRaises(ValueError, run5) def run6(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool3d( input_pd, kernel_size=2, @@ -522,11 +522,11 @@ def run6(): self.assertRaises(ValueError, run6) def run7(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = max_pool3d( input_pd, kernel_size=2, @@ -538,11 +538,11 @@ def run7(): self.assertRaises(ValueError, run7) def run8(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool3d( input_pd, kernel_size=2, @@ -555,11 +555,11 @@ def run8(): self.assertRaises(ValueError, run8) def run9(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = max_pool3d( input_pd, kernel_size=2, @@ -572,11 +572,11 @@ def run9(): self.assertRaises(ValueError, run9) def run10(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = max_pool3d( input_pd, kernel_size=2, @@ -589,11 +589,11 @@ def run10(): self.assertRaises(ValueError, run10) def run_kernel_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool3d( input_pd, kernel_size=-1, @@ -605,11 +605,11 @@ def run_kernel_out_of_range(): self.assertRaises(ValueError, run_kernel_out_of_range) def run_size_out_of_range(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_np = np.random.uniform(-1, 1, [2, 3, 32, 32, 32]).astype( np.float32 ) - input_pd = fluid.dygraph.to_variable(input_np) + input_pd = base.dygraph.to_variable(input_np) res_pd = avg_pool3d( input_pd, kernel_size=2, @@ -621,7 +621,7 @@ def run_size_out_of_range(): self.assertRaises(ValueError, run_size_out_of_range) def run_zero_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1, 1, 1]), dtype='float32' @@ -633,7 +633,7 @@ def run_zero_stride(): self.assertRaises(ValueError, run_zero_stride) def run_zero_tuple_stride(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = np.array([1], dtype=np.float32) x = paddle.to_tensor( np.reshape(array, [1, 1, 1, 1, 1]), dtype='float32' diff --git a/test/legacy_test/test_pool3d_op.py b/test/legacy_test/test_pool3d_op.py index 9f35227bd0684..649e5cd255bd8 100644 --- a/test/legacy_test/test_pool3d_op.py +++ b/test/legacy_test/test_pool3d_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core def adaptive_start_index(index, input_size, output_size): @@ -363,7 +363,7 @@ def setUp(self): self.padding_algorithm, ).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(input)} self.attrs = { 'strides': self.strides, diff --git a/test/legacy_test/test_pool_max_op.py b/test/legacy_test/test_pool_max_op.py index 70b37da5ebb39..e24a6a5f3bf5c 100644 --- a/test/legacy_test/test_pool_max_op.py +++ b/test/legacy_test/test_pool_max_op.py @@ -24,7 +24,7 @@ from testsuite import create_op import paddle -from paddle.fluid import core +from paddle.base import core def adaptive_start_index(index, input_size, output_size): diff --git a/test/legacy_test/test_pow.py b/test/legacy_test/test_pow.py index 011593b3e874e..659fc32c2423f 100755 --- a/test/legacy_test/test_pow.py +++ b/test/legacy_test/test_pow.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard DYNAMIC = 1 diff --git a/test/legacy_test/test_prelu_op.py b/test/legacy_test/test_prelu_op.py index 8588ed498d703..80772fdd671e5 100644 --- a/test/legacy_test/test_prelu_op.py +++ b/test/legacy_test/test_prelu_op.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import Program, core +from paddle import base +from paddle.base import Program, core def ref_prelu(x, weight): @@ -144,14 +144,14 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05) x = paddle.to_tensor(self.x_np) - m = paddle.nn.PReLU(weight_attr=fluid.ParamAttr(name="weight")) + m = paddle.nn.PReLU(weight_attr=base.ParamAttr(name="weight")) out = m(x) out_ref = ref_prelu_nn(self.x_np, 1, 0.25) np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-05) x = paddle.to_tensor(self.x_np) m = paddle.nn.PReLU( - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(0.5) ) ) @@ -471,7 +471,7 @@ def test_check_grad(self): def prelu_t(x, mode, param_attr=None, name=None, data_format='NCHW'): - helper = fluid.layer_helper.LayerHelper('prelu', **locals()) + helper = base.layer_helper.LayerHelper('prelu', **locals()) alpha_shape = [1, x.shape[1], 1, 1] dtype = helper.input_dtype(input_param_name='x') alpha = helper.create_parameter( @@ -503,7 +503,7 @@ def setUp(self): def test_mode_error(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = prelu_t(x, 'any') @@ -512,7 +512,7 @@ def test_mode_error(self): def test_data_format_error1(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = prelu_t(x, 'channel', data_format='N') @@ -521,7 +521,7 @@ def test_data_format_error1(self): def test_data_format_error2(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = paddle.static.nn.prelu(x, 'channel', data_format='N') diff --git a/test/legacy_test/test_print_op.py b/test/legacy_test/test_print_op.py index a8c67868117ed..3352d2b23ef93 100755 --- a/test/legacy_test/test_print_op.py +++ b/test/legacy_test/test_print_op.py @@ -18,9 +18,9 @@ from simple_nets import init_data, simple_fc_net import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import switch_main_program +from paddle import base +from paddle.base import core +from paddle.base.framework import switch_main_program from paddle.static import Program, program_guard paddle.enable_static() @@ -29,7 +29,7 @@ class TestPrintOpCPU(unittest.TestCase): def setUp(self): self.place = paddle.CPUPlace() - self.x_tensor = fluid.core.LoDTensor() + self.x_tensor = base.core.LoDTensor() tensor_np = np.random.random(size=(2, 3)).astype('float32') self.x_tensor.set(tensor_np, self.place) self.x_tensor.set_recursive_sequence_lengths([[1, 1]]) @@ -93,7 +93,7 @@ class TestPrintOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # The input type of Print_op must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([[-1]]), [[1]], paddle.CPUPlace() ) self.assertRaises(TypeError, paddle.static.Print, x1) @@ -108,7 +108,7 @@ def test_errors(self): class TestPrintOpGPU(TestPrintOpCPU): def setUp(self): self.place = paddle.CUDAPlace(0) - self.x_tensor = fluid.core.LoDTensor() + self.x_tensor = base.core.LoDTensor() tensor_np = np.random.random(size=(2, 3)).astype('float32') self.x_tensor.set(tensor_np, self.place) self.x_tensor.set_recursive_sequence_lengths([[1, 1]]) diff --git a/test/legacy_test/test_prod_op.py b/test/legacy_test/test_prod_op.py index 2146655baf5e0..2a0b06d76f849 100644 --- a/test/legacy_test/test_prod_op.py +++ b/test/legacy_test/test_prod_op.py @@ -138,7 +138,7 @@ def test_cpu(self): self.run_static() def test_gpu(self): - if not paddle.fluid.core.is_compiled_with_cuda(): + if not paddle.base.core.is_compiled_with_cuda(): return paddle.disable_static(place=paddle.CUDAPlace(0)) diff --git a/test/legacy_test/test_program.py b/test/legacy_test/test_program.py index a1b5b51a886e3..16c0351366adb 100644 --- a/test/legacy_test/test_program.py +++ b/test/legacy_test/test_program.py @@ -15,8 +15,8 @@ import unittest import paddle -from paddle import fluid -from paddle.fluid.framework import Program, default_main_program, program_guard +from paddle import base +from paddle.base.framework import Program, default_main_program, program_guard paddle.enable_static() @@ -105,7 +105,7 @@ def test_program_clone_with_parameter(self): self.assertNotEqual(0, len(new_program.blocks[0].all_parameters())) def test_program_all_parameters(self): - program = fluid.default_main_program() + program = base.default_main_program() data = paddle.static.data(name='x', shape=[None, 13], dtype='float32') hidden = paddle.static.nn.fc(x=data, size=10) loss = paddle.mean(hidden) @@ -118,19 +118,19 @@ def test_program_all_parameters(self): self.assertEqual(param_list[1].name, "fc_0.b_0") def test_prune_with_input_type_error(self): - program = fluid.default_main_program() + program = base.default_main_program() feed_var_names = [2, 3, 4] self.assertRaises( ValueError, program._prune_with_input, feed_var_names, [] ) def test_random_seed_error(self): - program = fluid.default_main_program() + program = base.default_main_program() with self.assertRaises(ValueError): program.random_seed = "seed" def test_copy_info_from_error(self): - program = fluid.default_main_program() + program = base.default_main_program() self.assertRaises(TypeError, program._copy_param_info_from, "program") self.assertRaises( TypeError, program._copy_dist_param_info_from, "program" diff --git a/test/legacy_test/test_program_code.py b/test/legacy_test/test_program_code.py index 03661d3b3fbb0..86979038a0a28 100644 --- a/test/legacy_test/test_program_code.py +++ b/test/legacy_test/test_program_code.py @@ -15,12 +15,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestProgramToReadableCode(unittest.TestCase): def setUp(self): - self.program = fluid.Program() + self.program = base.Program() self.block = self.program.current_block() self.var = self.block.create_var( name="X", shape=[-1, 23, 48], dtype='float32' @@ -45,7 +45,7 @@ def false_func(): shape=[3, 2], dtype='int32', value=-1 ) - with fluid.program_guard(program): + with base.program_guard(program): x = paddle.tensor.fill_constant( shape=[1], dtype='float32', value=0.1 ) diff --git a/test/legacy_test/test_program_converter.py b/test/legacy_test/test_program_converter.py index 9a9c49df01b68..3894ca930ee0f 100644 --- a/test/legacy_test/test_program_converter.py +++ b/test/legacy_test/test_program_converter.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.proto import framework_pb2 +from paddle.base.proto import framework_pb2 class TestSetValue(unittest.TestCase): diff --git a/test/legacy_test/test_program_prune_backward.py b/test/legacy_test/test_program_prune_backward.py index c304777e64570..237684e3b0bd9 100755 --- a/test/legacy_test/test_program_prune_backward.py +++ b/test/legacy_test/test_program_prune_backward.py @@ -25,8 +25,8 @@ ) import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def simple_fc_net_with_accuracy(use_feed): @@ -39,7 +39,7 @@ def simple_fc_net_with_accuracy(use_feed): hidden, size=200, activation='relu', - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -118,11 +118,11 @@ def loss2(opt, pred, label, with_optimize): class TestProgramPruneBackward(unittest.TestCase): def program_compare(self, program_a, program_b): assert isinstance( - program_a, fluid.framework.Program - ), "The first argument should be fluid.framework.Program." + program_a, base.framework.Program + ), "The first argument should be base.framework.Program." assert isinstance( - program_b, fluid.framework.Program - ), "The second argument should be fluid.framework Program." + program_b, base.framework.Program + ), "The second argument should be base.framework Program." self.assertEqual(len(program_a.blocks), len(program_b.blocks)) for idx in range(len(program_a.blocks)): @@ -140,7 +140,7 @@ def program_compare(self, program_a, program_b): def check_prune_correctness(self, method, feed_dict, optimizer): loss = method(use_feed=False) - main_program = fluid.default_main_program() + main_program = base.default_main_program() test_prog_orig = main_program.clone(for_test=True) optimizer().minimize(loss) test_prog_prune = main_program.clone(for_test=True) @@ -152,8 +152,8 @@ def check_prune_correctness(self, method, feed_dict, optimizer): places.append(core.CUDAPlace(0)) for place in places: - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) (loss_data_prune,) = exe.run( test_prog_prune, feed=feed_dict, fetch_list=[loss.name] @@ -230,7 +230,7 @@ def optimizer(): with self.program_scope_guard(): # the program argument is used to distinguish Program and CompiledProgram feed_dict = get_feed_data_reader().get_next( - fluid.Executor(core.CPUPlace()), fluid.default_main_program() + base.Executor(core.CPUPlace()), base.default_main_program() ) self.check_prune_correctness( method=transformer, feed_dict=feed_dict, optimizer=optimizer @@ -255,23 +255,23 @@ def test_optimization_in_cond(self): feed_dict = {'x': x_in, 'label': label_in} with self.program_scope_guard(): loss = optimization_in_cond_net(False) - main_program = fluid.default_main_program() + main_program = base.default_main_program() test_prog_orig = main_program.clone(for_test=True) place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) (loss_data_orig,) = exe.run( test_prog_orig, feed=feed_dict, fetch_list=[loss.name] ) with self.program_scope_guard(): loss = optimization_in_cond_net(True) - main_program = fluid.default_main_program() + main_program = base.default_main_program() test_prog_prune = main_program.clone(for_test=True) place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) (loss_data_prune,) = exe.run( test_prog_prune, feed=feed_dict, fetch_list=[loss.name] ) @@ -281,12 +281,12 @@ def test_optimization_in_cond(self): @contextlib.contextmanager def program_scope_guard(self): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): - with fluid.unique_name.guard(): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): + with base.unique_name.guard(): yield diff --git a/test/legacy_test/test_program_to_string.py b/test/legacy_test/test_program_to_string.py index e45e8cc394340..c6524d9cf5d92 100644 --- a/test/legacy_test/test_program_to_string.py +++ b/test/legacy_test/test_program_to_string.py @@ -15,12 +15,12 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestProgram(unittest.TestCase): def test_program_to_string(self): - prog = fluid.default_main_program() + prog = base.default_main_program() a = paddle.static.data(name="X", shape=[2, 3], dtype="float32") c = paddle.static.nn.fc(a, size=3) prog_string = prog.to_string(throw_on_error=True, with_details=False) diff --git a/test/legacy_test/test_protobuf.py b/test/legacy_test/test_protobuf.py index c76a5fe67f245..ec42e13bd6311 100644 --- a/test/legacy_test/test_protobuf.py +++ b/test/legacy_test/test_protobuf.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.proto import framework_pb2 +from paddle.base.proto import framework_pb2 class TestFrameworkProto(unittest.TestCase): diff --git a/test/legacy_test/test_protobuf_descs.py b/test/legacy_test/test_protobuf_descs.py index 9a84d43e44bd7..c26752ba46208 100644 --- a/test/legacy_test/test_protobuf_descs.py +++ b/test/legacy_test/test_protobuf_descs.py @@ -14,8 +14,8 @@ import unittest -from paddle.fluid import core -from paddle.fluid.framework import Program +from paddle.base import core +from paddle.base.framework import Program class TestOpDesc(unittest.TestCase): diff --git a/test/legacy_test/test_prune.py b/test/legacy_test/test_prune.py index 45f40d358901d..00b96074ab5c2 100644 --- a/test/legacy_test/test_prune.py +++ b/test/legacy_test/test_prune.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework class TestPrune(unittest.TestCase): @@ -39,7 +39,7 @@ def test_prune_with_input(self): program = framework.Program() startup_program = framework.Program() block = program.global_block() - with fluid.program_guard(program, startup_program): + with base.program_guard(program, startup_program): (x, y, label, loss) = self.net() self.assertEqual(len(block.ops), 5) self.assertEqual( @@ -65,7 +65,7 @@ def test_prune(self): program = framework.Program() startup_program = framework.Program() block = program.global_block() - with fluid.program_guard(program, startup_program): + with base.program_guard(program, startup_program): (x, y, label, loss) = self.net() self.assertEqual(len(block.ops), 5) self.assertEqual( @@ -95,7 +95,7 @@ def test_prune_target_not_list(self): program = framework.Program() startup_program = framework.Program() block = program.global_block() - with fluid.program_guard(program, startup_program): + with base.program_guard(program, startup_program): (x, y, label, loss) = self.net() self.assertEqual(len(block.ops), 5) self.assertEqual( @@ -125,7 +125,7 @@ def test_prune_target_none(self): program = framework.Program() startup_program = framework.Program() block = program.global_block() - with fluid.program_guard(program, startup_program): + with base.program_guard(program, startup_program): (x, y, label, loss) = self.net() self.assertEqual(len(block.ops), 5) self.assertEqual( @@ -154,10 +154,10 @@ def mock(self, program, feed, fetch, optimize_ops): @contextlib.contextmanager def _mock_guard(mock): - original = fluid.Executor._prune_program - fluid.Executor._prune_program = mock + original = base.Executor._prune_program + base.Executor._prune_program = mock yield - fluid.Executor._prune_program = original + base.Executor._prune_program = original class TestExecutorRunAutoPrune(unittest.TestCase): @@ -166,7 +166,7 @@ def net1(self): x.desc.set_need_check_feed(False) label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") label.desc.set_need_check_feed(False) - w_param_attrs = fluid.ParamAttr( + w_param_attrs = base.ParamAttr( name="fc_weight", learning_rate=0.5, initializer=paddle.nn.initializer.Constant(1.0), @@ -194,13 +194,13 @@ def net2(self): x2.desc.set_need_check_feed(False) label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") label.desc.set_need_check_feed(False) - w1_param_attrs = fluid.ParamAttr( + w1_param_attrs = base.ParamAttr( name="fc_weight1", learning_rate=0.5, initializer=paddle.nn.initializer.Constant(1.0), trainable=True, ) - w2_param_attrs = fluid.ParamAttr( + w2_param_attrs = base.ParamAttr( name="fc_weight2", learning_rate=0.5, initializer=paddle.nn.initializer.Constant(1.0), @@ -238,11 +238,11 @@ def test_not_prune(self): """ program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) x_np = np.random.random(size=(10, 2)).astype('float32') label_np = np.random.randint(1, size=(10, 1)).astype('int64') @@ -261,11 +261,11 @@ def test_prune_fetches_without_optimizer(self): """ program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() @@ -294,13 +294,13 @@ def test_prune_fetches_with_optimizer(self): """ program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) sgd_optimizer.minimize(loss1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() @@ -325,15 +325,15 @@ def test_prune_fetches_with_optimizer(self): def test_prune_compiled_program(self): program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) sgd_optimizer.minimize(loss1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) - compiled_prog = fluid.CompiledProgram(program) + compiled_prog = base.CompiledProgram(program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() ) @@ -357,11 +357,11 @@ def test_prune_compiled_program(self): def test_prune_feed_without_optimizer(self): program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() @@ -386,13 +386,13 @@ def test_prune_feed_without_optimizer(self): def test_prune_feed_with_optimizer(self): program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) sgd_optimizer.minimize(loss1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) x_np = np.random.random(size=(10, 2)).astype('float32') label_np = np.random.randint(1, size=(10, 1)).astype('int64') @@ -417,13 +417,13 @@ def test_prune_with_cache_program(self): 10 times with the same input arguments. ''' with _mock_guard(mock): - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.prune_called_times = 0 program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) sgd_optimizer.minimize(loss1) @@ -451,13 +451,13 @@ def test_prune_with_cache_program2(self): the cache_keys should be different and get different pruned program. ''' with _mock_guard(mock): - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.prune_called_times = 0 program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): ( x1, x2, @@ -519,13 +519,13 @@ def test_prune_with_cache_compiled_program(self): 10 times with the same input arguments. ''' with _mock_guard(mock): - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.prune_called_times = 0 program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) sgd_optimizer.minimize(loss1) @@ -534,7 +534,7 @@ def test_prune_with_cache_compiled_program(self): label_np = np.random.randint(1, size=(10, 1)).astype( 'int64' ) - compiled_prog = fluid.CompiledProgram(program) + compiled_prog = base.CompiledProgram(program) for i in range(10): res = exe.run( compiled_prog, @@ -552,13 +552,13 @@ def test_prune_with_multi_optimizers(self): If there are multiple optimizers in the program, we can run specific one by pass the return of optimize.minimize() to fetch_list. ''' - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() + scope = base.Scope() # do not use_prune - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) train1, _ = sgd_optimizer.minimize(loss1) @@ -577,9 +577,9 @@ def test_prune_with_multi_optimizers(self): scope.find_var(w_param_attrs.name).get_tensor() ) - scope = fluid.Scope() + scope = base.Scope() # use_prune - with fluid.scope_guard(scope): + with base.scope_guard(scope): exe.run(startup_program) res = exe.run( program, @@ -592,8 +592,8 @@ def test_prune_with_multi_optimizers(self): ) # expected - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) exe.run( cloned_program, @@ -613,13 +613,13 @@ def test_prune_program_with_tupe_in_fetch_list(self): If there are multiple optimizers in the program, we can run specific one by pass the return of optimize.minimize() to fetch_list. ''' - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() + scope = base.Scope() # do not use_prune - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) train1 = sgd_optimizer.minimize(loss1) @@ -641,9 +641,9 @@ def test_prune_program_with_tupe_in_fetch_list(self): scope.find_var(w_param_attrs.name).get_tensor() ) - scope = fluid.Scope() + scope = base.Scope() # use_prune - with fluid.scope_guard(scope): + with base.scope_guard(scope): exe.run(startup_program) res = exe.run( program, @@ -656,8 +656,8 @@ def test_prune_program_with_tupe_in_fetch_list(self): ) # expected - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) exe.run( cloned_program, @@ -679,9 +679,9 @@ def test_prune_program_partial_parameter_updated(self): """ program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): ( x1, x2, @@ -699,7 +699,7 @@ def test_prune_program_partial_parameter_updated(self): train1 = sgd_optimizer.minimize(loss1) sgd_optimizer1 = paddle.optimizer.SGD(learning_rate=0.5) train2 = sgd_optimizer1.minimize(loss2) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight1_init = np.array( scope.find_var(w1_param_attrs.name).get_tensor() @@ -737,13 +737,13 @@ def test_prune_override_use_prune(self): ''' If optimize_ops in provided in the fetch_list, the argument use_prune is always override to True. ''' - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() + scope = base.Scope() # do not use_prune - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.5) train1, _ = sgd_optimizer.minimize(loss1) @@ -763,9 +763,9 @@ def test_prune_override_use_prune(self): scope.find_var(w_param_attrs.name).get_tensor() ) - scope = fluid.Scope() + scope = base.Scope() # use_prune - with fluid.scope_guard(scope): + with base.scope_guard(scope): exe.run(startup_program) res = exe.run( program, @@ -777,8 +777,8 @@ def test_prune_override_use_prune(self): ) # expected - scope = fluid.Scope() - with fluid.scope_guard(scope): + scope = base.Scope() + with base.scope_guard(scope): exe.run(startup_program) exe.run( cloned_program, @@ -797,11 +797,11 @@ def test_prune_feed_var_in_fetchlist_1(self): # the variable to be fed is not leaf program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() @@ -828,11 +828,11 @@ def test_prune_feed_var_in_fetchlist_2(self): # the variable to be fed is leaf program = framework.Program() startup_program = framework.Program() - scope = fluid.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(program, startup_program): + scope = base.Scope() + with base.scope_guard(scope): + with base.program_guard(program, startup_program): (x, y, label, loss1, loss2, w_param_attrs) = self.net1() - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) weight_init = np.array( scope.find_var(w_param_attrs.name).get_tensor() diff --git a/test/legacy_test/test_prune_gate_by_capacity_op.py b/test/legacy_test/test_prune_gate_by_capacity_op.py index d6103be8d13d5..602c2ad787b60 100644 --- a/test/legacy_test/test_prune_gate_by_capacity_op.py +++ b/test/legacy_test/test_prune_gate_by_capacity_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.models.moe import utils -from paddle.fluid import core def count(x, upper_num): diff --git a/test/legacy_test/test_psroi_pool_op.py b/test/legacy_test/test_psroi_pool_op.py index 03c2424d87844..fc4b513acfe72 100644 --- a/test/legacy_test/test_psroi_pool_op.py +++ b/test/legacy_test/test_psroi_pool_op.py @@ -229,7 +229,7 @@ def test_dytype_is_float64(): np.testing.assert_allclose(out, expect_out, rtol=1e-05) places = ['cpu'] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append('gpu') for place in places: paddle.set_device(place) @@ -285,7 +285,7 @@ def test_dytype_is_float64(): paddle.disable_static() places = ['cpu'] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append('gpu') for place in places: paddle.set_device(place) @@ -382,11 +382,11 @@ def test_function_in_static(self): self.x, self.boxes, self.boxes_num, 10, 1.0, 7, 7 ) places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: exe = paddle.static.Executor(place) - boxes_lod_data = paddle.fluid.create_lod_tensor( + boxes_lod_data = paddle.base.create_lod_tensor( self.boxes, [[1, 2]], place ) (out_res,) = exe.run( diff --git a/test/legacy_test/test_pull_gpups_sparse_op.py b/test/legacy_test/test_pull_gpups_sparse_op.py index 054cc9f495dbf..03b4c370cbfb6 100644 --- a/test/legacy_test/test_pull_gpups_sparse_op.py +++ b/test/legacy_test/test_pull_gpups_sparse_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.incubate.layers import _pull_gpups_sparse paddle.enable_static() @@ -27,10 +27,10 @@ class TestPullGpupsSparse(unittest.TestCase): """Test PullGpupsSparse op.""" def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() + startup_program = base.Program() + train_program = base.Program() slots = [] - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): l = paddle.static.data( name='input', shape=[-1, 1], dtype="int64", lod_level=1 ) @@ -42,10 +42,10 @@ def test_static_graph(self): sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) sgd_optimizer.minimize(cost, train_program) block = train_program.global_block() - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([1]).astype(np.int64) res = exe.run( diff --git a/test/legacy_test/test_py_func_op.py b/test/legacy_test/test_py_func_op.py index 2719fff9235d3..1706ad14d644d 100644 --- a/test/legacy_test/test_py_func_op.py +++ b/test/legacy_test/test_py_func_op.py @@ -18,12 +18,12 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import compiler +from paddle import base +from paddle.base import compiler dev_cnt = 2 -if fluid.core.is_compiled_with_cuda(): - dev_cnt = fluid.core.get_cuda_device_count() +if base.core.is_compiled_with_cuda(): + dev_cnt = base.core.get_cuda_device_count() os.environ['CPU_NUM'] = str(dev_cnt) @@ -78,7 +78,7 @@ def simple_fc_net(img, label, use_py_func_op): hidden = paddle.static.nn.fc( hidden, size=200, - bias_attr=fluid.ParamAttr( + bias_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ), ) @@ -86,7 +86,7 @@ def simple_fc_net(img, label, use_py_func_op): hidden = paddle.tanh(hidden) else: new_hidden = ( - fluid.default_main_program() + base.default_main_program() .current_block() .create_var( name=f'hidden_{idx}', @@ -109,7 +109,7 @@ def simple_fc_net(img, label, use_py_func_op): ) else: loss = ( - fluid.default_main_program() + base.default_main_program() .current_block() .create_var(name='loss', dtype='float32', shape=[-1, 1]) ) @@ -122,7 +122,7 @@ def simple_fc_net(img, label, use_py_func_op): ) dummy_var = ( - fluid.default_main_program() + base.default_main_program() .current_block() .create_var(name='test_tmp_var', dtype='float32', shape=[1]) ) @@ -133,12 +133,12 @@ def simple_fc_net(img, label, use_py_func_op): paddle.static.py_func(func=dummy_func_with_no_output, x=loss, out=None) loss_out = ( - fluid.default_main_program() + base.default_main_program() .current_block() .create_var(dtype='float32', shape=[-1, 1]) ) dummy_var_out = ( - fluid.default_main_program() + base.default_main_program() .current_block() .create_var(dtype='float32', shape=[1]) ) @@ -172,11 +172,11 @@ def reader(): def test_main(use_cuda, use_py_func_op, use_parallel_executor): - if use_cuda and not fluid.core.is_compiled_with_cuda(): + if use_cuda and not base.core.is_compiled_with_cuda(): return None - with fluid.program_guard(fluid.Program(), fluid.Program()): - with fluid.scope_guard(fluid.core.Scope()): + with base.program_guard(base.Program(), base.Program()): + with base.scope_guard(base.core.Scope()): gen = paddle.seed(1) np.random.seed(1) img = paddle.static.data( @@ -189,19 +189,17 @@ def test_main(use_cuda, use_py_func_op, use_parallel_executor): optimizer = paddle.optimizer.SGD(learning_rate=1e-3) optimizer.minimize(loss) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - feeder = fluid.DataFeeder(feed_list=[img, label], place=place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + feeder = base.DataFeeder(feed_list=[img, label], place=place) r = paddle.batch(reader, batch_size=10) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) - train_cp = fluid.default_main_program() + train_cp = base.default_main_program() if use_parallel_executor: - train_cp = compiler.CompiledProgram( - fluid.default_main_program() - ) + train_cp = compiler.CompiledProgram(base.default_main_program()) fetch_list = [loss.name] else: fetch_list = [loss] diff --git a/test/legacy_test/test_py_reader_combination.py b/test/legacy_test/test_py_reader_combination.py index cd228c244c212..df62b0b61ccf7 100644 --- a/test/legacy_test/test_py_reader_combination.py +++ b/test/legacy_test/test_py_reader_combination.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestPyReaderCombination(unittest.TestCase): @@ -55,7 +55,7 @@ def _reset_iterable_reader(self, py_reader): py_reader._loader._reset() def main_impl(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): image = paddle.static.data( name='image', dtype='float32', shape=[-1, 784] ) @@ -63,10 +63,10 @@ def main_impl(self, place): name='label', dtype='int64', shape=[-1, 1] ) - py_reader1 = fluid.io.PyReader( + py_reader1 = base.io.PyReader( feed_list=[image, label], capacity=16, iterable=True ) - py_reader2 = fluid.io.PyReader( + py_reader2 = base.io.PyReader( feed_list=[image, label], capacity=16, iterable=True ) @@ -94,10 +94,10 @@ def main_impl(self, place): self._reset_iterable_reader(py_reader2) def get_places(self): - if fluid.is_compiled_with_cuda(): - return [fluid.CUDAPlace(0), fluid.CPUPlace()] + if base.is_compiled_with_cuda(): + return [base.CUDAPlace(0), base.CPUPlace()] else: - return [fluid.CPUPlace()] + return [base.CPUPlace()] def test_main(self): for p in self.get_places(): diff --git a/test/legacy_test/test_py_reader_return_list.py b/test/legacy_test/test_py_reader_return_list.py index cb3f4ca9b1a28..49c476c926254 100644 --- a/test/legacy_test/test_py_reader_return_list.py +++ b/test/legacy_test/test_py_reader_return_list.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestPyReader(unittest.TestCase): @@ -37,11 +37,11 @@ def reader(): return reader for return_list in [True, False]: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): image = paddle.static.data( name='image', shape=[-1, 784, 784], dtype='float32' ) - reader = fluid.io.PyReader( + reader = base.io.PyReader( feed_list=[image], capacity=4, iterable=True, @@ -53,11 +53,11 @@ def reader(): paddle.batch( user_defined_reader, batch_size=self.batch_size ), - fluid.core.CPUPlace(), + base.core.CPUPlace(), ) # definition of network is omitted - executor = fluid.Executor(fluid.core.CPUPlace()) - executor.run(fluid.default_main_program()) + executor = base.Executor(base.core.CPUPlace()) + executor.run(base.default_main_program()) for _ in range(self.epoch_num): for data in reader(): @@ -66,13 +66,13 @@ def reader(): else: executor.run(feed=data) - with fluid.dygraph.guard(): - batch_py_reader = fluid.io.PyReader(capacity=2) + with base.dygraph.guard(): + batch_py_reader = base.io.PyReader(capacity=2) user_defined_reader = reader_creator_random_image(784, 784) batch_py_reader.decorate_sample_generator( user_defined_reader, batch_size=self.batch_size, - places=fluid.core.CPUPlace(), + places=base.core.CPUPlace(), ) for epoch in range(self.epoch_num): diff --git a/test/legacy_test/test_py_reader_sample_generator.py b/test/legacy_test/test_py_reader_sample_generator.py index 2023b96b5ec03..ef520efb2683e 100644 --- a/test/legacy_test/test_py_reader_sample_generator.py +++ b/test/legacy_test/test_py_reader_sample_generator.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base os.environ['CPU_NUM'] = '1' @@ -58,7 +58,7 @@ def run_main(self, reader, use_sample_generator, iterable, drop_last): name='image', dtype='float32', shape=[-1, 784] ) label = paddle.static.data(name='label', dtype='int64', shape=[-1, 1]) - py_reader = fluid.io.PyReader( + py_reader = base.io.PyReader( feed_list=[image, label], capacity=16, iterable=iterable, @@ -70,11 +70,11 @@ def run_main(self, reader, use_sample_generator, iterable, drop_last): if not use_sample_generator: py_reader.decorate_sample_list_generator( - batch_reader, places=fluid.cpu_places() + batch_reader, places=base.cpu_places() ) else: py_reader.decorate_sample_generator( - reader, self.batch_size, drop_last, places=fluid.cpu_places() + reader, self.batch_size, drop_last, places=base.cpu_places() ) if drop_last: @@ -82,8 +82,8 @@ def run_main(self, reader, use_sample_generator, iterable, drop_last): else: batch_num = math.ceil(float(self.sample_num) / self.batch_size) - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) for _ in range(self.epoch_num): if py_reader.iterable: step = 0 @@ -102,7 +102,7 @@ def run_main(self, reader, use_sample_generator, iterable, drop_last): self.assertArrayEqual(img, all_datas[step][0]) self.assertArrayEqual(lbl, all_datas[step][1]) step += 1 - except fluid.core.EOFException: + except base.core.EOFException: py_reader.reset() self.assertEqual(step, len(all_datas)) break @@ -116,7 +116,7 @@ def test_main(self): for use_sample_generator in [False, True]: for iterable in [False, True]: for drop_last in [False, True]: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): self.run_main( reader, use_sample_generator, iterable, drop_last ) diff --git a/test/legacy_test/test_pybind_interface.py b/test/legacy_test/test_pybind_interface.py index 3a51c4bd09aa7..89c577e206e44 100644 --- a/test/legacy_test/test_pybind_interface.py +++ b/test/legacy_test/test_pybind_interface.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core class TestPybindInference(unittest.TestCase): diff --git a/test/legacy_test/test_pyramid_hash_op.py b/test/legacy_test/test_pyramid_hash_op.py index 8ef1173ac7572..ac1ce7d7c1db3 100644 --- a/test/legacy_test/test_pyramid_hash_op.py +++ b/test/legacy_test/test_pyramid_hash_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.incubate.layers.nn import search_pyramid_hash @@ -42,11 +42,11 @@ def test_api(self): black_list_len=2800, seed=3, lr=0.002, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="PyramidHash_emb_0", learning_rate=0, ), - param_attr_wl=fluid.ParamAttr( + param_attr_wl=base.ParamAttr( name="Filter", learning_rate=0, ), @@ -55,13 +55,13 @@ def test_api(self): name=None, ) - place = fluid.CPUPlace() - x_tensor = fluid.create_lod_tensor( + place = base.CPUPlace() + x_tensor = base.create_lod_tensor( np.random.randint(0, num_voc, x_shape).astype('int32'), x_lod, place ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={'x': x_tensor}, fetch_list=[hash_embd], return_numpy=False ) diff --git a/test/legacy_test/test_python_operator_overriding.py b/test/legacy_test/test_python_operator_overriding.py index 0beaf79a4750a..def870b0aa65b 100644 --- a/test/legacy_test/test_python_operator_overriding.py +++ b/test/legacy_test/test_python_operator_overriding.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework paddle.enable_static() @@ -39,16 +39,16 @@ def check_result(self, fn, place, dtype): ) out = fn(x_var, y_var) - exe = fluid.Executor(place) + exe = base.Executor(place) - exe.run(fluid.default_startup_program()) - fluid_out = exe.run( - fluid.default_main_program(), + exe.run(base.default_startup_program()) + base_out = exe.run( + base.default_main_program(), feed={'x': x_data, 'y': y_data}, fetch_list=[out], ) - np.testing.assert_array_equal(python_out, fluid_out[0]) + np.testing.assert_array_equal(python_out, base_out[0]) def test_override(self): # compare func to check @@ -62,9 +62,9 @@ def test_override(self): ] # places to check - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) # dtypes to check dtypes = ['int32', 'float32'] diff --git a/test/legacy_test/test_qr_op.py b/test/legacy_test/test_qr_op.py index ee7ae544f59f6..7352e16bd65c3 100644 --- a/test/legacy_test/test_qr_op.py +++ b/test/legacy_test/test_qr_op.py @@ -19,8 +19,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestQrOp(OpTest): @@ -143,9 +143,9 @@ def run_qr_dygraph(shape, mode, dtype): np_q = np.zeros(np_q_shape).astype(np_dtype) np_r = np.zeros(np_r_shape).astype(np_dtype) places = [] - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: batch_size = a.size // (a.shape[-1] * a.shape[-2]) for i in range(batch_size): @@ -209,11 +209,11 @@ def run_qr_static(shape, mode, dtype): np_q = np.zeros(np_q_shape).astype(np_dtype) np_r = np.zeros(np_r_shape).astype(np_dtype) places = [] - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): batch_size = a.size // (a.shape[-1] * a.shape[-2]) for i in range(batch_size): coord = np.unravel_index(i, a.shape[:-2]) @@ -229,9 +229,9 @@ def run_qr_static(shape, mode, dtype): ) if mode == "r": r = paddle.linalg.qr(x, mode=mode) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": a}, fetch_list=[r], ) @@ -240,9 +240,9 @@ def run_qr_static(shape, mode, dtype): ) else: q, r = paddle.linalg.qr(x, mode=mode) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": a}, fetch_list=[q, r], ) diff --git a/test/legacy_test/test_query_op.py b/test/legacy_test/test_query_op.py index edb1848f03baf..8c0f6ad3078f8 100644 --- a/test/legacy_test/test_query_op.py +++ b/test/legacy_test/test_query_op.py @@ -15,7 +15,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core class TestCudnnVersion(unittest.TestCase): diff --git a/test/legacy_test/test_queue.py b/test/legacy_test/test_queue.py index a594ec56cc3c5..5a1cbd53d43aa 100644 --- a/test/legacy_test/test_queue.py +++ b/test/legacy_test/test_queue.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestQueue(unittest.TestCase): @@ -27,10 +27,10 @@ def test_eq(self): test queue_generator op, enqueue op and dequeue op. """ - main_program = fluid.Program() - startup_program = fluid.Program() + main_program = base.Program() + startup_program = base.Program() value = np.random.rand(1) - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): data_in = paddle.static.create_global_var( shape=[2, 3], value=value, @@ -66,11 +66,11 @@ def test_eq(self): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) (ret,) = exe.run(main_program, fetch_list=[data_out.name]) np.testing.assert_allclose( diff --git a/test/legacy_test/test_rad2deg.py b/test/legacy_test/test_rad2deg.py index 710d77f0d9fc1..45841b8ca9d78 100644 --- a/test/legacy_test/test_rad2deg.py +++ b/test/legacy_test/test_rad2deg.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -33,22 +33,22 @@ def setUp(self): self.out_np = np.rad2deg(self.x_np) def test_static_graph(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(startup_program, train_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(startup_program, train_program): x = paddle.static.data( name='input', dtype=self.x_dtype, shape=self.x_shape ) out = paddle.rad2deg(x) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input': self.x_np}, fetch_list=[out], ) diff --git a/test/legacy_test/test_rand_op.py b/test/legacy_test/test_rand_op.py index 76bc134dafde2..c51005d1fa28c 100644 --- a/test/legacy_test/test_rand_op.py +++ b/test/legacy_test/test_rand_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid, rand -from paddle.fluid import Program, core, program_guard +from paddle import base, rand +from paddle.base import Program, core, program_guard class TestRandOpError(unittest.TestCase): @@ -32,8 +32,8 @@ def test_errors(self): with program_guard(main_prog, start_prog): def test_Variable(): - x1 = fluid.create_lod_tensor( - np.zeros((4, 784)), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.zeros((4, 784)), [[1, 1, 1, 1]], base.CPUPlace() ) rand(x1) @@ -53,12 +53,12 @@ class TestRandOp(unittest.TestCase): """ def run_net(self, use_cuda=False): - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) - train_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + train_program = base.Program() + startup_program = base.Program() + with base.program_guard(train_program, startup_program): result_0 = rand([3, 4]) result_1 = rand([3, 4], 'float64') @@ -98,8 +98,8 @@ class TestRandOpForDygraph(unittest.TestCase): """ def run_net(self, use_cuda=False): - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - with fluid.dygraph.guard(place): + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + with base.dygraph.guard(place): rand([3, 4]) rand([3, 4], 'float64') @@ -124,17 +124,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.rand([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.rand([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.rand([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) if paddle.is_compiled_with_cuda(): paddle.set_device('gpu') diff --git a/test/legacy_test/test_randint_op.py b/test/legacy_test/test_randint_op.py index 073b8255be957..478b980ac1604 100644 --- a/test/legacy_test/test_randint_op.py +++ b/test/legacy_test/test_randint_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import Program, program_guard paddle.enable_static() @@ -219,14 +219,14 @@ def test_dygraph(self): paddle.enable_static() def test_static(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.randint(-10, 10, []) # Test compile shape self.assertEqual(x.shape, ()) # Test runtime shape - exe = fluid.Executor() + exe = base.Executor() result = exe.run(fetch_list=[x]) self.assertEqual(result[0].shape, ()) diff --git a/test/legacy_test/test_randn_op.py b/test/legacy_test/test_randn_op.py index b298557b0b9f3..6bc3f48bc3faa 100644 --- a/test/legacy_test/test_randn_op.py +++ b/test/legacy_test/test_randn_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard diff --git a/test/legacy_test/test_random_routing_op.py b/test/legacy_test/test_random_routing_op.py index 8977e30011044..21a1746dd057f 100644 --- a/test/legacy_test/test_random_routing_op.py +++ b/test/legacy_test/test_random_routing_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle +from paddle.base import core from paddle.distributed.models.moe import utils -from paddle.fluid import core def random_routing(topk_idx, topk_value, prob, topk=2): diff --git a/test/legacy_test/test_random_seed.py b/test/legacy_test/test_random_seed.py index 70583a8cf2660..ead15119a9922 100644 --- a/test/legacy_test/test_random_seed.py +++ b/test/legacy_test/test_random_seed.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.tensor import random @@ -31,7 +31,7 @@ class TestGeneratorSeed(unittest.TestCase): def test_generator_uniform_random_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = paddle.uniform([10], dtype="float32", min=0.0, max=1.0) @@ -56,19 +56,19 @@ def test_generator_uniform_random_dygraph(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_generator_uniform_random_static(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = paddle.uniform(shape=[3, 4]) result_2 = paddle.uniform(shape=[3, 4]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -90,7 +90,7 @@ def test_generator_uniform_random_static(self): self.assertTrue(not np.allclose(out1_res2, out1_res1)) def test_gen_dropout_dygraph(self): - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(111111111) st = gen.get_state() @@ -109,18 +109,18 @@ def test_gen_dropout_dygraph(self): np.testing.assert_allclose(y_np, y1_np, rtol=1e-05) def test_gen_dropout_static(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. x_1 = paddle.uniform(shape=[2, 10]) y_1 = paddle.nn.functional.dropout(x_1, 0.5) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run(train_program, feed={}, fetch_list=[y_1]) # gen.set_state(cur_state) @@ -135,7 +135,7 @@ def test_gen_dropout_static(self): def test_generator_gaussian_random_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = random.gaussian([10], dtype="float32") @@ -156,19 +156,19 @@ def test_generator_gaussian_random_dygraph(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_generator_gaussian_random_static(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = random.gaussian(shape=[3, 4]) result_2 = random.gaussian(shape=[3, 4]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -192,7 +192,7 @@ def test_generator_gaussian_random_static(self): def test_generator_randint_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = paddle.randint(low=10, shape=[10], dtype="int32") @@ -213,19 +213,19 @@ def test_generator_randint_dygraph(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_generator_uniform_random_static_1(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = paddle.uniform(shape=[3, 4]) result_2 = paddle.uniform(shape=[3, 4]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -248,7 +248,7 @@ def test_generator_uniform_random_static_1(self): def test_generator_randint_dygraph_1(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = paddle.randint(low=1) @@ -267,19 +267,19 @@ def test_generator_randint_dygraph_1(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_generator_ranint_static(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = paddle.randint(low=10, shape=[3, 4]) result_2 = paddle.randint(low=10, shape=[3, 4]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -304,7 +304,7 @@ def test_generator_ranint_static(self): def test_generator_randperm_dygraph(self): """Test Generator seed.""" - fluid.enable_dygraph() + base.enable_dygraph() gen = paddle.seed(12312321111) x = paddle.randperm(10) @@ -325,19 +325,19 @@ def test_generator_randperm_dygraph(self): np.testing.assert_allclose(x_np, x3_np, rtol=1e-05) def test_generator_randperm_static(self): - fluid.disable_dygraph() + base.disable_dygraph() paddle.seed(123123143) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. result_1 = paddle.randperm(10) result_2 = paddle.randperm(10) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] @@ -360,14 +360,14 @@ def test_generator_randperm_static(self): self.assertTrue(not np.allclose(out1_res2, out1_res1)) def test_gen_TruncatedNormal_initializer(self): - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(123123143) cur_state = gen.get_state() - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): # example 1: # attr shape is a list which doesn't contain tensor Variable. x = paddle.uniform(shape=[2, 10]) @@ -386,14 +386,14 @@ def test_gen_TruncatedNormal_initializer(self): ), ) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) exe.run(startup_program) out1 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] ) gen.manual_seed(123123143) - with fluid.program_guard(train_program, startup_program): + with base.program_guard(train_program, startup_program): exe.run(startup_program) out2 = exe.run( train_program, feed={}, fetch_list=[result_1, result_2] diff --git a/test/legacy_test/test_randperm_op.py b/test/legacy_test/test_randperm_op.py index 14bf49b387b63..bc14e4cbd4452 100644 --- a/test/legacy_test/test_randperm_op.py +++ b/test/legacy_test/test_randperm_op.py @@ -22,7 +22,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard diff --git a/test/legacy_test/test_rank_attention_op.py b/test/legacy_test/test_rank_attention_op.py index d699a6311a3f4..f48fa41ba989a 100644 --- a/test/legacy_test/test_rank_attention_op.py +++ b/test/legacy_test/test_rank_attention_op.py @@ -18,7 +18,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core def gen_input_help(input, rank_offset, max_rank, max_size): diff --git a/test/legacy_test/test_raw_program_optimizer.py b/test/legacy_test/test_raw_program_optimizer.py index 906b67d61d8c8..362970cdf2d3f 100644 --- a/test/legacy_test/test_raw_program_optimizer.py +++ b/test/legacy_test/test_raw_program_optimizer.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet @@ -52,8 +52,8 @@ def test_single_gpu(self): sharding_startup_program = paddle.static.Program() strategy = fleet.DistributedStrategy() strategy.without_graph_optimization = True - with fluid.program_guard(sharding_program, sharding_startup_program): - with fluid.unique_name.guard(): + with base.program_guard(sharding_program, sharding_startup_program): + with base.unique_name.guard(): input_x = paddle.static.data( name="x", shape=[None, 32], dtype='float32' ) diff --git a/test/legacy_test/test_reader_reset.py b/test/legacy_test/test_reader_reset.py index 9a756af12ecfd..f91ea8a86be67 100644 --- a/test/legacy_test/test_reader_reset.py +++ b/test/legacy_test/test_reader_reset.py @@ -20,8 +20,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import compiler +from paddle import base +from paddle.base import compiler class TestReaderReset(unittest.TestCase): @@ -33,7 +33,7 @@ def fake_data_generator(): return fake_data_generator def setUp(self): - self.use_cuda = fluid.core.is_compiled_with_cuda() + self.use_cuda = base.core.is_compiled_with_cuda() self.ins_shape = [3] self.batch_size = 5 self.batch_num = 20 @@ -42,17 +42,17 @@ def setUp(self): self.prepare_data() def main(self, with_double_buffer): - main_prog = fluid.Program() - startup_prog = fluid.Program() + main_prog = base.Program() + startup_prog = base.Program() - with fluid.program_guard(main_prog, startup_prog): + with base.program_guard(main_prog, startup_prog): image = paddle.static.data( name='image', shape=[-1] + self.ins_shape, dtype='float32' ) label = paddle.static.data( name='label', shape=[-1, 1], dtype='int64' ) - data_reader_handle = fluid.io.PyReader( + data_reader_handle = base.io.PyReader( feed_list=[image, label], capacity=16, iterable=False, @@ -60,8 +60,8 @@ def main(self, with_double_buffer): ) fetch_list = [image.name, label.name] - place = fluid.CUDAPlace(0) if self.use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if self.use_cuda else base.CPUPlace() + exe = base.Executor(place) exe.run(startup_prog) data_reader_handle.decorate_sample_list_generator( @@ -85,7 +85,7 @@ def main(self, with_double_buffer): ) * label_val.reshape((ins_num, 1)) self.assertEqual(data_val.all(), broadcasted_label.all()) batch_id += 1 - except fluid.core.EOFException: + except base.core.EOFException: data_reader_handle.reset() pass_count += 1 self.assertEqual(pass_count * self.batch_num, batch_id) diff --git a/test/legacy_test/test_real_imag_op.py b/test/legacy_test/test_real_imag_op.py index 4a33add68e97e..3da20edb1b726 100644 --- a/test/legacy_test/test_real_imag_op.py +++ b/test/legacy_test/test_real_imag_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid, static +from paddle import base, static numpy_apis = { "real": np.real, @@ -125,7 +125,7 @@ def test_in_dynamic_mode(self): np_res = numpy_apis[self.api](input) for place in self.places: # it is more convenient to use `guard` than `enable/disable_**` here - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_t = paddle.to_tensor(input) res = paddle_apis[self.api](input_t).numpy() np.testing.assert_array_equal(np_res, res) @@ -151,7 +151,7 @@ def test_dtype_error(self): # in dynamic mode with self.assertRaises(RuntimeError): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input = np.random.random(self._shape).astype("float32") input_t = paddle.to_tensor(input) res = paddle_apis[self.api](input_t) diff --git a/test/legacy_test/test_reduce_op.py b/test/legacy_test/test_reduce_op.py index 4043cb8ccba26..91bbd7acdbe26 100644 --- a/test/legacy_test/test_reduce_op.py +++ b/test/legacy_test/test_reduce_op.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestSumOp(OpTest): @@ -1578,11 +1578,11 @@ def test_check_grad(self): class TestReduceSumOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program(), Program()): # The input type of reduce_sum_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.sum, x1) # The input dtype of reduce_sum_op must be float32 or float64 or int32 or int64. @@ -1597,17 +1597,17 @@ def run_static( if np_axis is None: np_axis = attr_axis - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data = paddle.static.data("data", shape=shape, dtype=x_dtype) result_sum = paddle.sum( x=data, axis=attr_axis, dtype=attr_dtype ) - exe = fluid.Executor(place) + exe = base.Executor(place) input_data = np.random.rand(*shape).astype(x_dtype) (res,) = exe.run( feed={"data": input_data}, fetch_list=[result_sum] @@ -1654,8 +1654,8 @@ def test_static(self): def test_dygraph(self): np_x = np.random.random([2, 3, 4]).astype('int32') - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(np_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(np_x) out0 = paddle.sum(x).numpy() out1 = paddle.sum(x, axis=0).numpy() out2 = paddle.sum(x, axis=(0, 1)).numpy() @@ -1671,49 +1671,49 @@ class TestAllAPI(unittest.TestCase): def setUp(self): np.random.seed(123) paddle.enable_static() - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data(name="input", shape=[4, 4], dtype="bool") result = paddle.all(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("bool") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.all(input_np)).all()) def check_static_float_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float" ) result = paddle.all(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("float") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.all(input_np)).all()) def check_static_int_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data(name="input", shape=[4, 4], dtype="int") result = paddle.all(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("int") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -1728,7 +1728,7 @@ def test_static(self): def test_dygraph(self): paddle.disable_static() for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): np_x = np.random.randint(0, 2, (12, 10)).astype(np.bool_) x = paddle.assign(np_x) x = paddle.cast(x, 'bool') @@ -1772,49 +1772,49 @@ class TestAnyAPI(unittest.TestCase): def setUp(self): np.random.seed(123) paddle.enable_static() - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data(name="input", shape=[4, 4], dtype="bool") result = paddle.any(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("bool") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.any(input_np)).all()) def check_static_float_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float" ) result = paddle.any(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("float") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) self.assertTrue((fetches[0] == np.any(input_np)).all()) def check_static_int_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data(name="input", shape=[4, 4], dtype="int") result = paddle.any(x=input) input_np = np.random.randint(0, 2, [4, 4]).astype("int") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -1829,7 +1829,7 @@ def test_static(self): def test_dygraph(self): paddle.disable_static() for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): np_x = np.random.randint(0, 2, (12, 10)).astype(np.bool_) x = paddle.assign(np_x) x = paddle.cast(x, 'bool') @@ -1874,7 +1874,7 @@ def test_dygraph(self): class TestAllZeroError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_registered_phi_kernels.py b/test/legacy_test/test_registered_phi_kernels.py index 67fcb2b9a7b0c..143f7999024f7 100644 --- a/test/legacy_test/test_registered_phi_kernels.py +++ b/test/legacy_test/test_registered_phi_kernels.py @@ -18,7 +18,7 @@ import yaml -from paddle.fluid import core +from paddle.base import core def parse_kernels_name(op_item): diff --git a/test/legacy_test/test_registry.py b/test/legacy_test/test_registry.py index 0b3690221c0a8..3c0b34a599cbe 100644 --- a/test/legacy_test/test_registry.py +++ b/test/legacy_test/test_registry.py @@ -18,7 +18,7 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid +from paddle import base class TestRegistry(unittest.TestCase): @@ -27,8 +27,8 @@ def test_registry_layer(self): x = paddle.static.data(name='X', shape=[-1, 10, 10], dtype='float32') output = paddle.mean(x) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) X = np.random.random((10, 10)).astype("float32") mean_out = exe.run(feed={"X": X}, fetch_list=[output]) self.assertAlmostEqual(np.mean(X), mean_out[0], delta=1e-5) diff --git a/test/legacy_test/test_regularizer.py b/test/legacy_test/test_regularizer.py index da0fde3558a5a..a0edf81058b9e 100644 --- a/test/legacy_test/test_regularizer.py +++ b/test/legacy_test/test_regularizer.py @@ -20,9 +20,9 @@ import numpy as np import paddle -from paddle import fluid, regularizer -from paddle.fluid import core, framework -from paddle.fluid.backward import append_backward +from paddle import base, regularizer +from paddle.base import core, framework +from paddle.base.backward import append_backward class TestL2Decay(unittest.TestCase): @@ -125,7 +125,7 @@ def bow_net( """ BOW net This model is from https://github.com/PaddlePaddle/models: - fluid/PaddleNLP/text_classification/nets.py + base/PaddleNLP/text_classification/nets.py """ emb = paddle.static.nn.embedding( input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim] @@ -161,18 +161,18 @@ def get_places(self): @contextlib.contextmanager def scope_prog_guard(self, main_prog, startup_prog): - scope = fluid.core.Scope() - with fluid.unique_name.guard(): - with fluid.scope_guard(scope): - with fluid.program_guard(main_prog, startup_prog): + scope = base.core.Scope() + with base.unique_name.guard(): + with base.scope_guard(scope): + with base.program_guard(main_prog, startup_prog): yield def run_program(self, place, feed_list): - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=feed_list, place=place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) - main_prog = fluid.default_main_program() + main_prog = base.default_main_program() param_list = [var.name for var in main_prog.block(0).all_parameters()] param_sum = [] @@ -189,8 +189,8 @@ def run_program(self, place, feed_list): def check_l2decay_regularizer(self, place, model): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - main_prog = fluid.framework.Program() - startup_prog = fluid.framework.Program() + main_prog = base.framework.Program() + startup_prog = base.framework.Program() with self.scope_prog_guard( main_prog=main_prog, startup_prog=startup_prog ): @@ -214,8 +214,8 @@ def check_l2decay_regularizer(self, place, model): def check_l2decay(self, place, model): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - main_prog = fluid.framework.Program() - startup_prog = fluid.framework.Program() + main_prog = base.framework.Program() + startup_prog = base.framework.Program() with self.scope_prog_guard( main_prog=main_prog, startup_prog=startup_prog @@ -229,7 +229,7 @@ def check_l2decay(self, place, model): avg_cost_l2 = model(data, label, self.word_len) - param_list = fluid.default_main_program().block(0).all_parameters() + param_list = base.default_main_program().block(0).all_parameters() para_sum = [] for para in param_list: para_mul = paddle.square(x=para) @@ -267,14 +267,14 @@ def test_repeated_regularization(self): fc_param_attr = paddle.ParamAttr( regularizer=paddle.regularizer.L1Decay() ) - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.uniform([2, 2, 3]) out = paddle.static.nn.fc(x, 5, weight_attr=fc_param_attr) loss = paddle.sum(out) sgd = paddle.optimizer.SGD(learning_rate=0.1, weight_decay=l2) sgd.minimize(loss) - with fluid.dygraph.guard(): - input = fluid.dygraph.to_variable( + with base.dygraph.guard(): + input = base.dygraph.to_variable( np.random.randn(3, 2).astype('float32') ) paddle.seed(1) @@ -289,14 +289,14 @@ def test_repeated_regularization(self): loss1 = linear1(input) loss1.backward() - # set l2 regularizer in optimizer, but l1 in fluid.ParamAttr + # set l2 regularizer in optimizer, but l1 in base.ParamAttr paddle.optimizer.SGD( parameters=linear1.parameters(), learning_rate=1e-2, weight_decay=l2, ).minimize(loss1) - # only set l1 in fluid.ParamAttr + # only set l1 in base.ParamAttr loss2 = linear2(input) loss2.backward() paddle.optimizer.SGD( @@ -307,13 +307,13 @@ def test_repeated_regularization(self): linear1.weight.numpy(), linear2.weight.numpy(), rtol=1e-05, - err_msg='weight should use the regularization in fluid.ParamAttr!', + err_msg='weight should use the regularization in base.ParamAttr!', ) np.testing.assert_allclose( linear1.bias.numpy(), linear2.bias.numpy(), rtol=1e-05, - err_msg='bias should use the regularization in fluid.ParamAttr!', + err_msg='bias should use the regularization in base.ParamAttr!', ) diff --git a/test/legacy_test/test_regularizer_api.py b/test/legacy_test/test_regularizer_api.py index 5a30dcb63649e..7eb6ecadefb0d 100644 --- a/test/legacy_test/test_regularizer_api.py +++ b/test/legacy_test/test_regularizer_api.py @@ -20,8 +20,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def bow_net( @@ -37,7 +37,7 @@ def bow_net( """ BOW net This model is from https://github.com/PaddlePaddle/models: - fluid/PaddleNLP/text_classification/nets.py + base/PaddleNLP/text_classification/nets.py """ emb = paddle.static.nn.embedding( input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim] @@ -74,18 +74,18 @@ def get_places(self): @contextlib.contextmanager def scope_prog_guard(self, main_prog, startup_prog): - scope = fluid.core.Scope() - with fluid.unique_name.guard(): - with fluid.scope_guard(scope): - with fluid.program_guard(main_prog, startup_prog): + scope = base.core.Scope() + with base.unique_name.guard(): + with base.scope_guard(scope): + with base.program_guard(main_prog, startup_prog): yield def run_program(self, place, feed_list): - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=feed_list, place=place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) - main_prog = fluid.default_main_program() + main_prog = base.default_main_program() param_list = [var.name for var in main_prog.block(0).all_parameters()] param_sum = [] @@ -102,8 +102,8 @@ def run_program(self, place, feed_list): def check_l2decay_regularizer(self, place, model): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - main_prog = fluid.framework.Program() - startup_prog = fluid.framework.Program() + main_prog = base.framework.Program() + startup_prog = base.framework.Program() with self.scope_prog_guard( main_prog=main_prog, startup_prog=startup_prog ): @@ -127,8 +127,8 @@ def check_l2decay_regularizer(self, place, model): def check_l2decay(self, place, model): paddle.seed(1) paddle.framework.random._manual_program_seed(1) - main_prog = fluid.framework.Program() - startup_prog = fluid.framework.Program() + main_prog = base.framework.Program() + startup_prog = base.framework.Program() with self.scope_prog_guard( main_prog=main_prog, startup_prog=startup_prog @@ -142,7 +142,7 @@ def check_l2decay(self, place, model): avg_cost_l2 = model(data, label, self.word_len) - param_list = fluid.default_main_program().block(0).all_parameters() + param_list = base.default_main_program().block(0).all_parameters() para_sum = [] for para in param_list: para_mul = paddle.square(x=para) @@ -182,14 +182,14 @@ def test_repeated_regularization(self): fc_param_attr = paddle.ParamAttr( regularizer=paddle.regularizer.L1Decay() ) - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.uniform([2, 2, 3]) out = paddle.static.nn.fc(x, 5, weight_attr=fc_param_attr) loss = paddle.sum(out) sgd = paddle.optimizer.SGD(learning_rate=0.1, weight_decay=l2) sgd.minimize(loss) - with fluid.dygraph.guard(): - input = fluid.dygraph.to_variable( + with base.dygraph.guard(): + input = base.dygraph.to_variable( np.random.randn(3, 2).astype('float32') ) paddle.seed(1) @@ -204,14 +204,14 @@ def test_repeated_regularization(self): loss1 = linear1(input) loss1.backward() - # set l2 regularizer in optimizer, but l1 in fluid.ParamAttr + # set l2 regularizer in optimizer, but l1 in base.ParamAttr paddle.optimizer.SGD( parameters=linear1.parameters(), learning_rate=1e-2, weight_decay=l2, ).minimize(loss1) - # only set l1 in fluid.ParamAttr + # only set l1 in base.ParamAttr loss2 = linear2(input) loss2.backward() paddle.optimizer.SGD( @@ -222,13 +222,13 @@ def test_repeated_regularization(self): linear1.weight.numpy(), linear2.weight.numpy(), rtol=1e-05, - err_msg='weight should use the regularization in fluid.ParamAttr!', + err_msg='weight should use the regularization in base.ParamAttr!', ) np.testing.assert_allclose( linear1.bias.numpy(), linear2.bias.numpy(), rtol=1e-05, - err_msg='bias should use the regularization in fluid.ParamAttr!', + err_msg='bias should use the regularization in base.ParamAttr!', ) diff --git a/test/legacy_test/test_renorm_op.py b/test/legacy_test/test_renorm_op.py index ae857a8bfb849..89029b02bd963 100644 --- a/test/legacy_test/test_renorm_op.py +++ b/test/legacy_test/test_renorm_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.set_device('cpu') @@ -40,7 +40,7 @@ def test_renorm_api(self): with program_guard(Program(), Program()): x = paddle.static.data(name="x", shape=[-1, 2, 3], dtype='float64') z = paddle.renorm(x, self.p, self.dim, self.max_norm) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={"x": self.data_x}, fetch_list=[z], return_numpy=False ) @@ -61,7 +61,7 @@ def test_renorm_api(self): def test_dygraph_api(self): self.input_data() # case axis none - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): input = [[[2.0, 2, -2], [3, 0.3, 3]], [[2, -8, 2], [3.1, 3.7, 3]]] x = paddle.to_tensor(input, stop_gradient=False) y = paddle.renorm(x, 1.0, 2, 2.05) @@ -90,7 +90,7 @@ def test_dygraph_api(self): expected_grad, np.array(x.grad), rtol=1e-05 ) # #test exception: - with fluid.dygraph.guard(): + with base.dygraph.guard(): input = [[[2.0, 2, -2], [3, 0.3, 3]], [[2, -8, 2], [3.1, 3.7, 3]]] x = paddle.to_tensor(input, stop_gradient=False) exp = False diff --git a/test/legacy_test/test_repeat_interleave_op.py b/test/legacy_test/test_repeat_interleave_op.py index 3c553fecb1b15..a0c1a3bad03d0 100644 --- a/test/legacy_test/test_repeat_interleave_op.py +++ b/test/legacy_test/test_repeat_interleave_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard class TestRepeatInterleaveOp(OpTest): @@ -129,7 +129,7 @@ def test_repeat_interleave_api(self): ) index.desc.set_need_check_feed(False) z = paddle.repeat_interleave(x, index, axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'repeats_': self.data_index}, fetch_list=[z.name], @@ -150,7 +150,7 @@ def test_repeat_interleave_api(self): ) index.desc.set_need_check_feed(False) z = paddle.repeat_interleave(x, index, axis=0) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={ 'x': self.data_x, @@ -167,7 +167,7 @@ def test_repeat_interleave_api(self): x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32') x.desc.set_need_check_feed(False) z = paddle.repeat_interleave(x, repeats, axis=0) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x}, fetch_list=[z.name], return_numpy=False ) @@ -179,7 +179,7 @@ def test_repeat_interleave_api(self): x = paddle.static.data(name='x', shape=[-1], dtype="float32") x.desc.set_need_check_feed(False) z = paddle.repeat_interleave(x, repeats) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_zero_dim_x}, fetch_list=[z.name], @@ -199,7 +199,7 @@ def test_repeat_interleave_api(self): ) index.desc.set_need_check_feed(False) z = paddle.repeat_interleave(x, index, axis=-1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x, 'repeats_': self.data_index}, fetch_list=[z.name], @@ -214,17 +214,17 @@ def test_dygraph_api(self): input_x = np.array([[1, 2, 1], [1, 2, 3]]).astype('int32') index_x = np.array([1, 1, 2, 1, 2, 2]).astype('int32') - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(input_x) - index = fluid.dygraph.to_variable(index_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(input_x) + index = base.dygraph.to_variable(index_x) z = paddle.repeat_interleave(x, index, None) np_z = z.numpy() expect_out = np.repeat(input_x, index_x, axis=None) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case repeats int - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(input_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(input_x) index = 2 z = paddle.repeat_interleave(x, index, None) np_z = z.numpy() @@ -232,17 +232,17 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 1: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.repeat_interleave(x, index, -1) np_z = z.numpy() expect_out = np.repeat(self.data_x, self.data_index, axis=-1) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.repeat_interleave(x, index, 1) np_z = z.numpy() expect_out = np.repeat(self.data_x, self.data_index, axis=1) @@ -250,17 +250,17 @@ def test_dygraph_api(self): # case 2: index_x = np.array([1, 2, 1]).astype('int32') - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(index_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(index_x) z = paddle.repeat_interleave(x, index, axis=0) np_z = z.numpy() expect_out = np.repeat(self.data_x, index, axis=0) np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 3 zero_dim: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_zero_dim_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_zero_dim_x) index = 2 z = paddle.repeat_interleave(x, index, None) np_z = z.numpy() diff --git a/test/legacy_test/test_require_version.py b/test/legacy_test/test_require_version.py index b989dd8b0a9a6..2b7f5909d6675 100644 --- a/test/legacy_test/test_require_version.py +++ b/test/legacy_test/test_require_version.py @@ -16,64 +16,64 @@ import warnings import paddle -import paddle.version as fluid_version -from paddle import fluid +import paddle.version as base_version +from paddle import base class VersionTest(unittest.TestCase): def test_check_output(self): warnings.warn( - "paddle.__version__: {}, fluid_version.full_version: {}, fluid_version.major: {}, fluid_version.minor: {}, fluid_version.patch: {}, fluid_version.rc: {}.".format( + "paddle.__version__: {}, base_version.full_version: {}, base_version.major: {}, base_version.minor: {}, base_version.patch: {}, base_version.rc: {}.".format( paddle.__version__, - fluid_version.full_version, - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.full_version, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ) ) - ori_full_version = fluid_version.full_version + ori_full_version = base_version.full_version ori_sep_version = [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] = ['1', '4', '1', '0'] - fluid.require_version('1') - fluid.require_version('1.4') - fluid.require_version('1.4.1.0') + base.require_version('1') + base.require_version('1.4') + base.require_version('1.4.1.0') # any version >= 1.4.1 is acceptable. - fluid.require_version('1.4.1') + base.require_version('1.4.1') # if 1.4.1 <= version <= 1.6.0, it is acceptable. - fluid.require_version(min_version='1.4.1', max_version='1.6.0') + base.require_version(min_version='1.4.1', max_version='1.6.0') # only version 1.4.1 is acceptable. - fluid.require_version(min_version='1.4.1', max_version='1.4.1') + base.require_version(min_version='1.4.1', max_version='1.4.1') # if installed version is 0.0.0.0, throw warning and skip the checking. [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] = ['0', '0', '0', '0'] - fluid.require_version('0.0.0') + base.require_version('0.0.0') - fluid_version.full_version = ori_full_version + base_version.full_version = ori_full_version [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] = ori_sep_version @@ -82,76 +82,76 @@ class TestErrors(unittest.TestCase): def test_errors(self): # The type of params must be str. def test_input_type(): - fluid.require_version(100) + base.require_version(100) self.assertRaises(TypeError, test_input_type) def test_input_type_1(): - fluid.require_version('0', 200) + base.require_version('0', 200) self.assertRaises(TypeError, test_input_type_1) # The value of params must be in format r'\d+(\.\d+){0,3}', like '1.5.2.0', '1.6' ... def test_input_value_1(): - fluid.require_version('string') + base.require_version('string') self.assertRaises(ValueError, test_input_value_1) def test_input_value_1_1(): - fluid.require_version('1.5', 'string') + base.require_version('1.5', 'string') self.assertRaises(ValueError, test_input_value_1_1) def test_input_value_2(): - fluid.require_version('1.5.2.0.0') + base.require_version('1.5.2.0.0') self.assertRaises(ValueError, test_input_value_2) def test_input_value_2_1(): - fluid.require_version('1.5', '1.5.2.0.0') + base.require_version('1.5', '1.5.2.0.0') self.assertRaises(ValueError, test_input_value_2_1) def test_input_value_3(): - fluid.require_version('1.5.2a.0') + base.require_version('1.5.2a.0') self.assertRaises(ValueError, test_input_value_3) # The installed version must be equal or greater than the required version. def test_version(): - fluid.require_version('100') + base.require_version('100') # The installed version must be in [min_version, max_version] def test_version_1(): - fluid.require_version('0.0.0', '1.4') + base.require_version('0.0.0', '1.4') def test_version_2(): - fluid.require_version('1.4.0', '1.2') + base.require_version('1.4.0', '1.2') - ori_full_version = fluid_version.full_version + ori_full_version = base_version.full_version ori_sep_version = [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] = ['1', '4', '1', '0'] self.assertRaises(Exception, test_version) self.assertRaises(Exception, test_version_1) self.assertRaises(Exception, test_version_2) - fluid_version.full_version = ori_full_version + base_version.full_version = ori_full_version [ - fluid_version.major, - fluid_version.minor, - fluid_version.patch, - fluid_version.rc, + base_version.major, + base_version.minor, + base_version.patch, + base_version.rc, ] = ori_sep_version diff --git a/test/legacy_test/test_reshape_op.py b/test/legacy_test/test_reshape_op.py index dc85f407aceab..5caaaae049b76 100755 --- a/test/legacy_test/test_reshape_op.py +++ b/test/legacy_test/test_reshape_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid +from paddle import base from paddle.static import Program, program_guard @@ -305,7 +305,7 @@ def setUp(self): self.python_api = paddle.tensor.reshape self.python_out_sig = ['Out'] input = np.random.randint(0, 127, self.ori_shape).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(input)} self.attrs = { 'shape': self.new_shape, 'use_mkldnn': self.use_mkldnn, @@ -325,7 +325,7 @@ def init_data(self): def test_check_output(self): self.check_output_with_place( - fluid.core.CPUPlace(), + base.core.CPUPlace(), atol=1e-5, no_check_set=['XShape'], ) @@ -413,7 +413,7 @@ def test_imperative(self): self._set_paddle_api() input = np.random.random([2, 25]).astype("float32") shape = [2, 5, 5] - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = self.to_tensor(input) positive_five = self.fill_constant([1], "int32", 5) @@ -437,7 +437,7 @@ def test_imperative(self): self._set_paddle_api() input = np.random.random([2, 25]).astype("float32") shape = [2, 5, 5] - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = self.to_tensor(input) positive_five = self.fill_constant([1], "int32", 5) @@ -463,7 +463,7 @@ def _test_errors(self): with program_guard(Program(), Program()): # The x type of reshape_op must be Variable. def test_x_type(): - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([[-1]]), [[1]], paddle.CPUPlace() ) self.reshape(x1, shape=[1]) @@ -602,18 +602,18 @@ def test_dygraph(self): paddle.enable_static() def test_static(self): - main_prog = fluid.Program() - with fluid.program_guard(main_prog, fluid.Program()): + main_prog = base.Program() + with base.program_guard(main_prog, base.Program()): x = paddle.rand([]) x.stop_gradient = False out = paddle.reshape(x, [-1]) - fluid.backward.append_backward(out) + base.backward.append_backward(out) prog = paddle.static.default_main_program() block = prog.global_block() - x_grad = block.var(fluid.framework.grad_var_name(x.name)) - out_grad = block.var(fluid.framework.grad_var_name(out.name)) + x_grad = block.var(base.framework.grad_var_name(x.name)) + out_grad = block.var(base.framework.grad_var_name(out.name)) # Test compile shape self.assertEqual(x.shape, ()) @@ -621,7 +621,7 @@ def test_static(self): self.assertEqual(x_grad.shape, ()) self.assertEqual(out_grad.shape, (1,)) - exe = fluid.Executor() + exe = base.Executor() result = exe.run(main_prog, fetch_list=[x, out, x_grad, out_grad]) # Test runtime shape diff --git a/test/legacy_test/test_retain_graph.py b/test/legacy_test/test_retain_graph.py index 03d45ec5b8457..873c1ad201249 100644 --- a/test/legacy_test/test_retain_graph.py +++ b/test/legacy_test/test_retain_graph.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base paddle.disable_static() SEED = 2020 @@ -152,7 +152,7 @@ def run_retain(self, need_retain): def test_retain(self): self.run_retain(need_retain=True) - if not fluid.framework.in_dygraph_mode(): + if not base.framework.in_dygraph_mode(): self.assertRaises(RuntimeError, self.run_retain, need_retain=False) diff --git a/test/legacy_test/test_rms_norm_op.py b/test/legacy_test/test_rms_norm_op.py index d5b6530ed51c8..cd9fa001e8362 100644 --- a/test/legacy_test/test_rms_norm_op.py +++ b/test/legacy_test/test_rms_norm_op.py @@ -16,8 +16,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def quant_helper( @@ -331,7 +331,7 @@ def check_rmsnorm(self, x_np, gamma_np, beta_np, dtype): self.epsilon, begin_norm_axis=1, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -381,7 +381,7 @@ def check_rmsnorm_int8(self, x_np, gamma_np, beta_np, dtype): quant_max_bound=self.quant_max_bound, quant_min_bound=self.quant_min_bound, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), @@ -435,7 +435,7 @@ def check_residual_bias_rmsnorm( residual=residual_static, ) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) out_s = exe.run( feed={ "x_static": x_np.astype(dtype), diff --git a/test/legacy_test/test_rmsprop_op.py b/test/legacy_test/test_rmsprop_op.py index 8540e7cf3f264..bc7d38a2d77a9 100644 --- a/test/legacy_test/test_rmsprop_op.py +++ b/test/legacy_test/test_rmsprop_op.py @@ -18,8 +18,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def create_selected_rows_and_tensor( @@ -56,7 +56,7 @@ def setup( ): np.random.seed(5) # fix seed - self.scope = fluid.global_scope() + self.scope = base.global_scope() self.place = place self.param_name = "param" @@ -231,12 +231,12 @@ def test_rmsprop(self): size = (128, 320) for place in places: for centered in [False, True]: - with fluid.scope_guard(core.Scope()): + with base.scope_guard(core.Scope()): self.check_with_place( place, is_sparse=False, centered=centered, size=size ) - with fluid.scope_guard(core.Scope()): + with base.scope_guard(core.Scope()): self.check_with_place( place, is_sparse=True, @@ -245,7 +245,7 @@ def test_rmsprop(self): size=size, ) - with fluid.scope_guard(core.Scope()): + with base.scope_guard(core.Scope()): self.check_with_place( place, is_sparse=True, @@ -274,9 +274,9 @@ def test_rmsprop_dygraph(self): def test_rmsprop(self): paddle.enable_static() - place = fluid.CPUPlace() - main = fluid.Program() - with fluid.program_guard(main): + place = base.CPUPlace() + main = base.Program() + with base.program_guard(main): x = paddle.static.data(name='x', shape=[-1, 13], dtype='float32') y = paddle.static.data(name='y', shape=[-1, 1], dtype='float32') y_predict = paddle.static.nn.fc(x, size=1) @@ -292,9 +292,9 @@ def test_rmsprop(self): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) diff --git a/test/legacy_test/test_rnn_cell_api.py b/test/legacy_test/test_rnn_cell_api.py index f163e62ea1f02..cf83bdb8120ec 100644 --- a/test/legacy_test/test_rnn_cell_api.py +++ b/test/legacy_test/test_rnn_cell_api.py @@ -23,10 +23,10 @@ from rnn_numpy import rnn as numpy_rnn import paddle -from paddle import fluid -from paddle.fluid import core, framework -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core, framework +from paddle.base.executor import Executor +from paddle.base.framework import Program, program_guard from paddle.nn.layer.rnn import rnn as dynamic_rnn paddle.enable_static() @@ -160,7 +160,7 @@ def test_run(self): 'float64' ) setattr(numpy_cell, k, param) - fluid.global_scope().find_var(v.name).get_tensor().set(param, place) + base.global_scope().find_var(v.name).get_tensor().set(param, place) sequence_length = paddle.static.data( name="sequence_length", shape=[None], dtype='int64' diff --git a/test/legacy_test/test_rnn_decode_api.py b/test/legacy_test/test_rnn_decode_api.py index efb7e30592da3..72bf68db45c44 100644 --- a/test/legacy_test/test_rnn_decode_api.py +++ b/test/legacy_test/test_rnn_decode_api.py @@ -19,9 +19,9 @@ import numpy as np import paddle -from paddle import Model, fluid, nn, set_device -from paddle.fluid import layers -from paddle.fluid.data_feeder import convert_dtype +from paddle import Model, base, nn, set_device +from paddle.base import layers +from paddle.base.data_feeder import convert_dtype from paddle.nn import ( RNN, BeamSearchDecoder, @@ -171,10 +171,10 @@ def __init__( seed=None, ): self.main_program = ( - fluid.Program() if main_program is None else main_program + base.Program() if main_program is None else main_program ) self.startup_program = ( - fluid.Program() if startup_program is None else startup_program + base.Program() if startup_program is None else startup_program ) if seed is not None: self.main_program.random_seed = seed @@ -183,7 +183,7 @@ def __init__( self.executor = executor def build_program(self, model_cls, alg_cls, model_hparams, alg_hparams): - with fluid.program_guard(self.main_program, self.startup_program): + with base.program_guard(self.main_program, self.startup_program): source = paddle.static.data( name="src", shape=[None, None], dtype="int64" ) @@ -300,13 +300,13 @@ def setUp(self): def _calc_output(self, place, mode="test", dygraph=True): if dygraph: - fluid.enable_dygraph(place) + base.enable_dygraph(place) else: - fluid.disable_dygraph() + base.disable_dygraph() gen = paddle.seed(self._random_seed) paddle.framework.random._manual_program_seed(self._random_seed) - scope = fluid.core.Scope() - with fluid.scope_guard(scope): + scope = base.core.Scope() + with base.scope_guard(scope): layer = ( self.model_cls(**self.attrs) if isinstance(self.attrs, dict) @@ -331,7 +331,7 @@ def check_output_with_place(self, place, mode="test"): ) def check_output(self): - devices = ["CPU", "GPU"] if fluid.is_compiled_with_cuda() else ["CPU"] + devices = ["CPU", "GPU"] if base.is_compiled_with_cuda() else ["CPU"] for device in devices: place = set_device(device) self.check_output_with_place(place) diff --git a/test/legacy_test/test_rnn_memory_helper_op.py b/test/legacy_test/test_rnn_memory_helper_op.py index 68bea9c94ff39..16a0cccb10d6f 100644 --- a/test/legacy_test/test_rnn_memory_helper_op.py +++ b/test/legacy_test/test_rnn_memory_helper_op.py @@ -16,9 +16,9 @@ import numpy as np -from paddle.fluid import core -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program +from paddle.base import core +from paddle.base.executor import Executor +from paddle.base.framework import Program class RNNMemoryHelperOpTest(unittest.TestCase): diff --git a/test/legacy_test/test_rnn_op.py b/test/legacy_test/test_rnn_op.py index da9c76b27ffa3..4d7412f949786 100644 --- a/test/legacy_test/test_rnn_op.py +++ b/test/legacy_test/test_rnn_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core sys.path.append("../../test/rnn") from convert import get_params_for_net diff --git a/test/legacy_test/test_roll_op.py b/test/legacy_test/test_roll_op.py index f491112b6a482..b8aa8e28529bd 100644 --- a/test/legacy_test/test_roll_op.py +++ b/test/legacy_test/test_roll_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestRollOp(OpTest): @@ -169,7 +169,7 @@ def test_roll_op_api(self): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') x.desc.set_need_check_feed(False) z = paddle.roll(x, shifts=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x}, fetch_list=[z.name], return_numpy=False ) @@ -183,7 +183,7 @@ def test_roll_op_api(self): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') x.desc.set_need_check_feed(False) z = paddle.roll(x, shifts=1, axis=0) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x}, fetch_list=[z.name], return_numpy=False ) @@ -195,8 +195,8 @@ def test_roll_op_api(self): def test_dygraph_api(self): self.input_data() # case 1: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) z = paddle.roll(x, shifts=1) np_z = z.numpy() expect_out = np.array( @@ -205,8 +205,8 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 2: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) z = paddle.roll(x, shifts=1, axis=0) np_z = z.numpy() expect_out = np.array( @@ -222,7 +222,7 @@ def test_axis_out_range(): x = paddle.static.data(name='x', shape=[-1, 3], dtype='float32') x.desc.set_need_check_feed(False) z = paddle.roll(x, shifts=1, axis=10) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': self.data_x}, fetch_list=[z.name], @@ -232,7 +232,7 @@ def test_axis_out_range(): self.assertRaises(ValueError, test_axis_out_range) def test_shifts_as_tensor_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.arange(9).reshape([3, 3]) shape = paddle.shape(x) shifts = shape // 2 @@ -250,12 +250,12 @@ def test_shifts_as_tensor_static(self): out = paddle.roll(x, shifts=shifts, axis=axes) expected_out = np.array([[8, 6, 7], [2, 0, 1], [5, 3, 4]]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) [out_np] = exe.run(fetch_list=[out]) np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) if paddle.is_compiled_with_cuda(): - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) [out_np] = exe.run(fetch_list=[out]) np.testing.assert_allclose(out_np, expected_out, rtol=1e-05) diff --git a/test/legacy_test/test_rot90_op.py b/test/legacy_test/test_rot90_op.py index e3fc1a75bd757..795cff3f7b357 100644 --- a/test/legacy_test/test_rot90_op.py +++ b/test/legacy_test/test_rot90_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TestRot90_API(unittest.TestCase): @@ -25,19 +25,19 @@ class TestRot90_API(unittest.TestCase): def test_static_graph(self): paddle.enable_static() - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=1, axes=[0, 1]) output = paddle.rot90(output, k=1, axes=[0, 1]) output = output.rot90(k=1, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -56,17 +56,17 @@ def test_static_graph(self): def test_static_k_0(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=0, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -85,17 +85,17 @@ def test_static_k_0(self): def test_static_k_2(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=2, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -114,17 +114,17 @@ def test_static_k_2(self): def test_static_k_3(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=3, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -143,17 +143,17 @@ def test_static_k_3(self): def test_static_neg_k_1(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=-1, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -172,17 +172,17 @@ def test_static_neg_k_1(self): def test_static_neg_k_2(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=-2, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -201,17 +201,17 @@ def test_static_neg_k_2(self): def test_static_neg_k_3(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=-3, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -230,17 +230,17 @@ def test_static_neg_k_3(self): def test_static_neg_k_4(self): paddle.enable_static() input = paddle.static.data(name='input', dtype='float32', shape=[2, 3]) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name='input', dtype='float32', shape=[2, 3] ) output = paddle.rot90(input, k=-4, axes=[0, 1]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) @@ -301,8 +301,8 @@ def run5(): def test_dygraph(self): img = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) - with fluid.dygraph.guard(): - inputs = fluid.dygraph.to_variable(img) + with base.dygraph.guard(): + inputs = base.dygraph.to_variable(img) ret = paddle.rot90(inputs, k=1, axes=[0, 1]) ret = ret.rot90(1, axes=[0, 1]) diff --git a/test/legacy_test/test_row_conv_op.py b/test/legacy_test/test_row_conv_op.py index e01f18a3f63e9..595fb7b8b12aa 100644 --- a/test/legacy_test/test_row_conv_op.py +++ b/test/legacy_test/test_row_conv_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -187,18 +187,18 @@ def setUp(self): self.out = row_conv_foward_Tensor(self.x, self.w) def check_identity(self): - start = fluid.Program() - main = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, start): + start = base.Program() + main = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, start): x = paddle.static.data("x", (-1, -1, self.C), "float32") out = paddle.static.nn.row_conv( x, self.context_length, param_attr=paddle.nn.initializer.Assign(self.w), ) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(start) (out_np,) = exe.run(main, feed={'x': self.x}, fetch_list=[out]) diff --git a/test/legacy_test/test_rrelu_op.py b/test/legacy_test/test_rrelu_op.py index b86b7808aba93..851b90c44c7ba 100644 --- a/test/legacy_test/test_rrelu_op.py +++ b/test/legacy_test/test_rrelu_op.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core, dygraph +from paddle import base +from paddle.base import core, dygraph paddle.seed(102) np.random.seed(102) @@ -51,13 +51,13 @@ def setUp(self): self.upper_1 = 0.33 self.places = [ - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ] def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 4, 5], dtype="float32" ) @@ -70,9 +70,9 @@ def check_static_result(self, place): in_np = np.random.uniform(-1.0, 1.0, [2, 3, 4, 5]).astype("float32") res_np1 = ref_rrelu(in_np, self.lower_0, self.upper_0) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res1], ) @@ -81,7 +81,7 @@ def check_static_result(self, place): res_np2 = ref_rrelu(in_np, self.lower_1, self.upper_1) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": in_np}, fetch_list=[res2], ) @@ -108,19 +108,19 @@ def test_static_graph_functional(self): exe = paddle.static.Executor(place=place) (res_1,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=out_1, use_prune=True, ) (res_2,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_np}, fetch_list=out_2, use_prune=True, ) (res_3,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_np}, fetch_list=out_3, use_prune=True, @@ -153,13 +153,13 @@ def test_static_graph_layer(self): exe = paddle.static.Executor(place=place) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": self.x_np}, fetch_list=out_1, use_prune=True, ) res_2 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x2": self.x_np}, fetch_list=out_2, use_prune=True, @@ -267,37 +267,37 @@ def test_error_functional(self): def test_error_layer(self): def error_int_dtype(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float64") rrelu = paddle.nn.RReLU(2, 3) rrelu(paddle.to_tensor(x)) def error_lower_dtype(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float32") rrelu = paddle.nn.RReLU(0, 0.5) rrelu(paddle.to_tensor(x)) def error_upper_dtype(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float32") rrelu = paddle.nn.RReLU(0.5, 1) rrelu(paddle.to_tensor(x)) def error_lower_range(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float32") rrelu = paddle.nn.RReLU(-1.0, 0.5) rrelu(paddle.to_tensor(x)) def error_upper_range(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float32") rrelu = paddle.nn.RReLU(0.5, 2.0) rrelu(paddle.to_tensor(x)) def error_lower_upper(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = np.random.random([2, 3]).astype("float32") rrelu = paddle.nn.RReLU(0.5, 0.2) rrelu(paddle.to_tensor(x)) diff --git a/test/legacy_test/test_run_fluid_by_module_or_command_line.py b/test/legacy_test/test_run_fluid_by_module_or_command_line.py index 473ff182aefc3..55927d1f383d8 100644 --- a/test/legacy_test/test_run_fluid_by_module_or_command_line.py +++ b/test/legacy_test/test_run_fluid_by_module_or_command_line.py @@ -20,13 +20,13 @@ class TestRunFluidByModule(unittest.TestCase): def test_module(self): print(sys.executable) - res = os.system(sys.executable + ' -m "paddle.fluid.reader"') + res = os.system(sys.executable + ' -m "paddle.base.reader"') self.assertEqual(res, 0) # 0 means status OK class TestRunFluidByCommand(unittest.TestCase): def test_command(self): - res = os.system(sys.executable + ' -c "import paddle.fluid"') + res = os.system(sys.executable + ' -c "import paddle.base"') self.assertEqual(res, 0) # 0 means status OK diff --git a/test/legacy_test/test_run_program_op.py b/test/legacy_test/test_run_program_op.py index 4cc056f2237f4..2d223e9474703 100644 --- a/test/legacy_test/test_run_program_op.py +++ b/test/legacy_test/test_run_program_op.py @@ -18,21 +18,21 @@ import numpy as np import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid import core, framework -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import _legacy_C_ops, base +from paddle.base import core, framework +from paddle.base.dygraph.base import switch_to_static_graph paddle.enable_static() @contextlib.contextmanager def program_scope_guard(): - prog = fluid.Program() - startup_prog = fluid.Program() - scope = fluid.core.Scope() - with fluid.scope_guard(scope): - with fluid.program_guard(prog, startup_prog): - with fluid.unique_name.guard(): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): + with base.unique_name.guard(): yield @@ -45,7 +45,7 @@ def _add_build_strategy_for(input_program, start_op_index, end_op_index): compiled_program._compile( core.Scope(), paddle.framework._current_expected_place() ) - ir_graph = paddle.fluid.framework.IrGraph(compiled_program._graph) + ir_graph = paddle.base.framework.IrGraph(compiled_program._graph) builded_program = ir_graph.to_program() return builded_program @@ -75,18 +75,18 @@ def build_model(self): ) def check_output(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: # TODO: RunProgramOp is not recommended for use in static graph mode now self.expect_outs = self.run_static_model(place, is_test=True) self.check_output_with_place(place) def check_grad(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: # TODO: RunProgramOp is not recommended for use in static graph mode now self.expect_grads = self.run_static_model(place, is_test=False) @@ -94,12 +94,12 @@ def check_grad(self): def run_static_model(self, place, is_test=True): with program_scope_guard(): - startup_program = fluid.default_startup_program() - main_program = fluid.default_main_program() + startup_program = base.default_startup_program() + main_program = base.default_main_program() self.build_model() - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) if is_test: @@ -115,7 +115,7 @@ def run_static_model(self, place, is_test=True): def get_program_desc(self): with program_scope_guard(): fwd_op_num = self.build_model() - return fluid.default_main_program().desc, fwd_op_num + return base.default_main_program().desc, fwd_op_num def get_forward_backward_program_desc( self, whole_program_desc, forward_op_num, output_num @@ -215,7 +215,7 @@ def calc_dygraph_output(self, place): self.program_desc, self.fwd_op_num = self.get_program_desc() self.attrs = self.prepare_attrs() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): inputs = self.prepare_dygraph_input(place) outputs = self.prepare_dygraph_output() @@ -267,7 +267,7 @@ def calc_dygraph_grad(self, place): self.program_desc, self.fwd_op_num = self.get_program_desc() self.attrs = self.prepare_attrs() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): # Step 1. run forward inputs, input_param_list = self.prepare_dygraph_input(place, True) outputs = self.prepare_dygraph_output() @@ -381,7 +381,7 @@ def build_model(self): shape=[None, 1, 28, 28], dtype='float32', ) - weight_attr = fluid.ParamAttr( + weight_attr = base.ParamAttr( name=self.input_names['Params'][0], learning_rate=0.5, initializer=paddle.nn.initializer.Assign( @@ -389,7 +389,7 @@ def build_model(self): ), trainable=True, ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( name=self.input_names['Params'][1], learning_rate=0.5, initializer=paddle.nn.initializer.Assign( @@ -405,9 +405,9 @@ def build_model(self): activation='relu', ) # 2. get forward op num - fwd_op_num = fluid.default_main_program().global_block().desc.op_size() + fwd_op_num = base.default_main_program().global_block().desc.op_size() # 3. append backward - grads = fluid.backward.gradients(targets=[pred], inputs=[img]) + grads = base.backward.gradients(targets=[pred], inputs=[img]) return fwd_op_num @@ -432,9 +432,9 @@ def test_check_output(self): def test_check_grad(self): # NOTE: fecth not support SelectedRows, catnot compare # sparse gradients with staic mode, only run dygraph - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: # TODO: RunProgramOp is not recommended for use in static graph mode now self.calc_dygraph_grad(place) @@ -447,7 +447,7 @@ def build_model(self): emb = paddle.static.nn.embedding( input=x, size=[10, 16], - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="emb_weight", learning_rate=10, initializer=paddle.nn.initializer.Assign( @@ -458,9 +458,9 @@ def build_model(self): ) y = paddle.sum(emb, axis=-1) # 2. get forward op num - fwd_op_num = fluid.default_main_program().global_block().desc.op_size() + fwd_op_num = base.default_main_program().global_block().desc.op_size() # 3. append backward - grads = fluid.backward.gradients(targets=[y], inputs=[x]) + grads = base.backward.gradients(targets=[y], inputs=[x]) return fwd_op_num diff --git a/test/legacy_test/test_save_model_without_var.py b/test/legacy_test/test_save_model_without_var.py index 04437ab783499..2da87c2142a9b 100644 --- a/test/legacy_test/test_save_model_without_var.py +++ b/test/legacy_test/test_save_model_without_var.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle import fluid +from paddle import base class TestSaveModelWithoutVar(unittest.TestCase): @@ -24,13 +24,13 @@ def test_no_var_save(self): data = paddle.static.data(name='data', shape=[-1, 1], dtype='float32') data_plus = data + 1 - if fluid.core.is_compiled_with_cuda(): - place = fluid.core.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = base.core.CUDAPlace(0) else: - place = fluid.core.CPUPlace() + place = base.core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") diff --git a/test/legacy_test/test_scalar.py b/test/legacy_test/test_scalar.py index 3a49611c15c34..0081fbaad9196 100644 --- a/test/legacy_test/test_scalar.py +++ b/test/legacy_test/test_scalar.py @@ -17,7 +17,7 @@ import numpy as np import op -from paddle.fluid import framework +from paddle.base import framework class TestWarpAsScalar(unittest.TestCase): diff --git a/test/legacy_test/test_scale_op.py b/test/legacy_test/test_scale_op.py index 7708ce8deaa88..9585357ce1b07 100644 --- a/test/legacy_test/test_scale_op.py +++ b/test/legacy_test/test_scale_op.py @@ -21,8 +21,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import Program, program_guard @@ -262,9 +262,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -293,9 +293,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_scatter_nd_op.py b/test/legacy_test/test_scatter_nd_op.py index ee6a2423e0d20..9d8c7bf876773 100644 --- a/test/legacy_test/test_scatter_nd_op.py +++ b/test/legacy_test/test_scatter_nd_op.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import switch_to_static_graph def numpy_scatter_nd(ref, index, updates, fun): @@ -324,7 +324,7 @@ class TestScatterNdOpAPI(unittest.TestCase): """ def testcase1(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): ref1 = paddle.static.data( name='ref1', shape=[10, 9, 8, 1, 3], @@ -343,7 +343,7 @@ def testcase1(self): output1 = paddle.scatter_nd_add(ref1, index1, updates1) def testcase2(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): ref2 = paddle.static.data( name='ref2', shape=[10, 9, 8, 1, 3], @@ -364,7 +364,7 @@ def testcase2(self): ) def testcase3(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): shape3 = [10, 9, 8, 1, 3] index3 = paddle.static.data( name='index3', @@ -379,7 +379,7 @@ def testcase3(self): output3 = paddle.scatter_nd(index3, updates3, shape3) def testcase4(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): shape4 = [10, 9, 8, 1, 3] index4 = paddle.static.data( name='index4', @@ -396,7 +396,7 @@ def testcase4(self): ) def testcase5(self): - if not fluid.core.is_compiled_with_cuda(): + if not base.core.is_compiled_with_cuda(): return shape = [2, 3, 4] @@ -404,7 +404,7 @@ def testcase5(self): index = np.array([[0, 0, 2], [0, 1, 2]]) val = np.array([-1, -3]) - with fluid.dygraph.guard(): + with base.dygraph.guard(): device = paddle.get_device() paddle.set_device('gpu') gpu_value = paddle.scatter_nd_add( @@ -450,7 +450,7 @@ def test_static_graph(): class TestScatterNdOpRaise(unittest.TestCase): def test_check_raise(self): def check_raise_is_test(): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): try: ref5 = paddle.static.data( name='ref5', shape=[-1, 3, 4, 5], dtype='float32' @@ -471,7 +471,7 @@ def check_raise_is_test(): def test_check_raise2(self): with self.assertRaises(ValueError): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): ref6 = paddle.static.data( name='ref6', shape=[10, 9, 8, 1, 3], @@ -491,7 +491,7 @@ def test_check_raise2(self): def test_check_raise3(self): def check_raise_is_test(): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): try: shape = [3, 4, 5] index7 = paddle.static.data( @@ -513,19 +513,19 @@ def check_raise_is_test(): class TestDygraph(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): index_data = np.array([[1, 1], [0, 1], [1, 3]]).astype(np.int64) - index = fluid.dygraph.to_variable(index_data) + index = base.dygraph.to_variable(index_data) updates = paddle.rand(shape=[3, 9, 10], dtype='float32') shape = [3, 5, 9, 10] output = paddle.scatter_nd(index, updates, shape) def test_dygraph_1(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): x = paddle.rand(shape=[3, 5, 9, 10], dtype='float32') updates = paddle.rand(shape=[3, 9, 10], dtype='float32') index_data = np.array([[1, 1], [0, 1], [1, 3]]).astype(np.int64) - index = fluid.dygraph.to_variable(index_data) + index = base.dygraph.to_variable(index_data) output = paddle.scatter_nd_add(x, index, updates) diff --git a/test/legacy_test/test_scatter_op.py b/test/legacy_test/test_scatter_op.py index df264887c6265..5c87f6aed919e 100644 --- a/test/legacy_test/test_scatter_op.py +++ b/test/legacy_test/test_scatter_op.py @@ -19,9 +19,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.dygraph.base import switch_to_static_graph +from paddle import base +from paddle.base import core +from paddle.base.dygraph.base import switch_to_static_graph class TestScatterOp(OpTest): @@ -571,16 +571,16 @@ def test_check_grad(self): class TestScatterAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) self.executed_api() def executed_api(self): self.scatter = paddle.scatter def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[3, 2], dtype="float64" ) @@ -596,9 +596,9 @@ def check_static_result(self, place): np.float64 ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "input": input_data, "index": index_data, @@ -619,16 +619,16 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): x_data = np.array([[1, 1], [2, 2], [3, 3]]).astype(np.float64) index_data = np.array([2, 1, 0, 1]).astype(np.int64) updates_data = np.array( [[1, 1], [2, 2], [3, 3], [4, 4]] ).astype(np.float64) - x = fluid.dygraph.to_variable(x_data) - index = fluid.dygraph.to_variable(index_data) - updates = fluid.dygraph.to_variable(updates_data) + x = base.dygraph.to_variable(x_data) + index = base.dygraph.to_variable(index_data) + updates = base.dygraph.to_variable(updates_data) output1 = self.scatter(x, index, updates, overwrite=False) self.assertEqual( @@ -648,7 +648,7 @@ def test_large_data(self): updates = np.ones(shape=[10759233, 256], dtype="float32") def test_dygraph(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): gpu_out = paddle.scatter( paddle.to_tensor(x), paddle.to_tensor(index), diff --git a/test/legacy_test/test_scope.py b/test/legacy_test/test_scope.py index f5c53718c5ad0..6d0e2ed77906b 100644 --- a/test/legacy_test/test_scope.py +++ b/test/legacy_test/test_scope.py @@ -14,24 +14,24 @@ import unittest -import paddle.fluid.core +import paddle.base.core class TestScope(unittest.TestCase): def test_create_destroy(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() self.assertIsNotNone(scope) scope_with_parent = scope.new_scope() self.assertIsNotNone(scope_with_parent) def test_none_variable(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() self.assertIsNone(scope.find_var("test")) def test_create_var_get_var(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() var_a = scope.var("var_a") self.assertIsNotNone(var_a) @@ -40,7 +40,7 @@ def test_create_var_get_var(self): self.assertIsNotNone(scope2.find_var('var_a')) def test_var_get_int(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() var = scope.var("test_int") var.set_int(10) @@ -48,7 +48,7 @@ def test_var_get_int(self): self.assertEqual(10, var.get_int()) def test_scope_pool(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() # Delete the scope. scope._remove_from_pool() @@ -59,7 +59,7 @@ def test_scope_pool(self): scope._remove_from_pool() def test_size(self): - paddle_c = paddle.fluid.core + paddle_c = paddle.base.core scope = paddle_c.Scope() var_a = scope.var("var_a") self.assertEqual(scope.size(), 1) diff --git a/test/legacy_test/test_searchsorted_op.py b/test/legacy_test/test_searchsorted_op.py index 6795c6e16424f..84fd7afac7aa0 100644 --- a/test/legacy_test/test_searchsorted_op.py +++ b/test/legacy_test/test_searchsorted_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_segment_ops.py b/test/legacy_test/test_segment_ops.py index d2be362e650cf..00b4f403fc7a0 100644 --- a/test/legacy_test/test_segment_ops.py +++ b/test/legacy_test/test_segment_ops.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def compute_segment_sum(x, segment_ids): @@ -365,7 +365,7 @@ def test_static(self): def test_dygraph(self): device = paddle.CPUPlace() - with paddle.fluid.dygraph.guard(device): + with paddle.base.dygraph.guard(device): x = paddle.to_tensor( [[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32' ) @@ -418,7 +418,7 @@ def test_static(self): def test_dygraph(self): device = paddle.CPUPlace() - with paddle.fluid.dygraph.guard(device): + with paddle.base.dygraph.guard(device): x = paddle.to_tensor( [[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float32' ) @@ -442,7 +442,7 @@ def test_dygraph(self): def test_dygraph_cpu_float16(self): device = paddle.CPUPlace() - with paddle.fluid.dygraph.guard(device): + with paddle.base.dygraph.guard(device): x = paddle.to_tensor( [[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float16' ) @@ -466,7 +466,7 @@ def test_dygraph_cpu_float16(self): def test_dygraph_cuda_float16(self): if core.is_compiled_with_cuda(): device = paddle.CUDAPlace(0) - with paddle.fluid.dygraph.guard(device): + with paddle.base.dygraph.guard(device): x = paddle.to_tensor( [[1, 2, 3], [3, 2, 1], [4, 5, 6]], dtype='float16' ) diff --git a/test/legacy_test/test_select_input_output_op.py b/test/legacy_test/test_select_input_output_op.py index 99292cbe2f25e..a06495a13c71a 100644 --- a/test/legacy_test/test_select_input_output_op.py +++ b/test/legacy_test/test_select_input_output_op.py @@ -17,11 +17,11 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.executor import Executor +from paddle.base.framework import Program, program_guard from paddle.static.nn.control_flow import select_input, select_output paddle.enable_static() @@ -53,9 +53,9 @@ def test_forward_backward_list_output(self): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) exe = Executor(place) diff --git a/test/legacy_test/test_selected_rows.py b/test/legacy_test/test_selected_rows.py index aab95d1f22d99..b8b8604d31c63 100644 --- a/test/legacy_test/test_selected_rows.py +++ b/test/legacy_test/test_selected_rows.py @@ -16,7 +16,7 @@ import numpy as np -from paddle.fluid import core +from paddle.base import core class TestSelectedRows(unittest.TestCase): diff --git a/test/legacy_test/test_selu_op.py b/test/legacy_test/test_selu_op.py index acc9214220ef3..17e4311615145 100644 --- a/test/legacy_test/test_selu_op.py +++ b/test/legacy_test/test_selu_op.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def ref_selu( @@ -144,11 +144,11 @@ def test_dygraph_api(self): np.testing.assert_allclose(out_ref, r.numpy(), rtol=1e-05) paddle.enable_static() - def test_fluid_api(self): - with fluid.program_guard(fluid.Program()): + def test_base_api(self): + with base.program_guard(base.Program()): x = paddle.static.data('X', self.x_np.shape, self.x_np.dtype) out = F.selu(x, self.scale, self.alpha) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(feed={'X': self.x_np}, fetch_list=[out]) out_ref = ref_selu(self.x_np, self.scale, self.alpha) np.testing.assert_allclose(out_ref, res[0], rtol=1e-05) diff --git a/test/legacy_test/test_set_bool_attr.py b/test/legacy_test/test_set_bool_attr.py index 66d0d774e7f1c..5f7282e12a1f8 100644 --- a/test/legacy_test/test_set_bool_attr.py +++ b/test/legacy_test/test_set_bool_attr.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base class TestAttrSet(unittest.TestCase): @@ -24,18 +24,18 @@ def test_set_bool_attr(self): x = paddle.static.data( name='x', shape=[-1, 3, 7, 3, 7], dtype='float32' ) - param_attr = fluid.ParamAttr( + param_attr = base.ParamAttr( name='batch_norm_w', initializer=paddle.nn.initializer.Constant(value=1.0), ) - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( name='batch_norm_b', initializer=paddle.nn.initializer.Constant(value=0.0), ) bn = paddle.static.nn.batch_norm( input=x, param_attr=param_attr, bias_attr=bias_attr ) - block = fluid.default_main_program().desc.block(0) + block = base.default_main_program().desc.block(0) op = block.op(0) before_type = op.attr_type('is_test') op._set_attr('is_test', True) diff --git a/test/legacy_test/test_set_value_op.py b/test/legacy_test/test_set_value_op.py index 9f797e6ab0ac3..db7a8ebbf91fa 100644 --- a/test/legacy_test/test_set_value_op.py +++ b/test/legacy_test/test_set_value_op.py @@ -21,8 +21,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core +from paddle.base.layer_helper import LayerHelper class TestSetValueBase(unittest.TestCase): @@ -1828,9 +1828,9 @@ def set_value(array, i, op): sgd = paddle.optimizer.Adam() sgd.minimize(loss) place = ( - paddle.fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else paddle.fluid.CUDAPlace(0) + paddle.base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else paddle.base.CUDAPlace(0) ) prog = paddle.static.default_main_program() @@ -1874,7 +1874,7 @@ def set_value(array, i, op): class TestSetValueInplace(unittest.TestCase): def test_inplace(self): paddle.disable_static() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) a.stop_gradient = False @@ -1894,7 +1894,7 @@ def test_inplace_var_become_leaf_var(self): paddle.disable_static() a_grad_1, b_grad_1, a_grad_2, b_grad_2 = 0, 1, 2, 3 - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) b = paddle.rand(shape=[1, 4]) @@ -1905,7 +1905,7 @@ def test_inplace_var_become_leaf_var(self): a_grad_1 = a.grad.numpy() b_grad_1 = b.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) b = paddle.rand(shape=[1, 4]) diff --git a/test/legacy_test/test_sgd_op.py b/test/legacy_test/test_sgd_op.py index e6239c8d8f112..1d9cdb3ffa3f3 100644 --- a/test/legacy_test/test_sgd_op.py +++ b/test/legacy_test/test_sgd_op.py @@ -19,8 +19,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -220,11 +220,11 @@ def runTest(self): sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) sgd_optimizer.minimize(avg_cost) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - compiled_prog = fluid.compiler.CompiledProgram( - fluid.default_main_program() + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) + compiled_prog = base.compiler.CompiledProgram( + base.default_main_program() ) result = exe.run(compiled_prog, fetch_list=[avg_cost]) diff --git a/test/legacy_test/test_sgd_op_bf16.py b/test/legacy_test/test_sgd_op_bf16.py index 9b58c7b00d2ce..816ab3b6359d8 100644 --- a/test/legacy_test/test_sgd_op_bf16.py +++ b/test/legacy_test/test_sgd_op_bf16.py @@ -25,8 +25,8 @@ from op import Operator import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import amp @@ -236,7 +236,7 @@ class TestSGDOpBF16API(unittest.TestCase): @classmethod def setUpClass(cls): np.random.seed(12345) - fluid.set_flags({'FLAGS_use_mkldnn': True}) + base.set_flags({'FLAGS_use_mkldnn': True}) def setUp(self): self.sample_count = 20 @@ -331,9 +331,9 @@ def _data_reader(self): yield data, label def test_sgd(self): - place = fluid.CPUPlace() - main = fluid.Program() - with fluid.program_guard(main): + place = base.CPUPlace() + main = base.Program() + with base.program_guard(main): ids_shape = list(self.ids_shape) x = paddle.static.data( name='X', shape=[-1] + ids_shape, dtype='int64' @@ -345,7 +345,7 @@ def test_sgd(self): emb = paddle.static.nn.embedding( input=x, size=self.w_shape, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( name="emb_weight", initializer=self.initializer ), is_sparse=False, @@ -368,12 +368,12 @@ def test_sgd(self): use_pure_bf16=True, ) sgd_optimizer.minimize( - avg_cost, startup_program=fluid.default_startup_program() + avg_cost, startup_program=base.default_startup_program() ) train_reader = paddle.batch(self._data_reader, batch_size=1) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) test_prog = main.clone(for_test=True) sgd_optimizer.amp_init( place, test_program=test_prog, use_bf16_test=True diff --git a/test/legacy_test/test_shape_op.py b/test/legacy_test/test_shape_op.py index d9dade1cf99ce..e7e7f9b62110a 100644 --- a/test/legacy_test/test_shape_op.py +++ b/test/legacy_test/test_shape_op.py @@ -19,7 +19,7 @@ from op import Operator import paddle -from paddle.fluid import core +from paddle.base import core class TestShapeOp(OpTest): diff --git a/test/legacy_test/test_share_data_op.py b/test/legacy_test/test_share_data_op.py index b369652e0ee4d..bf158beac1c97 100644 --- a/test/legacy_test/test_share_data_op.py +++ b/test/legacy_test/test_share_data_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from op import Operator -from paddle.fluid import core +from paddle.base import core class TestShareDataOp(OpTest): diff --git a/test/legacy_test/test_shuffle_batch_op.py b/test/legacy_test/test_shuffle_batch_op.py index c20890261294e..2bee79bda306d 100644 --- a/test/legacy_test/test_shuffle_batch_op.py +++ b/test/legacy_test/test_shuffle_batch_op.py @@ -19,7 +19,7 @@ import numpy as np from eager_op_test import OpTest -from paddle import fluid +from paddle import base class TestShuffleBatchOpBase(OpTest): @@ -33,7 +33,7 @@ def get_shape(self): def _get_places(self): # NOTE: shuffle_batch is not supported on Windows if os.name == 'nt': - return [fluid.CPUPlace()] + return [base.CPUPlace()] return super()._get_places() def setUp(self): diff --git a/test/legacy_test/test_sigmoid_cross_entropy_with_logits_op.py b/test/legacy_test/test_sigmoid_cross_entropy_with_logits_op.py index bb02c11f440e1..e0efedffc0043 100644 --- a/test/legacy_test/test_sigmoid_cross_entropy_with_logits_op.py +++ b/test/legacy_test/test_sigmoid_cross_entropy_with_logits_op.py @@ -19,8 +19,8 @@ from scipy.special import expit, logit import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard def loss_wrapper( @@ -321,15 +321,15 @@ def test_errors(self): def test_Variable(): # the input of sigmoid_cross_entropy_with_logits must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], - fluid.CPUPlace(), + base.CPUPlace(), ) - lab1 = fluid.create_lod_tensor( + lab1 = base.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], - fluid.CPUPlace(), + base.CPUPlace(), ) paddle.nn.functional.binary_cross_entropy_with_logits( x1, lab1 diff --git a/test/legacy_test/test_sigmoid_focal_loss.py b/test/legacy_test/test_sigmoid_focal_loss.py index 73df6a4c8d99b..b151d4c56a21e 100644 --- a/test/legacy_test/test_sigmoid_focal_loss.py +++ b/test/legacy_test/test_sigmoid_focal_loss.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def call_sfl_functional( @@ -130,9 +130,9 @@ def test_SigmoidFocalLoss(self): np.asarray([np.sum(label_np > 0)], dtype=label_np.dtype), None, ] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) reductions = ['sum', 'mean', 'none'] alphas = [0.25, 0.5] gammas = [3, 0.0] diff --git a/test/legacy_test/test_sign_op.py b/test/legacy_test/test_sign_op.py index 2617c2451f330..516330a58898a 100644 --- a/test/legacy_test/test_sign_op.py +++ b/test/legacy_test/test_sign_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestSignOp(OpTest): @@ -99,7 +99,7 @@ def test_errors(self): class TestSignAPI(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([-1.0, 0.0, -0.0, 1.2, 1.5], dtype='float64') x = paddle.to_tensor(np_x) z = paddle.sign(x) @@ -151,9 +151,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -182,9 +182,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_simple_rnn_op.py b/test/legacy_test/test_simple_rnn_op.py index 9bc75230953a4..fb731f87e951a 100644 --- a/test/legacy_test/test_simple_rnn_op.py +++ b/test/legacy_test/test_simple_rnn_op.py @@ -20,7 +20,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core sys.path.append("../../test/rnn") from convert import get_params_for_net diff --git a/test/legacy_test/test_size_op.py b/test/legacy_test/test_size_op.py index 0bb3ac64bce75..68ec73fb57ba4 100644 --- a/test/legacy_test/test_size_op.py +++ b/test/legacy_test/test_size_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base def size_wrapper(input): @@ -64,9 +64,9 @@ def config(self): class TestSizeAPI(unittest.TestCase): def test_size_static(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): shape1 = [2, 1, 4, 5] shape2 = [1, 4, 5] x_1 = paddle.static.data(shape=shape1, dtype='int32', name='x_1') @@ -103,9 +103,9 @@ def test_size_imperative(self): paddle.enable_static() def test_error(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): def test_x_type(): shape = [1, 4, 5] diff --git a/test/legacy_test/test_slice_op.py b/test/legacy_test/test_slice_op.py index 271824bd2fd5e..0aa24c85b87b0 100644 --- a/test/legacy_test/test_slice_op.py +++ b/test/legacy_test/test_slice_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.tensor.manipulation import tensor_array_to_tensor paddle.enable_static() @@ -620,9 +620,9 @@ def test_1(self): out_6 = x[minus_3:3, 0:100, :, 2:-1] out_7 = x[minus_1, 0:100, :, 2:minus_1] - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": input, 'starts': np.array([-3, 0, 2]).astype("int32"), @@ -642,7 +642,7 @@ def test_1(self): class TestSliceApiWithTensor(unittest.TestCase): def test_starts_ends_is_tensor(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): a = paddle.rand(shape=[4, 5, 6], dtype='float32') axes = [0, 1, 2] starts = [-3, 0, 2] @@ -658,7 +658,7 @@ def test_starts_ends_is_tensor(self): np.testing.assert_array_equal(a_1.numpy(), a_2.numpy()) def test_bool_tensor(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): array = (np.arange(60).reshape([3, 4, 5]) % 3).astype('bool') tt = paddle.to_tensor(array) tt.stop_gradient = False @@ -676,7 +676,7 @@ def test_bool_tensor(self): class TestSliceApiEager(unittest.TestCase): def test_slice_api(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): a = paddle.rand(shape=[4, 5, 6], dtype='float32') a.stop_gradient = False axes = [0, 1, 2] @@ -711,15 +711,15 @@ def setUp(self): self.axis = 1 self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) - self.exe = fluid.Executor(self.place) + self.exe = base.Executor(self.place) def set_program_and_run(self, main_program, case_num): with paddle_static_guard(): - with fluid.program_guard(main_program): + with base.program_guard(main_program): x = [ paddle.static.data( name='x0', shape=self.shape, dtype="float32" @@ -761,7 +761,7 @@ def set_program_and_run(self, main_program, case_num): ) loss = paddle.sum(output) - fluid.backward.append_backward(loss) + base.backward.append_backward(loss) g_vars = list( map( main_program.global_block().var, @@ -775,7 +775,7 @@ def set_program_and_run(self, main_program, case_num): ) def test_case_1(self): - main_program = fluid.Program() + main_program = base.Program() self.set_program_and_run(main_program, 1) self.assertTrue(self.sliced_arr.type == core.VarDesc.VarType.LOD_TENSOR) @@ -787,7 +787,7 @@ def test_case_1(self): def test_case_2(self): with paddle_static_guard(): - main_program = fluid.Program() + main_program = base.Program() self.set_program_and_run(main_program, 2) self.assertTrue( @@ -803,7 +803,7 @@ def test_case_2(self): def test_case_3(self): with paddle_static_guard(): - main_program = fluid.Program() + main_program = base.Program() self.set_program_and_run(main_program, 3) self.assertTrue( @@ -821,9 +821,9 @@ def test_case_3(self): class TestImperativeVarBaseGetItem(unittest.TestCase): def test_getitem_with_long(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): data = np.random.random((2, 80, 16128)).astype('float32') - var = fluid.dygraph.to_variable(data) + var = base.dygraph.to_variable(data) sliced = var[:, 10:, : var.shape[1]] # var.shape[1] is 80L here self.assertEqual(sliced.shape, [2, 70, 80]) @@ -832,17 +832,17 @@ def test_getitem_with_long(self): def test_getitem_with_float(self): def test_float_in_slice_item(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): data = np.random.random((2, 80, 16128)).astype('float32') - var = fluid.dygraph.to_variable(data) + var = base.dygraph.to_variable(data) sliced = var[:, 1.1:, : var.shape[1]] self.assertRaises(Exception, test_float_in_slice_item) def test_float_in_index(): - with fluid.dygraph.guard(): + with base.dygraph.guard(): data = np.random.random((2, 80, 16128)).astype('float32') - var = fluid.dygraph.to_variable(data) + var = base.dygraph.to_variable(data) sliced = var[1.1] self.assertRaises(Exception, test_float_in_index) @@ -860,7 +860,7 @@ def test(self): def test_axis_less_than_zero(self): # Using paddle.disable_static will make other unittests fail. - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_arr = np.arange(0, 24, dtype=np.float32).reshape([2, 3, 4]) x = paddle.to_tensor(x_arr) @@ -904,7 +904,7 @@ def test_axis_less_than_zero(self): class TestSliceOpError(unittest.TestCase): def test_dismatch_shape(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): with self.assertRaises(ValueError): array = np.array([], dtype=np.float32) x = paddle.to_tensor(np.reshape(array, [0]), dtype='float32') @@ -928,13 +928,13 @@ def test_dismatch_shape(self): ) class TestImperativeCUDAPinnedInput(unittest.TestCase): def test_input_cuda_pinned_var(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): data = np.random.random((2, 80, 16128)).astype('float32') var = core.eager.Tensor( value=data, name='', persistable=False, - place=fluid.CUDAPinnedPlace(), + place=base.CUDAPinnedPlace(), zero_copy=False, ) sliced = var[:, 10:, : var.shape[1]] @@ -969,9 +969,9 @@ def func(self, place): def test_grad(self): with paddle_static_guard(): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -1004,9 +1004,9 @@ def func(self, place): def test_grad(self): with paddle_static_guard(): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_slice_var.py b/test/legacy_test/test_slice_var.py index 29ec23bd13e6a..1ed9d4dfa9e27 100644 --- a/test/legacy_test/test_slice_var.py +++ b/test/legacy_test/test_slice_var.py @@ -15,14 +15,14 @@ import random import unittest -from paddle import fluid +from paddle import base from paddle.distributed.transpiler.distribute_transpiler import slice_variable class TestSliceVar(unittest.TestCase): def check_slice_output(self, shapes, expected_sizes, min_size): var_list = [] - program = fluid.Program() + program = base.Program() for shape in shapes: var = program.global_block().create_var( name=str(random.randint(10000, 99999)), diff --git a/test/legacy_test/test_smooth_l1_loss.py b/test/legacy_test/test_smooth_l1_loss.py index 9ad2e0ae718cb..f070b747aeb5e 100644 --- a/test/legacy_test/test_smooth_l1_loss.py +++ b/test/legacy_test/test_smooth_l1_loss.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base def smooth_l1_loss_forward(val, delta): @@ -46,14 +46,14 @@ def setUp(self): def test_smooth_l1_loss_mean(self): input_np = np.random.random([100, 200]).astype(np.float32) label_np = np.random.random([100, 200]).astype(np.float32) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype='float32' ) @@ -63,7 +63,7 @@ def test_smooth_l1_loss_mean(self): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss() ret = smooth_l1_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_ret,) = exe.run( prog, feed={ @@ -73,11 +73,11 @@ def test_smooth_l1_loss_mean(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss() dy_ret = smooth_l1_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -89,14 +89,14 @@ def test_smooth_l1_loss_mean(self): def test_smooth_l1_loss_sum(self): input_np = np.random.random([100, 200]).astype(np.float32) label_np = np.random.random([100, 200]).astype(np.float32) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype='float32' ) @@ -106,7 +106,7 @@ def test_smooth_l1_loss_sum(self): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(reduction='sum') ret = smooth_l1_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_ret,) = exe.run( prog, feed={ @@ -116,11 +116,11 @@ def test_smooth_l1_loss_sum(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(reduction='sum') dy_ret = smooth_l1_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -132,14 +132,14 @@ def test_smooth_l1_loss_sum(self): def test_smooth_l1_loss_none(self): input_np = np.random.random([100, 200]).astype(np.float32) label_np = np.random.random([100, 200]).astype(np.float32) - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype='float32' ) @@ -149,7 +149,7 @@ def test_smooth_l1_loss_none(self): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(reduction='none') ret = smooth_l1_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_ret,) = exe.run( prog, feed={ @@ -159,11 +159,11 @@ def test_smooth_l1_loss_none(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(reduction='none') dy_ret = smooth_l1_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) @@ -176,14 +176,14 @@ def test_smooth_l1_loss_delta(self): input_np = np.random.random([100, 200]).astype(np.float32) label_np = np.random.random([100, 200]).astype(np.float32) delta = np.random.rand() - prog = fluid.Program() - startup_prog = fluid.Program() + prog = base.Program() + startup_prog = base.Program() place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - with fluid.program_guard(prog, startup_prog): + with base.program_guard(prog, startup_prog): input = paddle.static.data( name='input', shape=[100, 200], dtype='float32' ) @@ -193,7 +193,7 @@ def test_smooth_l1_loss_delta(self): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(delta=delta) ret = smooth_l1_loss(input, label) - exe = fluid.Executor(place) + exe = base.Executor(place) (static_ret,) = exe.run( prog, feed={ @@ -203,11 +203,11 @@ def test_smooth_l1_loss_delta(self): fetch_list=[ret], ) self.assertIsNotNone(static_ret) - with fluid.dygraph.guard(): + with base.dygraph.guard(): smooth_l1_loss = paddle.nn.loss.SmoothL1Loss(delta=delta) dy_ret = smooth_l1_loss( - fluid.dygraph.to_variable(input_np), - fluid.dygraph.to_variable(label_np), + base.dygraph.to_variable(input_np), + base.dygraph.to_variable(label_np), ) dy_ret_value = dy_ret.numpy() self.assertIsNotNone(dy_ret_value) diff --git a/test/legacy_test/test_softmax2d.py b/test/legacy_test/test_softmax2d.py index 26bc47038bae6..4d57793744993 100644 --- a/test/legacy_test/test_softmax2d.py +++ b/test/legacy_test/test_softmax2d.py @@ -18,7 +18,7 @@ from test_softmax_op import ref_softmax import paddle -from paddle.fluid import core +from paddle.base import core class TestSoftmax2DAPI(unittest.TestCase): diff --git a/test/legacy_test/test_softmax_mask_fuse_op.py b/test/legacy_test/test_softmax_mask_fuse_op.py index 56a4ba24a6862..2adb2d4ce7029 100644 --- a/test/legacy_test/test_softmax_mask_fuse_op.py +++ b/test/legacy_test/test_softmax_mask_fuse_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid, incubate -from paddle.fluid import core +from paddle import base, incubate +from paddle.base import core paddle.enable_static() @@ -104,7 +104,7 @@ def test_check_grad(self): ) class TestDropoutBiasFuseOp3(unittest.TestCase): def test_static_result(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = paddle.static.data( name="x", shape=[1, 1, 8, 32], dtype="float32" ) @@ -118,22 +118,22 @@ def test_static_result(self): mask_in_np = np.where(mask == 1, -10000.0, mask) rst_np = _get_softmax(x_in_np, mask_in_np, False) - exe = fluid.Executor(fluid.CUDAPlace(0)) + exe = base.Executor(base.CUDAPlace(0)) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_in_np, "mask": mask_in_np}, fetch_list=[rst], ) np.testing.assert_allclose(fetches[0], rst_np, rtol=1e-05) def test_dygraph(self): - with fluid.dygraph.guard(fluid.CUDAPlace(0)): + with base.dygraph.guard(base.CUDAPlace(0)): x_in_np = np.random.random((1, 1, 8, 32)).astype("float32") mask = np.random.randint(0, 2, (1, 1, 8, 32)).astype("float32") mask_in_np = np.where(mask == 1, -10000.0, mask) rst_np = _get_softmax(x_in_np, mask_in_np, False) - input_x = fluid.dygraph.to_variable(x_in_np) - input_mask = fluid.dygraph.to_variable(mask_in_np) + input_x = base.dygraph.to_variable(x_in_np) + input_mask = base.dygraph.to_variable(mask_in_np) rst = incubate.softmax_mask_fuse(input_x, input_mask) np.testing.assert_allclose(rst, rst_np, rtol=1e-05) diff --git a/test/legacy_test/test_softmax_mask_fuse_upper_triangle_op.py b/test/legacy_test/test_softmax_mask_fuse_upper_triangle_op.py index cf1efa779dc83..67f2676a12885 100644 --- a/test/legacy_test/test_softmax_mask_fuse_upper_triangle_op.py +++ b/test/legacy_test/test_softmax_mask_fuse_upper_triangle_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid, incubate -from paddle.fluid import core +from paddle import base, incubate +from paddle.base import core paddle.enable_static() @@ -92,7 +92,7 @@ def setUp(self): def test_static(self): for dtype in self.dtypes: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_x = paddle.static.data( name="x", shape=[1, 4, 32, 32], dtype=dtype ) @@ -101,9 +101,9 @@ def test_static(self): x_in_np = np.random.random((1, 4, 32, 32)).astype(dtype) rst_np = _get_softmax_upper(x_in_np, dtype == 'float16') - exe = fluid.Executor(fluid.CUDAPlace(0)) + exe = base.Executor(base.CUDAPlace(0)) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_in_np}, fetch_list=[rst], ) @@ -111,10 +111,10 @@ def test_static(self): def test_dygraph(self): for dtype in self.dtypes: - with fluid.dygraph.guard(fluid.CUDAPlace(0)): + with base.dygraph.guard(base.CUDAPlace(0)): x_in_np = np.random.random((1, 4, 32, 32)).astype(dtype) rst_np = _get_softmax_upper(x_in_np, dtype == 'float16') - input_x = fluid.dygraph.to_variable(x_in_np) + input_x = base.dygraph.to_variable(x_in_np) rst = incubate.softmax_mask_fuse_upper_triangle(input_x) np.testing.assert_allclose(rst, rst_np, rtol=1e-05) diff --git a/test/legacy_test/test_softmax_op.py b/test/legacy_test/test_softmax_op.py index 4374dede00b66..6eb86b95fa620 100644 --- a/test/legacy_test/test_softmax_op.py +++ b/test/legacy_test/test_softmax_op.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core np.random.seed(10) @@ -67,7 +67,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.apply_along_axis(stable_softmax, self.axis, x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = { 'axis': self.axis, @@ -130,7 +130,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, []).astype(self.dtype) out = np.array(1.0).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = { 'axis': -1, @@ -164,7 +164,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, []).astype(self.dtype) out = np.array(1.0).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = { 'axis': -1, @@ -412,7 +412,7 @@ def setUp(self): out = np.apply_along_axis(stable_softmax, self.axis, x) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(convert_float_to_uint16(x)) + 'X': OpTest.np_dtype_to_base_dtype(convert_float_to_uint16(x)) } self.outputs = {'Out': convert_float_to_uint16(out)} self.attrs = { @@ -468,7 +468,7 @@ def executed_api(self): self.softmax = F.softmax def test_static_check(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('X', self.x_np.shape, 'float32') out1 = self.softmax(x) @@ -512,7 +512,7 @@ def test_dygraph_check(self): paddle.enable_static() def test_error(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with paddle.static.program_guard(paddle.static.Program()): # The input type must be Variable. self.assertRaises(TypeError, self.softmax, 1) @@ -546,19 +546,19 @@ def test_dygraph(self): paddle.enable_static() def test_static(self): - with paddle.fluid.framework._static_guard(): - main_prog = fluid.Program() - with fluid.program_guard(main_prog, fluid.Program()): + with paddle.base.framework._static_guard(): + main_prog = base.Program() + with base.program_guard(main_prog, base.Program()): x = paddle.rand([]) x.stop_gradient = False out = paddle.nn.functional.softmax(x) - fluid.backward.append_backward(out) + base.backward.append_backward(out) # Test compile shape self.assertEqual(x.shape, ()) self.assertEqual(out.shape, ()) - exe = fluid.Executor() + exe = base.Executor() result = exe.run(main_prog, fetch_list=[x, out]) # Test runtime shape diff --git a/test/legacy_test/test_softmax_with_cross_entropy_op.py b/test/legacy_test/test_softmax_with_cross_entropy_op.py index f94c6c4772cb1..73828072e9661 100644 --- a/test/legacy_test/test_softmax_with_cross_entropy_op.py +++ b/test/legacy_test/test_softmax_with_cross_entropy_op.py @@ -19,7 +19,7 @@ from test_softmax_op import stable_softmax import paddle -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard def cross_entropy(softmax, label, soft_label, axis, ignore_index=-1): diff --git a/test/legacy_test/test_solve_op.py b/test/legacy_test/test_solve_op.py index bda4d46bd627b..ecb05b1d18a28 100644 --- a/test/legacy_test/test_solve_op.py +++ b/test/legacy_test/test_solve_op.py @@ -18,13 +18,13 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core sys.path.append("..") from eager_op_test import OpTest -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard # 2D normal case @@ -249,11 +249,11 @@ class TestSolveOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # The input type of solve_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) - y1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + y1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.linalg.solve, x1, y1) @@ -295,7 +295,7 @@ def setUp(self): self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle_input_x = paddle.static.data( name="input_x", shape=[3, 3], dtype=self.dtype ) @@ -309,9 +309,9 @@ def check_static_result(self, place): np_result = np.linalg.solve(np_input_x, np_input_y) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": np_input_x, "input_y": np_input_y}, fetch_list=[paddle_result], ) @@ -356,7 +356,7 @@ def setUp(self): def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle_input_x = paddle.static.data( name="input_x", shape=[10, 10], dtype=self.dtype ) @@ -370,9 +370,9 @@ def check_static_result(self, place): np_result = np.linalg.solve(np_input_x, np_input_y) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": np_input_x, "input_y": np_input_y}, fetch_list=[paddle_result], ) @@ -416,7 +416,7 @@ def setUp(self): def check_static_result(self, place): paddle.enable_static() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle_input_x = paddle.static.data( name="input_x", shape=[10, 10], dtype=self.dtype ) @@ -430,9 +430,9 @@ def check_static_result(self, place): np_result = np.linalg.solve(np_input_x, np_input_y) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": np_input_x, "input_y": np_input_y}, fetch_list=[paddle_result], ) @@ -476,7 +476,7 @@ def setUp(self): self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle_input_x = paddle.static.data( name="input_x", shape=[2, 3, 3], dtype=self.dtype ) @@ -490,9 +490,9 @@ def check_static_result(self, place): np_result = np.linalg.solve(np_input_x, np_input_y) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": np_input_x, "input_y": np_input_y}, fetch_list=[paddle_result], ) @@ -529,13 +529,13 @@ def run(place): class TestSolveOpSingularAPI(unittest.TestCase): # Singular matrix is ​​not invertible def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] self.dtype = "float64" if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data(name="x", shape=[4, 4], dtype=self.dtype) y = paddle.static.data(name="y", shape=[4, 4], dtype=self.dtype) @@ -544,10 +544,10 @@ def check_static_result(self, place): input_x_np = np.ones([4, 4]).astype(self.dtype) input_y_np = np.ones([4, 4]).astype(self.dtype) - exe = fluid.Executor(place) + exe = base.Executor(place) try: fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": input_x_np, "y": input_y_np}, fetch_list=[result], ) @@ -563,11 +563,11 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x_np = np.ones([4, 4]).astype(self.dtype) input_y_np = np.ones([4, 4]).astype(self.dtype) - input_x = fluid.dygraph.to_variable(input_x_np) - input_y = fluid.dygraph.to_variable(input_y_np) + input_x = base.dygraph.to_variable(input_x_np) + input_y = base.dygraph.to_variable(input_y_np) try: result = paddle.linalg.solve(input_x, input_y) except RuntimeError as ex: diff --git a/test/legacy_test/test_sort_op.py b/test/legacy_test/test_sort_op.py index c2a8de287e6b9..bbae7e75c833b 100644 --- a/test/legacy_test/test_sort_op.py +++ b/test/legacy_test/test_sort_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestSortOnCPU(unittest.TestCase): @@ -26,12 +26,12 @@ def setUp(self): self.place = core.CPUPlace() def test_api_0(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 4], dtype="float32" ) output = paddle.sort(x=input) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) data = np.array( [ [[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]], @@ -44,12 +44,12 @@ def test_api_0(self): self.assertEqual((result == np_result).all(), True) def test_api_1(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): input = paddle.static.data( name="input", shape=[2, 3, 4], dtype="float32" ) output = paddle.sort(x=input, axis=1) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) data = np.array( [ [[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]], diff --git a/test/legacy_test/test_space_to_depth_op.py b/test/legacy_test/test_space_to_depth_op.py index 4f3af77924270..3e37ccfb2e104 100644 --- a/test/legacy_test/test_space_to_depth_op.py +++ b/test/legacy_test/test_space_to_depth_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle import fluid +from paddle import base class TestSpaceToDepthOp(OpTest): @@ -75,9 +75,9 @@ def init_data(self): def test_check_output(self): place = ( - fluid.core.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.core.CPUPlace() + base.core.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.core.CPUPlace() ) self.check_output_with_place( place=place, atol=1e-5, no_check_set=None, equal_nan=False @@ -85,9 +85,9 @@ def test_check_output(self): def test_check_grad(self): place = ( - fluid.core.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.core.CPUPlace() + base.core.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.core.CPUPlace() ) self.check_grad_with_place(place, ['X'], 'Out') diff --git a/test/legacy_test/test_sparse_attention_op.py b/test/legacy_test/test_sparse_attention_op.py index 87306b8b8d542..2a80906ad74aa 100644 --- a/test/legacy_test/test_sparse_attention_op.py +++ b/test/legacy_test/test_sparse_attention_op.py @@ -22,8 +22,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def get_cuda_version(): @@ -372,7 +372,7 @@ def test_static_graph(self): key_padding_mask_np = key_padding_mask_np.astype(self.dtype) attn_mask_np = attn_mask_np.astype(self.dtype) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) if self.use_mask: fetches_result = exe.run( feed={ diff --git a/test/legacy_test/test_sparse_conv_op.py b/test/legacy_test/test_sparse_conv_op.py index 2d2af3c11fcb6..755f792215834 100644 --- a/test/legacy_test/test_sparse_conv_op.py +++ b/test/legacy_test/test_sparse_conv_op.py @@ -19,7 +19,7 @@ import paddle from paddle import sparse -from paddle.fluid import core +from paddle.base import core logging.basicConfig( format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO diff --git a/test/legacy_test/test_sparse_embedding_op.py b/test/legacy_test/test_sparse_embedding_op.py index 0e0beda67971e..b70cb26c012a8 100644 --- a/test/legacy_test/test_sparse_embedding_op.py +++ b/test/legacy_test/test_sparse_embedding_op.py @@ -19,7 +19,7 @@ class TestSparseEmbeddingAPIError(unittest.TestCase): def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in sparse_embedding should not be 0. def test_0_size(): input = paddle.to_tensor([], dtype='int64') diff --git a/test/legacy_test/test_sparse_fused_attention_op.py b/test/legacy_test/test_sparse_fused_attention_op.py index d2db24218e1d2..68cdd16d4bd12 100644 --- a/test/legacy_test/test_sparse_fused_attention_op.py +++ b/test/legacy_test/test_sparse_fused_attention_op.py @@ -21,7 +21,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def get_cuda_version(): diff --git a/test/legacy_test/test_sparse_norm_op.py b/test/legacy_test/test_sparse_norm_op.py index 7d745ada2da7f..039c147a3bb4f 100644 --- a/test/legacy_test/test_sparse_norm_op.py +++ b/test/legacy_test/test_sparse_norm_op.py @@ -18,7 +18,7 @@ import numpy as np import paddle -from paddle import fluid, sparse +from paddle import base, sparse from paddle.sparse import nn @@ -157,8 +157,8 @@ def test_convert(self): nn.BatchNorm(5), nn.BatchNorm( 5, - weight_attr=fluid.ParamAttr(name='bn.scale'), - bias_attr=fluid.ParamAttr(name='bn.bias'), + weight_attr=base.ParamAttr(name='bn.scale'), + bias_attr=base.ParamAttr(name='bn.bias'), ), ) model = nn.SyncBatchNorm.convert_sync_batchnorm(model) diff --git a/test/legacy_test/test_sparse_unary_op.py b/test/legacy_test/test_sparse_unary_op.py index 908121ace0118..1f04694747e71 100644 --- a/test/legacy_test/test_sparse_unary_op.py +++ b/test/legacy_test/test_sparse_unary_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestSparseUnary(unittest.TestCase): diff --git a/test/legacy_test/test_sparse_utils_op.py b/test/legacy_test/test_sparse_utils_op.py index 2b7583db92175..f387b0738dc81 100644 --- a/test/legacy_test/test_sparse_utils_op.py +++ b/test/legacy_test/test_sparse_utils_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core devices = ['cpu', 'gpu'] diff --git a/test/legacy_test/test_spawn_and_init_parallel_env.py b/test/legacy_test/test_spawn_and_init_parallel_env.py index 290d48d72c911..69a35448b707d 100644 --- a/test/legacy_test/test_spawn_and_init_parallel_env.py +++ b/test/legacy_test/test_spawn_and_init_parallel_env.py @@ -18,12 +18,12 @@ import paddle import paddle.distributed as dist +from paddle.base import core from paddle.distributed.spawn import ( _get_default_nprocs, _get_subprocess_env_list, _options_valid_check, ) -from paddle.fluid import core # NOTE(chenweihang): Coverage CI is currently not able to count python3 # unittest, so the unittests here covers some cases that will only be diff --git a/test/legacy_test/test_spectral_norm_op.py b/test/legacy_test/test_spectral_norm_op.py index 74ba596087212..d0b4b712ce087 100644 --- a/test/legacy_test/test_spectral_norm_op.py +++ b/test/legacy_test/test_spectral_norm_op.py @@ -19,7 +19,7 @@ import paddle from paddle import _C_ops -from paddle.fluid.framework import Program, program_guard +from paddle.base.framework import Program, program_guard paddle.enable_static() diff --git a/test/legacy_test/test_split_op.py b/test/legacy_test/test_split_op.py index 8f7781f925cea..77a720ffc8d30 100644 --- a/test/legacy_test/test_split_op.py +++ b/test/legacy_test/test_split_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestSplitOp(OpTest): @@ -315,9 +315,9 @@ def test_api(self): ) paddle.split(x=x_2, num_or_sections=2, axis=2) - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) [res_0, res_1, res_2, res_3, res_4, res_5] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_1, "x_2": input_1}, fetch_list=[out_0, out_1, out_2, out_3, out_4, out_5], ) @@ -380,7 +380,7 @@ def test_axis_type_tensor(): self.assertRaises(TypeError, test_axis_type_tensor) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): def test_0_num_tensor(): x = paddle.uniform([1, 1, 1], dtype='float32') @@ -391,7 +391,7 @@ def test_0_num_tensor(): class API_TestSplit(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( 'data1', shape=[-1, 4, 6, 6], dtype='float64' ) @@ -399,8 +399,8 @@ def test_out(self): data2 = paddle.static.data('data2', shape=[-1, 1], dtype='int32') data2.desc.set_need_check_feed(False) x0, x1, x2 = paddle.split(data1, num_or_sections=3, axis=data2) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([4, 6, 6]).astype('float64') input2 = np.array([2]).astype('int32') ( @@ -418,14 +418,14 @@ def test_out(self): class API_TestSplit2(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( 'data1', shape=[-1, 4, 6, 6], dtype='float64' ) data1.desc.set_need_check_feed(False) x0, x1, x2 = paddle.split(data1, num_or_sections=3, axis=2) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([4, 6, 6]).astype('float64') ( r0, @@ -440,11 +440,11 @@ def test_out(self): class API_TestSplit3(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data = paddle.static.data('data', shape=[-1, 10], dtype='float64') x0, x1 = paddle.split(data, num_or_sections=(3, 7), axis=1) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([1, 10]).astype('float64') r0, r1 = exe.run(feed={"data": input1}, fetch_list=[x0, x1]) ex_x0, ex_x1 = np.split(input1, (3,), axis=1) @@ -454,12 +454,12 @@ def test_out(self): class API_TestSplit4(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data = paddle.static.data('data', shape=[-1, 10], dtype='float64') index = paddle.static.data('index', shape=[1], dtype='int32') x0, x1 = paddle.split(data, num_or_sections=(3, index), axis=1) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([1, 10]).astype('float64') input2 = np.array([7]).astype('int32') r0, r1 = exe.run( @@ -476,7 +476,7 @@ def test_out(self): [False, True] if core.is_compiled_with_cuda() else [False] ): place = paddle.CUDAPlace(0) if use_cuda else paddle.CPUPlace() - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input_1 = np.random.random([5, 4]).astype("int32") # input is a variable which shape is [5, 4] input = paddle.to_tensor(input_1) @@ -492,11 +492,11 @@ def test_out(self): class API_TestSplit6(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data = paddle.static.data('data', shape=[-1, 10], dtype='float64') x0, x1 = paddle.split(data, num_or_sections=[1, 1], axis=0) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([2, 10]).astype('float64') r0, r1 = exe.run(feed={"data": input1}, fetch_list=[x0, x1]) ex_x0, ex_x1 = np.split(input1, (1,), axis=0) @@ -506,7 +506,7 @@ def test_out(self): class API_TestDygraphFluidSplit(unittest.TestCase): def test_out1(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -536,7 +536,7 @@ def test_out1(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_out2(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -568,7 +568,7 @@ def test_out2(self): class API_TestDygraphSplit(unittest.TestCase): def test_out1(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -599,7 +599,7 @@ def test_out1(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_out2(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("bool") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -613,7 +613,7 @@ def test_out2(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_out3(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np.random.seed(2021) input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] @@ -631,7 +631,7 @@ def test_out3(self): np.testing.assert_allclose(ex_out, out_dy_np, rtol=1e-05) def test_out_tensor_input(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -648,7 +648,7 @@ def test_out_tensor_input(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_axis_tensor_input(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -665,7 +665,7 @@ def test_axis_tensor_input(self): np.testing.assert_allclose(ex_x2, x2_out, rtol=1e-05) def test_negative_one_section(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([4, 6, 6]).astype("int32") # input is a variable which shape is [4, 6, 6] input = paddle.to_tensor(input_1) @@ -677,7 +677,7 @@ def test_negative_one_section(self): class API_TestEmptySplit(unittest.TestCase): def test_axis_input_empty_section(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input_1 = np.random.random([8, 6, 6]).astype("float32") # input is a variable which shape is [8, 6, 6] input = paddle.to_tensor(input_1) diff --git a/test/legacy_test/test_splits_api.py b/test/legacy_test/test_splits_api.py index 74722d83632a7..2b562179b8752 100644 --- a/test/legacy_test/test_splits_api.py +++ b/test/legacy_test/test_splits_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def func_ref(func, x, num_or_sections): diff --git a/test/legacy_test/test_square_error_cost.py b/test/legacy_test/test_square_error_cost.py index 05932c67d589f..3dd54557e051e 100644 --- a/test/legacy_test/test_square_error_cost.py +++ b/test/legacy_test/test_square_error_cost.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import core +from paddle.base.executor import Executor class TestSquareErrorCost(unittest.TestCase): @@ -39,10 +39,10 @@ def test_square_error_cost(self): for use_cuda in ( [False, True] if core.is_compiled_with_cuda() else [False] ): - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() exe = Executor(place) (result,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_val, "label": label_val}, fetch_list=[output], ) diff --git a/test/legacy_test/test_squared_l2_norm_op.py b/test/legacy_test/test_squared_l2_norm_op.py index 4067acd29c525..96872b10f1295 100755 --- a/test/legacy_test/test_squared_l2_norm_op.py +++ b/test/legacy_test/test_squared_l2_norm_op.py @@ -96,7 +96,7 @@ def test_check_grad(self): class TestL2LossDeterministic(unittest.TestCase): def check_place(self, place): - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): x_np = np.random.rand(5, 11, 13).astype('float32') x = paddle.to_tensor(x_np) y1 = _legacy_C_ops.squared_l2_norm(x) diff --git a/test/legacy_test/test_squeeze2_op.py b/test/legacy_test/test_squeeze2_op.py index f43ccb8ba8120..ec9b96aed46cc 100755 --- a/test/legacy_test/test_squeeze2_op.py +++ b/test/legacy_test/test_squeeze2_op.py @@ -20,8 +20,8 @@ from test_attribute_var import UnittestBase import paddle -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle.base import core +from paddle.base.framework import Program, program_guard paddle.enable_static() diff --git a/test/legacy_test/test_squeeze_op.py b/test/legacy_test/test_squeeze_op.py index f0400f24667d5..6cdfa5fec0f02 100755 --- a/test/legacy_test/test_squeeze_op.py +++ b/test/legacy_test/test_squeeze_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard paddle.enable_static() @@ -140,7 +140,7 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # The input type of softmax_op must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([[-1]]), [[1]], paddle.CPUPlace() ) self.assertRaises(TypeError, paddle.squeeze, x1) @@ -265,9 +265,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -296,9 +296,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_stack_op.py b/test/legacy_test/test_stack_op.py index 5c5e653dbaeb6..5cc2234555853 100644 --- a/test/legacy_test/test_stack_op.py +++ b/test/legacy_test/test_stack_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base.framework import Program, program_guard paddle.enable_static() @@ -204,15 +204,15 @@ def setUp(self): self.input_shape = [2, 3] self.x = np.random.random(self.input_shape).astype("float32") self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.set_program() def set_program(self): - self.program = fluid.Program() - with fluid.program_guard(self.program): + self.program = base.Program() + with base.program_guard(self.program): input = paddle.assign(self.x) tensor_array = paddle.tensor.create_array(dtype='float32') zero = paddle.tensor.fill_constant( @@ -226,7 +226,7 @@ def set_program(self): def test_case(self): self.assertTrue(self.out_var.shape[self.axis] == -1) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(self.program, fetch_list=self.out_var) np.testing.assert_array_equal( res[0], np.stack([self.x] * self.iter_num, axis=self.axis) @@ -244,15 +244,15 @@ def setUp(self): self.input_shape = [2, 3] self.x = np.random.random(self.input_shape).astype("float32") self.place = ( - fluid.CUDAPlace(0) - if fluid.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.is_compiled_with_cuda() + else base.CPUPlace() ) self.set_program() def set_program(self): - self.program = fluid.Program() - with fluid.program_guard(self.program): + self.program = base.Program() + with base.program_guard(self.program): input = paddle.assign(self.x) tensor_array = paddle.tensor.create_array(dtype='float32') zero = paddle.tensor.fill_constant( @@ -266,7 +266,7 @@ def set_program(self): def test_case(self): self.assertTrue(self.out_var.shape[self.axis] == -1) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) res = exe.run(self.program, fetch_list=self.out_var) np.testing.assert_array_equal( res[0], np.stack([self.x] * self.iter_num, axis=self.axis) @@ -275,13 +275,13 @@ def test_case(self): class API_test(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data('data1', shape=[1, 2], dtype='float64') data2 = paddle.static.data('data2', shape=[1, 2], dtype='float64') data3 = paddle.static.data('data3', shape=[1, 2], dtype='float64') result_stack = paddle.stack([data1, data2, data3], axis=0) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) input1 = np.random.random([1, 2]).astype('float64') input2 = np.random.random([1, 2]).astype('float64') input3 = np.random.random([1, 2]).astype('float64') @@ -293,7 +293,7 @@ def test_out(self): np.testing.assert_allclose(expected_result, result, rtol=1e-05) def test_single_tensor_error(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.rand([2, 3]) self.assertRaises(TypeError, paddle.stack, x) @@ -303,24 +303,24 @@ def test_out(self): data1 = np.array([[1.0, 2.0]]) data2 = np.array([[3.0, 4.0]]) data3 = np.array([[5.0, 6.0]]) - with fluid.dygraph.guard(): - x1 = fluid.dygraph.to_variable(data1) - x2 = fluid.dygraph.to_variable(data2) - x3 = fluid.dygraph.to_variable(data3) + with base.dygraph.guard(): + x1 = base.dygraph.to_variable(data1) + x2 = base.dygraph.to_variable(data2) + x3 = base.dygraph.to_variable(data3) result = paddle.stack([x1, x2, x3]) result_np = result.numpy() expected_result = np.stack([data1, data2, data3]) np.testing.assert_allclose(expected_result, result_np, rtol=1e-05) - with fluid.dygraph.guard(): - y1 = fluid.dygraph.to_variable(data1) + with base.dygraph.guard(): + y1 = base.dygraph.to_variable(data1) result = paddle.stack([y1], axis=0) result_np_2 = result.numpy() expected_result_2 = np.stack([data1], axis=0) np.testing.assert_allclose(expected_result_2, result_np_2, rtol=1e-05) def test_single_tensor_error(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor([1, 2, 3]) self.assertRaises(Exception, paddle.stack, x) @@ -383,7 +383,7 @@ def setUp(self): def test_list_single_tensor(self): expect = paddle.stack(self.x) - paddle.fluid.core._set_prim_all_enabled(True) + paddle.base.core._set_prim_all_enabled(True) st_model = paddle.jit.to_static(paddle.stack) actual = st_model(self.x) np.testing.assert_allclose(expect, actual) diff --git a/test/legacy_test/test_static_model_parallel_fused_attention.py b/test/legacy_test/test_static_model_parallel_fused_attention.py index c00a91bda1f13..e11e1d25f2fa7 100644 --- a/test/legacy_test/test_static_model_parallel_fused_attention.py +++ b/test/legacy_test/test_static_model_parallel_fused_attention.py @@ -32,9 +32,9 @@ def _setup_config(self): self._pipeline_mode = True def test_dist_static_model_parallel_fused_feedforward(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "static_model_parallel_fused_attention.py", delta=1e-5, diff --git a/test/legacy_test/test_static_model_parallel_fused_feedforward.py b/test/legacy_test/test_static_model_parallel_fused_feedforward.py index 9c8c95bf769ca..61a0d5b4a00b3 100644 --- a/test/legacy_test/test_static_model_parallel_fused_feedforward.py +++ b/test/legacy_test/test_static_model_parallel_fused_feedforward.py @@ -32,9 +32,9 @@ def _setup_config(self): self._pipeline_mode = True def test_dist_static_model_parallel_fused_feedforward(self): - from paddle import fluid + from paddle import base - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): self.check_with_place( "static_model_parallel_fused_feedforward.py", delta=1e-5, diff --git a/test/legacy_test/test_static_model_parallel_fused_multi_transformer.py b/test/legacy_test/test_static_model_parallel_fused_multi_transformer.py index 705680b531b30..729772699d90e 100644 --- a/test/legacy_test/test_static_model_parallel_fused_multi_transformer.py +++ b/test/legacy_test/test_static_model_parallel_fused_multi_transformer.py @@ -32,10 +32,10 @@ def _setup_config(self): self._pipeline_mode = True def test_dist_static_model_parallel_fused_multi_transformer(self): - from paddle import fluid + from paddle import base if ( - fluid.core.is_compiled_with_cuda() + base.core.is_compiled_with_cuda() and not paddle.is_compiled_with_rocm() ): self.check_with_place( diff --git a/test/legacy_test/test_static_pylayer.py b/test/legacy_test/test_static_pylayer.py index 2cd61de9517df..d62bfb2fd2afa 100644 --- a/test/legacy_test/test_static_pylayer.py +++ b/test/legacy_test/test_static_pylayer.py @@ -17,10 +17,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.framework import Program, program_guard np.random.seed(123) @@ -45,11 +45,11 @@ def forward_fn(x): out = paddle.static.nn.static_pylayer(forward_fn, [data]) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) x = np.array([2.0], dtype=np.float32) (ret,) = exe.run(main_program, feed={"X": x}, fetch_list=[out.name]) np.testing.assert_allclose( @@ -76,11 +76,11 @@ def forward_fn(x): out = paddle.static.nn.static_pylayer(forward_fn, [data]) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) (ret,) = exe.run(main_program, fetch_list=[out.name]) np.testing.assert_allclose( np.asarray(ret), np.array(6.0, np.float32), rtol=1e-05 @@ -114,11 +114,11 @@ def backward_fn(dy): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret, x_grad = exe.run( main_program, fetch_list=[out.name, data.grad_name] ) @@ -146,11 +146,11 @@ def forward_fn(a, b): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret_1, ret_2 = exe.run( main_program, fetch_list=[out_1.name, out_2.name] ) @@ -184,11 +184,11 @@ def forward_fn(x): out = paddle.static.nn.static_pylayer(forward_fn, [data]) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(main_program) self.assertIsNone(out) @@ -284,11 +284,11 @@ def backward_fn(diout, daout): append_backward(loss) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) for feed_i in range(0, 10): print(feed_i) expected_a = 2.0 * feed_i diff --git a/test/legacy_test/test_static_pylayer_block.py b/test/legacy_test/test_static_pylayer_block.py index 060ea4d22d05c..b91125d47bffa 100644 --- a/test/legacy_test/test_static_pylayer_block.py +++ b/test/legacy_test/test_static_pylayer_block.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static import Executor, append_backward from paddle.static.nn.static_pylayer import StaticPyLayerBlock @@ -26,9 +26,9 @@ class StaticPyLayerBlockTest(unittest.TestCase): def test_forward_and_backward(self): paddle.enable_static() - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): data = paddle.static.data(name='X', shape=[10, 1], dtype='float32') data.stop_gradient = False static_pylayer_manager = StaticPyLayerBlock(inputs=[data]) diff --git a/test/legacy_test/test_static_save_load.py b/test/legacy_test/test_static_save_load.py index 9078de654d606..09e204e62191e 100644 --- a/test/legacy_test/test_static_save_load.py +++ b/test/legacy_test/test_static_save_load.py @@ -23,8 +23,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import core, framework +from paddle import base +from paddle.base import core, framework from paddle.optimizer import Adam paddle.enable_static() @@ -57,7 +57,7 @@ def __init__( for i in range(self._num_layers): weight_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -70,7 +70,7 @@ def __init__( ) self.weight_1_arr.append(self.add_parameter('w_%d' % i, weight_1)) bias_1 = self.create_parameter( - attr=fluid.ParamAttr( + attr=base.ParamAttr( initializer=paddle.nn.initializer.Uniform( low=-self._init_scale, high=self._init_scale ) @@ -181,7 +181,7 @@ def __init__( self.embedding = paddle.nn.Embedding( num_embeddings=vocab_size, embedding_dim=hidden_size, - weight_attr=fluid.ParamAttr( + weight_attr=base.ParamAttr( name='embedding_para', initializer=paddle.nn.initializer.Uniform( low=-init_scale, high=init_scale @@ -189,7 +189,7 @@ def __init__( ), ) self.softmax_weight = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.hidden_size, self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -197,7 +197,7 @@ def __init__( ), ) self.softmax_bias = self.create_parameter( - attr=fluid.ParamAttr(), + attr=base.ParamAttr(), shape=[self.vocab_size], dtype="float32", default_initializer=paddle.nn.initializer.Uniform( @@ -249,9 +249,9 @@ def forward(self, input, label, init_hidden, init_cell): class TestSaveLoadBase(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -266,8 +266,8 @@ def test_ptb_rnn_cpu_float32(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -278,7 +278,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -320,7 +320,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -339,7 +339,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -352,11 +352,11 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -370,7 +370,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -380,9 +380,9 @@ def test_ptb_rnn_cpu_float32(self): class TestSaveLoadPartial(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -397,8 +397,8 @@ def test_ptb_rnn_cpu_float32(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -409,7 +409,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -430,7 +430,7 @@ def test_ptb_rnn_cpu_float32(self): x, y, init_hidden, init_cell ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) add_1 = paddle.static.nn.fc( static_last_hidden, @@ -461,7 +461,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -480,7 +480,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -493,11 +493,11 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -509,7 +509,7 @@ def test_ptb_rnn_cpu_float32(self): for var in test_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -524,9 +524,9 @@ def test_ptb_rnn_cpu_float32(self): class TestSaveLoadSetStateDict(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -541,8 +541,8 @@ def test_ptb_rnn_cpu_float32(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -553,7 +553,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -595,7 +595,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -614,7 +614,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -627,11 +627,11 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -643,7 +643,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -653,9 +653,9 @@ def test_ptb_rnn_cpu_float32(self): class TestProgramStatePartial(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -670,8 +670,8 @@ def test_ptb_rnn_cpu_float32(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -682,7 +682,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -703,7 +703,7 @@ def test_ptb_rnn_cpu_float32(self): x, y, init_hidden, init_cell ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) add_1 = paddle.static.nn.fc( static_last_hidden, @@ -734,7 +734,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -753,7 +753,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -766,16 +766,16 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) - # fluid.load(test_program, "./test_1", None ) + # base.load(test_program, "./test_1", None ) program_state = paddle.static.load_program_state( os.path.join(temp_dir.name, 'test_1') ) @@ -797,7 +797,7 @@ def test_ptb_rnn_cpu_float32(self): for var in test_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -805,11 +805,11 @@ def test_ptb_rnn_cpu_float32(self): # check 1 for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -819,7 +819,7 @@ def test_ptb_rnn_cpu_float32(self): for var in test_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -827,11 +827,11 @@ def test_ptb_rnn_cpu_float32(self): # check 2 for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -841,7 +841,7 @@ def test_ptb_rnn_cpu_float32(self): for var in test_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -849,11 +849,11 @@ def test_ptb_rnn_cpu_float32(self): # check 3 for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -863,7 +863,7 @@ def test_ptb_rnn_cpu_float32(self): for var in test_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -873,9 +873,9 @@ def test_ptb_rnn_cpu_float32(self): class TestVariableInit(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_variable_init(self): @@ -884,12 +884,12 @@ def test_variable_init(self): z = paddle.static.nn.fc(y, 10) place = self.set_place() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) temp_dir = tempfile.TemporaryDirectory() paddle.static.save( - fluid.default_main_program(), + base.default_main_program(), os.path.join(temp_dir.name, "test_path"), ) @@ -897,26 +897,26 @@ def set_var(var, ndarray): t = var.get_tensor() p = t._place() if p.is_cpu_place(): - place = paddle.fluid.CPUPlace() + place = paddle.base.CPUPlace() elif p.is_cuda_pinned_place(): - place = paddle.fluid.CUDAPinnedPlace() + place = paddle.base.CUDAPinnedPlace() else: - p = paddle.fluid.core.Place() + p = paddle.base.core.Place() p.set_place(t._place()) - place = paddle.fluid.CUDAPlace(p.gpu_device_id()) + place = paddle.base.CUDAPlace(p.gpu_device_id()) t.set(ndarray, place) - program = fluid.default_main_program() - new_scope = fluid.core.Scope() + program = base.default_main_program() + new_scope = base.core.Scope() place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) parameter_list = list( filter(paddle.framework.is_parameter, program.list_vars()) ) - fluid.core._create_loaded_parameter( + base.core._create_loaded_parameter( parameter_list, new_scope, exe._default_executor ) parameter_file_name = os.path.join(temp_dir.name, "test_path.pdparams") @@ -939,7 +939,7 @@ def set_var(var, ndarray): ) ) - fluid.core._create_loaded_parameter( + base.core._create_loaded_parameter( opt_list, new_scope, exe._default_executor ) opt_file_name = os.path.join(temp_dir.name, "test_path.pdopt") @@ -960,7 +960,7 @@ def set_var(var, ndarray): for var in program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update base_map[var.name] = t @@ -986,9 +986,9 @@ def setUp(self): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def tearDown(self): @@ -1005,8 +1005,8 @@ def test_load_from_old_interface(self): batch_num = 200 with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -1017,7 +1017,7 @@ def test_load_from_old_interface(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -1038,7 +1038,7 @@ def test_load_from_old_interface(self): x, y, init_hidden, init_cell ) - test_clone_program = fluid.default_main_program().clone() + test_clone_program = base.default_main_program().clone() sgd.minimize(static_loss) static_param_updated = {} static_param_init = {} @@ -1061,7 +1061,7 @@ def test_load_from_old_interface(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -1080,13 +1080,13 @@ def test_load_from_old_interface(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) base_map[var.name] = t - # fluid.save(main_program, "./test_1") + # base.save(main_program, "./test_1") paddle.distributed.io.save_persistables( exe, os.path.join(self.temp_dir.name, "test_path"), main_program ) @@ -1094,11 +1094,11 @@ def test_load_from_old_interface(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -1110,14 +1110,14 @@ def test_load_from_old_interface(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() old_shape = np.array(ten).shape new_shape = [e + 10 for e in old_shape] @@ -1148,8 +1148,8 @@ def test_load_from_old_interface_var_list(self): batch_num = 200 with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -1160,7 +1160,7 @@ def test_load_from_old_interface_var_list(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -1180,7 +1180,7 @@ def test_load_from_old_interface_var_list(self): x, y, init_hidden, init_cell ) - test_clone_program = fluid.default_main_program().clone() + test_clone_program = base.default_main_program().clone() sgd.minimize(static_loss) static_param_updated = {} static_param_init = {} @@ -1203,7 +1203,7 @@ def test_load_from_old_interface_var_list(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -1222,13 +1222,13 @@ def test_load_from_old_interface_var_list(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) base_map[var.name] = t - # fluid.save(main_program, "./test_1") + # base.save(main_program, "./test_1") paddle.distributed.io.save_persistables( exe, os.path.join(self.temp_dir.name, "test_static_load_var_list"), @@ -1241,11 +1241,11 @@ def test_load_from_old_interface_var_list(self): if isinstance(var, framework.Parameter) or var.persistable: if i % 2 == 0: var_list.append(var) - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -1260,7 +1260,7 @@ def test_load_from_old_interface_var_list(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) if var.name in var_list_names: # loaded vars @@ -1274,9 +1274,9 @@ def test_load_from_old_interface_var_list(self): class TestLoadFromOldInterfaceSingleFile(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_load_from_old_interface(self): @@ -1291,8 +1291,8 @@ def test_load_from_old_interface(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -1303,7 +1303,7 @@ def test_load_from_old_interface(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -1345,7 +1345,7 @@ def test_load_from_old_interface(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -1364,13 +1364,13 @@ def test_load_from_old_interface(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) base_map[var.name] = t save_dir = os.path.join(temp_dir.name, "test_path") - # fluid.save(main_program, "./test_1") + # base.save(main_program, "./test_1") paddle.distributed.io.save_persistables( exe, save_dir, main_program, filename="model_single" ) @@ -1378,11 +1378,11 @@ def test_load_from_old_interface(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -1398,7 +1398,7 @@ def test_load_from_old_interface(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -1407,7 +1407,7 @@ def test_load_from_old_interface(self): # change shape for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() old_shape = np.array(ten).shape new_shape = [e + 10 for e in old_shape] @@ -1467,9 +1467,9 @@ def tearDown(self): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -1483,8 +1483,8 @@ def test_ptb_rnn_cpu_float32(self): batch_num = 200 with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -1495,7 +1495,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -1516,7 +1516,7 @@ def test_ptb_rnn_cpu_float32(self): x, y, init_hidden, init_cell ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) add_1 = paddle.static.nn.fc( static_last_hidden, @@ -1547,7 +1547,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -1566,7 +1566,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -1577,11 +1577,11 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -1615,7 +1615,7 @@ def symlink_force(target, link_name): if self.test_dygraph: # make sure `load_program_state` can be used in dynamic graph mode - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): load_state = paddle.static.load_program_state(save_dir) for k, v in load_state.items(): np.testing.assert_array_equal(base_map[k], v) @@ -1633,7 +1633,7 @@ def check_in_static(self, main_program, base_map): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -1642,9 +1642,9 @@ def check_in_static(self, main_program, base_map): class TestProgramStateOldSaveSingleModel(unittest.TestCase): def set_place(self): return ( - fluid.CPUPlace() + base.CPUPlace() if not core.is_compiled_with_cuda() - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) def test_ptb_rnn_cpu_float32(self): @@ -1659,8 +1659,8 @@ def test_ptb_rnn_cpu_float32(self): temp_dir = tempfile.TemporaryDirectory() with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -1671,7 +1671,7 @@ def test_ptb_rnn_cpu_float32(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = Adam(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -1692,7 +1692,7 @@ def test_ptb_rnn_cpu_float32(self): x, y, init_hidden, init_cell ) - test_program = fluid.default_main_program().clone(for_test=True) + test_program = base.default_main_program().clone(for_test=True) add_1 = paddle.static.nn.fc( static_last_hidden, @@ -1723,7 +1723,7 @@ def test_ptb_rnn_cpu_float32(self): ) fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -1742,7 +1742,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -1756,16 +1756,16 @@ def test_ptb_rnn_cpu_float32(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) - # fluid.load(test_program, "./test_1", None ) + # base.load(test_program, "./test_1", None ) program_state = paddle.static.load_program_state( os.path.join(save_dir, "model_1"), var_list=paddle.static.io.get_program_persistable_vars( @@ -1777,7 +1777,7 @@ def test_ptb_rnn_cpu_float32(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -1827,7 +1827,7 @@ def test_pickle_protocol(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -1854,12 +1854,12 @@ def test_pickle_protocol(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: ten = ( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -1868,7 +1868,7 @@ def test_pickle_protocol(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) diff --git a/test/legacy_test/test_static_save_load_bf16.py b/test/legacy_test/test_static_save_load_bf16.py index 10b374689d960..b087ac8dadfc2 100644 --- a/test/legacy_test/test_static_save_load_bf16.py +++ b/test/legacy_test/test_static_save_load_bf16.py @@ -21,8 +21,8 @@ from test_static_save_load import PtbModel import paddle -from paddle import fluid -from paddle.fluid import core, framework +from paddle import base +from paddle.base import core, framework @unittest.skipIf( @@ -36,7 +36,7 @@ def tearDown(self): self.temp_dir.cleanup() def set_place(self): - return fluid.CPUPlace() + return base.CPUPlace() def test_ptb_rnn_cpu_bfloat16(self): seed = 90 @@ -49,8 +49,8 @@ def test_ptb_rnn_cpu_bfloat16(self): batch_num = 100 with new_program_scope(): - fluid.default_startup_program().random_seed = seed - fluid.default_main_program().random_seed = seed + base.default_startup_program().random_seed = seed + base.default_main_program().random_seed = seed ptb_model = PtbModel( "ptb_model", hidden_size=hidden_size, @@ -61,7 +61,7 @@ def test_ptb_rnn_cpu_bfloat16(self): ) place = self.set_place() - exe = fluid.Executor(place) + exe = base.Executor(place) sgd = paddle.optimizer.SGD(learning_rate=1e-3) x = paddle.static.data( name="x", shape=[-1, num_steps], dtype='int64' @@ -110,7 +110,7 @@ def test_ptb_rnn_cpu_bfloat16(self): fetch_list = [static_loss, static_last_hidden, static_last_cell] out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": x_data, "y": y_data, @@ -126,7 +126,7 @@ def test_ptb_rnn_cpu_bfloat16(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -137,11 +137,11 @@ def test_ptb_rnn_cpu_bfloat16(self): # set var to zero for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been set to zero self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -155,7 +155,7 @@ def test_ptb_rnn_cpu_bfloat16(self): for var in main_program.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) diff --git a/test/legacy_test/test_static_save_load_large.py b/test/legacy_test/test_static_save_load_large.py index baf4837737744..201f30039c59b 100644 --- a/test/legacy_test/test_static_save_load_large.py +++ b/test/legacy_test/test_static_save_load_large.py @@ -20,8 +20,8 @@ from test_imperative_base import new_program_scope import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework LARGE_PARAM = 2**26 @@ -47,7 +47,7 @@ def test_large_parameters_static_save(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) # make sure all the paramerter or optimizer var have been update self.assertTrue(np.sum(np.abs(t)) != 0) @@ -62,11 +62,11 @@ def test_large_parameters_static_save(self): # set var to zero for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -75,7 +75,7 @@ def test_large_parameters_static_save(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) @@ -83,11 +83,11 @@ def test_large_parameters_static_save(self): # set var to zero for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: - ten = fluid.global_scope().find_var(var.name).get_tensor() + ten = base.global_scope().find_var(var.name).get_tensor() ten.set(np.zeros_like(np.array(ten)), place) new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) self.assertTrue(np.sum(np.abs(new_t)) == 0) @@ -96,7 +96,7 @@ def test_large_parameters_static_save(self): for var in prog.list_vars(): if isinstance(var, framework.Parameter) or var.persistable: new_t = np.array( - fluid.global_scope().find_var(var.name).get_tensor() + base.global_scope().find_var(var.name).get_tensor() ) base_t = base_map[var.name] np.testing.assert_array_equal(new_t, base_t) diff --git a/test/legacy_test/test_std_layer.py b/test/legacy_test/test_std_layer.py index 7cbf235699bd6..22ef341259142 100644 --- a/test/legacy_test/test_std_layer.py +++ b/test/legacy_test/test_std_layer.py @@ -39,7 +39,7 @@ def setUp(self): self.x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) @@ -122,7 +122,7 @@ def test_error(self): class Testfp16Std(unittest.TestCase): def test_fp16_with_gpu(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_stride.py b/test/legacy_test/test_stride.py index 531575a287230..a80451e36fdc4 100644 --- a/test/legacy_test/test_stride.py +++ b/test/legacy_test/test_stride.py @@ -629,7 +629,7 @@ def test_stride_cpu(self): @unittest.skipIf( - not paddle.fluid.core.is_compiled_with_cuda(), + not paddle.base.core.is_compiled_with_cuda(), "core is not compiled with CUDA", ) class TestStrideGPU(TestStride): diff --git a/test/legacy_test/test_strided_slice_op.py b/test/legacy_test/test_strided_slice_op.py index dde0bbf4e5c25..9aca4ab961aa7 100644 --- a/test/legacy_test/test_strided_slice_op.py +++ b/test/legacy_test/test_strided_slice_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -594,9 +594,9 @@ def test_1(self): out_6 = x[minus_3:3:1, 0:100:2, :, minus_1:2:minus_1] out_7 = x[minus_1, 0:100:2, :, -1:2:-1] - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": input, 'starts': np.array([-3, 0, 2]).astype("int32"), @@ -629,7 +629,7 @@ def test_dygraph_op(self): "Cannot use CUDAPinnedPlace in CPU only version", ) def test_cuda_pinned_place(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = paddle.to_tensor( np.random.randn(2, 10), place=paddle.CUDAPinnedPlace() ) @@ -760,7 +760,7 @@ def create_case(self, net): def test_strided_slice_tensor_array_cuda_pinned_place(self): if paddle.device.is_compiled_with_cuda(): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): class Simple(paddle.nn.Layer): def __init__(self): @@ -971,7 +971,7 @@ def array_slice(self, tensors): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestStridedSliceFloat16(unittest.TestCase): def init_test_case(self): diff --git a/test/legacy_test/test_subtract_op.py b/test/legacy_test/test_subtract_op.py index 10b9e4d0c97bb..a6b85af0f463d 100644 --- a/test/legacy_test/test_subtract_op.py +++ b/test/legacy_test/test_subtract_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class ApiSubtractTest(unittest.TestCase): diff --git a/test/legacy_test/test_sum_op.py b/test/legacy_test/test_sum_op.py index e70ec88176fea..fc37437409063 100644 --- a/test/legacy_test/test_sum_op.py +++ b/test/legacy_test/test_sum_op.py @@ -29,9 +29,9 @@ import paddle import paddle.inference as paddle_infer -from paddle import enable_static, fluid -from paddle.fluid import core -from paddle.fluid.layer_helper import LayerHelper +from paddle import base, enable_static +from paddle.base import core +from paddle.base.layer_helper import LayerHelper def sum_wrapper(X, use_mkldnn=False): @@ -358,7 +358,7 @@ def test_check_grad(self): class API_Test_Add_n(unittest.TestCase): def test_api(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input0 = paddle.tensor.fill_constant( shape=[2, 3], dtype='int64', value=5 ) @@ -368,12 +368,12 @@ def test_api(self): expected_result = np.empty((2, 3)) expected_result.fill(8) sum_value = paddle.add_n([input0, input1]) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) result = exe.run(fetch_list=[sum_value]) self.assertEqual((result == expected_result).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): input0 = paddle.ones(shape=[2, 3], dtype='float32') expected_result = np.empty((2, 3)) expected_result.fill(2) @@ -382,7 +382,7 @@ def test_api(self): self.assertEqual((sum_value.numpy() == expected_result).all(), True) def test_dygraph_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): input0 = paddle.ones(shape=[2, 3], dtype='float32') input1 = paddle.ones(shape=[2, 3], dtype='float32') input0.stop_gradient = False @@ -403,7 +403,7 @@ def test_dygraph_api(self): ) def test_add_n_and_add_and_grad(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([[1, 2, 3], [4, 5, 6]]) np_y = [[7, 8, 9], [10, 11, 12]] np_z = [[1, 1, 1], [1, 1, 1]] @@ -495,12 +495,12 @@ def test_out_dtype(): class TestSumOpError(unittest.TestCase): def test_errors(self): def test_empty_list_input(): - with fluid.dygraph.guard(): - fluid._legacy_C_ops.sum([]) + with base.dygraph.guard(): + base._legacy_C_ops.sum([]) def test_list_of_none_input(): - with fluid.dygraph.guard(): - fluid._legacy_C_ops.sum([None]) + with base.dygraph.guard(): + base._legacy_C_ops.sum([None]) self.assertRaises(Exception, test_empty_list_input) self.assertRaises(Exception, test_list_of_none_input) @@ -645,9 +645,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -687,9 +687,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -718,9 +718,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -749,9 +749,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_svd_op.py b/test/legacy_test/test_svd_op.py index 545a7aa8cf7d0..847a2e7e161cb 100644 --- a/test/legacy_test/test_svd_op.py +++ b/test/legacy_test/test_svd_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestSvdOp(OpTest): @@ -295,27 +295,27 @@ def test_dygraph(self): def test_static(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for place in places: - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): a = np.random.rand(5, 5) x = paddle.static.data( name="input", shape=[5, 5], dtype='float64' ) u, s, vh = paddle.linalg.svd(x) - exe = fluid.Executor(place) + exe = base.Executor(place) gt_u, gt_s, gt_vh = np.linalg.svd(a, full_matrices=False) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": a}, fetch_list=[s], ) np.testing.assert_allclose(fetches[0], gt_s, rtol=1e-05) def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): # The size of input in svd should not be 0. def test_0_size(): array = np.array([], dtype=np.float32) diff --git a/test/legacy_test/test_switch.py b/test/legacy_test/test_switch.py index 428e5537f8ba6..3c90ba5260542 100644 --- a/test/legacy_test/test_switch.py +++ b/test/legacy_test/test_switch.py @@ -15,9 +15,9 @@ import unittest import paddle -from paddle.fluid import core, framework -from paddle.fluid.executor import Executor -from paddle.fluid.framework import default_startup_program +from paddle.base import core, framework +from paddle.base.executor import Executor +from paddle.base.framework import default_startup_program class TestSwitch(unittest.TestCase): diff --git a/test/legacy_test/test_switch_autotune.py b/test/legacy_test/test_switch_autotune.py index 92ca789d8b074..fbd8928c4413c 100644 --- a/test/legacy_test/test_switch_autotune.py +++ b/test/legacy_test/test_switch_autotune.py @@ -85,7 +85,7 @@ def test_autotune(self): self.assertEqual(self.get_flags("FLAGS_use_autotune"), True) def check_status(self, expected_res): - status = paddle.fluid.core.autotune_status() + status = paddle.base.core.autotune_status() for key in status.keys(): v = status[key] if key == "cache_hit_rate": @@ -134,7 +134,7 @@ def run_program(self, enable_autotune): loss = static_program(net, data) place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) exe = paddle.static.Executor(place) @@ -160,7 +160,7 @@ def run_program(self, enable_autotune): for i in range(3): exe.run(program=main_program, feed={'X': x}, fetch_list=[loss]) - status = paddle.fluid.core.autotune_status() + status = paddle.base.core.autotune_status() expected_res = self.get_expected_res(i, enable_autotune) self.check_status(expected_res) paddle.disable_static() diff --git a/test/legacy_test/test_switch_case.py b/test/legacy_test/test_switch_case.py index d16c48540549a..742de7a9a01fa 100644 --- a/test/legacy_test/test_switch_case.py +++ b/test/legacy_test/test_switch_case.py @@ -18,10 +18,10 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.framework import Program, program_guard paddle.enable_static() @@ -85,11 +85,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, fetch_list=[out_0, out_1, out_2, out_3, out_4] @@ -172,11 +172,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, fetch_list=[out_0, out_1, out_2, out_3, out_4] @@ -234,11 +234,11 @@ def test_0d_tensor_backward(self): append_backward(out) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=[out.name, x.grad_name]) np.testing.assert_allclose( @@ -365,11 +365,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) ret = exe.run(main_program, fetch_list=out) np.testing.assert_allclose( @@ -463,11 +463,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, @@ -560,11 +560,11 @@ def fn_3(): ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, diff --git a/test/legacy_test/test_sync_batch_norm_op.py b/test/legacy_test/test_sync_batch_norm_op.py index 5fa9bce916b52..d98aed3531689 100644 --- a/test/legacy_test/test_sync_batch_norm_op.py +++ b/test/legacy_test/test_sync_batch_norm_op.py @@ -33,9 +33,9 @@ ) import paddle -from paddle import fluid, nn -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import in_dygraph_mode +from paddle import base, nn +from paddle.base import Program, core, program_guard +from paddle.base.framework import in_dygraph_mode _set_use_system_allocator(True) @@ -149,13 +149,13 @@ def _build_program( self, place, layout, seed, sync_bn=False, only_forward=False ): """Build program.""" - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() main.random_seed = seed startup.random_seed = seed use_cudnn = (self.dtype == np.float16) or (self.dtype == np.uint16) - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + with base.unique_name.guard(): + with base.program_guard(main, startup): data = paddle.static.data( name='input', shape=self.dshape, @@ -166,14 +166,14 @@ def _build_program( input=data, num_filters=32, filter_size=1, - param_attr=fluid.ParamAttr(name='conv2d_weight'), + param_attr=base.ParamAttr(name='conv2d_weight'), bias_attr=False, use_cudnn=use_cudnn, ) bn = paddle.static.nn.batch_norm( conv, - param_attr=fluid.ParamAttr(name='bn_scale'), - bias_attr=fluid.ParamAttr(name='bn_bias'), + param_attr=base.ParamAttr(name='bn_scale'), + bias_attr=base.ParamAttr(name='bn_bias'), moving_mean_name='bn_moving_mean', moving_variance_name='bn_moving_variance', data_layout=layout, @@ -226,14 +226,14 @@ def _compare_impl(self, place, layout, only_forward): ) np.save(filepath, data[id * stride : (id + 1) * stride]) data = create_or_get_tensor( - scope, "input", OpTest.np_dtype_to_fluid_dtype(data), place + scope, "input", OpTest.np_dtype_to_base_dtype(data), place ) # Single-GPU, N = 32 per GPU main, startup, outs = self._build_program( place, layout, seed, False, only_forward ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) fetch_names = [v.name for v in outs] + [ 'bn_moving_mean', @@ -374,8 +374,8 @@ def test_errors(self): cleanup = enable_static() with program_guard(Program(), Program()): my_sync_batch_norm = paddle.nn.SyncBatchNorm(10) - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CUDAPlace(0) + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.CUDAPlace(0) ) self.assertRaises(TypeError, my_sync_batch_norm, x1) @@ -405,8 +405,8 @@ def test_convert(self): paddle.nn.BatchNorm2D(5), paddle.nn.BatchNorm2D( 5, - weight_attr=fluid.ParamAttr(name='bn.scale'), - bias_attr=fluid.ParamAttr(name='bn.bias'), + weight_attr=base.ParamAttr(name='bn.scale'), + bias_attr=base.ParamAttr(name='bn.bias'), ), ) model = paddle.nn.SyncBatchNorm.convert_sync_batchnorm(model) @@ -451,7 +451,7 @@ def test_errors(self): if not core.is_compiled_with_cuda(): return - with fluid.dygraph.guard(fluid.CUDAPlace(0)): + with base.dygraph.guard(base.CUDAPlace(0)): my_sync_batch_norm = paddle.nn.SyncBatchNorm(10, data_format='CN') data = np.random.random([3, 3, 3]).astype('float32') x = paddle.to_tensor(data) diff --git a/test/legacy_test/test_take.py b/test/legacy_test/test_take.py index 1098dcdbc4f94..2ea39ae96dc90 100644 --- a/test/legacy_test/test_take.py +++ b/test/legacy_test/test_take.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class TestTakeAPI(unittest.TestCase): @@ -44,9 +44,9 @@ def setUp(self): self.set_dtype() self.set_input() self.place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) def test_static_graph(self): @@ -62,9 +62,9 @@ def test_static_graph(self): ) out = paddle.take(x, index, mode=self.mode) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) st_result = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'input': self.input_np, 'index': self.index_np}, fetch_list=out, ) @@ -173,9 +173,9 @@ def setUp(self): self.set_dtype() self.set_input() self.place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) def test_static_index_error(self): @@ -225,9 +225,9 @@ def setUp(self): self.set_dtype() self.set_input() self.place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) diff --git a/test/legacy_test/test_tdm_child_op.py b/test/legacy_test/test_tdm_child_op.py index 3aca9f10c8f78..0bb0e38c6cde6 100644 --- a/test/legacy_test/test_tdm_child_op.py +++ b/test/legacy_test/test_tdm_child_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid +from paddle import base from paddle.incubate.layers.nn import tdm_child @@ -152,14 +152,14 @@ def test_shape(self): x=x, node_nums=26, child_nums=2, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Assign(tree_info_np) ), ) - place = fluid.CPUPlace() - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place=place) + exe.run(base.default_startup_program()) feed = { 'x': np.array( diff --git a/test/legacy_test/test_tdm_sampler_op.py b/test/legacy_test/test_tdm_sampler_op.py index 9abaca34066d0..84d003ca323b0 100644 --- a/test/legacy_test/test_tdm_sampler_op.py +++ b/test/legacy_test/test_tdm_sampler_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.incubate.layers.nn import tdm_sampler @@ -290,10 +290,10 @@ def test_shape(self): neg_samples_num_list, layer_node_num_list, leaf_node_num, - tree_travel_attr=fluid.ParamAttr( + tree_travel_attr=base.ParamAttr( initializer=paddle.nn.initializer.Assign(travel_array) ), - tree_layer_attr=fluid.ParamAttr( + tree_layer_attr=base.ParamAttr( initializer=paddle.nn.initializer.Assign(layer_array) ), output_positive=True, @@ -303,9 +303,9 @@ def test_shape(self): dtype='int32', ) - place = fluid.CPUPlace() - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place=place) + exe.run(base.default_startup_program()) feed = { 'x': np.array( diff --git a/test/legacy_test/test_temporal_shift_op.py b/test/legacy_test/test_temporal_shift_op.py index 4dd5b29fc7014..4c51cbefe7317 100644 --- a/test/legacy_test/test_temporal_shift_op.py +++ b/test/legacy_test/test_temporal_shift_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def temporal_shift(x, seg_num, shift_ratio, data_format): @@ -140,14 +140,14 @@ def test_api(self): ) # dygraph - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): input = paddle.randn([6, 4, 2, 2]) out = paddle.nn.functional.temporal_shift( x=input, seg_num=2, shift_ratio=0.2 ) def test_static_fp16_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() diff --git a/test/legacy_test/test_tensor.py b/test/legacy_test/test_tensor.py index 01e55791cc985..bb30cc98d8fb3 100644 --- a/test/legacy_test/test_tensor.py +++ b/test/legacy_test/test_tensor.py @@ -17,8 +17,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestTensorPtr(unittest.TestCase): @@ -263,7 +263,7 @@ def test_empty_tensor(self): self.assertEqual((0, 1), tensor_array.shape) def run_slice_tensor(self, place, dtype): - tensor = fluid.Tensor() + tensor = base.Tensor() shape = [3, 3, 3] tensor._set_dims(shape) @@ -358,7 +358,7 @@ def test_tensor_poiter(self): self.assertTrue( isinstance(tensor._mutable_data(place, dtype), numbers.Integral) ) - places = fluid.cuda_pinned_places() + places = base.cuda_pinned_places() self.assertTrue( isinstance( tensor._mutable_data(places[0], dtype), numbers.Integral @@ -367,7 +367,7 @@ def test_tensor_poiter(self): def test_tensor_set_fp16(self): array = np.random.random((300, 500)).astype("float16") - tensor = fluid.Tensor() + tensor = base.Tensor() place = core.CPUPlace() tensor.set(array, place) self.assertEqual(tensor._dtype(), core.VarDesc.VarType.FP16) @@ -386,7 +386,7 @@ def test_tensor_set_fp16(self): def test_tensor_set_int16(self): array = np.random.randint(100, size=(300, 500)).astype("int16") - tensor = fluid.Tensor() + tensor = base.Tensor() place = core.CPUPlace() tensor.set(array, place) self.assertEqual(tensor._dtype(), core.VarDesc.VarType.INT16) @@ -406,7 +406,7 @@ def test_tensor_set_int16(self): def test_tensor_set_from_array_list(self): array = np.random.randint(1000, size=(200, 300)) list_array = [array, array] - tensor = fluid.Tensor() + tensor = base.Tensor() place = core.CPUPlace() tensor.set(list_array, place) self.assertEqual([2, 200, 300], tensor.shape()) @@ -443,7 +443,7 @@ def test_tensor_set_item_complex128(self): array = ( np.random.random((100, 100)) + 1j * np.random.random((100, 100)) ).astype(np.complex128) - tensor = fluid.Tensor() + tensor = base.Tensor() place = core.CPUPlace() tensor.set(array, place) @@ -474,7 +474,7 @@ def test_tensor_set_item_complex64(self): array = ( np.random.random((100, 100)) + 1j * np.random.random((100, 100)) ).astype(np.complex64) - tensor = fluid.Tensor() + tensor = base.Tensor() place = core.CPUPlace() tensor.set(array, place) diff --git a/test/legacy_test/test_tensor_array_to_tensor.py b/test/legacy_test/test_tensor_array_to_tensor.py index edca59989b7d5..827e2138fb3f6 100644 --- a/test/legacy_test/test_tensor_array_to_tensor.py +++ b/test/legacy_test/test_tensor_array_to_tensor.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard from paddle.tensor.manipulation import tensor_array_to_tensor paddle.enable_static() @@ -52,7 +52,7 @@ def setUp(self): def test_get_set(self): scope = core.Scope() - program = fluid.Program() + program = base.Program() block = program.global_block() input_arr = block.create_var( @@ -122,7 +122,7 @@ def test_get_set(self): fetch_list.append(block.var('Out')) fetch_list.append(block.var('OutIndex')) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) out = exe.run(program, fetch_list=fetch_list, scope=scope) # print ("index: ", np.array(out[1])) @@ -179,8 +179,8 @@ def setUp(self): var.persistable = True def set_program(self): - self.program = fluid.Program() - with fluid.program_guard(self.program): + self.program = base.Program() + with base.program_guard(self.program): self.array = array = paddle.tensor.create_array(dtype='float32') idx = paddle.tensor.fill_constant(shape=[1], dtype="int64", value=0) for i, x in enumerate(self.inputs): @@ -190,7 +190,7 @@ def set_program(self): input=array, **self.attrs ) loss = paddle.sum(output) - fluid.backward.append_backward(loss) + base.backward.append_backward(loss) self.output_vars = [output] def run_check(self, executor, scope): @@ -208,14 +208,14 @@ def run_check(self, executor, scope): def test_cpu(self): scope = core.Scope() place = core.CPUPlace() - executor = fluid.Executor(place) + executor = base.Executor(place) self.run_check(executor, scope) def test_gpu(self): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) scope = core.Scope() - executor = fluid.Executor(place) + executor = base.Executor(place) self.run_check(executor, scope) @@ -246,18 +246,18 @@ def test_case(self): inp1 = np.random.rand(2, 3, 4).astype("float32") _outs_static = self._test_case(inp0, inp1) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) outs_static = exe.run(fetch_list=list(_outs_static)) - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): outs_dynamic = self._test_case(inp0, inp1) for s, d in zip(outs_static, outs_dynamic): np.testing.assert_array_equal(s, d.numpy()) def test_while_loop_case(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): zero = paddle.tensor.fill_constant( shape=[1], dtype='int64', value=0 ) diff --git a/test/legacy_test/test_tensor_copy_from.py b/test/legacy_test/test_tensor_copy_from.py index 087da70861d42..0258a464043e7 100644 --- a/test/legacy_test/test_tensor_copy_from.py +++ b/test/legacy_test/test_tensor_copy_from.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.core import LoDTensor as Tensor +from paddle.base.core import LoDTensor as Tensor class TestTensorCopyFrom(unittest.TestCase): diff --git a/test/legacy_test/test_tensor_fill_.py b/test/legacy_test/test_tensor_fill_.py index 5008630952a68..ee663ed246b39 100644 --- a/test/legacy_test/test_tensor_fill_.py +++ b/test/legacy_test/test_tensor_fill_.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TensorFill_Test(unittest.TestCase): @@ -26,10 +26,10 @@ def setUp(self): def test_tensor_fill_true(self): typelist = ['float32', 'float64', 'int32', 'int64', 'float16'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - places.append(fluid.CUDAPinnedPlace()) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + places.append(base.CUDAPinnedPlace()) for idx, p in enumerate(places): if idx == 0: @@ -50,10 +50,10 @@ def test_tensor_fill_true(self): def test_tensor_fill_backward(self): typelist = ['float32'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - places.append(fluid.CUDAPinnedPlace()) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + places.append(base.CUDAPinnedPlace()) for idx, p in enumerate(places): if idx == 0: diff --git a/test/legacy_test/test_tensor_fill_diagonal_.py b/test/legacy_test/test_tensor_fill_diagonal_.py index 68ec97331c368..c48337fa06fe9 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_.py +++ b/test/legacy_test/test_tensor_fill_diagonal_.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TensorFillDiagonal_Test(unittest.TestCase): @@ -30,9 +30,9 @@ def test_dim2_normal(self): ) typelist = ['float32', 'float64', 'int32', 'int64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -65,9 +65,9 @@ def test_offset(self): ) typelist = ['float32', 'float64', 'int32', 'int64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -97,9 +97,9 @@ def test_bool(self): ) typelist = ['bool'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -138,9 +138,9 @@ def test_dim2_unnormal_wrap(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -189,9 +189,9 @@ def test_dim2_unnormal_unwrap(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: @@ -232,9 +232,9 @@ def test_dim_larger2_normal(self): ).astype('float32') typelist = ['float32', 'float64', 'int32', 'int64'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) for idx, p in enumerate(places): if idx == 0: diff --git a/test/legacy_test/test_tensor_fill_diagonal_tensor.py b/test/legacy_test/test_tensor_fill_diagonal_tensor.py index c09666eaa7ce1..7409cdae1f007 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_tensor.py +++ b/test/legacy_test/test_tensor_fill_diagonal_tensor.py @@ -18,15 +18,15 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TensorFillDiagTensor_Test(unittest.TestCase): def setUp(self): self.typelist = ['float32', 'float64', 'int32', 'int64'] - self.places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self.places.append(base.CUDAPlace(0)) def test_dim2(self): expected_np = np.array( diff --git a/test/legacy_test/test_tensor_fill_diagonal_tensor_.py b/test/legacy_test/test_tensor_fill_diagonal_tensor_.py index 992e09394891a..482f3e542f6fc 100644 --- a/test/legacy_test/test_tensor_fill_diagonal_tensor_.py +++ b/test/legacy_test/test_tensor_fill_diagonal_tensor_.py @@ -18,15 +18,15 @@ import paddle import paddle.nn.functional as F -from paddle import fluid +from paddle import base class TensorFillDiagTensor_Test(unittest.TestCase): def setUp(self): self.typelist = ['float32', 'float64', 'int32', 'int64'] - self.places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + self.places.append(base.CUDAPlace(0)) def test_dim2(self): expected_np = np.array( diff --git a/test/legacy_test/test_tensor_register_hook.py b/test/legacy_test/test_tensor_register_hook.py index d0fa1fe25974b..29c614713dff3 100644 --- a/test/legacy_test/test_tensor_register_hook.py +++ b/test/legacy_test/test_tensor_register_hook.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid, nn -from paddle.fluid import core +from paddle import base, nn +from paddle.base import core class SimpleNet(nn.Layer): @@ -475,7 +475,7 @@ def double_print_hook(grad): # after changed by hook: 8.0 # TODO(wuweilong): enable this case when DoubleGrad in eager mode is ready - if fluid.in_dygraph_mode(): + if base.in_dygraph_mode(): pass else: z.backward() diff --git a/test/legacy_test/test_tensor_to_list.py b/test/legacy_test/test_tensor_to_list.py index 0bc6162694215..b7c8093ca49d2 100644 --- a/test/legacy_test/test_tensor_to_list.py +++ b/test/legacy_test/test_tensor_to_list.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TensorToListTest(unittest.TestCase): @@ -25,10 +25,10 @@ def setUp(self): self.shape = [11, 25, 32, 43] def test_tensor_tolist(self): - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - places.append(fluid.CUDAPinnedPlace()) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + places.append(base.CUDAPinnedPlace()) for p in places: np_arr = np.reshape( diff --git a/test/legacy_test/test_tensor_to_numpy.py b/test/legacy_test/test_tensor_to_numpy.py index e9eab862e7042..8aa70a9b26ba2 100644 --- a/test/legacy_test/test_tensor_to_numpy.py +++ b/test/legacy_test/test_tensor_to_numpy.py @@ -16,7 +16,7 @@ import numpy as np -from paddle import fluid +from paddle import base class TensorToNumpyTest(unittest.TestCase): @@ -34,10 +34,10 @@ def test_main(self): 'bool', ] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - places.append(fluid.CUDAPinnedPlace()) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + places.append(base.CUDAPinnedPlace()) for p in places: for dtype in dtypes: @@ -46,7 +46,7 @@ def test_main(self): self.shape, ) - t = fluid.LoDTensor() + t = base.LoDTensor() t.set(np_arr, p) ret_np_arr = np.array(t) diff --git a/test/legacy_test/test_tensor_uva.py b/test/legacy_test/test_tensor_uva.py index 7942734473d95..34d7e59609e0b 100644 --- a/test/legacy_test/test_tensor_uva.py +++ b/test/legacy_test/test_tensor_uva.py @@ -17,12 +17,12 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core class TestTensorCopyFrom(unittest.TestCase): def test_main(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CPUPlace() np_value = np.random.random(size=[10, 30]).astype('float32') tensor = paddle.to_tensor(np_value, place=place) @@ -32,7 +32,7 @@ def test_main(self): class TestUVATensorFromNumpy(unittest.TestCase): def test_uva_tensor_creation(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): dtype_list = [ "int32", "int64", @@ -54,7 +54,7 @@ def test_uva_tensor_creation(self): np.testing.assert_allclose(tensor2.numpy(), data, rtol=1e-05) def test_uva_tensor_corectness(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): a = np.arange(0, 100, dtype="int32") a = a.reshape([10, 10]) slice_a = a[:, 5] diff --git a/test/legacy_test/test_tensor_zero_.py b/test/legacy_test/test_tensor_zero_.py index 6e34de1370908..0cfbfc7f1ae67 100644 --- a/test/legacy_test/test_tensor_zero_.py +++ b/test/legacy_test/test_tensor_zero_.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base class TensorFill_Test(unittest.TestCase): @@ -26,10 +26,10 @@ def setUp(self): def test_tensor_fill_true(self): typelist = ['float32', 'float64', 'int32', 'int64', 'float16'] - places = [fluid.CPUPlace()] - if fluid.core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) - places.append(fluid.CUDAPinnedPlace()) + places = [base.CPUPlace()] + if base.core.is_compiled_with_cuda(): + places.append(base.CUDAPlace(0)) + places.append(base.CUDAPinnedPlace()) for p in places: np_arr = np.reshape( diff --git a/test/legacy_test/test_tensordot.py b/test/legacy_test/test_tensordot.py index 4c647a1df17af..16d2015573d10 100644 --- a/test/legacy_test/test_tensordot.py +++ b/test/legacy_test/test_tensordot.py @@ -17,13 +17,13 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(2021) def tensordot_np(x, y, axes): - if isinstance(axes, paddle.fluid.framework.Variable): + if isinstance(axes, paddle.base.framework.Variable): axes = axes.tolist() # np.tensordot does not support empty axes @@ -228,7 +228,7 @@ def test_static(self): def test_fp16_with_gpu(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): for axes in self.all_axes: place = paddle.CUDAPlace(0) with paddle.static.program_guard( diff --git a/test/legacy_test/test_tf32_cublas.py b/test/legacy_test/test_tf32_cublas.py index b78695966ab83..c211bf5b5c053 100644 --- a/test/legacy_test/test_tf32_cublas.py +++ b/test/legacy_test/test_tf32_cublas.py @@ -17,14 +17,14 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestTF32Switch(unittest.TestCase): def test_on_off(self): if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) self.assertTrue(core.get_cublas_switch()) # default core.set_cublas_switch(False) self.assertFalse(core.get_cublas_switch()) # turn off @@ -39,9 +39,9 @@ def test_on_off(self): class TestTF32OnMatmul(unittest.TestCase): def test_dygraph_without_out(self): if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) core.set_cublas_switch(False) # turn off - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_array1 = np.random.rand(4, 12, 64, 88).astype("float32") input_array2 = np.random.rand(4, 12, 88, 512).astype("float32") data1 = paddle.to_tensor(input_array1) diff --git a/test/legacy_test/test_tf32_cudnn.py b/test/legacy_test/test_tf32_cudnn.py index 18f24ab35ad2e..547757c6b9b8b 100644 --- a/test/legacy_test/test_tf32_cudnn.py +++ b/test/legacy_test/test_tf32_cudnn.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core class TestTF32Switch(unittest.TestCase): diff --git a/test/legacy_test/test_tile_op.py b/test/legacy_test/test_tile_op.py index 282a0a52e86e8..e64e7113653ec 100644 --- a/test/legacy_test/test_tile_op.py +++ b/test/legacy_test/test_tile_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard # Situation 1: repeat_times is a list (without tensor) @@ -346,8 +346,8 @@ def test_check_output(self): class TestTileError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) repeat_times = [2, 2] self.assertRaises(TypeError, paddle.tile, x1, repeat_times) @@ -373,7 +373,7 @@ def test_api(self): # Test python API class TestTileAPI(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.random([12, 14]).astype("float32") x = paddle.to_tensor(np_x) @@ -416,9 +416,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -447,9 +447,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_top_k_v2_op.py b/test/legacy_test/test_top_k_v2_op.py index b3fa77086941b..df4cd67b70753 100644 --- a/test/legacy_test/test_top_k_v2_op.py +++ b/test/legacy_test/test_top_k_v2_op.py @@ -22,7 +22,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core def numpy_topk(x, k=1, axis=-1, largest=True): @@ -288,7 +288,7 @@ def setUp(self): self.large_input_data = np.random.rand(2, 1030) def run_dygraph(self, place): - with paddle.fluid.dygraph.guard(place): + with paddle.base.dygraph.guard(place): input_tensor = paddle.to_tensor(self.input_data) large_input_tensor = paddle.to_tensor(self.large_input_data) # test case for basic test case 1 @@ -474,7 +474,7 @@ def test_cases(self): self.run_static(place) def test_errors(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = paddle.to_tensor([1, 2, 3]) with self.assertRaises(BaseException): paddle.topk(x, k=-1) diff --git a/test/legacy_test/test_trace_op.py b/test/legacy_test/test_trace_op.py index f13dd1701affe..7b8f66b4bc35d 100644 --- a/test/legacy_test/test_trace_op.py +++ b/test/legacy_test/test_trace_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid, tensor -from paddle.fluid import core +from paddle import base, tensor +from paddle.base import core class TestTraceOp(OpTest): @@ -154,9 +154,9 @@ def test_case1(self): out2 = tensor.trace(data1, offset=-5, axis1=1, axis2=-1) place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) results = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"data1": case}, fetch_list=[out1, out2], return_numpy=True, diff --git a/test/legacy_test/test_traced_layer_err_msg.py b/test/legacy_test/test_traced_layer_err_msg.py index 7365f29975cb5..29f010aa209d0 100644 --- a/test/legacy_test/test_traced_layer_err_msg.py +++ b/test/legacy_test/test_traced_layer_err_msg.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle import fluid, nn +from paddle import base, nn class SimpleFCLayer(nn.Layer): @@ -60,17 +60,17 @@ def tearDown(self): self.temp_dir.cleanup() def test_trace_err(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return - with fluid.dygraph.guard(): - in_x = fluid.dygraph.to_variable( + with base.dygraph.guard(): + in_x = base.dygraph.to_variable( np.random.random((self.batch_size, self.feature_size)).astype( 'float32' ) ) with self.assertRaises(AssertionError) as e: - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( None, [in_x] ) self.assertEqual( @@ -80,77 +80,77 @@ def test_trace_err(self): str(e.exception), ) with self.assertRaises(TypeError) as e: - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( self.layer, 3 ) self.assertEqual( - "The type of 'each element of inputs' in paddle.jit.TracedLayer.trace must be fluid.Variable, but received <{} 'int'>.".format( + "The type of 'each element of inputs' in paddle.jit.TracedLayer.trace must be base.Variable, but received <{} 'int'>.".format( self.type_str ), str(e.exception), ) with self.assertRaises(TypeError) as e: - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( self.layer, [True, 1] ) self.assertEqual( - "The type of 'each element of inputs' in paddle.jit.TracedLayer.trace must be fluid.Variable, but received <{} 'bool'>.".format( + "The type of 'each element of inputs' in paddle.jit.TracedLayer.trace must be base.Variable, but received <{} 'bool'>.".format( self.type_str ), str(e.exception), ) - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( self.layer, [in_x] ) def test_set_strategy_err(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return - with fluid.dygraph.guard(): - in_x = fluid.dygraph.to_variable( + with base.dygraph.guard(): + in_x = base.dygraph.to_variable( np.random.random((self.batch_size, self.feature_size)).astype( 'float32' ) ) - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( self.layer, [in_x] ) with self.assertRaises(AssertionError) as e: - traced_layer.set_strategy(1, fluid.ExecutionStrategy()) + traced_layer.set_strategy(1, base.ExecutionStrategy()) self.assertEqual( - "The type of 'build_strategy' in paddle.jit.TracedLayer.set_strategy must be fluid.BuildStrategy, but received <{} 'int'>.".format( + "The type of 'build_strategy' in paddle.jit.TracedLayer.set_strategy must be base.BuildStrategy, but received <{} 'int'>.".format( self.type_str ), str(e.exception), ) with self.assertRaises(AssertionError) as e: - traced_layer.set_strategy(fluid.BuildStrategy(), False) + traced_layer.set_strategy(base.BuildStrategy(), False) self.assertEqual( - "The type of 'exec_strategy' in paddle.jit.TracedLayer.set_strategy must be fluid.ExecutionStrategy, but received <{} 'bool'>.".format( + "The type of 'exec_strategy' in paddle.jit.TracedLayer.set_strategy must be base.ExecutionStrategy, but received <{} 'bool'>.".format( self.type_str ), str(e.exception), ) - traced_layer.set_strategy(build_strategy=fluid.BuildStrategy()) - traced_layer.set_strategy(exec_strategy=fluid.ExecutionStrategy()) + traced_layer.set_strategy(build_strategy=base.BuildStrategy()) + traced_layer.set_strategy(exec_strategy=base.ExecutionStrategy()) traced_layer.set_strategy( - fluid.BuildStrategy(), fluid.ExecutionStrategy() + base.BuildStrategy(), base.ExecutionStrategy() ) def test_save_inference_model_err(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return - with fluid.dygraph.guard(): - in_x = fluid.dygraph.to_variable( + with base.dygraph.guard(): + in_x = base.dygraph.to_variable( np.random.random((self.batch_size, self.feature_size)).astype( 'float32' ) ) - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( self.layer, [in_x] ) @@ -207,7 +207,7 @@ def test_save_inference_model_err(self): def _train_simple_net(self): layer = None - with fluid.dygraph.guard(): + with base.dygraph.guard(): layer = SimpleFCLayer( self.feature_size, self.batch_size, self.fc_size ) @@ -216,7 +216,7 @@ def _train_simple_net(self): ) for i in range(5): - in_x = fluid.dygraph.to_variable( + in_x = base.dygraph.to_variable( np.random.random( (self.batch_size, self.feature_size) ).astype('float32') @@ -230,12 +230,12 @@ def _train_simple_net(self): class TestOutVarWithNoneErrMsg(unittest.TestCase): def test_linear_net_with_none(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return model = LinearNetWithNone(100, 16) in_x = paddle.to_tensor(np.random.random((4, 100)).astype('float32')) with self.assertRaises(TypeError): - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( model, [in_x] ) @@ -255,12 +255,12 @@ def tearDown(self): self.temp_dir.cleanup() def test_mkdir_when_input_path_non_exist(self): - if fluid.framework.in_dygraph_mode(): + if base.framework.in_dygraph_mode(): return fc_layer = SimpleFCLayer(3, 4, 2) input_var = paddle.to_tensor(np.random.random([4, 3]).astype('float32')) - with fluid.dygraph.guard(): - dygraph_out, traced_layer = fluid.dygraph.TracedLayer.trace( + with base.dygraph.guard(): + dygraph_out, traced_layer = base.dygraph.TracedLayer.trace( fc_layer, inputs=[input_var] ) self.assertFalse(os.path.exists(os.path.dirname(self.save_path))) diff --git a/test/legacy_test/test_trainable.py b/test/legacy_test/test_trainable.py index 07501923885a9..e6703637212c3 100644 --- a/test/legacy_test/test_trainable.py +++ b/test/legacy_test/test_trainable.py @@ -18,7 +18,7 @@ from simple_nets import init_data import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -27,7 +27,7 @@ def test_trainable(): x = paddle.static.data(name='image', shape=[-1, 784], dtype='float32') label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') feature = paddle.static.nn.fc( - x, size=10, weight_attr=fluid.ParamAttr(trainable=False) + x, size=10, weight_attr=base.ParamAttr(trainable=False) ) loss = paddle.nn.functional.cross_entropy( input=feature, label=label, reduction='none', use_softmax=False @@ -40,13 +40,13 @@ class TestTrainable(unittest.TestCase): def check_trainable( self, model, feed_dict, op_count, optimizer=paddle.optimizer.Adam() ): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) - main = fluid.Program() - startup = fluid.Program() + main = base.Program() + startup = base.Program() - with fluid.program_guard(main, startup): + with base.program_guard(main, startup): loss = model() optimizer.minimize(loss) @@ -58,7 +58,7 @@ def check_trainable( else: assert ops[op] == op_count[op] - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) exe.run(feed=feed_dict) def test_trainable(self): diff --git a/test/legacy_test/test_trainer_desc.py b/test/legacy_test/test_trainer_desc.py index 1e5ebd2977055..787015116744a 100644 --- a/test/legacy_test/test_trainer_desc.py +++ b/test/legacy_test/test_trainer_desc.py @@ -18,7 +18,7 @@ import unittest -from paddle import fluid +from paddle import base class TestTrainerDesc(unittest.TestCase): @@ -28,7 +28,7 @@ def test_config(self): """ Testcase for python config. """ - trainer_desc = fluid.trainer_desc.TrainerDesc() + trainer_desc = base.trainer_desc.TrainerDesc() trainer_desc._set_dump_fields(["a", "b"]) trainer_desc._set_mpi_rank(1) trainer_desc._set_dump_fields_path("path") @@ -46,7 +46,7 @@ def test_config_dump_simple(self): """ Testcase for dump_in_simple_mode """ - trainer_desc = fluid.trainer_desc.TrainerDesc() + trainer_desc = base.trainer_desc.TrainerDesc() trainer_desc._set_dump_fields(["a", "b"]) trainer_desc._set_is_dump_in_simple_mode(True) diff --git a/test/legacy_test/test_trans_layout_op.py b/test/legacy_test/test_trans_layout_op.py index e6e1967d75e00..3617cdb013b8a 100644 --- a/test/legacy_test/test_trans_layout_op.py +++ b/test/legacy_test/test_trans_layout_op.py @@ -44,11 +44,11 @@ def test_check_output(self): class LayoutAutoTune(unittest.TestCase): def test_config(self): - paddle.fluid.core.enable_layout_autotune() + paddle.base.core.enable_layout_autotune() if self.use_autoune(): - self.assertEqual(paddle.fluid.core.use_layout_autotune(), True) - paddle.fluid.core.disable_layout_autotune() - self.assertEqual(paddle.fluid.core.use_layout_autotune(), False) + self.assertEqual(paddle.base.core.use_layout_autotune(), True) + paddle.base.core.disable_layout_autotune() + self.assertEqual(paddle.base.core.use_layout_autotune(), False) self.use_autoune() def setUp(self): @@ -60,7 +60,7 @@ def use_autoune(self): paddle.incubate.autotune.set_config( config={"layout": {"enable": True}} ) - return paddle.fluid.core.use_layout_autotune() + return paddle.base.core.use_layout_autotune() else: config = {"layout": {"enable": False}} tfile = tempfile.NamedTemporaryFile(mode="w+", delete=False) @@ -68,7 +68,7 @@ def use_autoune(self): tfile.close() paddle.incubate.autotune.set_config(tfile.name) os.remove(tfile.name) - return paddle.fluid.core.use_layout_autotune() + return paddle.base.core.use_layout_autotune() def test_flatten_op_transposer(self): conv = paddle.nn.Conv2D(3, 8, (3, 3)) diff --git a/test/legacy_test/test_transfer_dtype_op.py b/test/legacy_test/test_transfer_dtype_op.py index d9d4310b0a516..0a087b5d4f205 100644 --- a/test/legacy_test/test_transfer_dtype_op.py +++ b/test/legacy_test/test_transfer_dtype_op.py @@ -22,7 +22,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core class TestTransferDtypeOpFp32ToFp64(OpTest): diff --git a/test/legacy_test/test_transfer_layout_op.py b/test/legacy_test/test_transfer_layout_op.py index 6dff0622c1b10..ffcd979b0ba06 100644 --- a/test/legacy_test/test_transfer_layout_op.py +++ b/test/legacy_test/test_transfer_layout_op.py @@ -18,10 +18,10 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base import core +from paddle.base.framework import Program, program_guard +from paddle.base.layer_helper import LayerHelper def transpose_layout(x, src_layout, dst_layout): @@ -75,11 +75,11 @@ def test_layout_transfer(self): z = softmax_with_data_format(y, data_format='NHWC') place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup_program) ret = exe.run( main_program, diff --git a/test/legacy_test/test_transformer_api.py b/test/legacy_test/test_transformer_api.py index d313c513595ba..a4297155de0e9 100644 --- a/test/legacy_test/test_transformer_api.py +++ b/test/legacy_test/test_transformer_api.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.nn.layer.transformer import ( MultiHeadAttention, Transformer, @@ -187,7 +187,7 @@ def scaled_dot_product_attention(q, k, v, d_key, attn_mask, multi_head_attn): def cal_qkv(key, value, num_heads, embed_dim, multi_head_attn): - with fluid.dygraph.guard(): + with base.dygraph.guard(): head_dim = embed_dim // num_heads k_weight = multi_head_attn.k_proj.weight.numpy() v_weight = multi_head_attn.v_proj.weight.numpy() @@ -226,8 +226,8 @@ def prepare_qkv( def add(x, y=None): - fluid.enable_dygraph() - with fluid.dygraph.guard(): + base.enable_dygraph() + with base.dygraph.guard(): x = x.numpy() if not isinstance(x, np.ndarray) else x if y is not None: x += y @@ -241,8 +241,8 @@ def relu(x): def layer_norm(x, normalized_shape, norm, epsilon=1e-05, act=None): - fluid.enable_dygraph() - with fluid.dygraph.guard(): + base.enable_dygraph() + with base.dygraph.guard(): # scale: weight = norm.weight.numpy() # shift: @@ -265,8 +265,8 @@ def layer_norm(x, normalized_shape, norm, epsilon=1e-05, act=None): def ffn(src, encoder_layer, ffn_fc1_act="relu"): assert ffn_fc1_act == "relu", "only relu is supported" - fluid.enable_dygraph() - with fluid.dygraph.guard(): + base.enable_dygraph() + with base.dygraph.guard(): src = src.numpy() if not isinstance(src, np.ndarray) else src w1 = encoder_layer.linear1.weight.numpy() w2 = encoder_layer.linear2.weight.numpy() @@ -284,7 +284,7 @@ def multihead_attention_test_helper(self_attention, cache): paddle.seed(2020) paddle.framework.random._manual_program_seed(2020) # self_attention|cross_attention, cache|No cache - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): # generate params for multi_head_attention ( batch_size, @@ -398,7 +398,7 @@ def multihead_attention_test_helper(self_attention, cache): multihead_attention_test_helper(False, False) def test_transformer_encoder_layer(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): paddle.framework.seed(2020) paddle.framework.random._manual_program_seed(2020) @@ -463,7 +463,7 @@ def test_transformer_encoder_layer(self): ) def test_transformer_encoder_layer_attr_1(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): paddle.framework.seed(2020) paddle.framework.random._manual_program_seed(2020) @@ -544,7 +544,7 @@ def test_transformer_encoder_layer_attr_1(self): ) def test_transformer_decoder_layer(self): - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): paddle.framework.seed(2020) activation = "relu" normalize_before = False @@ -679,7 +679,7 @@ def test_encoder(self): (batch_size, n_head, sequence_length, sequence_length) ).astype("float32") src_mask[0][0][0][0] = -np.inf - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): encoder_layer = TransformerEncoderLayer( d_model, n_head, dim_feedforward, dropout ) @@ -710,7 +710,7 @@ def test_encoder_attr_1(self): (batch_size, n_head, sequence_length, sequence_length) ).astype("float32") src_mask[0][0][0][0] = -np.inf - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): for cache in [True, False]: # paddle encoder_layer = TransformerEncoderLayer( @@ -755,7 +755,7 @@ def test_decoder(self): (batch_size, n_head, target_length, source_length) ).astype("float32") memory_mask[0][0][0][0] = -1e9 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): decoder_layer = TransformerDecoderLayer( d_model, n_head, dim_feedforward, dropout ) @@ -783,7 +783,7 @@ def test_transformer(self): ) = generate_basic_params(mode="decoder_layer") # batch_size, source_length, target_length, d_model, n_head = 4, 8, 8, 64, 8 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): transformer = Transformer( d_model, n_head, @@ -834,7 +834,7 @@ def test_transformer_attr_1(self): ) = generate_basic_params(mode="decoder_layer") # batch_size, source_length, target_length, d_model, n_head = 4, 8, 8, 64, 8 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): transformer = Transformer( d_model, n_head, @@ -887,7 +887,7 @@ def test_transformer_attr_2(self): ) = generate_basic_params(mode="decoder_layer") # batch_size, source_length, target_length, d_model, n_head = 4, 8, 8, 64, 8 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): transformer = Transformer( d_model, n_head, @@ -940,7 +940,7 @@ def test_transformer_attr_3(self): ) = generate_basic_params(mode="decoder_layer") # batch_size, source_length, target_length, d_model, n_head = 4, 8, 8, 64, 8 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): transformer = Transformer( d_model, n_head, @@ -993,7 +993,7 @@ def test_transformer_attr_boolean(self): ) = generate_basic_params(mode="decoder_layer") # batch_size, source_length, target_length, d_model, n_head = 4, 8, 8, 64, 8 - with fluid.dygraph.guard(fluid.CPUPlace()): + with base.dygraph.guard(base.CPUPlace()): transformer = Transformer( d_model, n_head, diff --git a/test/legacy_test/test_transpose_op.py b/test/legacy_test/test_transpose_op.py index 5f4ba4fb188de..adb0ca0779f87 100644 --- a/test/legacy_test/test_transpose_op.py +++ b/test/legacy_test/test_transpose_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard paddle.enable_static() @@ -194,9 +194,9 @@ def setUp(self): } def initTestCase(self): - fluid.core.set_autotune_range(0, 3) - fluid.core.update_autotune_status() - fluid.core.enable_autotune() + base.core.set_autotune_range(0, 3) + base.core.update_autotune_status() + base.core.enable_autotune() self.shape = (1, 12, 256, 1) self.axis = (0, 3, 2, 1) @@ -206,7 +206,7 @@ def init_op_type(self): def test_check_output(self): self.check_output(no_check_set=['XShape']) - fluid.core.disable_autotune() + base.core.disable_autotune() def test_check_grad(self): self.check_grad(['X'], 'Out', check_prim=True) @@ -231,9 +231,9 @@ def setUp(self): } def initTestCase(self): - fluid.core.set_autotune_range(0, 3) - fluid.core.update_autotune_status() - fluid.core.enable_autotune() + base.core.set_autotune_range(0, 3) + base.core.update_autotune_status() + base.core.enable_autotune() self.shape = (1, 12, 256, 1) self.axis = (0, 3, 2, 1) @@ -243,7 +243,7 @@ def init_op_type(self): def test_check_output(self): self.check_output(no_check_set=['XShape']) - fluid.core.disable_autotune() + base.core.disable_autotune() def test_check_grad(self): self.check_grad(['X'], 'Out', check_prim=True) @@ -275,9 +275,9 @@ def if_enable_cinn(self): self.enable_cinn = False def initTestCase(self): - fluid.core.set_autotune_range(0, 3) - fluid.core.update_autotune_status() - fluid.core.enable_autotune() + base.core.set_autotune_range(0, 3) + base.core.update_autotune_status() + base.core.enable_autotune() self.shape = (2, 8, 10) self.axis = (0, 2, 1) @@ -287,7 +287,7 @@ def init_op_type(self): def test_check_output(self): self.check_output(no_check_set=['XShape']) - fluid.core.disable_autotune() + base.core.disable_autotune() def test_check_grad(self): self.check_grad(['X'], 'Out', check_prim=True) @@ -549,66 +549,66 @@ def test_dygraph_out(self): class TestTAPI(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data = paddle.static.data(shape=[10], dtype="float64", name="data") data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) data_np = np.random.random([10]).astype("float64") (result,) = exe.run(feed={"data": data_np}, fetch_list=[data_t]) expected_result = np.transpose(data_np) self.assertEqual((result == expected_result).all(), True) - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data = paddle.static.data( shape=[10, 5], dtype="float64", name="data" ) data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) data_np = np.random.random([10, 5]).astype("float64") (result,) = exe.run(feed={"data": data_np}, fetch_list=[data_t]) expected_result = np.transpose(data_np) self.assertEqual((result == expected_result).all(), True) - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): data = paddle.static.data( shape=[1, 5], dtype="float64", name="data" ) data_t = paddle.t(data) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) data_np = np.random.random([1, 5]).astype("float64") (result,) = exe.run(feed={"data": data_np}, fetch_list=[data_t]) expected_result = np.transpose(data_np) self.assertEqual((result == expected_result).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.random([10]).astype("float64") - data = fluid.dygraph.to_variable(np_x) + data = base.dygraph.to_variable(np_x) z = paddle.t(data) np_z = z.numpy() z_expected = np.array(np.transpose(np_x)) self.assertEqual((np_z == z_expected).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.random([10, 5]).astype("float64") - data = fluid.dygraph.to_variable(np_x) + data = base.dygraph.to_variable(np_x) z = paddle.t(data) np_z = z.numpy() z_expected = np.array(np.transpose(np_x)) self.assertEqual((np_z == z_expected).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.random([1, 5]).astype("float64") - data = fluid.dygraph.to_variable(np_x) + data = base.dygraph.to_variable(np_x) z = paddle.t(data) np_z = z.numpy() z_expected = np.array(np.transpose(np_x)) self.assertEqual((np_z == z_expected).all(), True) def test_errors(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name='x', shape=[10, 5, 3], dtype='float64') def test_x_dimension_check(): @@ -622,7 +622,7 @@ def test_moveaxis1(self): x_np = np.random.randn(2, 3, 4, 5, 7) expected = np.moveaxis(x_np, [0, 4, 3, 2], [1, 3, 2, 0]) paddle.enable_static() - with paddle.static.program_guard(fluid.Program()): + with paddle.static.program_guard(base.Program()): x = paddle.static.data("x", shape=[2, 3, 4, 5, 7], dtype='float64') out = paddle.moveaxis(x, [0, 4, 3, 2], [1, 3, 2, 0]) @@ -642,7 +642,7 @@ def test_moveaxis2(self): x_np = np.random.randn(2, 3, 5) expected = np.moveaxis(x_np, -2, -1) paddle.enable_static() - with paddle.static.program_guard(fluid.Program()): + with paddle.static.program_guard(base.Program()): x = paddle.static.data("x", shape=[2, 3, 5], dtype='float64') out = x.moveaxis(-2, -1) @@ -722,9 +722,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -753,9 +753,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_trapezoid.py b/test/legacy_test/test_trapezoid.py index de18e75512717..d81590e5a50b3 100644 --- a/test/legacy_test/test_trapezoid.py +++ b/test/legacy_test/test_trapezoid.py @@ -228,7 +228,7 @@ def set_api(self): def test_fp16_with_gpu(self): paddle.enable_static() - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -253,7 +253,7 @@ def test_fp16_with_gpu(self): ) def test_fp16_func_dygraph(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) paddle.disable_static() input_y = np.random.random([4, 4]) diff --git a/test/legacy_test/test_triangular_solve_op.py b/test/legacy_test/test_triangular_solve_op.py index 23cb650b14bbe..60f699d88822d 100644 --- a/test/legacy_test/test_triangular_solve_op.py +++ b/test/legacy_test/test_triangular_solve_op.py @@ -21,8 +21,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard paddle.enable_static() @@ -257,7 +257,7 @@ def setUp(self): self.place.append(paddle.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): x = paddle.static.data(name="x", shape=[3, 3], dtype=self.dtype) y = paddle.static.data(name="y", shape=[3, 2], dtype=self.dtype) z = paddle.linalg.triangular_solve(x, y) @@ -266,9 +266,9 @@ def check_static_result(self, place): y_np = np.random.random([3, 2]).astype(self.dtype) z_np = np.linalg.solve(np.triu(x_np), y_np) - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "y": y_np}, fetch_list=[z], ) @@ -301,11 +301,11 @@ class TestTriangularSolveOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # The input type of solve_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) - y1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.CPUPlace() + y1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.CPUPlace() ) self.assertRaises(TypeError, paddle.linalg.triangular_solve, x1, y1) diff --git a/test/legacy_test/test_tril_indices_op.py b/test/legacy_test/test_tril_indices_op.py index db29ecb8acfb0..26d19e44c2887 100644 --- a/test/legacy_test/test_tril_indices_op.py +++ b/test/legacy_test/test_tril_indices_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base class TestTrilIndicesOp(OpTest): @@ -60,8 +60,8 @@ def init_config(self): class TestTrilIndicesAPICaseStatic(unittest.TestCase): def test_static(self): places = ( - [paddle.CPUPlace(), paddle.fluid.CUDAPlace(0)] - if fluid.core.is_compiled_with_cuda() + [paddle.CPUPlace(), paddle.base.CUDAPlace(0)] + if base.core.is_compiled_with_cuda() else [paddle.CPUPlace()] ) paddle.enable_static() @@ -79,12 +79,12 @@ def test_static(self): class TestTrilIndicesAPICaseDygraph(unittest.TestCase): def test_dygraph(self): places = ( - [paddle.CPUPlace(), paddle.fluid.CUDAPlace(0)] - if fluid.core.is_compiled_with_cuda() + [paddle.CPUPlace(), paddle.base.CUDAPlace(0)] + if base.core.is_compiled_with_cuda() else [paddle.CPUPlace()] ) for place in places: - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): out1 = paddle.tril_indices(4, 4, 2) expected_result1 = np.tril_indices(4, 2, 4) self.assertEqual((out1.numpy() == expected_result1).all(), True) @@ -120,7 +120,7 @@ def test_default_CPU(self): expected_result = np.tril_indices(4, 2) np.testing.assert_allclose(result, expected_result, rtol=1e-05) - with fluid.dygraph.base.guard(paddle.CPUPlace()): + with base.dygraph.base.guard(paddle.CPUPlace()): out = paddle.tril_indices(4, None, 2) expected_result = np.tril_indices(4, 2) self.assertEqual((out.numpy() == expected_result).all(), True) diff --git a/test/legacy_test/test_tril_triu_op.py b/test/legacy_test/test_tril_triu_op.py index c2b80f1c4a9cf..c0304354a1bd5 100644 --- a/test/legacy_test/test_tril_triu_op.py +++ b/test/legacy_test/test_tril_triu_op.py @@ -17,9 +17,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid, tensor -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle import base, tensor +from paddle.base import core +from paddle.base.framework import Program, program_guard class TrilTriuOpDefaultTest(OpTest): @@ -215,13 +215,13 @@ def test_api(self): tril_out, triu_out = tensor.tril(x), tensor.triu(x) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) tril_out, triu_out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": data}, fetch_list=[tril_out, triu_out], ) @@ -233,9 +233,9 @@ def test_api_with_dygraph(self): dtypes = ['float16', 'float32'] for dtype in dtypes: - with fluid.dygraph.guard(): + with base.dygraph.guard(): data = np.random.random([1, 9, 9, 4]).astype(dtype) - x = fluid.dygraph.to_variable(data) + x = base.dygraph.to_variable(data) tril_out, triu_out = ( tensor.tril(x).numpy(), tensor.triu(x).numpy(), @@ -243,7 +243,7 @@ def test_api_with_dygraph(self): np.testing.assert_allclose(tril_out, np.tril(data), rtol=1e-05) np.testing.assert_allclose(triu_out, np.triu(data), rtol=1e-05) - def test_fluid_api(self): + def test_base_api(self): paddle.enable_static() dtypes = ['float16', 'float32'] @@ -258,13 +258,13 @@ def test_fluid_api(self): triu_out = paddle.triu(x) place = ( - fluid.CUDAPlace(0) - if fluid.core.is_compiled_with_cuda() - else fluid.CPUPlace() + base.CUDAPlace(0) + if base.core.is_compiled_with_cuda() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) triu_out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": data}, fetch_list=[triu_out], ) diff --git a/test/legacy_test/test_trilinear_interp_op.py b/test/legacy_test/test_trilinear_interp_op.py index 031639fc080aa..bbdc14e434cfb 100755 --- a/test/legacy_test/test_trilinear_interp_op.py +++ b/test/legacy_test/test_trilinear_interp_op.py @@ -17,7 +17,7 @@ import numpy as np from eager_op_test import OpTest -from paddle.fluid import core +from paddle.base import core def trilinear_interp_np( diff --git a/test/legacy_test/test_trilinear_interp_v2_op.py b/test/legacy_test/test_trilinear_interp_v2_op.py index 43d4a9587f7fd..68101d5c5e0f7 100755 --- a/test/legacy_test/test_trilinear_interp_v2_op.py +++ b/test/legacy_test/test_trilinear_interp_v2_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.nn.functional import interpolate np.random.seed(123) @@ -964,7 +964,7 @@ def init_test_case(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestTrilinearInterpOpForFloat16(unittest.TestCase): def init_test_case(self): @@ -1007,7 +1007,7 @@ def test_main(self): @unittest.skipIf( - not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" ) class TestTrilinearInterpDatalayoutForFloat16(TestTrilinearInterpOpForFloat16): def init_test_case(self): diff --git a/test/legacy_test/test_triu_indices_op.py b/test/legacy_test/test_triu_indices_op.py index b560cec7dbae5..4f5c7610ddd7e 100644 --- a/test/legacy_test/test_triu_indices_op.py +++ b/test/legacy_test/test_triu_indices_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle import fluid +from paddle import base class TestTriuIndicesOp(OpTest): @@ -59,8 +59,8 @@ def init_config(self): class TestTriuIndicesAPICaseStatic(unittest.TestCase): def test_static(self): - if fluid.core.is_compiled_with_cuda(): - place = paddle.fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = paddle.base.CUDAPlace(0) else: place = paddle.CPUPlace() with paddle.static.program_guard( @@ -75,11 +75,11 @@ def test_static(self): class TestTriuIndicesAPICaseDygraph(unittest.TestCase): def test_dygraph(self): - if fluid.core.is_compiled_with_cuda(): - place = paddle.fluid.CUDAPlace(0) + if base.core.is_compiled_with_cuda(): + place = paddle.base.CUDAPlace(0) else: place = paddle.CPUPlace() - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): out = paddle.triu_indices(4, 4, 2) expected_result = np.triu_indices(4, 2, 4) np.testing.assert_array_equal(out, expected_result) @@ -115,7 +115,7 @@ def test_default_CPU(self): expected_result = np.triu_indices(4, 2) np.testing.assert_array_equal(result[0], expected_result) - with fluid.dygraph.base.guard(paddle.CPUPlace()): + with base.dygraph.base.guard(paddle.CPUPlace()): out = paddle.triu_indices(4, None, 2) expected_result = np.triu_indices(4, 2) np.testing.assert_array_equal(out, expected_result) diff --git a/test/legacy_test/test_trunc_op.py b/test/legacy_test/test_trunc_op.py index 404403fca0453..d1dae7c8f4081 100644 --- a/test/legacy_test/test_trunc_op.py +++ b/test/legacy_test/test_trunc_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/legacy_test/test_truncated_gaussian_random_op.py b/test/legacy_test/test_truncated_gaussian_random_op.py index 9380d4f81ea36..0f56c5f9ef15e 100644 --- a/test/legacy_test/test_truncated_gaussian_random_op.py +++ b/test/legacy_test/test_truncated_gaussian_random_op.py @@ -17,9 +17,9 @@ import numpy import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import core +from paddle.base.executor import Executor class TestTrunctedGaussianRandomOp(unittest.TestCase): @@ -35,16 +35,16 @@ def setUp(self): self.outputs = ["Out"] def test_cpu(self): - self.gaussian_random_test(place=fluid.CPUPlace()) - self.gaussian_random_test_eager(place=fluid.CPUPlace()) + self.gaussian_random_test(place=base.CPUPlace()) + self.gaussian_random_test_eager(place=base.CPUPlace()) def test_gpu(self): if core.is_compiled_with_cuda(): - self.gaussian_random_test(place=fluid.CUDAPlace(0)) - self.gaussian_random_test_eager(place=fluid.CUDAPlace(0)) + self.gaussian_random_test(place=base.CUDAPlace(0)) + self.gaussian_random_test_eager(place=base.CUDAPlace(0)) def gaussian_random_test(self, place): - program = fluid.Program() + program = base.Program() block = program.global_block() vout = block.create_var(name="Out") op = block.append_op( @@ -67,7 +67,7 @@ def gaussian_random_test(self, place): # TruncatedNormal.__call__ has no return value, so here call _C_ops api # directly def gaussian_random_test_eager(self, place): - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): out = paddle._C_ops.truncated_gaussian_random( self.attrs["shape"], self.attrs["mean"], diff --git a/test/legacy_test/test_unbind_op.py b/test/legacy_test/test_unbind_op.py index 670433a84c8b5..45a4d92fef019 100644 --- a/test/legacy_test/test_unbind_op.py +++ b/test/legacy_test/test_unbind_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid, tensor -from paddle.fluid import Program, program_guard +from paddle import base, tensor +from paddle.base import Program, program_guard class TestUnbind(unittest.TestCase): @@ -30,10 +30,10 @@ def test_unbind(self): [out_0, out_1] = tensor.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype("float32") axis = paddle.static.data(shape=[], dtype='int32', name='axis') - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) [res_1, res_2] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_1, "axis": 0}, fetch_list=[out_0, out_1], ) @@ -42,7 +42,7 @@ def test_unbind(self): np.testing.assert_array_equal(res_2, input_1[1, 0:100]) def test_unbind_static_fp16_gpu(self): - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): place = paddle.CUDAPlace(0) with paddle.static.program_guard( paddle.static.Program(), paddle.static.Program() @@ -65,7 +65,7 @@ def test_unbind_static_fp16_gpu(self): np.testing.assert_array_equal(res[1], input[1, :]) def test_unbind_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.random([2, 3]).astype("float32") x = paddle.to_tensor(np_x) x.stop_gradient = False @@ -88,10 +88,10 @@ def test_layers_unbind(self): [out_0, out_1] = paddle.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype("float32") axis = paddle.static.data(shape=[], dtype='int32', name='axis') - exe = fluid.Executor(place=fluid.CPUPlace()) + exe = base.Executor(place=base.CPUPlace()) [res_1, res_2] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_1, "axis": 0}, fetch_list=[out_0, out_1], ) diff --git a/test/legacy_test/test_unfold_op.py b/test/legacy_test/test_unfold_op.py index 2ed5bf3f82c85..9e9c0806708de 100644 --- a/test/legacy_test/test_unfold_op.py +++ b/test/legacy_test/test_unfold_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestUnfoldOp(OpTest): @@ -203,14 +203,14 @@ def setUp(self): self.op_type = 'unfold' self.python_api = paddle.nn.functional.unfold self.set_data() - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): - input = fluid.dygraph.to_variable(self.inputs['X']) + with base.dygraph.guard(place): + input = base.dygraph.to_variable(self.inputs['X']) m = paddle.nn.Unfold(**self.attrs) m.eval() result = m(input) diff --git a/test/legacy_test/test_uniform_random_bf16_op.py b/test/legacy_test/test_uniform_random_bf16_op.py index 426013daa2013..2e92c2041107b 100644 --- a/test/legacy_test/test_uniform_random_bf16_op.py +++ b/test/legacy_test/test_uniform_random_bf16_op.py @@ -20,8 +20,8 @@ from test_uniform_random_op import output_hist, output_hist_diag import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.tensor import random @@ -163,17 +163,17 @@ class TestUniformRandomOpAPISeed(unittest.TestCase): def test_attr_tensor_API(self): _seed = 10 gen = paddle.seed(_seed) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): _min = 5 _max = 10 ret = paddle.uniform([2, 3, 2], min=_min, max=_max, seed=_seed) ret_2 = paddle.uniform([2, 3, 2], min=_min, max=_max, seed=_seed) res = paddle.equal(ret, ret_2) - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) ret_value, cmp_value = exe.run(train_program, fetch_list=[ret, res]) @@ -237,9 +237,9 @@ def test_check_output(self): class TestUniformRandomBatchSizeLikeOpBF16API(unittest.TestCase): def test_attr_tensorlist_int32_API(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): input = paddle.static.data( name="input", shape=[1, 3], dtype='uint16' ) @@ -247,8 +247,8 @@ def test_attr_tensorlist_int32_API(self): input, [2, 4], dtype=np.uint16 ) # out_1.shape=[1, 4] - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) exe.run(startup_program) outs = exe.run(train_program, fetch_list=[out_1]) diff --git a/test/legacy_test/test_uniform_random_inplace_op.py b/test/legacy_test/test_uniform_random_inplace_op.py index c4a243f47897b..f022656d1ada6 100644 --- a/test/legacy_test/test_uniform_random_inplace_op.py +++ b/test/legacy_test/test_uniform_random_inplace_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_uint16_to_float import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def output_hist(out): @@ -46,7 +46,7 @@ def test_fp64(): self.assertEqual(tensor_fp64.dtype, paddle.float64) places = ['cpu'] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): places.append('gpu') for place in places: paddle.set_device(place) @@ -219,7 +219,7 @@ def test_attr_error(): class TestUniformRandomInplaceOpEmptyTensor(unittest.TestCase): def test_uniform_random_inplace_op_empty_tensor(self): places = ['cpu'] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): places.append('gpu') test_shapes = [(200, 0), (0, 200)] for place in places: @@ -249,7 +249,7 @@ def test_grad(): self.assertTrue((uniform_grad == 0).all()) places = ['cpu'] - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): places.append('gpu') for place in places: paddle.set_device(place) diff --git a/test/legacy_test/test_uniform_random_op.py b/test/legacy_test/test_uniform_random_op.py index ad74c34908bef..4cfafe4c7a84e 100644 --- a/test/legacy_test/test_uniform_random_op.py +++ b/test/legacy_test/test_uniform_random_op.py @@ -21,9 +21,9 @@ from test_attribute_var import UnittestBase import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.framework import convert_np_dtype_to_dtype_ from paddle.tensor import random @@ -179,7 +179,7 @@ def verify_output(self, outs): def test_check_api(self): places = self._get_places() for place in places: - with fluid.dygraph.base.guard(place=place): + with base.dygraph.base.guard(place=place): out = self.python_api( self.attrs['shape'], self.dtype, @@ -208,8 +208,8 @@ def test_errors(self): with program_guard(main_prog, start_prog): def test_Variable(): - x1 = fluid.create_lod_tensor( - np.zeros((4, 784)), [[1, 1, 1, 1]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.zeros((4, 784)), [[1, 1, 1, 1]], base.CPUPlace() ) paddle.uniform(x1) @@ -223,7 +223,7 @@ def test_Variable2(): def test_out_dtype(): out = paddle.uniform(shape=[3, 4], dtype='float64') - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) test_out_dtype() @@ -314,60 +314,60 @@ def test_api(self): ), ) - place = fluid.CPUPlace() - x_tensor = fluid.create_lod_tensor( + place = base.CPUPlace() + x_tensor = base.create_lod_tensor( np.random.rand(3, 16).astype("float32"), [[1, 2]], place ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run(feed={'x': x_tensor}, fetch_list=[y], return_numpy=False) class TestUniformRandomOp_attr_tensor_API(unittest.TestCase): def test_attr_tensor_API(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): dim_tensor = paddle.tensor.fill_constant([1], "int64", 3) ret = paddle.uniform([1, dim_tensor, 2]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) outs = exe.run(train_program, fetch_list=[ret]) def test_attr_tensorlist_int32_API(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): dim_1 = paddle.tensor.fill_constant([1], "int64", 3) dim_2 = paddle.tensor.fill_constant([1], "int32", 2) ret = paddle.uniform([1, dim_1, dim_2]) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) outs = exe.run(train_program, fetch_list=[ret]) def test_attr_tensor_int32_API(self): - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): shape = paddle.static.data( name='shape_tensor', shape=[2], dtype="int32" ) ret = paddle.uniform(shape) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) Shape = np.array([2, 3]).astype('int32') exe.run(startup_program) outs = exe.run( @@ -379,19 +379,19 @@ class TestUniformRandomOp_API_seed(unittest.TestCase): def test_attr_tensor_API(self): _seed = 10 gen = paddle.seed(_seed) - startup_program = fluid.Program() - train_program = fluid.Program() - with fluid.program_guard(train_program, startup_program): + startup_program = base.Program() + train_program = base.Program() + with base.program_guard(train_program, startup_program): _min = 5 _max = 10 ret = paddle.uniform([2, 3, 2], min=_min, max=_max, seed=_seed) ret_2 = paddle.uniform([2, 3, 2], min=_min, max=_max, seed=_seed) res = paddle.equal(ret, ret_2) - place = fluid.CPUPlace() - if fluid.core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) - exe = fluid.Executor(place) + place = base.CPUPlace() + if base.core.is_compiled_with_cuda(): + place = base.CUDAPlace(0) + exe = base.Executor(place) exe.run(startup_program) ret_value, cmp_value = exe.run(train_program, fetch_list=[ret, res]) @@ -467,7 +467,7 @@ def check_with_place(self, place): class TestUniformRandomDygraphMode(unittest.TestCase): def test_check_output(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.uniform([10], dtype="float32", min=0.0, max=1.0) x_np = x.numpy() for i in range(10): @@ -481,8 +481,8 @@ def test_errors(self): with program_guard(main_prog, start_prog): def test_Variable(): - x1 = fluid.create_lod_tensor( - np.zeros((100, 784)), [[10, 10, 10, 70]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.zeros((100, 784)), [[10, 10, 10, 70]], base.CPUPlace() ) random.uniform_random_batch_size_like(x1) @@ -524,8 +524,8 @@ def test_errors(self): with program_guard(main_prog, start_prog): def test_Variable(): - x1 = fluid.create_lod_tensor( - np.zeros((100, 784)), [[10, 10, 10, 70]], fluid.CPUPlace() + x1 = base.create_lod_tensor( + np.zeros((100, 784)), [[10, 10, 10, 70]], base.CPUPlace() ) paddle.tensor.random.uniform(x1) @@ -549,14 +549,14 @@ def test_out_dtype(): out = paddle.tensor.random.uniform( shape=[3, 4], dtype='float64' ) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) test_out_dtype() class TestUniformDygraphMode(unittest.TestCase): def test_check_output(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.tensor.random.uniform( [10], dtype="float32", min=0.0, max=1.0 ) @@ -572,17 +572,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.uniform([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.uniform([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.uniform([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) def test_dygraph_fp16(): if not paddle.is_compiled_with_cuda(): @@ -590,7 +590,7 @@ def test_dygraph_fp16(): return paddle.set_device('gpu') out = paddle.uniform([2, 3], dtype=paddle.float16) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) if paddle.is_compiled_with_cuda(): paddle.set_device('gpu') diff --git a/test/legacy_test/test_unique.py b/test/legacy_test/test_unique.py index 438a50df403cc..cf1746a4fd1d4 100644 --- a/test/legacy_test/test_unique.py +++ b/test/legacy_test/test_unique.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle.fluid import core +from paddle.base import core class TestUniqueOp(OpTest): diff --git a/test/legacy_test/test_unique_consecutive_op.py b/test/legacy_test/test_unique_consecutive_op.py index aa903830b7a43..587ef404b7c39 100644 --- a/test/legacy_test/test_unique_consecutive_op.py +++ b/test/legacy_test/test_unique_consecutive_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def reference_unique_consecutive( @@ -199,12 +199,12 @@ def setUp(self): class TestUniqueConsecutiveAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle.enable_static() input_x = paddle.static.data( name="input_x", @@ -215,9 +215,9 @@ def check_static_result(self, place): ) result = paddle.unique_consecutive(input_x) x_np = np.random.randint(20, size=100).astype("float32") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": x_np}, fetch_list=[result], ) @@ -228,7 +228,7 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x = np.random.randint(20, size=100).astype("float64") x = paddle.to_tensor(input_x) result = paddle.unique_consecutive(x) @@ -236,12 +236,12 @@ def test_dygraph(self): class TestUniqueConsecutiveCase2API(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle.enable_static() input_x = paddle.static.data( name="input_x", @@ -254,9 +254,9 @@ def check_static_result(self, place): input_x, return_inverse=True, return_counts=True ) x_np = np.random.randint(20, size=100).astype("float32") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": x_np}, fetch_list=[result], ) @@ -267,7 +267,7 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x = np.random.randint(20, size=100).astype("float64") x = paddle.to_tensor(input_x) result, inverse, counts = paddle.unique_consecutive( @@ -277,12 +277,12 @@ def test_dygraph(self): class TestUniqueConsecutiveCase3API(unittest.TestCase): def setUp(self): - self.places = [fluid.CPUPlace()] + self.places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - self.places.append(fluid.CUDAPlace(0)) + self.places.append(base.CUDAPlace(0)) def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): paddle.enable_static() input_x = paddle.static.data( name="input_x", @@ -295,9 +295,9 @@ def check_static_result(self, place): input_x, return_inverse=True, return_counts=True, axis=-1 ) x_np = np.random.randint(20, size=100).astype("float32") - exe = fluid.Executor(place) + exe = base.Executor(place) fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input_x": x_np}, fetch_list=[result], ) @@ -308,7 +308,7 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_x = np.random.randint(20, size=100).astype("float64") x = paddle.to_tensor(input_x) result, inverse, counts = paddle.unique_consecutive( diff --git a/test/legacy_test/test_unique_name.py b/test/legacy_test/test_unique_name.py index a0b6d790200ed..f6b6c18b12682 100644 --- a/test/legacy_test/test_unique_name.py +++ b/test/legacy_test/test_unique_name.py @@ -14,40 +14,40 @@ import unittest -from paddle import fluid +from paddle import base class TestUniqueName(unittest.TestCase): def test_guard(self): - with fluid.unique_name.guard(): - name_1 = fluid.unique_name.generate('') + with base.unique_name.guard(): + name_1 = base.unique_name.generate('') - with fluid.unique_name.guard(): - name_2 = fluid.unique_name.generate('') + with base.unique_name.guard(): + name_2 = base.unique_name.generate('') self.assertEqual(name_1, name_2) - with fluid.unique_name.guard("A"): - name_1 = fluid.unique_name.generate('') + with base.unique_name.guard("A"): + name_1 = base.unique_name.generate('') - with fluid.unique_name.guard('B'): - name_2 = fluid.unique_name.generate('') + with base.unique_name.guard('B'): + name_2 = base.unique_name.generate('') self.assertNotEqual(name_1, name_2) def test_generate(self): - with fluid.unique_name.guard(): - name1 = fluid.unique_name.generate('fc') - name2 = fluid.unique_name.generate('fc') - name3 = fluid.unique_name.generate('tmp') + with base.unique_name.guard(): + name1 = base.unique_name.generate('fc') + name2 = base.unique_name.generate('fc') + name3 = base.unique_name.generate('tmp') self.assertNotEqual(name1, name2) self.assertEqual(name1[-2:], name3[-2:]) class TestImperativeUniqueName(unittest.TestCase): def test_name_generator(self): - with fluid.dygraph.guard(): - tracer = fluid.framework._dygraph_tracer() + with base.dygraph.guard(): + tracer = base.framework._dygraph_tracer() tmp_var_0 = tracer._generate_unique_name() self.assertEqual(tmp_var_0, "dygraph_tmp_0") diff --git a/test/legacy_test/test_unique_with_counts.py b/test/legacy_test/test_unique_with_counts.py index 2690d61802476..364276a5e473d 100644 --- a/test/legacy_test/test_unique_with_counts.py +++ b/test/legacy_test/test_unique_with_counts.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, paddle_static_guard import paddle -from paddle.fluid import core +from paddle.base import core class TestUniqueWithCountsOp(OpTest): diff --git a/test/legacy_test/test_unpool1d_op.py b/test/legacy_test/test_unpool1d_op.py index e3cab5fe6d5ee..8d6435cc2a86f 100644 --- a/test/legacy_test/test_unpool1d_op.py +++ b/test/legacy_test/test_unpool1d_op.py @@ -61,7 +61,7 @@ def unpool1dmax_forward_naive( class TestUnpool1DOpAPI_dygraph(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -86,7 +86,7 @@ def test_case(self): class TestUnpool1DOpAPI_dygraph2(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -111,7 +111,7 @@ def test_case(self): class TestUnpool1DOpAPI_dygraph3(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -138,7 +138,7 @@ class TestUnpool1DOpAPI_static(unittest.TestCase): def test_case(self): paddle.enable_static() places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: with paddle.static.program_guard( @@ -157,9 +157,9 @@ def test_case(self): output, indices, kernel_size=2, stride=None ) - exe = paddle.fluid.Executor(place) + exe = paddle.base.Executor(place) fetches = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[output_unpool], return_numpy=True, diff --git a/test/legacy_test/test_unpool3d_op.py b/test/legacy_test/test_unpool3d_op.py index bb12198f9910f..f8404d9680985 100644 --- a/test/legacy_test/test_unpool3d_op.py +++ b/test/legacy_test/test_unpool3d_op.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core paddle.enable_static() paddle.seed(2022) @@ -284,7 +284,7 @@ def data_outputsize_error2(): class TestUnpool3DOpAPI_dygraph(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -314,7 +314,7 @@ def test_case(self): class TestUnpool3DOpAPI_dygraph2(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -344,7 +344,7 @@ def test_case(self): class TestUnpool3DOpAPI_dygraph3(unittest.TestCase): def test_case(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: paddle.disable_static() @@ -376,7 +376,7 @@ class TestUnpool3DOpAPI_static(unittest.TestCase): def test_case(self): paddle.enable_static() places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: with paddle.static.program_guard( @@ -412,9 +412,9 @@ def test_case(self): output, indices, kernel_size=2, stride=None ) - exe = paddle.fluid.Executor(place) + exe = paddle.base.Executor(place) fetches = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[output_unpool], return_numpy=True, diff --git a/test/legacy_test/test_unpool_op.py b/test/legacy_test/test_unpool_op.py index ecb7798ff09dd..6020b7b87f17a 100644 --- a/test/legacy_test/test_unpool_op.py +++ b/test/legacy_test/test_unpool_op.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard def _unpool_output_size(x, kernel_size, stride, padding, output_size): @@ -281,14 +281,14 @@ def test_case(self): import paddle import paddle.nn.functional as F - from paddle import fluid - from paddle.fluid import core + from paddle import base + from paddle.base import core if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.array( [ [ @@ -322,14 +322,14 @@ def test_case(self): import paddle import paddle.nn.functional as F - from paddle import fluid - from paddle.fluid import core + from paddle import base + from paddle.base import core if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.array( [ [ @@ -362,14 +362,14 @@ def test_case(self): import numpy as np import paddle - from paddle import fluid - from paddle.fluid import core + from paddle import base + from paddle.base import core if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) else: place = core.CPUPlace() - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_data = np.array( [ [ @@ -402,8 +402,8 @@ class TestUnpoolOpAPI_st(unittest.TestCase): def test_case(self): import paddle import paddle.nn.functional as F - from paddle import fluid - from paddle.fluid import core + from paddle import base + from paddle.base import core paddle.enable_static() @@ -422,11 +422,11 @@ def test_case(self): place = core.CUDAPlace(0) else: place = core.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) results = exe.run( - paddle.fluid.default_main_program(), + paddle.base.default_main_program(), feed={"x": input_data}, fetch_list=[unpool_out], return_numpy=True, diff --git a/test/legacy_test/test_unsqueeze_op.py b/test/legacy_test/test_unsqueeze_op.py index 85d21f5646472..0fbc44a728eed 100755 --- a/test/legacy_test/test_unsqueeze_op.py +++ b/test/legacy_test/test_unsqueeze_op.py @@ -20,8 +20,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -381,9 +381,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) @@ -412,9 +412,9 @@ def func(self, place): def test_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func(p) diff --git a/test/legacy_test/test_unstack_op.py b/test/legacy_test/test_unstack_op.py index 44830b66d4c1d..e07c96ce19019 100755 --- a/test/legacy_test/test_unstack_op.py +++ b/test/legacy_test/test_unstack_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestUnStackOpBase(OpTest): @@ -167,7 +167,7 @@ def test_check_output(self): self.check_output_with_place(place) def test_check_grad(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor(self.inputs['X']) x.stop_gradient = False y = paddle.unstack( diff --git a/test/legacy_test/test_unzip_op.py b/test/legacy_test/test_unzip_op.py index a4136b3d12ce2..8125edc15125a 100644 --- a/test/legacy_test/test_unzip_op.py +++ b/test/legacy_test/test_unzip_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestUnzipOp(unittest.TestCase): @@ -28,7 +28,7 @@ def test_result(self): """ paddle.enable_static() if core.is_compiled_with_cuda(): - place = fluid.CUDAPlace(0) + place = base.CUDAPlace(0) x = paddle.static.data(name='X', shape=[3, 4], dtype='float64') lod = paddle.static.data(name='lod', shape=[11], dtype='int64') output = paddle.incubate.operators.unzip(x, lod) @@ -45,8 +45,8 @@ def test_result(self): 'lod': np.array(lod).astype("int64"), } - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place=place) + exe.run(base.default_startup_program()) res = exe.run(feed=feed, fetch_list=[output]) out = [ [1.0, 2.0, 3.0, 4.0], diff --git a/test/legacy_test/test_update_loss_scaling_op.py b/test/legacy_test/test_update_loss_scaling_op.py index 56ffc0499699a..32cee8125fc27 100644 --- a/test/legacy_test/test_update_loss_scaling_op.py +++ b/test/legacy_test/test_update_loss_scaling_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.static.amp import amp_nn @@ -206,7 +206,7 @@ def test_check_output(self): class TestUpdateLossScalingLayer(unittest.TestCase): - def loss_scaling_check(self, use_cuda=True, scope=fluid.Scope()): + def loss_scaling_check(self, use_cuda=True, scope=base.Scope()): with paddle_static_guard(): a = paddle.static.data( name="a", shape=[1024, 1024], dtype='float32' @@ -251,10 +251,10 @@ def loss_scaling_check(self, use_cuda=True, scope=fluid.Scope()): name="update_loss_scaling", ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - with fluid.scope_guard(scope): - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + with base.scope_guard(scope): + exe.run(base.default_startup_program()) result_v = exe.run( feed={ 'a': a_v, @@ -289,7 +289,7 @@ def loss_scaling_check(self, use_cuda=True, scope=fluid.Scope()): result_v[7], np.zeros_like(num_bad_steps_v) ) - def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): + def loss_scaling_check_inf(self, use_cuda=True, scope=base.Scope()): with paddle_static_guard(): a = paddle.static.data( name="a", shape=[1024, 1024], dtype='float32' @@ -337,10 +337,10 @@ def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): name="update_loss_scaling", ) - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) - with fluid.scope_guard(scope): - exe.run(fluid.default_startup_program()) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) + with base.scope_guard(scope): + exe.run(base.default_startup_program()) result_v = exe.run( feed={ 'a': a_v, @@ -376,36 +376,36 @@ def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): def test_loss_scaling_cpu(self): with paddle_static_guard(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check(use_cuda=False) def test_loss_scaling_cpu_inf(self): with paddle_static_guard(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check_inf(use_cuda=False) def test_loss_scaling_gpu(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): with paddle_static_guard(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check(use_cuda=True) def test_loss_scaling_gpu_inf(self): - if fluid.core.is_compiled_with_cuda(): + if base.core.is_compiled_with_cuda(): with paddle_static_guard(): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check_inf(use_cuda=True) diff --git a/test/legacy_test/test_vander.py b/test/legacy_test/test_vander.py index 7ad2d8e120103..2399bd14f55c1 100644 --- a/test/legacy_test/test_vander.py +++ b/test/legacy_test/test_vander.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core np.random.seed(10) diff --git a/test/legacy_test/test_var_base.py b/test/legacy_test/test_var_base.py index 99209ca798cfa..205b2fecae192 100644 --- a/test/legacy_test/test_var_base.py +++ b/test/legacy_test/test_var_base.py @@ -19,8 +19,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestVarBase(unittest.TestCase): @@ -31,7 +31,7 @@ def setUp(self): def test_to_tensor(self): def check_with_place(place): - with fluid.dygraph.guard(): + with base.dygraph.guard(): paddle.set_default_dtype('float32') # set_default_dtype should not take effect on int x = paddle.to_tensor(1, place=place, stop_gradient=False) @@ -230,8 +230,8 @@ def check_with_place(place): numpy_array = np.random.randn(3, 4) # covert core.LoDTensor to paddle.Tensor - lod_tensor = paddle.fluid.core.LoDTensor() - place = paddle.fluid.framework._current_expected_place() + lod_tensor = paddle.base.core.LoDTensor() + place = paddle.base.framework._current_expected_place() lod_tensor.set(numpy_array, place) x = paddle.to_tensor(lod_tensor) np.testing.assert_array_equal(x.numpy(), numpy_array) @@ -241,7 +241,7 @@ def check_with_place(place): # covert core.Tensor to paddle.Tensor x = paddle.to_tensor(numpy_array) dlpack = x.value().get_tensor()._to_dlpack() - tensor_from_dlpack = paddle.fluid.core.from_dlpack(dlpack) + tensor_from_dlpack = paddle.base.core.from_dlpack(dlpack) x = paddle.to_tensor(tensor_from_dlpack) np.testing.assert_array_equal(x.numpy(), numpy_array) self.assertEqual(x.type, core.VarDesc.VarType.LOD_TENSOR) @@ -318,7 +318,7 @@ def check_with_place(place): check_with_place("gpu:0") def test_to_tensor_not_change_input_stop_gradient(self): - with paddle.fluid.dygraph.guard(core.CPUPlace()): + with paddle.base.dygraph.guard(core.CPUPlace()): a = paddle.zeros([1024]) a.stop_gradient = False b = paddle.to_tensor(a) @@ -328,17 +328,17 @@ def test_to_tensor_not_change_input_stop_gradient(self): def test_to_tensor_change_place(self): if core.is_compiled_with_cuda(): a_np = np.random.rand(1024, 1024) - with paddle.fluid.dygraph.guard(core.CPUPlace()): + with paddle.base.dygraph.guard(core.CPUPlace()): a = paddle.to_tensor(a_np, place=paddle.CUDAPinnedPlace()) a = paddle.to_tensor(a) self.assertEqual(a.place.__repr__(), "Place(cpu)") - with paddle.fluid.dygraph.guard(core.CUDAPlace(0)): + with paddle.base.dygraph.guard(core.CUDAPlace(0)): a = paddle.to_tensor(a_np, place=paddle.CUDAPinnedPlace()) a = paddle.to_tensor(a) self.assertEqual(a.place.__repr__(), "Place(gpu:0)") - with paddle.fluid.dygraph.guard(core.CUDAPlace(0)): + with paddle.base.dygraph.guard(core.CUDAPlace(0)): a = paddle.to_tensor(a_np, place=paddle.CPUPlace()) a = paddle.to_tensor(a, place=paddle.CUDAPinnedPlace()) self.assertEqual(a.place.__repr__(), "Place(gpu_pinned)") @@ -346,13 +346,13 @@ def test_to_tensor_change_place(self): def test_to_tensor_with_lodtensor(self): if core.is_compiled_with_cuda(): a_np = np.random.rand(1024, 1024) - with paddle.fluid.dygraph.guard(core.CPUPlace()): + with paddle.base.dygraph.guard(core.CPUPlace()): lod_tensor = core.LoDTensor() lod_tensor.set(a_np, core.CPUPlace()) a = paddle.to_tensor(lod_tensor) np.testing.assert_array_equal(a_np, a.numpy()) - with paddle.fluid.dygraph.guard(core.CUDAPlace(0)): + with paddle.base.dygraph.guard(core.CUDAPlace(0)): lod_tensor = core.LoDTensor() lod_tensor.set(a_np, core.CUDAPlace(0)) a = paddle.to_tensor(lod_tensor, place=core.CPUPlace()) @@ -360,8 +360,8 @@ def test_to_tensor_with_lodtensor(self): self.assertTrue(a.place.__repr__(), "Place(cpu)") def test_to_variable(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array, name="abc") + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array, name="abc") np.testing.assert_array_equal(var.numpy(), self.array) self.assertEqual(var.name, 'abc') # default value @@ -372,39 +372,39 @@ def test_to_variable(self): self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) # The type of input must be 'ndarray' or 'Variable', it will raise TypeError with self.assertRaises(TypeError): - var = fluid.dygraph.to_variable("test", name="abc") + var = base.dygraph.to_variable("test", name="abc") # test to_variable of LayerObjectHelper(LayerHelperBase) with self.assertRaises(TypeError): linear = paddle.nn.Linear(32, 64) var = linear._helper.to_variable("test", name="abc") def test_list_to_variable(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = [[[1, 2], [1, 2], [1.0, 2]], [[1, 2], [1, 2], [1, 2]]] - var = fluid.dygraph.to_variable(array, dtype='int32') + var = base.dygraph.to_variable(array, dtype='int32') np.testing.assert_array_equal(var.numpy(), array) self.assertEqual(var.shape, [2, 3, 2]) self.assertEqual(var.dtype, core.VarDesc.VarType.INT32) self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) def test_tuple_to_variable(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): array = (((1, 2), (1, 2), (1, 2)), ((1, 2), (1, 2), (1, 2))) - var = fluid.dygraph.to_variable(array, dtype='float32') + var = base.dygraph.to_variable(array, dtype='float32') np.testing.assert_array_equal(var.numpy(), array) self.assertEqual(var.shape, [2, 3, 2]) self.assertEqual(var.dtype, core.VarDesc.VarType.FP32) self.assertEqual(var.type, core.VarDesc.VarType.LOD_TENSOR) def test_tensor_to_variable(self): - with fluid.dygraph.guard(): - t = fluid.Tensor() - t.set(np.random.random((1024, 1024)), fluid.CPUPlace()) - var = fluid.dygraph.to_variable(t) + with base.dygraph.guard(): + t = base.Tensor() + t.set(np.random.random((1024, 1024)), base.CPUPlace()) + var = base.dygraph.to_variable(t) np.testing.assert_array_equal(t, var.numpy()) def test_leaf_tensor(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor(np.random.uniform(-1, 1, size=[10, 10])) self.assertTrue(x.is_leaf) y = x + 1 @@ -430,7 +430,7 @@ def test_leaf_tensor(self): self.assertFalse(out.is_leaf) def test_detach(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor([1.0], dtype="float64", stop_gradient=False) detach_x = x.detach() self.assertTrue(detach_x.stop_gradient, True) @@ -466,8 +466,8 @@ def test_detach(self): y.backward() def test_write_property(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) self.assertEqual(var.name, 'generated_tensor_0') var.name = 'test' @@ -482,7 +482,7 @@ def test_write_property(self): self.assertEqual(var.stop_gradient, False) def test_deep_copy(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): empty_var = core.eager.Tensor() empty_var_copy = copy.deepcopy(empty_var) self.assertEqual( @@ -554,8 +554,8 @@ def test_deep_copy(self): # test some patched methods def test_set_value(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) tmp1 = np.random.uniform(0.1, 1, [2, 2, 3]).astype(self.dtype) self.assertRaises(AssertionError, var.set_value, tmp1) @@ -564,12 +564,12 @@ def test_set_value(self): np.testing.assert_array_equal(var.numpy(), tmp2) def test_to_string(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) self.assertTrue(isinstance(str(var), str)) def test_element_size(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor(1, dtype='bool') self.assertEqual(x.element_size(), 1) @@ -604,8 +604,8 @@ def test_element_size(self): self.assertEqual(x.element_size(), 16) def test_backward(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) var.stop_gradient = False loss = F.relu(var) loss.backward() @@ -613,8 +613,8 @@ def test_backward(self): self.assertEqual(grad_var.shape, self.shape) def test_gradient(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) var.stop_gradient = False loss = F.relu(var) loss.backward() @@ -622,14 +622,14 @@ def test_gradient(self): self.assertEqual(grad_var.shape, self.array.shape) def test_block(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) self.assertEqual( - var.block, fluid.default_main_program().global_block() + var.block, base.default_main_program().global_block() ) def _test_slice(self): - w = fluid.dygraph.to_variable( + w = base.dygraph.to_variable( np.random.random((784, 100, 100)).astype('float64') ) @@ -660,7 +660,7 @@ def _test_slice(self): [[19, 20, 21], [22, 23, 24], [25, 26, 27]], ] ).astype('float32') - var = fluid.dygraph.to_variable(tensor_array) + var = base.dygraph.to_variable(tensor_array) var1 = var[0, 1, 1] var2 = var[1:] var3 = var[0:1] @@ -753,7 +753,7 @@ def _test_slice_for_tensor_attr(self): negative_one = paddle.full(shape=[], fill_value=-1, dtype="int32") four = paddle.full(shape=[], fill_value=4, dtype="int32") - var = fluid.dygraph.to_variable(tensor_array) + var = base.dygraph.to_variable(tensor_array) var1 = var[0, one, one] var2 = var[one:] var3 = var[0:one] @@ -964,7 +964,7 @@ def _test_scalar_bool_index(self): def _test_for_var(self): np_value = np.random.random((30, 100, 100)).astype('float32') - w = fluid.dygraph.to_variable(np_value) + w = base.dygraph.to_variable(np_value) for i, e in enumerate(w): np.testing.assert_array_equal(e.numpy(), np_value[i]) @@ -1028,7 +1028,7 @@ def _test_list_index(self): np.testing.assert_array_equal(array[row, col], x[row, col].numpy()) def test_slice(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): self._test_slice() self._test_slice_for_tensor_attr() self._test_for_var() @@ -1039,7 +1039,7 @@ def test_slice(self): self._test_numpy_index() self._test_list_index() - var = fluid.dygraph.to_variable(self.array) + var = base.dygraph.to_variable(self.array) np.testing.assert_array_equal(var[1, :].numpy(), self.array[1, :]) np.testing.assert_array_equal(var[::-1].numpy(), self.array[::-1]) @@ -1054,22 +1054,22 @@ def test_slice(self): var[paddle.to_tensor([0, 1]), mask] def test_var_base_to_np(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) np.testing.assert_array_equal(var.numpy(), var.numpy(False)) def test_var_base_as_np(self): - with fluid.dygraph.guard(): - var = fluid.dygraph.to_variable(self.array) + with base.dygraph.guard(): + var = base.dygraph.to_variable(self.array) np.testing.assert_array_equal(var.numpy(), np.array(var)) np.testing.assert_array_equal( var.numpy(), np.array(var, dtype=np.float32) ) def test_if(self): - with fluid.dygraph.guard(): - var1 = fluid.dygraph.to_variable(np.array([[[0]]])) - var2 = fluid.dygraph.to_variable(np.array([[[1]]])) + with base.dygraph.guard(): + var1 = base.dygraph.to_variable(np.array([[[0]]])) + var2 = base.dygraph.to_variable(np.array([[[1]]])) var1_bool = False var2_bool = False @@ -1086,13 +1086,13 @@ def test_if(self): assert bool(var2), "bool(var2) is True" def test_to_static_var(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): # Convert Tensor into Variable or Parameter - var_base = fluid.dygraph.to_variable(self.array, name="var_base_1") + var_base = base.dygraph.to_variable(self.array, name="var_base_1") static_var = var_base._to_static_var() self._assert_to_static(var_base, static_var) - var_base = fluid.dygraph.to_variable(self.array, name="var_base_2") + var_base = base.dygraph.to_variable(self.array, name="var_base_2") static_param = var_base._to_static_var(to_parameter=True) self._assert_to_static(var_base, static_param, True) @@ -1112,9 +1112,9 @@ def test_to_static_var(self): def _assert_to_static(self, var_base, static_var, is_param=False): if is_param: - self.assertTrue(isinstance(static_var, fluid.framework.Parameter)) + self.assertTrue(isinstance(static_var, base.framework.Parameter)) self.assertTrue(static_var.persistable, True) - if isinstance(var_base, fluid.framework.EagerParamBase): + if isinstance(var_base, base.framework.EagerParamBase): for attr in ['trainable', 'is_distributed', 'do_model_average']: self.assertEqual( getattr(var_base, attr), getattr(static_var, attr) @@ -1129,7 +1129,7 @@ def _assert_to_static(self, var_base, static_var, is_param=False): ) ) else: - self.assertTrue(isinstance(static_var, fluid.framework.Variable)) + self.assertTrue(isinstance(static_var, base.framework.Variable)) attr_keys = ['block', 'dtype', 'type', 'name'] for attr in attr_keys: @@ -1511,14 +1511,14 @@ def func_setUp(self): def func_test_to_api(self): x_double = self.x._to(dtype='double') - self.assertEqual(x_double.dtype, paddle.fluid.core.VarDesc.VarType.FP64) + self.assertEqual(x_double.dtype, paddle.base.core.VarDesc.VarType.FP64) np.testing.assert_allclose(self.np_x, x_double, rtol=1e-05) x_ = self.x._to() - self.assertEqual(self.x.dtype, paddle.fluid.core.VarDesc.VarType.FP64) + self.assertEqual(self.x.dtype, paddle.base.core.VarDesc.VarType.FP64) np.testing.assert_allclose(self.np_x, x_, rtol=1e-05) - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): x_gpu = self.x._to(device=paddle.CUDAPlace(0)) self.assertTrue(x_gpu.place.is_gpu_place()) self.assertEqual(x_gpu.place.gpu_device_id(), 0) @@ -1531,14 +1531,14 @@ def func_test_to_api(self): self.assertTrue(x_gpu1.place.is_gpu_place()) self.assertEqual(x_gpu1.place.gpu_device_id(), 0) self.assertEqual( - x_gpu1.dtype, paddle.fluid.core.VarDesc.VarType.FP64 + x_gpu1.dtype, paddle.base.core.VarDesc.VarType.FP64 ) x_gpu2 = self.x._to(device='gpu:0', dtype="float16") self.assertTrue(x_gpu2.place.is_gpu_place()) self.assertEqual(x_gpu2.place.gpu_device_id(), 0) self.assertEqual( - x_gpu2.dtype, paddle.fluid.core.VarDesc.VarType.FP16 + x_gpu2.dtype, paddle.base.core.VarDesc.VarType.FP16 ) x_cpu = self.x._to(device=paddle.CPUPlace()) @@ -1549,11 +1549,11 @@ def func_test_to_api(self): x_cpu1 = self.x._to(device=paddle.CPUPlace(), dtype="float64") self.assertTrue(x_cpu1.place.is_cpu_place()) - self.assertEqual(x_cpu1.dtype, paddle.fluid.core.VarDesc.VarType.FP64) + self.assertEqual(x_cpu1.dtype, paddle.base.core.VarDesc.VarType.FP64) x_cpu2 = self.x._to(device='cpu', dtype="float16") self.assertTrue(x_cpu2.place.is_cpu_place()) - self.assertEqual(x_cpu2.dtype, paddle.fluid.core.VarDesc.VarType.FP16) + self.assertEqual(x_cpu2.dtype, paddle.base.core.VarDesc.VarType.FP16) self.assertRaises(ValueError, self.x._to, device=1) self.assertRaises(AssertionError, self.x._to, blocking=1) @@ -1566,18 +1566,18 @@ def test_to_api(self): class TestVarBaseInitVarBaseFromTensorWithDevice(unittest.TestCase): def test_varbase_init(self): paddle.disable_static() - t = fluid.Tensor() + t = base.Tensor() np_x = np.random.random((3, 8, 8)) - t.set(np_x, fluid.CPUPlace()) + t.set(np_x, base.CPUPlace()) - if paddle.fluid.is_compiled_with_cuda(): + if paddle.base.is_compiled_with_cuda(): device = paddle.CUDAPlace(0) - tmp = fluid.core.eager.Tensor(t, device) + tmp = base.core.eager.Tensor(t, device) self.assertTrue(tmp.place.is_gpu_place()) self.assertEqual(tmp.numpy().all(), np_x.all()) device = paddle.CPUPlace() - tmp = fluid.core.eager.Tensor(t, device) + tmp = base.core.eager.Tensor(t, device) self.assertEqual(tmp.numpy().all(), np_x.all()) diff --git a/test/legacy_test/test_variable.py b/test/legacy_test/test_variable.py index 5774b08a3280b..cbc339fad8026 100644 --- a/test/legacy_test/test_variable.py +++ b/test/legacy_test/test_variable.py @@ -18,9 +18,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import ( +from paddle import base +from paddle.base import core +from paddle.base.framework import ( Program, convert_np_dtype_to_dtype_, default_main_program, @@ -70,14 +70,14 @@ def test_var(self): ) w = b.create_var( - dtype=paddle.fluid.core.VarDesc.VarType.STRINGS, + dtype=paddle.base.core.VarDesc.VarType.STRINGS, shape=[1], name="str_var", ) self.assertEqual(None, w.lod_level) def test_element_size(self): - with fluid.program_guard(Program(), Program()): + with base.program_guard(Program(), Program()): x = paddle.static.data(name='x1', shape=[2], dtype='bool') self.assertEqual(x.element_size(), 1) @@ -139,9 +139,9 @@ def _test_slice(self, place): self.assertEqual(0, nw.lod_level) - main = fluid.Program() - with fluid.program_guard(main): - exe = fluid.Executor(place) + main = base.Program() + with base.program_guard(main): + exe = base.Executor(place) tensor_array = np.array( [ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], @@ -170,10 +170,10 @@ def _test_slice(self, place): x = paddle.static.data(name='x', shape=[-1, 13], dtype='float32') y = paddle.static.nn.fc(x, size=1, activation=None) y_1 = y[:, 0] - feeder = fluid.DataFeeder(place=place, feed_list=[x]) + feeder = base.DataFeeder(place=place, feed_list=[x]) data = [] data.append(np.random.randint(10, size=[13]).astype('float32')) - exe.run(fluid.default_startup_program()) + exe.run(base.default_startup_program()) local_out = exe.run( main, @@ -387,7 +387,7 @@ def _test_slice_index_scalar_bool(self, place): self.assertTrue((result[0] == expected[0]).all()) def test_slice(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) @@ -409,16 +409,16 @@ def _tostring(self): self.assertTrue(isinstance(str(wc), str)) def test_tostring(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): self._tostring() - with fluid.program_guard(default_main_program()): + with base.program_guard(default_main_program()): self._tostring() def test_fake_interface_only_api(self): b = default_main_program().current_block() var = b.create_var(dtype="float64", lod_level=0) - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.assertRaises(AssertionError, var.numpy) self.assertRaises(AssertionError, var.backward) self.assertRaises(AssertionError, var.gradient) @@ -427,7 +427,7 @@ def test_fake_interface_only_api(self): def test_variable_in_dygraph_mode(self): b = default_main_program().current_block() var = b.create_var(dtype="float64", shape=[1, 1]) - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.assertTrue(var.to_string(True).startswith('name:')) self.assertFalse(var.persistable) @@ -440,8 +440,8 @@ def test_variable_in_dygraph_mode(self): self.assertTrue(var.name.startswith('_generated_var_')) self.assertEqual(var.shape, (1, 1)) - self.assertEqual(var.dtype, fluid.core.VarDesc.VarType.FP64) - self.assertEqual(var.type, fluid.core.VarDesc.VarType.LOD_TENSOR) + self.assertEqual(var.dtype, base.core.VarDesc.VarType.FP64) + self.assertEqual(var.type, base.core.VarDesc.VarType.LOD_TENSOR) def test_create_selected_rows(self): b = default_main_program().current_block() @@ -450,7 +450,7 @@ def test_create_selected_rows(self): name="var", shape=[1, 1], dtype="float32", - type=fluid.core.VarDesc.VarType.SELECTED_ROWS, + type=base.core.VarDesc.VarType.SELECTED_ROWS, persistable=True, ) @@ -463,7 +463,7 @@ def test_size(self): prog = paddle.static.Program() with paddle.static.program_guard(prog): x = paddle.assign(np.random.rand(2, 3, 4).astype("float32")) - exe = paddle.static.Executor(fluid.CPUPlace()) + exe = paddle.static.Executor(base.CPUPlace()) exe.run(paddle.static.default_startup_program()) output = exe.run(prog, fetch_list=[x.size()]) @@ -484,7 +484,7 @@ def test_detach(self): startup = paddle.static.Program() main = paddle.static.Program() - scope = fluid.core.Scope() + scope = base.core.Scope() with paddle.static.scope_guard(scope): with paddle.static.program_guard(main, startup): x = paddle.static.data( @@ -575,7 +575,7 @@ def _test_item_none_and_decrease(self, place): self.assertTrue((result[i] == expected[i]).all()) def test_slice(self): - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): places.append(core.CUDAPlace(0)) @@ -628,9 +628,9 @@ def test_static_graph_list_index(self): y = x[index_mod] place = ( - paddle.fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else paddle.fluid.CUDAPlace(0) + paddle.base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else paddle.base.CUDAPlace(0) ) prog = paddle.static.default_main_program() @@ -713,9 +713,9 @@ def test_static_graph_list_index_muti_dim(self): y = x[index1, index2] place = ( - paddle.fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else paddle.fluid.CUDAPlace(0) + paddle.base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else paddle.base.CUDAPlace(0) ) prog = paddle.static.default_main_program() @@ -777,7 +777,7 @@ def run_getitem_list_index(self, array, index): x = paddle.static.data(name='x', shape=array.shape, dtype='float32') y = x[index] - place = paddle.fluid.CPUPlace() + place = paddle.base.CPUPlace() prog = paddle.static.default_main_program() exe = paddle.static.Executor(place) @@ -835,7 +835,7 @@ def run_setitem_list_index(self, array, index, value_np): ) y = paddle.static.setitem(x, index, value) - place = paddle.fluid.CPUPlace() + place = paddle.base.CPUPlace() prog = paddle.static.default_main_program() exe = paddle.static.Executor(place) @@ -1034,9 +1034,9 @@ def test_static_graph_tensor_index_setitem_muti_dim(self): x1_out = paddle.static.setitem(x1, (index_1, index_2), value) x2_out = paddle.static.setitem(x2, index_1, value) place = ( - paddle.fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else paddle.fluid.CUDAPlace(0) + paddle.base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else paddle.base.CUDAPlace(0) ) prog = paddle.static.default_main_program() @@ -1117,9 +1117,9 @@ def test_static_graph_array_index_muti_dim(self): y1 = x1_out[index_mod2, index_mod1] y2 = x2_out[index_mod2] place = ( - paddle.fluid.CPUPlace() - if not paddle.fluid.core.is_compiled_with_cuda() - else paddle.fluid.CUDAPlace(0) + paddle.base.CPUPlace() + if not paddle.base.core.is_compiled_with_cuda() + else paddle.base.CUDAPlace(0) ) prog = paddle.static.default_main_program() diff --git a/test/legacy_test/test_variable_length_memory_efficient_attention.py b/test/legacy_test/test_variable_length_memory_efficient_attention.py index 76630a8e6c2f1..a02e942a29697 100644 --- a/test/legacy_test/test_variable_length_memory_efficient_attention.py +++ b/test/legacy_test/test_variable_length_memory_efficient_attention.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.framework import core from paddle.incubate.nn.functional import ( variable_length_memory_efficient_attention, @@ -277,7 +277,7 @@ def test_all(self): out = variable_length_memory_efficient_attention( q, k, v, seq_lens, seq_lens, mask, self.scale ) - exe = fluid.Executor() + exe = base.Executor() res = exe.run( feed={ "query": self.q, diff --git a/test/legacy_test/test_variance_layer.py b/test/legacy_test/test_variance_layer.py index 2613fb91b15c0..d4dc28f8f6cce 100644 --- a/test/legacy_test/test_variance_layer.py +++ b/test/legacy_test/test_variance_layer.py @@ -39,7 +39,7 @@ def setUp(self): self.x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) self.place = ( paddle.CUDAPlace(0) - if paddle.fluid.core.is_compiled_with_cuda() + if paddle.base.core.is_compiled_with_cuda() else paddle.CPUPlace() ) diff --git a/test/legacy_test/test_version.py b/test/legacy_test/test_version.py index 55b5228882bff..2b6d8f599c582 100644 --- a/test/legacy_test/test_version.py +++ b/test/legacy_test/test_version.py @@ -15,7 +15,7 @@ import re import unittest -import paddle.version as fluid_version +import paddle.version as base_version class VersionTest(unittest.TestCase): @@ -29,24 +29,24 @@ def setUp(self): def test_check_output(self): # check commit format - self.assertTrue(re.match(self._commit_regex, fluid_version.commit)) - self.assertTrue(isinstance(fluid_version.istaged, bool)) + self.assertTrue(re.match(self._commit_regex, base_version.commit)) + self.assertTrue(isinstance(base_version.istaged, bool)) # check version format - if fluid_version.istaged: - self.assertTrue(re.match(self._major_regex, fluid_version.major)) - self.assertTrue(re.match(self._minor_regex, fluid_version.minor)) - self.assertTrue(re.match(self._patch_regex, fluid_version.patch)) - self.assertTrue(re.match(self._rc_regex, fluid_version.rc)) + if base_version.istaged: + self.assertTrue(re.match(self._major_regex, base_version.major)) + self.assertTrue(re.match(self._minor_regex, base_version.minor)) + self.assertTrue(re.match(self._patch_regex, base_version.patch)) + self.assertTrue(re.match(self._rc_regex, base_version.rc)) self.assertTrue( - re.match(self._version_regex, fluid_version.full_version) + re.match(self._version_regex, base_version.full_version) ) else: - self.assertEqual(fluid_version.major, "0") - self.assertEqual(fluid_version.minor, "0") - self.assertEqual(fluid_version.patch, "0") - self.assertEqual(fluid_version.rc, "0") - self.assertEqual(fluid_version.full_version, "0.0.0") + self.assertEqual(base_version.major, "0") + self.assertEqual(base_version.minor, "0") + self.assertEqual(base_version.patch, "0") + self.assertEqual(base_version.rc, "0") + self.assertEqual(base_version.full_version, "0.0.0") if __name__ == '__main__': diff --git a/test/legacy_test/test_view_op_reuse_allocation.py b/test/legacy_test/test_view_op_reuse_allocation.py index cee8294541089..ea48c9addb5b3 100644 --- a/test/legacy_test/test_view_op_reuse_allocation.py +++ b/test/legacy_test/test_view_op_reuse_allocation.py @@ -66,7 +66,7 @@ def test_forward_version(self): def test_backward_error(self): # It raises an error because the inplace operator will result # in incorrect gradient computation. - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var_a = paddle.ones(shape=self.input_shape, dtype="float32") var_a.stop_gradient = False diff --git a/test/legacy_test/test_viterbi_decode_op.py b/test/legacy_test/test_viterbi_decode_op.py index c7891ddcd93a6..3b62d035b64fd 100644 --- a/test/legacy_test/test_viterbi_decode_op.py +++ b/test/legacy_test/test_viterbi_decode_op.py @@ -14,8 +14,8 @@ from eager_op_test import OpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -107,9 +107,9 @@ def set_attr(self): self.use_tag = True self.bz, self.len, self.ntags = 4, 8, 10 self.places = ( - [fluid.CPUPlace(), fluid.CUDAPlace(0)] + [base.CPUPlace(), base.CUDAPlace(0)] if core.is_compiled_with_cuda() - else [fluid.CPUPlace()] + else [base.CPUPlace()] ) def setUp(self): @@ -123,7 +123,7 @@ def setUp(self): def check_static_result(self, place): bz, length, ntags = self.bz, self.len, self.ntags - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): Input = paddle.static.data( name="Input", shape=[bz, length, ntags], dtype="float32" ) @@ -135,7 +135,7 @@ def check_static_result(self, place): ) decoder = paddle.text.ViterbiDecoder(Transition, self.use_tag) score, path = decoder(Input, Length) - exe = fluid.Executor(place) + exe = base.Executor(place) feed_list = { "Input": self.input, "Transition": self.transitions, diff --git a/test/legacy_test/test_warpctc_op.py b/test/legacy_test/test_warpctc_op.py index d33584ac9fd8c..e7c1ff28e1b5b 100644 --- a/test/legacy_test/test_warpctc_op.py +++ b/test/legacy_test/test_warpctc_op.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard CUDA_BLOCK_SIZE = 32 diff --git a/test/legacy_test/test_warprnnt_op.py b/test/legacy_test/test_warprnnt_op.py index 30ee35a2ae3cd..c5c2950cc7c24 100644 --- a/test/legacy_test/test_warprnnt_op.py +++ b/test/legacy_test/test_warprnnt_op.py @@ -19,7 +19,7 @@ import paddle from paddle import _C_ops -from paddle.fluid import Program, core, program_guard +from paddle.base import Program, core, program_guard paddle.enable_static() diff --git a/test/legacy_test/test_weight_decay.py b/test/legacy_test/test_weight_decay.py index 41bea82c4cd03..c237aa75fd8fd 100644 --- a/test/legacy_test/test_weight_decay.py +++ b/test/legacy_test/test_weight_decay.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import compiler, core +from paddle import base +from paddle.base import compiler, core def get_places(): @@ -32,10 +32,10 @@ def get_places(): @contextlib.contextmanager def prog_scope_guard(main_prog, startup_prog): - scope = fluid.core.Scope() - with fluid.unique_name.guard(): - with fluid.scope_guard(scope): - with fluid.program_guard(main_prog, startup_prog): + scope = base.core.Scope() + with base.unique_name.guard(): + with base.scope_guard(scope): + with base.program_guard(main_prog, startup_prog): yield @@ -52,7 +52,7 @@ def bow_net( """ BOW net This model is from https://github.com/PaddlePaddle/models: - fluid/PaddleNLP/text_classification/nets.py + base/PaddleNLP/text_classification/nets.py """ emb = paddle.static.nn.embedding( input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim] @@ -84,10 +84,10 @@ def setUp(self): self.learning_rate = 0.5 def run_executor(self, place, feed_list, loss): - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=feed_list, place=place) - exe.run(fluid.default_startup_program()) - main_prog = fluid.default_main_program() + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) + main_prog = base.default_main_program() loss_set = [] for data in self.train_data: out = exe.run( @@ -108,24 +108,24 @@ def run_parallel_exe( use_fast_executor=False, use_ir_memory_optimize=False, ): - exe = fluid.Executor(place) - feeder = fluid.DataFeeder(feed_list=feed_list, place=place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) - exec_strategy = fluid.ExecutionStrategy() + exec_strategy = base.ExecutionStrategy() if use_fast_executor: exec_strategy.use_experimental_executor = True - build_strategy = fluid.BuildStrategy() + build_strategy = base.BuildStrategy() build_strategy.reduce_strategy = ( - fluid.BuildStrategy.ReduceStrategy.Reduce + base.BuildStrategy.ReduceStrategy.Reduce if use_reduce - else fluid.BuildStrategy.ReduceStrategy.AllReduce + else base.BuildStrategy.ReduceStrategy.AllReduce ) build_strategy.memory_optimize = use_ir_memory_optimize train_cp = compiler.CompiledProgram( - fluid.default_main_program(), build_strategy=build_strategy + base.default_main_program(), build_strategy=build_strategy ) loss_set = [] @@ -140,8 +140,8 @@ def run_parallel_exe( def check_weight_decay( self, place, model, use_parallel_exe=False, use_reduce=False ): - main_prog = fluid.framework.Program() - startup_prog = fluid.framework.Program() + main_prog = base.framework.Program() + startup_prog = base.framework.Program() startup_prog.random_seed = 1 with prog_scope_guard(main_prog=main_prog, startup_prog=startup_prog): data = paddle.static.data( diff --git a/test/legacy_test/test_weight_normalization.py b/test/legacy_test/test_weight_normalization.py index e94e61387c061..f8793aef3052e 100644 --- a/test/legacy_test/test_weight_normalization.py +++ b/test/legacy_test/test_weight_normalization.py @@ -18,9 +18,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.param_attr import WeightNormParamAttr +from paddle import base +from paddle.base import core +from paddle.base.param_attr import WeightNormParamAttr class TestWeightNormalization(unittest.TestCase): @@ -49,7 +49,7 @@ def set_program(cls): activation=None, ) loss = paddle.sum(out) - fluid.backward.append_backward(loss=loss) + base.backward.append_backward(loss=loss) cls.fetch_list = [ 'weight_norm_param_g', 'weight_norm_param_v', @@ -63,10 +63,10 @@ def run_program(self): places.append(core.CUDAPlace(0)) for place in places: self.set_inputs(place) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) output = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed=self.inputs, fetch_list=self.fetch_list, return_numpy=False, @@ -99,7 +99,7 @@ def set_data(self): def set_inputs(self, place): self.inputs = {} for desc in self.data_desc: - tensor = fluid.Tensor() + tensor = base.Tensor() tensor.set(self.data[desc[0]][0], place) if self.data[desc[0]][1]: tensor.set_recursive_sequence_lengths(self.data[desc[0]][1]) diff --git a/test/legacy_test/test_where_op.py b/test/legacy_test/test_where_op.py index aa03f7276c1b3..3daa2fbbb6898 100644 --- a/test/legacy_test/test_where_op.py +++ b/test/legacy_test/test_where_op.py @@ -18,9 +18,9 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard -from paddle.fluid.backward import append_backward +from paddle import base +from paddle.base import Program, core, program_guard +from paddle.base.backward import append_backward class TestWhereOp(OpTest): @@ -127,7 +127,7 @@ def ref_y_backward(self, dout): def test_api(self, use_cuda=False): for x_stop_gradient in [False, True]: for y_stop_gradient in [False, True]: - with fluid.program_guard(Program(), Program()): + with base.program_guard(Program(), Program()): cond = paddle.static.data( name='cond', shape=[-1] + self.shape, dtype='bool' ) @@ -148,21 +148,19 @@ def test_api(self, use_cuda=False): result.stop_gradient = False append_backward(paddle.mean(result)) for use_cuda in [False, True]: - if use_cuda and ( - not fluid.core.is_compiled_with_cuda() - ): + if use_cuda and (not base.core.is_compiled_with_cuda()): break place = ( - fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() + base.CUDAPlace(0) if use_cuda else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) fetch_list = [result, result.grad_name] if x_stop_gradient is False: fetch_list.append(x.grad_name) if y_stop_gradient is False: fetch_list.append(y.grad_name) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'cond': self.cond, 'x': self.x, 'y': self.y}, fetch_list=fetch_list, ) @@ -182,7 +180,7 @@ def test_api(self, use_cuda=False): def test_api_broadcast(self, use_cuda=False): main_program = Program() - with fluid.program_guard(main_program): + with base.program_guard(main_program): x = paddle.static.data(name='x', shape=[-1, 4, 1], dtype='float32') x.desc.set_need_check_feed(False) y = paddle.static.data(name='y', shape=[-1, 4, 2], dtype='float32') @@ -193,12 +191,12 @@ def test_api_broadcast(self, use_cuda=False): ) result = paddle.where((x > 1), x=x, y=y) for use_cuda in [False, True]: - if use_cuda and (not fluid.core.is_compiled_with_cuda()): + if use_cuda and (not base.core.is_compiled_with_cuda()): return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': x_i, 'y': y_i}, fetch_list=[result], ) @@ -209,7 +207,7 @@ def test_api_broadcast(self, use_cuda=False): def test_scalar(self): paddle.enable_static() main_program = Program() - with fluid.program_guard(main_program): + with base.program_guard(main_program): cond_shape = [2, 4] cond = paddle.static.data( name='cond', shape=[-1] + cond_shape, dtype='bool' @@ -220,12 +218,12 @@ def test_scalar(self): cond_data = np.array([False, False, True, True]).astype('bool') result = paddle.where(condition=cond, x=x_data, y=y_data) for use_cuda in [False, True]: - if use_cuda and (not fluid.core.is_compiled_with_cuda()): + if use_cuda and (not base.core.is_compiled_with_cuda()): return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'cond': cond_data}, fetch_list=[result], ) @@ -235,7 +233,7 @@ def test_scalar(self): def __test_where_with_broadcast_static(self, cond_shape, x_shape, y_shape): paddle.enable_static() main_program = Program() - with fluid.program_guard(main_program): + with base.program_guard(main_program): cond = paddle.static.data( name='cond', shape=[-1] + cond_shape, dtype='bool' ) @@ -254,12 +252,12 @@ def __test_where_with_broadcast_static(self, cond_shape, x_shape, y_shape): y_data = np.random.random(size=y_shape).astype('float32') result = paddle.where(condition=cond, x=x, y=y) for use_cuda in [False, True]: - if use_cuda and (not fluid.core.is_compiled_with_cuda()): + if use_cuda and (not base.core.is_compiled_with_cuda()): return - place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CUDAPlace(0) if use_cuda else base.CPUPlace() + exe = base.Executor(place) out = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'cond': cond_data, 'x': x_data, 'y': y_data}, fetch_list=[result], ) @@ -317,29 +315,29 @@ def test_static_api_broadcast_8(self): class TestWhereDygraphAPI(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x_i = np.array([0.9383, 0.1983, 3.2, 1.2]).astype('float64') y_i = np.array([1.0, 1.0, 1.0, 1.0]).astype('float64') cond_i = np.array([False, False, True, True]).astype('bool') - x = fluid.dygraph.to_variable(x_i) - y = fluid.dygraph.to_variable(y_i) - cond = fluid.dygraph.to_variable(cond_i) + x = base.dygraph.to_variable(x_i) + y = base.dygraph.to_variable(y_i) + cond = base.dygraph.to_variable(cond_i) out = paddle.where(cond, x, y) np.testing.assert_array_equal( out.numpy(), np.where(cond_i, x_i, y_i) ) def test_scalar(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): cond_i = np.array([False, False, True, True]).astype('bool') x = 1.0 y = 2.0 - cond = fluid.dygraph.to_variable(cond_i) + cond = base.dygraph.to_variable(cond_i) out = paddle.where(cond, x, y) np.testing.assert_array_equal(out.numpy(), np.where(cond_i, x, y)) def __test_where_with_broadcast_dygraph(self, cond_shape, a_shape, b_shape): - with fluid.dygraph.guard(): + with base.dygraph.guard(): cond_tmp = paddle.rand(cond_shape) cond = cond_tmp < 0.3 a = paddle.rand(a_shape) @@ -406,7 +404,7 @@ def test_where_condition(self): self.assertEqual(type(y), tuple) self.assertEqual(len(y), 2) z = paddle.concat(list(y), axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[z.name], return_numpy=False ) @@ -420,7 +418,7 @@ def test_where_condition(self): self.assertEqual(type(y), tuple) self.assertEqual(len(y), 1) z = paddle.concat(list(y), axis=1) - exe = fluid.Executor(fluid.CPUPlace()) + exe = base.Executor(base.CPUPlace()) (res,) = exe.run( feed={'x': data}, fetch_list=[z.name], return_numpy=False ) @@ -454,7 +452,7 @@ def test_type(): self.assertRaises(TypeError, test_type) def test_value_error(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): cond_shape = [2, 2, 4] cond_tmp = paddle.rand(cond_shape) cond = cond_tmp < 0.3 diff --git a/test/legacy_test/test_while_loop_op.py b/test/legacy_test/test_while_loop_op.py index 9ba690f5b1d93..c05b62b7f7ac4 100644 --- a/test/legacy_test/test_while_loop_op.py +++ b/test/legacy_test/test_while_loop_op.py @@ -18,10 +18,10 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, program_guard +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.framework import Program, program_guard paddle.enable_static() @@ -45,11 +45,11 @@ def body(i): out = paddle.static.nn.while_loop(cond, body, (i,)) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=out) np.testing.assert_allclose( np.asarray(res[0]), np.full(1, 10, np.int64), rtol=1e-05 @@ -81,11 +81,11 @@ def body(i, mem): data_one = np.ones(10).astype('float32') place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, feed={'mem': data}, fetch_list=out) for i in range(10): data = np.add(data, data_one) @@ -144,11 +144,11 @@ def body(i, ten, test_dict, test_list, test_list_dict): cond, body, [i, ten, test_dict, test_list, test_list_dict] ) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, fetch_list=[ @@ -228,11 +228,11 @@ def internal_body(j, init, sums): data_sums = np.zeros([3, 3]).astype('float32') place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run( main_program, feed={'init': data, 'sums': data_sums}, fetch_list=out ) @@ -256,7 +256,7 @@ def body(i, x): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): i = paddle.static.data(name='i', shape=[1], dtype='float32') i.stop_gradient = False eleven = paddle.tensor.fill_constant( @@ -273,11 +273,11 @@ def body(i, x): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) feed_i = np.ones(1).astype('float32') feed_x = np.ones(1).astype('float32') @@ -303,7 +303,7 @@ def body(i, x): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): i = paddle.static.data(name='i', shape=[1], dtype='float32') i.stop_gradient = False x = paddle.static.data(name='x', shape=[1], dtype='float32') @@ -314,11 +314,11 @@ def body(i, x): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) feed_i = np.ones(1).astype('float32') feed_x = np.ones(1).astype('float32') @@ -367,7 +367,7 @@ def internal_body(j, x, mem_array): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): d0 = paddle.static.data(name='d0', shape=[10], dtype='float32') d1 = paddle.static.data(name='d1', shape=[10], dtype='float32') d2 = paddle.static.data(name='d2', shape=[10], dtype='float32') @@ -403,11 +403,11 @@ def internal_body(j, x, mem_array): append_backward(mean) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) d = [] for i in range(3): @@ -450,7 +450,7 @@ def fn_add_one(): main_program = Program() startup_program = Program() - with fluid.program_guard(main_program, startup_program): + with base.program_guard(main_program, startup_program): i = paddle.tensor.fill_constant(shape=[1], dtype='int64', value=1) ten = paddle.tensor.fill_constant( shape=[1], dtype='int64', value=10 @@ -462,11 +462,11 @@ def fn_add_one(): out = paddle.static.nn.while_loop(cond, body, [i]) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) res = exe.run(main_program, fetch_list=out) data = np.asarray([25]).astype('int64') @@ -661,11 +661,11 @@ def body(z, i): z, _ = paddle.static.nn.while_loop(cond, body, [z, i]) place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) np_x = np.array([1, 2, 3, 4, 5], dtype='int32') res = exe.run(main_program, feed={'x': np_x}, fetch_list=[z]) diff --git a/test/legacy_test/test_while_op.py b/test/legacy_test/test_while_op.py index 25d34f53e1de2..3f12fa397a3a8 100644 --- a/test/legacy_test/test_while_op.py +++ b/test/legacy_test/test_while_op.py @@ -17,10 +17,10 @@ import numpy import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.executor import Executor from paddle.incubate.layers.nn import shuffle_batch paddle.enable_static() @@ -78,9 +78,9 @@ def simple_net(self): return loss, sum_result def test_simple_net(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): loss, sum_result = self.simple_net() append_backward(loss) @@ -99,11 +99,11 @@ def test_simple_net(self): self.assertAlmostEqual(numpy.sum(d), numpy.sum(outs[0]), delta=0.01) def test_simple_net_forward(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): self.simple_net() - binary = fluid.compiler.CompiledProgram(main_program) + binary = base.compiler.CompiledProgram(main_program) cpu = core.CPUPlace() exe = Executor(cpu) @@ -130,7 +130,7 @@ def test_exceptions(self): class BadInputTest(unittest.TestCase): def test_error(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_bad_x(): x = [1, 2, 3] @@ -166,8 +166,8 @@ def body_func(i, ten, batch_info, origin_seq): output = shuffle_temp - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) input_x = numpy.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]]) input_x = input_x.reshape(3, 1, 4) @@ -175,7 +175,7 @@ def body_func(i, ten, batch_info, origin_seq): input_y = input_y.reshape(3, 1, 1) (res,) = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={'x': input_x, 'y': input_y}, fetch_list=[output], ) @@ -188,9 +188,9 @@ def test_outputs_exists_inputs(self): """ We guarantee that the output tensor must be in the input tensor, so that the output and input can correspond to each other, but the input can be greater than the number of outputs. It's required in paddle2onnx. """ - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): def func(x): s = paddle.zeros([]) diff --git a/test/legacy_test/test_yolov3_loss_op.py b/test/legacy_test/test_yolov3_loss_op.py index afe1dbc1c0a1f..c9a9779a2a984 100644 --- a/test/legacy_test/test_yolov3_loss_op.py +++ b/test/legacy_test/test_yolov3_loss_op.py @@ -19,7 +19,7 @@ from scipy.special import expit, logit import paddle -from paddle.fluid import core +from paddle.base import core def l1loss(x, y): diff --git a/test/legacy_test/test_zero_dim_tensor.py b/test/legacy_test/test_zero_dim_tensor.py index 2f02a00209f56..ed6eb3786e077 100644 --- a/test/legacy_test/test_zero_dim_tensor.py +++ b/test/legacy_test/test_zero_dim_tensor.py @@ -4921,7 +4921,7 @@ def test_sequence_pad(self): value = paddle.to_tensor(1000, dtype=paddle.int64).squeeze() out = paddle.static.nn.sequence_pad(x, value) - x_tensor = paddle.fluid.create_lod_tensor( + x_tensor = paddle.base.create_lod_tensor( np.arange(20).astype(np.int64).reshape(-1, 2), [[3, 3, 4]], place=self.exe.place, diff --git a/test/legacy_test/test_zeros_like_op.py b/test/legacy_test/test_zeros_like_op.py index 23ec95877da8c..538556cd4f1fc 100644 --- a/test/legacy_test/test_zeros_like_op.py +++ b/test/legacy_test/test_zeros_like_op.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import _C_ops, fluid, zeros_like -from paddle.fluid import Program, core, program_guard -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle import _C_ops, base, zeros_like +from paddle.base import Program, core, program_guard +from paddle.base.framework import convert_np_dtype_to_dtype_ class TestZerosLikeAPIError(unittest.TestCase): @@ -42,11 +42,11 @@ def test_api(self): out4 = zeros_like(x, 'int32') out5 = zeros_like(x, 'int64') place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) outs = exe.run( train_program, feed={'X': np.ones(shape).astype('float32')}, @@ -63,9 +63,9 @@ class TestZerosLikeImpeartive(unittest.TestCase): def test_out(self): shape = [3, 4] place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) paddle.disable_static(place) x = paddle.to_tensor(np.ones(shape)) @@ -85,9 +85,9 @@ class TestZerosAPI(unittest.TestCase): def test_api(self): shape = [3, 4] place = ( - fluid.CUDAPlace(0) + base.CUDAPlace(0) if core.is_compiled_with_cuda() - else fluid.CPUPlace() + else base.CPUPlace() ) paddle.disable_static(place) diff --git a/test/legacy_test/test_zeros_op.py b/test/legacy_test/test_zeros_op.py index f8da6e20726c1..ce4449335425c 100644 --- a/test/legacy_test/test_zeros_op.py +++ b/test/legacy_test/test_zeros_op.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard class ApiZerosTest(unittest.TestCase): @@ -52,7 +52,7 @@ def test_out(self): result = exe.run(fetch_list=[out]) self.assertEqual((result == out_np).all(), True) - def test_fluid_out(self): + def test_base_out(self): with program_guard(Program()): zeros = paddle.zeros(shape=[10], dtype='int64') place = paddle.CPUPlace() @@ -65,13 +65,13 @@ def test_fluid_out(self): class ApiZerosError(unittest.TestCase): def test_errors(self): def test_error1(): - with paddle.static.program_guard(fluid.Program()): + with paddle.static.program_guard(base.Program()): ones = paddle.zeros(shape=10, dtype='int64') self.assertRaises(TypeError, test_error1) def test_shape_errors(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): try: shape = [-1, 5] out = paddle.zeros(shape) diff --git a/test/legacy_test/testsuite.py b/test/legacy_test/testsuite.py index 9c591fdaafad0..1c48b90cb77a9 100644 --- a/test/legacy_test/testsuite.py +++ b/test/legacy_test/testsuite.py @@ -15,7 +15,7 @@ import numpy as np from op import Operator -from paddle.fluid import core +from paddle.base import core def create_op(scope, op_type, inputs, outputs, attrs, cache_list=None): diff --git a/test/legacy_test/transformer_model.py b/test/legacy_test/transformer_model.py index 03f926c1fb4c0..d1dffc491ba45 100644 --- a/test/legacy_test/transformer_model.py +++ b/test/legacy_test/transformer_model.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base pos_enc_param_names = ( "src_pos_enc_table", @@ -273,7 +273,7 @@ def prepare_encoder( src_pos, size=[src_max_len, src_emb_dim], padding_idx=pos_pad_idx, - param_attr=fluid.ParamAttr(name=pos_enc_param_name, trainable=False), + param_attr=base.ParamAttr(name=pos_enc_param_name, trainable=False), ) src_pos_enc.stop_gradient = True enc_input = src_word_emb + src_pos_enc diff --git a/test/legacy_test/utils.py b/test/legacy_test/utils.py index 86f467f46047c..71478024cbcdc 100644 --- a/test/legacy_test/utils.py +++ b/test/legacy_test/utils.py @@ -14,8 +14,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.framework import _dygraph_guard +from paddle import base +from paddle.base.framework import _dygraph_guard __all__ = ['DyGraphProgramDescTracerTestHelper', 'is_equal_program'] @@ -86,7 +86,7 @@ def _is_equal_program(prog1, prog2): def load_dygraph_vars_to_scope(model_path, scope, place): def load_dict_to_scope(scope, dictionary): if scope is None: - scope = fluid.global_scope() + scope = base.global_scope() for k, v in dictionary.items(): dst_t = scope.var(k).get_tensor() diff --git a/test/mkldnn/check_flags_mkldnn_ops_on_off.py b/test/mkldnn/check_flags_mkldnn_ops_on_off.py index f00f3967225ca..cc1b01c7ae2e9 100644 --- a/test/mkldnn/check_flags_mkldnn_ops_on_off.py +++ b/test/mkldnn/check_flags_mkldnn_ops_on_off.py @@ -17,9 +17,9 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.framework import _global_flags -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base.framework import _global_flags +from paddle.base.layer_helper import LayerHelper def check(): @@ -28,8 +28,8 @@ def check(): _global_flags()["FLAGS_use_mkldnn"], ) print( - "check: fluid.get_flags('FLAGS_use_mkldnn')=", - fluid.get_flags(['FLAGS_use_mkldnn']), + "check: base.get_flags('FLAGS_use_mkldnn')=", + base.get_flags(['FLAGS_use_mkldnn']), ) print("check: DNNL_VERBOSE=", os.environ['DNNL_VERBOSE']) print( @@ -42,11 +42,11 @@ def check(): ) a_np = np.random.uniform(-2, 2, (10, 20, 30)).astype(np.float32) b_np = np.random.uniform(-5, 5, (10, 20, 30)).astype(np.float32) - helper = LayerHelper(fluid.unique_name.generate("test"), act="relu") + helper = LayerHelper(base.unique_name.generate("test"), act="relu") func = helper.append_activation - with fluid.dygraph.guard(fluid.core.CPUPlace()): - a = fluid.dygraph.to_variable(a_np) - b = fluid.dygraph.to_variable(b_np) + with base.dygraph.guard(base.core.CPUPlace()): + a = base.dygraph.to_variable(a_np) + b = base.dygraph.to_variable(b_np) y = paddle.add(x=a, y=b) y = paddle.matmul(x=y, y=b, transpose_y=True) res1 = func(y) diff --git a/test/mkldnn/check_flags_use_mkldnn.py b/test/mkldnn/check_flags_use_mkldnn.py index 07b4829743cd6..87b590bcd4617 100644 --- a/test/mkldnn/check_flags_use_mkldnn.py +++ b/test/mkldnn/check_flags_use_mkldnn.py @@ -16,9 +16,9 @@ import numpy as np -from paddle import fluid -from paddle.fluid.framework import _global_flags -from paddle.fluid.layer_helper import LayerHelper +from paddle import base +from paddle.base.framework import _global_flags +from paddle.base.layer_helper import LayerHelper def check(): @@ -27,15 +27,15 @@ def check(): _global_flags()["FLAGS_use_mkldnn"], ) print( - "check: fluid.get_flags('FLAGS_use_mkldnn')=", - fluid.get_flags(['FLAGS_use_mkldnn']), + "check: base.get_flags('FLAGS_use_mkldnn')=", + base.get_flags(['FLAGS_use_mkldnn']), ) print("check: DNNL_VERBOSE=", os.environ['DNNL_VERBOSE']) a_np = np.random.uniform(-2, 2, (10, 20, 30)).astype(np.float32) - helper = LayerHelper(fluid.unique_name.generate("test"), act="relu") + helper = LayerHelper(base.unique_name.generate("test"), act="relu") func = helper.append_activation - with fluid.dygraph.guard(fluid.core.CPUPlace()): - a = fluid.dygraph.to_variable(a_np) + with base.dygraph.guard(base.core.CPUPlace()): + a = base.dygraph.to_variable(a_np) res1 = func(a) res2 = np.maximum(a_np, 0) np.testing.assert_array_equal(res1.numpy(), res2) diff --git a/test/mkldnn/mkldnn_op_test.py b/test/mkldnn/mkldnn_op_test.py index 6e4776ab84c24..0a72ab8800dfb 100644 --- a/test/mkldnn/mkldnn_op_test.py +++ b/test/mkldnn/mkldnn_op_test.py @@ -14,8 +14,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core def __assert_close(test_case, tensor, np_array, msg, atol=1e-4): @@ -33,8 +33,8 @@ def check_if_mkldnn_primitives_exist_in_bwd( var_names = list(var_dict.keys()) ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -65,7 +65,7 @@ def check_if_mkldnn_primitives_exist_in_bwd( grad_var = block.desc.find_var(arg.encode('ascii')) grad_var.set_dtype(core.VarDesc.VarType.FP32) - exe = fluid.Executor(place) + exe = base.Executor(place) # Do at least 2 iterations for i in range(2): @@ -92,8 +92,8 @@ def check_if_mkldnn_batchnorm_primitives_exist_in_bwd( 'saved_variance', ] ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: block.create_var( @@ -145,7 +145,7 @@ def check_if_mkldnn_batchnorm_primitives_exist_in_bwd( grad_var.set_dtype(core.VarDesc.VarType.FP32) program._sync_with_cpp() - exe = fluid.Executor(place) + exe = base.Executor(place) # Do at least 2 iterations for i in range(2): out = exe.run( diff --git a/test/mkldnn/test_activation_bf16_mkldnn_op.py b/test/mkldnn/test_activation_bf16_mkldnn_op.py index a3ce4b74fcba0..6e002222ae429 100644 --- a/test/mkldnn/test_activation_bf16_mkldnn_op.py +++ b/test/mkldnn/test_activation_bf16_mkldnn_op.py @@ -21,7 +21,7 @@ from test_activation_op import TestActivation from test_gelu_op import gelu -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if_not_cpu_bf16() diff --git a/test/mkldnn/test_activation_mkldnn_op.py b/test/mkldnn/test_activation_mkldnn_op.py index fe147e9228f29..6887bd9059edd 100644 --- a/test/mkldnn/test_activation_mkldnn_op.py +++ b/test/mkldnn/test_activation_mkldnn_op.py @@ -45,7 +45,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core class TestMKLDNNReluDim2(TestRelu): @@ -123,7 +123,7 @@ def setUp(self): x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype) out = gelu(x, False) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -137,7 +137,7 @@ def setUp(self): x = np.random.uniform(-1, 1, []).astype(self.dtype) out = gelu(x, False) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -151,7 +151,7 @@ def setUp(self): x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype) out = gelu(x, True) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True, "approximate": True} @@ -268,7 +268,7 @@ def setUp(self): x[np.abs(x) < 0.005] = 0.02 out = np.maximum(x, 0) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -285,7 +285,7 @@ def setUp(self): x[np.abs(x) < 0.005] = 0.02 out = np.maximum(x, 0.02 * x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -310,7 +310,7 @@ def setUp(self): x = np.random.uniform(-1, 1, [2, 4, 3, 5]).astype(self.dtype) out = gelu(x, False) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -324,7 +324,7 @@ def setUp(self): x = np.random.uniform(-1, 1, [2, 4, 3, 5]).astype(self.dtype) out = gelu(x, True) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True, "approximate": True} @@ -435,7 +435,7 @@ def setUp(self): out = ref_hardswish(x, threshold, scale, offset) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -452,7 +452,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, [2, 4, 3, 5]).astype(self.dtype) out = x * np.tanh(np.log(1 + np.exp(x))) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -466,7 +466,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, []).astype(self.dtype) out = x * np.tanh(np.log(1 + np.exp(x))) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} @@ -501,7 +501,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, [2, 4, 3, 5]).astype(self.dtype) out = 1 / (1 + np.exp(-x)) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {"use_mkldnn": True} diff --git a/test/mkldnn/test_batch_norm_mkldnn_op.py b/test/mkldnn/test_batch_norm_mkldnn_op.py index 49703d0d1a4a4..a274990047f17 100644 --- a/test/mkldnn/test_batch_norm_mkldnn_op.py +++ b/test/mkldnn/test_batch_norm_mkldnn_op.py @@ -24,7 +24,7 @@ _reference_training, ) -from paddle.fluid import core +from paddle.base import core _set_use_system_allocator(True) diff --git a/test/mkldnn/test_cast_mkldnn_op.py b/test/mkldnn/test_cast_mkldnn_op.py index 2bf0bb6d70b1a..edf484b8aa9cd 100644 --- a/test/mkldnn/test_cast_mkldnn_op.py +++ b/test/mkldnn/test_cast_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_clip_mkldnn_op.py b/test/mkldnn/test_clip_mkldnn_op.py index d484ddd890efd..b7538cb45b18c 100644 --- a/test/mkldnn/test_clip_mkldnn_op.py +++ b/test/mkldnn/test_clip_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core class TestClipOneDNNOp(OpTest): diff --git a/test/mkldnn/test_concat_bf16_mkldnn_op.py b/test/mkldnn/test_concat_bf16_mkldnn_op.py index d2bf99452a832..f62886aca8951 100644 --- a/test/mkldnn/test_concat_bf16_mkldnn_op.py +++ b/test/mkldnn/test_concat_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_concat_mkldnn_op.py b/test/mkldnn/test_concat_mkldnn_op.py index a719a81a07610..906bb4c731305 100644 --- a/test/mkldnn/test_concat_mkldnn_op.py +++ b/test/mkldnn/test_concat_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from paddle import enable_static -from paddle.fluid import core +from paddle.base import core class TestConcatAxis0OneDNNOp(OpTest): diff --git a/test/mkldnn/test_conv2d_bf16_mkldnn_op.py b/test/mkldnn/test_conv2d_bf16_mkldnn_op.py index b1300028368af..acaaa7406471c 100644 --- a/test/mkldnn/test_conv2d_bf16_mkldnn_op.py +++ b/test/mkldnn/test_conv2d_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 from test_conv2d_op import TestConv2DOp, conv2d_forward_naive -from paddle.fluid import core +from paddle.base import core def conv2d_residual_naive(out, residual): @@ -91,14 +91,14 @@ def setUp(self): self.inputs = { 'Input': self.input, - 'Filter': OpTest.np_dtype_to_fluid_dtype( + 'Filter': OpTest.np_dtype_to_base_dtype( self.filter.astype(self.weight_type) ), } if self.fuse_residual: self.op_type = "fused_conv2d" - self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype( + self.inputs['ResidualData'] = OpTest.np_dtype_to_base_dtype( convert_float_to_uint16(self.input_residual) ) diff --git a/test/mkldnn/test_conv2d_int8_mkldnn_op.py b/test/mkldnn/test_conv2d_int8_mkldnn_op.py index 21519593c6da2..dc2fe60848148 100644 --- a/test/mkldnn/test_conv2d_int8_mkldnn_op.py +++ b/test/mkldnn/test_conv2d_int8_mkldnn_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest from test_conv2d_op import TestConv2DOp, conv2d_forward_naive -from paddle.fluid import core +from paddle.base import core def conv2d_forward_refer(input, filter, group, conv_param): @@ -148,11 +148,11 @@ def residual_helper(init_low, init_high, output_): output = np.round(output).astype(self.dsttype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input.astype(self.srctype)), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input.astype(self.srctype)), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } if self.fuse_residual: - self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype( + self.inputs['ResidualData'] = OpTest.np_dtype_to_base_dtype( input_residual ) diff --git a/test/mkldnn/test_conv2d_mkldnn_op.py b/test/mkldnn/test_conv2d_mkldnn_op.py index cd4b84f443813..9fbd4ea46471c 100644 --- a/test/mkldnn/test_conv2d_mkldnn_op.py +++ b/test/mkldnn/test_conv2d_mkldnn_op.py @@ -70,7 +70,7 @@ def setUp(self): output = conv2d_bias_naive(output, bias) output = output.astype(self.dtype) self.attrs['fuse_bias'] = self.fuse_bias - self.inputs['Bias'] = OpTest.np_dtype_to_fluid_dtype(bias) + self.inputs['Bias'] = OpTest.np_dtype_to_base_dtype(bias) if ( self.fuse_residual_connection @@ -84,7 +84,7 @@ def setUp(self): self.attrs[ 'fuse_residual_connection' ] = self.fuse_residual_connection - self.inputs['ResidualData'] = OpTest.np_dtype_to_fluid_dtype( + self.inputs['ResidualData'] = OpTest.np_dtype_to_base_dtype( input_residual ) diff --git a/test/mkldnn/test_conv2d_transpose_bf16_mkldnn_op.py b/test/mkldnn/test_conv2d_transpose_bf16_mkldnn_op.py index a807a43ff85af..53fa07cb42628 100644 --- a/test/mkldnn/test_conv2d_transpose_bf16_mkldnn_op.py +++ b/test/mkldnn/test_conv2d_transpose_bf16_mkldnn_op.py @@ -19,7 +19,7 @@ from test_conv2d_transpose_op import conv2dtranspose_forward_naive from paddle import enable_static -from paddle.fluid import core +from paddle.base import core def conv2d_bias_naive(out, bias): @@ -113,7 +113,7 @@ def setUp(self): self.inputs = { 'Input': input.view(self.input_type), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } if self.fuse_bias and self.bias_size is not None: @@ -121,7 +121,7 @@ def setUp(self): output = conv2d_bias_naive(output, bias) output = output.astype(np.float32) self.attrs['fuse_bias'] = self.fuse_bias - self.inputs['Bias'] = OpTest.np_dtype_to_fluid_dtype(bias) + self.inputs['Bias'] = OpTest.np_dtype_to_base_dtype(bias) if self.fuse_activation == "relu": output = np.maximum(output, 0).astype(np.float32) diff --git a/test/mkldnn/test_conv2d_transpose_mkldnn_op.py b/test/mkldnn/test_conv2d_transpose_mkldnn_op.py index 507071a090d28..6a1a012f4c8fa 100644 --- a/test/mkldnn/test_conv2d_transpose_mkldnn_op.py +++ b/test/mkldnn/test_conv2d_transpose_mkldnn_op.py @@ -71,7 +71,7 @@ def setUp(self): output = conv2d_bias_naive(output, bias) output = output.astype(self.dtype) self.attrs['fuse_bias'] = self.fuse_bias - self.inputs['Bias'] = OpTest.np_dtype_to_fluid_dtype(bias) + self.inputs['Bias'] = OpTest.np_dtype_to_base_dtype(bias) if self.fuse_activation == "relu": output = np.maximum(output, 0).astype(self.dtype) diff --git a/test/mkldnn/test_dequantize_mkldnn_op.py b/test/mkldnn/test_dequantize_mkldnn_op.py index 0406b0a9243b0..e2a49d6d4754a 100644 --- a/test/mkldnn/test_dequantize_mkldnn_op.py +++ b/test/mkldnn/test_dequantize_mkldnn_op.py @@ -40,7 +40,7 @@ def setUp(self): def prepare_input_output_bf16(self): output = np.random.random(self.input_size).astype(np.float32) input = convert_float_to_uint16(output) - self.inputs = {'Input': OpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'Input': OpTest.np_dtype_to_base_dtype(input)} self.outputs = {'Output': output} def prepare_input_int8(self): @@ -55,7 +55,7 @@ def prepare_input_int8(self): self.data_type ) - self.inputs = {'Input': OpTest.np_dtype_to_fluid_dtype(self.input)} + self.inputs = {'Input': OpTest.np_dtype_to_base_dtype(self.input)} self.attrs = {'Scale': self.scale, 'Shift': self.shift} def prepare_output_int8(self): diff --git a/test/mkldnn/test_elementwise_add_bf16_mkldnn_op.py b/test/mkldnn/test_elementwise_add_bf16_mkldnn_op.py index a2894d09a2613..d9d3bac981ced 100644 --- a/test/mkldnn/test_elementwise_add_bf16_mkldnn_op.py +++ b/test/mkldnn/test_elementwise_add_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_elementwise_div_mkldnn_op.py b/test/mkldnn/test_elementwise_div_mkldnn_op.py index 18cac1574e3bf..d6c545cc017f5 100644 --- a/test/mkldnn/test_elementwise_div_mkldnn_op.py +++ b/test/mkldnn/test_elementwise_div_mkldnn_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core -from paddle.fluid.framework import _current_expected_place +from paddle.base import core +from paddle.base.framework import _current_expected_place @OpTestTool.skip_if( @@ -34,8 +34,8 @@ def setUp(self): self.init_kernel_type() self.init_axis() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} diff --git a/test/mkldnn/test_elementwise_mul_bf16_mkldnn_op.py b/test/mkldnn/test_elementwise_mul_bf16_mkldnn_op.py index 45743a5ae7ad1..ac1e47dd868d1 100644 --- a/test/mkldnn/test_elementwise_mul_bf16_mkldnn_op.py +++ b/test/mkldnn/test_elementwise_mul_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_elementwise_sub_onednn_op.py b/test/mkldnn/test_elementwise_sub_onednn_op.py index fc9b536899e08..1340eac9c92b7 100644 --- a/test/mkldnn/test_elementwise_sub_onednn_op.py +++ b/test/mkldnn/test_elementwise_sub_onednn_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core -from paddle.fluid.framework import _current_expected_place +from paddle.base import core +from paddle.base.framework import _current_expected_place @OpTestTool.skip_if( @@ -34,8 +34,8 @@ def setUp(self): self.init_kernel_type() self.init_axis() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} diff --git a/test/mkldnn/test_expand_v2_mkldnn_op.py b/test/mkldnn/test_expand_v2_mkldnn_op.py index 17374b922e711..1ef44c8b27cb8 100644 --- a/test/mkldnn/test_expand_v2_mkldnn_op.py +++ b/test/mkldnn/test_expand_v2_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if( diff --git a/test/mkldnn/test_fc_bf16_mkldnn_op.py b/test/mkldnn/test_fc_bf16_mkldnn_op.py index 8db2458be1956..7bc726e677693 100644 --- a/test/mkldnn/test_fc_bf16_mkldnn_op.py +++ b/test/mkldnn/test_fc_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core def fully_connected_naive(input, weights, bias_data): diff --git a/test/mkldnn/test_flatten_mkldnn_op.py b/test/mkldnn/test_flatten_mkldnn_op.py index ba1fb8a252f40..b54a3974614f2 100644 --- a/test/mkldnn/test_flatten_mkldnn_op.py +++ b/test/mkldnn/test_flatten_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if_not_cpu_bf16() diff --git a/test/mkldnn/test_fused_vit_attention.py b/test/mkldnn/test_fused_vit_attention.py index c3718886544ef..1de329d7032b8 100644 --- a/test/mkldnn/test_fused_vit_attention.py +++ b/test/mkldnn/test_fused_vit_attention.py @@ -17,7 +17,7 @@ import paddle import paddle.incubate -from paddle.fluid import core +from paddle.base import core paddle.enable_static() np.random.seed(0) @@ -54,7 +54,7 @@ def test_fuse_resenet_unit(): graph = core.Graph(program.desc) core.get_pass("self_attention_fuse_pass").apply(graph) - after_program = paddle.fluid.framework.IrGraph(graph).to_program() + after_program = paddle.base.framework.IrGraph(graph).to_program() exe = paddle.static.Executor(place) exe.run(startup_program) diff --git a/test/mkldnn/test_fusion_gru_bf16_mkldnn_op.py b/test/mkldnn/test_fusion_gru_bf16_mkldnn_op.py index 611edbb64ed6b..0bac634467b5b 100644 --- a/test/mkldnn/test_fusion_gru_bf16_mkldnn_op.py +++ b/test/mkldnn/test_fusion_gru_bf16_mkldnn_op.py @@ -19,7 +19,7 @@ from test_fusion_gru_op import fusion_gru from test_fusion_lstm_op import ACTIVATION -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_fusion_lstm_bf16_mkldnn_op.py b/test/mkldnn/test_fusion_lstm_bf16_mkldnn_op.py index bf1e3215fa195..4ccd419d40731 100644 --- a/test/mkldnn/test_fusion_lstm_bf16_mkldnn_op.py +++ b/test/mkldnn/test_fusion_lstm_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from test_fusion_lstm_op import ACTIVATION, fusion_lstm -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_layer_norm_bf16_mkldnn_op.py b/test/mkldnn/test_layer_norm_bf16_mkldnn_op.py index d92bb83ce084f..6e28472d5e9e0 100644 --- a/test/mkldnn/test_layer_norm_bf16_mkldnn_op.py +++ b/test/mkldnn/test_layer_norm_bf16_mkldnn_op.py @@ -24,8 +24,8 @@ _reference_layer_norm_naive, ) -from paddle import enable_static, fluid -from paddle.fluid import core +from paddle import base, enable_static +from paddle.base import core np.random.random(123) @@ -75,8 +75,8 @@ def check_forward( var_names.append('bias') ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() # scale and bias are fp32 and other vars are of bf16 @@ -115,7 +115,7 @@ def check_forward( }, ) - exe = fluid.Executor(core.CPUPlace()) + exe = base.Executor(core.CPUPlace()) input_list = ['x_bf16'] if with_scale_bias: diff --git a/test/mkldnn/test_layer_norm_mkldnn_op.py b/test/mkldnn/test_layer_norm_mkldnn_op.py index b58677fa0a731..4533ccd05179d 100644 --- a/test/mkldnn/test_layer_norm_mkldnn_op.py +++ b/test/mkldnn/test_layer_norm_mkldnn_op.py @@ -20,8 +20,8 @@ import numpy as np from eager_op_test import OpTestTool, _set_use_system_allocator -from paddle import enable_static, fluid -from paddle.fluid import core +from paddle import base, enable_static +from paddle.base import core np.random.random(123) @@ -92,8 +92,8 @@ def check_forward( var_names.append('bias') ground_truth = {name: var_dict[name] for name in var_names} - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in ground_truth: @@ -122,7 +122,7 @@ def check_forward( }, ) - exe = fluid.Executor(core.CPUPlace()) + exe = base.Executor(core.CPUPlace()) input_list = ['x'] if with_scale_bias: diff --git a/test/mkldnn/test_log_softmax_mkldnn_op.py b/test/mkldnn/test_log_softmax_mkldnn_op.py index 98898a7ae6f23..7bed35162cec6 100644 --- a/test/mkldnn/test_log_softmax_mkldnn_op.py +++ b/test/mkldnn/test_log_softmax_mkldnn_op.py @@ -19,7 +19,7 @@ from test_log_softmax import ref_log_softmax import paddle -from paddle.fluid import core +from paddle.base import core class TestLogSoftmaxOneDNNOp(OpTest): diff --git a/test/mkldnn/test_matmul_bf16_mkldnn_op.py b/test/mkldnn/test_matmul_bf16_mkldnn_op.py index 19578e7eb3ac0..74b55b8db78bb 100644 --- a/test/mkldnn/test_matmul_bf16_mkldnn_op.py +++ b/test/mkldnn/test_matmul_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_matmul_v2_mkldnn_op.py b/test/mkldnn/test_matmul_v2_mkldnn_op.py index 51c63970b7fed..97dab61f9dc0a 100644 --- a/test/mkldnn/test_matmul_v2_mkldnn_op.py +++ b/test/mkldnn/test_matmul_v2_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def reference_matmul(X, Y, transpose_x=False, transpose_y=False): diff --git a/test/mkldnn/test_mul_int8_mkldnn_op.py b/test/mkldnn/test_mul_int8_mkldnn_op.py index 575ed8e7b26a6..4d3fd1afca472 100644 --- a/test/mkldnn/test_mul_int8_mkldnn_op.py +++ b/test/mkldnn/test_mul_int8_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, skip_check_grad_ci import paddle -from paddle.fluid import core +from paddle.base import core ''' test case for s8 * s8 diff --git a/test/mkldnn/test_mul_mkldnn_op.py b/test/mkldnn/test_mul_mkldnn_op.py index f8a09633dc3be..948addfc17751 100644 --- a/test/mkldnn/test_mul_mkldnn_op.py +++ b/test/mkldnn/test_mul_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if_not_cpu_bf16() diff --git a/test/mkldnn/test_onnx_format_quantization_mobilenetv1.py b/test/mkldnn/test_onnx_format_quantization_mobilenetv1.py index bf5c55ae93869..71e484f87569b 100644 --- a/test/mkldnn/test_onnx_format_quantization_mobilenetv1.py +++ b/test/mkldnn/test_onnx_format_quantization_mobilenetv1.py @@ -22,7 +22,7 @@ from PIL import Image import paddle -from paddle import fluid +from paddle import base from paddle.dataset.common import download from paddle.static.quantization import PostTrainingQuantization @@ -271,8 +271,8 @@ def generate_quantized_model( is_optimize_model=False, onnx_format=False, ): - place = fluid.CPUPlace() - exe = fluid.Executor(place) + place = base.CPUPlace() + exe = base.Executor(place) val_reader = val() ptq = PostTrainingQuantization( diff --git a/test/mkldnn/test_pool2d_bf16_mkldnn_op.py b/test/mkldnn/test_pool2d_bf16_mkldnn_op.py index 527c868999df2..0a1b04d157a9b 100644 --- a/test/mkldnn/test_pool2d_bf16_mkldnn_op.py +++ b/test/mkldnn/test_pool2d_bf16_mkldnn_op.py @@ -25,7 +25,7 @@ ) from paddle import enable_static -from paddle.fluid import core +from paddle.base import core def pool2d_backward_naive( diff --git a/test/mkldnn/test_pool2d_int8_mkldnn_op.py b/test/mkldnn/test_pool2d_int8_mkldnn_op.py index 4736895f99b49..47ed3728150be 100644 --- a/test/mkldnn/test_pool2d_int8_mkldnn_op.py +++ b/test/mkldnn/test_pool2d_int8_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from test_pool2d_op import TestPool2D_Op, max_pool2D_forward_naive -from paddle.fluid import core +from paddle.base import core class TestPool2DMKLDNNInt8_Op(TestPool2D_Op): @@ -48,7 +48,7 @@ def setUp(self): self.dtype, ) ).astype(self.dtype) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(input)} self.outputs = {'Out': output} def test_check_output(self): diff --git a/test/mkldnn/test_prelu_mkldnn_op.py b/test/mkldnn/test_prelu_mkldnn_op.py index c2079f0e4d9ce..a3264c646bc3e 100644 --- a/test/mkldnn/test_prelu_mkldnn_op.py +++ b/test/mkldnn/test_prelu_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core def ref_prelu(x, weight, mode): diff --git a/test/mkldnn/test_quantize_mkldnn_op.py b/test/mkldnn/test_quantize_mkldnn_op.py index c7fedaffa8636..22324e4b035b3 100644 --- a/test/mkldnn/test_quantize_mkldnn_op.py +++ b/test/mkldnn/test_quantize_mkldnn_op.py @@ -48,7 +48,7 @@ def prepare_input(self): 'float32' ) - self.inputs = {'Input': OpTest.np_dtype_to_fluid_dtype(self.input)} + self.inputs = {'Input': OpTest.np_dtype_to_base_dtype(self.input)} self.attrs = { 'Scale': self.scale, 'Shift': self.shift, diff --git a/test/mkldnn/test_reduce_bf16_mkldnn_op.py b/test/mkldnn/test_reduce_bf16_mkldnn_op.py index 5001a03372e75..5407efd05a48d 100644 --- a/test/mkldnn/test_reduce_bf16_mkldnn_op.py +++ b/test/mkldnn/test_reduce_bf16_mkldnn_op.py @@ -23,7 +23,7 @@ ) import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/mkldnn/test_requantize_mkldnn_op.py b/test/mkldnn/test_requantize_mkldnn_op.py index 52f87b94cf9e5..fbe5d075422d1 100644 --- a/test/mkldnn/test_requantize_mkldnn_op.py +++ b/test/mkldnn/test_requantize_mkldnn_op.py @@ -18,8 +18,8 @@ from eager_op_test import OpTest from mkldnn_op_test import format_reorder -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class TestReQuantizeOp(OpTest): @@ -53,7 +53,7 @@ def prepare_input(self): self.input_data_type ) - self.inputs = {'Input': OpTest.np_dtype_to_fluid_dtype(self.input)} + self.inputs = {'Input': OpTest.np_dtype_to_base_dtype(self.input)} self.attrs = { 'Scale_in': self.scale_in, 'Scale_out': self.scale_out, @@ -339,8 +339,8 @@ def test_check_output(self): "input": self.input, "output": self.output, } - program = fluid.Program() - with fluid.program_guard(program): + program = base.Program() + with base.program_guard(program): block = program.global_block() for name in variables: block.create_var( @@ -360,7 +360,7 @@ def test_check_output(self): }, ) place = core.CPUPlace() - exe = fluid.Executor(place) + exe = base.Executor(place) for i in range(2): out = exe.run( program, diff --git a/test/mkldnn/test_reshape_bf16_op.py b/test/mkldnn/test_reshape_bf16_op.py index d2350b9d01386..76b509219b105 100644 --- a/test/mkldnn/test_reshape_bf16_op.py +++ b/test/mkldnn/test_reshape_bf16_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_reshape_mkldnn_op.py b/test/mkldnn/test_reshape_mkldnn_op.py index b62aef0c1deff..ca06b5fd4c748 100644 --- a/test/mkldnn/test_reshape_mkldnn_op.py +++ b/test/mkldnn/test_reshape_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/mkldnn/test_scale_bf16_mkldnn_op.py b/test/mkldnn/test_scale_bf16_mkldnn_op.py index b1d3aa09510a9..a50e47a712cb4 100644 --- a/test/mkldnn/test_scale_bf16_mkldnn_op.py +++ b/test/mkldnn/test_scale_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_shape_mkldnn_op.py b/test/mkldnn/test_shape_mkldnn_op.py index 14c869c4cb36f..4325062628e95 100644 --- a/test/mkldnn/test_shape_mkldnn_op.py +++ b/test/mkldnn/test_shape_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool import paddle -from paddle.fluid import core +from paddle.base import core class TestShape3DFP32OneDNNOp(OpTest): diff --git a/test/mkldnn/test_shuffle_channel_mkldnn_op.py b/test/mkldnn/test_shuffle_channel_mkldnn_op.py index ea8640ebbc520..d363bef88606b 100644 --- a/test/mkldnn/test_shuffle_channel_mkldnn_op.py +++ b/test/mkldnn/test_shuffle_channel_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if_not_cpu_bf16() diff --git a/test/mkldnn/test_slice_mkldnn_op.py b/test/mkldnn/test_slice_mkldnn_op.py index b18313b941238..b733a11ca912c 100644 --- a/test/mkldnn/test_slice_mkldnn_op.py +++ b/test/mkldnn/test_slice_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if( diff --git a/test/mkldnn/test_softmax_bf16_mkldnn_op.py b/test/mkldnn/test_softmax_bf16_mkldnn_op.py index a7fdc1de413f1..2e04100ec31ad 100644 --- a/test/mkldnn/test_softmax_bf16_mkldnn_op.py +++ b/test/mkldnn/test_softmax_bf16_mkldnn_op.py @@ -26,7 +26,7 @@ ) from paddle import enable_static -from paddle.fluid import core +from paddle.base import core def stable_softmax(x): diff --git a/test/mkldnn/test_softmax_mkldnn_op.py b/test/mkldnn/test_softmax_mkldnn_op.py index 2237161deba8b..777dfb0cb02dc 100644 --- a/test/mkldnn/test_softmax_mkldnn_op.py +++ b/test/mkldnn/test_softmax_mkldnn_op.py @@ -27,7 +27,7 @@ TestSoftmaxOp_ZeroDim1, ) -from paddle.fluid import core +from paddle.base import core def stable_softmax(x): @@ -56,7 +56,7 @@ def setUp(self): x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) out = np.apply_along_axis(stable_softmax, self.axis, x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = { 'axis': self.axis, diff --git a/test/mkldnn/test_split_bf16_mkldnn_op.py b/test/mkldnn/test_split_bf16_mkldnn_op.py index f1f79192c01a3..81e25ce43b9a9 100644 --- a/test/mkldnn/test_split_bf16_mkldnn_op.py +++ b/test/mkldnn/test_split_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_squeeze2_mkldnn_op.py b/test/mkldnn/test_squeeze2_mkldnn_op.py index fd02578e8a1a2..b1efad9690bb6 100644 --- a/test/mkldnn/test_squeeze2_mkldnn_op.py +++ b/test/mkldnn/test_squeeze2_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool, convert_float_to_uint16 import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if( diff --git a/test/mkldnn/test_stack_mkldnn_op.py b/test/mkldnn/test_stack_mkldnn_op.py index 4cc93e502b405..7a04ea38be9a5 100644 --- a/test/mkldnn/test_stack_mkldnn_op.py +++ b/test/mkldnn/test_stack_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, OpTestTool import paddle -from paddle.fluid import core +from paddle.base import core @OpTestTool.skip_if_not_cpu() diff --git a/test/mkldnn/test_sum_bf16_mkldnn_op.py b/test/mkldnn/test_sum_bf16_mkldnn_op.py index dc570c7f4f93c..af622b8cff5d4 100644 --- a/test/mkldnn/test_sum_bf16_mkldnn_op.py +++ b/test/mkldnn/test_sum_bf16_mkldnn_op.py @@ -19,7 +19,7 @@ from test_sum_op import TestSumOp from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_sum_mkldnn_op.py b/test/mkldnn/test_sum_mkldnn_op.py index 030158882a2b1..6750f1a79c7ce 100644 --- a/test/mkldnn/test_sum_mkldnn_op.py +++ b/test/mkldnn/test_sum_mkldnn_op.py @@ -18,7 +18,7 @@ from op import Operator from test_sum_op import TestSumOp -from paddle.fluid import core +from paddle.base import core class TestSumMKLDNN(TestSumOp): diff --git a/test/mkldnn/test_transpose_bf16_mkldnn_op.py b/test/mkldnn/test_transpose_bf16_mkldnn_op.py index d8a9519bf788d..d0817ab103db6 100644 --- a/test/mkldnn/test_transpose_bf16_mkldnn_op.py +++ b/test/mkldnn/test_transpose_bf16_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest, convert_float_to_uint16 from paddle import enable_static -from paddle.fluid import core +from paddle.base import core @unittest.skipIf( diff --git a/test/mkldnn/test_transpose_int8_mkldnn_op.py b/test/mkldnn/test_transpose_int8_mkldnn_op.py index 30df0eafb0415..282c6dd94acea 100644 --- a/test/mkldnn/test_transpose_int8_mkldnn_op.py +++ b/test/mkldnn/test_transpose_int8_mkldnn_op.py @@ -18,7 +18,7 @@ from eager_op_test import OpTest from mkldnn_op_test import format_reorder -from paddle.fluid import core +from paddle.base import core class TestTransposeOp(OpTest): diff --git a/test/prim/composite_ops/test_composite_batch_norm.py b/test/prim/composite_ops/test_composite_batch_norm.py index 9a25c6dc1d6d4..c74097b6e2313 100644 --- a/test/prim/composite_ops/test_composite_batch_norm.py +++ b/test/prim/composite_ops/test_composite_batch_norm.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import core, framework +from paddle.base import core, framework from paddle.incubate.autograd import primapi from paddle.nn import BatchNorm from paddle.tensor import ones # noqa: F401 diff --git a/test/prim/composite_ops/test_composite_batch_norm_grad.py b/test/prim/composite_ops/test_composite_batch_norm_grad.py index f8699f6c84c31..f23fe76456994 100644 --- a/test/prim/composite_ops/test_composite_batch_norm_grad.py +++ b/test/prim/composite_ops/test_composite_batch_norm_grad.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi np.random.seed(2023) diff --git a/test/prim/composite_ops/test_composite_dropout.py b/test/prim/composite_ops/test_composite_dropout.py index d1dabef0d04ff..1d835f78b2037 100644 --- a/test/prim/composite_ops/test_composite_dropout.py +++ b/test/prim/composite_ops/test_composite_dropout.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi np.random.seed(2023) diff --git a/test/prim/composite_ops/test_composite_gelu.py b/test/prim/composite_ops/test_composite_gelu.py index ef214c9a81f7f..89c721be8a504 100644 --- a/test/prim/composite_ops/test_composite_gelu.py +++ b/test/prim/composite_ops/test_composite_gelu.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/composite_ops/test_composite_gelu_grad.py b/test/prim/composite_ops/test_composite_gelu_grad.py index ca195fffce2a1..c87e985ebe8ff 100644 --- a/test/prim/composite_ops/test_composite_gelu_grad.py +++ b/test/prim/composite_ops/test_composite_gelu_grad.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/composite_ops/test_composite_layer_norm.py b/test/prim/composite_ops/test_composite_layer_norm.py index 6bfefc608eaf8..1f3fc2ccf31cd 100644 --- a/test/prim/composite_ops/test_composite_layer_norm.py +++ b/test/prim/composite_ops/test_composite_layer_norm.py @@ -19,8 +19,8 @@ import paddle from paddle import _C_ops -from paddle.fluid import core, framework -from paddle.fluid.layer_helper import LayerHelper +from paddle.base import core, framework +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode from paddle.incubate.autograd import primapi from paddle.nn import LayerNorm @@ -70,7 +70,7 @@ def layer_norm_wrapper( # create output helper = LayerHelper('layer_norm', **locals()) - from paddle.fluid.data_feeder import convert_dtype + from paddle.base.data_feeder import convert_dtype param_dtype = ( x.dtype if convert_dtype(x.dtype) != 'float16' else 'float32' diff --git a/test/prim/composite_ops/test_composite_layer_norm_grad.py b/test/prim/composite_ops/test_composite_layer_norm_grad.py index 61f34b4a311fd..06940b82fc704 100644 --- a/test/prim/composite_ops/test_composite_layer_norm_grad.py +++ b/test/prim/composite_ops/test_composite_layer_norm_grad.py @@ -21,7 +21,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi TOLERANCE_NUMPY = { diff --git a/test/prim/composite_ops/test_composite_mean.py b/test/prim/composite_ops/test_composite_mean.py index a1ad278b9e536..01d8feb72ba90 100644 --- a/test/prim/composite_ops/test_composite_mean.py +++ b/test/prim/composite_ops/test_composite_mean.py @@ -19,7 +19,7 @@ import paddle from paddle import tensor -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/composite_ops/test_composite_mean_grad.py b/test/prim/composite_ops/test_composite_mean_grad.py index ae1bfbc56af08..ba952ff3258bd 100644 --- a/test/prim/composite_ops/test_composite_mean_grad.py +++ b/test/prim/composite_ops/test_composite_mean_grad.py @@ -19,7 +19,7 @@ import paddle from paddle import tensor -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/composite_ops/test_composite_relu_custom_vjp.py b/test/prim/composite_ops/test_composite_relu_custom_vjp.py index 644d4956573de..af99b78d36d1e 100644 --- a/test/prim/composite_ops/test_composite_relu_custom_vjp.py +++ b/test/prim/composite_ops/test_composite_relu_custom_vjp.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def generate_data(shape, dtype="float32"): diff --git a/test/prim/composite_ops/test_composite_softmax.py b/test/prim/composite_ops/test_composite_softmax.py index a9cc804a1a4ec..ccd918f045b57 100644 --- a/test/prim/composite_ops/test_composite_softmax.py +++ b/test/prim/composite_ops/test_composite_softmax.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core, framework +from paddle.base import core, framework from paddle.incubate.autograd import primapi diff --git a/test/prim/composite_ops/test_composite_softmax_custom_vjp.py b/test/prim/composite_ops/test_composite_softmax_custom_vjp.py index 97bf177f7e296..d89574f2d521f 100644 --- a/test/prim/composite_ops/test_composite_softmax_custom_vjp.py +++ b/test/prim/composite_ops/test_composite_softmax_custom_vjp.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def generate_data(shape, dtype="float32"): diff --git a/test/prim/composite_ops/test_composite_softmax_grad.py b/test/prim/composite_ops/test_composite_softmax_grad.py index 3e491984955f0..bf228b569a9c3 100644 --- a/test/prim/composite_ops/test_composite_softmax_grad.py +++ b/test/prim/composite_ops/test_composite_softmax_grad.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/model/bert.py b/test/prim/model/bert.py index 227d0cdcaf504..f7cf05f7ca243 100644 --- a/test/prim/model/bert.py +++ b/test/prim/model/bert.py @@ -21,8 +21,8 @@ import paddle import paddle.nn.functional as F from paddle import Tensor, nn +from paddle.base.data_feeder import convert_dtype from paddle.distributed.fleet.utils import recompute -from paddle.fluid.data_feeder import convert_dtype from paddle.io import DataLoader, Dataset from paddle.nn import MultiHeadAttention diff --git a/test/prim/model/test_bert_cinn.py b/test/prim/model/test_bert_cinn.py index 9651859a48b49..3ae1bcb27aeea 100644 --- a/test/prim/model/test_bert_cinn.py +++ b/test/prim/model/test_bert_cinn.py @@ -20,9 +20,9 @@ from bert import Bert, BertPretrainingCriterion, create_pretraining_dataset import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.dataset.common import DATA_HOME, download -from paddle.fluid import core SEED = 2023 BATCH_SIZE = 2 @@ -56,7 +56,7 @@ def train(to_static, enable_prim, enable_cinn): paddle.set_device('gpu') else: paddle.set_device('cpu') - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) np.random.seed(SEED) paddle.seed(SEED) diff --git a/test/prim/model/test_bert_prim.py b/test/prim/model/test_bert_prim.py index 2c55eeba8c9ff..74a65e2f0761c 100644 --- a/test/prim/model/test_bert_prim.py +++ b/test/prim/model/test_bert_prim.py @@ -20,9 +20,9 @@ from bert import Bert, BertPretrainingCriterion, create_pretraining_dataset import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.dataset.common import DATA_HOME, download -from paddle.fluid import core SEED = 2023 BATCH_SIZE = 2 @@ -55,7 +55,7 @@ def train(to_static, enable_prim, enable_cinn): paddle.set_device('gpu') else: paddle.set_device('cpu') - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) np.random.seed(SEED) paddle.seed(SEED) diff --git a/test/prim/model/test_bert_prim_cinn.py b/test/prim/model/test_bert_prim_cinn.py index 3a8634fbdc099..46e8d63b7a572 100644 --- a/test/prim/model/test_bert_prim_cinn.py +++ b/test/prim/model/test_bert_prim_cinn.py @@ -20,9 +20,9 @@ from bert import Bert, BertPretrainingCriterion, create_pretraining_dataset import paddle -from paddle import fluid +from paddle import base +from paddle.base import core from paddle.dataset.common import DATA_HOME, download -from paddle.fluid import core SEED = 2023 BATCH_SIZE = 2 @@ -56,7 +56,7 @@ def train(to_static, enable_prim, enable_cinn): paddle.set_device('gpu') else: paddle.set_device('cpu') - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) np.random.seed(SEED) paddle.seed(SEED) diff --git a/test/prim/model/test_comp_model_simple_net.py b/test/prim/model/test_comp_model_simple_net.py index 27b300e9afd44..e9b5dcf2f7fcb 100644 --- a/test/prim/model/test_comp_model_simple_net.py +++ b/test/prim/model/test_comp_model_simple_net.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework @param.parameterized_class( diff --git a/test/prim/model/test_prim_simplenet_cinn.py b/test/prim/model/test_prim_simplenet_cinn.py index 8a4a0861b0d86..6482e849560e0 100644 --- a/test/prim/model/test_prim_simplenet_cinn.py +++ b/test/prim/model/test_prim_simplenet_cinn.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.nn import BatchNorm np.random.seed(2023) diff --git a/test/prim/model/test_resnet_cinn.py b/test/prim/model/test_resnet_cinn.py index b2d6004aa2f7b..ef932603f8a58 100644 --- a/test/prim/model/test_resnet_cinn.py +++ b/test/prim/model/test_resnet_cinn.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.vision.models import resnet50 SEED = 2020 @@ -42,7 +42,7 @@ # 8.438933372497559, # 10.305074691772461, -# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the fluid version. +# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the base version. # The results in ci as as follows: DY2ST_CINN_GT = [ 5.847333908081055, @@ -170,7 +170,7 @@ def train(to_static, enable_prim, enable_cinn): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) dataset = TransedFlowerDataSet( reader_decorator(paddle.dataset.flowers.train(use_xmap=False)), diff --git a/test/prim/model/test_resnet_prim.py b/test/prim/model/test_resnet_prim.py index c423702a31db0..de81f2b78b650 100644 --- a/test/prim/model/test_resnet_prim.py +++ b/test/prim/model/test_resnet_prim.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.vision.models import resnet50 SEED = 2020 @@ -43,7 +43,7 @@ # 10.256929397583008, # ] -# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the fluid version. +# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the base version. # The results in ci as as follows: DY2ST_PRIM_GT = [ 5.847333908081055, @@ -171,7 +171,7 @@ def train(to_static, enable_prim, enable_cinn): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) dataset = TransedFlowerDataSet( reader_decorator(paddle.dataset.flowers.train(use_xmap=False)), diff --git a/test/prim/model/test_resnet_prim_cinn.py b/test/prim/model/test_resnet_prim_cinn.py index d3ad08e80c51e..933da8fcf105c 100644 --- a/test/prim/model/test_resnet_prim_cinn.py +++ b/test/prim/model/test_resnet_prim_cinn.py @@ -18,8 +18,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core from paddle.vision.models import resnet50 SEED = 2020 @@ -43,7 +43,7 @@ # 9.919631958007812, # ] -# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the fluid version. +# note: Version 2.0 momentum is fused to OP when L2Decay is available, and the results are different from the base version. # The results in ci as as follows: DY2ST_PRIM_CINN_GT = [ 5.847333908081055, @@ -171,7 +171,7 @@ def train(to_static, enable_prim, enable_cinn): np.random.seed(SEED) paddle.seed(SEED) paddle.framework.random._manual_program_seed(SEED) - fluid.core._set_prim_all_enabled(enable_prim) + base.core._set_prim_all_enabled(enable_prim) dataset = TransedFlowerDataSet( reader_decorator(paddle.dataset.flowers.train(use_xmap=False)), diff --git a/test/prim/new_ir_prim/test_vjp_prim.py b/test/prim/new_ir_prim/test_vjp_prim.py index 22309a08823ec..c6244892bfa5a 100644 --- a/test/prim/new_ir_prim/test_vjp_prim.py +++ b/test/prim/new_ir_prim/test_vjp_prim.py @@ -16,7 +16,7 @@ import paddle from paddle import ir -from paddle.fluid.core import call_vjp +from paddle.base.core import call_vjp paddle.enable_static() diff --git a/test/prim/prim/flags/test_prim_flags.py b/test/prim/prim/flags/test_prim_flags.py index db736e95a65ac..610d8acdd6315 100644 --- a/test/prim/prim/flags/test_prim_flags.py +++ b/test/prim/prim/flags/test_prim_flags.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.framework import core +from paddle.base import core from paddle.incubate.autograd import primapi diff --git a/test/prim/prim/flags/test_prim_flags_case.py b/test/prim/prim/flags/test_prim_flags_case.py index 30fe2af5621f9..d244aca1faca2 100644 --- a/test/prim/prim/flags/test_prim_flags_case.py +++ b/test/prim/prim/flags/test_prim_flags_case.py @@ -17,7 +17,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/eager/test_comp_eager_add_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_add_grad.py index b50e49a3e4443..6348514109801 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_add_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_add_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_batch_norm_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_batch_norm_grad.py index 5fa9599097398..720653b3d0240 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_batch_norm_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_batch_norm_grad.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core np.random.seed(2023) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_cast_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_cast_grad.py index b8c47466e8584..6547352b2b001 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_cast_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_cast_grad.py @@ -19,7 +19,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( @@ -67,7 +67,7 @@ def desired(primal, cotangent): actual = actual(self.primal, self.cotangent) desired = desired(self.primal, self.cotangent) - from paddle.fluid.data_feeder import _PADDLE_DTYPE_2_NUMPY_DTYPE + from paddle.base.data_feeder import _PADDLE_DTYPE_2_NUMPY_DTYPE self.assertEqual( _PADDLE_DTYPE_2_NUMPY_DTYPE[actual[0].dtype], desired.dtype diff --git a/test/prim/prim/vjp/eager/test_comp_eager_div_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_div_grad.py index 6546776a20774..e838bf2a5f466 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_div_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_div_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_exp_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_exp_grad.py index 172a37956c883..e7cb5f9fd002d 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_exp_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_exp_grad.py @@ -20,7 +20,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/eager/test_comp_eager_expand_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_expand_grad.py index 1991bd06f0cf4..e0dc1736b21fa 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_expand_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_expand_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/eager/test_comp_eager_gather_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_gather_grad.py index 7d71e5187f2c3..8ca9b3294c018 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_gather_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_gather_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/eager/test_comp_eager_matmul_double_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_matmul_double_grad.py index 3d24604419dee..ce94cc1eb2a50 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_matmul_double_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_matmul_double_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_multiply_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_multiply_grad.py index 207e3f414f256..86bf36eb6a72b 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_multiply_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_multiply_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/eager/test_comp_eager_reshape_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_reshape_grad.py index e98f8ba58c3cd..3395aecf68ea2 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_reshape_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_reshape_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_sigmoid_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_sigmoid_grad.py index 6640a7e8d3ddd..7693c6417eeec 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_sigmoid_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_sigmoid_grad.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/eager/test_comp_eager_sqrt_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_sqrt_grad.py index 4f6dc8b2ada55..33dd31e7c9ea1 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_sqrt_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_sqrt_grad.py @@ -20,7 +20,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_sub_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_sub_grad.py index 62aa0e936f758..89eb8f5186f7a 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_sub_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_sub_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_sum_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_sum_grad.py index 9cade52686fe1..9b522cde3b729 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_sum_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_sum_grad.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def actual(primal, cotangent, axis, keep_dim): diff --git a/test/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py index b919ea3e95271..687ce527469ff 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_tanh_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/eager/test_comp_eager_transpose_grad.py b/test/prim/prim/vjp/eager/test_comp_eager_transpose_grad.py index b4a68059a5334..79996d0373b6b 100644 --- a/test/prim/prim/vjp/eager/test_comp_eager_transpose_grad.py +++ b/test/prim/prim/vjp/eager/test_comp_eager_transpose_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core.set_prim_eager_enabled(True) diff --git a/test/prim/prim/vjp/static/test_comp_add_grad.py b/test/prim/prim/vjp/static/test_comp_add_grad.py index 9da9e7131d1a9..211564f2cd7b0 100644 --- a/test/prim/prim/vjp/static/test_comp_add_grad.py +++ b/test/prim/prim/vjp/static/test_comp_add_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_add_tanh_grad.py b/test/prim/prim/vjp/static/test_comp_add_tanh_grad.py index 520aef634b6fa..6320395e9ee4e 100644 --- a/test/prim/prim/vjp/static/test_comp_add_tanh_grad.py +++ b/test/prim/prim/vjp/static/test_comp_add_tanh_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_batch_norm_grad.py b/test/prim/prim/vjp/static/test_comp_batch_norm_grad.py index 5f084bab8617c..b8f25b5131203 100644 --- a/test/prim/prim/vjp/static/test_comp_batch_norm_grad.py +++ b/test/prim/prim/vjp/static/test_comp_batch_norm_grad.py @@ -18,7 +18,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core, framework +from paddle.base import core, framework np.random.seed(2023) diff --git a/test/prim/prim/vjp/static/test_comp_cast_grad.py b/test/prim/prim/vjp/static/test_comp_cast_grad.py index 850ad82220165..ce0bcbd7895f2 100644 --- a/test/prim/prim/vjp/static/test_comp_cast_grad.py +++ b/test/prim/prim/vjp/static/test_comp_cast_grad.py @@ -19,7 +19,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_div_grad.py b/test/prim/prim/vjp/static/test_comp_div_grad.py index 844d30894de18..f0f73d2002438 100644 --- a/test/prim/prim/vjp/static/test_comp_div_grad.py +++ b/test/prim/prim/vjp/static/test_comp_div_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_exp_grad.py b/test/prim/prim/vjp/static/test_comp_exp_grad.py index 2e720f6934f5c..c90c63b006769 100644 --- a/test/prim/prim/vjp/static/test_comp_exp_grad.py +++ b/test/prim/prim/vjp/static/test_comp_exp_grad.py @@ -20,7 +20,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/static/test_comp_expand_grad.py b/test/prim/prim/vjp/static/test_comp_expand_grad.py index 2772719a81820..4d12c4a77c968 100644 --- a/test/prim/prim/vjp/static/test_comp_expand_grad.py +++ b/test/prim/prim/vjp/static/test_comp_expand_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/static/test_comp_gather_grad.py b/test/prim/prim/vjp/static/test_comp_gather_grad.py index 77693dd1071da..c4550dd13cc02 100644 --- a/test/prim/prim/vjp/static/test_comp_gather_grad.py +++ b/test/prim/prim/vjp/static/test_comp_gather_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework np.random.seed(2023) diff --git a/test/prim/prim/vjp/static/test_comp_matmul_double_grad.py b/test/prim/prim/vjp/static/test_comp_matmul_double_grad.py index 809a4ed64d71a..2bebdcb10772d 100644 --- a/test/prim/prim/vjp/static/test_comp_matmul_double_grad.py +++ b/test/prim/prim/vjp/static/test_comp_matmul_double_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core core._set_prim_backward_enabled(True) diff --git a/test/prim/prim/vjp/static/test_comp_multiply_grad.py b/test/prim/prim/vjp/static/test_comp_multiply_grad.py index 4befaf48ca90e..d824bcf9ff4f1 100644 --- a/test/prim/prim/vjp/static/test_comp_multiply_grad.py +++ b/test/prim/prim/vjp/static/test_comp_multiply_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework @param.parameterized_class( diff --git a/test/prim/prim/vjp/static/test_comp_reshape_grad.py b/test/prim/prim/vjp/static/test_comp_reshape_grad.py index 4523e4af4bf44..7577c29b251cd 100644 --- a/test/prim/prim/vjp/static/test_comp_reshape_grad.py +++ b/test/prim/prim/vjp/static/test_comp_reshape_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_sigmoid_grad.py b/test/prim/prim/vjp/static/test_comp_sigmoid_grad.py index 43a1e0b14619b..cca9702dd03bc 100644 --- a/test/prim/prim/vjp/static/test_comp_sigmoid_grad.py +++ b/test/prim/prim/vjp/static/test_comp_sigmoid_grad.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import core +from paddle.base import core @param.parameterized_class( diff --git a/test/prim/prim/vjp/static/test_comp_sqrt_grad.py b/test/prim/prim/vjp/static/test_comp_sqrt_grad.py index a3b854fcc296b..09789ef6602ca 100644 --- a/test/prim/prim/vjp/static/test_comp_sqrt_grad.py +++ b/test/prim/prim/vjp/static/test_comp_sqrt_grad.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core core._set_prim_backward_enabled(True) diff --git a/test/prim/prim/vjp/static/test_comp_sub_grad.py b/test/prim/prim/vjp/static/test_comp_sub_grad.py index 87bd6ff0b3602..49b1e33e3c049 100644 --- a/test/prim/prim/vjp/static/test_comp_sub_grad.py +++ b/test/prim/prim/vjp/static/test_comp_sub_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core +from paddle.base import core def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/static/test_comp_sum_grad.py b/test/prim/prim/vjp/static/test_comp_sum_grad.py index 4301ff7b07b9a..d2fd37362b289 100644 --- a/test/prim/prim/vjp/static/test_comp_sum_grad.py +++ b/test/prim/prim/vjp/static/test_comp_sum_grad.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def actual(primal, cotangent, axis, keep_dim): diff --git a/test/prim/prim/vjp/static/test_comp_tanh_grad.py b/test/prim/prim/vjp/static/test_comp_tanh_grad.py index 869a36e806695..43edf96c3aff0 100644 --- a/test/prim/prim/vjp/static/test_comp_tanh_grad.py +++ b/test/prim/prim/vjp/static/test_comp_tanh_grad.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid import core +from paddle.base import core core._set_prim_backward_enabled(True) diff --git a/test/prim/prim/vjp/static/test_comp_transpose_grad.py b/test/prim/prim/vjp/static/test_comp_transpose_grad.py index 7be0be582fd3b..2f7cb85e3145d 100644 --- a/test/prim/prim/vjp/static/test_comp_transpose_grad.py +++ b/test/prim/prim/vjp/static/test_comp_transpose_grad.py @@ -18,7 +18,7 @@ import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework def apply_to_static(net, use_cinn): diff --git a/test/prim/prim/vjp/test_comp_high_grad.py b/test/prim/prim/vjp/test_comp_high_grad.py index c080d4d7231a1..96762679df519 100644 --- a/test/prim/prim/vjp/test_comp_high_grad.py +++ b/test/prim/prim/vjp/test_comp_high_grad.py @@ -22,8 +22,8 @@ from decorator_helper import prog_scope import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core @param.parameterized_class( @@ -75,7 +75,7 @@ def func_double(self, place): y_arr = np.random.uniform(-2, 2, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.double_grad_check( @@ -101,7 +101,7 @@ def func_triple(self, place): y_arr = np.random.uniform(-1, 1, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.triple_grad_check( @@ -114,9 +114,9 @@ def func_triple(self, place): def test_high_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func_double(p) self.func_triple(p) @@ -171,7 +171,7 @@ def func_double(self, place): y_arr = np.random.uniform(-2, 2, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.double_grad_check( @@ -201,7 +201,7 @@ def func_triple(self, place): y_arr = np.random.uniform(-2, 2, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.triple_grad_check( @@ -218,9 +218,9 @@ def func_triple(self, place): def test_high_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func_double(p) self.func_triple(p) @@ -275,7 +275,7 @@ def func_double(self, place): y_arr = np.random.uniform(-2, 2, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.double_grad_check( @@ -305,7 +305,7 @@ def func_triple(self, place): y_arr = np.random.uniform(-1, 1, shape2).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 y_arr[np.abs(y_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.triple_grad_check( @@ -322,9 +322,9 @@ def func_triple(self, place): def test_high_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func_double(p) self.func_triple(p) @@ -360,7 +360,7 @@ def func_double(self, place): x_arr[np.abs(x_arr) < 0.005] = 0.002 # silu double grad only has CompositeOpMaker,don't need set prim_flag - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.double_grad_check( @@ -386,7 +386,7 @@ def func_triple(self, place): out = paddle.nn.functional.silu(x) x_arr = np.random.uniform(-1, 1, shape1).astype(dtype) x_arr[np.abs(x_arr) < 0.005] = 0.002 - from paddle.fluid import core + from paddle.base import core core._set_prim_backward_enabled(True) gradient_checker.triple_grad_check( @@ -403,9 +403,9 @@ def func_triple(self, place): def test_high_grad(self): paddle.enable_static() - places = [fluid.CPUPlace()] + places = [base.CPUPlace()] if core.is_compiled_with_cuda(): - places.append(fluid.CUDAPlace(0)) + places.append(base.CUDAPlace(0)) for p in places: self.func_double(p) self.func_triple(p) diff --git a/test/prim/process/test_check_inputs.py b/test/prim/process/test_check_inputs.py index bf1d3e7a5064f..631da96cc8b23 100644 --- a/test/prim/process/test_check_inputs.py +++ b/test/prim/process/test_check_inputs.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core def fn(x, shape): diff --git a/test/prim/process/test_copy_op.py b/test/prim/process/test_copy_op.py index de208f1163262..e91597c01abc1 100644 --- a/test/prim/process/test_copy_op.py +++ b/test/prim/process/test_copy_op.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.incubate.autograd import primapi paddle.framework.random._manual_program_seed(2023) diff --git a/test/prim/process/test_prim_amp.py b/test/prim/process/test_prim_amp.py index 8c81f938679e1..8a632c13a4e07 100644 --- a/test/prim/process/test_prim_amp.py +++ b/test/prim/process/test_prim_amp.py @@ -19,7 +19,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import core, framework +from paddle.base import core, framework from paddle.nn import BatchNorm np.random.seed(2023) diff --git a/test/prim/test_comp_custom_vjp.py b/test/prim/test_comp_custom_vjp.py index ef4418f6934f5..fb62fe80202a4 100644 --- a/test/prim/test_comp_custom_vjp.py +++ b/test/prim/test_comp_custom_vjp.py @@ -14,7 +14,7 @@ import unittest import paddle -from paddle.fluid import core +from paddle.base import core class TestCustomVJP(unittest.TestCase): diff --git a/test/prim/test_comp_dispensable.py b/test/prim/test_comp_dispensable.py index a4f4df5fdd1c5..be76ce92ce7f0 100644 --- a/test/prim/test_comp_dispensable.py +++ b/test/prim/test_comp_dispensable.py @@ -19,10 +19,10 @@ class TestDispensable(unittest.TestCase): def setUp(self): - paddle.fluid.core._set_prim_all_enabled(True) + paddle.base.core._set_prim_all_enabled(True) def tearDown(self): - paddle.fluid.core._set_prim_all_enabled(False) + paddle.base.core._set_prim_all_enabled(False) def test_dispensable(self): @paddle.jit.to_static @@ -36,7 +36,7 @@ def f(x): op = f.get_concrete_program(x)[1].backward_program.block(0).ops[-1] self.assertEqual( op.attr('op_role'), - int(paddle.fluid.core.op_proto_and_checker_maker.OpRole.Backward), + int(paddle.base.core.op_proto_and_checker_maker.OpRole.Backward), ) self.assertIn('AxisTensor', op.input_names) diff --git a/test/prim/test_comp_get_grad_op_desc_prim_disabled.py b/test/prim/test_comp_get_grad_op_desc_prim_disabled.py index 25b3340f7b591..710fdc44a85d5 100644 --- a/test/prim/test_comp_get_grad_op_desc_prim_disabled.py +++ b/test/prim/test_comp_get_grad_op_desc_prim_disabled.py @@ -15,14 +15,14 @@ import unittest -from paddle.fluid import core +from paddle.base import core core._set_prim_backward_enabled(False) import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework @param.parameterized_class( diff --git a/test/prim/test_comp_get_grad_op_desc_prim_enabled.py b/test/prim/test_comp_get_grad_op_desc_prim_enabled.py index 9c89ab72bf18e..274abc2bcb1a5 100644 --- a/test/prim/test_comp_get_grad_op_desc_prim_enabled.py +++ b/test/prim/test_comp_get_grad_op_desc_prim_enabled.py @@ -15,14 +15,14 @@ import unittest -from paddle.fluid import core +from paddle.base import core core._set_prim_backward_enabled(True) import parameterized as param import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework @param.parameterized_class( diff --git a/test/prim/test_comp_skip_op_set.py b/test/prim/test_comp_skip_op_set.py index bca3c97ec4d40..8c3e446a62692 100644 --- a/test/prim/test_comp_skip_op_set.py +++ b/test/prim/test_comp_skip_op_set.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle.fluid import core, framework +from paddle.base import core, framework class TestGetGradOpDescPrimEnabled(unittest.TestCase): diff --git a/test/ps/fl_ps_trainer.py b/test/ps/fl_ps_trainer.py index 7eacd5a0e33ca..ad59a68b0a35e 100755 --- a/test/ps/fl_ps_trainer.py +++ b/test/ps/fl_ps_trainer.py @@ -16,12 +16,12 @@ import time import paddle -from paddle import fluid +from paddle import base from paddle.distributed import fleet def get_dataset(inputs, config, pipe_cmd, role="worker"): - dataset = fluid.DatasetFactory().create_dataset() + dataset = base.DatasetFactory().create_dataset() dataset.set_use_var(inputs) dataset.set_pipe_command(pipe_cmd) dataset.set_batch_size(config.get('runner.batch_size')) @@ -102,9 +102,9 @@ def fl_ps_train(): _runtime_handle._run_server() # 4.2 run worker elif role_maker._is_worker(): - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.CPUPlace() + exe = base.Executor(place) + exe.run(base.default_startup_program()) _runtime_handle._init_worker() print('trainer get dataset') inputs = feeds_list[1:-1] @@ -112,8 +112,8 @@ def fl_ps_train(): inputs, config, "python dataset_generator_A.py" ) print( - "fluid.default_main_program: {}".format( - fluid.default_main_program()._heter_pipeline_opt + "base.default_main_program: {}".format( + base.default_main_program()._heter_pipeline_opt ) ) for epoch in range(epoch_num): @@ -121,7 +121,7 @@ def fl_ps_train(): dataset.set_filelist(file_list) start_time = time.time() exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, print_period=2, debug=False, @@ -135,8 +135,8 @@ def fl_ps_train(): _runtime_handle._stop_worker() print("Fl partyA Trainer Success!") else: - exe = fluid.Executor() - exe.run(fluid.default_startup_program()) + exe = base.Executor() + exe.run(base.default_startup_program()) _runtime_handle._init_worker() inputs = [ feeds_list[0], @@ -146,14 +146,14 @@ def fl_ps_train(): inputs, config, "python dataset_generator_B.py", "heter_worker" ) print( - "fluid.default_main_program: {}".format( - fluid.default_main_program()._heter_pipeline_opt + "base.default_main_program: {}".format( + base.default_main_program()._heter_pipeline_opt ) ) for epoch in range(epoch_num): dataset.set_filelist(file_list) exe.train_from_dataset( - program=fluid.default_main_program(), + program=base.default_main_program(), dataset=dataset, print_period=2, debug=False, diff --git a/test/ps/ps_dnn_model.py b/test/ps/ps_dnn_model.py index 7fc74c03c2cca..625d106c1f3e8 100755 --- a/test/ps/ps_dnn_model.py +++ b/test/ps/ps_dnn_model.py @@ -88,7 +88,7 @@ def forward(self, sparse_inputs, dense_inputs): y_dnn = paddle.concat(x=sparse_embs + [dense_inputs], axis=1) if self.sync_mode == 'heter': - with paddle.fluid.device_guard('gpu'): + with paddle.base.device_guard('gpu'): for n_layer in self._mlp_layers: y_dnn = n_layer(y_dnn) else: @@ -196,7 +196,7 @@ def __init__( self._mlp_layers_top.append(act) def bottom_a_layer(self, sparse_inputs): - with paddle.fluid.device_guard(self.PART_A_DEVICE_FlAG): + with paddle.base.device_guard(self.PART_A_DEVICE_FlAG): sparse_embs = [] for s_input in sparse_inputs: emb = self.embedding(s_input) @@ -208,7 +208,7 @@ def bottom_a_layer(self, sparse_inputs): y = self._mlp_layers_a[1](y) y = self._mlp_layers_a[2](y) - with paddle.fluid.device_guard( + with paddle.base.device_guard( self.PART_A_JOINT_OP_DEVICE_FlAG ): # joint point bottom_a = self._mlp_layers_a[3](y) @@ -216,7 +216,7 @@ def bottom_a_layer(self, sparse_inputs): return bottom_a def bottom_b_layer(self, dense_inputs): - with paddle.fluid.device_guard(self.PART_B_DEVICE_FlAG): + with paddle.base.device_guard(self.PART_B_DEVICE_FlAG): y = self._mlp_layers_b[0](dense_inputs) y = self._mlp_layers_b[1](y) @@ -226,14 +226,14 @@ def bottom_b_layer(self, dense_inputs): return bottom_b def interactive_layer(self, bottom_a, bottom_b): - with paddle.fluid.device_guard( + with paddle.base.device_guard( self.PART_B_JOINT_OP_DEVICE_FlAG ): # joint point interactive = paddle.add(bottom_a, bottom_b) return interactive def top_layer(self, interactive, label_input): - with paddle.fluid.device_guard(self.PART_B_DEVICE_FlAG): + with paddle.base.device_guard(self.PART_B_DEVICE_FlAG): y = self._mlp_layers_top[0](interactive) y_top = self._mlp_layers_top[1](y) predict_2d = paddle.nn.functional.softmax(y_top) diff --git a/test/quantization/README.md b/test/quantization/README.md index ce049b45f27f3..fd05146628ded 100644 --- a/test/quantization/README.md +++ b/test/quantization/README.md @@ -94,7 +94,7 @@ The code snipped shows how the `Quant2Int8MkldnnPass` can be applied to a model import paddle import paddle.static as static from paddle.static.quantization import Quant2Int8MkldnnPass - from paddle.fluid.framework import IrGraph + from paddle.base.framework import IrGraph from paddle.framework import core # Create the IrGraph by Program diff --git a/test/quantization/convert_model2dot.py b/test/quantization/convert_model2dot.py index d4f298ddcd5b9..8e7a4bed5033d 100644 --- a/test/quantization/convert_model2dot.py +++ b/test/quantization/convert_model2dot.py @@ -18,7 +18,7 @@ import unittest import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core paddle.enable_static() diff --git a/test/quantization/quant2_int8_image_classification_comparison.py b/test/quantization/quant2_int8_image_classification_comparison.py index c50a926562d27..5c9954df91118 100644 --- a/test/quantization/quant2_int8_image_classification_comparison.py +++ b/test/quantization/quant2_int8_image_classification_comparison.py @@ -23,7 +23,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import Quant2Int8MkldnnPass diff --git a/test/quantization/quant2_int8_lstm_model.py b/test/quantization/quant2_int8_lstm_model.py index ffb2c755433d5..5bbb378e9c35e 100644 --- a/test/quantization/quant2_int8_lstm_model.py +++ b/test/quantization/quant2_int8_lstm_model.py @@ -81,9 +81,7 @@ def get_warmup_tensor(self, data_path, place): [len(feat) // 4 // 8, 8] ) lod_feat = [feat.shape[0]] - minputs = paddle.fluid.create_lod_tensor( - feat, [lod_feat], place - ) + minputs = paddle.base.create_lod_tensor(feat, [lod_feat], place) infer_data = core.PaddleTensor() infer_data.lod = minputs.lod() diff --git a/test/quantization/quant2_int8_nlp_comparison.py b/test/quantization/quant2_int8_nlp_comparison.py index e0ee817e92c79..985fb62f1d11e 100644 --- a/test/quantization/quant2_int8_nlp_comparison.py +++ b/test/quantization/quant2_int8_nlp_comparison.py @@ -21,7 +21,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.inference import Config, create_predictor paddle.enable_static() @@ -281,7 +281,7 @@ def _ints_from_csv(self, string): return set(map(int, string.split(','))) def test_graph_transformation(self): - if not fluid.core.is_compiled_with_mkldnn(): + if not base.core.is_compiled_with_mkldnn(): return quant_model_path = test_case_args.quant_model diff --git a/test/quantization/quant_int8_image_classification_comparison.py b/test/quantization/quant_int8_image_classification_comparison.py index 74b2b0827d870..cc0a6ad32ffc2 100644 --- a/test/quantization/quant_int8_image_classification_comparison.py +++ b/test/quantization/quant_int8_image_classification_comparison.py @@ -23,7 +23,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import QuantInt8MkldnnPass diff --git a/test/quantization/test_graph.py b/test/quantization/test_graph.py index 189ca7cfcae02..3fdb087fcf00b 100644 --- a/test/quantization/test_graph.py +++ b/test/quantization/test_graph.py @@ -16,7 +16,7 @@ import unittest import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core paddle.enable_static() @@ -87,7 +87,7 @@ def graph_apis(self, use_cuda=False, for_ci=True): train_reader = paddle.batch( paddle.dataset.mnist.train(), batch_size=batch_size ) - feeder = paddle.fluid.DataFeeder(feed_list=feeds, place=place) + feeder = paddle.base.DataFeeder(feed_list=feeds, place=place) def _train(binary): for _ in range(iters): diff --git a/test/quantization/test_imperative_out_scale.py b/test/quantization/test_imperative_out_scale.py index 1288281cab2c1..a8317addd6edd 100644 --- a/test/quantization/test_imperative_out_scale.py +++ b/test/quantization/test_imperative_out_scale.py @@ -20,7 +20,7 @@ from imperative_test_utils import fix_model_dict, train_lenet import paddle -from paddle import fluid +from paddle import base from paddle.framework import core, set_flags from paddle.nn import ( BatchNorm2D, @@ -143,7 +143,7 @@ def test_out_scale_acc(self): activation_quantize_type=activation_quantize_type, ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): np.random.seed(seed) paddle.static.default_main_program().random_seed = seed paddle.static.default_startup_program().random_seed = seed @@ -170,7 +170,7 @@ def test_out_scale_acc(self): msg='Failed to do the imperative qat.', ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): lenet = ImperativeLenet() load_dict = paddle.load(self.param_save_path) imperative_out_scale.quantize(lenet) diff --git a/test/quantization/test_imperative_qat.py b/test/quantization/test_imperative_qat.py index 1f95de8e8d1b3..f1cadf1dbe2a3 100644 --- a/test/quantization/test_imperative_qat.py +++ b/test/quantization/test_imperative_qat.py @@ -21,7 +21,7 @@ from imperative_test_utils import ImperativeLenet, fix_model_dict import paddle -from paddle import fluid +from paddle import base from paddle.framework import core, set_flags from paddle.nn import Conv2D, Conv2DTranspose from paddle.nn.quant.quant_layers import ( @@ -72,7 +72,7 @@ def test_qat(self): onnx_format=self.onnx_format, ) - with fluid.dygraph.guard(): + with base.dygraph.guard(): # For CI coverage conv1 = Conv2D( in_channels=3, diff --git a/test/quantization/test_imperative_qat_amp.py b/test/quantization/test_imperative_qat_amp.py index 5b88ef49f9774..4dba9c5421df4 100644 --- a/test/quantization/test_imperative_qat_amp.py +++ b/test/quantization/test_imperative_qat_amp.py @@ -22,7 +22,7 @@ from imperative_test_utils import ImperativeLenet import paddle -from paddle import fluid +from paddle import base from paddle.dataset.common import download from paddle.framework import set_flags from paddle.quantization import ImperativeQuantAware @@ -197,7 +197,7 @@ def test_ptq(self): ) params_path += "/lenet_pretrained/lenet.pdparams" - with fluid.dygraph.guard(): + with base.dygraph.guard(): model = ImperativeLenet() model_state_dict = paddle.load(params_path) model.set_state_dict(model_state_dict) diff --git a/test/quantization/test_llm_int8_linear.py b/test/quantization/test_llm_int8_linear.py index b26285c3049f0..e79b802d37243 100644 --- a/test/quantization/test_llm_int8_linear.py +++ b/test/quantization/test_llm_int8_linear.py @@ -19,9 +19,9 @@ import paddle import paddle.nn.quant as Q -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import default_main_program +from paddle import base +from paddle.base import core +from paddle.base.framework import default_main_program from paddle.framework import set_default_dtype np.random.seed(123) @@ -53,7 +53,7 @@ def setUp(self): x = np.random.random((self.batch, self.token, self.in_features)) self.x = paddle.to_tensor(x, dtype=self.dtype) if self.bias: - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( trainable=False, regularizer=None, initializer=paddle.nn.initializer.Constant(value=1.0), @@ -88,9 +88,9 @@ def get_llm_int8_linear_out(self): def get_llm_int8_linear_out_static(self): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.program_guard(main, start): x = paddle.static.data("x", self.x.shape, dtype=self.x.dtype) weight = paddle.static.data( @@ -126,7 +126,7 @@ def get_llm_int8_linear_out_static(self): 'bias': bias_np, "weight_scale": weight_scale_np, } - exe = fluid.Executor(paddle.CUDAPlace(0)) + exe = base.Executor(paddle.CUDAPlace(0)) exe.run(start) (out,) = exe.run(main, feed=feed_dict, fetch_list=[out]) paddle.disable_static() diff --git a/test/quantization/test_post_training_quantization_lstm_model.py b/test/quantization/test_post_training_quantization_lstm_model.py index d365fda19d4ed..81f68fd2b3986 100644 --- a/test/quantization/test_post_training_quantization_lstm_model.py +++ b/test/quantization/test_post_training_quantization_lstm_model.py @@ -22,7 +22,7 @@ import numpy as np import paddle -from paddle import fluid +from paddle import base from paddle.dataset.common import download from paddle.static.quantization import PostTrainingQuantization @@ -97,7 +97,7 @@ def reader(): ) lod_feat = [feat.shape[0]] - minputs = fluid.create_lod_tensor(feat, [lod_feat], place) + minputs = base.create_lod_tensor(feat, [lod_feat], place) yield [minputs] return reader @@ -127,7 +127,7 @@ def reader(): ) lod_feat = [feat.shape[0]] - minputs = fluid.create_lod_tensor(feat, [lod_feat], place) + minputs = base.create_lod_tensor(feat, [lod_feat], place) yield minputs, label return reader diff --git a/test/quantization/test_quant2_int8_mkldnn_pass.py b/test/quantization/test_quant2_int8_mkldnn_pass.py index e51da1db81ba8..3b8e01c713023 100644 --- a/test/quantization/test_quant2_int8_mkldnn_pass.py +++ b/test/quantization/test_quant2_int8_mkldnn_pass.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import Quant2Int8MkldnnPass diff --git a/test/quantization/test_quant_aware.py b/test/quantization/test_quant_aware.py index e401dda82346f..4a07ad69bae9d 100644 --- a/test/quantization/test_quant_aware.py +++ b/test/quantization/test_quant_aware.py @@ -385,7 +385,7 @@ def test(program): # self.assertEqual(convert_quant_op_nums_1, convert_quant_op_nums_2) def get_op_number(self, prog): - graph = paddle.fluid.framework.IrGraph( + graph = paddle.base.framework.IrGraph( paddle.framework.core.Graph(prog.desc), for_test=False ) quant_op_nums = 0 @@ -398,7 +398,7 @@ def get_op_number(self, prog): return op_nums, quant_op_nums def get_convert_op_number(self, prog): - graph = paddle.fluid.framework.IrGraph( + graph = paddle.base.framework.IrGraph( paddle.framework.core.Graph(prog.desc), for_test=True ) quant_op_nums = 0 diff --git a/test/quantization/test_quant_aware_user_defined.py b/test/quantization/test_quant_aware_user_defined.py index 5169f39bc8584..4352145511f53 100644 --- a/test/quantization/test_quant_aware_user_defined.py +++ b/test/quantization/test_quant_aware_user_defined.py @@ -25,7 +25,7 @@ def pact(x): - helper = paddle.fluid.layer_helper.LayerHelper("pact", **locals()) + helper = paddle.base.layer_helper.LayerHelper("pact", **locals()) dtype = 'float32' init_thres = 20 u_param_attr = paddle.ParamAttr( diff --git a/test/quantization/test_quantization_mkldnn_pass.py b/test/quantization/test_quantization_mkldnn_pass.py index cf5ad87bb1c00..a7c66e456e648 100644 --- a/test/quantization/test_quantization_mkldnn_pass.py +++ b/test/quantization/test_quantization_mkldnn_pass.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import ( QuantInt8MkldnnPass, @@ -156,7 +156,7 @@ def mkldnn_based_freeze_graph( test_reader = paddle.batch( paddle.dataset.mnist.test(), batch_size=batch_size ) - feeder = paddle.fluid.DataFeeder(feed_list=feeds, place=place) + feeder = paddle.base.DataFeeder(feed_list=feeds, place=place) # Training the model to get the weights value with paddle.static.scope_guard(scope): diff --git a/test/quantization/test_quantization_pass.py b/test/quantization/test_quantization_pass.py index 1d1147a0e29de..b91f204c293b2 100644 --- a/test/quantization/test_quantization_pass.py +++ b/test/quantization/test_quantization_pass.py @@ -19,8 +19,8 @@ import numpy as np import paddle -from paddle import fluid -from paddle.fluid.framework import IrGraph +from paddle import base +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import ( AddQuantDequantPass, @@ -386,7 +386,7 @@ def build_program(main, startup, is_test): test_reader = paddle.batch( paddle.dataset.mnist.test(), batch_size=batch_size ) - feeder = paddle.fluid.DataFeeder(feed_list=feeds, place=place) + feeder = paddle.base.DataFeeder(feed_list=feeds, place=place) with paddle.static.scope_guard(scope): for _ in range(iters): data = next(train_reader()) @@ -407,7 +407,7 @@ def build_program(main, startup, is_test): test_data = next(test_reader()) with paddle.static.program_guard(quantized_test_program): - w_var = fluid.framework._get_var( + w_var = base.framework._get_var( 'conv2d_1.w_0.quantized', quantized_test_program ) # Testing diff --git a/test/quantization/test_quantization_scale_pass.py b/test/quantization/test_quantization_scale_pass.py index 644d853da3d93..48a7a71a73f64 100644 --- a/test/quantization/test_quantization_scale_pass.py +++ b/test/quantization/test_quantization_scale_pass.py @@ -20,7 +20,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import core from paddle.static.quantization import ( AddQuantDequantPass, @@ -152,7 +152,7 @@ def build_program(main, startup, is_test): paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=batch_size, ) - feeder = paddle.fluid.DataFeeder(feed_list=feeds, place=place) + feeder = paddle.base.DataFeeder(feed_list=feeds, place=place) with paddle.static.scope_guard(scope): for _ in range(iters): data = next(train_reader()) diff --git a/test/quantization/test_user_defined_quantization.py b/test/quantization/test_user_defined_quantization.py index 80d4e6e564cc2..9a26cc0b4a8fe 100644 --- a/test/quantization/test_user_defined_quantization.py +++ b/test/quantization/test_user_defined_quantization.py @@ -21,7 +21,7 @@ import numpy as np import paddle -from paddle.fluid.framework import IrGraph +from paddle.base.framework import IrGraph from paddle.framework import LayerHelper, core from paddle.static.quantization import ( AddQuantDequantPass, @@ -200,7 +200,7 @@ def save_dict(Dict, mapping_table_path): paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500), batch_size=batch_size, ) - feeder = paddle.fluid.DataFeeder(feed_list=feeds, place=place) + feeder = paddle.base.DataFeeder(feed_list=feeds, place=place) with paddle.static.scope_guard(scope): for _ in range(iters): data = next(train_reader()) diff --git a/test/quantization/test_weight_only_linear.py b/test/quantization/test_weight_only_linear.py index 6c30c13ec21d1..3e40f4d64d36a 100644 --- a/test/quantization/test_weight_only_linear.py +++ b/test/quantization/test_weight_only_linear.py @@ -21,9 +21,9 @@ import paddle import paddle.nn.quant as Q -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.framework import default_main_program +from paddle import base +from paddle.base import core +from paddle.base.framework import default_main_program from paddle.framework import set_default_dtype np.random.seed(123) @@ -80,7 +80,7 @@ def setUp(self): x = np.random.random((self.batch, self.token, self.in_features)) self.x = paddle.to_tensor(x, dtype=self.dtype) if self.bias: - bias_attr = fluid.ParamAttr( + bias_attr = base.ParamAttr( trainable=False, regularizer=None, initializer=paddle.nn.initializer.Constant(value=1.0), @@ -118,9 +118,9 @@ def get_weight_only_linear_out(self): def get_weight_only_linear_out_static(self): paddle.enable_static() - main = fluid.Program() - start = fluid.Program() - with fluid.program_guard(main, start): + main = base.Program() + start = base.Program() + with base.program_guard(main, start): x = paddle.static.data("x", self.x.shape, dtype=self.x.dtype) weight = paddle.static.data( @@ -156,7 +156,7 @@ def get_weight_only_linear_out_static(self): 'bias': bias_np, "weight_scale": weight_scale_np, } - exe = fluid.Executor(paddle.CUDAPlace(0)) + exe = base.Executor(paddle.CUDAPlace(0)) exe.run(start) (out,) = exe.run(main, feed=feed_dict, fetch_list=[out]) paddle.disable_static() diff --git a/test/rnn/test_rnn_api.py b/test/rnn/test_rnn_api.py index 0ed42e27f9025..6e607083d6605 100644 --- a/test/rnn/test_rnn_api.py +++ b/test/rnn/test_rnn_api.py @@ -21,8 +21,8 @@ import numpy as np -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework bidirectional_list = ["bidirectional", "bidirect"] @@ -53,7 +53,7 @@ def setUp(self): paddle.enable_static() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): main_program = paddle.static.Program() startup_program = paddle.static.Program() with paddle.static.program_guard( @@ -62,10 +62,10 @@ def setUp(self): paddle.seed(self.seed) paddle.framework.random._manual_program_seed(self.seed) - self.exe = fluid.Executor( - fluid.CPUPlace() + self.exe = base.Executor( + base.CPUPlace() if self.place == "cpu" - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) rnn_in_data = paddle.static.data( @@ -158,7 +158,7 @@ def setUp(self): paddle.enable_static() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): main_program = paddle.static.Program() startup_program = paddle.static.Program() with paddle.static.program_guard( @@ -167,10 +167,10 @@ def setUp(self): paddle.seed(self.seed) paddle.framework.random._manual_program_seed(self.seed) - self.exe = fluid.Executor( - fluid.CPUPlace() + self.exe = base.Executor( + base.CPUPlace() if self.place == "cpu" - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) rnn_in_data = paddle.static.data( @@ -261,7 +261,7 @@ def setUp(self): paddle.enable_static() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): main_program = paddle.static.Program() startup_program = paddle.static.Program() with paddle.static.program_guard( @@ -270,10 +270,10 @@ def setUp(self): paddle.seed(self.seed) paddle.framework.random._manual_program_seed(self.seed) - self.exe = paddle.fluid.Executor( - fluid.CPUPlace() + self.exe = paddle.base.Executor( + base.CPUPlace() if self.place == "cpu" - else fluid.CUDAPlace(0) + else base.CUDAPlace(0) ) rnn_in_data = paddle.static.data( diff --git a/test/rnn/test_rnn_cells.py b/test/rnn/test_rnn_cells.py index 9c126fcb08dcc..4bb6f49963f84 100644 --- a/test/rnn/test_rnn_cells.py +++ b/test/rnn/test_rnn_cells.py @@ -186,9 +186,7 @@ def runTest(self): def load_tests(loader, tests, pattern): suite = unittest.TestSuite() - devices = ( - ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"] - ) + devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] for bias in [True, False]: for device in devices: for test_class in [TestSimpleRNNCell, TestGRUCell, TestLSTMCell]: diff --git a/test/rnn/test_rnn_cells_static.py b/test/rnn/test_rnn_cells_static.py index a7edbc6749619..df0dbb11bbb51 100644 --- a/test/rnn/test_rnn_cells_static.py +++ b/test/rnn/test_rnn_cells_static.py @@ -37,7 +37,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.SimpleRNNCell( 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias @@ -45,7 +45,7 @@ def setUp(self): place = self.place exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) @@ -71,7 +71,7 @@ def test_with_initial_state(self): y1, h1 = rnn1(x, prev_h) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -103,7 +103,7 @@ def test_with_zero_state(self): y1, h1 = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -139,7 +139,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.GRUCell( 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias @@ -147,7 +147,7 @@ def setUp(self): place = self.place exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) @@ -174,7 +174,7 @@ def test_with_initial_state(self): y1, h1 = rnn1(x, prev_h) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -206,7 +206,7 @@ def test_with_zero_state(self): y1, h1 = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -242,7 +242,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.LSTMCell( 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias @@ -250,7 +250,7 @@ def setUp(self): place = self.place exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) @@ -278,7 +278,7 @@ def test_with_initial_state(self): y1, (h1, c1) = rnn1(x, (prev_h, prev_c)) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -316,7 +316,7 @@ def test_with_zero_state(self): y1, (h1, c1) = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -342,9 +342,7 @@ def runTest(self): def load_tests(loader, tests, pattern): suite = unittest.TestSuite() - devices = ( - ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"] - ) + devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] for bias in [True, False]: for device in devices: for test_class in [TestSimpleRNNCell, TestGRUCell, TestLSTMCell]: diff --git a/test/rnn/test_rnn_cudnn_params_packing.py b/test/rnn/test_rnn_cudnn_params_packing.py index cd5b8b4bfd217..473ad18bfc2e6 100644 --- a/test/rnn/test_rnn_cudnn_params_packing.py +++ b/test/rnn/test_rnn_cudnn_params_packing.py @@ -47,9 +47,9 @@ def test_rnn_with_cudnn_clone(self): # flattening rnn's parameters for the need to call cudnn kernel is such # a case. with paddle.static.program_guard(train_program, startup_prog): - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): bilstm = create_model() - with paddle.fluid.program_guard(test_program, startup_prog): - with paddle.fluid.unique_name.guard(): + with paddle.base.program_guard(test_program, startup_prog): + with paddle.base.unique_name.guard(): bilstm = create_model() diff --git a/test/rnn/test_rnn_nets.py b/test/rnn/test_rnn_nets.py index 0ac68bdbf30d6..734dcae0fde56 100644 --- a/test/rnn/test_rnn_nets.py +++ b/test/rnn/test_rnn_nets.py @@ -359,9 +359,7 @@ def forward(self, input): def load_tests(loader, tests, pattern): suite = unittest.TestSuite() - devices = ( - ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"] - ) + devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] for direction in ["forward", "bidirectional", "bidirect"]: for time_major in [True, False]: for device in devices: diff --git a/test/rnn/test_rnn_nets_static.py b/test/rnn/test_rnn_nets_static.py index da04ca9e1bdb0..20b8a7975e8c2 100644 --- a/test/rnn/test_rnn_nets_static.py +++ b/test/rnn/test_rnn_nets_static.py @@ -46,7 +46,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.SimpleRNN( 16, @@ -57,7 +57,7 @@ def setUp(self): ) exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) @@ -86,7 +86,7 @@ def test_with_initial_state(self): y1, h1 = rnn1(x, prev_h) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -121,7 +121,7 @@ def test_with_zero_state(self): y1, h1 = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -153,7 +153,7 @@ def test_with_input_lengths(self): y1, h1 = rnn1(x, sequence_length=sequence_length) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -202,7 +202,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.GRU( 16, @@ -213,7 +213,7 @@ def setUp(self): ) exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) @@ -243,7 +243,7 @@ def test_with_initial_state(self): y1, h1 = rnn1(x, prev_h) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -278,7 +278,7 @@ def test_with_zero_state(self): y1, h1 = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -310,7 +310,7 @@ def test_with_input_lengths(self): y1, h1 = rnn1(x, sequence_length=sequence_length) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -358,7 +358,7 @@ def setUp(self): mp = paddle.static.Program() sp = paddle.static.Program() - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): rnn2 = paddle.nn.LSTM( 16, @@ -369,7 +369,7 @@ def setUp(self): ) exe = paddle.static.Executor(place) - scope = paddle.fluid.Scope() + scope = paddle.base.Scope() with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) @@ -399,7 +399,7 @@ def test_with_initial_state(self): y1, (h1, c1) = rnn1(x, (prev_h, prev_c)) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -440,7 +440,7 @@ def test_with_zero_state(self): y1, (h1, c1) = rnn1(x) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -473,7 +473,7 @@ def test_with_input_lengths(self): y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) - with paddle.fluid.unique_name.guard(): + with paddle.base.unique_name.guard(): with paddle.static.program_guard(mp, sp): x_data = paddle.static.data( "input", @@ -507,9 +507,7 @@ def runTest(self): def load_tests(loader, tests, pattern): suite = unittest.TestSuite() - devices = ( - ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"] - ) + devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] for direction in ["forward", "bidirectional", "bidirect"]: for time_major in [True, False]: for device in devices: diff --git a/test/rnn/test_wrappers.py b/test/rnn/test_wrappers.py index aa926808f397c..957d5894b9885 100644 --- a/test/rnn/test_wrappers.py +++ b/test/rnn/test_wrappers.py @@ -195,9 +195,7 @@ def runTest(self): def load_tests(loader, tests, pattern): suite = unittest.TestSuite() - devices = ( - ["cpu", "gpu"] if paddle.fluid.is_compiled_with_cuda() else ["cpu"] - ) + devices = ["cpu", "gpu"] if paddle.base.is_compiled_with_cuda() else ["cpu"] for direction in ["forward", "backward"]: for device in devices: for time_major in [False]: diff --git a/test/sequence/test_sequence_conv.py b/test/sequence/test_sequence_conv.py index 6f71298a0fc02..62ac526d61e0f 100644 --- a/test/sequence/test_sequence_conv.py +++ b/test/sequence/test_sequence_conv.py @@ -292,19 +292,19 @@ def init_test_case(self): class TestSeqConvApi(unittest.TestCase): def test_api(self): - from paddle import fluid + from paddle import base x = paddle.static.data('x', shape=[-1, 32], lod_level=1) y = paddle.static.nn.sequence_lod.sequence_conv( input=x, num_filters=2, filter_size=3, padding_start=None ) - place = fluid.CPUPlace() - x_tensor = fluid.create_lod_tensor( + place = base.CPUPlace() + x_tensor = base.create_lod_tensor( np.random.rand(10, 32).astype("float32"), [[2, 3, 1, 4]], place ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run(feed={'x': x_tensor}, fetch_list=[y], return_numpy=False) diff --git a/test/sequence/test_sequence_expand_as.py b/test/sequence/test_sequence_expand_as.py index ba141e20e1924..090a6242c003a 100644 --- a/test/sequence/test_sequence_expand_as.py +++ b/test/sequence/test_sequence_expand_as.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard class TestSequenceExpandAs(OpTest): diff --git a/test/sequence/test_sequence_first_step.py b/test/sequence/test_sequence_first_step.py index 2967832b69115..d808c20ff2633 100644 --- a/test/sequence/test_sequence_first_step.py +++ b/test/sequence/test_sequence_first_step.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.framework import Program, program_guard +from paddle.base.framework import Program, program_guard sys.path.append("../") diff --git a/test/sequence/test_sequence_last_step.py b/test/sequence/test_sequence_last_step.py index 771b49379110a..8799f20d15bd0 100644 --- a/test/sequence/test_sequence_last_step.py +++ b/test/sequence/test_sequence_last_step.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.fluid.framework import Program, program_guard +from paddle.base.framework import Program, program_guard sys.path.append("../") diff --git a/test/sequence/test_sequence_mask.py b/test/sequence/test_sequence_mask.py index 152092eb4424d..54d7a3c87dcad 100644 --- a/test/sequence/test_sequence_mask.py +++ b/test/sequence/test_sequence_mask.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid.framework import ( +from paddle.base.framework import ( Program, convert_np_dtype_to_dtype_, program_guard, diff --git a/test/sequence/test_sequence_pad_op.py b/test/sequence/test_sequence_pad_op.py index b657ca8640aab..b78ddf12eef9a 100644 --- a/test/sequence/test_sequence_pad_op.py +++ b/test/sequence/test_sequence_pad_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest import paddle -from paddle.fluid import core +from paddle.base import core class TestSequencePadOp(OpTest): diff --git a/test/sequence/test_sequence_softmax_op.py b/test/sequence/test_sequence_softmax_op.py index 6bc81cb80ed22..de6258bbc2612 100644 --- a/test/sequence/test_sequence_softmax_op.py +++ b/test/sequence/test_sequence_softmax_op.py @@ -19,7 +19,7 @@ from eager_op_test import OpTest from test_softmax_op import stable_softmax -from paddle.fluid import core +from paddle.base import core class TestSequenceSoftmaxOp(OpTest): diff --git a/test/standalone_executor/test_standalone_controlflow.py b/test/standalone_executor/test_standalone_controlflow.py index fcdf7f8158708..ecd0e517f89c7 100644 --- a/test/standalone_executor/test_standalone_controlflow.py +++ b/test/standalone_executor/test_standalone_controlflow.py @@ -17,8 +17,8 @@ import numpy as np import paddle -from paddle.fluid import core -from paddle.fluid.framework import Program, program_guard +from paddle.base import core +from paddle.base.framework import Program, program_guard paddle.enable_static() diff --git a/test/standalone_executor/test_standalone_cross_step_overlap.py b/test/standalone_executor/test_standalone_cross_step_overlap.py index a4fe9f9d25849..170ed47e2bd84 100644 --- a/test/standalone_executor/test_standalone_cross_step_overlap.py +++ b/test/standalone_executor/test_standalone_cross_step_overlap.py @@ -31,7 +31,7 @@ def setUp(self): self.step_num = 3 def test_cross_step_overlap(self): - if not paddle.fluid.core.is_compiled_with_cuda(): + if not paddle.base.core.is_compiled_with_cuda(): return # In this test case, z=x+y is calculated in the default stream, diff --git a/test/standalone_executor/test_standalone_custom_event.py b/test/standalone_executor/test_standalone_custom_event.py index 3b9fe7a3197b6..e65ed021e7972 100644 --- a/test/standalone_executor/test_standalone_custom_event.py +++ b/test/standalone_executor/test_standalone_custom_event.py @@ -15,13 +15,13 @@ import unittest import paddle +from paddle.base import core +from paddle.base.executor import _add_feed_fetch_ops, _StandaloneExecutor from paddle.distributed.passes.pass_utils import ( _add_event_dependency, get_skip_gc_vars, split_program, ) -from paddle.fluid import core -from paddle.fluid.executor import _add_feed_fetch_ops, _StandaloneExecutor paddle.enable_static() diff --git a/test/standalone_executor/test_standalone_custom_stream.py b/test/standalone_executor/test_standalone_custom_stream.py index 4126f84ed1e8c..f9635a918068c 100644 --- a/test/standalone_executor/test_standalone_custom_stream.py +++ b/test/standalone_executor/test_standalone_custom_stream.py @@ -17,7 +17,7 @@ from test_standalone_executor import build_program import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/standalone_executor/test_standalone_executor.py b/test/standalone_executor/test_standalone_executor.py index 5724ff42667cb..65a9bf06ab563 100644 --- a/test/standalone_executor/test_standalone_executor.py +++ b/test/standalone_executor/test_standalone_executor.py @@ -22,7 +22,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core from paddle.profiler import profiler paddle.enable_static() @@ -89,7 +89,7 @@ def run_with_statistics(self, executor=None): enable = True if executor == 'ParallelExecutor': - main_program = paddle.fluid.compiler.CompiledProgram(main_program) + main_program = paddle.base.compiler.CompiledProgram(main_program) enable = False elif executor == 'Executor': enable = False @@ -236,9 +236,9 @@ def test_with_feed(self): data = np.ones([2, 2], dtype="float32") feed = {"a": data, 'fake_input': data} - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): res = self.run_new_executor(feed) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): gt = self.run_dygraph(feed) for x, y in zip(gt, res): np.testing.assert_array_equal(x, y) @@ -311,7 +311,7 @@ def test_exception(self): def test_nan(self): flags = {'FLAGS_check_nan_inf': True, 'FLAGS_benchmark': True} - paddle.fluid.set_flags(flags) + paddle.base.set_flags(flags) feed = [ { 'id': np.array([1, 2, 3, 4, 5]).astype(np.int64), @@ -345,7 +345,7 @@ def test_scope_find_temp_var(self): class TestFetchEmptyTensor(unittest.TestCase): def test_fetch(self): places = [paddle.CPUPlace()] - if paddle.fluid.core.is_compiled_with_cuda(): + if paddle.base.core.is_compiled_with_cuda(): places.append(paddle.CUDAPlace(0)) for place in places: with paddle.static.program_guard(paddle.static.Program()): @@ -357,10 +357,10 @@ def test_fetch(self): class TestInplaceApiWithDataTransform(unittest.TestCase): def test_increment(self): - if paddle.fluid.core.is_compiled_with_cuda(): - with paddle.fluid.device_guard("gpu:0"): + if paddle.base.core.is_compiled_with_cuda(): + with paddle.base.device_guard("gpu:0"): x = paddle.tensor.fill_constant([1], "float32", 0) - with paddle.fluid.device_guard("cpu"): + with paddle.base.device_guard("cpu"): x = paddle.increment(x) exe = paddle.static.Executor(paddle.CUDAPlace(0)) for i in range(10): diff --git a/test/standalone_executor/test_standalone_executor_aot_choose_kernel.py b/test/standalone_executor/test_standalone_executor_aot_choose_kernel.py index 334ac8ffc1a9f..f950e165132d4 100644 --- a/test/standalone_executor/test_standalone_executor_aot_choose_kernel.py +++ b/test/standalone_executor/test_standalone_executor_aot_choose_kernel.py @@ -99,14 +99,14 @@ def run_resnet50(aot_choose_kernel=False, use_amp=False): class TestAOTChooseKernel(unittest.TestCase): def test_resnet50_aot_choose_kernel(self): - if not paddle.fluid.core.is_compiled_with_cuda(): + if not paddle.base.core.is_compiled_with_cuda(): return loss1 = run_resnet50(aot_choose_kernel=True) loss2 = run_resnet50(aot_choose_kernel=False) self.assertEqual(loss1, loss2) def test_resnet50_amp_aot_choose_kernel(self): - if not paddle.fluid.core.is_compiled_with_cuda(): + if not paddle.base.core.is_compiled_with_cuda(): return loss1 = run_resnet50(aot_choose_kernel=True, use_amp=True) loss2 = run_resnet50(aot_choose_kernel=False, use_amp=True) diff --git a/test/standalone_executor/test_standalone_executor_multi_micro_batch.py b/test/standalone_executor/test_standalone_executor_multi_micro_batch.py index 61b76559c0098..f44e23ddf8dc2 100644 --- a/test/standalone_executor/test_standalone_executor_multi_micro_batch.py +++ b/test/standalone_executor/test_standalone_executor_multi_micro_batch.py @@ -19,10 +19,10 @@ import numpy as np import paddle +from paddle.base import core +from paddle.base.core import Job, Plan +from paddle.base.executor import _add_feed_fetch_ops, _StandaloneExecutor from paddle.distributed.passes.pass_utils import get_skip_gc_vars, split_program -from paddle.fluid import core -from paddle.fluid.core import Job, Plan -from paddle.fluid.executor import _add_feed_fetch_ops, _StandaloneExecutor from paddle.nn import TransformerEncoderLayer paddle.enable_static() @@ -94,7 +94,7 @@ def build_program(self, micro_batch_size, src_len, d_model, n_head): dtype="float32", ) - loader = paddle.fluid.io.DataLoader.from_generator( + loader = paddle.base.io.DataLoader.from_generator( feed_list=[enc_input, attn_mask], use_double_buffer=False, capacity=16, diff --git a/test/standalone_executor/test_standalone_executor_plan.py b/test/standalone_executor/test_standalone_executor_plan.py index 72fc4ab9bb731..82bb89855ef89 100644 --- a/test/standalone_executor/test_standalone_executor_plan.py +++ b/test/standalone_executor/test_standalone_executor_plan.py @@ -15,7 +15,7 @@ import unittest from paddle import static -from paddle.fluid import core +from paddle.base import core class TestStandaloneExecutorPlan(unittest.TestCase): diff --git a/test/standalone_executor/test_standalone_multiply_write.py b/test/standalone_executor/test_standalone_multiply_write.py index 96e35a336951c..63a13f489c0ef 100644 --- a/test/standalone_executor/test_standalone_multiply_write.py +++ b/test/standalone_executor/test_standalone_multiply_write.py @@ -17,7 +17,7 @@ from test_standalone_controlflow import TestCompatibility import paddle -from paddle.fluid.framework import Program +from paddle.base.framework import Program paddle.enable_static() diff --git a/test/tokenizer/test_faster_tokenizer_op.py b/test/tokenizer/test_faster_tokenizer_op.py index 3aea8b4155c2a..7d67c09226288 100755 --- a/test/tokenizer/test_faster_tokenizer_op.py +++ b/test/tokenizer/test_faster_tokenizer_op.py @@ -21,8 +21,8 @@ import paddle from paddle import _legacy_C_ops, nn -from paddle.fluid.framework import core -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.framework import core +from paddle.base.layer_helper import LayerHelper from paddle.framework import in_dynamic_mode diff --git a/test/xpu/collective_allgather_op_xpu.py b/test/xpu/collective_allgather_op_xpu.py index 6e2ab46dc9975..0c88f00f1a10f 100644 --- a/test/xpu/collective_allgather_op_xpu.py +++ b/test/xpu/collective_allgather_op_xpu.py @@ -15,8 +15,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/collective_allreduce_op_xpu.py b/test/xpu/collective_allreduce_op_xpu.py index 6ab13b2be13e4..f85502580b712 100644 --- a/test/xpu/collective_allreduce_op_xpu.py +++ b/test/xpu/collective_allreduce_op_xpu.py @@ -17,8 +17,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -29,7 +29,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/collective_broadcast_op_xpu.py b/test/xpu/collective_broadcast_op_xpu.py index a5d51e714115a..fee4deaf6caff 100755 --- a/test/xpu/collective_broadcast_op_xpu.py +++ b/test/xpu/collective_broadcast_op_xpu.py @@ -17,8 +17,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -30,7 +30,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 rootid = 1 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/collective_concat_op.py b/test/xpu/collective_concat_op.py index 1fd946dff091d..ebda6c3883f6a 100644 --- a/test/xpu/collective_concat_op.py +++ b/test/xpu/collective_concat_op.py @@ -15,8 +15,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core, layers +from paddle import base +from paddle.base import core, layers paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = layers.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/collective_identity_op_xpu.py b/test/xpu/collective_identity_op_xpu.py index 840e0236371c9..1c5cac6716388 100644 --- a/test/xpu/collective_identity_op_xpu.py +++ b/test/xpu/collective_identity_op_xpu.py @@ -15,8 +15,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = paddle.static.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/collective_split_op.py b/test/xpu/collective_split_op.py index 56ca1a6d71751..82f8db770e524 100644 --- a/test/xpu/collective_split_op.py +++ b/test/xpu/collective_split_op.py @@ -15,8 +15,8 @@ from test_collective_base_xpu import TestCollectiveRunnerBase, runtime_main import paddle -from paddle import fluid -from paddle.fluid import core, layers +from paddle import base +from paddle.base import core, layers paddle.enable_static() @@ -28,7 +28,7 @@ def __init__(self): def get_model(self, main_prog, startup_program): ring_id = 0 nranks = 2 - with fluid.program_guard(main_prog, startup_program): + with base.program_guard(main_prog, startup_program): tindata = layers.data( name="tindata", shape=[10, 1000], dtype='float32' ) diff --git a/test/xpu/get_test_cover_info.py b/test/xpu/get_test_cover_info.py index 4ef72587a1e20..806847f451c12 100644 --- a/test/xpu/get_test_cover_info.py +++ b/test/xpu/get_test_cover_info.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core type_dict_paddle_to_str = { paddle.bool: 'bool', diff --git a/test/xpu/op_test_xpu.py b/test/xpu/op_test_xpu.py index 076859c8a1708..b17561758ad2d 100644 --- a/test/xpu/op_test_xpu.py +++ b/test/xpu/op_test_xpu.py @@ -24,10 +24,10 @@ from white_list import no_grad_set_white_list, op_threshold_white_list import paddle -from paddle import fluid -from paddle.fluid import core -from paddle.fluid.backward import append_backward -from paddle.fluid.framework import Program, convert_np_dtype_to_dtype_ +from paddle import base +from paddle.base import core +from paddle.base.backward import append_backward +from paddle.base.framework import Program, convert_np_dtype_to_dtype_ class XPUOpTest(OpTest): @@ -359,7 +359,7 @@ def get_grad_with_place( ) fetch_list = [g for p, g in param_grad_list] - executor = fluid.Executor(place) + executor = base.Executor(place) return list( map( np.array, diff --git a/test/xpu/test_activation_op_xpu.py b/test/xpu/test_activation_op_xpu.py index d6ece4edf8107..b2a546542b91a 100644 --- a/test/xpu/test_activation_op_xpu.py +++ b/test/xpu/test_activation_op_xpu.py @@ -46,7 +46,7 @@ def set_case(self): x = np.random.uniform(-1, 1, self.shape).astype(self.dtype) out = np.exp(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def init_dtype(self): @@ -72,7 +72,7 @@ def set_case(self): x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype) out = np.exp(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} class XPUTestExp_ZeroDIm(TestActivationOPBase): @@ -201,7 +201,7 @@ def set_case(self): out = 1 / (1 + np.exp(-self.x)) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Out': out} def init_config(self): @@ -248,7 +248,7 @@ def set_case(self): x = np.random.uniform(-1, 1, [11, 17]).astype(self.dtype) out = np.tanh(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} @@ -271,7 +271,7 @@ def set_case(self): out = np.sqrt(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} @@ -294,7 +294,7 @@ def set_case(self): out = np.floor(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def test_check_grad(self): @@ -325,7 +325,7 @@ def set_case(self): out = np.abs(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} @@ -476,7 +476,7 @@ def set_case(self): out = np.log(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} class TestLogCase_ZeroDim(XPUTestLog): @@ -518,7 +518,7 @@ def set_case(self): out = np.square(self.x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.outputs = {'Out': out} def init_config(self): @@ -563,7 +563,7 @@ def set_case(self): self.init_config() out = np.power(self.x, self.factor) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(self.x)} self.attrs = {'factor': self.factor, 'use_xpu': True} self.outputs = {'Out': out} @@ -662,7 +662,7 @@ def set_case(self): x = np.random.uniform(1, 2, [1111, 1117]).astype(self.dtype) out = np.reciprocal(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'use_xpu': True} @@ -775,7 +775,7 @@ def set_case(self): x = np.random.uniform(-1, 1, [10, 12]).astype(self.dtype) out = np.ceil(x) - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} self.attrs = {'use_xpu': True} diff --git a/test/xpu/test_adadelta_op_xpu.py b/test/xpu/test_adadelta_op_xpu.py index b6ef0fbdf8ec8..7e30557d2be78 100644 --- a/test/xpu/test_adadelta_op_xpu.py +++ b/test/xpu/test_adadelta_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -165,9 +165,9 @@ def test_adadelta_dygraph(self): def test_adadelta(self): self.dtype = self.in_type paddle.enable_static() - place = fluid.XPUPlace(0) - main = fluid.Program() - with fluid.program_guard(main): + place = base.XPUPlace(0) + main = base.Program() + with base.program_guard(main): x = paddle.static.data( name='x', shape=[-1, 13], dtype=self.dtype ) @@ -187,9 +187,9 @@ def test_adadelta(self): train_reader = paddle.batch( paddle.dataset.uci_housing.train(), batch_size=1 ) - feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + feeder = base.DataFeeder(place=place, feed_list=[x, y]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) for data in train_reader(): exe.run(main, feed=feeder.feed(data), fetch_list=fetch_list) diff --git a/test/xpu/test_adam_op_xpu.py b/test/xpu/test_adam_op_xpu.py index 02a84c7ae57b1..823cf1543e0a5 100644 --- a/test/xpu/test_adam_op_xpu.py +++ b/test/xpu/test_adam_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core class XPUTestAdamOp(XPUOpTestWrapper): diff --git a/test/xpu/test_adamw_op_xpu.py b/test/xpu/test_adamw_op_xpu.py index 768cbe8151da3..1a777f2d23578 100644 --- a/test/xpu/test_adamw_op_xpu.py +++ b/test/xpu/test_adamw_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base def adamw_step(inputs, attributes): @@ -185,13 +185,13 @@ def test_adamw_op_coverage(self): def test_adamw_op(self): paddle.enable_static() - place = fluid.XPUPlace(0) + place = base.XPUPlace(0) shape = [2, 3, 8, 8] - exe = fluid.Executor(place) - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + exe = base.Executor(place) + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): data = paddle.static.data(name="data", shape=shape) conv = paddle.static.nn.conv2d(data, 8, 3) loss = paddle.mean(conv) @@ -437,7 +437,7 @@ def get_numpy_output(param, grad, moment1, moment2, lr_ratio, t): def test_adamw_op(self): paddle.enable_static() - place = fluid.XPUPlace(0) + place = base.XPUPlace(0) learning_rate = 0.0001 beta1 = 0.85 @@ -445,10 +445,10 @@ def test_adamw_op(self): weight_decay = 0.01 epsilon = 1e-8 - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): - with fluid.unique_name.guard(): + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): + with base.unique_name.guard(): x = paddle.static.data( name='x', shape=[None, 10], dtype='float32' ) @@ -557,7 +557,7 @@ def get_numpy_output(param, grad, moment1, moment2, lr_ratio, t): "linear_1.b_0@GRAD", ] - exe = fluid.Executor(place) + exe = base.Executor(place) exe.run(startup) test_prog = train_prog.clone(for_test=True) diff --git a/test/xpu/test_affine_channel_op_xpu.py b/test/xpu/test_affine_channel_op_xpu.py index c200235ff879c..8d404b35dd3b2 100644 --- a/test/xpu/test_affine_channel_op_xpu.py +++ b/test/xpu/test_affine_channel_op_xpu.py @@ -21,7 +21,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core def affine_channel(x, scale, bias, layout): diff --git a/test/xpu/test_assign_value_op_xpu.py b/test/xpu/test_assign_value_op_xpu.py index a0e3a57dc8ac5..f6d2d2ec96ae3 100644 --- a/test/xpu/test_assign_value_op_xpu.py +++ b/test/xpu/test_assign_value_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import framework +from paddle import base +from paddle.base import framework paddle.enable_static() @@ -82,18 +82,18 @@ def setUp(self): self.value = (-100 + 200 * np.random.random(size=(2, 5))).astype( self.dtype ) - self.place = fluid.XPUPlace(0) + self.place = base.XPUPlace(0) def init_dtype(self): self.dtype = "float32" def test_assign(self): - main_program = fluid.Program() - with fluid.program_guard(main_program): + main_program = base.Program() + with base.program_guard(main_program): x = paddle.tensor.create_tensor(dtype=self.dtype) paddle.assign(self.value, output=x) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) [fetched_x] = exe.run(main_program, feed={}, fetch_list=[x]) np.testing.assert_allclose(fetched_x, self.value) self.assertEqual(fetched_x.dtype, self.value.dtype) @@ -115,7 +115,7 @@ def setUp(self): self.value = np.random.choice(a=[False, True], size=(2, 5)).astype( np.bool_ ) - self.place = fluid.XPUPlace(0) + self.place = base.XPUPlace(0) def init_dtype(self): self.dtype = "bool" diff --git a/test/xpu/test_batch_norm_op_xpu.py b/test/xpu/test_batch_norm_op_xpu.py index e9ca4da20e778..3432442930cc9 100644 --- a/test/xpu/test_batch_norm_op_xpu.py +++ b/test/xpu/test_batch_norm_op_xpu.py @@ -23,8 +23,8 @@ import paddle import paddle.nn.functional as F -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -260,11 +260,11 @@ def init_test(self): def test_global_stats(self): for p in self.places: - with fluid.dygraph.guard(p): + with base.dygraph.guard(p): x = paddle.randn([2, 6, 6, 4]) net1 = paddle.nn.BatchNorm( 6, - param_attr=fluid.ParamAttr( + param_attr=base.ParamAttr( initializer=paddle.nn.initializer.Constant(1.0) ), use_global_stats=self.use_global_stats, diff --git a/test/xpu/test_bilinear_interp_op_xpu.py b/test/xpu/test_bilinear_interp_op_xpu.py index a5a849f080e6a..6c08731d3b01d 100755 --- a/test/xpu/test_bilinear_interp_op_xpu.py +++ b/test/xpu/test_bilinear_interp_op_xpu.py @@ -471,12 +471,12 @@ def test_case(self): scale_tensor = paddle.static.data( name="scale_tensor", shape=[1], dtype="float32") - out1 = fluid.layers.resize_bilinear(x, out_shape=[12, 12]) - out2 = fluid.layers.resize_bilinear(x, out_shape=[12, dim]) - out3 = fluid.layers.resize_bilinear(x, out_shape=shape_tensor) - out4 = fluid.layers.resize_bilinear( + out1 = base.layers.resize_bilinear(x, out_shape=[12, 12]) + out2 = base.layers.resize_bilinear(x, out_shape=[12, dim]) + out3 = base.layers.resize_bilinear(x, out_shape=shape_tensor) + out4 = base.layers.resize_bilinear( x, out_shape=[4, 4], actual_shape=actual_size) - out5 = fluid.layers.resize_bilinear(x, scale=scale_tensor) + out5 = base.layers.resize_bilinear(x, scale=scale_tensor) x_data = np.random.random((2, 3, 6, 6)).astype("float32") dim_data = np.array([12]).astype("int32") @@ -485,9 +485,9 @@ def test_case(self): scale_data = np.array([2.0]).astype("float32") place = core.XPUPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - results = exe.run(fluid.default_main_program(), + exe = base.Executor(place) + exe.run(base.default_startup_program()) + results = exe.run(base.default_main_program(), feed={ "x": x_data, "dim": dim_data, diff --git a/test/xpu/test_bitwise_op_xpu.py b/test/xpu/test_bitwise_op_xpu.py index 0466ad39d6702..1d261c95bb49a 100644 --- a/test/xpu/test_bitwise_op_xpu.py +++ b/test/xpu/test_bitwise_op_xpu.py @@ -59,8 +59,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -133,8 +133,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -206,8 +206,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -270,7 +270,7 @@ def set_case(self): out = np.bitwise_not(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def init_case(self): diff --git a/test/xpu/test_c_concat.py b/test/xpu/test_c_concat.py index d2490aa3772dc..c3731a4e04c1a 100644 --- a/test/xpu/test_c_concat.py +++ b/test/xpu/test_c_concat.py @@ -22,7 +22,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_c_split.py b/test/xpu/test_c_split.py index 67e2f1a6cc5f6..c4533eaeb0bf6 100644 --- a/test/xpu/test_c_split.py +++ b/test/xpu/test_c_split.py @@ -22,7 +22,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_cast_op_xpu.py b/test/xpu/test_cast_op_xpu.py index fbfe6e979e7f4..0a8043d523f5f 100644 --- a/test/xpu/test_cast_op_xpu.py +++ b/test/xpu/test_cast_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard typeid_dict = { 'int32': int(core.VarDesc.VarType.INT32), @@ -93,8 +93,8 @@ class TestCastOpError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): # The input type of cast_op must be Variable. - x1 = fluid.create_lod_tensor( - np.array([[-1]]), [[1]], fluid.XPUPlace(0) + x1 = base.create_lod_tensor( + np.array([[-1]]), [[1]], base.XPUPlace(0) ) self.assertRaises(TypeError, paddle.cast, x1, 'int32') diff --git a/test/xpu/test_clip_op_xpu.py b/test/xpu/test_clip_op_xpu.py index 79d4e3e779869..a85482a2cce67 100644 --- a/test/xpu/test_clip_op_xpu.py +++ b/test/xpu/test_clip_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, core, program_guard +from paddle import base +from paddle.base import Program, core, program_guard class XPUTestClipOp(XPUOpTestWrapper): @@ -159,11 +159,11 @@ def test_clip(self): max = paddle.static.data(name='max', shape=[1], dtype='float32') place = ( - fluid.XPUPlace(0) - if fluid.core.is_compiled_with_xpu() - else fluid.CPUPlace() + base.XPUPlace(0) + if base.core.is_compiled_with_xpu() + else base.CPUPlace() ) - exe = fluid.Executor(place) + exe = base.Executor(place) out_1 = self._executed_api(images, min=min, max=max) out_2 = self._executed_api(images, min=0.2, max=0.9) @@ -175,7 +175,7 @@ def test_clip(self): out_8 = self._executed_api(images) res1, res2, res3, res4, res5, res6, res7, res8 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "image": data, "min": np.array([0.2]).astype('float32'), @@ -197,9 +197,9 @@ def test_clip(self): def test_clip_dygraph(self): paddle.disable_static() place = ( - fluid.XPUPlace(0) - if fluid.core.is_compiled_with_xpu() - else fluid.CPUPlace() + base.XPUPlace(0) + if base.core.is_compiled_with_xpu() + else base.CPUPlace() ) paddle.disable_static(place) data_shape = [1, 9, 9, 4] diff --git a/test/xpu/test_coalesce_tensor_op_xpu.py b/test/xpu/test_coalesce_tensor_op_xpu.py index f0f053137949f..614274e72fda0 100644 --- a/test/xpu/test_coalesce_tensor_op_xpu.py +++ b/test/xpu/test_coalesce_tensor_op_xpu.py @@ -16,7 +16,7 @@ import numpy as np -from paddle.fluid import core +from paddle.base import core alignment = 256 from get_test_cover_info import ( @@ -40,7 +40,7 @@ class TestAllocContinuousSpace(XPUOpTest): def setUp(self): self.op_type = "coalesce_tensor" self.use_xpu = True - self.dtype, self.fluid_dtype = self.init_dtype() + self.dtype, self.base_dtype = self.init_dtype() attrs = self.init_attr() self.copy_data = attrs["copy_data"] self.constant = attrs["constant"] @@ -76,7 +76,7 @@ def init_attr(self): "copy_data": True, "set_constant": False, "constant": 0.0, - "dtype": self.fluid_dtype, + "dtype": self.base_dtype, } def init_output(self, input_list, set_constant, constant): @@ -115,7 +115,7 @@ def init_attr(self): "copy_data": False, "set_constant": True, "constant": 0.5, - "dtype": self.fluid_dtype, + "dtype": self.base_dtype, "user_defined_size_of_dtype": 2, } diff --git a/test/xpu/test_collective_allgather_xpu.py b/test/xpu/test_collective_allgather_xpu.py index 3651ed2062957..fc30a2449e112 100644 --- a/test/xpu/test_collective_allgather_xpu.py +++ b/test/xpu/test_collective_allgather_xpu.py @@ -22,7 +22,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_collective_allreduce_xpu.py b/test/xpu/test_collective_allreduce_xpu.py index 05539aeaae432..c8626c72e1a17 100644 --- a/test/xpu/test_collective_allreduce_xpu.py +++ b/test/xpu/test_collective_allreduce_xpu.py @@ -22,7 +22,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_collective_base_xpu.py b/test/xpu/test_collective_base_xpu.py index 022ef400164d1..3cc33125f9233 100644 --- a/test/xpu/test_collective_base_xpu.py +++ b/test/xpu/test_collective_base_xpu.py @@ -24,9 +24,9 @@ import numpy as np -import paddle.fluid.unique_name as nameGen -from paddle import fluid -from paddle.fluid import core +import paddle.base.unique_name as nameGen +from paddle import base +from paddle.base import core def DataTypeCast(date_type): @@ -127,8 +127,8 @@ def initCommunicator( ) def run_trainer(self, args): - train_prog = fluid.Program() - startup_prog = fluid.Program() + train_prog = base.Program() + startup_prog = base.Program() endpoints = args["endpoints"].split(",") rank = args["trainerid"] current_endpoint = args["currentendpoint"] @@ -139,8 +139,8 @@ def run_trainer(self, args): self.rank = rank result = self.get_model(train_prog, startup_prog) device_id = int(os.getenv("FLAGS_selected_xpus", "0")) - place = fluid.XPUPlace(device_id) - exe = fluid.Executor(place) + place = base.XPUPlace(device_id) + exe = base.Executor(place) exe.run(startup_prog) np.random.seed(os.getpid()) np_data_type = DataTypeCast(args["data_type"]) diff --git a/test/xpu/test_collective_broadcast_xpu.py b/test/xpu/test_collective_broadcast_xpu.py index 5ddb451e7e4fa..061c0a76a041f 100644 --- a/test/xpu/test_collective_broadcast_xpu.py +++ b/test/xpu/test_collective_broadcast_xpu.py @@ -18,7 +18,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_collective_identity_xpu.py b/test/xpu/test_collective_identity_xpu.py index 421f9168a28d3..830a5657e7b43 100644 --- a/test/xpu/test_collective_identity_xpu.py +++ b/test/xpu/test_collective_identity_xpu.py @@ -22,7 +22,7 @@ from test_collective_base_xpu import TestDistBase import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_conv2d_op_xpu.py b/test/xpu/test_conv2d_op_xpu.py index d09402f934c69..6a97a2067727e 100644 --- a/test/xpu/test_conv2d_op_xpu.py +++ b/test/xpu/test_conv2d_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core def conv2d_forward_naive( @@ -232,8 +232,8 @@ def setUp(self): output = output.astype(self.dtype) self.inputs = { - 'Input': XPUOpTest.np_dtype_to_fluid_dtype(input), - 'Filter': XPUOpTest.np_dtype_to_fluid_dtype(filter), + 'Input': XPUOpTest.np_dtype_to_base_dtype(input), + 'Filter': XPUOpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, @@ -392,8 +392,8 @@ def setUp(self): output = output.astype(self.dtype) self.inputs = { - 'Input': XPUOpTest.np_dtype_to_fluid_dtype(input), - 'Filter': XPUOpTest.np_dtype_to_fluid_dtype(filter), + 'Input': XPUOpTest.np_dtype_to_base_dtype(input), + 'Filter': XPUOpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/xpu/test_conv3d_op_xpu.py b/test/xpu/test_conv3d_op_xpu.py index 566c1960f8263..4ecdeb8c7cd6c 100644 --- a/test/xpu/test_conv3d_op_xpu.py +++ b/test/xpu/test_conv3d_op_xpu.py @@ -235,8 +235,8 @@ def setUp(self): ).astype(self.dtype) self.inputs = { - 'Input': XPUOpTest.np_dtype_to_fluid_dtype(input), - 'Filter': XPUOpTest.np_dtype_to_fluid_dtype(filter), + 'Input': XPUOpTest.np_dtype_to_base_dtype(input), + 'Filter': XPUOpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, @@ -409,8 +409,8 @@ def setUp(self): ).astype(self.dtype) self.inputs = { - 'Input': XPUOpTest.np_dtype_to_fluid_dtype(input), - 'Filter': XPUOpTest.np_dtype_to_fluid_dtype(filter), + 'Input': XPUOpTest.np_dtype_to_base_dtype(input), + 'Filter': XPUOpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/xpu/test_deformable_conv_op_xpu.py b/test/xpu/test_deformable_conv_op_xpu.py index 62d274fbc74f0..da8d1b86dcf7f 100644 --- a/test/xpu/test_deformable_conv_op_xpu.py +++ b/test/xpu/test_deformable_conv_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import OpTest, XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core def dmc_bilinear(data_im, height, width, h, w): @@ -162,10 +162,10 @@ def setUp(self): output = output.astype(self.dtype) self.inputs = { - 'Input': OpTest.np_dtype_to_fluid_dtype(input), - 'Offset': OpTest.np_dtype_to_fluid_dtype(offset), - 'Mask': OpTest.np_dtype_to_fluid_dtype(mask), - 'Filter': OpTest.np_dtype_to_fluid_dtype(filter), + 'Input': OpTest.np_dtype_to_base_dtype(input), + 'Offset': OpTest.np_dtype_to_base_dtype(offset), + 'Mask': OpTest.np_dtype_to_base_dtype(mask), + 'Filter': OpTest.np_dtype_to_base_dtype(filter), } self.attrs = { 'strides': self.stride, diff --git a/test/xpu/test_device_guard_xpu.py b/test/xpu/test_device_guard_xpu.py index cc9fb142279ac..ce85946aee74e 100644 --- a/test/xpu/test_device_guard_xpu.py +++ b/test/xpu/test_device_guard_xpu.py @@ -16,7 +16,7 @@ import warnings import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_diag_v2_op_xpu.py b/test/xpu/test_diag_v2_op_xpu.py index 51f42d00507fe..3c688337cb57a 100644 --- a/test/xpu/test_diag_v2_op_xpu.py +++ b/test/xpu/test_diag_v2_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -240,9 +240,9 @@ def run_static(self, use_gpu=False): result12 = paddle.diag(x5, offset=-1) result13 = paddle.diag(x6, offset=-1) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + place = base.XPUPlace(0) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ( res0, res1, @@ -299,12 +299,12 @@ def run_static(self, use_gpu=False): np.testing.assert_allclose(res13, self.expected12, rtol=1e-05) def test_xpu(self): - paddle.disable_static(place=paddle.fluid.XPUPlace(0)) + paddle.disable_static(place=paddle.base.XPUPlace(0)) self.run_imperative() paddle.enable_static() - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): self.run_static() diff --git a/test/xpu/test_dropout_op_xpu.py b/test/xpu/test_dropout_op_xpu.py index 931089d7a5656..345eeda034789 100644 --- a/test/xpu/test_dropout_op_xpu.py +++ b/test/xpu/test_dropout_op_xpu.py @@ -18,8 +18,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import _legacy_C_ops, fluid -from paddle.fluid import Program, program_guard +from paddle import _legacy_C_ops, base +from paddle.base import Program, program_guard paddle.enable_static() @@ -125,10 +125,10 @@ def test_errors(self): def test_Variable(): # the input of dropout must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], - fluid.CPUPlace(), + base.CPUPlace(), ) paddle.nn.functional.dropout(x1, p=0.5) @@ -147,15 +147,15 @@ def test_dtype(): class TestDropoutCAPI(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] - self.places.append(fluid.XPUPlace(0)) + self.places = [base.CPUPlace()] + self.places.append(base.XPUPlace(0)) def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.random.random([40, 40]).astype(self.in_type) result_np = input_np - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) m = paddle.nn.Dropout(p=0.0) m.eval() result = m(input) @@ -164,8 +164,8 @@ def test_dygraph(self): class TestDropoutBackward(unittest.TestCase): def setUp(self): np.random.seed(123) - self.places = [fluid.CPUPlace()] - self.places.append(fluid.XPUPlace(0)) + self.places = [base.CPUPlace()] + self.places.append(base.XPUPlace(0)) def cal_grad_upscale_train(self, mask, prob): return mask.astype(self.in_type) / (1 - prob) @@ -175,7 +175,7 @@ def cal_grad_downscale_in_infer(self, mask): def test_backward_downscale_in_infer(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input = paddle.uniform([40, 40], dtype=self.in_type) input.stop_gradient = False out, mask = _legacy_C_ops.dropout( @@ -190,7 +190,7 @@ def test_backward_downscale_in_infer(self): def test_backward_upscale_train(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): prob = 0.5 input = paddle.uniform([40, 40], dtype=self.in_type) input.stop_gradient = False @@ -210,7 +210,7 @@ def test_backward_upscale_train(self): def test_backward_upscale_train_2(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): prob = 0.3 input = paddle.uniform([40, 40], dtype=self.in_type) input.stop_gradient = False diff --git a/test/xpu/test_elementwise_add_op_xpu.py b/test/xpu/test_elementwise_add_op_xpu.py index e77b840064a87..796f936c16f87 100644 --- a/test/xpu/test_elementwise_add_op_xpu.py +++ b/test/xpu/test_elementwise_add_op_xpu.py @@ -25,7 +25,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -43,8 +43,8 @@ def setUp(self): self.init_axis() self.init_max_relative_error() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} @@ -261,7 +261,7 @@ def init_axis(self): class TestAddOp(unittest.TestCase): def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") y = paddle.static.data(name='y', shape=[2, 3], dtype='float32') @@ -269,7 +269,7 @@ def test_name(self): self.assertEqual(('add_res' in y_1.name), True) def test_declarative(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def gen_data(): return { @@ -281,18 +281,18 @@ def gen_data(): y = paddle.static.data(name="y", shape=[3], dtype='float32') z = paddle.add(x, y) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place) + place = base.XPUPlace(0) + exe = base.Executor(place) z_value = exe.run(feed=gen_data(), fetch_list=[z.name]) z_expected = np.array([3.0, 8.0, 6.0]) self.assertEqual((z_value == z_expected).all(), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 4]).astype('float32') np_y = np.array([1, 5, 2]).astype('float32') - x = fluid.dygraph.to_variable(np_x) - y = fluid.dygraph.to_variable(np_y) + x = base.dygraph.to_variable(np_x) + y = base.dygraph.to_variable(np_y) z = paddle.add(x, y) np_z = z.numpy() z_expected = np.array([3.0, 8.0, 6.0]) diff --git a/test/xpu/test_elementwise_add_op_xpu_kp.py b/test/xpu/test_elementwise_add_op_xpu_kp.py index 76daa814c4fc7..ff5c1afeaa902 100644 --- a/test/xpu/test_elementwise_add_op_xpu_kp.py +++ b/test/xpu/test_elementwise_add_op_xpu_kp.py @@ -20,7 +20,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -36,8 +36,8 @@ def setUp(self): self.init_axis() self.init_max_relative_error() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} @@ -308,7 +308,7 @@ def init_axis(self): ) class TestAddOp(unittest.TestCase): def test_name(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2, 3], dtype="float32") y = paddle.static.data(name='y', shape=[2, 3], dtype='float32') @@ -316,7 +316,7 @@ def test_name(self): self.assertEqual(('add_res' in y_1.name), True) def test_declarative(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def gen_data(): return { @@ -328,18 +328,18 @@ def gen_data(): y = paddle.static.data(name="y", shape=[3], dtype='float32') z = paddle.add(x, y) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place) + place = base.XPUPlace(0) + exe = base.Executor(place) z_value = exe.run(feed=gen_data(), fetch_list=[z.name]) z_expected = np.array([3.0, 8.0, 6.0]) self.assertEqual((z_value == z_expected).all(), True) def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.array([2, 3, 4]).astype('float32') np_y = np.array([1, 5, 2]).astype('float32') - x = fluid.dygraph.to_variable(np_x) - y = fluid.dygraph.to_variable(np_y) + x = base.dygraph.to_variable(np_x) + y = base.dygraph.to_variable(np_y) z = paddle.add(x, y) np_z = z.numpy() z_expected = np.array([3.0, 8.0, 6.0]) diff --git a/test/xpu/test_elementwise_div_op_xpu.py b/test/xpu/test_elementwise_div_op_xpu.py index 7ec429354204c..481bb6c9c0d7c 100644 --- a/test/xpu/test_elementwise_div_op_xpu.py +++ b/test/xpu/test_elementwise_div_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -404,13 +404,13 @@ def init_input_output(self): class TestElementwiseDivBroadcast(unittest.TestCase): def test_shape_with_batch_sizes(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x_var = paddle.static.data( name='x', dtype='float32', shape=[None, 3, None, None] ) one = 2.0 out = one / x_var - exe = fluid.Executor(fluid.XPUPlace(0)) + exe = base.Executor(base.XPUPlace(0)) x = np.random.uniform(0.1, 0.6, (1, 3, 32, 32)).astype( 'float32' ) diff --git a/test/xpu/test_elementwise_floordiv_op_xpu.py b/test/xpu/test_elementwise_floordiv_op_xpu.py index c626f63bbd68a..1fbe2b72f70b1 100644 --- a/test/xpu/test_elementwise_floordiv_op_xpu.py +++ b/test/xpu/test_elementwise_floordiv_op_xpu.py @@ -47,8 +47,8 @@ def setUp(self): self.init_axis() self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} self.outputs = {'Out': self.out} diff --git a/test/xpu/test_elementwise_mod_op_xpu.py b/test/xpu/test_elementwise_mod_op_xpu.py index 518df93ade8f6..06ed903b47201 100644 --- a/test/xpu/test_elementwise_mod_op_xpu.py +++ b/test/xpu/test_elementwise_mod_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -43,8 +43,8 @@ def init_input_output(self): self.y = np.random.uniform(0, 1000, [10, 10]).astype(self.dtype) self.out = np.mod(self.x, self.y) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} @@ -72,7 +72,7 @@ def test_check_output(self): class TestRemainderOp(unittest.TestCase): def test_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): np_x = np.random.rand(22, 128, 3).astype('int64') np_y = np.random.rand(22, 128, 3).astype('int64') x = paddle.to_tensor(np_x) diff --git a/test/xpu/test_elementwise_mul_op_xpu.py b/test/xpu/test_elementwise_mul_op_xpu.py index 9dc75c96647a3..fdb9673cf1c72 100644 --- a/test/xpu/test_elementwise_mul_op_xpu.py +++ b/test/xpu/test_elementwise_mul_op_xpu.py @@ -89,8 +89,8 @@ def init_input_output(self): self.y = np.random.uniform(0.1, 1, [13, 17]).astype(self.dtype) self.out = np.multiply(self.x, self.y) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(self.x), - 'Y': OpTest.np_dtype_to_fluid_dtype(self.y), + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), } self.outputs = {'Out': self.out} self.attrs = {'axis': self.axis, 'use_mkldnn': self.use_mkldnn} diff --git a/test/xpu/test_empty_op_xpu.py b/test/xpu/test_empty_op_xpu.py index 71c25f335b1ba..6f54f4ea01cbf 100644 --- a/test/xpu/test_empty_op_xpu.py +++ b/test/xpu/test_empty_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.base.framework import convert_np_dtype_to_dtype_ paddle.enable_static() diff --git a/test/xpu/test_expand_as_v2_op_xpu.py b/test/xpu/test_expand_as_v2_op_xpu.py index 41f345091054c..7ad70874c481d 100644 --- a/test/xpu/test_expand_as_v2_op_xpu.py +++ b/test/xpu/test_expand_as_v2_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() np.random.seed(10) @@ -141,9 +141,9 @@ def test_api(self): out_1 = paddle.expand_as(x, y=y) - exe = fluid.Executor(place=fluid.XPUPlace(0)) + exe = base.Executor(place=base.XPUPlace(0)) res_1 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x": x_np, "target_tensor": y_np}, fetch_list=[out_1], ) diff --git a/test/xpu/test_expand_v2_op_xpu.py b/test/xpu/test_expand_v2_op_xpu.py index ad5397ff3bcb2..4d0948a1d246f 100644 --- a/test/xpu/test_expand_v2_op_xpu.py +++ b/test/xpu/test_expand_v2_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() np.random.seed(10) @@ -195,7 +195,7 @@ def test_check_grad(self): # Test python API class TestExpandV2API(unittest.TestCase): def test_static(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = np.random.random([12, 14]).astype("float32") x = paddle.static.data( name='x', @@ -214,11 +214,11 @@ def test_static(self): out_2 = paddle.expand(x, shape=[positive_2, 14]) out_3 = paddle.expand(x, shape=expand_shape) - g0 = fluid.backward.calc_gradient(out_2, x) + g0 = base.backward.calc_gradient(out_2, x) - exe = fluid.Executor(place=paddle.XPUPlace(0)) + exe = base.Executor(place=paddle.XPUPlace(0)) res_1, res_2, res_3 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "x": input, "expand_shape": np.array([12, 14]).astype("int32"), diff --git a/test/xpu/test_fill_any_op_xpu.py b/test/xpu/test_fill_any_op_xpu.py index 824accc91297a..2d71f78e05c34 100644 --- a/test/xpu/test_fill_any_op_xpu.py +++ b/test/xpu/test_fill_any_op_xpu.py @@ -75,7 +75,7 @@ def init(self): class TestFillAnyInplace(unittest.TestCase): def test_fill_any_version(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): var = paddle.to_tensor(np.ones((4, 2, 3)).astype(np.float32)) self.assertEqual(var.inplace_version, 0) @@ -89,7 +89,7 @@ def test_fill_any_version(self): self.assertEqual(var.inplace_version, 3) def test_fill_any_eqaul(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): tensor = paddle.to_tensor( np.random.random((20, 30)).astype(np.float32) ) @@ -100,7 +100,7 @@ def test_fill_any_eqaul(self): self.assertEqual((tensor.numpy() == target).all().item(), True) def test_backward(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x = paddle.full([10, 10], -1.0, dtype='float32') x.stop_gradient = False y = 2 * x diff --git a/test/xpu/test_fill_op_xpu.py b/test/xpu/test_fill_op_xpu.py index ce2c33709b9f3..8adb6fc08b998 100644 --- a/test/xpu/test_fill_op_xpu.py +++ b/test/xpu/test_fill_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_fleet_exe_dist_model_run_xpu.py b/test/xpu/test_fleet_exe_dist_model_run_xpu.py index a55f82426bfa8..d78f16fb97bbc 100644 --- a/test/xpu/test_fleet_exe_dist_model_run_xpu.py +++ b/test/xpu/test_fleet_exe_dist_model_run_xpu.py @@ -19,7 +19,7 @@ import numpy as np import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_fused_attention_op_xpu.py b/test/xpu/test_fused_attention_op_xpu.py index 9db584f278e7f..063580457eeee 100644 --- a/test/xpu/test_fused_attention_op_xpu.py +++ b/test/xpu/test_fused_attention_op_xpu.py @@ -26,7 +26,7 @@ import paddle.incubate.nn.functional as incubate_f import paddle.nn.functional as F from paddle import tensor -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.nn.layer.common import Dropout, Linear from paddle.nn.layer.norm import LayerNorm from paddle.nn.layer.transformer import _convert_attention_mask diff --git a/test/xpu/test_fused_feedforward_op_xpu.py b/test/xpu/test_fused_feedforward_op_xpu.py index 11f7148e188d0..755c48a8b6d9b 100644 --- a/test/xpu/test_fused_feedforward_op_xpu.py +++ b/test/xpu/test_fused_feedforward_op_xpu.py @@ -20,7 +20,7 @@ import paddle import paddle.incubate.nn.functional as incubate_f import paddle.nn.functional as F -from paddle.fluid.framework import default_main_program +from paddle.base.framework import default_main_program from paddle.nn.layer import transformer from paddle.nn.layer.common import Dropout, Linear from paddle.nn.layer.norm import LayerNorm diff --git a/test/xpu/test_fused_gemm_epilogue_grad_op_xpu.py b/test/xpu/test_fused_gemm_epilogue_grad_op_xpu.py index 394fe515554f3..f60aed378afcd 100644 --- a/test/xpu/test_fused_gemm_epilogue_grad_op_xpu.py +++ b/test/xpu/test_fused_gemm_epilogue_grad_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core def get_outputs(DOut, X, Y): diff --git a/test/xpu/test_fused_gemm_epilogue_op_xpu.py b/test/xpu/test_fused_gemm_epilogue_op_xpu.py index 37b1271963faf..fe1d08e36bc39 100644 --- a/test/xpu/test_fused_gemm_epilogue_op_xpu.py +++ b/test/xpu/test_fused_gemm_epilogue_op_xpu.py @@ -25,7 +25,7 @@ import paddle from paddle import _legacy_C_ops -from paddle.fluid import core +from paddle.base import core def gelu(x): diff --git a/test/xpu/test_fused_resnet_basic_block_op_xpu.py b/test/xpu/test_fused_resnet_basic_block_op_xpu.py index 073d128150da9..709222496fcde 100644 --- a/test/xpu/test_fused_resnet_basic_block_op_xpu.py +++ b/test/xpu/test_fused_resnet_basic_block_op_xpu.py @@ -24,9 +24,9 @@ ) import paddle -from paddle import fluid, nn -from paddle.fluid import core -from paddle.fluid.framework import default_main_program +from paddle import base, nn +from paddle.base import core +from paddle.base.framework import default_main_program from paddle.incubate.xpu.resnet_block import ResNetBasicBlock paddle.enable_static() @@ -69,34 +69,34 @@ def getShortcut(self): def Base(self): paddle.disable_static() - conv1_weight = fluid.ParamAttr( + conv1_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - conv2_weight = fluid.ParamAttr( + conv2_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - conv3_weight = fluid.ParamAttr( + conv3_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - bn1_weight = fluid.ParamAttr( + bn1_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - bn1_bias = fluid.ParamAttr( + bn1_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) - bn2_weight = fluid.ParamAttr( + bn2_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - bn2_bias = fluid.ParamAttr( + bn2_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) - bn3_weight = fluid.ParamAttr( + bn3_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - bn3_bias = fluid.ParamAttr( + bn3_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) @@ -169,34 +169,34 @@ def Base(self): def FusedResNetBasicBlock(self): paddle.disable_static() - fused_conv1_weight = fluid.ParamAttr( + fused_conv1_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - fused_conv2_weight = fluid.ParamAttr( + fused_conv2_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - fused_conv3_weight = fluid.ParamAttr( + fused_conv3_weight = base.ParamAttr( initializer=paddle.nn.initializer.XavierNormal(), learning_rate=0.001, ) - fused_bn1_weight = fluid.ParamAttr( + fused_bn1_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - fused_bn1_bias = fluid.ParamAttr( + fused_bn1_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) - fused_bn2_weight = fluid.ParamAttr( + fused_bn2_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - fused_bn2_bias = fluid.ParamAttr( + fused_bn2_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) - fused_bn3_weight = fluid.ParamAttr( + fused_bn3_weight = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=1.0) ) - fused_bn3_bias = fluid.ParamAttr( + fused_bn3_bias = base.ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0) ) diff --git a/test/xpu/test_gaussian_random_op_xpu.py b/test/xpu/test_gaussian_random_op_xpu.py index f30b994dcd18b..abdec498f0a62 100644 --- a/test/xpu/test_gaussian_random_op_xpu.py +++ b/test/xpu/test_gaussian_random_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() from paddle.tensor import random @@ -234,9 +234,9 @@ def test_api(self): seed=10, ) - exe = fluid.Executor(place=fluid.XPUPlace(0)) + exe = base.Executor(place=base.XPUPlace(0)) res_1, res_2, res_3, res_4, res_5, res_6 = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={ "shape_tensor_int32": np.array([2000, 500]).astype("int32"), "shape_tensor_int64": np.array([2000, 500]).astype("int64"), @@ -263,17 +263,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.gaussian([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) test_default_fp64() test_default_fp32() @@ -289,17 +289,17 @@ def test_default_dtype(self): def test_default_fp16(): paddle.framework.set_default_dtype('float16') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP16) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP16) def test_default_fp32(): paddle.framework.set_default_dtype('float32') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP32) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP32) def test_default_fp64(): paddle.framework.set_default_dtype('float64') out = paddle.tensor.random.standard_normal([2, 3]) - self.assertEqual(out.dtype, fluid.core.VarDesc.VarType.FP64) + self.assertEqual(out.dtype, base.core.VarDesc.VarType.FP64) test_default_fp64() test_default_fp32() diff --git a/test/xpu/test_gen_bkcl_id_op.py b/test/xpu/test_gen_bkcl_id_op.py index 7c7ae3511a252..267d1bd581026 100644 --- a/test/xpu/test_gen_bkcl_id_op.py +++ b/test/xpu/test_gen_bkcl_id_op.py @@ -21,7 +21,7 @@ os.environ['GLOG_vmodule'] = "gen_bkcl_id_op*=10,gen_comm_id*=10" import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_generate_proposals_v2_op_xpu.py b/test/xpu/test_generate_proposals_v2_op_xpu.py index b7e22032f5f0c..024d09603b7d9 100644 --- a/test/xpu/test_generate_proposals_v2_op_xpu.py +++ b/test/xpu/test_generate_proposals_v2_op_xpu.py @@ -25,7 +25,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_group_norm_op_xpu.py b/test/xpu/test_group_norm_op_xpu.py index 3607fffdbfd15..bf0b528b6e8a7 100644 --- a/test/xpu/test_group_norm_op_xpu.py +++ b/test/xpu/test_group_norm_op_xpu.py @@ -78,9 +78,9 @@ def setUp(self): ) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(input), - 'Scale': OpTest.np_dtype_to_fluid_dtype(scale), - 'Bias': OpTest.np_dtype_to_fluid_dtype(bias), + 'X': OpTest.np_dtype_to_base_dtype(input), + 'Scale': OpTest.np_dtype_to_base_dtype(scale), + 'Bias': OpTest.np_dtype_to_base_dtype(bias), } self.outputs = {'Y': output, 'Mean': mean, 'Variance': var} self.attrs['data_layout'] = self.data_format diff --git a/test/xpu/test_huber_loss_op_xpu.py b/test/xpu/test_huber_loss_op_xpu.py index bedfac1a20c61..a41d4a250327c 100644 --- a/test/xpu/test_huber_loss_op_xpu.py +++ b/test/xpu/test_huber_loss_op_xpu.py @@ -58,8 +58,8 @@ def set_inputs(self): x = np.random.uniform(0, 1.0, shape).astype(self.dtype) y = np.random.uniform(0, 1.0, shape).astype(self.dtype) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } def set_attrs(self): diff --git a/test/xpu/test_index_sample_op_xpu.py b/test/xpu/test_index_sample_op_xpu.py index e5204a1247f46..f07c81fa42d23 100644 --- a/test/xpu/test_index_sample_op_xpu.py +++ b/test/xpu/test_index_sample_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -125,9 +125,9 @@ def test_shape(self): index = paddle.static.data(name='index', shape=[-1, 3], dtype='int32') output = paddle.index_sample(x=x, index=index) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place=place) - exe.run(fluid.default_startup_program()) + place = base.XPUPlace(0) + exe = base.Executor(place=place) + exe.run(base.default_startup_program()) feed = {'x': x_np, 'index': index_np} res = exe.run(feed=feed, fetch_list=[output]) @@ -135,7 +135,7 @@ def test_shape(self): class TestIndexSampleDynamic(unittest.TestCase): def test_result(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): x = paddle.to_tensor( [ [1.0, 2.0, 3.0, 4.0], diff --git a/test/xpu/test_index_select_op_xpu.py b/test/xpu/test_index_select_op_xpu.py index 62b9dd54c2e8b..4a4f0d3e47786 100644 --- a/test/xpu/test_index_select_op_xpu.py +++ b/test/xpu/test_index_select_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -101,7 +101,7 @@ def test_index_select_api(self): x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32') index = paddle.static.data(name='index', shape=[3], dtype='int32') z = paddle.index_select(x, index, axis=1) - exe = fluid.Executor(fluid.XPUPlace(0)) + exe = base.Executor(base.XPUPlace(0)) (res,) = exe.run( feed={'x': self.data_x, 'index': self.data_index}, fetch_list=[z.name], @@ -117,7 +117,7 @@ def test_index_select_api(self): x = paddle.static.data(name='x', shape=[-1, 4], dtype='float32') index = paddle.static.data(name='index', shape=[3], dtype='int32') z = paddle.index_select(x, index) - exe = fluid.Executor(fluid.XPUPlace(0)) + exe = base.Executor(base.XPUPlace(0)) (res,) = exe.run( feed={'x': self.data_x, 'index': self.data_index}, fetch_list=[z.name], @@ -131,9 +131,9 @@ def test_index_select_api(self): def test_dygraph_api(self): self.input_data() # case 1: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.index_select(x, index) np_z = z.numpy() expect_out = np.array( @@ -142,9 +142,9 @@ def test_dygraph_api(self): np.testing.assert_allclose(expect_out, np_z, rtol=1e-05) # case 2: - with fluid.dygraph.guard(): - x = fluid.dygraph.to_variable(self.data_x) - index = fluid.dygraph.to_variable(self.data_index) + with base.dygraph.guard(): + x = base.dygraph.to_variable(self.data_x) + index = base.dygraph.to_variable(self.data_index) z = paddle.index_select(x, index, axis=1) np_z = z.numpy() expect_out = np.array( diff --git a/test/xpu/test_instance_norm_op_xpu.py b/test/xpu/test_instance_norm_op_xpu.py index 06714984a1b6d..27ee3c0813a44 100644 --- a/test/xpu/test_instance_norm_op_xpu.py +++ b/test/xpu/test_instance_norm_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -176,8 +176,8 @@ def setUp(self): def test_errors(self): with program_guard(Program(), Program()): # the input of instance_norm must be Variable. - x1 = fluid.create_lod_tensor( - np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.XPUPlace(0) + x1 = base.create_lod_tensor( + np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], base.XPUPlace(0) ) self.assertRaises(TypeError, paddle.static.nn.instance_norm, x1) diff --git a/test/xpu/test_inverse_op_xpu.py b/test/xpu/test_inverse_op_xpu.py index 8ddd4636372a8..911d1974bf5d6 100644 --- a/test/xpu/test_inverse_op_xpu.py +++ b/test/xpu/test_inverse_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -76,10 +76,10 @@ def set_shape(self): class TestInverseSingularAPI(unittest.TestCase): def setUp(self): - self.places = [fluid.XPUPlace(0)] + self.places = [base.XPUPlace(0)] def check_static_result(self, place): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input = paddle.static.data( name="input", shape=[4, 4], dtype="float32" ) @@ -87,10 +87,10 @@ def check_static_result(self, place): input_np = np.ones([4, 4]).astype("float32") - exe = fluid.Executor(place) + exe = base.Executor(place) with self.assertRaises(OSError): fetches = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"input": input_np}, fetch_list=[result], ) @@ -101,9 +101,9 @@ def test_static(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): + with base.dygraph.guard(place): input_np = np.ones([4, 4]).astype("float32") - input = fluid.dygraph.to_variable(input_np) + input = base.dygraph.to_variable(input_np) with self.assertRaises(OSError): result = paddle.inverse(input) diff --git a/test/xpu/test_kldiv_loss_op_xpu.py b/test/xpu/test_kldiv_loss_op_xpu.py index e7ebe5e3f1f80..2879990916287 100644 --- a/test/xpu/test_kldiv_loss_op_xpu.py +++ b/test/xpu/test_kldiv_loss_op_xpu.py @@ -105,7 +105,7 @@ def run_kl_loss(self, reduction, shape=(5, 20)): target = np.random.uniform(-10, 10, shape).astype('float32') gt_loss = kldiv_loss(x, target, reduction) - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): kldiv_criterion = paddle.nn.KLDivLoss(reduction) pred_loss = kldiv_criterion( paddle.to_tensor(x), paddle.to_tensor(target) @@ -125,7 +125,7 @@ def test_kl_loss_static_api(self): class TestKLDivLossTypePromotion(unittest.TestCase): def test_kl_div_promotion(self): - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): x1 = paddle.rand([5, 20], dtype='float32') target1 = paddle.rand([5, 20], dtype='float32') diff --git a/test/xpu/test_logical_op_xpu.py b/test/xpu/test_logical_op_xpu.py index 175e1fe569bad..90d6ba1c9335c 100755 --- a/test/xpu/test_logical_op_xpu.py +++ b/test/xpu/test_logical_op_xpu.py @@ -58,8 +58,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -119,8 +119,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -180,8 +180,8 @@ def set_case(self): self.attrs = {'use_xpu': True} self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } self.outputs = {'Out': out} @@ -237,7 +237,7 @@ def set_case(self): out = np.logical_not(x) self.attrs = {'use_xpu': True} - self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.inputs = {'X': OpTest.np_dtype_to_base_dtype(x)} self.outputs = {'Out': out} def init_case(self): diff --git a/test/xpu/test_matmul_op_xpu.py b/test/xpu/test_matmul_op_xpu.py index fb30c2ecbe879..24369b79f8cab 100644 --- a/test/xpu/test_matmul_op_xpu.py +++ b/test/xpu/test_matmul_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base def reference_matmul(X, Y, transpose_X=False, transpose_Y=False): @@ -132,11 +132,11 @@ def __init__(self): class API_TestMm(unittest.TestCase): def test_out(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): x = paddle.static.data(name="x", shape=[2], dtype=self.in_type) y = paddle.static.data(name='y', shape=[2], dtype=self.in_type) result = paddle.mm(x, y) - exe = fluid.Executor(fluid.XPUPlace(0)) + exe = base.Executor(base.XPUPlace(0)) data1 = np.random.rand(2).astype(self.in_type) data2 = np.random.rand(2).astype(self.in_type) np_res = exe.run( @@ -147,12 +147,12 @@ def test_out(self): np.testing.assert_allclose(np_res, expected_result, atol=1e-3) def test_dygraph_without_out(self): - device = fluid.XPUPlace(0) - with fluid.dygraph.guard(device): + device = base.XPUPlace(0) + with base.dygraph.guard(device): input_array1 = np.random.rand(3, 4).astype(self.in_type) input_array2 = np.random.rand(4, 3).astype(self.in_type) - data1 = fluid.dygraph.to_variable(input_array1) - data2 = fluid.dygraph.to_variable(input_array2) + data1 = base.dygraph.to_variable(input_array1) + data2 = base.dygraph.to_variable(input_array2) out = paddle.mm(data1, data2) expected_result = np.matmul(input_array1, input_array2) np.testing.assert_allclose( @@ -161,14 +161,14 @@ def test_dygraph_without_out(self): class Test_API_Matmul(unittest.TestCase): def test_dygraph_without_out(self): - device = fluid.XPUPlace(0) - with fluid.dygraph.guard(device): + device = base.XPUPlace(0) + with base.dygraph.guard(device): input_array1 = np.random.rand(3, 4).astype(self.in_type) input_array2 = np.random.rand(4, 3).astype(self.in_type) - data1 = fluid.dygraph.to_variable(input_array1).astype( + data1 = base.dygraph.to_variable(input_array1).astype( self.in_type ) - data2 = fluid.dygraph.to_variable(input_array2).astype( + data2 = base.dygraph.to_variable(input_array2).astype( self.in_type ) out = paddle.matmul(data1, data2) @@ -180,7 +180,7 @@ def test_dygraph_without_out(self): class API_TestMmError(unittest.TestCase): def test_errors(self): def test_error1(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[10, 2], dtype="float32" ) @@ -192,7 +192,7 @@ def test_error1(): self.assertRaises(ValueError, test_error1) def test_error2(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[-1, 10, 2], dtype="float32" ) @@ -204,7 +204,7 @@ def test_error2(): test_error2() def test_error3(): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): data1 = paddle.static.data( name="data1", shape=[10, 10, 2], dtype="float32" ) diff --git a/test/xpu/test_mean_op_xpu.py b/test/xpu/test_mean_op_xpu.py index 66ed8d7edbce3..b55270ac274c8 100644 --- a/test/xpu/test_mean_op_xpu.py +++ b/test/xpu/test_mean_op_xpu.py @@ -18,7 +18,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard np.random.seed(10) diff --git a/test/xpu/test_merged_momentum_op_xpu.py b/test/xpu/test_merged_momentum_op_xpu.py index 8f3afc5a32697..e7b6008e43644 100644 --- a/test/xpu/test_merged_momentum_op_xpu.py +++ b/test/xpu/test_merged_momentum_op_xpu.py @@ -38,7 +38,7 @@ def setUp(self): def set_case(self): self.shapes = [[3, 4], [2, 7], [5, 6, 8]] - self.place = paddle.fluid.XPUPlace(0) + self.place = paddle.base.XPUPlace(0) self.seed = 1 def testalltype(self): diff --git a/test/xpu/test_merged_momentum_op_xpu_base.py b/test/xpu/test_merged_momentum_op_xpu_base.py index 62f534a1b41a6..e41c7fd4feeab 100644 --- a/test/xpu/test_merged_momentum_op_xpu_base.py +++ b/test/xpu/test_merged_momentum_op_xpu_base.py @@ -21,7 +21,7 @@ import numpy as np import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper def run_momentum_op( @@ -181,7 +181,7 @@ def setUp(self): paddle.enable_static() self.shapes = [[3, 4], [2, 7], [5, 6], [7, 8]] self.seed = 10 - self.place = paddle.fluid.XPUPlace(0) + self.place = paddle.base.XPUPlace(0) self.__class__.use_xpu = True def gen_rand_data(self, shapes, dtype): diff --git a/test/xpu/test_momentum_op_xpu.py b/test/xpu/test_momentum_op_xpu.py index 50854cdeb9fae..117a7ee34d2e9 100644 --- a/test/xpu/test_momentum_op_xpu.py +++ b/test/xpu/test_momentum_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_nearest_interp_op_xpu.py b/test/xpu/test_nearest_interp_op_xpu.py index 13958a331ddce..1165521339da1 100644 --- a/test/xpu/test_nearest_interp_op_xpu.py +++ b/test/xpu/test_nearest_interp_op_xpu.py @@ -423,14 +423,14 @@ def test_exception(self): def attr_data_format(): # for 4-D input, data_format can only be NCHW or NHWC - out = fluid.layers.resize_nearest( + out = base.layers.resize_nearest( input, out_shape=[4, 8], data_format="NDHWC") def attr_scale_type(): - out = fluid.layers.resize_nearest(input, scale="scale") + out = base.layers.resize_nearest(input, scale="scale") def attr_scale_value(): - out = fluid.layers.resize_nearest(input, scale=-0.3) + out = base.layers.resize_nearest(input, scale=-0.3) self.assertRaises(ValueError, attr_data_format) self.assertRaises(TypeError, attr_scale_type) diff --git a/test/xpu/test_one_hot_op_xpu.py b/test/xpu/test_one_hot_op_xpu.py index 941387b3eb1fb..322f5f29d2707 100644 --- a/test/xpu/test_one_hot_op_xpu.py +++ b/test/xpu/test_one_hot_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_one_hot_v2_op_xpu.py b/test/xpu/test_one_hot_v2_op_xpu.py index 80a60eed539c0..f32c3960c6dfc 100644 --- a/test/xpu/test_one_hot_v2_op_xpu.py +++ b/test/xpu/test_one_hot_v2_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -158,22 +158,22 @@ def test_api_with_dygraph(self): label = np.array( [np.random.randint(0, depth - 1) for i in range(6)] ).reshape([6, 1]) - with fluid.dygraph.guard(): + with base.dygraph.guard(): one_hot_label = paddle.nn.functional.one_hot( - x=fluid.dygraph.to_variable(label), num_classes=depth + x=base.dygraph.to_variable(label), num_classes=depth ) def _run(self, depth): label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64") one_hot_label = paddle.nn.functional.one_hot(x=label, num_classes=depth) - place = fluid.XPUPlace(0) + place = base.XPUPlace(0) label_data = np.array( [np.random.randint(0, 10 - 1) for i in range(6)] ).reshape([6, 1]) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run( feed={ 'label': label_data, @@ -185,7 +185,7 @@ def _run(self, depth): class BadInputTestOnehotV2(unittest.TestCase): def test_error(self): - with fluid.program_guard(fluid.Program()): + with base.program_guard(base.Program()): def test_bad_x(): label = paddle.static.data( diff --git a/test/xpu/test_pad3d_op_xpu.py b/test/xpu/test_pad3d_op_xpu.py index 2757ed1e3e70a..233986ef909b2 100644 --- a/test/xpu/test_pad3d_op_xpu.py +++ b/test/xpu/test_pad3d_op_xpu.py @@ -25,7 +25,7 @@ import paddle import paddle.nn.functional as F from paddle import nn -from paddle.fluid import Executor, Program, default_main_program, program_guard +from paddle.base import Executor, Program, default_main_program, program_guard paddle.enable_static() diff --git a/test/xpu/test_pad_op_xpu.py b/test/xpu/test_pad_op_xpu.py index 4f4d68ab73d0e..cd5b9ffb5f3d5 100644 --- a/test/xpu/test_pad_op_xpu.py +++ b/test/xpu/test_pad_op_xpu.py @@ -25,7 +25,7 @@ from test_attribute_var import UnittestBase import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard def pad_wrapper(x, paddings, pad_value): @@ -101,7 +101,7 @@ def init_test_case(self): class TestPadOpError(unittest.TestCase): def test_errors(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): with program_guard(Program(), Program()): input_data = np.random.random((2, 2)).astype("float32") @@ -123,7 +123,7 @@ def init_info(self): ) def test_static(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): main_prog = Program() starup_prog = Program() with program_guard(main_prog, starup_prog): @@ -182,7 +182,7 @@ def call_func(self, x): class TestPaddingValueTensor3(unittest.TestCase): def test_static(self): - with paddle.fluid.framework._static_guard(): + with paddle.base.framework._static_guard(): np_x = np.random.random((16, 16)).astype('float32') main_prog = Program() starup_prog = Program() diff --git a/test/xpu/test_parallel_dygraph_dataparallel.py b/test/xpu/test_parallel_dygraph_dataparallel.py index 11fa8405317dc..0070f8ade9802 100644 --- a/test/xpu/test_parallel_dygraph_dataparallel.py +++ b/test/xpu/test_parallel_dygraph_dataparallel.py @@ -19,7 +19,7 @@ import unittest import paddle -from paddle import fluid +from paddle import base from paddle.distributed.utils.launch_utils import ( TrainerProc, find_free_ports, @@ -109,8 +109,8 @@ def start_local_trainers( class TestMultipleXpus(unittest.TestCase): def run_mnist_2xpu(self, target_file_name, eager_mode=True): if ( - not fluid.core.is_compiled_with_xpu() - or fluid.core.get_xpu_device_count() == 0 + not base.core.is_compiled_with_xpu() + or base.core.get_xpu_device_count() == 0 ): return diff --git a/test/xpu/test_pool2d_op_xpu.py b/test/xpu/test_pool2d_op_xpu.py index d40c29a6683b8..5081fb31cb39b 100644 --- a/test/xpu/test_pool2d_op_xpu.py +++ b/test/xpu/test_pool2d_op_xpu.py @@ -319,7 +319,7 @@ def setUp(self): self.pool_type, self.padding_algorithm, ).astype(self.dtype) - self.inputs = {'X': XPUOpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': XPUOpTest.np_dtype_to_base_dtype(input)} self.attrs = { 'strides': self.strides, diff --git a/test/xpu/test_pool3d_op_xpu.py b/test/xpu/test_pool3d_op_xpu.py index c9441de646428..865029ad0d07d 100644 --- a/test/xpu/test_pool3d_op_xpu.py +++ b/test/xpu/test_pool3d_op_xpu.py @@ -313,7 +313,7 @@ def setUp(self): self.padding_algorithm, ).astype(self.dtype) - self.inputs = {'X': XPUOpTest.np_dtype_to_fluid_dtype(input)} + self.inputs = {'X': XPUOpTest.np_dtype_to_base_dtype(input)} self.attrs = { 'strides': self.strides, diff --git a/test/xpu/test_prelu_op_xpu.py b/test/xpu/test_prelu_op_xpu.py index 0a0ea28269722..d84ce2de8f9cb 100644 --- a/test/xpu/test_prelu_op_xpu.py +++ b/test/xpu/test_prelu_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program +from paddle import base +from paddle.base import Program paddle.enable_static() @@ -148,7 +148,7 @@ def init_attr(self): def prelu_t(x, mode, param_attr=None, name=None, data_format='NCHW'): - helper = fluid.layer_helper.LayerHelper('prelu', **locals()) + helper = base.layer_helper.LayerHelper('prelu', **locals()) alpha_shape = [1, x.shape[1], 1, 1] dtype = helper.input_dtype(input_param_name='x') alpha = helper.create_parameter( @@ -176,7 +176,7 @@ def setUp(self): def test_mode_error(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = prelu_t(x, 'any') @@ -185,7 +185,7 @@ def test_mode_error(self): def test_data_format_error1(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = prelu_t(x, 'channel', data_format='N') @@ -194,7 +194,7 @@ def test_data_format_error1(self): def test_data_format_error2(self): main_program = Program() - with fluid.program_guard(main_program, Program()): + with base.program_guard(main_program, Program()): x = paddle.static.data(name='x', shape=[2, 3, 4, 5]) try: y = paddle.static.nn.prelu(x, 'channel', data_format='N') diff --git a/test/xpu/test_randperm_op_xpu.py b/test/xpu/test_randperm_op_xpu.py index f28944e0009a2..ba6b3ae4ac412 100644 --- a/test/xpu/test_randperm_op_xpu.py +++ b/test/xpu/test_randperm_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core from paddle.static import Program, program_guard paddle.enable_static() diff --git a/test/xpu/test_refactor_op_xpu.py b/test/xpu/test_refactor_op_xpu.py index 9403a515097c7..51bad8cec2e82 100644 --- a/test/xpu/test_refactor_op_xpu.py +++ b/test/xpu/test_refactor_op_xpu.py @@ -25,7 +25,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() @@ -231,8 +231,8 @@ def set_inputs(self): x = np.random.uniform(0, 1.0, shape).astype(self.dtype) y = np.random.uniform(0, 1.0, shape).astype(self.dtype) self.inputs = { - 'X': OpTest.np_dtype_to_fluid_dtype(x), - 'Y': OpTest.np_dtype_to_fluid_dtype(y), + 'X': OpTest.np_dtype_to_base_dtype(x), + 'Y': OpTest.np_dtype_to_base_dtype(y), } def set_attrs(self): diff --git a/test/xpu/test_rmsprop_op_xpu.py b/test/xpu/test_rmsprop_op_xpu.py index f2897bde7399a..0e90de302af99 100644 --- a/test/xpu/test_rmsprop_op_xpu.py +++ b/test/xpu/test_rmsprop_op_xpu.py @@ -24,8 +24,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -165,7 +165,7 @@ def setup( ): np.random.seed(5) # fix seed - self.scope = fluid.global_scope() + self.scope = base.global_scope() self.place = place self.param_name = "param" @@ -332,7 +332,7 @@ def test_rmsprop(self): size = (128, 320) for place in places: for centered in [False, True]: - with fluid.scope_guard(core.Scope()): + with base.scope_guard(core.Scope()): self.check_with_place( place, is_sparse=False, centered=centered, size=size ) diff --git a/test/xpu/test_rnn_op_xpu.py b/test/xpu/test_rnn_op_xpu.py index eaeafc5e01c58..1029698d1d79f 100755 --- a/test/xpu/test_rnn_op_xpu.py +++ b/test/xpu/test_rnn_op_xpu.py @@ -28,7 +28,7 @@ from rnn_numpy import LSTM import paddle -from paddle.fluid import core +from paddle.base import core random.seed(2) np.set_printoptions(threshold=np.inf) diff --git a/test/xpu/test_roi_align_op_xpu.py b/test/xpu/test_roi_align_op_xpu.py index d65f78be1a488..78712e1211f9c 100644 --- a/test/xpu/test_roi_align_op_xpu.py +++ b/test/xpu/test_roi_align_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() diff --git a/test/xpu/test_scale_op_xpu.py b/test/xpu/test_scale_op_xpu.py index 8ad7800fe2311..df7f59be8f7a9 100644 --- a/test/xpu/test_scale_op_xpu.py +++ b/test/xpu/test_scale_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard class XPUTestScaleOp(XPUOpTestWrapper): diff --git a/test/xpu/test_sequence_conv_op_xpu.py b/test/xpu/test_sequence_conv_op_xpu.py index 4a52ea54f4aff..1b0816938a248 100644 --- a/test/xpu/test_sequence_conv_op_xpu.py +++ b/test/xpu/test_sequence_conv_op_xpu.py @@ -429,18 +429,18 @@ def init_test_case(self): class TestSeqConvApi(unittest.TestCase): def test_api(self): - from paddle import fluid + from paddle import base x = paddle.static.data('x', shape=[-1, 32], lod_level=1) y = paddle.static.nn.sequence_lod.sequence_conv( input=x, num_filters=2, filter_size=3, padding_start=None ) - place = fluid.CPUPlace() - x_tensor = fluid.create_lod_tensor( + place = base.CPUPlace() + x_tensor = base.create_lod_tensor( np.random.rand(10, 32).astype("float32"), [[2, 3, 1, 4]], place ) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) + exe = base.Executor(place) + exe.run(base.default_startup_program()) ret = exe.run(feed={'x': x_tensor}, fetch_list=[y], return_numpy=False) diff --git a/test/xpu/test_sequence_unpad_op_xpu.py b/test/xpu/test_sequence_unpad_op_xpu.py index 15215fcb0c614..0a61d8b22ec96 100644 --- a/test/xpu/test_sequence_unpad_op_xpu.py +++ b/test/xpu/test_sequence_unpad_op_xpu.py @@ -100,7 +100,7 @@ def initTestCase(self): class TestSequenceUnpadOpError(unittest.TestCase): def test_error(self): """ - The type of 'x' in paddle.static.nn.sequence_unpad must be , but received . + The type of 'x' in paddle.static.nn.sequence_unpad must be , but received . """ def test_x_variable(): @@ -110,7 +110,7 @@ def test_x_variable(): self.assertRaises(TypeError, test_x_variable) """ - The type of 'length' in fluid.layers.sequence_unpad must be , but received . + The type of 'length' in base.layers.sequence_unpad must be , but received . """ def test_length_variable(): @@ -120,7 +120,7 @@ def test_length_variable(): self.assertRaises(TypeError, test_length_variable) """ - The data type of 'x' in fluid.layers.sequence_unpad must be ['float32', 'float64', 'int32', 'int64'], but received float16 + The data type of 'x' in base.layers.sequence_unpad must be ['float32', 'float64', 'int32', 'int64'], but received float16 """ def test_x_dtype(): @@ -130,7 +130,7 @@ def test_x_dtype(): self.assertRaises(TypeError, test_x_dtype) """ - The data type of 'length' in fluid.layers.sequence_unpad must be ['int64'], but received int32 + The data type of 'length' in base.layers.sequence_unpad must be ['int64'], but received int32 """ def test_length_dtype(): diff --git a/test/xpu/test_set_value_op_xpu.py b/test/xpu/test_set_value_op_xpu.py index 1cc4bb4fcd4f4..ba69ff178b738 100644 --- a/test/xpu/test_set_value_op_xpu.py +++ b/test/xpu/test_set_value_op_xpu.py @@ -29,7 +29,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid.layer_helper import LayerHelper +from paddle.base.layer_helper import LayerHelper class XPUTestSetValueOp(XPUOpTestWrapper): @@ -1638,7 +1638,7 @@ def setUp(self): def test_inplace(self): paddle.disable_static() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) a.stop_gradient = False @@ -1662,7 +1662,7 @@ def test_inplace_var_become_leaf_var(self): paddle.disable_static() a_grad_1, b_grad_1, a_grad_2, b_grad_2 = 0, 1, 2, 3 - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) b = paddle.rand(shape=[1, 4]) @@ -1673,7 +1673,7 @@ def test_inplace_var_become_leaf_var(self): a_grad_1 = a.grad.numpy() b_grad_1 = b.grad.numpy() - with paddle.fluid.dygraph.guard(): + with paddle.base.dygraph.guard(): paddle.seed(100) a = paddle.rand(shape=[1, 4]) b = paddle.rand(shape=[1, 4]) diff --git a/test/xpu/test_sgd_op_xpu.py b/test/xpu/test_sgd_op_xpu.py index 7720b992060bc..130bd76c897b4 100644 --- a/test/xpu/test_sgd_op_xpu.py +++ b/test/xpu/test_sgd_op_xpu.py @@ -24,8 +24,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core class XPUTestSgdOp(XPUOpTestWrapper): @@ -80,9 +80,9 @@ def runTest(self): sgd_optimizer.minimize(avg_cost) place = paddle.XPUPlace(0) - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - result = exe.run(fluid.default_main_program(), fetch_list=[avg_cost]) + exe = base.Executor(place) + exe.run(base.default_startup_program()) + result = exe.run(base.default_main_program(), fetch_list=[avg_cost]) class TestSparseSGDOp(unittest.TestCase): diff --git a/test/xpu/test_shape_op_xpu.py b/test/xpu/test_shape_op_xpu.py index d90b53a5c7a2b..f2a9d439a6311 100644 --- a/test/xpu/test_shape_op_xpu.py +++ b/test/xpu/test_shape_op_xpu.py @@ -24,7 +24,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() @@ -103,7 +103,7 @@ def check_with_place(self, place): def test_check_output(self): for place in self.get_places(): if ( - type(place) is paddle.fluid.libpaddle.CPUPlace + type(place) is paddle.base.libpaddle.CPUPlace and self.dtype == np.float16 ): # fp16 not available on cpu diff --git a/test/xpu/test_squeeze_op_xpu.py b/test/xpu/test_squeeze_op_xpu.py index 5aae366c85635..c5b9efce7a770 100644 --- a/test/xpu/test_squeeze_op_xpu.py +++ b/test/xpu/test_squeeze_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -106,7 +106,7 @@ def test_errors(self): paddle.enable_static() with program_guard(Program(), Program()): # The input type of softmax_op must be Variable. - x1 = fluid.create_lod_tensor( + x1 = base.create_lod_tensor( np.array([[-1]]), [[1]], paddle.XPUPlace(0) ) self.assertRaises(TypeError, paddle.squeeze, x1) diff --git a/test/xpu/test_sum_op_xpu.py b/test/xpu/test_sum_op_xpu.py index 3b51b0adb76d0..f582d6322362b 100644 --- a/test/xpu/test_sum_op_xpu.py +++ b/test/xpu/test_sum_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core paddle.enable_static() @@ -100,7 +100,7 @@ def test_w_is_selected_rows(self): class API_Test_Add_n(unittest.TestCase): def test_api(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): + with base.program_guard(base.Program(), base.Program()): input0 = paddle.tensor.fill_constant( shape=[2, 3], dtype='int64', value=5 ) @@ -110,12 +110,12 @@ def test_api(self): expected_result = np.empty((2, 3)) expected_result.fill(8) sum_value = paddle.add_n([input0, input1]) - exe = fluid.Executor(fluid.XPUPlace(0)) + exe = base.Executor(base.XPUPlace(0)) result = exe.run(fetch_list=[sum_value]) self.assertEqual((result == expected_result).all(), True) - with fluid.dygraph.guard(): + with base.dygraph.guard(): input0 = paddle.ones(shape=[2, 3], dtype='float32') expected_result = np.empty((2, 3)) expected_result.fill(2) @@ -193,12 +193,12 @@ def test_out_dtype(): class TestSumOpError(unittest.TestCase): def test_errors(self): def test_empty_list_input(): - with fluid.dygraph.guard(): - fluid._legacy_C_ops.sum([]) + with base.dygraph.guard(): + base._legacy_C_ops.sum([]) def test_list_of_none_input(): - with fluid.dygraph.guard(): - fluid._legacy_C_ops.sum([None]) + with base.dygraph.guard(): + base._legacy_C_ops.sum([None]) self.assertRaises(Exception, test_empty_list_input) self.assertRaises(Exception, test_list_of_none_input) diff --git a/test/xpu/test_tile_op_xpu.py b/test/xpu/test_tile_op_xpu.py index ae2e1b2f0d7dd..eee06d0e7c3bd 100644 --- a/test/xpu/test_tile_op_xpu.py +++ b/test/xpu/test_tile_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() np.random.seed(10) @@ -205,7 +205,7 @@ def init_data(self): # Test python API class TestTileAPI(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(paddle.XPUPlace(0)): + with base.dygraph.guard(paddle.XPUPlace(0)): np_x = np.random.random([12, 14]).astype("float32") x = paddle.to_tensor(np_x) diff --git a/test/xpu/test_truncated_gaussian_random_op_xpu.py b/test/xpu/test_truncated_gaussian_random_op_xpu.py index cad48d44d92bc..d412c24cb3cce 100644 --- a/test/xpu/test_truncated_gaussian_random_op_xpu.py +++ b/test/xpu/test_truncated_gaussian_random_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base.executor import Executor paddle.enable_static() @@ -58,10 +58,10 @@ def set_attrs(self): self.std = 1.0 def test_check_output(self): - self.gaussian_random_test(place=fluid.XPUPlace(0)) + self.gaussian_random_test(place=base.XPUPlace(0)) def gaussian_random_test(self, place): - program = fluid.Program() + program = base.Program() block = program.global_block() vout = block.create_var(name="Out") op = block.append_op( diff --git a/test/xpu/test_unbind_op_xpu.py b/test/xpu/test_unbind_op_xpu.py index 3ec10511a7e90..bb3d3a027b2f1 100644 --- a/test/xpu/test_unbind_op_xpu.py +++ b/test/xpu/test_unbind_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid, tensor -from paddle.fluid import Program, program_guard +from paddle import base, tensor +from paddle.base import Program, program_guard paddle.enable_static() @@ -42,10 +42,10 @@ def test_unbind(self): [out_0, out_1] = tensor.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype(self.dtype) axis = paddle.static.data(shape=[], dtype='int32', name='axis') - exe = fluid.Executor(place=self.place) + exe = base.Executor(place=self.place) [res_1, res_2] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_1, "axis": 0}, fetch_list=[out_0, out_1], ) @@ -54,7 +54,7 @@ def test_unbind(self): np.testing.assert_array_equal(res_2, input_1[1, 0:100]) def test_unbind_dygraph(self): - with fluid.dygraph.guard(): + with base.dygraph.guard(): self.dtype = self.in_type self.place = paddle.XPUPlace(0) np_x = np.random.random([2, 3]).astype(self.dtype) @@ -81,10 +81,10 @@ def test_layers_unbind(self): [out_0, out_1] = paddle.unbind(input=x_1, axis=0) input_1 = np.random.random([2, 3]).astype(self.dtype) axis = paddle.static.data(shape=[], dtype='int32', name='axis') - exe = fluid.Executor(place=self.place) + exe = base.Executor(place=self.place) [res_1, res_2] = exe.run( - fluid.default_main_program(), + base.default_main_program(), feed={"x_1": input_1, "axis": 0}, fetch_list=[out_0, out_1], ) diff --git a/test/xpu/test_unfold_op_xpu.py b/test/xpu/test_unfold_op_xpu.py index c6e80469f7d0d..82faf188d7ae4 100644 --- a/test/xpu/test_unfold_op_xpu.py +++ b/test/xpu/test_unfold_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base paddle.enable_static() @@ -158,8 +158,8 @@ def setUp(self): def test_dygraph(self): for place in self.places: - with fluid.dygraph.guard(place): - input = fluid.dygraph.to_variable(self.inputs['X']) + with base.dygraph.guard(place): + input = base.dygraph.to_variable(self.inputs['X']) m = paddle.nn.Unfold(**self.attrs) m.eval() result = m(input) diff --git a/test/xpu/test_unique_op_xpu.py b/test/xpu/test_unique_op_xpu.py index 691c4bea4b0b3..65d1333b37078 100644 --- a/test/xpu/test_unique_op_xpu.py +++ b/test/xpu/test_unique_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle.fluid import core +from paddle.base import core paddle.enable_static() @@ -50,7 +50,7 @@ def init_config(self): 'dtype': int(core.VarDesc.VarType.INT32), 'return_index': True, 'return_inverse': True, - 'is_sorted': True, # is_sorted must be set to true to call paddle.unique rather than fluid.layers.unique + 'is_sorted': True, # is_sorted must be set to true to call paddle.unique rather than base.layers.unique } self.outputs = { 'Out': np.array([1, 2, 3, 5], dtype=self.dtype), diff --git a/test/xpu/test_update_loss_scaling_op_xpu.py b/test/xpu/test_update_loss_scaling_op_xpu.py index c8e398a3d7782..c8ef942dc0c0b 100644 --- a/test/xpu/test_update_loss_scaling_op_xpu.py +++ b/test/xpu/test_update_loss_scaling_op_xpu.py @@ -23,7 +23,7 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid +from paddle import base from paddle.static.amp import amp_nn paddle.enable_static() @@ -108,7 +108,7 @@ def test_check_output(self): # self.check_output() class TestUpdateLossScalingLayer(unittest.TestCase): - def loss_scaling_check(self, scope=fluid.Scope()): + def loss_scaling_check(self, scope=base.Scope()): a = paddle.static.data( name="a", shape=[1024, 1024], dtype='float32' ) @@ -152,10 +152,10 @@ def loss_scaling_check(self, scope=fluid.Scope()): name="update_loss_scaling", ) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place) - with fluid.scope_guard(scope): - exe.run(fluid.default_startup_program()) + place = base.XPUPlace(0) + exe = base.Executor(place) + with base.scope_guard(scope): + exe.run(base.default_startup_program()) result_v = exe.run( feed={ 'a': a_v, @@ -189,7 +189,7 @@ def loss_scaling_check(self, scope=fluid.Scope()): result_v[7], np.zeros_like(num_bad_steps_v) ) - def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): + def loss_scaling_check_inf(self, use_cuda=True, scope=base.Scope()): a = paddle.static.data( name="a", shape=[1024, 1024], dtype='float32' ) @@ -236,10 +236,10 @@ def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): name="update_loss_scaling", ) - place = fluid.XPUPlace(0) - exe = fluid.Executor(place) - with fluid.scope_guard(scope): - exe.run(fluid.default_startup_program()) + place = base.XPUPlace(0) + exe = base.Executor(place) + with base.scope_guard(scope): + exe.run(base.default_startup_program()) result_v = exe.run( feed={ 'a': a_v, @@ -274,17 +274,17 @@ def loss_scaling_check_inf(self, use_cuda=True, scope=fluid.Scope()): ) def test_loss_scaling(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check() def test_loss_scaling_inf(self): - main = fluid.Program() - startup = fluid.Program() - with fluid.unique_name.guard(): - with fluid.program_guard(main, startup): + main = base.Program() + startup = base.Program() + with base.unique_name.guard(): + with base.program_guard(main, startup): self.loss_scaling_check_inf() diff --git a/test/xpu/test_warpctc_op_xpu.py b/test/xpu/test_warpctc_op_xpu.py index d486ec009dc72..50a9ed961c86a 100644 --- a/test/xpu/test_warpctc_op_xpu.py +++ b/test/xpu/test_warpctc_op_xpu.py @@ -26,7 +26,7 @@ import paddle import paddle.nn.functional as F -from paddle.fluid import Program, program_guard +from paddle.base import Program, program_guard paddle.enable_static() diff --git a/test/xpu/test_where_index_xpu.py b/test/xpu/test_where_index_xpu.py index 70c4a3454e8a4..6d0cf79032ef6 100644 --- a/test/xpu/test_where_index_xpu.py +++ b/test/xpu/test_where_index_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid import Program, program_guard +from paddle import base +from paddle.base import Program, program_guard paddle.enable_static() @@ -100,10 +100,10 @@ def test_api(self): cond = paddle.static.data(name='cond', shape=[-1, 4], dtype='bool') result = paddle.nonzero(cond) - exe = fluid.Executor(paddle.XPUPlace(0)) - exe.run(fluid.default_startup_program()) + exe = base.Executor(paddle.XPUPlace(0)) + exe.run(base.default_startup_program()) cond_i = np.array([True, False, False, False]).astype("bool") - out = exe.run(fluid.default_main_program(), feed={'cond': cond_i}) + out = exe.run(base.default_main_program(), feed={'cond': cond_i}) class TestWhereRaiseError(unittest.TestCase): diff --git a/test/xpu/test_where_op_xpu.py b/test/xpu/test_where_op_xpu.py index 13ec8c8c446a7..5a740f8dee5e9 100644 --- a/test/xpu/test_where_op_xpu.py +++ b/test/xpu/test_where_op_xpu.py @@ -23,8 +23,8 @@ from op_test_xpu import XPUOpTest import paddle -from paddle import fluid -from paddle.fluid.backward import append_backward +from paddle import base +from paddle.base.backward import append_backward paddle.enable_static() @@ -99,9 +99,9 @@ def ref_y_backward(self, dout): def test_api(self): for x_stop_gradient in [False, True]: for y_stop_gradient in [False, True]: - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): cond = paddle.static.data( name='cond', shape=self.shape, dtype='bool' ) @@ -119,7 +119,7 @@ def test_api(self): result.stop_gradient = False append_backward(paddle.mean(result)) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) fetch_list = [result, result.grad_name] @@ -148,9 +148,9 @@ def test_api(self): ) def test_api_broadcast(self, use_cuda=False): - train_prog = fluid.Program() - startup = fluid.Program() - with fluid.program_guard(train_prog, startup): + train_prog = base.Program() + startup = base.Program() + with base.program_guard(train_prog, startup): x = paddle.static.data(name='x', shape=[-1, 4, 1], dtype='float32') y = paddle.static.data(name='y', shape=[-1, 4, 2], dtype='float32') x_i = np.array([[0.9383, 0.1983, 3.2, 1.2]]).astype("float32") @@ -159,7 +159,7 @@ def test_api_broadcast(self, use_cuda=False): ) result = paddle.where(x > 1, x=x, y=y) - exe = fluid.Executor(self.place) + exe = base.Executor(self.place) exe.run(startup) out = exe.run( @@ -170,13 +170,13 @@ def test_api_broadcast(self, use_cuda=False): class TestWhereDygraphAPI(unittest.TestCase): def test_api(self): - with fluid.dygraph.guard(paddle.XPUPlace(0)): + with base.dygraph.guard(paddle.XPUPlace(0)): x_i = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float32") y_i = np.array([1.0, 1.0, 1.0, 1.0]).astype("float32") cond_i = np.array([False, False, True, True]).astype("bool") - x = fluid.dygraph.to_variable(x_i) - y = fluid.dygraph.to_variable(y_i) - cond = fluid.dygraph.to_variable(cond_i) + x = base.dygraph.to_variable(x_i) + y = base.dygraph.to_variable(y_i) + cond = base.dygraph.to_variable(cond_i) out = paddle.where(cond, x, y) np.testing.assert_array_equal( out.numpy(), np.where(cond_i, x_i, y_i) diff --git a/test/xpu/test_while_op_xpu.py b/test/xpu/test_while_op_xpu.py index e151298ca4f43..e8fa27315021e 100644 --- a/test/xpu/test_while_op_xpu.py +++ b/test/xpu/test_while_op_xpu.py @@ -17,9 +17,9 @@ import numpy import paddle -from paddle import fluid -from paddle.fluid.backward import append_backward -from paddle.fluid.executor import Executor +from paddle import base +from paddle.base.backward import append_backward +from paddle.base.executor import Executor paddle.enable_static() @@ -76,9 +76,9 @@ def simple_net(self): return loss, sum_result def test_simple_net(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): loss, sum_result = self.simple_net() append_backward(loss) @@ -97,11 +97,11 @@ def test_simple_net(self): self.assertAlmostEqual(numpy.sum(d), numpy.sum(outs[0]), delta=0.01) def test_simple_net_forward(self): - main_program = fluid.Program() - startup_program = fluid.Program() - with fluid.program_guard(main_program, startup_program): + main_program = base.Program() + startup_program = base.Program() + with base.program_guard(main_program, startup_program): self.simple_net() - binary = fluid.compiler.CompiledProgram(main_program) + binary = base.compiler.CompiledProgram(main_program) xpu_place = paddle.XPUPlace(0) exe = Executor(xpu_place) diff --git a/test/xpu/test_xpu_place.py b/test/xpu/test_xpu_place.py index f401d721c0805..08fc3ff627aa0 100644 --- a/test/xpu/test_xpu_place.py +++ b/test/xpu/test_xpu_place.py @@ -16,8 +16,8 @@ import unittest import paddle -from paddle import fluid, static -from paddle.fluid import core +from paddle import base, static +from paddle.base import core class Test_XPU_Places(unittest.TestCase): @@ -31,12 +31,12 @@ def test_check_preset_envs(self): if core.is_compiled_with_xpu(): os.environ["FLAGS_selected_xpus"] = "0" place_list = static.xpu_places() - self.assert_places_equal([fluid.XPUPlace(0)], place_list) + self.assert_places_equal([base.XPUPlace(0)], place_list) def test_check_no_preset_envs(self): if core.is_compiled_with_xpu(): place_list = static.xpu_places(0) - self.assert_places_equal([fluid.XPUPlace(0)], place_list) + self.assert_places_equal([base.XPUPlace(0)], place_list) if __name__ == '__main__': diff --git a/tools/CrossStackProfiler/ProfileFileReader.py b/tools/CrossStackProfiler/ProfileFileReader.py index ad6e44c6ac003..af955bd6652c4 100755 --- a/tools/CrossStackProfiler/ProfileFileReader.py +++ b/tools/CrossStackProfiler/ProfileFileReader.py @@ -26,7 +26,7 @@ getLogger, ) -from paddle.fluid.proto.profiler import profiler_pb2 +from paddle.base.proto.profiler import profiler_pb2 class profileFileReader(FileReader): diff --git a/tools/check_file_diff_approvals.sh b/tools/check_file_diff_approvals.sh index 6395c2d5ef27e..d467ae1cc4671 100644 --- a/tools/check_file_diff_approvals.sh +++ b/tools/check_file_diff_approvals.sh @@ -38,28 +38,28 @@ API_FILES=("CMakeLists.txt" "python/paddle/distributed/fleet/__init__.py" "python/paddle/distributed/fleet/launch.py" "python/requirements.txt" - "python/paddle/fluid/__init__.py" - "python/paddle/fluid/compiler.py" - "python/paddle/fluid/parallel_executor.py" - "python/paddle/fluid/framework.py" - "python/paddle/fluid/backward.py" + "python/paddle/base/__init__.py" + "python/paddle/base/compiler.py" + "python/paddle/base/parallel_executor.py" + "python/paddle/base/framework.py" + "python/paddle/base/backward.py" "paddle/fluid/operators/distributed/send_recv.proto.in" "paddle/fluid/framework/unused_var_check.cc" - "python/paddle/fluid/tests/unittests/white_list/check_shape_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/op_accuracy_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/compile_vs_runtime_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/no_check_set_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/check_op_sequence_instance_0_input_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/op_threshold_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/check_op_sequence_batch_1_input_white_list.py" - "python/paddle/fluid/tests/unittests/white_list/no_grad_set_white_list.py" + "python/paddle/base/tests/unittests/white_list/check_shape_white_list.py" + "python/paddle/base/tests/unittests/white_list/op_accuracy_white_list.py" + "python/paddle/base/tests/unittests/white_list/compile_vs_runtime_white_list.py" + "python/paddle/base/tests/unittests/white_list/no_check_set_white_list.py" + "python/paddle/base/tests/unittests/white_list/check_op_sequence_instance_0_input_white_list.py" + "python/paddle/base/tests/unittests/white_list/op_threshold_white_list.py" + "python/paddle/base/tests/unittests/white_list/check_op_sequence_batch_1_input_white_list.py" + "python/paddle/base/tests/unittests/white_list/no_grad_set_white_list.py" "tools/print_signatures.py" "tools/sampcd_processor.py" "tools/check_pr_approval.py" "paddle/scripts/paddle_build.bat" "tools/windows/run_unittests.sh" "tools/parallel_UT_rule.py" - "python/paddle/fluid/dygraph/layers.py" + "python/paddle/base/dygraph/layers.py" "paddle/fluid/eager/grad_node_info.h" "paddle/fluid/eager/grad_node_info.cc" "paddle/fluid/eager/grad_tensor_holder.h" @@ -142,8 +142,8 @@ for API_FILE in ${API_FILES[*]}; do elif [ "${API_FILE}" == "third_party" ];then echo_line="You must have one RD (risemeup1 or tianshuo78520a) approval for ${API_FILE}.\n" check_approval 1 risemeup1 tianshuo78520a - elif [ "${API_FILE}" == "python/paddle/fluid/__init__.py" ];then - echo_line="You must have one RD (lanxianghit (Recommend), phlrain, luotao1, Aurelius84 or qili93) approval for the python/paddle/fluid/init.py, which manages the environment variables.\n" + elif [ "${API_FILE}" == "python/paddle/base/__init__.py" ];then + echo_line="You must have one RD (lanxianghit (Recommend), phlrain, luotao1, Aurelius84 or qili93) approval for the python/paddle/base/init.py, which manages the environment variables.\n" check_approval 1 lanxianghit phlrain luotao1 Aurelius84 qili93 elif [ "${API_FILE}" == "python/requirements.txt" ];then echo_line="You must have one RD (phlrain) and one TPM (dingjiaweiww) and one QA (kolinwei) approval for python/requirements.txt, which manages the third-party python package.\n" @@ -154,29 +154,29 @@ for API_FILE in ${API_FILES[*]}; do elif [ "${API_FILE}" == "paddle/fluid/framework/unused_var_check.cc" ];then echo_line="You must have one RD (zhiqiu (Recommend) or chenwhql) approval for the changes of paddle/fluid/framework/unused_var_check.cc, which manages the allow list of operators that have unused input variables. Before change the allow list, please read the specification [https://github.com/PaddlePaddle/Paddle/wiki/OP-Should-Not-Have-Unused-Input] and try to refine code first. \n" check_approval 1 zhiqiu chenwhql - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/check_shape_white_list.py" ];then + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/check_shape_white_list.py" ];then echo_line="It is an Op accuracy problem, please take care of it. You must have one RD (hong19860320 (Recommend), luotao1, Aurelisu84, phlrain) approval for the changes of check_shape_white_list.py, which manages the white list of operators with limited input size. Inputs size of all cases in the op test must be greater than or equal to 100. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/OP-Test-Input-Shape-Requirements. \n" check_approval 1 hong19860320 luotao1 Aurelisu84 phlrain - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/op_accuracy_white_list.py" ];then - echo_line="It is an Op accuracy problem, please take care of it. You must have one RD (juncaipeng (Recommend), zhangting2020 (Recommend) or luotao1 or Aurelius84) approval for the python/paddle/fluid/tests/unittests/white_list/op_accuracy_white_list.py, which manages the white list of upgrading the precision of op test to float64. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Upgrade-OP-Precision-to-Float64. \n" + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/op_accuracy_white_list.py" ];then + echo_line="It is an Op accuracy problem, please take care of it. You must have one RD (juncaipeng (Recommend), zhangting2020 (Recommend) or luotao1 or Aurelius84) approval for the python/paddle/base/tests/unittests/white_list/op_accuracy_white_list.py, which manages the white list of upgrading the precision of op test to float64. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Upgrade-OP-Precision-to-Float64. \n" check_approval 1 juncaipeng zhangting2020 luotao1 Aurelius84 - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/compile_vs_runtime_white_list.py" ];then - echo_line="You must have one RD (DannyIsFunny (Recommend), luotao1, Aurelius84, phlrain) approval for the python/paddle/fluid/tests/unittests/white_list/compile_vs_runtime_white_list.py, which manages the white list of compile&runtime lod-level check. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Compile_vs_Runtime-Check-Specification. \n" + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/compile_vs_runtime_white_list.py" ];then + echo_line="You must have one RD (DannyIsFunny (Recommend), luotao1, Aurelius84, phlrain) approval for the python/paddle/base/tests/unittests/white_list/compile_vs_runtime_white_list.py, which manages the white list of compile&runtime lod-level check. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Compile_vs_Runtime-Check-Specification. \n" check_approval 1 DannyIsFunny luotao1 Aurelius84 phlrain - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/no_check_set_white_list.py" ];then - echo_line="You must have one RD (cryoco (Recommend), luotao1, Aurelius84 or phlrain) approval for the python/paddle/fluid/tests/unittests/white_list/no_check_set_white_list.py, which manages the white list of setting no_check_set of check_output. \n" + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/no_check_set_white_list.py" ];then + echo_line="You must have one RD (cryoco (Recommend), luotao1, Aurelius84 or phlrain) approval for the python/paddle/base/tests/unittests/white_list/no_check_set_white_list.py, which manages the white list of setting no_check_set of check_output. \n" check_approval 1 cryoco luotao1 Aurelius84 phlrain - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/check_op_sequence_instance_0_input_white_list.py" ]; then + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/check_op_sequence_instance_0_input_white_list.py" ]; then echo_line="You must have one RD (luotao1, Aurelisu84, lanxianghit, phlrain) approval for the ${API_FILE}, which manages the white list of instance size 0 input for sequence op test. For more information, please refer to [https://github.com/PaddlePaddle/Paddle/wiki/It-is-required-to-include-LoDTensor-input-with-instance_size=0-in-sequence-OP-test]. \n" check_approval 1 luotao1 Aurelisu84 lanxianghit phlrain - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/op_threshold_white_list.py" ];then - echo_line="It is an Op accuracy problem, please take care of it. You must have one RD (juncaipeng (Recommend), zhangting2020 or luotao1, Aurelius84) approval for the python/paddle/fluid/tests/unittests/white_list/op_threshold_white_list.py, which manages the white list of error threshold for op test with float64 precision. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Upgrade-OP-Precision-to-Float64. \n" + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/op_threshold_white_list.py" ];then + echo_line="It is an Op accuracy problem, please take care of it. You must have one RD (juncaipeng (Recommend), zhangting2020 or luotao1, Aurelius84) approval for the python/paddle/base/tests/unittests/white_list/op_threshold_white_list.py, which manages the white list of error threshold for op test with float64 precision. For more information, please refer to: https://github.com/PaddlePaddle/Paddle/wiki/Upgrade-OP-Precision-to-Float64. \n" check_approval 1 juncaipeng zhangting2020 luotao1 Aurelius84 - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/check_op_sequence_batch_1_input_white_list.py" ];then + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/check_op_sequence_batch_1_input_white_list.py" ];then echo_line="You must have one RD (luotao1, Aurelius84, lanxianghit or phlrain) approval for ${API_FILE}, which manages the white list of batch size 1 input for sequence op test. For more information, please refer to [https://github.com/PaddlePaddle/Paddle/wiki/It-is-required-to-include-LoDTensor-input-with-batch_size=1-in-sequence-OP-test]. \n" check_approval 1 luotao1 Aurelius84 lanxianghit phlrain - elif [ "${API_FILE}" == "python/paddle/fluid/tests/unittests/white_list/no_grad_set_white_list.py" ];then - echo_line="You must have one RD (Shixiaowei02 (Recommend), luotao1, Aurelius84 or phlrain) approval for the python/paddle/fluid/tests/unittests/white_list/no_grad_set_white_list.py, which manages the white list of no_grad_set without value in operators. For more information, please refer to[https://github.com/PaddlePaddle/Paddle/wiki/It's-recommend-to-set-no_grad_set-to-be-None].\n" + elif [ "${API_FILE}" == "python/paddle/base/tests/unittests/white_list/no_grad_set_white_list.py" ];then + echo_line="You must have one RD (Shixiaowei02 (Recommend), luotao1, Aurelius84 or phlrain) approval for the python/paddle/base/tests/unittests/white_list/no_grad_set_white_list.py, which manages the white list of no_grad_set without value in operators. For more information, please refer to[https://github.com/PaddlePaddle/Paddle/wiki/It's-recommend-to-set-no_grad_set-to-be-None].\n" check_approval 1 Shixiaowei02 luotao1 Aurelius84 phlrain elif [ "${API_FILE}" == "tools/sampcd_processor.py" ];then echo_line="test_sampcd_processor.py will be executed for changed sampcd_processor.py.\n" @@ -205,10 +205,10 @@ for API_FILE in ${API_FILES[*]}; do elif [ "${API_FILE}" == "tools/parallel_UT_rule.py" ]; then echo_line="You must have one RD (zhwesky2010 (Recommend), wanghuancoder, luotao1 or Aurelius84) approval for ${API_FILE} changes, which manages the rule of running unittest with a same GPU. If the unittest failed due to Insufficient GPU memory or CUBLAS_STATUS_ALLOC_FAILED, you can remove it from ${API_FILE}.\n" check_approval 1 zhwesky2010 wanghuancoder luotao1 Aurelius84 - elif [ "${API_FILE}" == "python/paddle/fluid/parallel_executor.py" ]; then + elif [ "${API_FILE}" == "python/paddle/base/parallel_executor.py" ]; then echo_line="You must have one RD (Xreki, luotao1, zhhsplendid or Aurelius84) approval for ${API_FILE}, which manages the underlying code for PaddlePaddle.\n" check_approval 1 Xreki luotao1 zhhsplendid Aurelius84 - elif [ "${API_FILE}" == "python/paddle/fluid/dygraph/layers.py" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_node_info.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_node_info.cc" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_tensor_holder.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_tensor_holder.cc" ] || [ "${API_FILE}" == "paddle/fluid/eager/tensor_wrapper.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/autograd_meta.cc"] || [ "${API_FILE}" == "paddle/fluid/eager/autograd_meta.h"] || [ "${API_FILE}" == "paddle/fluid/eager/backward.cc"] || [ "${API_FILE}" == "paddle/fluid/eager/backward.h"]; then + elif [ "${API_FILE}" == "python/paddle/base/dygraph/layers.py" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_node_info.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_node_info.cc" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_tensor_holder.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/grad_tensor_holder.cc" ] || [ "${API_FILE}" == "paddle/fluid/eager/tensor_wrapper.h" ] || [ "${API_FILE}" == "paddle/fluid/eager/autograd_meta.cc"] || [ "${API_FILE}" == "paddle/fluid/eager/autograd_meta.h"] || [ "${API_FILE}" == "paddle/fluid/eager/backward.cc"] || [ "${API_FILE}" == "paddle/fluid/eager/backward.h"]; then echo_line="You must have one RD (JiabinYang,chenwhql,phlrain) approval for ${API_FILE}, which manages the underlying code for PaddlePaddle.\n" check_approval JiabinYang chenwhql phlrain elif [ "${API_FILE}" == "paddle/phi/api/include/tensor.h" ] || [ "${API_FILE}" == "paddle/phi/core/tensor_base.h" ] || [ "${API_FILE}" == "paddle/phi/core/dense_tensor.h" ] || [ "${API_FILE}" == "paddle/phi/core/meta_tensor.h" ] || [ "${API_FILE}" == "paddle/phi/core/tensor_meta.h" ] || [ "${API_FILE}" == "paddle/phi/core/attribute.h" ] || [ "${API_FILE}" == "paddle/phi/core/device_context.h" ] || [ "${API_FILE}" == "paddle/phi/core/kernel_utils.h" ] || [ "${API_FILE}" == "paddle/phi/core/kernel_registry.h" ] || [ "${API_FILE}" == "paddle/phi/core/kernel_factory.h" ] || [ "${API_FILE}" == "paddle/phi/core/kernel_context.h" ] || [ "${API_FILE}" == "paddle/phi/core/infermeta_utils.h" ]; then @@ -341,9 +341,9 @@ if [ "${HAS_MODIFIED_STATIC_BUILD}" != "" ] && [ "${GIT_PR_ID}" != ""]; then check_approval 1 From00 zhiqiu fi -HAS_MODIFIED_PY_FLUID=`git diff --name-only upstream/$BRANCH | grep "python/paddle/fluid" || true` +HAS_MODIFIED_PY_FLUID=`git diff --name-only upstream/$BRANCH | grep "python/paddle/base" || true` if [ "${HAS_MODIFIED_PY_FLUID}" != "" ] && [ "${GIT_PR_ID}" != "" ]; then - echo_line="You must have one RD (zoooo0820(Recommend), or jeff41404) approval for file changes in python/paddle/fluid, because fluid API is going to be removed.\n" + echo_line="You must have one RD (zoooo0820(Recommend), or jeff41404) approval for file changes in python/paddle/base, because fluid API is going to be removed.\n" check_approval 1 zoooo0820 jeff41404 fi diff --git a/tools/check_op_desc.py b/tools/check_op_desc.py index ce234589564a2..89a5e87af0b45 100644 --- a/tools/check_op_desc.py +++ b/tools/check_op_desc.py @@ -15,7 +15,7 @@ import json import sys -from paddle.fluid.core import OpUpdateType +from paddle.base.core import OpUpdateType from paddle.utils import OpLastCheckpointChecker INPUTS = "Inputs" diff --git a/tools/check_op_register_type.py b/tools/check_op_register_type.py index 349c8f3616589..5c3a72622ec38 100644 --- a/tools/check_op_register_type.py +++ b/tools/check_op_register_type.py @@ -23,14 +23,14 @@ import re import sys -from paddle import fluid +from paddle import base INTS = {'int', 'int64_t'} FLOATS = {'float', 'double'} def get_all_kernels(): - all_kernels_info = fluid.core._get_all_register_op_kernels() + all_kernels_info = base.core._get_all_register_op_kernels() # [u'data_type[double]:data_layout[ANY_LAYOUT]:place[CPUPlace]:library_type[PLAIN]' op_kernel_types = collections.defaultdict(list) for op_type, op_infos in all_kernels_info.items(): diff --git a/tools/cinn/paddle_benchmark/paddle_test_benchmark.py b/tools/cinn/paddle_benchmark/paddle_test_benchmark.py index ff7f4d5bd72c8..56099e4749a70 100755 --- a/tools/cinn/paddle_benchmark/paddle_test_benchmark.py +++ b/tools/cinn/paddle_benchmark/paddle_test_benchmark.py @@ -18,7 +18,7 @@ import numpy as np import paddle.inference as paddle_infer -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor +from paddle.base.core import AnalysisConfig, create_paddle_predictor def main(): diff --git a/tools/cinn/paddle_benchmark/test_paddle_ops.py b/tools/cinn/paddle_benchmark/test_paddle_ops.py index 6cb9e806d3096..f830eb9394655 100755 --- a/tools/cinn/paddle_benchmark/test_paddle_ops.py +++ b/tools/cinn/paddle_benchmark/test_paddle_ops.py @@ -18,7 +18,7 @@ import paddle from paddle import static -from paddle.fluid.core import AnalysisConfig, create_paddle_predictor +from paddle.base.core import AnalysisConfig, create_paddle_predictor def set_config(op_name, input_shapes, enable_gpu=False): diff --git a/tools/count_api_without_core_ops.py b/tools/count_api_without_core_ops.py index 0a5da2a8fa5d4..90d5e48f5167c 100644 --- a/tools/count_api_without_core_ops.py +++ b/tools/count_api_without_core_ops.py @@ -24,9 +24,9 @@ # APIs that should not be printed into API.spec omitted_list = [ - "paddle.fluid.LoDTensor.set", # Do not know why it should be omitted - "paddle.fluid.io.ComposeNotAligned", - "paddle.fluid.io.ComposeNotAligned.__init__", + "paddle.base.LoDTensor.set", # Do not know why it should be omitted + "paddle.base.io.ComposeNotAligned", + "paddle.base.io.ComposeNotAligned.__init__", ] @@ -143,7 +143,7 @@ def visit_all_module(mod, func): if mod_name != 'paddle' and not mod_name.startswith('paddle.'): return - if mod_name.startswith('paddle.fluid.core'): + if mod_name.startswith('paddle.base.core'): return if mod in visited_modules: diff --git a/tools/diff_api.py b/tools/diff_api.py index b52ec91badb86..107892395c92f 100644 --- a/tools/diff_api.py +++ b/tools/diff_api.py @@ -44,7 +44,7 @@ 1. cd ${paddle_path}, compile paddle; 2. pip install build/python/dist/(build whl package); - 3. run "python tools/print_signatures.py paddle.fluid> paddle/fluid/API.spec" + 3. run "python tools/print_signatures.py paddle.base> paddle/fluid/API.spec" ''' if error: print('API Difference is: ') diff --git a/tools/diff_use_default_grad_op_maker.py b/tools/diff_use_default_grad_op_maker.py index c79d758b3e781..a8e82fe657fd1 100644 --- a/tools/diff_use_default_grad_op_maker.py +++ b/tools/diff_use_default_grad_op_maker.py @@ -18,12 +18,12 @@ import sys -from paddle import fluid +from paddle import base def generate_spec(filename): with open(filename, 'w') as f: - ops = fluid.core._get_use_default_grad_op_desc_maker_ops() + ops = base.core._get_use_default_grad_op_desc_maker_ops() for op in ops: f.write(op + '\n') diff --git a/tools/parse_kernel_info.py b/tools/parse_kernel_info.py index ad5a696bc626c..23106ab0d2ebb 100644 --- a/tools/parse_kernel_info.py +++ b/tools/parse_kernel_info.py @@ -94,13 +94,11 @@ def parse_paddle_kernels(lib="phi", kernel_type="function", print_detail=False): if lib == "phi": assert kernel_type in ["function", "structure", "all"] # phi kernel type can be: function, structure, all - kernel_infos = paddle.fluid.core._get_registered_phi_kernels( - kernel_type - ) + kernel_infos = paddle.base.core._get_registered_phi_kernels(kernel_type) else: # fluid, phi, all assert kernel_type in ["fluid", "phi", "all"] - kernel_infos = paddle.fluid.core._get_all_register_op_kernels( + kernel_infos = paddle.base.core._get_all_register_op_kernels( kernel_type ) diff --git a/tools/print_op_desc.py b/tools/print_op_desc.py index b078ad96e33ab..4bfaddeb953fa 100644 --- a/tools/print_op_desc.py +++ b/tools/print_op_desc.py @@ -44,7 +44,7 @@ import json -from paddle.fluid import core, framework +from paddle.base import core, framework INPUTS = "Inputs" OUTPUTS = "Outputs" diff --git a/tools/print_signatures.py b/tools/print_signatures.py index 3af095fff2e10..cdae91ece7023 100644 --- a/tools/print_signatures.py +++ b/tools/print_signatures.py @@ -15,7 +15,7 @@ Print all signature of a python module in alphabet order. Usage: - ./print_signature "paddle.fluid" > signature.txt + ./print_signature "paddle.base" > signature.txt """ import argparse @@ -88,7 +88,7 @@ def visit_all_module(mod): if mod_name != 'paddle' and not mod_name.startswith('paddle.'): return - if mod_name.startswith('paddle.fluid.core'): + if mod_name.startswith('paddle.base.core'): return if mod in visited_modules: @@ -353,7 +353,7 @@ def parse_args(): dest='skipped', type=str, help='Skip Checking submodules', - default='paddle.fluid.libpaddle.eager.ops', + default='paddle.base.libpaddle.eager.ops', ) if len(sys.argv) == 1: diff --git a/tools/summary_env.py b/tools/summary_env.py index 42019c280d666..10d82cd8746f9 100644 --- a/tools/summary_env.py +++ b/tools/summary_env.py @@ -42,7 +42,7 @@ def get_paddle_info(): import paddle envs['paddle_version'] = paddle.__version__ - envs['paddle_with_cuda'] = paddle.fluid.core.is_compiled_with_cuda() + envs['paddle_with_cuda'] = paddle.base.core.is_compiled_with_cuda() except: envs['paddle_version'] = 'N/A' envs['paddle_with_cuda'] = 'N/A' diff --git a/tools/test_check_api_compatible.py b/tools/test_check_api_compatible.py index dd134334ed9fd..0ec61521cb2da 100644 --- a/tools/test_check_api_compatible.py +++ b/tools/test_check_api_compatible.py @@ -74,7 +74,7 @@ def test_args_reduced(self): class Test_check_compatible_str(unittest.TestCase): def setUp(self) -> None: self.fullargspec_prefix = 'inspect.Full' - # paddle.fluid.layer_helper_base.LayerHelperBase.create_parameter + # paddle.base.layer_helper_base.LayerHelperBase.create_parameter self.argspec_str_o = ( self.fullargspec_prefix + """ArgSpec(args=['self', 'attr', 'shape', 'dtype', 'is_bias', 'default_initializer', 'stop_gradient', 'type'], varargs=None, varkw=None, defaults=(None, False, None, False, VarType.LOD_TENSOR), kwonlyargs=[], kwonlydefaults=None, annotations={})""" diff --git a/tools/test_runner.py b/tools/test_runner.py index f7e73be2c920b..37d132fbc1535 100644 --- a/tools/test_runner.py +++ b/tools/test_runner.py @@ -19,8 +19,8 @@ from io import StringIO import paddle -from paddle import fluid -from paddle.fluid import core +from paddle import base +from paddle.base import core sys.path.append(os.path.abspath(os.path.dirname(__file__))) sys.path.append( @@ -52,12 +52,12 @@ def main(): flag_need_static_mode = True paddle.enable_static() buffer = StringIO() - main = fluid.Program() - startup = fluid.Program() - scope = fluid.core.Scope() - with fluid.program_guard(main, startup): - with fluid.scope_guard(scope): - with fluid.unique_name.guard(): + main = base.Program() + startup = base.Program() + scope = base.core.Scope() + with base.program_guard(main, startup): + with base.scope_guard(scope): + with base.unique_name.guard(): test_loader = unittest.TestLoader() module = importlib.import_module(module_name) tests = test_loader.loadTestsFromModule(module) diff --git a/tools/timeline.py b/tools/timeline.py index c5521bdfb107f..ff8d0946378d7 100644 --- a/tools/timeline.py +++ b/tools/timeline.py @@ -15,7 +15,7 @@ import argparse import json -from paddle.fluid.proto.profiler import profiler_pb2 +from paddle.base.proto.profiler import profiler_pb2 parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( From e98875b06df1299dbb48cf9bc16877471e18c791 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Fri, 8 Sep 2023 09:13:17 +0800 Subject: [PATCH 187/194] refine add_n (#57003) --- paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc index 45d29ce80b269..599e5c9564507 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_manual_op.cc @@ -182,7 +182,6 @@ void AddN_Op::Build(ir::Builder &builder, VLOG(4) << "Builder construction outputs"; ir::VectorType inputs = inputs_.type().dyn_cast(); - (void)inputs; std::vector vec_dense_inputs; for (size_t i = 0; i < static_cast(inputs.size()); i++) { vec_dense_inputs.push_back(phi::DenseTensor( @@ -311,7 +310,6 @@ void AddNWithKernelOp::Build(ir::Builder &builder, VLOG(4) << "Builder construction outputs"; ir::VectorType inputs = inputs_.type().dyn_cast(); - (void)inputs; std::vector vec_dense_inputs; for (size_t i = 0; i < static_cast(inputs.size()); i++) { vec_dense_inputs.push_back(phi::DenseTensor( From 530b6b66189b632d7ffad0f1a13c668abea52e30 Mon Sep 17 00:00:00 2001 From: xiaoguoguo626807 <100397923+xiaoguoguo626807@users.noreply.github.com> Date: Fri, 8 Sep 2023 09:52:40 +0800 Subject: [PATCH 188/194] =?UTF-8?q?=E3=80=90newir=E3=80=91Concat=20optest?= =?UTF-8?q?=20(#57043)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add reference of lbfgs * add reference of lbfgs * modify concat kernel choose * modify ci --- .../ir/transforms/pd_op_to_kernel_pass.cc | 23 +++++++++++++++++++ python/paddle/_C_ops.py | 2 +- python/paddle/new_ir_utils.py | 2 ++ python/paddle/tensor/manipulation.py | 9 ++++---- python/paddle/tensor/math.py | 12 ++++++---- test/legacy_test/eager_op_test.py | 21 +++++++++++++---- test/legacy_test/test_mean_op.py | 2 ++ 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index d75c7cc4779ff..ef954c376e448 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -569,6 +569,29 @@ phi::KernelKey GetKernelKey( kernel_key_parser.key_set.backend_set = kernel_key_parser.key_set.backend_set | paddle::experimental::BackendSet(data_op_backend); + } else if (op->operand_source(i).GetDefiningOp()->name() == + "builtin.combine") { + auto combine_op = op->operand_source(i).GetDefiningOp(); + for (size_t j = 0; j < combine_op->num_operands(); ++j) { + if (combine_op->operand_source(j).GetDefiningOp()->name() == + "pd.data") { + auto data_op = combine_op->operand_source(j).GetDefiningOp(); + auto data_place = data_op->attributes() + .at("place") + .dyn_cast() + .data(); + + auto data_op_backend = + paddle::experimental::ParseBackend(data_place); + if (data_op_backend == phi::Backend::UNDEFINED) { + data_op_backend = paddle::experimental::ParseBackend(place); + } + kernel_key_parser.key_set.backend_set = + kernel_key_parser.key_set.backend_set | + paddle::experimental::BackendSet(data_op_backend); + break; + } + } } } diff --git a/python/paddle/_C_ops.py b/python/paddle/_C_ops.py index f8999b7aeba2e..9c0104b382c2e 100644 --- a/python/paddle/_C_ops.py +++ b/python/paddle/_C_ops.py @@ -16,7 +16,7 @@ __all__ = [] -UNIFIED_APIS = ['mean'] +UNIFIED_APIS = ['mean', 'concat', 'add_n', 'scale'] for name in dir(core.eager.ops): globals()[name] = getattr(core.eager.ops, name) diff --git a/python/paddle/new_ir_utils.py b/python/paddle/new_ir_utils.py index d401cde662b17..6aaa64076e98e 100644 --- a/python/paddle/new_ir_utils.py +++ b/python/paddle/new_ir_utils.py @@ -32,12 +32,14 @@ def __init__(self): paddle.base.framework.set_flags(old_flag) def __enter__(self): + paddle.enable_static() paddle.framework.set_flags({"FLAGS_enable_new_ir_api": True}) self._switch_to_new_ir() def __exit__(self, exc_type, exc_val, exc_tb): paddle.framework.set_flags({"FLAGS_enable_new_ir_api": False}) self._switch_to_old_ir() + paddle.disable_static() def _switch_to_new_ir(self): if paddle.ir.core._use_new_ir_api(): diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index a6262b604a52f..639cce92fa3a8 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -34,6 +34,7 @@ core, dygraph_only, in_dynamic_mode, + in_new_ir_mode, ) from .creation import _complex_to_real_dtype, _real_to_complex_dtype, zeros @@ -1131,11 +1132,11 @@ def concat(x, axis=0, name=None): if not isinstance(input, Variable): input = [t for t in input if t.shape.count(0) == 0] return _C_ops.concat(input, axis) + elif in_new_ir_mode(): + if not isinstance(input, paddle.ir.Value): + input = [t for t in input if t.shape.count(0) == 0] + return _C_ops.concat(input, axis) else: - if paddle.ir.core._use_new_ir_api(): - if not isinstance(input, paddle.ir.Value): - input = [t for t in input if t.shape.count(0) == 0] - return paddle._ir_ops.concat(input, axis) check_type(input, 'input', (list, tuple, Variable), 'concat') if not isinstance(input, Variable): for id, x in enumerate(input): diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 0f9a3e4ebeb13..1b9fe1164a510 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -15,6 +15,7 @@ math functions """ + import numpy as np import paddle @@ -34,6 +35,8 @@ convert_np_dtype_to_dtype_, core, in_dynamic_mode, + in_dynamic_or_new_ir_mode, + in_new_ir_mode, ) from .creation import _complex_to_real_dtype from .layer_function_generator import generate_layer_fn, templatedoc @@ -264,6 +267,10 @@ def scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None): return _C_ops.scale(x, scale, float(bias), bias_after_scale) out = _C_ops.scale(x, scale, float(bias), bias_after_scale) return dygraph_utils._append_activation_in_dygraph(out, act) + elif in_new_ir_mode(): + if act is None: + return _C_ops.scale(x, scale, float(bias), bias_after_scale) + raise ValueError("act is not implement in new ir of scale api.") else: check_variable_and_dtype( x, @@ -1956,14 +1963,11 @@ def add_n(inputs, name=None): [[8. , 10., 12.], [14., 16., 18.]]) """ - if in_dynamic_mode(): + if in_dynamic_or_new_ir_mode(): if isinstance(inputs, Variable): inputs = [inputs] return _C_ops.add_n(inputs) else: - if paddle.ir.core._use_new_ir_api(): - return paddle._ir_ops.add_n(inputs) - helper = LayerHelper('add_n', **locals()) check_type(inputs, 'inputs', (Variable, tuple, list), 'add_n') if isinstance(inputs, (list, tuple)): diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index e7e8b8059ab99..504e10291b40a 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -2921,7 +2921,6 @@ def check_grad_with_place( if user_defined_grads is None and self.is_compared_with_fp32(): self.enable_cal_ref_output() - numeric_grads = self._get_gradient( inputs_to_check, place, @@ -3164,7 +3163,6 @@ def _get_dygraph_grad( # delete the inputs which no need to calculate grad for no_grad_val in no_grad_set: del inputs[no_grad_val] - grad_inputs = paddle.grad( outputs=paddle.utils.flatten(outputs), inputs=paddle.utils.flatten(inputs), @@ -3433,7 +3431,7 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): ( static_inputs, attrs, - input_dict, + inputs_dict, feed, ) = self.get_ir_input_attr_dict_and_feed(stop_gradient=False) # prepare args @@ -3480,14 +3478,27 @@ def construct_output_dict_by_kernel_sig(ret_tuple, output_sig): fetch_list = getattr(self, "fetch_list", []) outputs_valid = outputs - grad_inputs = inputs_to_check + loss_inputs = [] + for input_name in inputs_to_check: + loss_inputs.append(inputs_dict[input_name]) + if user_defined_grad_outputs is None: if len(outputs_valid) == 1: for outputs_valid_key in outputs_valid: loss = paddle.mean(outputs_valid[outputs_valid_key][0]) + else: + avg_sum = [] + for cur_loss in outputs_valid: + cur_avg_loss = paddle.mean(outputs_valid[cur_loss][0]) + avg_sum.append(cur_avg_loss) + loss_sum = paddle.add_n(avg_sum) + loss = paddle.scale( + loss_sum, scale=1.0 / float(len(avg_sum)) + ) + grad_inputs = ir_grad( outputs=paddle.utils.flatten(loss), - inputs=paddle.utils.flatten(static_inputs), + inputs=paddle.utils.flatten(loss_inputs), grad_outputs=None, ) else: diff --git a/test/legacy_test/test_mean_op.py b/test/legacy_test/test_mean_op.py index 023a8aceddde3..360e450625cbf 100644 --- a/test/legacy_test/test_mean_op.py +++ b/test/legacy_test/test_mean_op.py @@ -75,6 +75,7 @@ def test_checkout_grad(self): class TestMeanOpError(unittest.TestCase): def test_errors(self): + paddle.enable_static() with program_guard(Program(), Program()): # The input type of mean_op must be Variable. input1 = 12 @@ -88,6 +89,7 @@ def test_errors(self): name='input3', shape=[-1, 4], dtype="float16" ) paddle.nn.functional.softmax(input3) + paddle.disable_static() @unittest.skipIf( From 14f00dc5336b3d8fc637fd089b9fc6588e95c66a Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Fri, 8 Sep 2023 10:10:49 +0800 Subject: [PATCH 189/194] fix pd_ops.yaml generation bug (#57041) --- paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt index 08cc463c34c9b..64e3e982133be 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/CMakeLists.txt @@ -58,8 +58,12 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E make_directory ${parsed_op_dir} COMMAND ${PYTHON_EXECUTABLE} ${op_gen_parsed_yaml_file} --op_yaml_path ${pd_op_forward_yaml_file} --output_path ${op_yaml_file3} + COMMENT "Generate pd_ops.parsed.yaml" COMMAND ${PYTHON_EXECUTABLE} ${op_gen_parsed_yaml_file} --op_yaml_path ${pd_op_backward_yaml_file} --output_path ${op_yaml_file4} --backward + COMMENT "Generate pd_ops_backward.parsed.yaml" + DEPENDS ${op_gen_parsed_yaml_file} ${pd_op_forward_yaml_file} + ${pd_op_backward_yaml_file} VERBATIM) add_custom_command( From 9358b4bc7dd02dcab517816a84bf6e47b0754805 Mon Sep 17 00:00:00 2001 From: chen2016013 <111894720+chen2016013@users.noreply.github.com> Date: Fri, 8 Sep 2023 10:25:25 +0800 Subject: [PATCH 190/194] [IR] Refactor code in pd_op_to_kernel_pass.cc and reset vlog level (#57054) * fix merge bug * fix codestyle --- .../ir/phi_kernel_adaptor/phi_kernel_util.cc | 24 +- .../ir/transforms/pd_op_to_kernel_pass.cc | 331 +++++++++--------- 2 files changed, 175 insertions(+), 180 deletions(-) diff --git a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc index 7e7b0dbe76bbb..c72641046f520 100644 --- a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc +++ b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc @@ -253,7 +253,8 @@ void HandleForSpecialOp( variable_list); } - if (op_name == "pd.feed") { + if (op_name == "pd.feed" || op_name == "pd.data") { + VLOG(6) << "Handle for" << op_name; auto value = op->result(0); VLOG(6) << "link feed output to feed in variable" << inner_scope; @@ -273,27 +274,6 @@ void HandleForSpecialOp( variable_list); } - if (op_name == "pd.data") { - VLOG(6) << "Handle for pd.data"; - auto var_name = - op->attributes().at("name").dyn_cast().AsString(); - - auto value = op->result(0); - - paddle::framework::Variable* var = inner_scope->FindVar(var_name); - PADDLE_ENFORCE(var, - paddle::platform::errors::InvalidArgument( - "The variable %s shoud exist", var_name)); - - AddNewData(value, - var_name, - var, - value_2_var_name, - variable_2_var_name, - var_name_2_id, - variable_list); - } - if (op_name == "builtin.combine") { auto out_value = op->result(0); diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index ef954c376e448..618e89977961f 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -62,6 +62,166 @@ const std::unordered_set UnchangeOutputOps = { "builtin.get_parameter", "pd.shadow_output"}; +const std::unordered_set SpecialOpList = { + "builtin.combine", "builtin.slice", "builtin.split"}; + +ir::OpResult GetNewInput( + const ir::Value cur_in, + const std::unordered_map& map_value_pair, + const int index, + const std::string op_name) { + PADDLE_ENFORCE_EQ( + map_value_pair.count(cur_in), + true, + phi::errors::PreconditionNotMet( + "[%d]'s input of [%s] op MUST be in map pair", index, op_name)); + auto new_in = map_value_pair.at(cur_in); + return new_in; +} + +void DealWithSpecialBuiltinOps( + ir::Operation* op_item, + ir::Program* program, + std::unordered_map* map_op_pair, + std::unordered_map* map_value_pair, + ir::IrContext* ctx) { + if (op_item->name() == "builtin.combine") { + std::vector out_places; + // Copy op inputs + std::vector vec_inputs; + std::vector vec_inner_types; + if (op_item->num_operands() > 0) { + for (size_t i = 0; i < op_item->num_operands(); ++i) { + auto cur_in = op_item->operand_source(i); + if (!cur_in) { + vec_inputs.emplace_back(); + continue; + } + auto new_in = GetNewInput(cur_in, *map_value_pair, i, op_item->name()); + vec_inputs.push_back(new_in); + vec_inner_types.push_back(new_in.type()); + if (new_in.type().isa()) { + out_places.push_back( + new_in.type() + .dyn_cast() + .place()); + } else if (new_in.type() + .isa()) { + out_places.push_back( + new_in.type() + .dyn_cast() + .place()); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "only support dense tensor type for now")); + } + } + } + // Copy op output type + std::vector op_output_types; + ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types); + op_output_types.push_back(t1); + + // Get op info + ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); + // Generate new op + ir::Operation* op = ir::Operation::Create( + vec_inputs, op_item->attributes(), op_output_types, op_info); + program->block()->push_back(op); + (*map_op_pair)[op_item] = op; + // only deal with single output + if (op_item->num_results() > 0) { + for (size_t i = 0; i < op_item->num_results(); ++i) { + (*map_value_pair)[op_item->result(i)] = op->result(i); + } + } + } + + if (op_item->name() == "builtin.slice") { + std::vector vec_inputs; + std::vector op_output_types; + if (op_item->num_operands() > 0) { + for (size_t i = 0; i < op_item->num_operands(); ++i) { + auto cur_in = op_item->operand_source(i); + if (!cur_in) { + vec_inputs.emplace_back(); + continue; + } + auto new_in = GetNewInput(cur_in, *map_value_pair, i, op_item->name()); + vec_inputs.push_back(new_in); + if (new_in.type().isa()) { + auto vec_types = new_in.type().dyn_cast().data(); + auto index = op_item->attributes() + .at("index") + .dyn_cast() + .data(); + op_output_types.push_back(vec_types[index]); + } else { + PADDLE_THROW( + phi::errors::Unimplemented("only support vector type for now")); + } + } + } + + // Get op info + ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); + // Generate new op + ir::Operation* op = ir::Operation::Create( + vec_inputs, op_item->attributes(), op_output_types, op_info); + program->block()->push_back(op); + (*map_op_pair)[op_item] = op; + // only deal with single output + if (op_item->num_results() > 0) { + for (size_t i = 0; i < op_item->num_results(); ++i) { + (*map_value_pair)[op_item->result(i)] = op->result(i); + } + } + } + + if (op_item->name() == "builtin.split") { + std::vector out_places(op_item->num_results()); + // Copy op inputs + std::vector vec_inputs; + std::vector op_output_types; + if (op_item->num_operands() > 0) { + for (size_t i = 0; i < op_item->num_operands(); ++i) { + auto cur_in = op_item->operand_source(i); + if (!cur_in) { + vec_inputs.emplace_back(); + continue; + } + auto new_in = GetNewInput(cur_in, *map_value_pair, i, op_item->name()); + vec_inputs.push_back(new_in); + + if (new_in.type().isa()) { + auto vec_types = new_in.type().dyn_cast().data(); + for (uint64_t idx = 0; idx < vec_types.size(); idx++) { + op_output_types.push_back(vec_types[idx]); + } + } else { + PADDLE_THROW( + phi::errors::Unimplemented("only support vector type for now")); + } + } + } + + // Get op info + ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); + // Generate new op + ir::Operation* op = ir::Operation::Create( + vec_inputs, op_item->attributes(), op_output_types, op_info); + program->block()->push_back(op); + (*map_op_pair)[op_item] = op; + // only deal with single output + if (op_item->num_results() > 0) { + for (size_t i = 0; i < op_item->num_results(); ++i) { + (*map_value_pair)[op_item->result(i)] = op->result(i); + } + } + } + VLOG(6) << "Deep copy a new builtin op: " << op_item->name(); +} + bool NeedFallBackCpu(const ir::Operation* op, const std::string& kernel_fn_name, const phi::KernelKey& kernel_key) { @@ -620,6 +780,11 @@ phi::KernelKey GetKernelKey( std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, phi::Place place) { + if (VLOG_IS_ON(2)) { + std::stringstream ss; + prog->Print(ss); + VLOG(2) << "Program after lowering to kernel pass : " << ss.str(); + } auto program = std::make_unique(ir::IrContext::Instance()); auto block = prog->block(); @@ -647,163 +812,9 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, continue; } - if (op_item->name() == "builtin.combine") { - std::vector out_places; - // Copy op inputs - std::vector vec_inputs; - std::vector vec_inner_types; - if (op_item->num_operands() > 0) { - for (size_t i = 0; i < op_item->num_operands(); ++i) { - auto cur_in = op_item->operand_source(i); - if (!cur_in) { - vec_inputs.emplace_back(); - continue; - } - PADDLE_ENFORCE_EQ(map_value_pair.count(cur_in), - true, - phi::errors::PreconditionNotMet( - "[%d]'s input of [%s] op MUST in map pair", - i, - op_item->name())); - auto new_in = map_value_pair.at(cur_in); - vec_inputs.push_back(new_in); - vec_inner_types.push_back(new_in.type()); - if (new_in.type().isa()) { - out_places.push_back( - new_in.type() - .dyn_cast() - .place()); - } else if (new_in.type() - .isa()) { - out_places.push_back( - new_in.type() - .dyn_cast() - .place()); - } else { - PADDLE_THROW(phi::errors::Unimplemented( - "only support dense tensor type for now")); - } - } - } - // Copy op output type - std::vector op_output_types; - ir::Type t1 = ir::VectorType::get(ctx, vec_inner_types); - op_output_types.push_back(t1); - - // Get op info - ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); - // Generate new op - ir::Operation* op = ir::Operation::Create( - vec_inputs, op_item->attributes(), op_output_types, op_info); - program->block()->push_back(op); - map_op_pair[op_item] = op; - // only deal with single output - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - map_value_pair[op_item->result(i)] = op->result(i); - } - } - VLOG(6) << "Deep copy a new builtin op: " << op_item->name(); - continue; - } - - if (op_item->name() == "builtin.slice") { - std::vector vec_inputs; - std::vector op_output_types; - if (op_item->num_operands() > 0) { - for (size_t i = 0; i < op_item->num_operands(); ++i) { - auto cur_in = op_item->operand_source(i); - if (!cur_in) { - vec_inputs.emplace_back(); - continue; - } - PADDLE_ENFORCE_EQ(map_value_pair.count(cur_in), - true, - phi::errors::PreconditionNotMet( - "[%d]'s input of [%s] op MUST in map pair", - i, - op_item->name())); - auto new_in = map_value_pair.at(cur_in); - vec_inputs.push_back(new_in); - - if (new_in.type().isa()) { - auto vec_types = new_in.type().dyn_cast().data(); - auto index = op_item->attributes() - .at("index") - .dyn_cast() - .data(); - op_output_types.push_back(vec_types[index]); - } else { - PADDLE_THROW( - phi::errors::Unimplemented("only support vector type for now")); - } - } - } - - // Get op info - ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); - // Generate new op - ir::Operation* op = ir::Operation::Create( - vec_inputs, op_item->attributes(), op_output_types, op_info); - program->block()->push_back(op); - map_op_pair[op_item] = op; - // only deal with single output - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - map_value_pair[op_item->result(i)] = op->result(i); - } - } - VLOG(6) << "Deep copy a new builtin op: " << op_item->name(); - continue; - } - - if (op_item->name() == "builtin.split") { - std::vector out_places(op_item->num_results()); - // Copy op inputs - std::vector vec_inputs; - std::vector op_output_types; - if (op_item->num_operands() > 0) { - for (size_t i = 0; i < op_item->num_operands(); ++i) { - auto cur_in = op_item->operand_source(i); - if (!cur_in) { - vec_inputs.emplace_back(); - continue; - } - PADDLE_ENFORCE_EQ(map_value_pair.count(cur_in), - true, - phi::errors::PreconditionNotMet( - "[%d]'s input of [%s] op MUST in map pair", - i, - op_item->name())); - auto new_in = map_value_pair.at(cur_in); - vec_inputs.push_back(new_in); - - if (new_in.type().isa()) { - auto vec_types = new_in.type().dyn_cast().data(); - for (uint64_t idx = 0; idx < vec_types.size(); idx++) { - op_output_types.push_back(vec_types[idx]); - } - } else { - PADDLE_THROW( - phi::errors::Unimplemented("only support vector type for now")); - } - } - } - - // Get op info - ir::OpInfo op_info = ctx->GetRegisteredOpInfo(op_item->name()); - // Generate new op - ir::Operation* op = ir::Operation::Create( - vec_inputs, op_item->attributes(), op_output_types, op_info); - program->block()->push_back(op); - map_op_pair[op_item] = op; - // only deal with single output - if (op_item->num_results() > 0) { - for (size_t i = 0; i < op_item->num_results(); ++i) { - map_value_pair[op_item->result(i)] = op->result(i); - } - } - VLOG(6) << "Deep copy a new builtin op: " << op_item->name(); + if (SpecialOpList.count(op_item->name())) { + DealWithSpecialBuiltinOps( + op_item, program.get(), &map_op_pair, &map_value_pair, ctx); continue; } @@ -1167,7 +1178,11 @@ std::unique_ptr PdOpLowerToKernelPass(ir::Program* prog, } } } - + if (VLOG_IS_ON(2)) { + std::stringstream ss1; + program->Print(ss1); + VLOG(2) << "Program after lowering to kernel pass : " << ss1.str(); + } return program; } From e9fac90ec9b85ca598984056ef1c318503121879 Mon Sep 17 00:00:00 2001 From: zhangbo9674 <82555433+zhangbo9674@users.noreply.github.com> Date: Fri, 8 Sep 2023 10:27:36 +0800 Subject: [PATCH 191/194] [IR] Refine some ir logic (#57072) * fix bug * refine code * refine code * refine code * refine code * refine code * refine code * fix bug --- .../new_executor/new_ir_interpreter.cc | 1 - .../ir/dialect/op_generator/op_build_gen.py | 12 ++-- .../ir/kernel_dialect.cc | 41 +++++++++++- .../paddle_kernel_dialect/ir/kernel_dialect.h | 3 + .../paddle_kernel_dialect/ir/kernel_op.cc | 37 +++++++++++ .../paddle_kernel_dialect/ir/kernel_op.h | 14 ++++- .../ir/legacy_kernel_op.cc | 62 ------------------- .../ir/legacy_kernel_op.h | 38 ------------ .../ir/transforms/pd_op_to_kernel_pass.cc | 1 - .../ir_kernel_dialect_pass_test.cc | 1 - 10 files changed, 100 insertions(+), 110 deletions(-) delete mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc delete mode 100644 paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h diff --git a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc index b3b668e05ffc4..94ef1e3af217e 100644 --- a/paddle/fluid/framework/new_executor/new_ir_interpreter.cc +++ b/paddle/fluid/framework/new_executor/new_ir_interpreter.cc @@ -46,7 +46,6 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h" #include "paddle/ir/core/builtin_attribute.h" diff --git a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py index 2a78ea6b6e995..66d1094c9e5fc 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_build_gen.py @@ -330,14 +330,16 @@ def GenBuildOutputs( .dyn_cast() .data() .GetData())); - }} - else {{ - PADDLE_ENFORCE( - {name}_.type().isa(), - phi::errors::PreconditionNotMet("section Type should be VectorType.")); + }} else if ({name}_.type().isa()) {{ size_t {name}_size = {name}_.type().dyn_cast().size(); {name} = std::move(phi::IntArray(std::vector({name}_size, -1))); {name}.SetFromTensor(true); + }} else if ({name}_.type().isa()) {{ + size_t {name}_size = phi::product({name}_.type().dyn_cast().dims()); + {name} = std::move(phi::IntArray(std::vector({name}_size, -1))); + {name}.SetFromTensor(true); + }} else {{ + PADDLE_THROW(phi::errors::Unimplemented("Only support VectorType or DenseTensorType")); }}\n""" CREATE_SCALAR_MUTABLE_ATTRIBUE_WITH_UNKONW_DATA_TEMPLATE = """ phi::Scalar {name}; diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc index 5269fe62892a5..c2f4dfefb4d2b 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.cc @@ -16,8 +16,8 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/init_phi.h" +#include "paddle/ir/core/ir_printer.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/ddim.h" @@ -75,6 +75,45 @@ void PaddleKernelDialect::PrintAttribute(ir::Attribute attr, << "|dtype:" << kernel.dtype() << ">"; } +void PaddleKernelDialect::PrintOperation(ir::Operation *op, + ir::IrPrinter &printer) const { + if (op->dyn_cast() || op->dyn_cast()) { + auto &os = printer.os; + printer.PrintOpResult(op); + os << " ="; + if (auto phi_kernel_op = op->dyn_cast()) { + std::string kernel_name = phi_kernel_op.kernel_name(); + if (op->attributes().count("is_inplace") != 0 && + op->attributes() + .at("is_inplace") + .dyn_cast() + .data()) { + kernel_name = kernel_name + "_"; + } + os << " \"" << kernel_name << "(phi_kernel)\""; + } else { + auto legacy_kernel_op = op->dyn_cast(); + std::string kernel_name = legacy_kernel_op.kernel_name(); + if (op->attributes().count("is_inplace") != 0 && + op->attributes() + .at("is_inplace") + .dyn_cast() + .data()) { + kernel_name = kernel_name + "_"; + } + os << " \"" << kernel_name << "(legacy_kernel)\""; + } + printer.PrintOpOperands(op); + printer.PrintAttributeMap(op); + os << " :"; + printer.PrintOperandsType(op); + os << " -> "; + printer.PrintOpReturnType(op); + } else { + printer.PrintGeneralOperation(op); + } +} + } // namespace dialect } // namespace paddle diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h index 7685a9d65459a..8099e1d1da093 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h @@ -29,6 +29,9 @@ class PaddleKernelDialect : public ir::Dialect { void PrintAttribute(ir::Attribute attr, std::ostream& os) const override; + void PrintOperation(ir::Operation* op, + ir::IrPrinter& printer) const override; // NOLINT + private: void initialize(); }; diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.cc index b463fc6ae85ca..4a934505aad55 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.cc +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.cc @@ -56,7 +56,44 @@ phi::KernelKey PhiKernelOp::kernel_key() { return attributes().at("kernel_key").dyn_cast().data(); } +const char* LegacyKernelOp::attributes_name[attributes_num] = { // NOLINT + "op_name", + "kernel_name", + "kernel_key"}; + +void LegacyKernelOp::Verify() { + VLOG(4) << "Verifying inputs, outputs and attributes for: LegacyKernelOp."; + + auto& attributes = this->attributes(); + + PADDLE_ENFORCE(attributes.count("op_name") > 0 && + attributes.at("op_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: op_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_name") > 0 && + attributes.at("kernel_name").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_name is not right.")); + + PADDLE_ENFORCE(attributes.count("kernel_key") > 0 && + attributes.at("kernel_key").isa(), + phi::errors::PreconditionNotMet( + "Type of attribute: kernel_key is not right.")); +} + +std::string LegacyKernelOp::op_name() { + return attributes().at("op_name").dyn_cast().AsString(); +} +std::string LegacyKernelOp::kernel_name() { + return attributes().at("kernel_name").dyn_cast().AsString(); +} +phi::KernelKey LegacyKernelOp::kernel_key() { + return attributes().at("kernel_key").dyn_cast().data(); +} + } // namespace dialect } // namespace paddle IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp) +IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h index 91dae1cfd560f..0a574bc60b218 100644 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h +++ b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h @@ -20,7 +20,6 @@ namespace paddle { namespace dialect { - class PhiKernelOp : public ir::Op { public: using Op::Op; @@ -33,7 +32,20 @@ class PhiKernelOp : public ir::Op { void Verify(); }; +class LegacyKernelOp : public ir::Op { + public: + using Op::Op; + static const char *name() { return "pd_kernel.legacy_kernel"; } + static constexpr uint32_t attributes_num = 3; + static const char *attributes_name[attributes_num]; + std::string op_name(); + std::string kernel_name(); + phi::KernelKey kernel_key(); + void Verify(); +}; + } // namespace dialect } // namespace paddle IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::PhiKernelOp) +IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc deleted file mode 100644 index ca15657e10485..0000000000000 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.cc +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_attribute.h" -#include "paddle/ir/core/builtin_attribute.h" -#include "paddle/phi/core/enforce.h" - -namespace paddle { -namespace dialect { - -const char* LegacyKernelOp::attributes_name[attributes_num] = { // NOLINT - "op_name", - "kernel_name", - "kernel_key"}; - -void LegacyKernelOp::Verify() { - VLOG(4) << "Verifying inputs, outputs and attributes for: LegacyKernelOp."; - - auto& attributes = this->attributes(); - - PADDLE_ENFORCE(attributes.count("op_name") > 0 && - attributes.at("op_name").isa(), - phi::errors::PreconditionNotMet( - "Type of attribute: op_name is not right.")); - - PADDLE_ENFORCE(attributes.count("kernel_name") > 0 && - attributes.at("kernel_name").isa(), - phi::errors::PreconditionNotMet( - "Type of attribute: kernel_name is not right.")); - - PADDLE_ENFORCE(attributes.count("kernel_key") > 0 && - attributes.at("kernel_key").isa(), - phi::errors::PreconditionNotMet( - "Type of attribute: kernel_key is not right.")); -} - -std::string LegacyKernelOp::op_name() { - return attributes().at("op_name").dyn_cast().AsString(); -} -std::string LegacyKernelOp::kernel_name() { - return attributes().at("kernel_name").dyn_cast().AsString(); -} -phi::KernelKey LegacyKernelOp::kernel_key() { - return attributes().at("kernel_key").dyn_cast().data(); -} - -} // namespace dialect -} // namespace paddle - -IR_DEFINE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h b/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h deleted file mode 100644 index 524505a48305f..0000000000000 --- a/paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#pragma once - -#include "paddle/ir/core/builder.h" -#include "paddle/ir/core/op_base.h" -#include "paddle/phi/core/kernel_factory.h" - -namespace paddle { -namespace dialect { -class LegacyKernelOp : public ir::Op { - public: - using Op::Op; - static const char *name() { return "pd_kernel.legacy_kernel"; } - static constexpr uint32_t attributes_num = 3; - static const char *attributes_name[attributes_num]; - std::string op_name(); - std::string kernel_name(); - phi::KernelKey kernel_key(); - void Verify(); -}; - -} // namespace dialect -} // namespace paddle - -IR_DECLARE_EXPLICIT_TYPE_ID(paddle::dialect::LegacyKernelOp) diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 618e89977961f..042929dd20d2d 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -27,7 +27,6 @@ #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_type.h" -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/api/lib/kernel_dispatch.h" diff --git a/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc b/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc index 72dbbf73b9bc2..22442c40555d0 100644 --- a/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc +++ b/test/cpp/ir/kernel_dialect/ir_kernel_dialect_pass_test.cc @@ -26,7 +26,6 @@ #include "paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_dialect.h" #include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/kernel_op.h" -#include "paddle/fluid/ir/dialect/paddle_kernel_dialect/ir/legacy_kernel_op.h" #include "paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_adaptor.h" #include "paddle/fluid/ir/transforms/pd_op_to_kernel_pass.h" #include "paddle/fluid/platform/init.h" From 8aaceba5dc0c73eb4ab047047ef1ece23a81f7d6 Mon Sep 17 00:00:00 2001 From: iLeGend <824040212@qq.com> Date: Fri, 8 Sep 2023 10:31:20 +0800 Subject: [PATCH 192/194] [xdoctest][task 238] reformat example code with google style in `python/paddle/distributed/fleet/launch_utils.py` (#57060) * [Doctest]fix No.238, test=docs_preview * fix * fix --- python/paddle/distributed/fleet/launch_utils.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/python/paddle/distributed/fleet/launch_utils.py b/python/paddle/distributed/fleet/launch_utils.py index 45774f5e8c6ab..41179c4ea8d8a 100755 --- a/python/paddle/distributed/fleet/launch_utils.py +++ b/python/paddle/distributed/fleet/launch_utils.py @@ -370,11 +370,16 @@ def get_host_name_ip(): def add_arguments(argname, type, default, help, argparser, **kwargs): """Add argparse's argument. - Usage: - .. code-block:: python - parser = argparse.ArgumentParser() - add_argument("name", str, "Jonh", "User name.", parser) - args = parser.parse_args() + + Examples: + .. code-block:: python + + >>> import argparse + >>> from paddle.distributed.fleet.launch_utils import add_arguments + >>> parser = argparse.ArgumentParser() + >>> add_arguments("name", str, "Jonh", "User name.", parser) + >>> args = parser.parse_args() + """ type = strtobool if type == bool else type argparser.add_argument( From fc71459fea434f01f4c40dfe96f520fd5a0b894c Mon Sep 17 00:00:00 2001 From: David Nicolas <37790151+liyongchao911@users.noreply.github.com> Date: Fri, 8 Sep 2023 10:32:35 +0800 Subject: [PATCH 193/194] [xdoctest][task 167-170] reformat example code with google style in /paddle/vision/datasets/*; test=docs_preview (#56906) * reformat example code with google style * udpate * update * add timeout for dataset download * update cifar timeout * update cifar timeout and fix an output * update cifar timeout * add a blank line --------- Co-authored-by: SigureMo --- python/paddle/amp/__init__.py | 14 +- python/paddle/vision/datasets/cifar.py | 157 +++--- python/paddle/vision/datasets/flowers.py | 78 ++- python/paddle/vision/transforms/transforms.py | 526 +++++++++--------- 4 files changed, 386 insertions(+), 389 deletions(-) diff --git a/python/paddle/amp/__init__.py b/python/paddle/amp/__init__.py index e0cc1f3a592f8..615e6c4f36d55 100644 --- a/python/paddle/amp/__init__.py +++ b/python/paddle/amp/__init__.py @@ -53,10 +53,11 @@ def is_float16_supported(device=None): Examples: - .. code-block:: python + .. code-block:: python - import paddle - paddle.amp.is_float16_supported() # True or False + >>> import paddle + >>> paddle.amp.is_float16_supported() # True or False + False """ device = ( @@ -79,10 +80,11 @@ def is_bfloat16_supported(device=None): Examples: - .. code-block:: python + .. code-block:: python - import paddle - paddle.amp.is_bfloat16_supported() # True or False + >>> import paddle + >>> paddle.amp.is_bfloat16_supported() # True or False + True """ device = ( diff --git a/python/paddle/vision/datasets/cifar.py b/python/paddle/vision/datasets/cifar.py index 41a96fef16d27..a787f1479a28a 100644 --- a/python/paddle/vision/datasets/cifar.py +++ b/python/paddle/vision/datasets/cifar.py @@ -61,46 +61,46 @@ class Cifar10(Dataset): .. code-block:: python - import itertools - import paddle.vision.transforms as T - from paddle.vision.datasets import Cifar10 - - - cifar10 = Cifar10() - print(len(cifar10)) - # 50000 - - for i in range(5): # only show first 5 images - img, label = cifar10[i] - # do something with img and label - print(type(img), img.size, label) - # (32, 32) 6 - - - transform = T.Compose( - [ - T.Resize(64), - T.ToTensor(), - T.Normalize( - mean=[0.5, 0.5, 0.5], - std=[0.5, 0.5, 0.5], - to_rgb=True, - ), - ] - ) - - cifar10_test = Cifar10( - mode="test", - transform=transform, # apply transform to every image - backend="cv2", # use OpenCV as image transform backend - ) - print(len(cifar10_test)) - # 10000 + >>> # doctest: +TIMEOUT(60) + >>> import itertools + >>> import paddle.vision.transforms as T + >>> from paddle.vision.datasets import Cifar10 + + >>> cifar10 = Cifar10() + >>> print(len(cifar10)) + 50000 + + >>> for i in range(5): # only show first 5 images + ... img, label = cifar10[i] + ... # do something with img and label + ... print(type(img), img.size, label) + ... # (32, 32) 6 + + + >>> transform = T.Compose( + ... [ + ... T.Resize(64), + ... T.ToTensor(), + ... T.Normalize( + ... mean=[0.5, 0.5, 0.5], + ... std=[0.5, 0.5, 0.5], + ... to_rgb=True, + ... ), + ... ] + ... ) + >>> cifar10_test = Cifar10( + ... mode="test", + ... transform=transform, # apply transform to every image + ... backend="cv2", # use OpenCV as image transform backend + ... ) + >>> print(len(cifar10_test)) + 10000 + + >>> for img, label in itertools.islice(iter(cifar10_test), 5): # only show first 5 images + ... # do something with img and label + ... print(type(img), img.shape, label) + ... # [3, 64, 64] 3 - for img, label in itertools.islice(iter(cifar10_test), 5): # only show first 5 images - # do something with img and label - print(type(img), img.shape, label) - # [3, 64, 64] 3 """ def __init__( @@ -210,46 +210,47 @@ class Cifar100(Cifar10): .. code-block:: python - import itertools - import paddle.vision.transforms as T - from paddle.vision.datasets import Cifar100 - - - cifar100 = Cifar100() - print(len(cifar100)) - # 50000 - - for i in range(5): # only show first 5 images - img, label = cifar100[i] - # do something with img and label - print(type(img), img.size, label) - # (32, 32) 19 - - - transform = T.Compose( - [ - T.Resize(64), - T.ToTensor(), - T.Normalize( - mean=[0.5, 0.5, 0.5], - std=[0.5, 0.5, 0.5], - to_rgb=True, - ), - ] - ) - - cifar100_test = Cifar100( - mode="test", - transform=transform, # apply transform to every image - backend="cv2", # use OpenCV as image transform backend - ) - print(len(cifar100_test)) - # 10000 + >>> # doctest: +TIMEOUT(60) + >>> import itertools + >>> import paddle.vision.transforms as T + >>> from paddle.vision.datasets import Cifar100 + + >>> cifar100 = Cifar100() + >>> print(len(cifar100)) + 50000 + + >>> for i in range(5): # only show first 5 images + ... img, label = cifar100[i] + ... # do something with img and label + ... print(type(img), img.size, label) + ... # (32, 32) 19 + + + >>> transform = T.Compose( + ... [ + ... T.Resize(64), + ... T.ToTensor(), + ... T.Normalize( + ... mean=[0.5, 0.5, 0.5], + ... std=[0.5, 0.5, 0.5], + ... to_rgb=True, + ... ), + ... ] + ... ) + + >>> cifar100_test = Cifar100( + ... mode="test", + ... transform=transform, # apply transform to every image + ... backend="cv2", # use OpenCV as image transform backend + ... ) + >>> print(len(cifar100_test)) + 10000 + + >>> for img, label in itertools.islice(iter(cifar100_test), 5): # only show first 5 images + ... # do something with img and label + ... print(type(img), img.shape, label) + ... # [3, 64, 64] 49 - for img, label in itertools.islice(iter(cifar100_test), 5): # only show first 5 images - # do something with img and label - print(type(img), img.shape, label) - # [3, 64, 64] 49 """ def __init__( diff --git a/python/paddle/vision/datasets/flowers.py b/python/paddle/vision/datasets/flowers.py index 8b9c1d3a926df..94ff24850a8ef 100644 --- a/python/paddle/vision/datasets/flowers.py +++ b/python/paddle/vision/datasets/flowers.py @@ -65,46 +65,44 @@ class Flowers(Dataset): .. code-block:: python - import itertools - import paddle.vision.transforms as T - from paddle.vision.datasets import Flowers - - - flowers = Flowers() - print(len(flowers)) - # 6149 - - for i in range(5): # only show first 5 images - img, label = flowers[i] - # do something with img and label - print(type(img), img.size, label) - # (523, 500) [1] - - - transform = T.Compose( - [ - T.Resize(64), - T.ToTensor(), - T.Normalize( - mean=[0.5, 0.5, 0.5], - std=[0.5, 0.5, 0.5], - to_rgb=True, - ), - ] - ) - - flowers_test = Flowers( - mode="test", - transform=transform, # apply transform to every image - backend="cv2", # use OpenCV as image transform backend - ) - print(len(flowers_test)) - # 1020 - - for img, label in itertools.islice(iter(flowers_test), 5): # only show first 5 images - # do something with img and label - print(type(img), img.shape, label) - # [3, 64, 96] [1] + >>> # doctest: +TIMEOUT(60) + >>> import itertools + >>> import paddle.vision.transforms as T + >>> from paddle.vision.datasets import Flowers + + >>> flowers = Flowers() + >>> print(len(flowers)) + 6149 + + >>> for i in range(5): # only show first 5 images + ... img, label = flowers[i] + ... # do something with img and label + ... print(type(img), img.size, label) + ... # (523, 500) [1] + + >>> transform = T.Compose( + ... [ + ... T.Resize(64), + ... T.ToTensor(), + ... T.Normalize( + ... mean=[0.5, 0.5, 0.5], + ... std=[0.5, 0.5, 0.5], + ... to_rgb=True, + ... ), + ... ] + ... ) + >>> flowers_test = Flowers( + ... mode="test", + ... transform=transform, # apply transform to every image + ... backend="cv2", # use OpenCV as image transform backend + ... ) + >>> print(len(flowers_test)) + 1020 + + >>> for img, label in itertools.islice(iter(flowers_test), 5): # only show first 5 images + ... # do something with img and label + ... print(type(img), img.shape, label) + ... # [3, 64, 96] [1] """ def __init__( diff --git a/python/paddle/vision/transforms/transforms.py b/python/paddle/vision/transforms/transforms.py index 70efff7c6db1b..eebccb4c8decf 100644 --- a/python/paddle/vision/transforms/transforms.py +++ b/python/paddle/vision/transforms/transforms.py @@ -91,16 +91,16 @@ class Compose: .. code-block:: python - from paddle.vision.datasets import Flowers - from paddle.vision.transforms import Compose, ColorJitter, Resize - - transform = Compose([ColorJitter(), Resize(size=608)]) - flowers = Flowers(mode='test', transform=transform) - - for i in range(10): - sample = flowers[i] - print(sample[0].size, sample[1]) - + >>> from paddle.vision.datasets import Flowers + >>> from paddle.vision.transforms import Compose, ColorJitter, Resize + >>> transform = Compose([ColorJitter(), Resize(size=608)]) + >>> flowers = Flowers(mode='test', transform=transform) + >>> for i in range(3): + ... sample = flowers[i] + ... print(sample[0].size, sample[1]) + (916, 608) [1] + (758, 608) [1] + (811, 608) [1] """ def __init__(self, transforms): @@ -166,72 +166,72 @@ class BaseTransform: .. code-block:: python - import numpy as np - from PIL import Image - import paddle.vision.transforms.functional as F - from paddle.vision.transforms import BaseTransform - - def _get_image_size(img): - if F._is_pil_image(img): - return img.size - elif F._is_numpy_image(img): - return img.shape[:2][::-1] - else: - raise TypeError("Unexpected type {}".format(type(img))) - - class CustomRandomFlip(BaseTransform): - def __init__(self, prob=0.5, keys=None): - super().__init__(keys) - self.prob = prob - - def _get_params(self, inputs): - image = inputs[self.keys.index('image')] - params = {} - params['flip'] = np.random.random() < self.prob - params['size'] = _get_image_size(image) - return params - - def _apply_image(self, image): - if self.params['flip']: - return F.hflip(image) - return image - - # if you only want to transform image, do not need to rewrite this function - def _apply_coords(self, coords): - if self.params['flip']: - w = self.params['size'][0] - coords[:, 0] = w - coords[:, 0] - return coords - - # if you only want to transform image, do not need to rewrite this function - def _apply_boxes(self, boxes): - idxs = np.array([(0, 1), (2, 1), (0, 3), (2, 3)]).flatten() - coords = np.asarray(boxes).reshape(-1, 4)[:, idxs].reshape(-1, 2) - coords = self._apply_coords(coords).reshape((-1, 4, 2)) - minxy = coords.min(axis=1) - maxxy = coords.max(axis=1) - trans_boxes = np.concatenate((minxy, maxxy), axis=1) - return trans_boxes - - # if you only want to transform image, do not need to rewrite this function - def _apply_mask(self, mask): - if self.params['flip']: - return F.hflip(mask) - return mask - - # create fake inputs - fake_img = Image.fromarray((np.random.rand(400, 500, 3) * 255.).astype('uint8')) - fake_boxes = np.array([[2, 3, 200, 300], [50, 60, 80, 100]]) - fake_mask = fake_img.convert('L') - - # only transform for image: - flip_transform = CustomRandomFlip(1.0) - converted_img = flip_transform(fake_img) - - # transform for image, boxes and mask - flip_transform = CustomRandomFlip(1.0, keys=('image', 'boxes', 'mask')) - (converted_img, converted_boxes, converted_mask) = flip_transform((fake_img, fake_boxes, fake_mask)) - print('converted boxes', converted_boxes) + >>> import numpy as np + >>> from PIL import Image + >>> import paddle.vision.transforms.functional as F + >>> from paddle.vision.transforms import BaseTransform + + >>> def _get_image_size(img): + ... if F._is_pil_image(img): + ... return img.size + ... elif F._is_numpy_image(img): + ... return img.shape[:2][::-1] + ... else: + ... raise TypeError("Unexpected type {}".format(type(img))) + ... + >>> class CustomRandomFlip(BaseTransform): + ... def __init__(self, prob=0.5, keys=None): + ... super().__init__(keys) + ... self.prob = prob + ... + ... def _get_params(self, inputs): + ... image = inputs[self.keys.index('image')] + ... params = {} + ... params['flip'] = np.random.random() < self.prob + ... params['size'] = _get_image_size(image) + ... return params + ... + ... def _apply_image(self, image): + ... if self.params['flip']: + ... return F.hflip(image) + ... return image + ... + ... # if you only want to transform image, do not need to rewrite this function + ... def _apply_coords(self, coords): + ... if self.params['flip']: + ... w = self.params['size'][0] + ... coords[:, 0] = w - coords[:, 0] + ... return coords + ... + ... # if you only want to transform image, do not need to rewrite this function + ... def _apply_boxes(self, boxes): + ... idxs = np.array([(0, 1), (2, 1), (0, 3), (2, 3)]).flatten() + ... coords = np.asarray(boxes).reshape(-1, 4)[:, idxs].reshape(-1, 2) + ... coords = self._apply_coords(coords).reshape((-1, 4, 2)) + ... minxy = coords.min(axis=1) + ... maxxy = coords.max(axis=1) + ... trans_boxes = np.concatenate((minxy, maxxy), axis=1) + ... return trans_boxes + ... + ... # if you only want to transform image, do not need to rewrite this function + ... def _apply_mask(self, mask): + ... if self.params['flip']: + ... return F.hflip(mask) + ... return mask + ... + >>> # create fake inputs + >>> fake_img = Image.fromarray((np.random.rand(400, 500, 3) * 255.).astype('uint8')) + >>> fake_boxes = np.array([[2, 3, 200, 300], [50, 60, 80, 100]]) + >>> fake_mask = fake_img.convert('L') + >>> # only transform for image: + >>> flip_transform = CustomRandomFlip(1.0) + >>> converted_img = flip_transform(fake_img) + >>> # transform for image, boxes and mask + >>> flip_transform = CustomRandomFlip(1.0, keys=('image', 'boxes', 'mask')) + >>> (converted_img, converted_boxes, converted_mask) = flip_transform((fake_img, fake_boxes, fake_mask)) + >>> converted_boxes + array([[300, 3, 498, 300], + [420, 60, 450, 100]]) """ @@ -319,23 +319,18 @@ class ToTensor(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - - import paddle.vision.transforms as T - import paddle.vision.transforms.functional as F - - fake_img = Image.fromarray((np.random.rand(4, 5, 3) * 255.).astype(np.uint8)) - - transform = T.ToTensor() - - tensor = transform(fake_img) - - print(tensor.shape) - # [3, 4, 5] - - print(tensor.dtype) - # paddle.float32 + >>> import numpy as np + >>> from PIL import Image + >>> import paddle.vision.transforms as T + >>> import paddle.vision.transforms.functional as F + + >>> fake_img = Image.fromarray((np.random.rand(4, 5, 3) * 255.).astype(np.uint8)) + >>> transform = T.ToTensor() + >>> tensor = transform(fake_img) + >>> print(tensor.shape) + [3, 4, 5] + >>> print(tensor.dtype) + paddle.float32 """ def __init__(self, data_format='CHW', keys=None): @@ -389,21 +384,19 @@ class Resize(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import Resize - - fake_img = Image.fromarray((np.random.rand(256, 300, 3) * 255.).astype(np.uint8)) - - transform = Resize(size=224) - converted_img = transform(fake_img) - print(converted_img.size) - # (262, 224) - - transform = Resize(size=(200,150)) - converted_img = transform(fake_img) - print(converted_img.size) - # (150, 200) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import Resize + + >>> fake_img = Image.fromarray((np.random.rand(256, 300, 3) * 255.).astype(np.uint8)) + >>> transform = Resize(size=224) + >>> converted_img = transform(fake_img) + >>> print(converted_img.size) + (262, 224) + >>> transform = Resize(size=(200,150)) + >>> converted_img = transform(fake_img) + >>> print(converted_img.size) + (150, 200) """ def __init__(self, size, interpolation='bilinear', keys=None): @@ -456,16 +449,15 @@ class RandomResizedCrop(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import RandomResizedCrop - - transform = RandomResizedCrop(224) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import RandomResizedCrop - fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) - print(fake_img.size) + >>> transform = RandomResizedCrop(224) + >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) """ @@ -643,16 +635,16 @@ class CenterCrop(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import CenterCrop - - transform = CenterCrop(224) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import CenterCrop - fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> transform = CenterCrop(224) + >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) - fake_img = transform(fake_img) - print(fake_img.size) """ def __init__(self, size, keys=None): @@ -684,16 +676,15 @@ class RandomHorizontalFlip(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import RandomHorizontalFlip - - transform = RandomHorizontalFlip(0.5) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import RandomHorizontalFlip - fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) - print(fake_img.size) + >>> transform = RandomHorizontalFlip(0.5) + >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (320, 300) """ def __init__(self, prob=0.5, keys=None): @@ -738,16 +729,14 @@ class RandomVerticalFlip(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import RandomVerticalFlip - - transform = RandomVerticalFlip() - - fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) - print(fake_img.size) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import RandomVerticalFlip + >>> transform = RandomVerticalFlip() + >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (320, 300) """ @@ -799,21 +788,22 @@ class Normalize(BaseTransform): Examples: .. code-block:: python - :name: code-example - import paddle - from paddle.vision.transforms import Normalize - - normalize = Normalize(mean=[127.5, 127.5, 127.5], - std=[127.5, 127.5, 127.5], - data_format='HWC') - - fake_img = paddle.rand([300,320,3]).numpy() * 255. - - fake_img = normalize(fake_img) - print(fake_img.shape) - # (300, 320, 3) - print(fake_img.max(), fake_img.min()) - # 0.99999905 -0.999974 + :name: code-example + + >>> import paddle + >>> from paddle.vision.transforms import Normalize + >>> paddle.seed(2023) + + >>> normalize = Normalize(mean=[127.5, 127.5, 127.5], + ... std=[127.5, 127.5, 127.5], + ... data_format='HWC') + ... + >>> fake_img = paddle.rand([300,320,3]).numpy() * 255. + >>> fake_img = normalize(fake_img) + >>> print(fake_img.shape) + (300, 320, 3) + >>> print(fake_img.max(), fake_img.min()) + 0.99999464 -0.9999929 """ @@ -860,16 +850,15 @@ class Transpose(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import Transpose - - transform = Transpose() - - fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import Transpose - fake_img = transform(fake_img) - print(fake_img.shape) + >>> transform = Transpose() + >>> fake_img = Image.fromarray((np.random.rand(300, 320, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.shape) + (3, 300, 320) """ @@ -908,15 +897,19 @@ class BrightnessTransform(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import BrightnessTransform + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import BrightnessTransform + >>> np.random.seed(2023) - transform = BrightnessTransform(0.4) - - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) + >>> transform = BrightnessTransform(0.4) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> print(fake_img.load()[1,1]) + (60, 169, 34) + >>> # doctest: +SKIP('random sample in Brightness function') + >>> fake_img = transform(fake_img) + >>> print(fake_img.load()[1,1]) + (68, 192, 38) """ @@ -951,15 +944,15 @@ class ContrastTransform(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import ContrastTransform - - transform = ContrastTransform(0.4) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import ContrastTransform - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) + >>> transform = ContrastTransform(0.4) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) """ @@ -996,16 +989,15 @@ class SaturationTransform(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import SaturationTransform - - transform = SaturationTransform(0.4) - - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import SaturationTransform + >>> transform = SaturationTransform(0.4) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) """ def __init__(self, value, keys=None): @@ -1039,15 +1031,15 @@ class HueTransform(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import HueTransform - - transform = HueTransform(0.4) - - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import HueTransform - fake_img = transform(fake_img) + >>> transform = HueTransform(0.4) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) """ @@ -1090,15 +1082,15 @@ class ColorJitter(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import ColorJitter + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import ColorJitter - transform = ColorJitter(0.4, 0.4, 0.4, 0.4) - - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) + >>> transform = ColorJitter(0.4, 0.4, 0.4, 0.4) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (224, 224) """ @@ -1195,17 +1187,19 @@ class RandomCrop(BaseTransform): Examples: .. code-block:: python - :name: code-example1 + :name: code-example1 - import paddle - from paddle.vision.transforms import RandomCrop - transform = RandomCrop(224) + >>> import paddle + >>> from paddle.vision.transforms import RandomCrop + >>> transform = RandomCrop(224) - fake_img = paddle.randint(0, 255, shape=(3, 324,300), dtype = 'int32') - print(fake_img.shape) # [3, 324, 300] + >>> fake_img = paddle.randint(0, 255, shape=(3, 324,300), dtype = 'int32') + >>> print(fake_img.shape) + [3, 324, 300] - crop_img = transform(fake_img) - print(crop_img.shape) # [3, 224, 224] + >>> crop_img = transform(fake_img) + >>> print(crop_img.shape) + [3, 224, 224] """ def __init__( @@ -1313,16 +1307,15 @@ class Pad(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import Pad - - transform = Pad(2) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import Pad - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) - print(fake_img.size) + >>> transform = Pad(2) + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (228, 228) """ def __init__(self, padding, fill=0, padding_mode='constant', keys=None): @@ -1429,15 +1422,14 @@ class RandomAffine(BaseTransform): .. code-block:: python - import paddle - from paddle.vision.transforms import RandomAffine - - transform = RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 10]) - - fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) + >>> import paddle + >>> from paddle.vision.transforms import RandomAffine - fake_img = transform(fake_img) - print(fake_img.shape) + >>> transform = RandomAffine([-90, 90], translate=[0.2, 0.2], scale=[0.5, 0.5], shear=[-10, 10]) + >>> fake_img = paddle.randn((3, 256, 300)).astype(paddle.float32) + >>> fake_img = transform(fake_img) + >>> print(fake_img.shape) + [3, 256, 300] """ def __init__( @@ -1583,16 +1575,15 @@ class RandomRotation(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import RandomRotation + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import RandomRotation - transform = RandomRotation(90) - - fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8)) - - fake_img = transform(fake_img) - print(fake_img.size) + >>> transform = RandomRotation(90) + >>> fake_img = Image.fromarray((np.random.rand(200, 150, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(fake_img.size) + (150, 200) """ def __init__( @@ -1683,15 +1674,14 @@ class RandomPerspective(BaseTransform): .. code-block:: python - import paddle - from paddle.vision.transforms import RandomPerspective - - transform = RandomPerspective(prob=1.0, distortion_scale=0.9) + >>> import paddle + >>> from paddle.vision.transforms import RandomPerspective - fake_img = paddle.randn((3, 200, 150)).astype(paddle.float32) - - fake_img = transform(fake_img) - print(fake_img.shape) + >>> transform = RandomPerspective(prob=1.0, distortion_scale=0.9) + >>> fake_img = paddle.randn((3, 200, 150)).astype(paddle.float32) + >>> fake_img = transform(fake_img) + >>> print(fake_img.shape) + [3, 200, 150] """ def __init__( @@ -1806,16 +1796,15 @@ class Grayscale(BaseTransform): .. code-block:: python - import numpy as np - from PIL import Image - from paddle.vision.transforms import Grayscale - - transform = Grayscale() - - fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> import numpy as np + >>> from PIL import Image + >>> from paddle.vision.transforms import Grayscale - fake_img = transform(fake_img) - print(np.array(fake_img).shape) + >>> transform = Grayscale() + >>> fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) + >>> fake_img = transform(fake_img) + >>> print(np.array(fake_img).shape) + (224, 224) """ def __init__(self, num_output_channels=1, keys=None): @@ -1861,13 +1850,20 @@ class RandomErasing(BaseTransform): .. code-block:: python - import paddle + >>> import paddle - fake_img = paddle.randn((3, 10, 10)).astype(paddle.float32) - transform = paddle.vision.transforms.RandomErasing() - result = transform(fake_img) + >>> fake_img = paddle.randn((1, 5, 5)).astype(paddle.float32) + >>> transform = paddle.vision.transforms.RandomErasing() + >>> result = transform(fake_img) + >>> # doctest: +SKIP('random sample') + >>> print(result) + Tensor(shape=[1, 5, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[[-0.22141267, -0.71004093, 1.71224928, 2.99622107, -0.82959402], + [ 0.36916021, -0.25601348, 0.86669374, 1.27504587, -0.56462914], + [-0.45704395, -0.87613666, 1.12195814, -0.87974882, 0.04902615], + [-0.91549885, -0.15066874, 1.26381516, 0. , 0. ], + [ 0.87887472, -1.59914243, -0.73970413, 0. , 0. ]]]) - print(result) """ def __init__( From ad0e7a2603349aed0bb1ef9798809ec5221807ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=89=91=E5=AF=92?= Date: Fri, 8 Sep 2023 10:39:12 +0800 Subject: [PATCH 194/194] =?UTF-8?q?=E3=80=90CINN=E3=80=91Integate=20cast?= =?UTF-8?q?=5Fsimplify=20into=20ir=5Fsimplify=20(#56958)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * integate cast_simplify into ir_simplify * fix cast simplify testcase --- paddle/cinn/common/cas.cc | 1 - paddle/cinn/common/ir_util.cc | 3 +- paddle/cinn/optim/CMakeLists.txt | 1 - paddle/cinn/optim/cast_simplify.cc | 117 ------------------------ paddle/cinn/optim/cast_simplify.h | 31 ------- paddle/cinn/optim/cast_simplify_test.cc | 12 +-- paddle/cinn/optim/ir_simplify.cc | 90 +++++++++++++++++- paddle/cinn/optim/ir_simplify.h | 2 + paddle/cinn/optim/optimize.cc | 1 - 9 files changed, 96 insertions(+), 162 deletions(-) delete mode 100644 paddle/cinn/optim/cast_simplify.cc delete mode 100644 paddle/cinn/optim/cast_simplify.h diff --git a/paddle/cinn/common/cas.cc b/paddle/cinn/common/cas.cc index b72650301bbfe..6264c5b12d453 100644 --- a/paddle/cinn/common/cas.cc +++ b/paddle/cinn/common/cas.cc @@ -27,7 +27,6 @@ #include "paddle/cinn/ir/utils/ir_nodes_collector.h" #include "paddle/cinn/ir/utils/ir_printer.h" #include "paddle/cinn/ir/utils/ir_visitor.h" -#include "paddle/cinn/optim/cast_simplify.h" #include "paddle/cinn/utils/string.h" namespace cinn { diff --git a/paddle/cinn/common/ir_util.cc b/paddle/cinn/common/ir_util.cc index 97d56f2ceaeb4..f0f219ee105f7 100644 --- a/paddle/cinn/common/ir_util.cc +++ b/paddle/cinn/common/ir_util.cc @@ -21,7 +21,6 @@ #include "paddle/cinn/ir/op/ir_operators.h" #include "paddle/cinn/ir/utils/ir_mutator.h" #include "paddle/cinn/ir/utils/ir_printer.h" -#include "paddle/cinn/optim/cast_simplify.h" namespace cinn { namespace common { @@ -147,7 +146,7 @@ Expr IndiceToAbsOffset(const std::vector &shape, for (int i = 0; i < shape.size(); i++) { CHECK_EQ(shape[i].type(), Int(32)); Expr indice_prod = indices[i]; - optim::CastSimplify(&indice_prod); + optim::SimplifyCast(&indice_prod); for (int j = i + 1; j < shape.size(); j++) { indice_prod = RampRelatedMul(indice_prod, shape[j]); } diff --git a/paddle/cinn/optim/CMakeLists.txt b/paddle/cinn/optim/CMakeLists.txt index 45c38c2632717..99ae9cf3bd3d6 100755 --- a/paddle/cinn/optim/CMakeLists.txt +++ b/paddle/cinn/optim/CMakeLists.txt @@ -23,7 +23,6 @@ gather_srcs( compute_inline_expand.cc buffer_assign.cc replace_const_param_to_integer.cc - cast_simplify.cc lower_intrin.cc cast_bool_to_int8.cc collect_undefined_vars.cc diff --git a/paddle/cinn/optim/cast_simplify.cc b/paddle/cinn/optim/cast_simplify.cc deleted file mode 100644 index a6431e42e467c..0000000000000 --- a/paddle/cinn/optim/cast_simplify.cc +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) 2021 CINN Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#include "paddle/cinn/optim/cast_simplify.h" - -#include "paddle/cinn/ir/utils/ir_mutator.h" - -namespace cinn::optim { - -using cinn::common::bfloat16; -using cinn::common::float16; - -namespace { - -template -CastType NormCastValue(T value) { - if (type_of().is_uint() || type_of().is_uint()) { - // not support uint - return static_cast(value); - } - - if (std::isinf(value)) { - return std::numeric_limits::infinity(); - } else if (std::isnan(value)) { - return std::numeric_limits::signaling_NaN(); - } else if (value >= static_cast(std::numeric_limits::max())) { - return std::numeric_limits::max(); - } else if (value <= static_cast(std::numeric_limits::lowest())) { - return std::numeric_limits::lowest(); - } - return static_cast(value); -} - -struct Mutator : ir::IRMutator<> { - using ir::IRMutator<>::Visit; - - void Visit(const ir::Cast* op, Expr* expr) { - auto* node = expr->As(); - - Visit(&node->v(), &node->v()); - - if (op->type() == op->v().type()) { - *expr = op->v(); - return; - } - -#define __CAST_TO_TYPE(type__) \ - if (auto* i = op->v().As()) { \ - *expr = Expr(static_cast(i->value)); \ - } else if (auto* f = op->v().As()) { \ - *expr = Expr(static_cast(NormCastValue(f->value))); \ - } else if (auto* u = op->v().As()) { \ - *expr = Expr(static_cast(u->value)); \ - } else { \ - CINN_NOT_IMPLEMENTED \ - } - - if (op->v().is_constant()) { - if (op->type() == type_of()) { - __CAST_TO_TYPE(int8_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(int16_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(int32_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(int64_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint8_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint16_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint32_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint64_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(float) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(double) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(bool) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint32_t) - } else if (op->type() == type_of()) { - __CAST_TO_TYPE(uint64_t) - } else if (op->type() == type_of()) { - // Cannot simplify!!! pass - __CAST_TO_TYPE(bfloat16) - } else if (op->type() == type_of()) { - // Cannot simplify!!! pass - __CAST_TO_TYPE(float16) - } else { - CINN_NOT_IMPLEMENTED - } - } -#undef __CAST_TO_TYPE - } -}; - -} // namespace - -void CastSimplify(Expr* e) { - Mutator mutator; - mutator.Visit(e, e); -} - -} // namespace cinn::optim diff --git a/paddle/cinn/optim/cast_simplify.h b/paddle/cinn/optim/cast_simplify.h deleted file mode 100644 index 072f39783d187..0000000000000 --- a/paddle/cinn/optim/cast_simplify.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 CINN Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// 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. - -#pragma once - -#include "paddle/cinn/ir/ir.h" - -namespace cinn::optim { - -/** - * Simplify the Cast nodes. - * - * There are several patterns: - * 1. the source and target type are the same, drop the Cast node - * 2. for intermediate numbers, just replace the Cast node with a Node of the - * target type - */ -void CastSimplify(Expr* e); - -} // namespace cinn::optim diff --git a/paddle/cinn/optim/cast_simplify_test.cc b/paddle/cinn/optim/cast_simplify_test.cc index 5b7a21bae86f4..57103db27f397 100644 --- a/paddle/cinn/optim/cast_simplify_test.cc +++ b/paddle/cinn/optim/cast_simplify_test.cc @@ -12,13 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/cinn/optim/cast_simplify.h" - #include #include "paddle/cinn/ir/op/ir_operators.h" #include "paddle/cinn/ir/utils/ir_printer.h" - +#include "paddle/cinn/optim/ir_simplify.h" namespace cinn::optim { TEST(CastSimplify, same_type) { @@ -26,7 +24,7 @@ TEST(CastSimplify, same_type) { Expr a = ir::Cast::Make(Int(32), n); LOG(INFO) << n->type(); LOG(INFO) << a; - CastSimplify(&a); + SimplifyCast(&a); ASSERT_EQ(utils::GetStreamCnt(a), "n"); } @@ -34,7 +32,7 @@ TEST(CastSimplify, Imm_int) { Expr a = ir::Cast::Make(Int(64), Expr(1)); Expr c = ir::Cast::Make(Int(32), a); LOG(INFO) << c; - CastSimplify(&c); + SimplifyCast(&c); LOG(INFO) << c; ASSERT_EQ(utils::GetStreamCnt(c), "1"); ASSERT_EQ(c.type(), Int(32)); @@ -44,7 +42,7 @@ TEST(CastSimplify, Imm_double) { Expr a = ir::Cast::Make(Float(64), Expr(2.33)); Expr c = ir::Cast::Make(Int(32), a); LOG(INFO) << c; - CastSimplify(&c); + SimplifyCast(&c); LOG(INFO) << c; ASSERT_EQ(utils::GetStreamCnt(c), "2"); ASSERT_EQ(c.type(), Int(32)); @@ -54,7 +52,7 @@ TEST(CastSimplify, Imm_uint) { Expr a = ir::Cast::Make(UInt(64), Expr(1)); Expr c = ir::Cast::Make(UInt(32), a); LOG(INFO) << c; - CastSimplify(&c); + SimplifyCast(&c); LOG(INFO) << c; ASSERT_EQ(utils::GetStreamCnt(c), "1"); ASSERT_EQ(c.type(), UInt(32)); diff --git a/paddle/cinn/optim/ir_simplify.cc b/paddle/cinn/optim/ir_simplify.cc index 3c0f9298dae2c..bfed498da521d 100644 --- a/paddle/cinn/optim/ir_simplify.cc +++ b/paddle/cinn/optim/ir_simplify.cc @@ -29,13 +29,14 @@ #include "paddle/cinn/ir/utils/ir_mutator.h" #include "paddle/cinn/ir/utils/ir_printer.h" #include "paddle/cinn/ir/utils/ir_visitor.h" -#include "paddle/cinn/optim/cast_simplify.h" #include "paddle/cinn/utils/string.h" namespace cinn { namespace optim { using namespace ir; // NOLINT +using common::bfloat16; using common::ExprToGinacConverter; +using common::float16; using utils::GetStreamCnt; using utils::Replace; @@ -346,11 +347,95 @@ struct SimplifyForLoopsMutator : public ir::IRMutator<> { } }; +template +CastType NormCastValue(T value) { + if (type_of().is_uint() || type_of().is_uint()) { + // not support uint + return static_cast(value); + } + + if (std::isinf(value)) { + return std::numeric_limits::infinity(); + } else if (std::isnan(value)) { + return std::numeric_limits::signaling_NaN(); + } else if (value >= static_cast(std::numeric_limits::max())) { + return std::numeric_limits::max(); + } else if (value <= static_cast(std::numeric_limits::lowest())) { + return std::numeric_limits::lowest(); + } + return static_cast(value); +} + +struct SimplifyCastMutator : public ir::IRMutator<> { + void operator()(Expr* expr) { ir::IRMutator::Visit(expr, expr); } + + void Visit(const ir::Cast* op, Expr* expr) { + auto* node = expr->As(); + + ir::IRMutator::Visit(&node->v(), &node->v()); + + if (op->type() == op->v().type()) { + *expr = op->v(); + return; + } + +#define __CAST_TO_TYPE(type__) \ + if (auto* i = op->v().As()) { \ + *expr = Expr(static_cast(i->value)); \ + } else if (auto* f = op->v().As()) { \ + *expr = Expr(static_cast(NormCastValue(f->value))); \ + } else if (auto* u = op->v().As()) { \ + *expr = Expr(static_cast(u->value)); \ + } else { \ + CINN_NOT_IMPLEMENTED \ + } + + if (op->v().is_constant()) { + if (op->type() == type_of()) { + __CAST_TO_TYPE(int8_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(int16_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(int32_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(int64_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint8_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint16_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint32_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint64_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(float) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(double) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(bool) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint32_t) + } else if (op->type() == type_of()) { + __CAST_TO_TYPE(uint64_t) + } else if (op->type() == type_of()) { + // Cannot simplify!!! pass + __CAST_TO_TYPE(bfloat16) + } else if (op->type() == type_of()) { + // Cannot simplify!!! pass + __CAST_TO_TYPE(float16) + } else { + CINN_NOT_IMPLEMENTED + } + } +#undef __CAST_TO_TYPE + } +}; + } // namespace void Simplify(Expr* expr) { VLOG(3) << "Begin Simplify " << *expr; - optim::CastSimplify(expr); + SimplifyCastMutator()(expr); SimplifyRampMutator()(expr); SimplifyLoadMutator()(expr); SimplifyStoreMutator()(expr); @@ -363,6 +448,7 @@ void Simplify(Expr* expr) { ReplaceFracWithDivMutator()(expr); } +void SimplifyCast(Expr* expr) { SimplifyCastMutator()(expr); } void SimplifyForLoops(Expr* expr) { SimplifyForLoopsMutator()(expr); } void SimplifyBlocks(Expr* expr) { SimplifyBlocksMutator()(expr); } diff --git a/paddle/cinn/optim/ir_simplify.h b/paddle/cinn/optim/ir_simplify.h index 1d6abf1cd9a9f..b0e695af53537 100644 --- a/paddle/cinn/optim/ir_simplify.h +++ b/paddle/cinn/optim/ir_simplify.h @@ -30,6 +30,8 @@ namespace optim { */ void Simplify(Expr *expr); +void SimplifyCast(Expr *expr); + void SimplifyForLoops(Expr *expr); void SimplifyBlocks(Expr *expr); diff --git a/paddle/cinn/optim/optimize.cc b/paddle/cinn/optim/optimize.cc index 18db99df48e2b..b1e73e3c58a9b 100644 --- a/paddle/cinn/optim/optimize.cc +++ b/paddle/cinn/optim/optimize.cc @@ -19,7 +19,6 @@ #include "paddle/cinn/ir/utils/ir_printer.h" #include "paddle/cinn/optim/call_arg_list_to_pod_value.h" #include "paddle/cinn/optim/cast_bool_to_int8.h" -#include "paddle/cinn/optim/cast_simplify.h" #include "paddle/cinn/optim/eliminate_broadcast_in_forloop.h" #include "paddle/cinn/optim/extern_call_process.h" #include "paddle/cinn/optim/fold_cinn_call_arguments.h"